1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_da_format.h"
13 #include "xfs_da_btree.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trace.h"
21 * Prototypes for internal functions.
23 static void xfs_dir2_sf_addname_easy(xfs_da_args_t
*args
,
24 xfs_dir2_sf_entry_t
*sfep
,
25 xfs_dir2_data_aoff_t offset
,
27 static void xfs_dir2_sf_addname_hard(xfs_da_args_t
*args
, int objchange
,
29 static int xfs_dir2_sf_addname_pick(xfs_da_args_t
*args
, int objchange
,
30 xfs_dir2_sf_entry_t
**sfepp
,
31 xfs_dir2_data_aoff_t
*offsetp
);
33 static void xfs_dir2_sf_check(xfs_da_args_t
*args
);
35 #define xfs_dir2_sf_check(args)
38 static void xfs_dir2_sf_toino4(xfs_da_args_t
*args
);
39 static void xfs_dir2_sf_toino8(xfs_da_args_t
*args
);
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_data_hdr_t
*hdr
, /* 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
= 0; /* parent inode number */
65 int size
=0; /* total computed size */
67 struct xfs_da_geometry
*geo
;
73 * if there is a filetype field, add the extra byte to the namelen
74 * for each entry that we see.
76 has_ftype
= xfs_sb_version_hasftype(&mp
->m_sb
) ? 1 : 0;
78 count
= i8count
= namelen
= 0;
79 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
80 blp
= xfs_dir2_block_leaf_p(btp
);
83 * Iterate over the block's data entries by using the leaf pointers.
85 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
86 if ((addr
= be32_to_cpu(blp
[i
].address
)) == XFS_DIR2_NULL_DATAPTR
)
89 * Calculate the pointer to the entry at hand.
91 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
92 xfs_dir2_dataptr_to_off(geo
, addr
));
94 * Detect . and .., so we can special-case them.
95 * . is not included in sf directories.
96 * .. is included by just the parent inode number.
98 isdot
= dep
->namelen
== 1 && dep
->name
[0] == '.';
101 dep
->name
[0] == '.' && dep
->name
[1] == '.';
104 i8count
+= be64_to_cpu(dep
->inumber
) > XFS_DIR2_MAX_SHORT_INUM
;
106 /* take into account the file type field */
107 if (!isdot
&& !isdotdot
) {
109 namelen
+= dep
->namelen
+ has_ftype
;
111 parent
= be64_to_cpu(dep
->inumber
);
113 * Calculate the new size, see if we should give up yet.
115 size
= xfs_dir2_sf_hdr_size(i8count
) + /* header */
116 count
* 3 * sizeof(u8
) + /* namelen + offset */
118 (i8count
? /* inumber */
119 count
* XFS_INO64_SIZE
:
120 count
* XFS_INO32_SIZE
);
121 if (size
> XFS_IFORK_DSIZE(dp
))
122 return size
; /* size value is a failure */
125 * Create the output header, if it worked.
128 sfhp
->i8count
= i8count
;
129 dp
->d_ops
->sf_put_parent_ino(sfhp
, parent
);
134 * Convert a block format directory to shortform.
135 * Caller has already checked that it will fit, and built us a header.
138 xfs_dir2_block_to_sf(
139 xfs_da_args_t
*args
, /* operation arguments */
141 int size
, /* shortform directory size */
142 xfs_dir2_sf_hdr_t
*sfhp
) /* shortform directory hdr */
144 xfs_dir2_data_hdr_t
*hdr
; /* block header */
145 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
146 xfs_inode_t
*dp
; /* incore directory inode */
147 xfs_dir2_data_unused_t
*dup
; /* unused data pointer */
148 char *endptr
; /* end of data entries */
149 int error
; /* error return value */
150 int logflags
; /* inode logging flags */
151 xfs_mount_t
*mp
; /* filesystem mount point */
152 char *ptr
; /* current data pointer */
153 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
154 xfs_dir2_sf_hdr_t
*sfp
; /* shortform directory header */
155 xfs_dir2_sf_hdr_t
*dst
; /* temporary data buffer */
157 trace_xfs_dir2_block_to_sf(args
);
163 * allocate a temporary destination buffer the size of the inode
164 * to format the data into. Once we have formatted the data, we
165 * can free the block and copy the formatted data into the inode literal
168 dst
= kmem_alloc(mp
->m_sb
.sb_inodesize
, KM_SLEEP
);
172 * Copy the header into the newly allocate local space.
174 sfp
= (xfs_dir2_sf_hdr_t
*)dst
;
175 memcpy(sfp
, sfhp
, xfs_dir2_sf_hdr_size(sfhp
->i8count
));
178 * Set up to loop over the block's entries.
180 ptr
= (char *)dp
->d_ops
->data_entry_p(hdr
);
181 endptr
= xfs_dir3_data_endp(args
->geo
, hdr
);
182 sfep
= xfs_dir2_sf_firstentry(sfp
);
184 * Loop over the active and unused entries.
185 * Stop when we reach the leaf/tail portion of the block.
187 while (ptr
< endptr
) {
189 * If it's unused, just skip over it.
191 dup
= (xfs_dir2_data_unused_t
*)ptr
;
192 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
193 ptr
+= be16_to_cpu(dup
->length
);
196 dep
= (xfs_dir2_data_entry_t
*)ptr
;
200 if (dep
->namelen
== 1 && dep
->name
[0] == '.')
201 ASSERT(be64_to_cpu(dep
->inumber
) == dp
->i_ino
);
203 * Skip .., but make sure the inode number is right.
205 else if (dep
->namelen
== 2 &&
206 dep
->name
[0] == '.' && dep
->name
[1] == '.')
207 ASSERT(be64_to_cpu(dep
->inumber
) ==
208 dp
->d_ops
->sf_get_parent_ino(sfp
));
210 * Normal entry, copy it into shortform.
213 sfep
->namelen
= dep
->namelen
;
214 xfs_dir2_sf_put_offset(sfep
,
215 (xfs_dir2_data_aoff_t
)
216 ((char *)dep
- (char *)hdr
));
217 memcpy(sfep
->name
, dep
->name
, dep
->namelen
);
218 dp
->d_ops
->sf_put_ino(sfp
, sfep
,
219 be64_to_cpu(dep
->inumber
));
220 dp
->d_ops
->sf_put_ftype(sfep
,
221 dp
->d_ops
->data_get_ftype(dep
));
223 sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
);
225 ptr
+= dp
->d_ops
->data_entsize(dep
->namelen
);
227 ASSERT((char *)sfep
- (char *)sfp
== size
);
229 /* now we are done with the block, we can shrink the inode */
230 logflags
= XFS_ILOG_CORE
;
231 error
= xfs_dir2_shrink_inode(args
, args
->geo
->datablk
, bp
);
233 ASSERT(error
!= -ENOSPC
);
238 * The buffer is now unconditionally gone, whether
239 * xfs_dir2_shrink_inode worked or not.
241 * Convert the inode to local format and copy the data in.
243 ASSERT(dp
->i_df
.if_bytes
== 0);
244 xfs_init_local_fork(dp
, XFS_DATA_FORK
, dst
, size
);
245 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
246 dp
->i_d
.di_size
= size
;
248 logflags
|= XFS_ILOG_DDATA
;
249 xfs_dir2_sf_check(args
);
251 xfs_trans_log_inode(args
->trans
, dp
, logflags
);
257 * Add a name to a shortform directory.
258 * There are two algorithms, "easy" and "hard" which we decide on
259 * before changing anything.
260 * Convert to block form if necessary, if the new entry won't fit.
264 xfs_da_args_t
*args
) /* operation arguments */
266 xfs_inode_t
*dp
; /* incore directory inode */
267 int error
; /* error return value */
268 int incr_isize
; /* total change in size */
269 int new_isize
; /* di_size after adding name */
270 int objchange
; /* changing to 8-byte inodes */
271 xfs_dir2_data_aoff_t offset
= 0; /* offset for new entry */
272 int pick
; /* which algorithm to use */
273 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
274 xfs_dir2_sf_entry_t
*sfep
= NULL
; /* shortform entry */
276 trace_xfs_dir2_sf_addname(args
);
278 ASSERT(xfs_dir2_sf_lookup(args
) == -ENOENT
);
280 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
282 * Make sure the shortform value has some of its header.
284 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
285 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
288 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
289 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
290 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
291 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->i8count
));
293 * Compute entry (and change in) size.
295 incr_isize
= dp
->d_ops
->sf_entsize(sfp
, args
->namelen
);
299 * Do we have to change to 8 byte inodes?
301 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->i8count
== 0) {
303 * Yes, adjust the inode size. old count + (parent + new)
305 incr_isize
+= (sfp
->count
+ 2) * XFS_INO64_DIFF
;
309 new_isize
= (int)dp
->i_d
.di_size
+ incr_isize
;
311 * Won't fit as shortform any more (due to size),
312 * or the pick routine says it won't (due to offset values).
314 if (new_isize
> XFS_IFORK_DSIZE(dp
) ||
316 xfs_dir2_sf_addname_pick(args
, objchange
, &sfep
, &offset
)) == 0) {
318 * Just checking or no space reservation, it doesn't fit.
320 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
323 * Convert to block form then add the name.
325 error
= xfs_dir2_sf_to_block(args
);
328 return xfs_dir2_block_addname(args
);
331 * Just checking, it fits.
333 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
336 * Do it the easy way - just add it at the end.
339 xfs_dir2_sf_addname_easy(args
, sfep
, offset
, new_isize
);
341 * Do it the hard way - look for a place to insert the new entry.
342 * Convert to 8 byte inode numbers first if necessary.
347 xfs_dir2_sf_toino8(args
);
348 xfs_dir2_sf_addname_hard(args
, objchange
, new_isize
);
350 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
355 * Add the new entry the "easy" way.
356 * This is copying the old directory and adding the new entry at the end.
357 * Since it's sorted by "offset" we need room after the last offset
358 * that's already there, and then room to convert to a block directory.
359 * This is already checked by the pick routine.
362 xfs_dir2_sf_addname_easy(
363 xfs_da_args_t
*args
, /* operation arguments */
364 xfs_dir2_sf_entry_t
*sfep
, /* pointer to new entry */
365 xfs_dir2_data_aoff_t offset
, /* offset to use for new ent */
366 int new_isize
) /* new directory size */
368 int byteoff
; /* byte offset in sf dir */
369 xfs_inode_t
*dp
; /* incore directory inode */
370 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
374 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
375 byteoff
= (int)((char *)sfep
- (char *)sfp
);
377 * Grow the in-inode space.
379 xfs_idata_realloc(dp
, dp
->d_ops
->sf_entsize(sfp
, args
->namelen
),
382 * Need to set up again due to realloc of the inode data.
384 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
385 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ byteoff
);
387 * Fill in the new entry.
389 sfep
->namelen
= args
->namelen
;
390 xfs_dir2_sf_put_offset(sfep
, offset
);
391 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
392 dp
->d_ops
->sf_put_ino(sfp
, sfep
, args
->inumber
);
393 dp
->d_ops
->sf_put_ftype(sfep
, args
->filetype
);
396 * Update the header and inode.
399 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
)
401 dp
->i_d
.di_size
= new_isize
;
402 xfs_dir2_sf_check(args
);
406 * Add the new entry the "hard" way.
407 * The caller has already converted to 8 byte inode numbers if necessary,
408 * in which case we need to leave the i8count at 1.
409 * Find a hole that the new entry will fit into, and copy
410 * the first part of the entries, the new entry, and the last part of
415 xfs_dir2_sf_addname_hard(
416 xfs_da_args_t
*args
, /* operation arguments */
417 int objchange
, /* changing inode number size */
418 int new_isize
) /* new directory size */
420 int add_datasize
; /* data size need for new ent */
421 char *buf
; /* buffer for old */
422 xfs_inode_t
*dp
; /* incore directory inode */
423 int eof
; /* reached end of old dir */
424 int nbytes
; /* temp for byte copies */
425 xfs_dir2_data_aoff_t new_offset
; /* next offset value */
426 xfs_dir2_data_aoff_t offset
; /* current offset value */
427 int old_isize
; /* previous di_size */
428 xfs_dir2_sf_entry_t
*oldsfep
; /* entry in original dir */
429 xfs_dir2_sf_hdr_t
*oldsfp
; /* original shortform dir */
430 xfs_dir2_sf_entry_t
*sfep
; /* entry in new dir */
431 xfs_dir2_sf_hdr_t
*sfp
; /* new shortform dir */
434 * Copy the old directory to the stack buffer.
438 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
439 old_isize
= (int)dp
->i_d
.di_size
;
440 buf
= kmem_alloc(old_isize
, KM_SLEEP
);
441 oldsfp
= (xfs_dir2_sf_hdr_t
*)buf
;
442 memcpy(oldsfp
, sfp
, old_isize
);
444 * Loop over the old directory finding the place we're going
445 * to insert the new entry.
446 * If it's going to end up at the end then oldsfep will point there.
448 for (offset
= dp
->d_ops
->data_first_offset
,
449 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
),
450 add_datasize
= dp
->d_ops
->data_entsize(args
->namelen
),
451 eof
= (char *)oldsfep
== &buf
[old_isize
];
453 offset
= new_offset
+ dp
->d_ops
->data_entsize(oldsfep
->namelen
),
454 oldsfep
= dp
->d_ops
->sf_nextentry(oldsfp
, oldsfep
),
455 eof
= (char *)oldsfep
== &buf
[old_isize
]) {
456 new_offset
= xfs_dir2_sf_get_offset(oldsfep
);
457 if (offset
+ add_datasize
<= new_offset
)
461 * Get rid of the old directory, then allocate space for
462 * the new one. We do this so xfs_idata_realloc won't copy
465 xfs_idata_realloc(dp
, -old_isize
, XFS_DATA_FORK
);
466 xfs_idata_realloc(dp
, new_isize
, XFS_DATA_FORK
);
468 * Reset the pointer since the buffer was reallocated.
470 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
472 * Copy the first part of the directory, including the header.
474 nbytes
= (int)((char *)oldsfep
- (char *)oldsfp
);
475 memcpy(sfp
, oldsfp
, nbytes
);
476 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfp
+ nbytes
);
478 * Fill in the new entry, and update the header counts.
480 sfep
->namelen
= args
->namelen
;
481 xfs_dir2_sf_put_offset(sfep
, offset
);
482 memcpy(sfep
->name
, args
->name
, sfep
->namelen
);
483 dp
->d_ops
->sf_put_ino(sfp
, sfep
, args
->inumber
);
484 dp
->d_ops
->sf_put_ftype(sfep
, args
->filetype
);
486 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& !objchange
)
489 * If there's more left to copy, do that.
492 sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
);
493 memcpy(sfep
, oldsfep
, old_isize
- nbytes
);
496 dp
->i_d
.di_size
= new_isize
;
497 xfs_dir2_sf_check(args
);
501 * Decide if the new entry will fit at all.
502 * If it will fit, pick between adding the new entry to the end (easy)
503 * or somewhere else (hard).
504 * Return 0 (won't fit), 1 (easy), 2 (hard).
507 static int /* pick result */
508 xfs_dir2_sf_addname_pick(
509 xfs_da_args_t
*args
, /* operation arguments */
510 int objchange
, /* inode # size changes */
511 xfs_dir2_sf_entry_t
**sfepp
, /* out(1): new entry ptr */
512 xfs_dir2_data_aoff_t
*offsetp
) /* out(1): new offset */
514 xfs_inode_t
*dp
; /* incore directory inode */
515 int holefit
; /* found hole it will fit in */
516 int i
; /* entry number */
517 xfs_dir2_data_aoff_t offset
; /* data block offset */
518 xfs_dir2_sf_entry_t
*sfep
; /* shortform entry */
519 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
520 int size
; /* entry's data size */
521 int used
; /* data bytes used */
525 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
526 size
= dp
->d_ops
->data_entsize(args
->namelen
);
527 offset
= dp
->d_ops
->data_first_offset
;
528 sfep
= xfs_dir2_sf_firstentry(sfp
);
531 * Loop over sf entries.
532 * Keep track of data offset and whether we've seen a place
533 * to insert the new entry.
535 for (i
= 0; i
< sfp
->count
; i
++) {
537 holefit
= offset
+ size
<= xfs_dir2_sf_get_offset(sfep
);
538 offset
= xfs_dir2_sf_get_offset(sfep
) +
539 dp
->d_ops
->data_entsize(sfep
->namelen
);
540 sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
);
543 * Calculate data bytes used excluding the new entry, if this
544 * was a data block (block form directory).
547 (sfp
->count
+ 3) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
548 (uint
)sizeof(xfs_dir2_block_tail_t
);
550 * If it won't fit in a block form then we can't insert it,
551 * we'll go back, convert to block, then try the insert and convert
554 if (used
+ (holefit
? 0 : size
) > args
->geo
->blksize
)
557 * If changing the inode number size, do it the hard way.
562 * If it won't fit at the end then do it the hard way (use the hole).
564 if (used
+ size
> args
->geo
->blksize
)
567 * Do it the easy way.
576 * Check consistency of shortform directory, assert if bad.
580 xfs_da_args_t
*args
) /* operation arguments */
582 xfs_inode_t
*dp
; /* incore directory inode */
583 int i
; /* entry number */
584 int i8count
; /* number of big inode#s */
585 xfs_ino_t ino
; /* entry inode number */
586 int offset
; /* data offset */
587 xfs_dir2_sf_entry_t
*sfep
; /* shortform dir entry */
588 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
592 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
593 offset
= dp
->d_ops
->data_first_offset
;
594 ino
= dp
->d_ops
->sf_get_parent_ino(sfp
);
595 i8count
= ino
> XFS_DIR2_MAX_SHORT_INUM
;
597 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
);
599 i
++, sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
)) {
600 ASSERT(xfs_dir2_sf_get_offset(sfep
) >= offset
);
601 ino
= dp
->d_ops
->sf_get_ino(sfp
, sfep
);
602 i8count
+= ino
> XFS_DIR2_MAX_SHORT_INUM
;
604 xfs_dir2_sf_get_offset(sfep
) +
605 dp
->d_ops
->data_entsize(sfep
->namelen
);
606 ASSERT(dp
->d_ops
->sf_get_ftype(sfep
) < XFS_DIR3_FT_MAX
);
608 ASSERT(i8count
== sfp
->i8count
);
609 ASSERT((char *)sfep
- (char *)sfp
== dp
->i_d
.di_size
);
611 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
612 (uint
)sizeof(xfs_dir2_block_tail_t
) <= args
->geo
->blksize
);
616 /* Verify the consistency of an inline directory. */
619 struct xfs_inode
*ip
)
621 struct xfs_mount
*mp
= ip
->i_mount
;
622 struct xfs_dir2_sf_hdr
*sfp
;
623 struct xfs_dir2_sf_entry
*sfep
;
624 struct xfs_dir2_sf_entry
*next_sfep
;
626 const struct xfs_dir_ops
*dops
;
627 struct xfs_ifork
*ifp
;
636 ASSERT(ip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
);
638 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
639 * so we can only trust the mountpoint to have the right pointer.
641 dops
= xfs_dir_get_ops(mp
, NULL
);
643 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
644 sfp
= (struct xfs_dir2_sf_hdr
*)ifp
->if_u1
.if_data
;
645 size
= ifp
->if_bytes
;
648 * Give up if the directory is way too short.
650 if (size
<= offsetof(struct xfs_dir2_sf_hdr
, parent
) ||
651 size
< xfs_dir2_sf_hdr_size(sfp
->i8count
))
652 return __this_address
;
654 endp
= (char *)sfp
+ size
;
657 ino
= dops
->sf_get_parent_ino(sfp
);
658 i8count
= ino
> XFS_DIR2_MAX_SHORT_INUM
;
659 error
= xfs_dir_ino_validate(mp
, ino
);
661 return __this_address
;
662 offset
= dops
->data_first_offset
;
664 /* Check all reported entries */
665 sfep
= xfs_dir2_sf_firstentry(sfp
);
666 for (i
= 0; i
< sfp
->count
; i
++) {
668 * struct xfs_dir2_sf_entry has a variable length.
669 * Check the fixed-offset parts of the structure are
670 * within the data buffer.
672 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
673 return __this_address
;
675 /* Don't allow names with known bad length. */
676 if (sfep
->namelen
== 0)
677 return __this_address
;
680 * Check that the variable-length part of the structure is
681 * within the data buffer. The next entry starts after the
682 * name component, so nextentry is an acceptable test.
684 next_sfep
= dops
->sf_nextentry(sfp
, sfep
);
685 if (endp
< (char *)next_sfep
)
686 return __this_address
;
688 /* Check that the offsets always increase. */
689 if (xfs_dir2_sf_get_offset(sfep
) < offset
)
690 return __this_address
;
692 /* Check the inode number. */
693 ino
= dops
->sf_get_ino(sfp
, sfep
);
694 i8count
+= ino
> XFS_DIR2_MAX_SHORT_INUM
;
695 error
= xfs_dir_ino_validate(mp
, ino
);
697 return __this_address
;
699 /* Check the file type. */
700 filetype
= dops
->sf_get_ftype(sfep
);
701 if (filetype
>= XFS_DIR3_FT_MAX
)
702 return __this_address
;
704 offset
= xfs_dir2_sf_get_offset(sfep
) +
705 dops
->data_entsize(sfep
->namelen
);
709 if (i8count
!= sfp
->i8count
)
710 return __this_address
;
711 if ((void *)sfep
!= (void *)endp
)
712 return __this_address
;
714 /* Make sure this whole thing ought to be in local format. */
715 if (offset
+ (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
) +
716 (uint
)sizeof(xfs_dir2_block_tail_t
) > mp
->m_dir_geo
->blksize
)
717 return __this_address
;
723 * Create a new (shortform) directory.
725 int /* error, always 0 */
727 xfs_da_args_t
*args
, /* operation arguments */
728 xfs_ino_t pino
) /* parent inode number */
730 xfs_inode_t
*dp
; /* incore directory inode */
731 int i8count
; /* parent inode is an 8-byte number */
732 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
733 int size
; /* directory size */
735 trace_xfs_dir2_sf_create(args
);
740 ASSERT(dp
->i_d
.di_size
== 0);
742 * If it's currently a zero-length extent file,
743 * convert it to local format.
745 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
) {
746 dp
->i_df
.if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
747 dp
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
748 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
749 dp
->i_df
.if_flags
|= XFS_IFINLINE
;
751 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
752 ASSERT(dp
->i_df
.if_bytes
== 0);
753 i8count
= pino
> XFS_DIR2_MAX_SHORT_INUM
;
754 size
= xfs_dir2_sf_hdr_size(i8count
);
756 * Make a buffer for the data.
758 xfs_idata_realloc(dp
, size
, XFS_DATA_FORK
);
760 * Fill in the header,
762 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
763 sfp
->i8count
= i8count
;
765 * Now can put in the inode number, since i8count is set.
767 dp
->d_ops
->sf_put_parent_ino(sfp
, pino
);
769 dp
->i_d
.di_size
= size
;
770 xfs_dir2_sf_check(args
);
771 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
776 * Lookup an entry in a shortform directory.
777 * Returns EEXIST if found, ENOENT if not found.
781 xfs_da_args_t
*args
) /* operation arguments */
783 xfs_inode_t
*dp
; /* incore directory inode */
784 int i
; /* entry index */
786 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
787 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
788 enum xfs_dacmp cmp
; /* comparison result */
789 xfs_dir2_sf_entry_t
*ci_sfep
; /* case-insens. entry */
791 trace_xfs_dir2_sf_lookup(args
);
793 xfs_dir2_sf_check(args
);
796 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
798 * Bail out if the directory is way too short.
800 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
801 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
804 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
805 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
806 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
807 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->i8count
));
811 if (args
->namelen
== 1 && args
->name
[0] == '.') {
812 args
->inumber
= dp
->i_ino
;
813 args
->cmpresult
= XFS_CMP_EXACT
;
814 args
->filetype
= XFS_DIR3_FT_DIR
;
818 * Special case for ..
820 if (args
->namelen
== 2 &&
821 args
->name
[0] == '.' && args
->name
[1] == '.') {
822 args
->inumber
= dp
->d_ops
->sf_get_parent_ino(sfp
);
823 args
->cmpresult
= XFS_CMP_EXACT
;
824 args
->filetype
= XFS_DIR3_FT_DIR
;
828 * Loop over all the entries trying to match ours.
831 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
); i
< sfp
->count
;
832 i
++, sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
)) {
834 * Compare name and if it's an exact match, return the inode
835 * number. If it's the first case-insensitive match, store the
836 * inode number and continue looking for an exact match.
838 cmp
= dp
->i_mount
->m_dirnameops
->compname(args
, sfep
->name
,
840 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
841 args
->cmpresult
= cmp
;
842 args
->inumber
= dp
->d_ops
->sf_get_ino(sfp
, sfep
);
843 args
->filetype
= dp
->d_ops
->sf_get_ftype(sfep
);
844 if (cmp
== XFS_CMP_EXACT
)
849 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
851 * Here, we can only be doing a lookup (not a rename or replace).
852 * If a case-insensitive match was not found, return -ENOENT.
856 /* otherwise process the CI match as required by the caller */
857 error
= xfs_dir_cilookup_result(args
, ci_sfep
->name
, ci_sfep
->namelen
);
862 * Remove an entry from a shortform directory.
865 xfs_dir2_sf_removename(
868 int byteoff
; /* offset of removed entry */
869 xfs_inode_t
*dp
; /* incore directory inode */
870 int entsize
; /* this entry's size */
871 int i
; /* shortform entry index */
872 int newsize
; /* new inode size */
873 int oldsize
; /* old inode size */
874 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
875 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
877 trace_xfs_dir2_sf_removename(args
);
881 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
882 oldsize
= (int)dp
->i_d
.di_size
;
884 * Bail out if the directory is way too short.
886 if (oldsize
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
887 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
890 ASSERT(dp
->i_df
.if_bytes
== oldsize
);
891 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
892 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
893 ASSERT(oldsize
>= xfs_dir2_sf_hdr_size(sfp
->i8count
));
895 * Loop over the old directory entries.
896 * Find the one we're deleting.
898 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
); i
< sfp
->count
;
899 i
++, sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
)) {
900 if (xfs_da_compname(args
, sfep
->name
, sfep
->namelen
) ==
902 ASSERT(dp
->d_ops
->sf_get_ino(sfp
, sfep
) ==
915 byteoff
= (int)((char *)sfep
- (char *)sfp
);
916 entsize
= dp
->d_ops
->sf_entsize(sfp
, args
->namelen
);
917 newsize
= oldsize
- entsize
;
919 * Copy the part if any after the removed entry, sliding it down.
921 if (byteoff
+ entsize
< oldsize
)
922 memmove((char *)sfp
+ byteoff
, (char *)sfp
+ byteoff
+ entsize
,
923 oldsize
- (byteoff
+ entsize
));
925 * Fix up the header and file size.
928 dp
->i_d
.di_size
= newsize
;
930 * Reallocate, making it smaller.
932 xfs_idata_realloc(dp
, newsize
- oldsize
, XFS_DATA_FORK
);
933 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
935 * Are we changing inode number size?
937 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
938 if (sfp
->i8count
== 1)
939 xfs_dir2_sf_toino4(args
);
943 xfs_dir2_sf_check(args
);
944 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
949 * Replace the inode number of an entry in a shortform directory.
953 xfs_da_args_t
*args
) /* operation arguments */
955 xfs_inode_t
*dp
; /* incore directory inode */
956 int i
; /* entry index */
957 xfs_ino_t ino
=0; /* entry old inode number */
958 int i8elevated
; /* sf_toino8 set i8count=1 */
959 xfs_dir2_sf_entry_t
*sfep
; /* shortform directory entry */
960 xfs_dir2_sf_hdr_t
*sfp
; /* shortform structure */
962 trace_xfs_dir2_sf_replace(args
);
966 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
968 * Bail out if the shortform directory is way too small.
970 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
971 ASSERT(XFS_FORCED_SHUTDOWN(dp
->i_mount
));
974 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
975 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
976 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
977 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(sfp
->i8count
));
980 * New inode number is large, and need to convert to 8-byte inodes.
982 if (args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
&& sfp
->i8count
== 0) {
983 int error
; /* error return value */
984 int newsize
; /* new inode size */
986 newsize
= dp
->i_df
.if_bytes
+ (sfp
->count
+ 1) * XFS_INO64_DIFF
;
988 * Won't fit as shortform, convert to block then do replace.
990 if (newsize
> XFS_IFORK_DSIZE(dp
)) {
991 error
= xfs_dir2_sf_to_block(args
);
995 return xfs_dir2_block_replace(args
);
998 * Still fits, convert to 8-byte now.
1000 xfs_dir2_sf_toino8(args
);
1002 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1006 ASSERT(args
->namelen
!= 1 || args
->name
[0] != '.');
1008 * Replace ..'s entry.
1010 if (args
->namelen
== 2 &&
1011 args
->name
[0] == '.' && args
->name
[1] == '.') {
1012 ino
= dp
->d_ops
->sf_get_parent_ino(sfp
);
1013 ASSERT(args
->inumber
!= ino
);
1014 dp
->d_ops
->sf_put_parent_ino(sfp
, args
->inumber
);
1017 * Normal entry, look for the name.
1020 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
); i
< sfp
->count
;
1021 i
++, sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
)) {
1022 if (xfs_da_compname(args
, sfep
->name
, sfep
->namelen
) ==
1024 ino
= dp
->d_ops
->sf_get_ino(sfp
, sfep
);
1025 ASSERT(args
->inumber
!= ino
);
1026 dp
->d_ops
->sf_put_ino(sfp
, sfep
, args
->inumber
);
1027 dp
->d_ops
->sf_put_ftype(sfep
, args
->filetype
);
1034 if (i
== sfp
->count
) {
1035 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1037 xfs_dir2_sf_toino4(args
);
1042 * See if the old number was large, the new number is small.
1044 if (ino
> XFS_DIR2_MAX_SHORT_INUM
&&
1045 args
->inumber
<= XFS_DIR2_MAX_SHORT_INUM
) {
1047 * And the old count was one, so need to convert to small.
1049 if (sfp
->i8count
== 1)
1050 xfs_dir2_sf_toino4(args
);
1055 * See if the old number was small, the new number is large.
1057 if (ino
<= XFS_DIR2_MAX_SHORT_INUM
&&
1058 args
->inumber
> XFS_DIR2_MAX_SHORT_INUM
) {
1060 * add to the i8count unless we just converted to 8-byte
1061 * inodes (which does an implied i8count = 1)
1063 ASSERT(sfp
->i8count
!= 0);
1067 xfs_dir2_sf_check(args
);
1068 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_DDATA
);
1073 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1074 * The last 8-byte inode number is gone, but the count is still 1.
1078 xfs_da_args_t
*args
) /* operation arguments */
1080 char *buf
; /* old dir's buffer */
1081 xfs_inode_t
*dp
; /* incore directory inode */
1082 int i
; /* entry index */
1083 int newsize
; /* new inode size */
1084 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1085 xfs_dir2_sf_hdr_t
*oldsfp
; /* old sf directory */
1086 int oldsize
; /* old inode size */
1087 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1088 xfs_dir2_sf_hdr_t
*sfp
; /* new sf directory */
1090 trace_xfs_dir2_sf_toino4(args
);
1095 * Copy the old directory to the buffer.
1096 * Then nuke it from the inode, and add the new buffer to the inode.
1097 * Don't want xfs_idata_realloc copying the data here.
1099 oldsize
= dp
->i_df
.if_bytes
;
1100 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1101 oldsfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1102 ASSERT(oldsfp
->i8count
== 1);
1103 memcpy(buf
, oldsfp
, oldsize
);
1105 * Compute the new inode size.
1107 newsize
= oldsize
- (oldsfp
->count
+ 1) * XFS_INO64_DIFF
;
1108 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1109 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1111 * Reset our pointers, the data has moved.
1113 oldsfp
= (xfs_dir2_sf_hdr_t
*)buf
;
1114 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1116 * Fill in the new header.
1118 sfp
->count
= oldsfp
->count
;
1120 dp
->d_ops
->sf_put_parent_ino(sfp
, dp
->d_ops
->sf_get_parent_ino(oldsfp
));
1122 * Copy the entries field by field.
1124 for (i
= 0, sfep
= xfs_dir2_sf_firstentry(sfp
),
1125 oldsfep
= xfs_dir2_sf_firstentry(oldsfp
);
1127 i
++, sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
),
1128 oldsfep
= dp
->d_ops
->sf_nextentry(oldsfp
, oldsfep
)) {
1129 sfep
->namelen
= oldsfep
->namelen
;
1130 memcpy(sfep
->offset
, oldsfep
->offset
, sizeof(sfep
->offset
));
1131 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1132 dp
->d_ops
->sf_put_ino(sfp
, sfep
,
1133 dp
->d_ops
->sf_get_ino(oldsfp
, oldsfep
));
1134 dp
->d_ops
->sf_put_ftype(sfep
, dp
->d_ops
->sf_get_ftype(oldsfep
));
1137 * Clean up the inode.
1140 dp
->i_d
.di_size
= newsize
;
1141 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
1145 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1146 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1147 * i8count set to 1, but no corresponding 8-byte entry.
1151 xfs_da_args_t
*args
) /* operation arguments */
1153 char *buf
; /* old dir's buffer */
1154 xfs_inode_t
*dp
; /* incore directory inode */
1155 int i
; /* entry index */
1156 int newsize
; /* new inode size */
1157 xfs_dir2_sf_entry_t
*oldsfep
; /* old sf entry */
1158 xfs_dir2_sf_hdr_t
*oldsfp
; /* old sf directory */
1159 int oldsize
; /* old inode size */
1160 xfs_dir2_sf_entry_t
*sfep
; /* new sf entry */
1161 xfs_dir2_sf_hdr_t
*sfp
; /* new sf directory */
1163 trace_xfs_dir2_sf_toino8(args
);
1168 * Copy the old directory to the buffer.
1169 * Then nuke it from the inode, and add the new buffer to the inode.
1170 * Don't want xfs_idata_realloc copying the data here.
1172 oldsize
= dp
->i_df
.if_bytes
;
1173 buf
= kmem_alloc(oldsize
, KM_SLEEP
);
1174 oldsfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1175 ASSERT(oldsfp
->i8count
== 0);
1176 memcpy(buf
, oldsfp
, oldsize
);
1178 * Compute the new inode size (nb: entry count + 1 for parent)
1180 newsize
= oldsize
+ (oldsfp
->count
+ 1) * XFS_INO64_DIFF
;
1181 xfs_idata_realloc(dp
, -oldsize
, XFS_DATA_FORK
);
1182 xfs_idata_realloc(dp
, newsize
, XFS_DATA_FORK
);
1184 * Reset our pointers, the data has moved.
1186 oldsfp
= (xfs_dir2_sf_hdr_t
*)buf
;
1187 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1189 * Fill in the new header.
1191 sfp
->count
= oldsfp
->count
;
1193 dp
->d_ops
->sf_put_parent_ino(sfp
, dp
->d_ops
->sf_get_parent_ino(oldsfp
));
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
= dp
->d_ops
->sf_nextentry(sfp
, sfep
),
1201 oldsfep
= dp
->d_ops
->sf_nextentry(oldsfp
, oldsfep
)) {
1202 sfep
->namelen
= oldsfep
->namelen
;
1203 memcpy(sfep
->offset
, oldsfep
->offset
, sizeof(sfep
->offset
));
1204 memcpy(sfep
->name
, oldsfep
->name
, sfep
->namelen
);
1205 dp
->d_ops
->sf_put_ino(sfp
, sfep
,
1206 dp
->d_ops
->sf_get_ino(oldsfp
, oldsfep
));
1207 dp
->d_ops
->sf_put_ftype(sfep
, dp
->d_ops
->sf_get_ftype(oldsfep
));
1210 * Clean up the inode.
1213 dp
->i_d
.di_size
= newsize
;
1214 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_DDATA
);