f8ccfd5a047752ff3fbb11bdad6b0fa134a50c68
[cascardo/linux.git] / fs / xfs / libxfs / xfs_dir2_sf.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_da_format.h"
25 #include "xfs_da_btree.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_error.h"
30 #include "xfs_dir2.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_trace.h"
33
34 /*
35  * Prototypes for internal functions.
36  */
37 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
38                                      xfs_dir2_sf_entry_t *sfep,
39                                      xfs_dir2_data_aoff_t offset,
40                                      int new_isize);
41 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
42                                      int new_isize);
43 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
44                                     xfs_dir2_sf_entry_t **sfepp,
45                                     xfs_dir2_data_aoff_t *offsetp);
46 #ifdef DEBUG
47 static void xfs_dir2_sf_check(xfs_da_args_t *args);
48 #else
49 #define xfs_dir2_sf_check(args)
50 #endif /* DEBUG */
51
52 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
53 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
54
55 /*
56  * Given a block directory (dp/block), calculate its size as a shortform (sf)
57  * directory and a header for the sf directory, if it will fit it the
58  * space currently present in the inode.  If it won't fit, the output
59  * size is too big (but not accurate).
60  */
61 int                                             /* size for sf form */
62 xfs_dir2_block_sfsize(
63         xfs_inode_t             *dp,            /* incore inode pointer */
64         xfs_dir2_data_hdr_t     *hdr,           /* block directory data */
65         xfs_dir2_sf_hdr_t       *sfhp)          /* output: header for sf form */
66 {
67         xfs_dir2_dataptr_t      addr;           /* data entry address */
68         xfs_dir2_leaf_entry_t   *blp;           /* leaf area of the block */
69         xfs_dir2_block_tail_t   *btp;           /* tail area of the block */
70         int                     count;          /* shortform entry count */
71         xfs_dir2_data_entry_t   *dep;           /* data entry in the block */
72         int                     i;              /* block entry index */
73         int                     i8count;        /* count of big-inode entries */
74         int                     isdot;          /* entry is "." */
75         int                     isdotdot;       /* entry is ".." */
76         xfs_mount_t             *mp;            /* mount structure pointer */
77         int                     namelen;        /* total name bytes */
78         xfs_ino_t               parent = 0;     /* parent inode number */
79         int                     size=0;         /* total computed size */
80         int                     has_ftype;
81         struct xfs_da_geometry  *geo;
82
83         mp = dp->i_mount;
84         geo = mp->m_dir_geo;
85
86         /*
87          * if there is a filetype field, add the extra byte to the namelen
88          * for each entry that we see.
89          */
90         has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
91
92         count = i8count = namelen = 0;
93         btp = xfs_dir2_block_tail_p(geo, hdr);
94         blp = xfs_dir2_block_leaf_p(btp);
95
96         /*
97          * Iterate over the block's data entries by using the leaf pointers.
98          */
99         for (i = 0; i < be32_to_cpu(btp->count); i++) {
100                 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
101                         continue;
102                 /*
103                  * Calculate the pointer to the entry at hand.
104                  */
105                 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
106                                 xfs_dir2_dataptr_to_off(geo, addr));
107                 /*
108                  * Detect . and .., so we can special-case them.
109                  * . is not included in sf directories.
110                  * .. is included by just the parent inode number.
111                  */
112                 isdot = dep->namelen == 1 && dep->name[0] == '.';
113                 isdotdot =
114                         dep->namelen == 2 &&
115                         dep->name[0] == '.' && dep->name[1] == '.';
116
117                 if (!isdot)
118                         i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
119
120                 /* take into account the file type field */
121                 if (!isdot && !isdotdot) {
122                         count++;
123                         namelen += dep->namelen + has_ftype;
124                 } else if (isdotdot)
125                         parent = be64_to_cpu(dep->inumber);
126                 /*
127                  * Calculate the new size, see if we should give up yet.
128                  */
129                 size = xfs_dir2_sf_hdr_size(i8count) +  /* header */
130                        count * 3 * sizeof(u8) +         /* namelen + offset */
131                        namelen +                        /* name */
132                        (i8count ?                       /* inumber */
133                                 (uint)sizeof(xfs_dir2_ino8_t) * count :
134                                 (uint)sizeof(xfs_dir2_ino4_t) * count);
135                 if (size > XFS_IFORK_DSIZE(dp))
136                         return size;            /* size value is a failure */
137         }
138         /*
139          * Create the output header, if it worked.
140          */
141         sfhp->count = count;
142         sfhp->i8count = i8count;
143         dp->d_ops->sf_put_parent_ino(sfhp, parent);
144         return size;
145 }
146
147 /*
148  * Convert a block format directory to shortform.
149  * Caller has already checked that it will fit, and built us a header.
150  */
151 int                                             /* error */
152 xfs_dir2_block_to_sf(
153         xfs_da_args_t           *args,          /* operation arguments */
154         struct xfs_buf          *bp,
155         int                     size,           /* shortform directory size */
156         xfs_dir2_sf_hdr_t       *sfhp)          /* shortform directory hdr */
157 {
158         xfs_dir2_data_hdr_t     *hdr;           /* block header */
159         xfs_dir2_block_tail_t   *btp;           /* block tail pointer */
160         xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
161         xfs_inode_t             *dp;            /* incore directory inode */
162         xfs_dir2_data_unused_t  *dup;           /* unused data pointer */
163         char                    *endptr;        /* end of data entries */
164         int                     error;          /* error return value */
165         int                     logflags;       /* inode logging flags */
166         xfs_mount_t             *mp;            /* filesystem mount point */
167         char                    *ptr;           /* current data pointer */
168         xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
169         xfs_dir2_sf_hdr_t       *sfp;           /* shortform directory header */
170         xfs_dir2_sf_hdr_t       *dst;           /* temporary data buffer */
171
172         trace_xfs_dir2_block_to_sf(args);
173
174         dp = args->dp;
175         mp = dp->i_mount;
176
177         /*
178          * allocate a temporary destination buffer the size of the inode
179          * to format the data into. Once we have formatted the data, we
180          * can free the block and copy the formatted data into the inode literal
181          * area.
182          */
183         dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
184         hdr = bp->b_addr;
185
186         /*
187          * Copy the header into the newly allocate local space.
188          */
189         sfp = (xfs_dir2_sf_hdr_t *)dst;
190         memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
191
192         /*
193          * Set up to loop over the block's entries.
194          */
195         btp = xfs_dir2_block_tail_p(args->geo, hdr);
196         ptr = (char *)dp->d_ops->data_entry_p(hdr);
197         endptr = (char *)xfs_dir2_block_leaf_p(btp);
198         sfep = xfs_dir2_sf_firstentry(sfp);
199         /*
200          * Loop over the active and unused entries.
201          * Stop when we reach the leaf/tail portion of the block.
202          */
203         while (ptr < endptr) {
204                 /*
205                  * If it's unused, just skip over it.
206                  */
207                 dup = (xfs_dir2_data_unused_t *)ptr;
208                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
209                         ptr += be16_to_cpu(dup->length);
210                         continue;
211                 }
212                 dep = (xfs_dir2_data_entry_t *)ptr;
213                 /*
214                  * Skip .
215                  */
216                 if (dep->namelen == 1 && dep->name[0] == '.')
217                         ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
218                 /*
219                  * Skip .., but make sure the inode number is right.
220                  */
221                 else if (dep->namelen == 2 &&
222                          dep->name[0] == '.' && dep->name[1] == '.')
223                         ASSERT(be64_to_cpu(dep->inumber) ==
224                                dp->d_ops->sf_get_parent_ino(sfp));
225                 /*
226                  * Normal entry, copy it into shortform.
227                  */
228                 else {
229                         sfep->namelen = dep->namelen;
230                         xfs_dir2_sf_put_offset(sfep,
231                                 (xfs_dir2_data_aoff_t)
232                                 ((char *)dep - (char *)hdr));
233                         memcpy(sfep->name, dep->name, dep->namelen);
234                         dp->d_ops->sf_put_ino(sfp, sfep,
235                                               be64_to_cpu(dep->inumber));
236                         dp->d_ops->sf_put_ftype(sfep,
237                                         dp->d_ops->data_get_ftype(dep));
238
239                         sfep = dp->d_ops->sf_nextentry(sfp, sfep);
240                 }
241                 ptr += dp->d_ops->data_entsize(dep->namelen);
242         }
243         ASSERT((char *)sfep - (char *)sfp == size);
244
245         /* now we are done with the block, we can shrink the inode */
246         logflags = XFS_ILOG_CORE;
247         error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
248         if (error) {
249                 ASSERT(error != -ENOSPC);
250                 goto out;
251         }
252
253         /*
254          * The buffer is now unconditionally gone, whether
255          * xfs_dir2_shrink_inode worked or not.
256          *
257          * Convert the inode to local format and copy the data in.
258          */
259         ASSERT(dp->i_df.if_bytes == 0);
260         xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
261         dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
262         dp->i_d.di_size = size;
263
264         logflags |= XFS_ILOG_DDATA;
265         xfs_dir2_sf_check(args);
266 out:
267         xfs_trans_log_inode(args->trans, dp, logflags);
268         kmem_free(dst);
269         return error;
270 }
271
272 /*
273  * Add a name to a shortform directory.
274  * There are two algorithms, "easy" and "hard" which we decide on
275  * before changing anything.
276  * Convert to block form if necessary, if the new entry won't fit.
277  */
278 int                                             /* error */
279 xfs_dir2_sf_addname(
280         xfs_da_args_t           *args)          /* operation arguments */
281 {
282         xfs_inode_t             *dp;            /* incore directory inode */
283         int                     error;          /* error return value */
284         int                     incr_isize;     /* total change in size */
285         int                     new_isize;      /* di_size after adding name */
286         int                     objchange;      /* changing to 8-byte inodes */
287         xfs_dir2_data_aoff_t    offset = 0;     /* offset for new entry */
288         int                     pick;           /* which algorithm to use */
289         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
290         xfs_dir2_sf_entry_t     *sfep = NULL;   /* shortform entry */
291
292         trace_xfs_dir2_sf_addname(args);
293
294         ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
295         dp = args->dp;
296         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
297         /*
298          * Make sure the shortform value has some of its header.
299          */
300         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
301                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
302                 return -EIO;
303         }
304         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
305         ASSERT(dp->i_df.if_u1.if_data != NULL);
306         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
307         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
308         /*
309          * Compute entry (and change in) size.
310          */
311         incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
312         objchange = 0;
313
314         /*
315          * Do we have to change to 8 byte inodes?
316          */
317         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
318                 /*
319                  * Yes, adjust the inode size.  old count + (parent + new)
320                  */
321                 incr_isize +=
322                         (sfp->count + 2) *
323                         ((uint)sizeof(xfs_dir2_ino8_t) -
324                          (uint)sizeof(xfs_dir2_ino4_t));
325                 objchange = 1;
326         }
327
328         new_isize = (int)dp->i_d.di_size + incr_isize;
329         /*
330          * Won't fit as shortform any more (due to size),
331          * or the pick routine says it won't (due to offset values).
332          */
333         if (new_isize > XFS_IFORK_DSIZE(dp) ||
334             (pick =
335              xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
336                 /*
337                  * Just checking or no space reservation, it doesn't fit.
338                  */
339                 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
340                         return -ENOSPC;
341                 /*
342                  * Convert to block form then add the name.
343                  */
344                 error = xfs_dir2_sf_to_block(args);
345                 if (error)
346                         return error;
347                 return xfs_dir2_block_addname(args);
348         }
349         /*
350          * Just checking, it fits.
351          */
352         if (args->op_flags & XFS_DA_OP_JUSTCHECK)
353                 return 0;
354         /*
355          * Do it the easy way - just add it at the end.
356          */
357         if (pick == 1)
358                 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
359         /*
360          * Do it the hard way - look for a place to insert the new entry.
361          * Convert to 8 byte inode numbers first if necessary.
362          */
363         else {
364                 ASSERT(pick == 2);
365                 if (objchange)
366                         xfs_dir2_sf_toino8(args);
367                 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
368         }
369         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
370         return 0;
371 }
372
373 /*
374  * Add the new entry the "easy" way.
375  * This is copying the old directory and adding the new entry at the end.
376  * Since it's sorted by "offset" we need room after the last offset
377  * that's already there, and then room to convert to a block directory.
378  * This is already checked by the pick routine.
379  */
380 static void
381 xfs_dir2_sf_addname_easy(
382         xfs_da_args_t           *args,          /* operation arguments */
383         xfs_dir2_sf_entry_t     *sfep,          /* pointer to new entry */
384         xfs_dir2_data_aoff_t    offset,         /* offset to use for new ent */
385         int                     new_isize)      /* new directory size */
386 {
387         int                     byteoff;        /* byte offset in sf dir */
388         xfs_inode_t             *dp;            /* incore directory inode */
389         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
390
391         dp = args->dp;
392
393         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
394         byteoff = (int)((char *)sfep - (char *)sfp);
395         /*
396          * Grow the in-inode space.
397          */
398         xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
399                           XFS_DATA_FORK);
400         /*
401          * Need to set up again due to realloc of the inode data.
402          */
403         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
404         sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
405         /*
406          * Fill in the new entry.
407          */
408         sfep->namelen = args->namelen;
409         xfs_dir2_sf_put_offset(sfep, offset);
410         memcpy(sfep->name, args->name, sfep->namelen);
411         dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
412         dp->d_ops->sf_put_ftype(sfep, args->filetype);
413
414         /*
415          * Update the header and inode.
416          */
417         sfp->count++;
418         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
419                 sfp->i8count++;
420         dp->i_d.di_size = new_isize;
421         xfs_dir2_sf_check(args);
422 }
423
424 /*
425  * Add the new entry the "hard" way.
426  * The caller has already converted to 8 byte inode numbers if necessary,
427  * in which case we need to leave the i8count at 1.
428  * Find a hole that the new entry will fit into, and copy
429  * the first part of the entries, the new entry, and the last part of
430  * the entries.
431  */
432 /* ARGSUSED */
433 static void
434 xfs_dir2_sf_addname_hard(
435         xfs_da_args_t           *args,          /* operation arguments */
436         int                     objchange,      /* changing inode number size */
437         int                     new_isize)      /* new directory size */
438 {
439         int                     add_datasize;   /* data size need for new ent */
440         char                    *buf;           /* buffer for old */
441         xfs_inode_t             *dp;            /* incore directory inode */
442         int                     eof;            /* reached end of old dir */
443         int                     nbytes;         /* temp for byte copies */
444         xfs_dir2_data_aoff_t    new_offset;     /* next offset value */
445         xfs_dir2_data_aoff_t    offset;         /* current offset value */
446         int                     old_isize;      /* previous di_size */
447         xfs_dir2_sf_entry_t     *oldsfep;       /* entry in original dir */
448         xfs_dir2_sf_hdr_t       *oldsfp;        /* original shortform dir */
449         xfs_dir2_sf_entry_t     *sfep;          /* entry in new dir */
450         xfs_dir2_sf_hdr_t       *sfp;           /* new shortform dir */
451
452         /*
453          * Copy the old directory to the stack buffer.
454          */
455         dp = args->dp;
456
457         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
458         old_isize = (int)dp->i_d.di_size;
459         buf = kmem_alloc(old_isize, KM_SLEEP);
460         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
461         memcpy(oldsfp, sfp, old_isize);
462         /*
463          * Loop over the old directory finding the place we're going
464          * to insert the new entry.
465          * If it's going to end up at the end then oldsfep will point there.
466          */
467         for (offset = dp->d_ops->data_first_offset,
468               oldsfep = xfs_dir2_sf_firstentry(oldsfp),
469               add_datasize = dp->d_ops->data_entsize(args->namelen),
470               eof = (char *)oldsfep == &buf[old_isize];
471              !eof;
472              offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
473               oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
474               eof = (char *)oldsfep == &buf[old_isize]) {
475                 new_offset = xfs_dir2_sf_get_offset(oldsfep);
476                 if (offset + add_datasize <= new_offset)
477                         break;
478         }
479         /*
480          * Get rid of the old directory, then allocate space for
481          * the new one.  We do this so xfs_idata_realloc won't copy
482          * the data.
483          */
484         xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
485         xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
486         /*
487          * Reset the pointer since the buffer was reallocated.
488          */
489         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
490         /*
491          * Copy the first part of the directory, including the header.
492          */
493         nbytes = (int)((char *)oldsfep - (char *)oldsfp);
494         memcpy(sfp, oldsfp, nbytes);
495         sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
496         /*
497          * Fill in the new entry, and update the header counts.
498          */
499         sfep->namelen = args->namelen;
500         xfs_dir2_sf_put_offset(sfep, offset);
501         memcpy(sfep->name, args->name, sfep->namelen);
502         dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
503         dp->d_ops->sf_put_ftype(sfep, args->filetype);
504         sfp->count++;
505         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
506                 sfp->i8count++;
507         /*
508          * If there's more left to copy, do that.
509          */
510         if (!eof) {
511                 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
512                 memcpy(sfep, oldsfep, old_isize - nbytes);
513         }
514         kmem_free(buf);
515         dp->i_d.di_size = new_isize;
516         xfs_dir2_sf_check(args);
517 }
518
519 /*
520  * Decide if the new entry will fit at all.
521  * If it will fit, pick between adding the new entry to the end (easy)
522  * or somewhere else (hard).
523  * Return 0 (won't fit), 1 (easy), 2 (hard).
524  */
525 /*ARGSUSED*/
526 static int                                      /* pick result */
527 xfs_dir2_sf_addname_pick(
528         xfs_da_args_t           *args,          /* operation arguments */
529         int                     objchange,      /* inode # size changes */
530         xfs_dir2_sf_entry_t     **sfepp,        /* out(1): new entry ptr */
531         xfs_dir2_data_aoff_t    *offsetp)       /* out(1): new offset */
532 {
533         xfs_inode_t             *dp;            /* incore directory inode */
534         int                     holefit;        /* found hole it will fit in */
535         int                     i;              /* entry number */
536         xfs_dir2_data_aoff_t    offset;         /* data block offset */
537         xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
538         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
539         int                     size;           /* entry's data size */
540         int                     used;           /* data bytes used */
541
542         dp = args->dp;
543
544         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
545         size = dp->d_ops->data_entsize(args->namelen);
546         offset = dp->d_ops->data_first_offset;
547         sfep = xfs_dir2_sf_firstentry(sfp);
548         holefit = 0;
549         /*
550          * Loop over sf entries.
551          * Keep track of data offset and whether we've seen a place
552          * to insert the new entry.
553          */
554         for (i = 0; i < sfp->count; i++) {
555                 if (!holefit)
556                         holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
557                 offset = xfs_dir2_sf_get_offset(sfep) +
558                          dp->d_ops->data_entsize(sfep->namelen);
559                 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
560         }
561         /*
562          * Calculate data bytes used excluding the new entry, if this
563          * was a data block (block form directory).
564          */
565         used = offset +
566                (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
567                (uint)sizeof(xfs_dir2_block_tail_t);
568         /*
569          * If it won't fit in a block form then we can't insert it,
570          * we'll go back, convert to block, then try the insert and convert
571          * to leaf.
572          */
573         if (used + (holefit ? 0 : size) > args->geo->blksize)
574                 return 0;
575         /*
576          * If changing the inode number size, do it the hard way.
577          */
578         if (objchange)
579                 return 2;
580         /*
581          * If it won't fit at the end then do it the hard way (use the hole).
582          */
583         if (used + size > args->geo->blksize)
584                 return 2;
585         /*
586          * Do it the easy way.
587          */
588         *sfepp = sfep;
589         *offsetp = offset;
590         return 1;
591 }
592
593 #ifdef DEBUG
594 /*
595  * Check consistency of shortform directory, assert if bad.
596  */
597 static void
598 xfs_dir2_sf_check(
599         xfs_da_args_t           *args)          /* operation arguments */
600 {
601         xfs_inode_t             *dp;            /* incore directory inode */
602         int                     i;              /* entry number */
603         int                     i8count;        /* number of big inode#s */
604         xfs_ino_t               ino;            /* entry inode number */
605         int                     offset;         /* data offset */
606         xfs_dir2_sf_entry_t     *sfep;          /* shortform dir entry */
607         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
608
609         dp = args->dp;
610
611         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
612         offset = dp->d_ops->data_first_offset;
613         ino = dp->d_ops->sf_get_parent_ino(sfp);
614         i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
615
616         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
617              i < sfp->count;
618              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
619                 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
620                 ino = dp->d_ops->sf_get_ino(sfp, sfep);
621                 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
622                 offset =
623                         xfs_dir2_sf_get_offset(sfep) +
624                         dp->d_ops->data_entsize(sfep->namelen);
625                 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
626         }
627         ASSERT(i8count == sfp->i8count);
628         ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
629         ASSERT(offset +
630                (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
631                (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
632 }
633 #endif  /* DEBUG */
634
635 /*
636  * Create a new (shortform) directory.
637  */
638 int                                     /* error, always 0 */
639 xfs_dir2_sf_create(
640         xfs_da_args_t   *args,          /* operation arguments */
641         xfs_ino_t       pino)           /* parent inode number */
642 {
643         xfs_inode_t     *dp;            /* incore directory inode */
644         int             i8count;        /* parent inode is an 8-byte number */
645         xfs_dir2_sf_hdr_t *sfp;         /* shortform structure */
646         int             size;           /* directory size */
647
648         trace_xfs_dir2_sf_create(args);
649
650         dp = args->dp;
651
652         ASSERT(dp != NULL);
653         ASSERT(dp->i_d.di_size == 0);
654         /*
655          * If it's currently a zero-length extent file,
656          * convert it to local format.
657          */
658         if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
659                 dp->i_df.if_flags &= ~XFS_IFEXTENTS;    /* just in case */
660                 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
661                 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
662                 dp->i_df.if_flags |= XFS_IFINLINE;
663         }
664         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
665         ASSERT(dp->i_df.if_bytes == 0);
666         i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
667         size = xfs_dir2_sf_hdr_size(i8count);
668         /*
669          * Make a buffer for the data.
670          */
671         xfs_idata_realloc(dp, size, XFS_DATA_FORK);
672         /*
673          * Fill in the header,
674          */
675         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
676         sfp->i8count = i8count;
677         /*
678          * Now can put in the inode number, since i8count is set.
679          */
680         dp->d_ops->sf_put_parent_ino(sfp, pino);
681         sfp->count = 0;
682         dp->i_d.di_size = size;
683         xfs_dir2_sf_check(args);
684         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
685         return 0;
686 }
687
688 /*
689  * Lookup an entry in a shortform directory.
690  * Returns EEXIST if found, ENOENT if not found.
691  */
692 int                                             /* error */
693 xfs_dir2_sf_lookup(
694         xfs_da_args_t           *args)          /* operation arguments */
695 {
696         xfs_inode_t             *dp;            /* incore directory inode */
697         int                     i;              /* entry index */
698         int                     error;
699         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
700         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
701         enum xfs_dacmp          cmp;            /* comparison result */
702         xfs_dir2_sf_entry_t     *ci_sfep;       /* case-insens. entry */
703
704         trace_xfs_dir2_sf_lookup(args);
705
706         xfs_dir2_sf_check(args);
707         dp = args->dp;
708
709         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
710         /*
711          * Bail out if the directory is way too short.
712          */
713         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
714                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
715                 return -EIO;
716         }
717         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
718         ASSERT(dp->i_df.if_u1.if_data != NULL);
719         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
720         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
721         /*
722          * Special case for .
723          */
724         if (args->namelen == 1 && args->name[0] == '.') {
725                 args->inumber = dp->i_ino;
726                 args->cmpresult = XFS_CMP_EXACT;
727                 args->filetype = XFS_DIR3_FT_DIR;
728                 return -EEXIST;
729         }
730         /*
731          * Special case for ..
732          */
733         if (args->namelen == 2 &&
734             args->name[0] == '.' && args->name[1] == '.') {
735                 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
736                 args->cmpresult = XFS_CMP_EXACT;
737                 args->filetype = XFS_DIR3_FT_DIR;
738                 return -EEXIST;
739         }
740         /*
741          * Loop over all the entries trying to match ours.
742          */
743         ci_sfep = NULL;
744         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
745              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
746                 /*
747                  * Compare name and if it's an exact match, return the inode
748                  * number. If it's the first case-insensitive match, store the
749                  * inode number and continue looking for an exact match.
750                  */
751                 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
752                                                                 sfep->namelen);
753                 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
754                         args->cmpresult = cmp;
755                         args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
756                         args->filetype = dp->d_ops->sf_get_ftype(sfep);
757                         if (cmp == XFS_CMP_EXACT)
758                                 return -EEXIST;
759                         ci_sfep = sfep;
760                 }
761         }
762         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
763         /*
764          * Here, we can only be doing a lookup (not a rename or replace).
765          * If a case-insensitive match was not found, return -ENOENT.
766          */
767         if (!ci_sfep)
768                 return -ENOENT;
769         /* otherwise process the CI match as required by the caller */
770         error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
771         return error;
772 }
773
774 /*
775  * Remove an entry from a shortform directory.
776  */
777 int                                             /* error */
778 xfs_dir2_sf_removename(
779         xfs_da_args_t           *args)
780 {
781         int                     byteoff;        /* offset of removed entry */
782         xfs_inode_t             *dp;            /* incore directory inode */
783         int                     entsize;        /* this entry's size */
784         int                     i;              /* shortform entry index */
785         int                     newsize;        /* new inode size */
786         int                     oldsize;        /* old inode size */
787         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
788         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
789
790         trace_xfs_dir2_sf_removename(args);
791
792         dp = args->dp;
793
794         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
795         oldsize = (int)dp->i_d.di_size;
796         /*
797          * Bail out if the directory is way too short.
798          */
799         if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
800                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
801                 return -EIO;
802         }
803         ASSERT(dp->i_df.if_bytes == oldsize);
804         ASSERT(dp->i_df.if_u1.if_data != NULL);
805         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
806         ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
807         /*
808          * Loop over the old directory entries.
809          * Find the one we're deleting.
810          */
811         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
812              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
813                 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
814                                                                 XFS_CMP_EXACT) {
815                         ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
816                                args->inumber);
817                         break;
818                 }
819         }
820         /*
821          * Didn't find it.
822          */
823         if (i == sfp->count)
824                 return -ENOENT;
825         /*
826          * Calculate sizes.
827          */
828         byteoff = (int)((char *)sfep - (char *)sfp);
829         entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
830         newsize = oldsize - entsize;
831         /*
832          * Copy the part if any after the removed entry, sliding it down.
833          */
834         if (byteoff + entsize < oldsize)
835                 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
836                         oldsize - (byteoff + entsize));
837         /*
838          * Fix up the header and file size.
839          */
840         sfp->count--;
841         dp->i_d.di_size = newsize;
842         /*
843          * Reallocate, making it smaller.
844          */
845         xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
846         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
847         /*
848          * Are we changing inode number size?
849          */
850         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
851                 if (sfp->i8count == 1)
852                         xfs_dir2_sf_toino4(args);
853                 else
854                         sfp->i8count--;
855         }
856         xfs_dir2_sf_check(args);
857         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
858         return 0;
859 }
860
861 /*
862  * Replace the inode number of an entry in a shortform directory.
863  */
864 int                                             /* error */
865 xfs_dir2_sf_replace(
866         xfs_da_args_t           *args)          /* operation arguments */
867 {
868         xfs_inode_t             *dp;            /* incore directory inode */
869         int                     i;              /* entry index */
870         xfs_ino_t               ino=0;          /* entry old inode number */
871         int                     i8elevated;     /* sf_toino8 set i8count=1 */
872         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
873         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
874
875         trace_xfs_dir2_sf_replace(args);
876
877         dp = args->dp;
878
879         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
880         /*
881          * Bail out if the shortform directory is way too small.
882          */
883         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
884                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
885                 return -EIO;
886         }
887         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
888         ASSERT(dp->i_df.if_u1.if_data != NULL);
889         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
890         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
891
892         /*
893          * New inode number is large, and need to convert to 8-byte inodes.
894          */
895         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
896                 int     error;                  /* error return value */
897                 int     newsize;                /* new inode size */
898
899                 newsize =
900                         dp->i_df.if_bytes +
901                         (sfp->count + 1) *
902                         ((uint)sizeof(xfs_dir2_ino8_t) -
903                          (uint)sizeof(xfs_dir2_ino4_t));
904                 /*
905                  * Won't fit as shortform, convert to block then do replace.
906                  */
907                 if (newsize > XFS_IFORK_DSIZE(dp)) {
908                         error = xfs_dir2_sf_to_block(args);
909                         if (error) {
910                                 return error;
911                         }
912                         return xfs_dir2_block_replace(args);
913                 }
914                 /*
915                  * Still fits, convert to 8-byte now.
916                  */
917                 xfs_dir2_sf_toino8(args);
918                 i8elevated = 1;
919                 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
920         } else
921                 i8elevated = 0;
922
923         ASSERT(args->namelen != 1 || args->name[0] != '.');
924         /*
925          * Replace ..'s entry.
926          */
927         if (args->namelen == 2 &&
928             args->name[0] == '.' && args->name[1] == '.') {
929                 ino = dp->d_ops->sf_get_parent_ino(sfp);
930                 ASSERT(args->inumber != ino);
931                 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
932         }
933         /*
934          * Normal entry, look for the name.
935          */
936         else {
937                 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
938                      i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
939                         if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
940                                                                 XFS_CMP_EXACT) {
941                                 ino = dp->d_ops->sf_get_ino(sfp, sfep);
942                                 ASSERT(args->inumber != ino);
943                                 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
944                                 dp->d_ops->sf_put_ftype(sfep, args->filetype);
945                                 break;
946                         }
947                 }
948                 /*
949                  * Didn't find it.
950                  */
951                 if (i == sfp->count) {
952                         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
953                         if (i8elevated)
954                                 xfs_dir2_sf_toino4(args);
955                         return -ENOENT;
956                 }
957         }
958         /*
959          * See if the old number was large, the new number is small.
960          */
961         if (ino > XFS_DIR2_MAX_SHORT_INUM &&
962             args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
963                 /*
964                  * And the old count was one, so need to convert to small.
965                  */
966                 if (sfp->i8count == 1)
967                         xfs_dir2_sf_toino4(args);
968                 else
969                         sfp->i8count--;
970         }
971         /*
972          * See if the old number was small, the new number is large.
973          */
974         if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
975             args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
976                 /*
977                  * add to the i8count unless we just converted to 8-byte
978                  * inodes (which does an implied i8count = 1)
979                  */
980                 ASSERT(sfp->i8count != 0);
981                 if (!i8elevated)
982                         sfp->i8count++;
983         }
984         xfs_dir2_sf_check(args);
985         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
986         return 0;
987 }
988
989 /*
990  * Convert from 8-byte inode numbers to 4-byte inode numbers.
991  * The last 8-byte inode number is gone, but the count is still 1.
992  */
993 static void
994 xfs_dir2_sf_toino4(
995         xfs_da_args_t           *args)          /* operation arguments */
996 {
997         char                    *buf;           /* old dir's buffer */
998         xfs_inode_t             *dp;            /* incore directory inode */
999         int                     i;              /* entry index */
1000         int                     newsize;        /* new inode size */
1001         xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1002         xfs_dir2_sf_hdr_t       *oldsfp;        /* old sf directory */
1003         int                     oldsize;        /* old inode size */
1004         xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1005         xfs_dir2_sf_hdr_t       *sfp;           /* new sf directory */
1006
1007         trace_xfs_dir2_sf_toino4(args);
1008
1009         dp = args->dp;
1010
1011         /*
1012          * Copy the old directory to the buffer.
1013          * Then nuke it from the inode, and add the new buffer to the inode.
1014          * Don't want xfs_idata_realloc copying the data here.
1015          */
1016         oldsize = dp->i_df.if_bytes;
1017         buf = kmem_alloc(oldsize, KM_SLEEP);
1018         oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1019         ASSERT(oldsfp->i8count == 1);
1020         memcpy(buf, oldsfp, oldsize);
1021         /*
1022          * Compute the new inode size.
1023          */
1024         newsize =
1025                 oldsize -
1026                 (oldsfp->count + 1) *
1027                 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1028         xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1029         xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1030         /*
1031          * Reset our pointers, the data has moved.
1032          */
1033         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1034         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1035         /*
1036          * Fill in the new header.
1037          */
1038         sfp->count = oldsfp->count;
1039         sfp->i8count = 0;
1040         dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1041         /*
1042          * Copy the entries field by field.
1043          */
1044         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1045                     oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1046              i < sfp->count;
1047              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1048                   oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1049                 sfep->namelen = oldsfep->namelen;
1050                 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1051                 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1052                 dp->d_ops->sf_put_ino(sfp, sfep,
1053                                       dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1054                 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1055         }
1056         /*
1057          * Clean up the inode.
1058          */
1059         kmem_free(buf);
1060         dp->i_d.di_size = newsize;
1061         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1062 }
1063
1064 /*
1065  * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1066  * The new entry w/ an 8-byte inode number is not there yet; we leave with
1067  * i8count set to 1, but no corresponding 8-byte entry.
1068  */
1069 static void
1070 xfs_dir2_sf_toino8(
1071         xfs_da_args_t           *args)          /* operation arguments */
1072 {
1073         char                    *buf;           /* old dir's buffer */
1074         xfs_inode_t             *dp;            /* incore directory inode */
1075         int                     i;              /* entry index */
1076         int                     newsize;        /* new inode size */
1077         xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1078         xfs_dir2_sf_hdr_t       *oldsfp;        /* old sf directory */
1079         int                     oldsize;        /* old inode size */
1080         xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1081         xfs_dir2_sf_hdr_t       *sfp;           /* new sf directory */
1082
1083         trace_xfs_dir2_sf_toino8(args);
1084
1085         dp = args->dp;
1086
1087         /*
1088          * Copy the old directory to the buffer.
1089          * Then nuke it from the inode, and add the new buffer to the inode.
1090          * Don't want xfs_idata_realloc copying the data here.
1091          */
1092         oldsize = dp->i_df.if_bytes;
1093         buf = kmem_alloc(oldsize, KM_SLEEP);
1094         oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1095         ASSERT(oldsfp->i8count == 0);
1096         memcpy(buf, oldsfp, oldsize);
1097         /*
1098          * Compute the new inode size (nb: entry count + 1 for parent)
1099          */
1100         newsize =
1101                 oldsize +
1102                 (oldsfp->count + 1) *
1103                 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1104         xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1105         xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1106         /*
1107          * Reset our pointers, the data has moved.
1108          */
1109         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1110         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1111         /*
1112          * Fill in the new header.
1113          */
1114         sfp->count = oldsfp->count;
1115         sfp->i8count = 1;
1116         dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1117         /*
1118          * Copy the entries field by field.
1119          */
1120         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1121                     oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1122              i < sfp->count;
1123              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1124                   oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1125                 sfep->namelen = oldsfep->namelen;
1126                 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1127                 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1128                 dp->d_ops->sf_put_ino(sfp, sfep,
1129                                       dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1130                 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1131         }
1132         /*
1133          * Clean up the inode.
1134          */
1135         kmem_free(buf);
1136         dp->i_d.di_size = newsize;
1137         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1138 }