1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_errortag.h"
19 #include "xfs_trace.h"
20 #include "xfs_health.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_parent.h"
25 #include "xfs_ialloc.h"
27 const struct xfs_name xfs_name_dotdot
= {
28 .name
= (const unsigned char *)"..",
30 .type
= XFS_DIR3_FT_DIR
,
33 const struct xfs_name xfs_name_dot
= {
34 .name
= (const unsigned char *)".",
36 .type
= XFS_DIR3_FT_DIR
,
40 * Convert inode mode to directory entry filetype
46 switch (mode
& S_IFMT
) {
48 return XFS_DIR3_FT_REG_FILE
;
50 return XFS_DIR3_FT_DIR
;
52 return XFS_DIR3_FT_CHRDEV
;
54 return XFS_DIR3_FT_BLKDEV
;
56 return XFS_DIR3_FT_FIFO
;
58 return XFS_DIR3_FT_SOCK
;
60 return XFS_DIR3_FT_SYMLINK
;
62 return XFS_DIR3_FT_UNKNOWN
;
67 * ASCII case-insensitive (ie. A-Z) support for directories that was
71 xfs_ascii_ci_hashname(
72 const struct xfs_name
*name
)
77 for (i
= 0, hash
= 0; i
< name
->len
; i
++)
78 hash
= xfs_ascii_ci_xfrm(name
->name
[i
]) ^ rol32(hash
, 7);
84 xfs_ascii_ci_compname(
85 struct xfs_da_args
*args
,
86 const unsigned char *name
,
89 enum xfs_dacmp result
;
92 if (args
->namelen
!= len
)
93 return XFS_CMP_DIFFERENT
;
95 result
= XFS_CMP_EXACT
;
96 for (i
= 0; i
< len
; i
++) {
97 if (args
->name
[i
] == name
[i
])
99 if (xfs_ascii_ci_xfrm(args
->name
[i
]) !=
100 xfs_ascii_ci_xfrm(name
[i
]))
101 return XFS_CMP_DIFFERENT
;
102 result
= XFS_CMP_CASE
;
110 struct xfs_mount
*mp
)
112 struct xfs_da_geometry
*dageo
;
115 ASSERT(mp
->m_sb
.sb_versionnum
& XFS_SB_VERSION_DIRV2BIT
);
116 ASSERT(xfs_dir2_dirblock_bytes(&mp
->m_sb
) <= XFS_MAX_BLOCKSIZE
);
118 mp
->m_dir_geo
= kzalloc(sizeof(struct xfs_da_geometry
),
119 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
120 mp
->m_attr_geo
= kzalloc(sizeof(struct xfs_da_geometry
),
121 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
122 if (!mp
->m_dir_geo
|| !mp
->m_attr_geo
) {
123 kfree(mp
->m_dir_geo
);
124 kfree(mp
->m_attr_geo
);
128 /* set up directory geometry */
129 dageo
= mp
->m_dir_geo
;
130 dageo
->blklog
= mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
;
131 dageo
->fsblog
= mp
->m_sb
.sb_blocklog
;
132 dageo
->blksize
= xfs_dir2_dirblock_bytes(&mp
->m_sb
);
133 dageo
->fsbcount
= 1 << mp
->m_sb
.sb_dirblklog
;
134 if (xfs_has_crc(mp
)) {
135 dageo
->node_hdr_size
= sizeof(struct xfs_da3_node_hdr
);
136 dageo
->leaf_hdr_size
= sizeof(struct xfs_dir3_leaf_hdr
);
137 dageo
->free_hdr_size
= sizeof(struct xfs_dir3_free_hdr
);
138 dageo
->data_entry_offset
=
139 sizeof(struct xfs_dir3_data_hdr
);
141 dageo
->node_hdr_size
= sizeof(struct xfs_da_node_hdr
);
142 dageo
->leaf_hdr_size
= sizeof(struct xfs_dir2_leaf_hdr
);
143 dageo
->free_hdr_size
= sizeof(struct xfs_dir2_free_hdr
);
144 dageo
->data_entry_offset
=
145 sizeof(struct xfs_dir2_data_hdr
);
147 dageo
->leaf_max_ents
= (dageo
->blksize
- dageo
->leaf_hdr_size
) /
148 sizeof(struct xfs_dir2_leaf_entry
);
149 dageo
->free_max_bests
= (dageo
->blksize
- dageo
->free_hdr_size
) /
150 sizeof(xfs_dir2_data_off_t
);
152 dageo
->data_first_offset
= dageo
->data_entry_offset
+
153 xfs_dir2_data_entsize(mp
, 1) +
154 xfs_dir2_data_entsize(mp
, 2);
157 * Now we've set up the block conversion variables, we can calculate the
158 * segment block constants using the geometry structure.
160 dageo
->datablk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_DATA_OFFSET
);
161 dageo
->leafblk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_LEAF_OFFSET
);
162 dageo
->freeblk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_FREE_OFFSET
);
163 dageo
->node_ents
= (dageo
->blksize
- dageo
->node_hdr_size
) /
164 (uint
)sizeof(xfs_da_node_entry_t
);
165 dageo
->max_extents
= (XFS_DIR2_MAX_SPACES
* XFS_DIR2_SPACE_SIZE
) >>
166 mp
->m_sb
.sb_blocklog
;
167 dageo
->magicpct
= (dageo
->blksize
* 37) / 100;
169 /* set up attribute geometry - single fsb only */
170 dageo
= mp
->m_attr_geo
;
171 dageo
->blklog
= mp
->m_sb
.sb_blocklog
;
172 dageo
->fsblog
= mp
->m_sb
.sb_blocklog
;
173 dageo
->blksize
= 1 << dageo
->blklog
;
175 dageo
->node_hdr_size
= mp
->m_dir_geo
->node_hdr_size
;
176 dageo
->node_ents
= (dageo
->blksize
- dageo
->node_hdr_size
) /
177 (uint
)sizeof(xfs_da_node_entry_t
);
179 if (xfs_has_large_extent_counts(mp
))
180 dageo
->max_extents
= XFS_MAX_EXTCNT_ATTR_FORK_LARGE
;
182 dageo
->max_extents
= XFS_MAX_EXTCNT_ATTR_FORK_SMALL
;
184 dageo
->magicpct
= (dageo
->blksize
* 37) / 100;
190 struct xfs_mount
*mp
)
192 kfree(mp
->m_dir_geo
);
193 kfree(mp
->m_attr_geo
);
197 * Return 1 if directory contains only "." and "..".
203 xfs_dir2_sf_hdr_t
*sfp
;
205 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
206 if (dp
->i_disk_size
== 0) /* might happen during shutdown. */
208 if (dp
->i_disk_size
> xfs_inode_data_fork_size(dp
))
210 sfp
= dp
->i_df
.if_data
;
215 * Validate a given inode number.
218 xfs_dir_ino_validate(
222 bool ino_ok
= xfs_verify_dir_ino(mp
, ino
);
224 if (XFS_IS_CORRUPT(mp
, !ino_ok
) ||
225 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
)) {
226 xfs_warn(mp
, "Invalid inode number 0x%Lx",
227 (unsigned long long) ino
);
228 return -EFSCORRUPTED
;
234 * Initialize a directory with its "." and ".." entries.
242 struct xfs_da_args
*args
;
245 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
246 error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
);
250 args
= kzalloc(sizeof(*args
), GFP_KERNEL
| __GFP_NOFAIL
);
254 args
->geo
= dp
->i_mount
->m_dir_geo
;
257 args
->owner
= dp
->i_ino
;
258 error
= xfs_dir2_sf_create(args
, pdp
->i_ino
);
265 struct xfs_da_args
*args
,
268 struct xfs_inode
*dp
= args
->dp
;
269 struct xfs_mount
*mp
= dp
->i_mount
;
270 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
273 xfs_assert_ilocked(dp
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
);
276 if (dp
->i_df
.if_format
== XFS_DINODE_FMT_LOCAL
)
277 return XFS_DIR2_FMT_SF
;
279 *error
= xfs_bmap_last_offset(dp
, &eof
, XFS_DATA_FORK
);
281 return XFS_DIR2_FMT_ERROR
;
283 if (eof
== XFS_B_TO_FSB(mp
, geo
->blksize
)) {
284 if (XFS_IS_CORRUPT(mp
, dp
->i_disk_size
!= geo
->blksize
)) {
285 xfs_da_mark_sick(args
);
286 *error
= -EFSCORRUPTED
;
287 return XFS_DIR2_FMT_ERROR
;
289 return XFS_DIR2_FMT_BLOCK
;
291 if (eof
== geo
->leafblk
+ geo
->fsbcount
)
292 return XFS_DIR2_FMT_LEAF
;
293 return XFS_DIR2_FMT_NODE
;
297 xfs_dir_createname_args(
298 struct xfs_da_args
*args
)
303 args
->op_flags
|= XFS_DA_OP_JUSTCHECK
;
305 switch (xfs_dir2_format(args
, &error
)) {
306 case XFS_DIR2_FMT_SF
:
307 return xfs_dir2_sf_addname(args
);
308 case XFS_DIR2_FMT_BLOCK
:
309 return xfs_dir2_block_addname(args
);
310 case XFS_DIR2_FMT_LEAF
:
311 return xfs_dir2_leaf_addname(args
);
312 case XFS_DIR2_FMT_NODE
:
313 return xfs_dir2_node_addname(args
);
320 * Enter a name in a directory, or check for available space.
321 * If inum is 0, only the available space test is performed.
325 struct xfs_trans
*tp
,
326 struct xfs_inode
*dp
,
327 const struct xfs_name
*name
,
328 xfs_ino_t inum
, /* new entry inode number */
329 xfs_extlen_t total
) /* bmap's total block count */
331 struct xfs_da_args
*args
;
334 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
337 rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
);
340 XFS_STATS_INC(dp
->i_mount
, xs_dir_create
);
343 args
= kzalloc(sizeof(*args
), GFP_KERNEL
| __GFP_NOFAIL
);
347 args
->geo
= dp
->i_mount
->m_dir_geo
;
348 args
->name
= name
->name
;
349 args
->namelen
= name
->len
;
350 args
->filetype
= name
->type
;
351 args
->hashval
= xfs_dir2_hashname(dp
->i_mount
, name
);
352 args
->inumber
= inum
;
355 args
->whichfork
= XFS_DATA_FORK
;
357 args
->op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
358 args
->owner
= dp
->i_ino
;
360 rval
= xfs_dir_createname_args(args
);
366 * If doing a CI lookup and case-insensitive match, dup actual name into
367 * args.value. Return EEXIST for success (ie. name found) or an error.
370 xfs_dir_cilookup_result(
371 struct xfs_da_args
*args
,
372 const unsigned char *name
,
375 if (args
->cmpresult
== XFS_CMP_DIFFERENT
)
377 if (args
->cmpresult
!= XFS_CMP_CASE
||
378 !(args
->op_flags
& XFS_DA_OP_CILOOKUP
))
381 args
->value
= kmemdup(name
, len
,
382 GFP_KERNEL
| __GFP_NOLOCKDEP
| __GFP_RETRY_MAYFAIL
);
386 args
->valuelen
= len
;
392 struct xfs_da_args
*args
)
396 switch (xfs_dir2_format(args
, &error
)) {
397 case XFS_DIR2_FMT_SF
:
398 error
= xfs_dir2_sf_lookup(args
);
400 case XFS_DIR2_FMT_BLOCK
:
401 error
= xfs_dir2_block_lookup(args
);
403 case XFS_DIR2_FMT_LEAF
:
404 error
= xfs_dir2_leaf_lookup(args
);
406 case XFS_DIR2_FMT_NODE
:
407 error
= xfs_dir2_node_lookup(args
);
413 if (error
!= -EEXIST
)
419 * Lookup a name in a directory, give back the inode number.
420 * If ci_name is not NULL, returns the actual name in ci_name if it differs
421 * to name, or ci_name->name is set to NULL for an exact match.
426 struct xfs_trans
*tp
,
427 struct xfs_inode
*dp
,
428 const struct xfs_name
*name
,
429 xfs_ino_t
*inum
, /* out: inode number */
430 struct xfs_name
*ci_name
) /* out: actual name if CI match */
432 struct xfs_da_args
*args
;
436 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
437 XFS_STATS_INC(dp
->i_mount
, xs_dir_lookup
);
439 args
= kzalloc(sizeof(*args
),
440 GFP_KERNEL
| __GFP_NOLOCKDEP
| __GFP_NOFAIL
);
441 args
->geo
= dp
->i_mount
->m_dir_geo
;
442 args
->name
= name
->name
;
443 args
->namelen
= name
->len
;
444 args
->filetype
= name
->type
;
445 args
->hashval
= xfs_dir2_hashname(dp
->i_mount
, name
);
447 args
->whichfork
= XFS_DATA_FORK
;
449 args
->op_flags
= XFS_DA_OP_OKNOENT
;
450 args
->owner
= dp
->i_ino
;
452 args
->op_flags
|= XFS_DA_OP_CILOOKUP
;
454 lock_mode
= xfs_ilock_data_map_shared(dp
);
455 rval
= xfs_dir_lookup_args(args
);
457 *inum
= args
->inumber
;
459 ci_name
->name
= args
->value
;
460 ci_name
->len
= args
->valuelen
;
463 xfs_iunlock(dp
, lock_mode
);
469 xfs_dir_removename_args(
470 struct xfs_da_args
*args
)
474 switch (xfs_dir2_format(args
, &error
)) {
475 case XFS_DIR2_FMT_SF
:
476 return xfs_dir2_sf_removename(args
);
477 case XFS_DIR2_FMT_BLOCK
:
478 return xfs_dir2_block_removename(args
);
479 case XFS_DIR2_FMT_LEAF
:
480 return xfs_dir2_leaf_removename(args
);
481 case XFS_DIR2_FMT_NODE
:
482 return xfs_dir2_node_removename(args
);
489 * Remove an entry from a directory.
493 struct xfs_trans
*tp
,
494 struct xfs_inode
*dp
,
495 const struct xfs_name
*name
,
497 xfs_extlen_t total
) /* bmap's total block count */
499 struct xfs_da_args
*args
;
502 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
503 XFS_STATS_INC(dp
->i_mount
, xs_dir_remove
);
505 args
= kzalloc(sizeof(*args
), GFP_KERNEL
| __GFP_NOFAIL
);
509 args
->geo
= dp
->i_mount
->m_dir_geo
;
510 args
->name
= name
->name
;
511 args
->namelen
= name
->len
;
512 args
->filetype
= name
->type
;
513 args
->hashval
= xfs_dir2_hashname(dp
->i_mount
, name
);
517 args
->whichfork
= XFS_DATA_FORK
;
519 args
->owner
= dp
->i_ino
;
520 rval
= xfs_dir_removename_args(args
);
526 xfs_dir_replace_args(
527 struct xfs_da_args
*args
)
531 switch (xfs_dir2_format(args
, &error
)) {
532 case XFS_DIR2_FMT_SF
:
533 return xfs_dir2_sf_replace(args
);
534 case XFS_DIR2_FMT_BLOCK
:
535 return xfs_dir2_block_replace(args
);
536 case XFS_DIR2_FMT_LEAF
:
537 return xfs_dir2_leaf_replace(args
);
538 case XFS_DIR2_FMT_NODE
:
539 return xfs_dir2_node_replace(args
);
546 * Replace the inode number of a directory entry.
550 struct xfs_trans
*tp
,
551 struct xfs_inode
*dp
,
552 const struct xfs_name
*name
, /* name of entry to replace */
553 xfs_ino_t inum
, /* new inode number */
554 xfs_extlen_t total
) /* bmap's total block count */
556 struct xfs_da_args
*args
;
559 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
561 rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
);
565 args
= kzalloc(sizeof(*args
), GFP_KERNEL
| __GFP_NOFAIL
);
569 args
->geo
= dp
->i_mount
->m_dir_geo
;
570 args
->name
= name
->name
;
571 args
->namelen
= name
->len
;
572 args
->filetype
= name
->type
;
573 args
->hashval
= xfs_dir2_hashname(dp
->i_mount
, name
);
574 args
->inumber
= inum
;
577 args
->whichfork
= XFS_DATA_FORK
;
579 args
->owner
= dp
->i_ino
;
580 rval
= xfs_dir_replace_args(args
);
586 * See if this entry can be added to the directory without allocating space.
590 struct xfs_trans
*tp
,
591 struct xfs_inode
*dp
,
592 const struct xfs_name
*name
) /* name of entry to add */
594 return xfs_dir_createname(tp
, dp
, name
, 0, 0);
602 * Add a block to the directory.
604 * This routine is for data and free blocks, not leaf/node blocks which are
605 * handled by xfs_da_grow_inode.
609 struct xfs_da_args
*args
,
610 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
611 xfs_dir2_db_t
*dbp
) /* out: block number added */
613 struct xfs_inode
*dp
= args
->dp
;
614 struct xfs_mount
*mp
= dp
->i_mount
;
615 xfs_fileoff_t bno
; /* directory offset of new block */
616 int count
; /* count of filesystem blocks */
619 trace_xfs_dir2_grow_inode(args
, space
);
622 * Set lowest possible block in the space requested.
624 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
625 count
= args
->geo
->fsbcount
;
627 error
= xfs_da_grow_inode_int(args
, &bno
, count
);
631 *dbp
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)bno
);
634 * Update file's size if this is the data space and it grew.
636 if (space
== XFS_DIR2_DATA_SPACE
) {
637 xfs_fsize_t size
; /* directory file (data) size */
639 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
640 if (size
> dp
->i_disk_size
) {
641 dp
->i_disk_size
= size
;
642 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
649 * Remove the given block from the directory.
650 * This routine is used for data and free blocks, leaf/node are done
651 * by xfs_da_shrink_inode.
654 xfs_dir2_shrink_inode(
655 struct xfs_da_args
*args
,
659 xfs_fileoff_t bno
; /* directory file offset */
660 xfs_dablk_t da
; /* directory file offset */
661 int done
; /* bunmap is finished */
662 struct xfs_inode
*dp
;
664 struct xfs_mount
*mp
;
665 struct xfs_trans
*tp
;
667 trace_xfs_dir2_shrink_inode(args
, db
);
672 da
= xfs_dir2_db_to_da(args
->geo
, db
);
674 /* Unmap the fsblock(s). */
675 error
= xfs_bunmapi(tp
, dp
, da
, args
->geo
->fsbcount
, 0, 0, &done
);
678 * ENOSPC actually can happen if we're in a removename with no
679 * space reservation, and the resulting block removal would
680 * cause a bmap btree split or conversion from extents to btree.
681 * This can only happen for un-fragmented directory blocks,
682 * since you need to be punching out the middle of an extent.
683 * In this case we need to leave the block in the file, and not
684 * binval it. So the block has to be in a consistent empty
685 * state and appropriately logged. We don't free up the buffer,
686 * the caller can tell it hasn't happened since it got an error
693 * Invalidate the buffer from the transaction.
695 xfs_trans_binval(tp
, bp
);
697 * If it's not a data block, we're done.
699 if (db
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
))
702 * If the block isn't the last one in the directory, we're done.
704 if (dp
->i_disk_size
> xfs_dir2_db_off_to_byte(args
->geo
, db
+ 1, 0))
707 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
709 * This can't really happen unless there's kernel corruption.
713 if (db
== args
->geo
->datablk
)
718 * Set the size to the new last block.
720 dp
->i_disk_size
= XFS_FSB_TO_B(mp
, bno
);
721 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
725 /* Returns true if the directory entry name is valid. */
732 * MAXNAMELEN includes the trailing null, but (name/length) leave it
733 * out, so use >= for the length check.
735 if (length
>= MAXNAMELEN
)
738 /* There shouldn't be any slashes or nulls here */
739 return !memchr(name
, '/', length
) && !memchr(name
, 0, length
);
744 struct xfs_mount
*mp
,
745 const struct xfs_name
*name
)
747 if (unlikely(xfs_has_asciici(mp
)))
748 return xfs_ascii_ci_hashname(name
);
749 return xfs_da_hashname(name
->name
, name
->len
);
754 struct xfs_da_args
*args
,
755 const unsigned char *name
,
758 if (unlikely(xfs_has_asciici(args
->dp
->i_mount
)))
759 return xfs_ascii_ci_compname(args
, name
, len
);
760 return xfs_da_compname(args
, name
, len
);
763 #ifdef CONFIG_XFS_LIVE_HOOKS
765 * Use a static key here to reduce the overhead of directory live update hooks.
766 * If the compiler supports jump labels, the static branch will be replaced by
767 * a nop sled when there are no hook users. Online fsck is currently the only
768 * caller, so this is a reasonable tradeoff.
770 * Note: Patching the kernel code requires taking the cpu hotplug lock. Other
771 * parts of the kernel allocate memory with that lock held, which means that
772 * XFS callers cannot hold any locks that might be used by memory reclaim or
773 * writeback when calling the static_branch_{inc,dec} functions.
775 DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dir_hooks_switch
);
778 xfs_dir_hook_disable(void)
780 xfs_hooks_switch_off(&xfs_dir_hooks_switch
);
784 xfs_dir_hook_enable(void)
786 xfs_hooks_switch_on(&xfs_dir_hooks_switch
);
789 /* Call hooks for a directory update relating to a child dirent update. */
792 struct xfs_inode
*dp
,
793 struct xfs_inode
*ip
,
795 const struct xfs_name
*name
)
797 if (xfs_hooks_switched_on(&xfs_dir_hooks_switch
)) {
798 struct xfs_dir_update_params p
= {
804 struct xfs_mount
*mp
= ip
->i_mount
;
806 xfs_hooks_call(&mp
->m_dir_update_hooks
, 0, &p
);
810 /* Call the specified function during a directory update. */
813 struct xfs_mount
*mp
,
814 struct xfs_dir_hook
*hook
)
816 return xfs_hooks_add(&mp
->m_dir_update_hooks
, &hook
->dirent_hook
);
819 /* Stop calling the specified function during a directory update. */
822 struct xfs_mount
*mp
,
823 struct xfs_dir_hook
*hook
)
825 xfs_hooks_del(&mp
->m_dir_update_hooks
, &hook
->dirent_hook
);
828 /* Configure directory update hook functions. */
831 struct xfs_dir_hook
*hook
,
832 notifier_fn_t mod_fn
)
834 xfs_hook_setup(&hook
->dirent_hook
, mod_fn
);
836 #endif /* CONFIG_XFS_LIVE_HOOKS */
839 * Given a directory @dp, a newly allocated inode @ip, and a @name, link @ip
840 * into @dp under the given @name. If @ip is a directory, it will be
841 * initialized. Both inodes must have the ILOCK held and the transaction must
842 * have sufficient blocks reserved.
845 xfs_dir_create_child(
846 struct xfs_trans
*tp
,
847 unsigned int resblks
,
848 struct xfs_dir_update
*du
)
850 struct xfs_inode
*dp
= du
->dp
;
851 const struct xfs_name
*name
= du
->name
;
852 struct xfs_inode
*ip
= du
->ip
;
855 xfs_assert_ilocked(ip
, XFS_ILOCK_EXCL
);
856 xfs_assert_ilocked(dp
, XFS_ILOCK_EXCL
);
858 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
, resblks
);
860 ASSERT(error
!= -ENOSPC
);
864 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
865 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
867 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
868 error
= xfs_dir_init(tp
, ip
, dp
);
872 xfs_bumplink(tp
, dp
);
876 * If we have parent pointers, we need to add the attribute containing
877 * the parent information now.
880 error
= xfs_parent_addname(tp
, du
->ppargs
, dp
, name
, ip
);
885 xfs_dir_update_hook(dp
, ip
, 1, name
);
890 * Given a directory @dp, an existing non-directory inode @ip, and a @name,
891 * link @ip into @dp under the given @name. Both inodes must have the ILOCK
896 struct xfs_trans
*tp
,
897 unsigned int resblks
,
898 struct xfs_dir_update
*du
)
900 struct xfs_inode
*dp
= du
->dp
;
901 const struct xfs_name
*name
= du
->name
;
902 struct xfs_inode
*ip
= du
->ip
;
903 struct xfs_mount
*mp
= tp
->t_mountp
;
906 xfs_assert_ilocked(ip
, XFS_ILOCK_EXCL
);
907 xfs_assert_ilocked(dp
, XFS_ILOCK_EXCL
);
908 ASSERT(!S_ISDIR(VFS_I(ip
)->i_mode
));
911 error
= xfs_dir_canenter(tp
, dp
, name
);
917 * Handle initial link state of O_TMPFILE inode
919 if (VFS_I(ip
)->i_nlink
== 0) {
920 struct xfs_perag
*pag
;
922 pag
= xfs_perag_get(mp
, XFS_INO_TO_AGNO(mp
, ip
->i_ino
));
923 error
= xfs_iunlink_remove(tp
, pag
, ip
);
929 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
, resblks
);
933 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
934 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
936 xfs_bumplink(tp
, ip
);
939 * If we have parent pointers, we now need to add the parent record to
940 * the attribute fork of the inode. If this is the initial parent
941 * attribute, we need to create it correctly, otherwise we can just add
942 * the parent to the inode.
945 error
= xfs_parent_addname(tp
, du
->ppargs
, dp
, name
, ip
);
950 xfs_dir_update_hook(dp
, ip
, 1, name
);
955 * Given a directory @dp, a child @ip, and a @name, remove the (@name, @ip)
956 * entry from the directory. Both inodes must have the ILOCK held.
959 xfs_dir_remove_child(
960 struct xfs_trans
*tp
,
961 unsigned int resblks
,
962 struct xfs_dir_update
*du
)
964 struct xfs_inode
*dp
= du
->dp
;
965 const struct xfs_name
*name
= du
->name
;
966 struct xfs_inode
*ip
= du
->ip
;
969 xfs_assert_ilocked(ip
, XFS_ILOCK_EXCL
);
970 xfs_assert_ilocked(dp
, XFS_ILOCK_EXCL
);
973 * If we're removing a directory perform some additional validation.
975 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
976 ASSERT(VFS_I(ip
)->i_nlink
>= 2);
977 if (VFS_I(ip
)->i_nlink
!= 2)
979 if (!xfs_dir_isempty(ip
))
982 /* Drop the link from ip's "..". */
983 error
= xfs_droplink(tp
, dp
);
987 /* Drop the "." link from ip to self. */
988 error
= xfs_droplink(tp
, ip
);
993 * Point the unlinked child directory's ".." entry to the root
994 * directory to eliminate back-references to inodes that may
995 * get freed before the child directory is closed. If the fs
996 * gets shrunk, this can lead to dirent inode validation errors.
998 if (dp
->i_ino
!= tp
->t_mountp
->m_sb
.sb_rootino
) {
999 error
= xfs_dir_replace(tp
, ip
, &xfs_name_dotdot
,
1000 tp
->t_mountp
->m_sb
.sb_rootino
, 0);
1006 * When removing a non-directory we need to log the parent
1007 * inode here. For a directory this is done implicitly
1008 * by the xfs_droplink call for the ".." entry.
1010 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1012 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1014 /* Drop the link from dp to ip. */
1015 error
= xfs_droplink(tp
, ip
);
1019 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
, resblks
);
1021 ASSERT(error
!= -ENOENT
);
1025 /* Remove parent pointer. */
1027 error
= xfs_parent_removename(tp
, du
->ppargs
, dp
, name
, ip
);
1032 xfs_dir_update_hook(dp
, ip
, -1, name
);
1037 * Exchange the entry (@name1, @ip1) in directory @dp1 with the entry (@name2,
1038 * @ip2) in directory @dp2, and update '..' @ip1 and @ip2's entries as needed.
1039 * @ip1 and @ip2 need not be of the same type.
1041 * All inodes must have the ILOCK held, and both entries must already exist.
1044 xfs_dir_exchange_children(
1045 struct xfs_trans
*tp
,
1046 struct xfs_dir_update
*du1
,
1047 struct xfs_dir_update
*du2
,
1048 unsigned int spaceres
)
1050 struct xfs_inode
*dp1
= du1
->dp
;
1051 const struct xfs_name
*name1
= du1
->name
;
1052 struct xfs_inode
*ip1
= du1
->ip
;
1053 struct xfs_inode
*dp2
= du2
->dp
;
1054 const struct xfs_name
*name2
= du2
->name
;
1055 struct xfs_inode
*ip2
= du2
->ip
;
1061 /* Swap inode number for dirent in first parent */
1062 error
= xfs_dir_replace(tp
, dp1
, name1
, ip2
->i_ino
, spaceres
);
1066 /* Swap inode number for dirent in second parent */
1067 error
= xfs_dir_replace(tp
, dp2
, name2
, ip1
->i_ino
, spaceres
);
1072 * If we're renaming one or more directories across different parents,
1073 * update the respective ".." entries (and link counts) to match the new
1077 dp2_flags
= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
1079 if (S_ISDIR(VFS_I(ip2
)->i_mode
)) {
1080 error
= xfs_dir_replace(tp
, ip2
, &xfs_name_dotdot
,
1081 dp1
->i_ino
, spaceres
);
1085 /* transfer ip2 ".." reference to dp1 */
1086 if (!S_ISDIR(VFS_I(ip1
)->i_mode
)) {
1087 error
= xfs_droplink(tp
, dp2
);
1090 xfs_bumplink(tp
, dp1
);
1094 * Although ip1 isn't changed here, userspace needs
1095 * to be warned about the change, so that applications
1096 * relying on it (like backup ones), will properly
1099 ip1_flags
|= XFS_ICHGTIME_CHG
;
1100 ip2_flags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
1103 if (S_ISDIR(VFS_I(ip1
)->i_mode
)) {
1104 error
= xfs_dir_replace(tp
, ip1
, &xfs_name_dotdot
,
1105 dp2
->i_ino
, spaceres
);
1109 /* transfer ip1 ".." reference to dp2 */
1110 if (!S_ISDIR(VFS_I(ip2
)->i_mode
)) {
1111 error
= xfs_droplink(tp
, dp1
);
1114 xfs_bumplink(tp
, dp2
);
1118 * Although ip2 isn't changed here, userspace needs
1119 * to be warned about the change, so that applications
1120 * relying on it (like backup ones), will properly
1123 ip1_flags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
1124 ip2_flags
|= XFS_ICHGTIME_CHG
;
1129 xfs_trans_ichgtime(tp
, ip1
, ip1_flags
);
1130 xfs_trans_log_inode(tp
, ip1
, XFS_ILOG_CORE
);
1133 xfs_trans_ichgtime(tp
, ip2
, ip2_flags
);
1134 xfs_trans_log_inode(tp
, ip2
, XFS_ILOG_CORE
);
1137 xfs_trans_ichgtime(tp
, dp2
, dp2_flags
);
1138 xfs_trans_log_inode(tp
, dp2
, XFS_ILOG_CORE
);
1140 xfs_trans_ichgtime(tp
, dp1
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1141 xfs_trans_log_inode(tp
, dp1
, XFS_ILOG_CORE
);
1143 /* Schedule parent pointer replacements */
1145 error
= xfs_parent_replacename(tp
, du1
->ppargs
, dp1
, name1
,
1152 error
= xfs_parent_replacename(tp
, du2
->ppargs
, dp2
, name2
,
1159 * Inform our hook clients that we've finished an exchange operation as
1160 * follows: removed the source and target files from their directories;
1161 * added the target to the source directory; and added the source to
1162 * the target directory. All inodes are locked, so it's ok to model a
1163 * rename this way so long as we say we deleted entries before we add
1166 xfs_dir_update_hook(dp1
, ip1
, -1, name1
);
1167 xfs_dir_update_hook(dp2
, ip2
, -1, name2
);
1168 xfs_dir_update_hook(dp1
, ip2
, 1, name1
);
1169 xfs_dir_update_hook(dp2
, ip1
, 1, name2
);
1174 * Given an entry (@src_name, @src_ip) in directory @src_dp, make the entry
1175 * @target_name in directory @target_dp point to @src_ip and remove the
1176 * original entry, cleaning up everything left behind.
1178 * Cleanup involves dropping a link count on @target_ip, and either removing
1179 * the (@src_name, @src_ip) entry from @src_dp or simply replacing the entry
1180 * with (@src_name, @wip) if a whiteout inode @wip is supplied.
1182 * All inodes must have the ILOCK held. We assume that if @src_ip is a
1183 * directory then its '..' doesn't already point to @target_dp, and that @wip
1184 * is a freshly allocated whiteout.
1187 xfs_dir_rename_children(
1188 struct xfs_trans
*tp
,
1189 struct xfs_dir_update
*du_src
,
1190 struct xfs_dir_update
*du_tgt
,
1191 unsigned int spaceres
,
1192 struct xfs_dir_update
*du_wip
)
1194 struct xfs_mount
*mp
= tp
->t_mountp
;
1195 struct xfs_inode
*src_dp
= du_src
->dp
;
1196 const struct xfs_name
*src_name
= du_src
->name
;
1197 struct xfs_inode
*src_ip
= du_src
->ip
;
1198 struct xfs_inode
*target_dp
= du_tgt
->dp
;
1199 const struct xfs_name
*target_name
= du_tgt
->name
;
1200 struct xfs_inode
*target_ip
= du_tgt
->ip
;
1201 bool new_parent
= (src_dp
!= target_dp
);
1202 bool src_is_directory
;
1205 src_is_directory
= S_ISDIR(VFS_I(src_ip
)->i_mode
);
1208 * Check for expected errors before we dirty the transaction
1209 * so we can return an error without a transaction abort.
1211 if (target_ip
== NULL
) {
1213 * If there's no space reservation, check the entry will
1214 * fit before actually inserting it.
1217 error
= xfs_dir_canenter(tp
, target_dp
, target_name
);
1223 * If target exists and it's a directory, check that whether
1224 * it can be destroyed.
1226 if (S_ISDIR(VFS_I(target_ip
)->i_mode
) &&
1227 (!xfs_dir_isempty(target_ip
) ||
1228 (VFS_I(target_ip
)->i_nlink
> 2)))
1233 * Directory entry creation below may acquire the AGF. Remove
1234 * the whiteout from the unlinked list first to preserve correct
1235 * AGI/AGF locking order. This dirties the transaction so failures
1236 * after this point will abort and log recovery will clean up the
1239 * For whiteouts, we need to bump the link count on the whiteout
1240 * inode. After this point, we have a real link, clear the tmpfile
1241 * state flag from the inode so it doesn't accidentally get misused
1245 struct xfs_perag
*pag
;
1247 ASSERT(VFS_I(du_wip
->ip
)->i_nlink
== 0);
1249 pag
= xfs_perag_get(mp
, XFS_INO_TO_AGNO(mp
, du_wip
->ip
->i_ino
));
1250 error
= xfs_iunlink_remove(tp
, pag
, du_wip
->ip
);
1255 xfs_bumplink(tp
, du_wip
->ip
);
1259 * Set up the target.
1261 if (target_ip
== NULL
) {
1263 * If target does not exist and the rename crosses
1264 * directories, adjust the target directory link count
1265 * to account for the ".." reference from the new entry.
1267 error
= xfs_dir_createname(tp
, target_dp
, target_name
,
1268 src_ip
->i_ino
, spaceres
);
1272 xfs_trans_ichgtime(tp
, target_dp
,
1273 XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1275 if (new_parent
&& src_is_directory
) {
1276 xfs_bumplink(tp
, target_dp
);
1278 } else { /* target_ip != NULL */
1280 * Link the source inode under the target name.
1281 * If the source inode is a directory and we are moving
1282 * it across directories, its ".." entry will be
1283 * inconsistent until we replace that down below.
1285 * In case there is already an entry with the same
1286 * name at the destination directory, remove it first.
1288 error
= xfs_dir_replace(tp
, target_dp
, target_name
,
1289 src_ip
->i_ino
, spaceres
);
1293 xfs_trans_ichgtime(tp
, target_dp
,
1294 XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1297 * Decrement the link count on the target since the target
1298 * dir no longer points to it.
1300 error
= xfs_droplink(tp
, target_ip
);
1304 if (src_is_directory
) {
1306 * Drop the link from the old "." entry.
1308 error
= xfs_droplink(tp
, target_ip
);
1312 } /* target_ip != NULL */
1315 * Remove the source.
1317 if (new_parent
&& src_is_directory
) {
1319 * Rewrite the ".." entry to point to the new
1322 error
= xfs_dir_replace(tp
, src_ip
, &xfs_name_dotdot
,
1323 target_dp
->i_ino
, spaceres
);
1324 ASSERT(error
!= -EEXIST
);
1330 * We always want to hit the ctime on the source inode.
1332 * This isn't strictly required by the standards since the source
1333 * inode isn't really being changed, but old unix file systems did
1334 * it and some incremental backup programs won't work without it.
1336 xfs_trans_ichgtime(tp
, src_ip
, XFS_ICHGTIME_CHG
);
1337 xfs_trans_log_inode(tp
, src_ip
, XFS_ILOG_CORE
);
1340 * Adjust the link count on src_dp. This is necessary when
1341 * renaming a directory, either within one parent when
1342 * the target existed, or across two parent directories.
1344 if (src_is_directory
&& (new_parent
|| target_ip
!= NULL
)) {
1347 * Decrement link count on src_directory since the
1348 * entry that's moved no longer points to it.
1350 error
= xfs_droplink(tp
, src_dp
);
1356 * For whiteouts, we only need to update the source dirent with the
1357 * inode number of the whiteout inode rather than removing it
1361 error
= xfs_dir_replace(tp
, src_dp
, src_name
, du_wip
->ip
->i_ino
,
1364 error
= xfs_dir_removename(tp
, src_dp
, src_name
, src_ip
->i_ino
,
1369 xfs_trans_ichgtime(tp
, src_dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1370 xfs_trans_log_inode(tp
, src_dp
, XFS_ILOG_CORE
);
1372 xfs_trans_log_inode(tp
, target_dp
, XFS_ILOG_CORE
);
1374 /* Schedule parent pointer updates. */
1375 if (du_wip
->ppargs
) {
1376 error
= xfs_parent_addname(tp
, du_wip
->ppargs
, src_dp
,
1377 src_name
, du_wip
->ip
);
1382 if (du_src
->ppargs
) {
1383 error
= xfs_parent_replacename(tp
, du_src
->ppargs
, src_dp
,
1384 src_name
, target_dp
, target_name
, src_ip
);
1389 if (du_tgt
->ppargs
) {
1390 error
= xfs_parent_removename(tp
, du_tgt
->ppargs
, target_dp
,
1391 target_name
, target_ip
);
1397 * Inform our hook clients that we've finished a rename operation as
1398 * follows: removed the source and target files from their directories;
1399 * that we've added the source to the target directory; and finally
1400 * that we've added the whiteout, if there was one. All inodes are
1401 * locked, so it's ok to model a rename this way so long as we say we
1402 * deleted entries before we add new ones.
1405 xfs_dir_update_hook(target_dp
, target_ip
, -1, target_name
);
1406 xfs_dir_update_hook(src_dp
, src_ip
, -1, src_name
);
1407 xfs_dir_update_hook(target_dp
, src_ip
, 1, target_name
);
1409 xfs_dir_update_hook(src_dp
, du_wip
->ip
, 1, src_name
);