2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_error.h"
36 #include "xfs_dir2_data.h"
37 #include "xfs_dir2_leaf.h"
38 #include "xfs_dir2_block.h"
39 #include "xfs_dir2_trace.h"
42 * Prototypes for internal functions.
44 static void xfs_dir2_sf_addname_easy(xfs_da_args_t
*args
,
45 xfs_dir2_sf_entry_t
*sfep
,
46 xfs_dir2_data_aoff_t offset
,
48 static void xfs_dir2_sf_addname_hard(xfs_da_args_t
*args
, int objchange
,
50 static int xfs_dir2_sf_addname_pick(xfs_da_args_t
*args
, int objchange
,
51 xfs_dir2_sf_entry_t
**sfepp
,
52 xfs_dir2_data_aoff_t
*offsetp
);
54 static void xfs_dir2_sf_check(xfs_da_args_t
*args
);
56 #define xfs_dir2_sf_check(args)
59 static void xfs_dir2_sf_toino4(xfs_da_args_t
*args
);
60 static void xfs_dir2_sf_toino8(xfs_da_args_t
*args
);
61 #endif /* XFS_BIG_INUMS */
64 * Given a block directory (dp/block), calculate its size as a shortform (sf)
65 * directory and a header for the sf directory, if it will fit it the
66 * space currently present in the inode. If it won't fit, the output
67 * size is too big (but not accurate).
69 int /* size for sf form */
70 xfs_dir2_block_sfsize(
71 xfs_inode_t
*dp
, /* incore inode pointer */
72 xfs_dir2_block_t
*block
, /* block directory data */
73 xfs_dir2_sf_hdr_t
*sfhp
) /* output: header for sf form */
75 xfs_dir2_dataptr_t addr
; /* data entry address */
76 xfs_dir2_leaf_entry_t
*blp
; /* leaf area of the block */
77 xfs_dir2_block_tail_t
*btp
; /* tail area of the block */
78 int count
; /* shortform entry count */
79 xfs_dir2_data_entry_t
*dep
; /* data entry in the block */
80 int i
; /* block entry index */
81 int i8count
; /* count of big-inode entries */
82 int isdot
; /* entry is "." */
83 int isdotdot
; /* entry is ".." */
84 xfs_mount_t
*mp
; /* mount structure pointer */
85 int namelen
; /* total name bytes */
86 xfs_ino_t parent
= 0; /* parent inode number */
87 int size
=0; /* total computed size */
91 count
= i8count
= namelen
= 0;
92 btp
= xfs_dir2_block_tail_p(mp
, block
);
93 blp
= xfs_dir2_block_leaf_p(btp
);
96 * Iterate over the block's data entries by using the leaf pointers.
98 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
99 if ((addr
= be32_to_cpu(blp
[i
].address
)) == XFS_DIR2_NULL_DATAPTR
)
102 * Calculate the pointer to the entry at hand.
104 dep
= (xfs_dir2_data_entry_t
*)
105 ((char *)block
+ xfs_dir2_dataptr_to_off(mp
, addr
));
107 * Detect . and .., so we can special-case them.
108 * . is not included in sf directories.
109 * .. is included by just the parent inode number.
111 isdot
= dep
->namelen
== 1 && dep
->name
[0] == '.';
114 dep
->name
[0] == '.' && dep
->name
[1] == '.';
117 i8count
+= be64_to_cpu(dep
->inumber
) > XFS_DIR2_MAX_SHORT_INUM
;
119 if (!isdot
&& !isdotdot
) {
121 namelen
+= dep
->namelen
;
123 parent
= be64_to_cpu(dep
->inumber
);
125 * Calculate the new size, see if we should give up yet.
127 size
= xfs_dir2_sf_hdr_size(i8count
) + /* header */
128 count
+ /* namelen */
129 count
* (uint
)sizeof(xfs_dir2_sf_off_t
) + /* offset */
131 (i8count
? /* inumber */
132 (uint
)sizeof(xfs_dir2_ino8_t
) * count
:
133 (uint
)sizeof(xfs_dir2_ino4_t
) * count
);
134 if (size
> XFS_IFORK_DSIZE(dp
))
135 return size
; /* size value is a failure */
138 * Create the output header, if it worked.
141 sfhp
->i8count
= i8count
;
142 xfs_dir2_sf_put_inumber((xfs_dir2_sf_t
*)sfhp
, &parent
, &sfhp
->parent
);
147 * Convert a block format directory to shortform.
148 * Caller has already checked that it will fit, and built us a header.
151 xfs_dir2_block_to_sf(
152 xfs_da_args_t
*args
, /* operation arguments */
153 xfs_dabuf_t
*bp
, /* block buffer */
154 int size
, /* shortform directory size */
155 xfs_dir2_sf_hdr_t
*sfhp
) /* shortform directory hdr */
157 xfs_dir2_block_t
*block
; /* block structure */
158 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
159 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
160 xfs_inode_t
*dp
; /* incore directory inode */
161 xfs_dir2_data_unused_t
*dup
; /* unused data pointer */
162 char *endptr
; /* end of data entries */
163 int error
; /* error return value */
164 int logflags
; /* inode logging flags */
165 xfs_mount_t
*mp
; /* filesystem mount point */
166 char *ptr
; /* current data pointer */
167 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
168 xfs_dir2_sf_t
*sfp
; /* shortform structure */
171 xfs_dir2_trace_args_sb("block_to_sf", args
, size
, bp
);
176 * Make a copy of the block data, so we can shrink the inode
177 * and add local data.
179 block
= kmem_alloc(mp
->m_dirblksize
, KM_SLEEP
);
180 memcpy(block
, bp
->data
, mp
->m_dirblksize
);
181 logflags
= XFS_ILOG_CORE
;
182 if ((error
= xfs_dir2_shrink_inode(args
, mp
->m_dirdatablk
, bp
))) {
183 ASSERT(error
!= ENOSPC
);
187 * The buffer is now unconditionally gone, whether
188 * xfs_dir2_shrink_inode worked or not.
190 * Convert the inode to local format.
192 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
;
193 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
194 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
195 ASSERT(dp
->i_df
.if_bytes
== 0);
196 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
197 logflags
|= XFS_ILOG_DDATA
;
199 * Copy the header into the newly allocate local space.
201 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
202 memcpy(sfp
, sfhp
, xfs_dir2_sf_hdr_size(sfhp
->i8count
));
203 dp
->i_d
.di_size
= size
;
205 * Set up to loop over the block's entries.
207 btp
= xfs_dir2_block_tail_p(mp
, block
);
208 ptr
= (char *)block
->u
;
209 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
210 sfep
= xfs_dir2_sf_firstentry(sfp
);
212 * Loop over the active and unused entries.
213 * Stop when we reach the leaf/tail portion of the block.
215 while (ptr
< endptr
) {
217 * If it's unused, just skip over it.
219 dup
= (xfs_dir2_data_unused_t
*)ptr
;
220 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
221 ptr
+= be16_to_cpu(dup
->length
);
224 dep
= (xfs_dir2_data_entry_t
*)ptr
;
228 if (dep
->namelen
== 1 && dep
->name
[0] == '.')
229 ASSERT(be64_to_cpu(dep
->inumber
) == dp
->i_ino
);
231 * Skip .., but make sure the inode number is right.
233 else if (dep
->namelen
== 2 &&
234 dep
->name
[0] == '.' && dep
->name
[1] == '.')
235 ASSERT(be64_to_cpu(dep
->inumber
) ==
236 xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
));
238 * Normal entry, copy it into shortform.
241 sfep
->namelen
= dep
->namelen
;
242 xfs_dir2_sf_put_offset(sfep
,
243 (xfs_dir2_data_aoff_t
)
244 ((char *)dep
- (char *)block
));
245 memcpy(sfep
->name
, dep
->name
, dep
->namelen
);
246 temp
= be64_to_cpu(dep
->inumber
);
247 xfs_dir2_sf_put_inumber(sfp
, &temp
,
248 xfs_dir2_sf_inumberp(sfep
));
249 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
251 ptr
+= xfs_dir2_data_entsize(dep
->namelen
);
253 ASSERT((char *)sfep
- (char *)sfp
== size
);
254 xfs_dir2_sf_check(args
);
256 xfs_trans_log_inode(args
->trans
, dp
, logflags
);
257 kmem_free(block
, mp
->m_dirblksize
);
262 * Add a name to a shortform directory.
263 * There are two algorithms, "easy" and "hard" which we decide on
264 * before changing anything.
265 * Convert to block form if necessary, if the new entry won't fit.
269 xfs_da_args_t
*args
) /* operation arguments */
271 int add_entsize
; /* size of the new entry */
272 xfs_inode_t
*dp
; /* incore directory inode */
273 int error
; /* error return value */
274 int incr_isize
; /* total change in size */
275 int new_isize
; /* di_size after adding name */
276 int objchange
; /* changing to 8-byte inodes */
277 xfs_dir2_data_aoff_t offset
= 0; /* offset for new entry */
278 int old_isize
; /* di_size before adding name */
279 int pick
; /* which algorithm to use */
280 xfs_dir2_sf_t
*sfp
; /* shortform structure */
281 xfs_dir2_sf_entry_t
*sfep
= NULL
; /* shortform entry */
283 xfs_dir2_trace_args("sf_addname", args
);
284 ASSERT(xfs_dir2_sf_lookup(args
) == ENOENT
);
286 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
288 * Make sure the shortform value has some of its header.
290 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
291 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
292 return XFS_ERROR(EIO
);
294 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
295 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
296 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
297 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
299 * Compute entry (and change in) size.
301 add_entsize
= xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
);
302 incr_isize
= add_entsize
;
306 * Do we have to change to 8 byte inodes?
308 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
310 * Yes, adjust the entry size and the total size.
313 (uint
)sizeof(xfs_dir2_ino8_t
) -
314 (uint
)sizeof(xfs_dir2_ino4_t
);
316 (sfp
->hdr
.count
+ 2) *
317 ((uint
)sizeof(xfs_dir2_ino8_t
) -
318 (uint
)sizeof(xfs_dir2_ino4_t
));
322 old_isize
= (int)dp
->i_d
.di_size
;
323 new_isize
= old_isize
+ incr_isize
;
325 * Won't fit as shortform any more (due to size),
326 * or the pick routine says it won't (due to offset values).
328 if (new_isize
> XFS_IFORK_DSIZE(dp
) ||
330 xfs_dir2_sf_addname_pick(args
, objchange
, &sfep
, &offset
)) == 0) {
332 * Just checking or no space reservation, it doesn't fit.
334 if (args
->justcheck
|| args
->total
== 0)
335 return XFS_ERROR(ENOSPC
);
337 * Convert to block form then add the name.
339 error
= xfs_dir2_sf_to_block(args
);
342 return xfs_dir2_block_addname(args
);
345 * Just checking, it fits.
350 * Do it the easy way - just add it at the end.
353 xfs_dir2_sf_addname_easy(args
, sfep
, offset
, new_isize
);
355 * Do it the hard way - look for a place to insert the new entry.
356 * Convert to 8 byte inode numbers first if necessary.
362 xfs_dir2_sf_toino8(args
);
364 xfs_dir2_sf_addname_hard(args
, objchange
, new_isize
);
366 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
371 * Add the new entry the "easy" way.
372 * This is copying the old directory and adding the new entry at the end.
373 * Since it's sorted by "offset" we need room after the last offset
374 * that's already there, and then room to convert to a block directory.
375 * This is already checked by the pick routine.
378 xfs_dir2_sf_addname_easy(
379 xfs_da_args_t
*args
, /* operation arguments */
380 xfs_dir2_sf_entry_t
*sfep
, /* pointer to new entry */
381 xfs_dir2_data_aoff_t offset
, /* offset to use for new ent */
382 int new_isize
) /* new directory size */
384 int byteoff
; /* byte offset in sf dir */
385 xfs_inode_t
*dp
; /* incore directory inode */
386 xfs_dir2_sf_t
*sfp
; /* shortform structure */
390 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
391 byteoff
= (int)((char *)sfep
- (char *)sfp
);
393 * Grow the in-inode space.
395 xfs_idata_realloc(dp
, xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
),
398 * Need to set up again due to realloc of the inode data.
400 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
401 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ byteoff
);
403 * Fill in the new entry.
405 sfep
->namelen
= args
->namelen
;
406 xfs_dir2_sf_put_offset(sfep
, offset
);
407 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
408 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
409 xfs_dir2_sf_inumberp(sfep
));
411 * Update the header and inode.
415 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
)
418 dp
->i_d
.di_size
= new_isize
;
419 xfs_dir2_sf_check(args
);
423 * Add the new entry the "hard" way.
424 * The caller has already converted to 8 byte inode numbers if necessary,
425 * in which case we need to leave the i8count at 1.
426 * Find a hole that the new entry will fit into, and copy
427 * the first part of the entries, the new entry, and the last part of
432 xfs_dir2_sf_addname_hard(
433 xfs_da_args_t
*args
, /* operation arguments */
434 int objchange
, /* changing inode number size */
435 int new_isize
) /* new directory size */
437 int add_datasize
; /* data size need for new ent */
438 char *buf
; /* buffer for old */
439 xfs_inode_t
*dp
; /* incore directory inode */
440 int eof
; /* reached end of old dir */
441 int nbytes
; /* temp for byte copies */
442 xfs_dir2_data_aoff_t new_offset
; /* next offset value */
443 xfs_dir2_data_aoff_t offset
; /* current offset value */
444 int old_isize
; /* previous di_size */
445 xfs_dir2_sf_entry_t
*oldsfep
; /* entry in original dir */
446 xfs_dir2_sf_t
*oldsfp
; /* original shortform dir */
447 xfs_dir2_sf_entry_t
*sfep
; /* entry in new dir */
448 xfs_dir2_sf_t
*sfp
; /* new shortform dir */
451 * Copy the old directory to the stack buffer.
455 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
456 old_isize
= (int)dp
->i_d
.di_size
;
457 buf
= kmem_alloc(old_isize
, KM_SLEEP
);
458 oldsfp
= (xfs_dir2_sf_t
*)buf
;
459 memcpy(oldsfp
, sfp
, old_isize
);
461 * Loop over the old directory finding the place we're going
462 * to insert the new entry.
463 * If it's going to end up at the end then oldsfep will point there.
465 for (offset
= XFS_DIR2_DATA_FIRST_OFFSET
,
466 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
),
467 add_datasize
= xfs_dir2_data_entsize(args
->namelen
),
468 eof
= (char *)oldsfep
== &buf
[old_isize
];
470 offset
= new_offset
+ xfs_dir2_data_entsize(oldsfep
->namelen
),
471 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
),
472 eof
= (char *)oldsfep
== &buf
[old_isize
]) {
473 new_offset
= xfs_dir2_sf_get_offset(oldsfep
);
474 if (offset
+ add_datasize
<= new_offset
)
478 * Get rid of the old directory, then allocate space for
479 * the new one. We do this so xfs_idata_realloc won't copy
482 xfs_idata_realloc(dp
, -old_isize
, XFS_DATA_FORK
);
483 xfs_idata_realloc(dp
, new_isize
, XFS_DATA_FORK
);
485 * Reset the pointer since the buffer was reallocated.
487 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
489 * Copy the first part of the directory, including the header.
491 nbytes
= (int)((char *)oldsfep
- (char *)oldsfp
);
492 memcpy(sfp
, oldsfp
, nbytes
);
493 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ nbytes
);
495 * Fill in the new entry, and update the header counts.
497 sfep
->namelen
= args
->namelen
;
498 xfs_dir2_sf_put_offset(sfep
, offset
);
499 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
500 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
501 xfs_dir2_sf_inumberp(sfep
));
504 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& !objchange
)
508 * If there's more left to copy, do that.
511 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
512 memcpy(sfep
, oldsfep
, old_isize
- nbytes
);
514 kmem_free(buf
, old_isize
);
515 dp
->i_d
.di_size
= new_isize
;
516 xfs_dir2_sf_check(args
);
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).
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 */
533 xfs_inode_t
*dp
; /* incore directory inode */
534 int holefit
; /* found hole it will fit in */
535 int i
; /* entry number */
536 xfs_mount_t
*mp
; /* filesystem mount point */
537 xfs_dir2_data_aoff_t offset
; /* data block offset */
538 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
539 xfs_dir2_sf_t
*sfp
; /* shortform structure */
540 int size
; /* entry's data size */
541 int used
; /* data bytes used */
546 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
547 size
= xfs_dir2_data_entsize(args
->namelen
);
548 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
549 sfep
= xfs_dir2_sf_firstentry(sfp
);
552 * Loop over sf entries.
553 * Keep track of data offset and whether we've seen a place
554 * to insert the new entry.
556 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
558 holefit
= offset
+ size
<= xfs_dir2_sf_get_offset(sfep
);
559 offset
= xfs_dir2_sf_get_offset(sfep
) +
560 xfs_dir2_data_entsize(sfep
->namelen
);
561 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
564 * Calculate data bytes used excluding the new entry, if this
565 * was a data block (block form directory).
568 (sfp
->hdr
.count
+ 3) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
569 (uint
)sizeof(xfs_dir2_block_tail_t
);
571 * If it won't fit in a block form then we can't insert it,
572 * we'll go back, convert to block, then try the insert and convert
575 if (used
+ (holefit
? 0 : size
) > mp
->m_dirblksize
)
578 * If changing the inode number size, do it the hard way.
585 ASSERT(objchange
== 0);
588 * If it won't fit at the end then do it the hard way (use the hole).
590 if (used
+ size
> mp
->m_dirblksize
)
593 * Do it the easy way.
602 * Check consistency of shortform directory, assert if bad.
606 xfs_da_args_t
*args
) /* operation arguments */
608 xfs_inode_t
*dp
; /* incore directory inode */
609 int i
; /* entry number */
610 int i8count
; /* number of big inode#s */
611 xfs_ino_t ino
; /* entry inode number */
612 int offset
; /* data offset */
613 xfs_dir2_sf_entry_t
*sfep
; /* shortform dir entry */
614 xfs_dir2_sf_t
*sfp
; /* shortform structure */
618 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
619 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
620 ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
621 i8count
= ino
> XFS_DIR2_MAX_SHORT_INUM
;
623 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
625 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
626 ASSERT(xfs_dir2_sf_get_offset(sfep
) >= offset
);
627 ino
= xfs_dir2_sf_get_inumber(sfp
, xfs_dir2_sf_inumberp(sfep
));
628 i8count
+= ino
> XFS_DIR2_MAX_SHORT_INUM
;
630 xfs_dir2_sf_get_offset(sfep
) +
631 xfs_dir2_data_entsize(sfep
->namelen
);
633 ASSERT(i8count
== sfp
->hdr
.i8count
);
634 ASSERT(XFS_BIG_INUMS
|| i8count
== 0);
635 ASSERT((char *)sfep
- (char *)sfp
== dp
->i_d
.di_size
);
637 (sfp
->hdr
.count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
638 (uint
)sizeof(xfs_dir2_block_tail_t
) <=
639 dp
->i_mount
->m_dirblksize
);
644 * Create a new (shortform) directory.
646 int /* error, always 0 */
648 xfs_da_args_t
*args
, /* operation arguments */
649 xfs_ino_t pino
) /* parent inode number */
651 xfs_inode_t
*dp
; /* incore directory inode */
652 int i8count
; /* parent inode is an 8-byte number */
653 xfs_dir2_sf_t
*sfp
; /* shortform structure */
654 int size
; /* directory size */
656 xfs_dir2_trace_args_i("sf_create", args
, pino
);
660 ASSERT(dp
->i_d
.di_size
== 0);
662 * If it's currently a zero-length extent file,
663 * convert it to local format.
665 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
) {
666 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
667 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
668 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
669 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
671 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
672 ASSERT(dp
->i_df
.if_bytes
== 0);
673 i8count
= pino
> XFS_DIR2_MAX_SHORT_INUM
;
674 size
= xfs_dir2_sf_hdr_size(i8count
);
676 * Make a buffer for the data.
678 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
680 * Fill in the header,
682 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
683 sfp
->hdr
.i8count
= i8count
;
685 * Now can put in the inode number, since i8count is set.
687 xfs_dir2_sf_put_inumber(sfp
, &pino
, &sfp
->hdr
.parent
);
689 dp
->i_d
.di_size
= size
;
690 xfs_dir2_sf_check(args
);
691 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
696 xfs_dir2_sf_getdents(
697 xfs_inode_t
*dp
, /* incore directory inode */
698 uio_t
*uio
, /* caller's buffer control */
699 int *eofp
, /* eof reached? (out) */
700 xfs_dirent_t
*dbp
, /* caller's buffer */
701 xfs_dir2_put_t put
) /* abi's formatting function */
703 int error
; /* error return value */
704 int i
; /* shortform entry number */
705 xfs_mount_t
*mp
; /* filesystem mount point */
706 xfs_dir2_dataptr_t off
; /* current entry's offset */
707 xfs_dir2_put_args_t p
; /* arg package for put rtn */
708 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
709 xfs_dir2_sf_t
*sfp
; /* shortform structure */
710 xfs_off_t dir_offset
;
714 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
716 * Give up if the directory is way too short.
718 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
719 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
720 return XFS_ERROR(EIO
);
723 dir_offset
= uio
->uio_offset
;
725 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
726 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
728 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
730 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
733 * If the block number in the offset is out of range, we're done.
735 if (xfs_dir2_dataptr_to_db(mp
, dir_offset
) > mp
->m_dirdatablk
) {
741 * Set up putargs structure.
747 * Put . entry unless we're starting past it.
750 xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
751 XFS_DIR2_DATA_DOT_OFFSET
)) {
752 p
.cook
= xfs_dir2_db_off_to_dataptr(mp
, 0,
753 XFS_DIR2_DATA_DOTDOT_OFFSET
);
756 p
.ino
+= mp
->m_inoadd
;
765 xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
766 XFS_DIR2_DATA_DOT_OFFSET
);
772 * Put .. entry unless we're starting past it.
775 xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
776 XFS_DIR2_DATA_DOTDOT_OFFSET
)) {
777 p
.cook
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
778 XFS_DIR2_DATA_FIRST_OFFSET
);
779 p
.ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
781 p
.ino
+= mp
->m_inoadd
;
790 xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
791 XFS_DIR2_DATA_DOTDOT_OFFSET
);
797 * Loop while there are more entries and put'ing works.
799 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
801 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
803 off
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
804 xfs_dir2_sf_get_offset(sfep
));
806 if (dir_offset
> off
)
809 p
.namelen
= sfep
->namelen
;
811 p
.cook
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
812 xfs_dir2_sf_get_offset(sfep
) +
813 xfs_dir2_data_entsize(p
.namelen
));
815 p
.ino
= xfs_dir2_sf_get_inumber(sfp
, xfs_dir2_sf_inumberp(sfep
));
817 p
.ino
+= mp
->m_inoadd
;
819 p
.name
= (char *)sfep
->name
;
824 uio
->uio_offset
= off
;
835 xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
+ 1, 0);
841 * Lookup an entry in a shortform directory.
842 * Returns EEXIST if found, ENOENT if not found.
846 xfs_da_args_t
*args
) /* operation arguments */
848 xfs_inode_t
*dp
; /* incore directory inode */
849 int i
; /* entry index */
850 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
851 xfs_dir2_sf_t
*sfp
; /* shortform structure */
853 xfs_dir2_trace_args("sf_lookup", args
);
854 xfs_dir2_sf_check(args
);
857 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
859 * Bail out if the directory is way too short.
861 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
862 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
863 return XFS_ERROR(EIO
);
865 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
866 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
867 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
868 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
872 if (args
->namelen
== 1 && args
->name
[0] == '.') {
873 args
->inumber
= dp
->i_ino
;
874 return XFS_ERROR(EEXIST
);
877 * Special case for ..
879 if (args
->namelen
== 2 &&
880 args
->name
[0] == '.' && args
->name
[1] == '.') {
881 args
->inumber
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
882 return XFS_ERROR(EEXIST
);
885 * Loop over all the entries trying to match ours.
887 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
889 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
890 if (sfep
->namelen
== args
->namelen
&&
891 sfep
->name
[0] == args
->name
[0] &&
892 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
894 xfs_dir2_sf_get_inumber(sfp
,
895 xfs_dir2_sf_inumberp(sfep
));
896 return XFS_ERROR(EEXIST
);
902 ASSERT(args
->oknoent
);
903 return XFS_ERROR(ENOENT
);
907 * Remove an entry from a shortform directory.
910 xfs_dir2_sf_removename(
913 int byteoff
; /* offset of removed entry */
914 xfs_inode_t
*dp
; /* incore directory inode */
915 int entsize
; /* this entry's size */
916 int i
; /* shortform entry index */
917 int newsize
; /* new inode size */
918 int oldsize
; /* old inode size */
919 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
920 xfs_dir2_sf_t
*sfp
; /* shortform structure */
922 xfs_dir2_trace_args("sf_removename", args
);
925 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
926 oldsize
= (int)dp
->i_d
.di_size
;
928 * Bail out if the directory is way too short.
930 if (oldsize
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
931 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
932 return XFS_ERROR(EIO
);
934 ASSERT(dp
->i_df
.if_bytes
== oldsize
);
935 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
936 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
937 ASSERT(oldsize
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
939 * Loop over the old directory entries.
940 * Find the one we're deleting.
942 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
944 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
945 if (sfep
->namelen
== args
->namelen
&&
946 sfep
->name
[0] == args
->name
[0] &&
947 memcmp(sfep
->name
, args
->name
, args
->namelen
) == 0) {
948 ASSERT(xfs_dir2_sf_get_inumber(sfp
,
949 xfs_dir2_sf_inumberp(sfep
)) ==
957 if (i
== sfp
->hdr
.count
) {
958 return XFS_ERROR(ENOENT
);
963 byteoff
= (int)((char *)sfep
- (char *)sfp
);
964 entsize
= xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
);
965 newsize
= oldsize
- entsize
;
967 * Copy the part if any after the removed entry, sliding it down.
969 if (byteoff
+ entsize
< oldsize
)
970 memmove((char *)sfp
+ byteoff
, (char *)sfp
+ byteoff
+ entsize
,
971 oldsize
- (byteoff
+ entsize
));
973 * Fix up the header and file size.
976 dp
->i_d
.di_size
= newsize
;
978 * Reallocate, making it smaller.
980 xfs_idata_realloc(dp
, newsize
- oldsize
, XFS_DATA_FORK
);
981 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
984 * Are we changing inode number size?
986 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
987 if (sfp
->hdr
.i8count
== 1)
988 xfs_dir2_sf_toino4(args
);
993 xfs_dir2_sf_check(args
);
994 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
999 * Replace the inode number of an entry in a shortform directory.
1002 xfs_dir2_sf_replace(
1003 xfs_da_args_t
*args
) /* operation arguments */
1005 xfs_inode_t
*dp
; /* incore directory inode */
1006 int i
; /* entry index */
1007 #if XFS_BIG_INUMS || defined(DEBUG)
1008 xfs_ino_t ino
=0; /* entry old inode number */
1011 int i8elevated
; /* sf_toino8 set i8count=1 */
1013 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
1014 xfs_dir2_sf_t
*sfp
; /* shortform structure */
1016 xfs_dir2_trace_args("sf_replace", args
);
1019 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1021 * Bail out if the shortform directory is way too small.
1023 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1024 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
1025 return XFS_ERROR(EIO
);
1027 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1028 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1029 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1030 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
1033 * New inode number is large, and need to convert to 8-byte inodes.
1035 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
1036 int error
; /* error return value */
1037 int newsize
; /* new inode size */
1041 (sfp
->hdr
.count
+ 1) *
1042 ((uint
)sizeof(xfs_dir2_ino8_t
) -
1043 (uint
)sizeof(xfs_dir2_ino4_t
));
1045 * Won't fit as shortform, convert to block then do replace.
1047 if (newsize
> XFS_IFORK_DSIZE(dp
)) {
1048 error
= xfs_dir2_sf_to_block(args
);
1052 return xfs_dir2_block_replace(args
);
1055 * Still fits, convert to 8-byte now.
1057 xfs_dir2_sf_toino8(args
);
1059 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1063 ASSERT(args
->namelen
!= 1 || args
->name
[0] != '.');
1065 * Replace ..'s entry.
1067 if (args
->namelen
== 2 &&
1068 args
->name
[0] == '.' && args
->name
[1] == '.') {
1069 #if XFS_BIG_INUMS || defined(DEBUG)
1070 ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
1071 ASSERT(args
->inumber
!= ino
);
1073 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
, &sfp
->hdr
.parent
);
1076 * Normal entry, look for the name.
1079 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
1081 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
1082 if (sfep
->namelen
== args
->namelen
&&
1083 sfep
->name
[0] == args
->name
[0] &&
1084 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
1085 #if XFS_BIG_INUMS || defined(DEBUG)
1086 ino
= xfs_dir2_sf_get_inumber(sfp
,
1087 xfs_dir2_sf_inumberp(sfep
));
1088 ASSERT(args
->inumber
!= ino
);
1090 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
1091 xfs_dir2_sf_inumberp(sfep
));
1098 if (i
== sfp
->hdr
.count
) {
1099 ASSERT(args
->oknoent
);
1102 xfs_dir2_sf_toino4(args
);
1104 return XFS_ERROR(ENOENT
);
1109 * See if the old number was large, the new number is small.
1111 if (ino
> XFS_DIR2_MAX_SHORT_INUM
&&
1112 args
->inumber
<= XFS_DIR2_MAX_SHORT_INUM
) {
1114 * And the old count was one, so need to convert to small.
1116 if (sfp
->hdr
.i8count
== 1)
1117 xfs_dir2_sf_toino4(args
);
1122 * See if the old number was small, the new number is large.
1124 if (ino
<= XFS_DIR2_MAX_SHORT_INUM
&&
1125 args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
1127 * add to the i8count unless we just converted to 8-byte
1128 * inodes (which does an implied i8count = 1)
1130 ASSERT(sfp
->hdr
.i8count
!= 0);
1135 xfs_dir2_sf_check(args
);
1136 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_DDATA
);
1142 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1143 * The last 8-byte inode number is gone, but the count is still 1.
1147 xfs_da_args_t
*args
) /* operation arguments */
1149 char *buf
; /* old dir's buffer */
1150 xfs_inode_t
*dp
; /* incore directory inode */
1151 int i
; /* entry index */
1152 xfs_ino_t ino
; /* entry inode number */
1153 int newsize
; /* new inode size */
1154 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1155 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
1156 int oldsize
; /* old inode size */
1157 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1158 xfs_dir2_sf_t
*sfp
; /* new sf directory */
1160 xfs_dir2_trace_args("sf_toino4", args
);
1164 * Copy the old directory to the buffer.
1165 * Then nuke it from the inode, and add the new buffer to the inode.
1166 * Don't want xfs_idata_realloc copying the data here.
1168 oldsize
= dp
->i_df
.if_bytes
;
1169 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1170 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1171 ASSERT(oldsfp
->hdr
.i8count
== 1);
1172 memcpy(buf
, oldsfp
, oldsize
);
1174 * Compute the new inode size.
1178 (oldsfp
->hdr
.count
+ 1) *
1179 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1180 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1181 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1183 * Reset our pointers, the data has moved.
1185 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1186 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1188 * Fill in the new header.
1190 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1191 sfp
->hdr
.i8count
= 0;
1192 ino
= xfs_dir2_sf_get_inumber(oldsfp
, &oldsfp
->hdr
.parent
);
1193 xfs_dir2_sf_put_inumber(sfp
, &ino
, &sfp
->hdr
.parent
);
1195 * Copy the entries field by field.
1197 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
),
1198 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
);
1200 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
),
1201 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
)) {
1202 sfep
->namelen
= oldsfep
->namelen
;
1203 sfep
->offset
= oldsfep
->offset
;
1204 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1205 ino
= xfs_dir2_sf_get_inumber(oldsfp
,
1206 xfs_dir2_sf_inumberp(oldsfep
));
1207 xfs_dir2_sf_put_inumber(sfp
, &ino
, xfs_dir2_sf_inumberp(sfep
));
1210 * Clean up the inode.
1212 kmem_free(buf
, oldsize
);
1213 dp
->i_d
.di_size
= newsize
;
1214 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1218 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1219 * The new 8-byte inode number is not there yet, we leave with the
1220 * count 1 but no corresponding entry.
1224 xfs_da_args_t
*args
) /* operation arguments */
1226 char *buf
; /* old dir's buffer */
1227 xfs_inode_t
*dp
; /* incore directory inode */
1228 int i
; /* entry index */
1229 xfs_ino_t ino
; /* entry inode number */
1230 int newsize
; /* new inode size */
1231 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1232 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
1233 int oldsize
; /* old inode size */
1234 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1235 xfs_dir2_sf_t
*sfp
; /* new sf directory */
1237 xfs_dir2_trace_args("sf_toino8", args
);
1241 * Copy the old directory to the buffer.
1242 * Then nuke it from the inode, and add the new buffer to the inode.
1243 * Don't want xfs_idata_realloc copying the data here.
1245 oldsize
= dp
->i_df
.if_bytes
;
1246 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1247 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1248 ASSERT(oldsfp
->hdr
.i8count
== 0);
1249 memcpy(buf
, oldsfp
, oldsize
);
1251 * Compute the new inode size.
1255 (oldsfp
->hdr
.count
+ 1) *
1256 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1257 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1258 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1260 * Reset our pointers, the data has moved.
1262 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1263 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1265 * Fill in the new header.
1267 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1268 sfp
->hdr
.i8count
= 1;
1269 ino
= xfs_dir2_sf_get_inumber(oldsfp
, &oldsfp
->hdr
.parent
);
1270 xfs_dir2_sf_put_inumber(sfp
, &ino
, &sfp
->hdr
.parent
);
1272 * Copy the entries field by field.
1274 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
),
1275 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
);
1277 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
),
1278 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
)) {
1279 sfep
->namelen
= oldsfep
->namelen
;
1280 sfep
->offset
= oldsfep
->offset
;
1281 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1282 ino
= xfs_dir2_sf_get_inumber(oldsfp
,
1283 xfs_dir2_sf_inumberp(oldsfep
));
1284 xfs_dir2_sf_put_inumber(sfp
, &ino
, xfs_dir2_sf_inumberp(sfep
));
1287 * Clean up the inode.
1289 kmem_free(buf
, oldsize
);
1290 dp
->i_d
.di_size
= newsize
;
1291 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1293 #endif /* XFS_BIG_INUMS */