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"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_error.h"
37 #include "xfs_dir2_data.h"
38 #include "xfs_dir2_leaf.h"
39 #include "xfs_dir2_block.h"
40 #include "xfs_dir2_trace.h"
43 * Prototypes for internal functions.
45 static void xfs_dir2_sf_addname_easy(xfs_da_args_t
*args
,
46 xfs_dir2_sf_entry_t
*sfep
,
47 xfs_dir2_data_aoff_t offset
,
49 static void xfs_dir2_sf_addname_hard(xfs_da_args_t
*args
, int objchange
,
51 static int xfs_dir2_sf_addname_pick(xfs_da_args_t
*args
, int objchange
,
52 xfs_dir2_sf_entry_t
**sfepp
,
53 xfs_dir2_data_aoff_t
*offsetp
);
55 static void xfs_dir2_sf_check(xfs_da_args_t
*args
);
57 #define xfs_dir2_sf_check(args)
60 static void xfs_dir2_sf_toino4(xfs_da_args_t
*args
);
61 static void xfs_dir2_sf_toino8(xfs_da_args_t
*args
);
62 #endif /* XFS_BIG_INUMS */
65 * Given a block directory (dp/block), calculate its size as a shortform (sf)
66 * directory and a header for the sf directory, if it will fit it the
67 * space currently present in the inode. If it won't fit, the output
68 * size is too big (but not accurate).
70 int /* size for sf form */
71 xfs_dir2_block_sfsize(
72 xfs_inode_t
*dp
, /* incore inode pointer */
73 xfs_dir2_block_t
*block
, /* block directory data */
74 xfs_dir2_sf_hdr_t
*sfhp
) /* output: header for sf form */
76 xfs_dir2_dataptr_t addr
; /* data entry address */
77 xfs_dir2_leaf_entry_t
*blp
; /* leaf area of the block */
78 xfs_dir2_block_tail_t
*btp
; /* tail area of the block */
79 int count
; /* shortform entry count */
80 xfs_dir2_data_entry_t
*dep
; /* data entry in the block */
81 int i
; /* block entry index */
82 int i8count
; /* count of big-inode entries */
83 int isdot
; /* entry is "." */
84 int isdotdot
; /* entry is ".." */
85 xfs_mount_t
*mp
; /* mount structure pointer */
86 int namelen
; /* total name bytes */
87 xfs_ino_t parent
= 0; /* parent inode number */
88 int size
=0; /* total computed size */
92 count
= i8count
= namelen
= 0;
93 btp
= xfs_dir2_block_tail_p(mp
, block
);
94 blp
= xfs_dir2_block_leaf_p(btp
);
97 * Iterate over the block's data entries by using the leaf pointers.
99 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
100 if ((addr
= be32_to_cpu(blp
[i
].address
)) == XFS_DIR2_NULL_DATAPTR
)
103 * Calculate the pointer to the entry at hand.
105 dep
= (xfs_dir2_data_entry_t
*)
106 ((char *)block
+ xfs_dir2_dataptr_to_off(mp
, addr
));
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.
112 isdot
= dep
->namelen
== 1 && dep
->name
[0] == '.';
115 dep
->name
[0] == '.' && dep
->name
[1] == '.';
118 i8count
+= be64_to_cpu(dep
->inumber
) > XFS_DIR2_MAX_SHORT_INUM
;
120 if (!isdot
&& !isdotdot
) {
122 namelen
+= dep
->namelen
;
124 parent
= be64_to_cpu(dep
->inumber
);
126 * Calculate the new size, see if we should give up yet.
128 size
= xfs_dir2_sf_hdr_size(i8count
) + /* header */
129 count
+ /* namelen */
130 count
* (uint
)sizeof(xfs_dir2_sf_off_t
) + /* offset */
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 */
139 * Create the output header, if it worked.
142 sfhp
->i8count
= i8count
;
143 xfs_dir2_sf_put_inumber((xfs_dir2_sf_t
*)sfhp
, &parent
, &sfhp
->parent
);
148 * Convert a block format directory to shortform.
149 * Caller has already checked that it will fit, and built us a header.
152 xfs_dir2_block_to_sf(
153 xfs_da_args_t
*args
, /* operation arguments */
154 xfs_dabuf_t
*bp
, /* block buffer */
155 int size
, /* shortform directory size */
156 xfs_dir2_sf_hdr_t
*sfhp
) /* shortform directory hdr */
158 xfs_dir2_block_t
*block
; /* block structure */
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_t
*sfp
; /* shortform structure */
172 xfs_dir2_trace_args_sb("block_to_sf", args
, size
, bp
);
177 * Make a copy of the block data, so we can shrink the inode
178 * and add local data.
180 block
= kmem_alloc(mp
->m_dirblksize
, KM_SLEEP
);
181 memcpy(block
, bp
->data
, mp
->m_dirblksize
);
182 logflags
= XFS_ILOG_CORE
;
183 if ((error
= xfs_dir2_shrink_inode(args
, mp
->m_dirdatablk
, bp
))) {
184 ASSERT(error
!= ENOSPC
);
188 * The buffer is now unconditionally gone, whether
189 * xfs_dir2_shrink_inode worked or not.
191 * Convert the inode to local format.
193 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
;
194 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
195 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
196 ASSERT(dp
->i_df
.if_bytes
== 0);
197 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
198 logflags
|= XFS_ILOG_DDATA
;
200 * Copy the header into the newly allocate local space.
202 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
203 memcpy(sfp
, sfhp
, xfs_dir2_sf_hdr_size(sfhp
->i8count
));
204 dp
->i_d
.di_size
= size
;
206 * Set up to loop over the block's entries.
208 btp
= xfs_dir2_block_tail_p(mp
, block
);
209 ptr
= (char *)block
->u
;
210 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
211 sfep
= xfs_dir2_sf_firstentry(sfp
);
213 * Loop over the active and unused entries.
214 * Stop when we reach the leaf/tail portion of the block.
216 while (ptr
< endptr
) {
218 * If it's unused, just skip over it.
220 dup
= (xfs_dir2_data_unused_t
*)ptr
;
221 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
222 ptr
+= be16_to_cpu(dup
->length
);
225 dep
= (xfs_dir2_data_entry_t
*)ptr
;
229 if (dep
->namelen
== 1 && dep
->name
[0] == '.')
230 ASSERT(be64_to_cpu(dep
->inumber
) == dp
->i_ino
);
232 * Skip .., but make sure the inode number is right.
234 else if (dep
->namelen
== 2 &&
235 dep
->name
[0] == '.' && dep
->name
[1] == '.')
236 ASSERT(be64_to_cpu(dep
->inumber
) ==
237 xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
));
239 * Normal entry, copy it into shortform.
242 sfep
->namelen
= dep
->namelen
;
243 xfs_dir2_sf_put_offset(sfep
,
244 (xfs_dir2_data_aoff_t
)
245 ((char *)dep
- (char *)block
));
246 memcpy(sfep
->name
, dep
->name
, dep
->namelen
);
247 temp
= be64_to_cpu(dep
->inumber
);
248 xfs_dir2_sf_put_inumber(sfp
, &temp
,
249 xfs_dir2_sf_inumberp(sfep
));
250 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
252 ptr
+= xfs_dir2_data_entsize(dep
->namelen
);
254 ASSERT((char *)sfep
- (char *)sfp
== size
);
255 xfs_dir2_sf_check(args
);
257 xfs_trans_log_inode(args
->trans
, dp
, logflags
);
258 kmem_free(block
, mp
->m_dirblksize
);
263 * Add a name to a shortform directory.
264 * There are two algorithms, "easy" and "hard" which we decide on
265 * before changing anything.
266 * Convert to block form if necessary, if the new entry won't fit.
270 xfs_da_args_t
*args
) /* operation arguments */
272 int add_entsize
; /* size of the new entry */
273 xfs_inode_t
*dp
; /* incore directory inode */
274 int error
; /* error return value */
275 int incr_isize
; /* total change in size */
276 int new_isize
; /* di_size after adding name */
277 int objchange
; /* changing to 8-byte inodes */
278 xfs_dir2_data_aoff_t offset
= 0; /* offset for new entry */
279 int old_isize
; /* di_size before adding name */
280 int pick
; /* which algorithm to use */
281 xfs_dir2_sf_t
*sfp
; /* shortform structure */
282 xfs_dir2_sf_entry_t
*sfep
= NULL
; /* shortform entry */
284 xfs_dir2_trace_args("sf_addname", args
);
285 ASSERT(xfs_dir2_sf_lookup(args
) == ENOENT
);
287 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
289 * Make sure the shortform value has some of its header.
291 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
292 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
293 return XFS_ERROR(EIO
);
295 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
296 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
297 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
298 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
300 * Compute entry (and change in) size.
302 add_entsize
= xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
);
303 incr_isize
= add_entsize
;
307 * Do we have to change to 8 byte inodes?
309 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
311 * Yes, adjust the entry size and the total size.
314 (uint
)sizeof(xfs_dir2_ino8_t
) -
315 (uint
)sizeof(xfs_dir2_ino4_t
);
317 (sfp
->hdr
.count
+ 2) *
318 ((uint
)sizeof(xfs_dir2_ino8_t
) -
319 (uint
)sizeof(xfs_dir2_ino4_t
));
323 old_isize
= (int)dp
->i_d
.di_size
;
324 new_isize
= old_isize
+ incr_isize
;
326 * Won't fit as shortform any more (due to size),
327 * or the pick routine says it won't (due to offset values).
329 if (new_isize
> XFS_IFORK_DSIZE(dp
) ||
331 xfs_dir2_sf_addname_pick(args
, objchange
, &sfep
, &offset
)) == 0) {
333 * Just checking or no space reservation, it doesn't fit.
335 if (args
->justcheck
|| args
->total
== 0)
336 return XFS_ERROR(ENOSPC
);
338 * Convert to block form then add the name.
340 error
= xfs_dir2_sf_to_block(args
);
343 return xfs_dir2_block_addname(args
);
346 * Just checking, it fits.
351 * Do it the easy way - just add it at the end.
354 xfs_dir2_sf_addname_easy(args
, sfep
, offset
, new_isize
);
356 * Do it the hard way - look for a place to insert the new entry.
357 * Convert to 8 byte inode numbers first if necessary.
363 xfs_dir2_sf_toino8(args
);
365 xfs_dir2_sf_addname_hard(args
, objchange
, new_isize
);
367 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
372 * Add the new entry the "easy" way.
373 * This is copying the old directory and adding the new entry at the end.
374 * Since it's sorted by "offset" we need room after the last offset
375 * that's already there, and then room to convert to a block directory.
376 * This is already checked by the pick routine.
379 xfs_dir2_sf_addname_easy(
380 xfs_da_args_t
*args
, /* operation arguments */
381 xfs_dir2_sf_entry_t
*sfep
, /* pointer to new entry */
382 xfs_dir2_data_aoff_t offset
, /* offset to use for new ent */
383 int new_isize
) /* new directory size */
385 int byteoff
; /* byte offset in sf dir */
386 xfs_inode_t
*dp
; /* incore directory inode */
387 xfs_dir2_sf_t
*sfp
; /* shortform structure */
391 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
392 byteoff
= (int)((char *)sfep
- (char *)sfp
);
394 * Grow the in-inode space.
396 xfs_idata_realloc(dp
, xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
),
399 * Need to set up again due to realloc of the inode data.
401 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
402 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ byteoff
);
404 * Fill in the new entry.
406 sfep
->namelen
= args
->namelen
;
407 xfs_dir2_sf_put_offset(sfep
, offset
);
408 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
409 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
410 xfs_dir2_sf_inumberp(sfep
));
412 * Update the header and inode.
416 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
)
419 dp
->i_d
.di_size
= new_isize
;
420 xfs_dir2_sf_check(args
);
424 * Add the new entry the "hard" way.
425 * The caller has already converted to 8 byte inode numbers if necessary,
426 * in which case we need to leave the i8count at 1.
427 * Find a hole that the new entry will fit into, and copy
428 * the first part of the entries, the new entry, and the last part of
433 xfs_dir2_sf_addname_hard(
434 xfs_da_args_t
*args
, /* operation arguments */
435 int objchange
, /* changing inode number size */
436 int new_isize
) /* new directory size */
438 int add_datasize
; /* data size need for new ent */
439 char *buf
; /* buffer for old */
440 xfs_inode_t
*dp
; /* incore directory inode */
441 int eof
; /* reached end of old dir */
442 int nbytes
; /* temp for byte copies */
443 xfs_dir2_data_aoff_t new_offset
; /* next offset value */
444 xfs_dir2_data_aoff_t offset
; /* current offset value */
445 int old_isize
; /* previous di_size */
446 xfs_dir2_sf_entry_t
*oldsfep
; /* entry in original dir */
447 xfs_dir2_sf_t
*oldsfp
; /* original shortform dir */
448 xfs_dir2_sf_entry_t
*sfep
; /* entry in new dir */
449 xfs_dir2_sf_t
*sfp
; /* new shortform dir */
452 * Copy the old directory to the stack buffer.
456 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
457 old_isize
= (int)dp
->i_d
.di_size
;
458 buf
= kmem_alloc(old_isize
, KM_SLEEP
);
459 oldsfp
= (xfs_dir2_sf_t
*)buf
;
460 memcpy(oldsfp
, sfp
, old_isize
);
462 * Loop over the old directory finding the place we're going
463 * to insert the new entry.
464 * If it's going to end up at the end then oldsfep will point there.
466 for (offset
= XFS_DIR2_DATA_FIRST_OFFSET
,
467 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
),
468 add_datasize
= xfs_dir2_data_entsize(args
->namelen
),
469 eof
= (char *)oldsfep
== &buf
[old_isize
];
471 offset
= new_offset
+ xfs_dir2_data_entsize(oldsfep
->namelen
),
472 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
),
473 eof
= (char *)oldsfep
== &buf
[old_isize
]) {
474 new_offset
= xfs_dir2_sf_get_offset(oldsfep
);
475 if (offset
+ add_datasize
<= new_offset
)
479 * Get rid of the old directory, then allocate space for
480 * the new one. We do this so xfs_idata_realloc won't copy
483 xfs_idata_realloc(dp
, -old_isize
, XFS_DATA_FORK
);
484 xfs_idata_realloc(dp
, new_isize
, XFS_DATA_FORK
);
486 * Reset the pointer since the buffer was reallocated.
488 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
490 * Copy the first part of the directory, including the header.
492 nbytes
= (int)((char *)oldsfep
- (char *)oldsfp
);
493 memcpy(sfp
, oldsfp
, nbytes
);
494 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ nbytes
);
496 * Fill in the new entry, and update the header counts.
498 sfep
->namelen
= args
->namelen
;
499 xfs_dir2_sf_put_offset(sfep
, offset
);
500 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
501 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
502 xfs_dir2_sf_inumberp(sfep
));
505 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& !objchange
)
509 * If there's more left to copy, do that.
512 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
513 memcpy(sfep
, oldsfep
, old_isize
- nbytes
);
515 kmem_free(buf
, old_isize
);
516 dp
->i_d
.di_size
= new_isize
;
517 xfs_dir2_sf_check(args
);
521 * Decide if the new entry will fit at all.
522 * If it will fit, pick between adding the new entry to the end (easy)
523 * or somewhere else (hard).
524 * Return 0 (won't fit), 1 (easy), 2 (hard).
527 static int /* pick result */
528 xfs_dir2_sf_addname_pick(
529 xfs_da_args_t
*args
, /* operation arguments */
530 int objchange
, /* inode # size changes */
531 xfs_dir2_sf_entry_t
**sfepp
, /* out(1): new entry ptr */
532 xfs_dir2_data_aoff_t
*offsetp
) /* out(1): new offset */
534 xfs_inode_t
*dp
; /* incore directory inode */
535 int holefit
; /* found hole it will fit in */
536 int i
; /* entry number */
537 xfs_mount_t
*mp
; /* filesystem mount point */
538 xfs_dir2_data_aoff_t offset
; /* data block offset */
539 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
540 xfs_dir2_sf_t
*sfp
; /* shortform structure */
541 int size
; /* entry's data size */
542 int used
; /* data bytes used */
547 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
548 size
= xfs_dir2_data_entsize(args
->namelen
);
549 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
550 sfep
= xfs_dir2_sf_firstentry(sfp
);
553 * Loop over sf entries.
554 * Keep track of data offset and whether we've seen a place
555 * to insert the new entry.
557 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
559 holefit
= offset
+ size
<= xfs_dir2_sf_get_offset(sfep
);
560 offset
= xfs_dir2_sf_get_offset(sfep
) +
561 xfs_dir2_data_entsize(sfep
->namelen
);
562 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
565 * Calculate data bytes used excluding the new entry, if this
566 * was a data block (block form directory).
569 (sfp
->hdr
.count
+ 3) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
570 (uint
)sizeof(xfs_dir2_block_tail_t
);
572 * If it won't fit in a block form then we can't insert it,
573 * we'll go back, convert to block, then try the insert and convert
576 if (used
+ (holefit
? 0 : size
) > mp
->m_dirblksize
)
579 * If changing the inode number size, do it the hard way.
586 ASSERT(objchange
== 0);
589 * If it won't fit at the end then do it the hard way (use the hole).
591 if (used
+ size
> mp
->m_dirblksize
)
594 * Do it the easy way.
603 * Check consistency of shortform directory, assert if bad.
607 xfs_da_args_t
*args
) /* operation arguments */
609 xfs_inode_t
*dp
; /* incore directory inode */
610 int i
; /* entry number */
611 int i8count
; /* number of big inode#s */
612 xfs_ino_t ino
; /* entry inode number */
613 int offset
; /* data offset */
614 xfs_dir2_sf_entry_t
*sfep
; /* shortform dir entry */
615 xfs_dir2_sf_t
*sfp
; /* shortform structure */
619 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
620 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
621 ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
622 i8count
= ino
> XFS_DIR2_MAX_SHORT_INUM
;
624 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
626 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
627 ASSERT(xfs_dir2_sf_get_offset(sfep
) >= offset
);
628 ino
= xfs_dir2_sf_get_inumber(sfp
, xfs_dir2_sf_inumberp(sfep
));
629 i8count
+= ino
> XFS_DIR2_MAX_SHORT_INUM
;
631 xfs_dir2_sf_get_offset(sfep
) +
632 xfs_dir2_data_entsize(sfep
->namelen
);
634 ASSERT(i8count
== sfp
->hdr
.i8count
);
635 ASSERT(XFS_BIG_INUMS
|| i8count
== 0);
636 ASSERT((char *)sfep
- (char *)sfp
== dp
->i_d
.di_size
);
638 (sfp
->hdr
.count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
639 (uint
)sizeof(xfs_dir2_block_tail_t
) <=
640 dp
->i_mount
->m_dirblksize
);
645 * Create a new (shortform) directory.
647 int /* error, always 0 */
649 xfs_da_args_t
*args
, /* operation arguments */
650 xfs_ino_t pino
) /* parent inode number */
652 xfs_inode_t
*dp
; /* incore directory inode */
653 int i8count
; /* parent inode is an 8-byte number */
654 xfs_dir2_sf_t
*sfp
; /* shortform structure */
655 int size
; /* directory size */
657 xfs_dir2_trace_args_i("sf_create", args
, pino
);
661 ASSERT(dp
->i_d
.di_size
== 0);
663 * If it's currently a zero-length extent file,
664 * convert it to local format.
666 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
) {
667 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
668 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
669 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
670 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
672 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
673 ASSERT(dp
->i_df
.if_bytes
== 0);
674 i8count
= pino
> XFS_DIR2_MAX_SHORT_INUM
;
675 size
= xfs_dir2_sf_hdr_size(i8count
);
677 * Make a buffer for the data.
679 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
681 * Fill in the header,
683 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
684 sfp
->hdr
.i8count
= i8count
;
686 * Now can put in the inode number, since i8count is set.
688 xfs_dir2_sf_put_inumber(sfp
, &pino
, &sfp
->hdr
.parent
);
690 dp
->i_d
.di_size
= size
;
691 xfs_dir2_sf_check(args
);
692 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
697 xfs_dir2_sf_getdents(
698 xfs_inode_t
*dp
, /* incore directory inode */
703 int i
; /* shortform entry number */
704 xfs_mount_t
*mp
; /* filesystem mount point */
705 xfs_dir2_dataptr_t off
; /* current entry's offset */
706 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
707 xfs_dir2_sf_t
*sfp
; /* shortform structure */
708 xfs_dir2_dataptr_t dot_offset
;
709 xfs_dir2_dataptr_t dotdot_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 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
724 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
726 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
728 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
731 * If the block number in the offset is out of range, we're done.
733 if (xfs_dir2_dataptr_to_db(mp
, *offset
) > mp
->m_dirdatablk
)
737 * Precalculate offsets for . and .. as we will always need them.
739 * XXX(hch): the second argument is sometimes 0 and sometimes
742 dot_offset
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
743 XFS_DIR2_DATA_DOT_OFFSET
);
744 dotdot_offset
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
745 XFS_DIR2_DATA_DOTDOT_OFFSET
);
748 * Put . entry unless we're starting past it.
750 if (*offset
<= dot_offset
) {
755 if (filldir(dirent
, ".", 1, dot_offset
, ino
, DT_DIR
)) {
756 *offset
= dot_offset
;
762 * Put .. entry unless we're starting past it.
764 if (*offset
<= dotdot_offset
) {
765 ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
769 if (filldir(dirent
, "..", 2, dotdot_offset
, ino
, DT_DIR
)) {
770 *offset
= dotdot_offset
;
776 * Loop while there are more entries and put'ing works.
778 sfep
= xfs_dir2_sf_firstentry(sfp
);
779 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
780 off
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
781 xfs_dir2_sf_get_offset(sfep
));
784 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
788 ino
= xfs_dir2_sf_get_inumber(sfp
, xfs_dir2_sf_inumberp(sfep
));
793 if (filldir(dirent
, sfep
->name
, sfep
->namelen
,
794 off
, ino
, DT_UNKNOWN
)) {
798 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
801 *offset
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
+ 1, 0);
806 * Lookup an entry in a shortform directory.
807 * Returns EEXIST if found, ENOENT if not found.
811 xfs_da_args_t
*args
) /* operation arguments */
813 xfs_inode_t
*dp
; /* incore directory inode */
814 int i
; /* entry index */
815 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
816 xfs_dir2_sf_t
*sfp
; /* shortform structure */
818 xfs_dir2_trace_args("sf_lookup", args
);
819 xfs_dir2_sf_check(args
);
822 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
824 * Bail out if the directory is way too short.
826 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
827 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
828 return XFS_ERROR(EIO
);
830 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
831 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
832 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
833 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
837 if (args
->namelen
== 1 && args
->name
[0] == '.') {
838 args
->inumber
= dp
->i_ino
;
839 return XFS_ERROR(EEXIST
);
842 * Special case for ..
844 if (args
->namelen
== 2 &&
845 args
->name
[0] == '.' && args
->name
[1] == '.') {
846 args
->inumber
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
847 return XFS_ERROR(EEXIST
);
850 * Loop over all the entries trying to match ours.
852 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
854 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
855 if (sfep
->namelen
== args
->namelen
&&
856 sfep
->name
[0] == args
->name
[0] &&
857 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
859 xfs_dir2_sf_get_inumber(sfp
,
860 xfs_dir2_sf_inumberp(sfep
));
861 return XFS_ERROR(EEXIST
);
867 ASSERT(args
->oknoent
);
868 return XFS_ERROR(ENOENT
);
872 * Remove an entry from a shortform directory.
875 xfs_dir2_sf_removename(
878 int byteoff
; /* offset of removed entry */
879 xfs_inode_t
*dp
; /* incore directory inode */
880 int entsize
; /* this entry's size */
881 int i
; /* shortform entry index */
882 int newsize
; /* new inode size */
883 int oldsize
; /* old inode size */
884 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
885 xfs_dir2_sf_t
*sfp
; /* shortform structure */
887 xfs_dir2_trace_args("sf_removename", args
);
890 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
891 oldsize
= (int)dp
->i_d
.di_size
;
893 * Bail out if the directory is way too short.
895 if (oldsize
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
896 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
897 return XFS_ERROR(EIO
);
899 ASSERT(dp
->i_df
.if_bytes
== oldsize
);
900 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
901 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
902 ASSERT(oldsize
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
904 * Loop over the old directory entries.
905 * Find the one we're deleting.
907 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
909 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
910 if (sfep
->namelen
== args
->namelen
&&
911 sfep
->name
[0] == args
->name
[0] &&
912 memcmp(sfep
->name
, args
->name
, args
->namelen
) == 0) {
913 ASSERT(xfs_dir2_sf_get_inumber(sfp
,
914 xfs_dir2_sf_inumberp(sfep
)) ==
922 if (i
== sfp
->hdr
.count
) {
923 return XFS_ERROR(ENOENT
);
928 byteoff
= (int)((char *)sfep
- (char *)sfp
);
929 entsize
= xfs_dir2_sf_entsize_byname(sfp
, args
->namelen
);
930 newsize
= oldsize
- entsize
;
932 * Copy the part if any after the removed entry, sliding it down.
934 if (byteoff
+ entsize
< oldsize
)
935 memmove((char *)sfp
+ byteoff
, (char *)sfp
+ byteoff
+ entsize
,
936 oldsize
- (byteoff
+ entsize
));
938 * Fix up the header and file size.
941 dp
->i_d
.di_size
= newsize
;
943 * Reallocate, making it smaller.
945 xfs_idata_realloc(dp
, newsize
- oldsize
, XFS_DATA_FORK
);
946 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
949 * Are we changing inode number size?
951 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
952 if (sfp
->hdr
.i8count
== 1)
953 xfs_dir2_sf_toino4(args
);
958 xfs_dir2_sf_check(args
);
959 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
964 * Replace the inode number of an entry in a shortform directory.
968 xfs_da_args_t
*args
) /* operation arguments */
970 xfs_inode_t
*dp
; /* incore directory inode */
971 int i
; /* entry index */
972 #if XFS_BIG_INUMS || defined(DEBUG)
973 xfs_ino_t ino
=0; /* entry old inode number */
976 int i8elevated
; /* sf_toino8 set i8count=1 */
978 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
979 xfs_dir2_sf_t
*sfp
; /* shortform structure */
981 xfs_dir2_trace_args("sf_replace", args
);
984 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
986 * Bail out if the shortform directory is way too small.
988 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
989 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
990 return XFS_ERROR(EIO
);
992 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
993 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
994 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
995 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->hdr
.i8count
));
998 * New inode number is large, and need to convert to 8-byte inodes.
1000 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
1001 int error
; /* error return value */
1002 int newsize
; /* new inode size */
1006 (sfp
->hdr
.count
+ 1) *
1007 ((uint
)sizeof(xfs_dir2_ino8_t
) -
1008 (uint
)sizeof(xfs_dir2_ino4_t
));
1010 * Won't fit as shortform, convert to block then do replace.
1012 if (newsize
> XFS_IFORK_DSIZE(dp
)) {
1013 error
= xfs_dir2_sf_to_block(args
);
1017 return xfs_dir2_block_replace(args
);
1020 * Still fits, convert to 8-byte now.
1022 xfs_dir2_sf_toino8(args
);
1024 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1028 ASSERT(args
->namelen
!= 1 || args
->name
[0] != '.');
1030 * Replace ..'s entry.
1032 if (args
->namelen
== 2 &&
1033 args
->name
[0] == '.' && args
->name
[1] == '.') {
1034 #if XFS_BIG_INUMS || defined(DEBUG)
1035 ino
= xfs_dir2_sf_get_inumber(sfp
, &sfp
->hdr
.parent
);
1036 ASSERT(args
->inumber
!= ino
);
1038 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
, &sfp
->hdr
.parent
);
1041 * Normal entry, look for the name.
1044 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
1046 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
)) {
1047 if (sfep
->namelen
== args
->namelen
&&
1048 sfep
->name
[0] == args
->name
[0] &&
1049 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
1050 #if XFS_BIG_INUMS || defined(DEBUG)
1051 ino
= xfs_dir2_sf_get_inumber(sfp
,
1052 xfs_dir2_sf_inumberp(sfep
));
1053 ASSERT(args
->inumber
!= ino
);
1055 xfs_dir2_sf_put_inumber(sfp
, &args
->inumber
,
1056 xfs_dir2_sf_inumberp(sfep
));
1063 if (i
== sfp
->hdr
.count
) {
1064 ASSERT(args
->oknoent
);
1067 xfs_dir2_sf_toino4(args
);
1069 return XFS_ERROR(ENOENT
);
1074 * See if the old number was large, the new number is small.
1076 if (ino
> XFS_DIR2_MAX_SHORT_INUM
&&
1077 args
->inumber
<= XFS_DIR2_MAX_SHORT_INUM
) {
1079 * And the old count was one, so need to convert to small.
1081 if (sfp
->hdr
.i8count
== 1)
1082 xfs_dir2_sf_toino4(args
);
1087 * See if the old number was small, the new number is large.
1089 if (ino
<= XFS_DIR2_MAX_SHORT_INUM
&&
1090 args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
1092 * add to the i8count unless we just converted to 8-byte
1093 * inodes (which does an implied i8count = 1)
1095 ASSERT(sfp
->hdr
.i8count
!= 0);
1100 xfs_dir2_sf_check(args
);
1101 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_DDATA
);
1107 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1108 * The last 8-byte inode number is gone, but the count is still 1.
1112 xfs_da_args_t
*args
) /* operation arguments */
1114 char *buf
; /* old dir's buffer */
1115 xfs_inode_t
*dp
; /* incore directory inode */
1116 int i
; /* entry index */
1117 xfs_ino_t ino
; /* entry inode number */
1118 int newsize
; /* new inode size */
1119 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1120 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
1121 int oldsize
; /* old inode size */
1122 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1123 xfs_dir2_sf_t
*sfp
; /* new sf directory */
1125 xfs_dir2_trace_args("sf_toino4", args
);
1129 * Copy the old directory to the buffer.
1130 * Then nuke it from the inode, and add the new buffer to the inode.
1131 * Don't want xfs_idata_realloc copying the data here.
1133 oldsize
= dp
->i_df
.if_bytes
;
1134 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1135 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1136 ASSERT(oldsfp
->hdr
.i8count
== 1);
1137 memcpy(buf
, oldsfp
, oldsize
);
1139 * Compute the new inode size.
1143 (oldsfp
->hdr
.count
+ 1) *
1144 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1145 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1146 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1148 * Reset our pointers, the data has moved.
1150 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1151 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1153 * Fill in the new header.
1155 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1156 sfp
->hdr
.i8count
= 0;
1157 ino
= xfs_dir2_sf_get_inumber(oldsfp
, &oldsfp
->hdr
.parent
);
1158 xfs_dir2_sf_put_inumber(sfp
, &ino
, &sfp
->hdr
.parent
);
1160 * Copy the entries field by field.
1162 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
),
1163 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
);
1165 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
),
1166 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
)) {
1167 sfep
->namelen
= oldsfep
->namelen
;
1168 sfep
->offset
= oldsfep
->offset
;
1169 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1170 ino
= xfs_dir2_sf_get_inumber(oldsfp
,
1171 xfs_dir2_sf_inumberp(oldsfep
));
1172 xfs_dir2_sf_put_inumber(sfp
, &ino
, xfs_dir2_sf_inumberp(sfep
));
1175 * Clean up the inode.
1177 kmem_free(buf
, oldsize
);
1178 dp
->i_d
.di_size
= newsize
;
1179 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1183 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1184 * The new 8-byte inode number is not there yet, we leave with the
1185 * count 1 but no corresponding entry.
1189 xfs_da_args_t
*args
) /* operation arguments */
1191 char *buf
; /* old dir's buffer */
1192 xfs_inode_t
*dp
; /* incore directory inode */
1193 int i
; /* entry index */
1194 xfs_ino_t ino
; /* entry inode number */
1195 int newsize
; /* new inode size */
1196 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1197 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
1198 int oldsize
; /* old inode size */
1199 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1200 xfs_dir2_sf_t
*sfp
; /* new sf directory */
1202 xfs_dir2_trace_args("sf_toino8", args
);
1206 * Copy the old directory to the buffer.
1207 * Then nuke it from the inode, and add the new buffer to the inode.
1208 * Don't want xfs_idata_realloc copying the data here.
1210 oldsize
= dp
->i_df
.if_bytes
;
1211 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1212 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1213 ASSERT(oldsfp
->hdr
.i8count
== 0);
1214 memcpy(buf
, oldsfp
, oldsize
);
1216 * Compute the new inode size.
1220 (oldsfp
->hdr
.count
+ 1) *
1221 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1222 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1223 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1225 * Reset our pointers, the data has moved.
1227 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1228 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1230 * Fill in the new header.
1232 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1233 sfp
->hdr
.i8count
= 1;
1234 ino
= xfs_dir2_sf_get_inumber(oldsfp
, &oldsfp
->hdr
.parent
);
1235 xfs_dir2_sf_put_inumber(sfp
, &ino
, &sfp
->hdr
.parent
);
1237 * Copy the entries field by field.
1239 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
),
1240 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
);
1242 i
++, sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
),
1243 oldsfep
= xfs_dir2_sf_nextentry(oldsfp
, oldsfep
)) {
1244 sfep
->namelen
= oldsfep
->namelen
;
1245 sfep
->offset
= oldsfep
->offset
;
1246 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1247 ino
= xfs_dir2_sf_get_inumber(oldsfp
,
1248 xfs_dir2_sf_inumberp(oldsfep
));
1249 xfs_dir2_sf_put_inumber(sfp
, &ino
, xfs_dir2_sf_inumberp(sfep
));
1252 * Clean up the inode.
1254 kmem_free(buf
, oldsize
);
1255 dp
->i_d
.di_size
= newsize
;
1256 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1258 #endif /* XFS_BIG_INUMS */