2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 * Shortform directory implementation for v2 directories.
42 * Given a block directory (dp/block), calculate its size as a shortform (sf)
43 * directory and a header for the sf directory, if it will fit it the
44 * space currently present in the inode. If it won't fit, the output
45 * size is too big (but not accurate).
47 int /* size for sf form */
48 xfs_dir2_block_sfsize(
49 xfs_inode_t
*dp
, /* incore inode pointer */
50 xfs_dir2_block_t
*block
, /* block directory data */
51 xfs_dir2_sf_hdr_t
*sfhp
) /* output: header for sf form */
53 xfs_dir2_dataptr_t addr
; /* data entry address */
54 xfs_dir2_leaf_entry_t
*blp
; /* leaf area of the block */
55 xfs_dir2_block_tail_t
*btp
; /* tail area of the block */
56 int count
; /* shortform entry count */
57 xfs_dir2_data_entry_t
*dep
; /* data entry in the block */
58 int i
; /* block entry index */
59 int i8count
; /* count of big-inode entries */
60 int isdot
; /* entry is "." */
61 int isdotdot
; /* entry is ".." */
62 xfs_mount_t
*mp
; /* mount structure pointer */
63 int namelen
; /* total name bytes */
64 xfs_ino_t parent
; /* parent inode number */
65 int size
=0; /* total computed size */
69 count
= i8count
= namelen
= 0;
70 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
71 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
74 * Iterate over the block's data entries by using the leaf pointers.
76 for (i
= 0; i
< INT_GET(btp
->count
, ARCH_CONVERT
); i
++) {
77 if ((addr
= INT_GET(blp
[i
].address
, ARCH_CONVERT
)) == XFS_DIR2_NULL_DATAPTR
)
80 * Calculate the pointer to the entry at hand.
82 dep
= (xfs_dir2_data_entry_t
*)
83 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, addr
));
85 * Detect . and .., so we can special-case them.
86 * . is not included in sf directories.
87 * .. is included by just the parent inode number.
89 isdot
= dep
->namelen
== 1 && dep
->name
[0] == '.';
92 dep
->name
[0] == '.' && dep
->name
[1] == '.';
93 #if XFS_BIG_FILESYSTEMS
95 i8count
+= INT_GET(dep
->inumber
, ARCH_CONVERT
) > XFS_DIR2_MAX_SHORT_INUM
;
97 if (!isdot
&& !isdotdot
) {
99 namelen
+= dep
->namelen
;
101 parent
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
103 * Calculate the new size, see if we should give up yet.
105 size
= XFS_DIR2_SF_HDR_SIZE(i8count
) + /* header */
106 count
+ /* namelen */
107 count
* (uint
)sizeof(xfs_dir2_sf_off_t
) + /* offset */
109 (i8count
? /* inumber */
110 (uint
)sizeof(xfs_dir2_ino8_t
) * count
:
111 (uint
)sizeof(xfs_dir2_ino4_t
) * count
);
112 if (size
> XFS_IFORK_DSIZE(dp
))
113 return size
; /* size value is a failure */
116 * Create the output header, if it worked.
119 sfhp
->i8count
= i8count
;
120 XFS_DIR2_SF_PUT_INUMBER_ARCH((xfs_dir2_sf_t
*)sfhp
, &parent
, &sfhp
->parent
, ARCH_CONVERT
);
125 * Convert a block format directory to shortform.
126 * Caller has already checked that it will fit, and built us a header.
129 xfs_dir2_block_to_sf(
130 xfs_da_args_t
*args
, /* operation arguments */
131 xfs_dabuf_t
*bp
, /* block buffer */
132 int size
, /* shortform directory size */
133 xfs_dir2_sf_hdr_t
*sfhp
) /* shortform directory hdr */
135 xfs_dir2_block_t
*block
; /* block structure */
136 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
137 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
138 xfs_inode_t
*dp
; /* incore directory inode */
139 xfs_dir2_data_unused_t
*dup
; /* unused data pointer */
140 char *endptr
; /* end of data entries */
141 int error
; /* error return value */
142 int logflags
; /* inode logging flags */
143 xfs_mount_t
*mp
; /* filesystem mount point */
144 char *ptr
; /* current data pointer */
145 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
146 xfs_dir2_sf_t
*sfp
; /* shortform structure */
149 xfs_dir2_trace_args_sb("block_to_sf", args
, size
, bp
);
154 * Make a copy of the block data, so we can shrink the inode
155 * and add local data.
157 block
= kmem_alloc(mp
->m_dirblksize
, KM_SLEEP
);
158 memcpy(block
, bp
->data
, mp
->m_dirblksize
);
159 logflags
= XFS_ILOG_CORE
;
160 if ((error
= xfs_dir2_shrink_inode(args
, mp
->m_dirdatablk
, bp
))) {
161 ASSERT(error
!= ENOSPC
);
165 * The buffer is now unconditionally gone, whether
166 * xfs_dir2_shrink_inode worked or not.
168 * Convert the inode to local format.
170 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
;
171 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
172 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
173 ASSERT(dp
->i_df
.if_bytes
== 0);
174 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
175 logflags
|= XFS_ILOG_DDATA
;
177 * Copy the header into the newly allocate local space.
179 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
180 memcpy(sfp
, sfhp
, XFS_DIR2_SF_HDR_SIZE(sfhp
->i8count
));
181 dp
->i_d
.di_size
= size
;
183 * Set up to loop over the block's entries.
185 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
186 ptr
= (char *)block
->u
;
187 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
188 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
190 * Loop over the active and unused entries.
191 * Stop when we reach the leaf/tail portion of the block.
193 while (ptr
< endptr
) {
195 * If it's unused, just skip over it.
197 dup
= (xfs_dir2_data_unused_t
*)ptr
;
198 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
199 ptr
+= INT_GET(dup
->length
, ARCH_CONVERT
);
202 dep
= (xfs_dir2_data_entry_t
*)ptr
;
206 if (dep
->namelen
== 1 && dep
->name
[0] == '.')
207 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) == dp
->i_ino
);
209 * Skip .., but make sure the inode number is right.
211 else if (dep
->namelen
== 2 &&
212 dep
->name
[0] == '.' && dep
->name
[1] == '.')
213 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) ==
214 XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
));
216 * Normal entry, copy it into shortform.
219 sfep
->namelen
= dep
->namelen
;
220 XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep
,
221 (xfs_dir2_data_aoff_t
)
222 ((char *)dep
- (char *)block
), ARCH_CONVERT
);
223 memcpy(sfep
->name
, dep
->name
, dep
->namelen
);
224 temp
=INT_GET(dep
->inumber
, ARCH_CONVERT
);
225 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &temp
,
226 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
227 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
229 ptr
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
231 ASSERT((char *)sfep
- (char *)sfp
== size
);
232 xfs_dir2_sf_check(args
);
234 xfs_trans_log_inode(args
->trans
, dp
, logflags
);
235 kmem_free(block
, mp
->m_dirblksize
);
240 * Add a name to a shortform directory.
241 * There are two algorithms, "easy" and "hard" which we decide on
242 * before changing anything.
243 * Convert to block form if necessary, if the new entry won't fit.
247 xfs_da_args_t
*args
) /* operation arguments */
249 int add_entsize
; /* size of the new entry */
250 xfs_inode_t
*dp
; /* incore directory inode */
251 int error
; /* error return value */
252 int incr_isize
; /* total change in size */
253 int new_isize
; /* di_size after adding name */
254 int objchange
; /* changing to 8-byte inodes */
255 xfs_dir2_data_aoff_t offset
; /* offset for new entry */
256 int old_isize
; /* di_size before adding name */
257 int pick
; /* which algorithm to use */
258 xfs_dir2_sf_t
*sfp
; /* shortform structure */
259 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
261 xfs_dir2_trace_args("sf_addname", args
);
262 ASSERT(xfs_dir2_sf_lookup(args
) == ENOENT
);
264 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
266 * Make sure the shortform value has some of its header.
268 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
269 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
270 return XFS_ERROR(EIO
);
272 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
273 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
274 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
275 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
277 * Compute entry (and change in) size.
279 add_entsize
= XFS_DIR2_SF_ENTSIZE_BYNAME(sfp
, args
->namelen
);
280 incr_isize
= add_entsize
;
281 #if XFS_BIG_FILESYSTEMS
283 * Do we have to change to 8 byte inodes?
285 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
287 * Yes, adjust the entry size and the total size.
290 (uint
)sizeof(xfs_dir2_ino8_t
) -
291 (uint
)sizeof(xfs_dir2_ino4_t
);
293 (sfp
->hdr
.count
+ 2) *
294 ((uint
)sizeof(xfs_dir2_ino8_t
) -
295 (uint
)sizeof(xfs_dir2_ino4_t
));
302 old_isize
= (int)dp
->i_d
.di_size
;
303 new_isize
= old_isize
+ incr_isize
;
305 * Won't fit as shortform any more (due to size),
306 * or the pick routine says it won't (due to offset values).
308 if (new_isize
> XFS_IFORK_DSIZE(dp
) ||
310 xfs_dir2_sf_addname_pick(args
, objchange
, &sfep
, &offset
)) == 0) {
312 * Just checking or no space reservation, it doesn't fit.
314 if (args
->justcheck
|| args
->total
== 0)
315 return XFS_ERROR(ENOSPC
);
317 * Convert to block form then add the name.
319 error
= xfs_dir2_sf_to_block(args
);
322 return xfs_dir2_block_addname(args
);
325 * Just checking, it fits.
330 * Do it the easy way - just add it at the end.
333 xfs_dir2_sf_addname_easy(args
, sfep
, offset
, new_isize
);
335 * Do it the hard way - look for a place to insert the new entry.
336 * Convert to 8 byte inode numbers first if necessary.
340 #if XFS_BIG_FILESYSTEMS
342 xfs_dir2_sf_toino8(args
);
344 xfs_dir2_sf_addname_hard(args
, objchange
, new_isize
);
346 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
351 * Add the new entry the "easy" way.
352 * This is copying the old directory and adding the new entry at the end.
353 * Since it's sorted by "offset" we need room after the last offset
354 * that's already there, and then room to convert to a block directory.
355 * This is already checked by the pick routine.
358 xfs_dir2_sf_addname_easy(
359 xfs_da_args_t
*args
, /* operation arguments */
360 xfs_dir2_sf_entry_t
*sfep
, /* pointer to new entry */
361 xfs_dir2_data_aoff_t offset
, /* offset to use for new ent */
362 int new_isize
) /* new directory size */
364 int byteoff
; /* byte offset in sf dir */
365 xfs_inode_t
*dp
; /* incore directory inode */
366 xfs_dir2_sf_t
*sfp
; /* shortform structure */
370 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
371 byteoff
= (int)((char *)sfep
- (char *)sfp
);
373 * Grow the in-inode space.
375 xfs_idata_realloc(dp
, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp
, args
->namelen
),
378 * Need to set up again due to realloc of the inode data.
380 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
381 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ byteoff
);
383 * Fill in the new entry.
385 sfep
->namelen
= args
->namelen
;
386 XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep
, offset
, ARCH_CONVERT
);
387 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
388 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &args
->inumber
,
389 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
391 * Update the header and inode.
394 #if XFS_BIG_FILESYSTEMS
395 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
)
398 dp
->i_d
.di_size
= new_isize
;
399 xfs_dir2_sf_check(args
);
403 * Add the new entry the "hard" way.
404 * The caller has already converted to 8 byte inode numbers if necessary,
405 * in which case we need to leave the i8count at 1.
406 * Find a hole that the new entry will fit into, and copy
407 * the first part of the entries, the new entry, and the last part of
412 xfs_dir2_sf_addname_hard(
413 xfs_da_args_t
*args
, /* operation arguments */
414 int objchange
, /* changing inode number size */
415 int new_isize
) /* new directory size */
417 int add_datasize
; /* data size need for new ent */
418 char *buf
; /* buffer for old */
419 xfs_inode_t
*dp
; /* incore directory inode */
420 int eof
; /* reached end of old dir */
421 int nbytes
; /* temp for byte copies */
422 xfs_dir2_data_aoff_t new_offset
; /* next offset value */
423 xfs_dir2_data_aoff_t offset
; /* current offset value */
424 int old_isize
; /* previous di_size */
425 xfs_dir2_sf_entry_t
*oldsfep
; /* entry in original dir */
426 xfs_dir2_sf_t
*oldsfp
; /* original shortform dir */
427 xfs_dir2_sf_entry_t
*sfep
; /* entry in new dir */
428 xfs_dir2_sf_t
*sfp
; /* new shortform dir */
431 * Copy the old directory to the stack buffer.
435 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
436 old_isize
= (int)dp
->i_d
.di_size
;
437 buf
= kmem_alloc(old_isize
, KM_SLEEP
);
438 oldsfp
= (xfs_dir2_sf_t
*)buf
;
439 memcpy(oldsfp
, sfp
, old_isize
);
441 * Loop over the old directory finding the place we're going
442 * to insert the new entry.
443 * If it's going to end up at the end then oldsfep will point there.
445 for (offset
= XFS_DIR2_DATA_FIRST_OFFSET
,
446 oldsfep
= XFS_DIR2_SF_FIRSTENTRY(oldsfp
),
447 add_datasize
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
),
448 eof
= (char *)oldsfep
== &buf
[old_isize
];
450 offset
= new_offset
+ XFS_DIR2_DATA_ENTSIZE(oldsfep
->namelen
),
451 oldsfep
= XFS_DIR2_SF_NEXTENTRY(oldsfp
, oldsfep
),
452 eof
= (char *)oldsfep
== &buf
[old_isize
]) {
453 new_offset
= XFS_DIR2_SF_GET_OFFSET_ARCH(oldsfep
, ARCH_CONVERT
);
454 if (offset
+ add_datasize
<= new_offset
)
458 * Get rid of the old directory, then allocate space for
459 * the new one. We do this so xfs_idata_realloc won't copy
462 xfs_idata_realloc(dp
, -old_isize
, XFS_DATA_FORK
);
463 xfs_idata_realloc(dp
, new_isize
, XFS_DATA_FORK
);
465 * Reset the pointer since the buffer was reallocated.
467 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
469 * Copy the first part of the directory, including the header.
471 nbytes
= (int)((char *)oldsfep
- (char *)oldsfp
);
472 memcpy(sfp
, oldsfp
, nbytes
);
473 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ nbytes
);
475 * Fill in the new entry, and update the header counts.
477 sfep
->namelen
= args
->namelen
;
478 XFS_DIR2_SF_PUT_OFFSET_ARCH(sfep
, offset
, ARCH_CONVERT
);
479 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
480 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &args
->inumber
,
481 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
483 #if XFS_BIG_FILESYSTEMS
484 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& !objchange
)
488 * If there's more left to copy, do that.
491 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
492 memcpy(sfep
, oldsfep
, old_isize
- nbytes
);
494 kmem_free(buf
, old_isize
);
495 dp
->i_d
.di_size
= new_isize
;
496 xfs_dir2_sf_check(args
);
500 * Decide if the new entry will fit at all.
501 * If it will fit, pick between adding the new entry to the end (easy)
502 * or somewhere else (hard).
503 * Return 0 (won't fit), 1 (easy), 2 (hard).
506 STATIC
int /* pick result */
507 xfs_dir2_sf_addname_pick(
508 xfs_da_args_t
*args
, /* operation arguments */
509 int objchange
, /* inode # size changes */
510 xfs_dir2_sf_entry_t
**sfepp
, /* out(1): new entry ptr */
511 xfs_dir2_data_aoff_t
*offsetp
) /* out(1): new offset */
513 xfs_inode_t
*dp
; /* incore directory inode */
514 int holefit
; /* found hole it will fit in */
515 int i
; /* entry number */
516 xfs_mount_t
*mp
; /* filesystem mount point */
517 xfs_dir2_data_aoff_t offset
; /* data block offset */
518 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
519 xfs_dir2_sf_t
*sfp
; /* shortform structure */
520 int size
; /* entry's data size */
521 int used
; /* data bytes used */
526 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
527 size
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
528 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
529 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
532 * Loop over sf entries.
533 * Keep track of data offset and whether we've seen a place
534 * to insert the new entry.
536 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
538 holefit
= offset
+ size
<= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
);
539 offset
= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
) +
540 XFS_DIR2_DATA_ENTSIZE(sfep
->namelen
);
541 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
544 * Calculate data bytes used excluding the new entry, if this
545 * was a data block (block form directory).
548 (sfp
->hdr
.count
+ 3) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
549 (uint
)sizeof(xfs_dir2_block_tail_t
);
551 * If it won't fit in a block form then we can't insert it,
552 * we'll go back, convert to block, then try the insert and convert
555 if (used
+ (holefit
? 0 : size
) > mp
->m_dirblksize
)
558 * If changing the inode number size, do it the hard way.
560 #if XFS_BIG_FILESYSTEMS
565 ASSERT(objchange
== 0);
568 * If it won't fit at the end then do it the hard way (use the hole).
570 if (used
+ size
> mp
->m_dirblksize
)
573 * Do it the easy way.
582 * Check consistency of shortform directory, assert if bad.
586 xfs_da_args_t
*args
) /* operation arguments */
588 xfs_inode_t
*dp
; /* incore directory inode */
589 int i
; /* entry number */
590 int i8count
; /* number of big inode#s */
591 xfs_ino_t ino
; /* entry inode number */
592 int offset
; /* data offset */
593 xfs_dir2_sf_entry_t
*sfep
; /* shortform dir entry */
594 xfs_dir2_sf_t
*sfp
; /* shortform structure */
598 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
599 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
600 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
601 i8count
= ino
> XFS_DIR2_MAX_SHORT_INUM
;
603 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
605 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
)) {
606 ASSERT(XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
) >= offset
);
607 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
608 i8count
+= ino
> XFS_DIR2_MAX_SHORT_INUM
;
610 XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
) +
611 XFS_DIR2_DATA_ENTSIZE(sfep
->namelen
);
613 ASSERT(i8count
== sfp
->hdr
.i8count
);
614 #if !XFS_BIG_FILESYSTEMS
615 ASSERT(i8count
== 0);
617 ASSERT((char *)sfep
- (char *)sfp
== dp
->i_d
.di_size
);
619 (sfp
->hdr
.count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
620 (uint
)sizeof(xfs_dir2_block_tail_t
) <=
621 dp
->i_mount
->m_dirblksize
);
626 * Create a new (shortform) directory.
628 int /* error, always 0 */
630 xfs_da_args_t
*args
, /* operation arguments */
631 xfs_ino_t pino
) /* parent inode number */
633 xfs_inode_t
*dp
; /* incore directory inode */
634 int i8count
; /* parent inode is an 8-byte number */
635 xfs_dir2_sf_t
*sfp
; /* shortform structure */
636 int size
; /* directory size */
638 xfs_dir2_trace_args_i("sf_create", args
, pino
);
642 ASSERT(dp
->i_d
.di_size
== 0);
644 * If it's currently a zero-length extent file,
645 * convert it to local format.
647 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
) {
648 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
649 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
650 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
651 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
653 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
654 ASSERT(dp
->i_df
.if_bytes
== 0);
655 i8count
= pino
> XFS_DIR2_MAX_SHORT_INUM
;
656 size
= XFS_DIR2_SF_HDR_SIZE(i8count
);
658 * Make a buffer for the data.
660 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
662 * Fill in the header,
664 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
665 sfp
->hdr
.i8count
= i8count
;
667 * Now can put in the inode number, since i8count is set.
669 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &pino
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
671 dp
->i_d
.di_size
= size
;
672 xfs_dir2_sf_check(args
);
673 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
678 * Lookup an entry in a shortform directory.
679 * Returns EEXIST if found, ENOENT if not found.
683 xfs_da_args_t
*args
) /* operation arguments */
685 xfs_inode_t
*dp
; /* incore directory inode */
686 int i
; /* entry index */
687 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
688 xfs_dir2_sf_t
*sfp
; /* shortform structure */
690 xfs_dir2_trace_args("sf_lookup", args
);
691 xfs_dir2_sf_check(args
);
694 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
696 * Bail out if the directory is way too short.
698 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
699 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
700 return XFS_ERROR(EIO
);
702 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
703 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
704 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
705 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
709 if (args
->namelen
== 1 && args
->name
[0] == '.') {
710 args
->inumber
= dp
->i_ino
;
711 return XFS_ERROR(EEXIST
);
714 * Special case for ..
716 if (args
->namelen
== 2 &&
717 args
->name
[0] == '.' && args
->name
[1] == '.') {
718 args
->inumber
= XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
719 return XFS_ERROR(EEXIST
);
722 * Loop over all the entries trying to match ours.
724 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
726 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
)) {
727 if (sfep
->namelen
== args
->namelen
&&
728 sfep
->name
[0] == args
->name
[0] &&
729 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
731 XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
,
732 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
733 return XFS_ERROR(EEXIST
);
739 ASSERT(args
->oknoent
);
740 return XFS_ERROR(ENOENT
);
744 * Remove an entry from a shortform directory.
747 xfs_dir2_sf_removename(
750 int byteoff
; /* offset of removed entry */
751 xfs_inode_t
*dp
; /* incore directory inode */
752 int entsize
; /* this entry's size */
753 int i
; /* shortform entry index */
754 int newsize
; /* new inode size */
755 int oldsize
; /* old inode size */
756 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
757 xfs_dir2_sf_t
*sfp
; /* shortform structure */
759 xfs_dir2_trace_args("sf_removename", args
);
762 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
763 oldsize
= (int)dp
->i_d
.di_size
;
765 * Bail out if the directory is way too short.
767 if (oldsize
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
768 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
769 return XFS_ERROR(EIO
);
771 ASSERT(dp
->i_df
.if_bytes
== oldsize
);
772 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
773 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
774 ASSERT(oldsize
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
776 * Loop over the old directory entries.
777 * Find the one we're deleting.
779 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
781 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
)) {
782 if (sfep
->namelen
== args
->namelen
&&
783 sfep
->name
[0] == args
->name
[0] &&
784 memcmp(sfep
->name
, args
->name
, args
->namelen
) == 0) {
785 ASSERT(XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
,
786 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
) ==
794 if (i
== sfp
->hdr
.count
) {
795 return XFS_ERROR(ENOENT
);
800 byteoff
= (int)((char *)sfep
- (char *)sfp
);
801 entsize
= XFS_DIR2_SF_ENTSIZE_BYNAME(sfp
, args
->namelen
);
802 newsize
= oldsize
- entsize
;
804 * Copy the part if any after the removed entry, sliding it down.
806 if (byteoff
+ entsize
< oldsize
)
807 memmove((char *)sfp
+ byteoff
, (char *)sfp
+ byteoff
+ entsize
,
808 oldsize
- (byteoff
+ entsize
));
810 * Fix up the header and file size.
813 dp
->i_d
.di_size
= newsize
;
815 * Reallocate, making it smaller.
817 xfs_idata_realloc(dp
, newsize
- oldsize
, XFS_DATA_FORK
);
818 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
819 #if XFS_BIG_FILESYSTEMS
821 * Are we changing inode number size?
823 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
824 if (sfp
->hdr
.i8count
== 1)
825 xfs_dir2_sf_toino4(args
);
830 xfs_dir2_sf_check(args
);
831 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
836 * Replace the inode number of an entry in a shortform directory.
840 xfs_da_args_t
*args
) /* operation arguments */
842 xfs_inode_t
*dp
; /* incore directory inode */
843 int i
; /* entry index */
844 #if XFS_BIG_FILESYSTEMS || defined(DEBUG)
845 xfs_ino_t ino
=0; /* entry old inode number */
847 #if XFS_BIG_FILESYSTEMS
848 int i8elevated
; /* sf_toino8 set i8count=1 */
850 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
851 xfs_dir2_sf_t
*sfp
; /* shortform structure */
853 xfs_dir2_trace_args("sf_replace", args
);
856 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
858 * Bail out if the shortform directory is way too small.
860 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
861 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
862 return XFS_ERROR(EIO
);
864 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
865 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
866 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
867 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
868 #if XFS_BIG_FILESYSTEMS
870 * New inode number is large, and need to convert to 8-byte inodes.
872 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->hdr
.i8count
== 0) {
873 int error
; /* error return value */
874 int newsize
; /* new inode size */
878 (sfp
->hdr
.count
+ 1) *
879 ((uint
)sizeof(xfs_dir2_ino8_t
) -
880 (uint
)sizeof(xfs_dir2_ino4_t
));
882 * Won't fit as shortform, convert to block then do replace.
884 if (newsize
> XFS_IFORK_DSIZE(dp
)) {
885 error
= xfs_dir2_sf_to_block(args
);
889 return xfs_dir2_block_replace(args
);
892 * Still fits, convert to 8-byte now.
894 xfs_dir2_sf_toino8(args
);
896 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
900 ASSERT(args
->namelen
!= 1 || args
->name
[0] != '.');
902 * Replace ..'s entry.
904 if (args
->namelen
== 2 &&
905 args
->name
[0] == '.' && args
->name
[1] == '.') {
906 #if XFS_BIG_FILESYSTEMS || defined(DEBUG)
907 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
908 ASSERT(args
->inumber
!= ino
);
910 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &args
->inumber
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
913 * Normal entry, look for the name.
916 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
918 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
)) {
919 if (sfep
->namelen
== args
->namelen
&&
920 sfep
->name
[0] == args
->name
[0] &&
921 memcmp(args
->name
, sfep
->name
, args
->namelen
) == 0) {
922 #if XFS_BIG_FILESYSTEMS || defined(DEBUG)
923 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
,
924 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
925 ASSERT(args
->inumber
!= ino
);
927 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &args
->inumber
,
928 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
935 if (i
== sfp
->hdr
.count
) {
936 ASSERT(args
->oknoent
);
937 #if XFS_BIG_FILESYSTEMS
939 xfs_dir2_sf_toino4(args
);
941 return XFS_ERROR(ENOENT
);
944 #if XFS_BIG_FILESYSTEMS
946 * See if the old number was large, the new number is small.
948 if (ino
> XFS_DIR2_MAX_SHORT_INUM
&&
949 args
->inumber
<= XFS_DIR2_MAX_SHORT_INUM
) {
951 * And the old count was one, so need to convert to small.
953 if (sfp
->hdr
.i8count
== 1)
954 xfs_dir2_sf_toino4(args
);
959 * See if the old number was small, the new number is large.
961 if (ino
<= XFS_DIR2_MAX_SHORT_INUM
&&
962 args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
964 * add to the i8count unless we just converted to 8-byte
965 * inodes (which does an implied i8count = 1)
967 ASSERT(sfp
->hdr
.i8count
!= 0);
972 xfs_dir2_sf_check(args
);
973 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_DDATA
);
977 #if XFS_BIG_FILESYSTEMS
979 * Convert from 8-byte inode numbers to 4-byte inode numbers.
980 * The last 8-byte inode number is gone, but the count is still 1.
984 xfs_da_args_t
*args
) /* operation arguments */
986 char *buf
; /* old dir's buffer */
987 xfs_inode_t
*dp
; /* incore directory inode */
988 int i
; /* entry index */
989 xfs_ino_t ino
; /* entry inode number */
990 int newsize
; /* new inode size */
991 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
992 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
993 int oldsize
; /* old inode size */
994 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
995 xfs_dir2_sf_t
*sfp
; /* new sf directory */
997 xfs_dir2_trace_args("sf_toino4", args
);
1001 * Copy the old directory to the buffer.
1002 * Then nuke it from the inode, and add the new buffer to the inode.
1003 * Don't want xfs_idata_realloc copying the data here.
1005 oldsize
= dp
->i_df
.if_bytes
;
1006 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1007 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1008 ASSERT(oldsfp
->hdr
.i8count
== 1);
1009 memcpy(buf
, oldsfp
, oldsize
);
1011 * Compute the new inode size.
1015 (oldsfp
->hdr
.count
+ 1) *
1016 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1017 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1018 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1020 * Reset our pointers, the data has moved.
1022 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1023 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1025 * Fill in the new header.
1027 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1028 sfp
->hdr
.i8count
= 0;
1029 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp
, &oldsfp
->hdr
.parent
, ARCH_CONVERT
);
1030 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &ino
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
1032 * Copy the entries field by field.
1034 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
),
1035 oldsfep
= XFS_DIR2_SF_FIRSTENTRY(oldsfp
);
1037 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
),
1038 oldsfep
= XFS_DIR2_SF_NEXTENTRY(oldsfp
, oldsfep
)) {
1039 sfep
->namelen
= oldsfep
->namelen
;
1040 sfep
->offset
= oldsfep
->offset
;
1041 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1042 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp
,
1043 XFS_DIR2_SF_INUMBERP(oldsfep
), ARCH_CONVERT
);
1044 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &ino
, XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
1047 * Clean up the inode.
1049 kmem_free(buf
, oldsize
);
1050 dp
->i_d
.di_size
= newsize
;
1051 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1055 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1056 * The new 8-byte inode number is not there yet, we leave with the
1057 * count 1 but no corresponding entry.
1061 xfs_da_args_t
*args
) /* operation arguments */
1063 char *buf
; /* old dir's buffer */
1064 xfs_inode_t
*dp
; /* incore directory inode */
1065 int i
; /* entry index */
1066 xfs_ino_t ino
; /* entry inode number */
1067 int newsize
; /* new inode size */
1068 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1069 xfs_dir2_sf_t
*oldsfp
; /* old sf directory */
1070 int oldsize
; /* old inode size */
1071 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1072 xfs_dir2_sf_t
*sfp
; /* new sf directory */
1074 xfs_dir2_trace_args("sf_toino8", args
);
1078 * Copy the old directory to the buffer.
1079 * Then nuke it from the inode, and add the new buffer to the inode.
1080 * Don't want xfs_idata_realloc copying the data here.
1082 oldsize
= dp
->i_df
.if_bytes
;
1083 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1084 oldsfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1085 ASSERT(oldsfp
->hdr
.i8count
== 0);
1086 memcpy(buf
, oldsfp
, oldsize
);
1088 * Compute the new inode size.
1092 (oldsfp
->hdr
.count
+ 1) *
1093 ((uint
)sizeof(xfs_dir2_ino8_t
) - (uint
)sizeof(xfs_dir2_ino4_t
));
1094 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1095 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1097 * Reset our pointers, the data has moved.
1099 oldsfp
= (xfs_dir2_sf_t
*)buf
;
1100 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1102 * Fill in the new header.
1104 sfp
->hdr
.count
= oldsfp
->hdr
.count
;
1105 sfp
->hdr
.i8count
= 1;
1106 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp
, &oldsfp
->hdr
.parent
, ARCH_CONVERT
);
1107 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &ino
, &sfp
->hdr
.parent
, ARCH_CONVERT
);
1109 * Copy the entries field by field.
1111 for (i
= 0, sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
),
1112 oldsfep
= XFS_DIR2_SF_FIRSTENTRY(oldsfp
);
1114 i
++, sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
),
1115 oldsfep
= XFS_DIR2_SF_NEXTENTRY(oldsfp
, oldsfep
)) {
1116 sfep
->namelen
= oldsfep
->namelen
;
1117 sfep
->offset
= oldsfep
->offset
;
1118 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1119 ino
= XFS_DIR2_SF_GET_INUMBER_ARCH(oldsfp
,
1120 XFS_DIR2_SF_INUMBERP(oldsfep
), ARCH_CONVERT
);
1121 XFS_DIR2_SF_PUT_INUMBER_ARCH(sfp
, &ino
, XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
);
1124 * Clean up the inode.
1126 kmem_free(buf
, oldsize
);
1127 dp
->i_d
.di_size
= newsize
;
1128 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1130 #endif /* XFS_BIG_FILESYSTEMS */