1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
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_defer.h"
13 #include "xfs_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_inode_item.h"
20 #include "xfs_dir2_priv.h"
21 #include "xfs_ialloc.h"
22 #include "xfs_errortag.h"
23 #include "xfs_error.h"
24 #include "xfs_trace.h"
26 struct xfs_name xfs_name_dotdot
= { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR
};
29 * Convert inode mode to directory entry filetype
35 switch (mode
& S_IFMT
) {
37 return XFS_DIR3_FT_REG_FILE
;
39 return XFS_DIR3_FT_DIR
;
41 return XFS_DIR3_FT_CHRDEV
;
43 return XFS_DIR3_FT_BLKDEV
;
45 return XFS_DIR3_FT_FIFO
;
47 return XFS_DIR3_FT_SOCK
;
49 return XFS_DIR3_FT_SYMLINK
;
51 return XFS_DIR3_FT_UNKNOWN
;
56 * ASCII case-insensitive (ie. A-Z) support for directories that was
60 xfs_ascii_ci_hashname(
61 struct xfs_name
*name
)
66 for (i
= 0, hash
= 0; i
< name
->len
; i
++)
67 hash
= tolower(name
->name
[i
]) ^ rol32(hash
, 7);
73 xfs_ascii_ci_compname(
74 struct xfs_da_args
*args
,
75 const unsigned char *name
,
78 enum xfs_dacmp result
;
81 if (args
->namelen
!= len
)
82 return XFS_CMP_DIFFERENT
;
84 result
= XFS_CMP_EXACT
;
85 for (i
= 0; i
< len
; i
++) {
86 if (args
->name
[i
] == name
[i
])
88 if (tolower(args
->name
[i
]) != tolower(name
[i
]))
89 return XFS_CMP_DIFFERENT
;
90 result
= XFS_CMP_CASE
;
96 static const struct xfs_nameops xfs_ascii_ci_nameops
= {
97 .hashname
= xfs_ascii_ci_hashname
,
98 .compname
= xfs_ascii_ci_compname
,
103 struct xfs_mount
*mp
)
105 struct xfs_da_geometry
*dageo
;
109 ASSERT(mp
->m_sb
.sb_versionnum
& XFS_SB_VERSION_DIRV2BIT
);
110 ASSERT(xfs_dir2_dirblock_bytes(&mp
->m_sb
) <= XFS_MAX_BLOCKSIZE
);
112 mp
->m_dir_inode_ops
= xfs_dir_get_ops(mp
, NULL
);
113 mp
->m_nondir_inode_ops
= xfs_nondir_get_ops(mp
, NULL
);
115 nodehdr_size
= mp
->m_dir_inode_ops
->node_hdr_size
;
116 mp
->m_dir_geo
= kmem_zalloc(sizeof(struct xfs_da_geometry
),
117 KM_SLEEP
| KM_MAYFAIL
);
118 mp
->m_attr_geo
= kmem_zalloc(sizeof(struct xfs_da_geometry
),
119 KM_SLEEP
| KM_MAYFAIL
);
120 if (!mp
->m_dir_geo
|| !mp
->m_attr_geo
) {
121 kmem_free(mp
->m_dir_geo
);
122 kmem_free(mp
->m_attr_geo
);
126 /* set up directory geometry */
127 dageo
= mp
->m_dir_geo
;
128 dageo
->blklog
= mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
;
129 dageo
->fsblog
= mp
->m_sb
.sb_blocklog
;
130 dageo
->blksize
= xfs_dir2_dirblock_bytes(&mp
->m_sb
);
131 dageo
->fsbcount
= 1 << mp
->m_sb
.sb_dirblklog
;
134 * Now we've set up the block conversion variables, we can calculate the
135 * segment block constants using the geometry structure.
137 dageo
->datablk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_DATA_OFFSET
);
138 dageo
->leafblk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_LEAF_OFFSET
);
139 dageo
->freeblk
= xfs_dir2_byte_to_da(dageo
, XFS_DIR2_FREE_OFFSET
);
140 dageo
->node_ents
= (dageo
->blksize
- nodehdr_size
) /
141 (uint
)sizeof(xfs_da_node_entry_t
);
142 dageo
->magicpct
= (dageo
->blksize
* 37) / 100;
144 /* set up attribute geometry - single fsb only */
145 dageo
= mp
->m_attr_geo
;
146 dageo
->blklog
= mp
->m_sb
.sb_blocklog
;
147 dageo
->fsblog
= mp
->m_sb
.sb_blocklog
;
148 dageo
->blksize
= 1 << dageo
->blklog
;
150 dageo
->node_ents
= (dageo
->blksize
- nodehdr_size
) /
151 (uint
)sizeof(xfs_da_node_entry_t
);
152 dageo
->magicpct
= (dageo
->blksize
* 37) / 100;
154 if (xfs_sb_version_hasasciici(&mp
->m_sb
))
155 mp
->m_dirnameops
= &xfs_ascii_ci_nameops
;
157 mp
->m_dirnameops
= &xfs_default_nameops
;
164 struct xfs_mount
*mp
)
166 kmem_free(mp
->m_dir_geo
);
167 kmem_free(mp
->m_attr_geo
);
171 * Return 1 if directory contains only "." and "..".
177 xfs_dir2_sf_hdr_t
*sfp
;
179 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
180 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
182 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
184 sfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
189 * Validate a given inode number.
192 xfs_dir_ino_validate(
196 bool ino_ok
= xfs_verify_dir_ino(mp
, ino
);
198 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
))) {
199 xfs_warn(mp
, "Invalid inode number 0x%Lx",
200 (unsigned long long) ino
);
201 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
202 return -EFSCORRUPTED
;
208 * Initialize a directory with its "." and ".." entries.
216 struct xfs_da_args
*args
;
219 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
220 error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
);
224 args
= kmem_zalloc(sizeof(*args
), KM_SLEEP
| KM_NOFS
);
228 args
->geo
= dp
->i_mount
->m_dir_geo
;
231 error
= xfs_dir2_sf_create(args
, pdp
->i_ino
);
237 * Enter a name in a directory, or check for available space.
238 * If inum is 0, only the available space test is performed.
242 struct xfs_trans
*tp
,
243 struct xfs_inode
*dp
,
244 struct xfs_name
*name
,
245 xfs_ino_t inum
, /* new entry inode number */
246 xfs_extlen_t total
) /* bmap's total block count */
248 struct xfs_da_args
*args
;
250 int v
; /* type-checking value */
252 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
255 rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
);
258 XFS_STATS_INC(dp
->i_mount
, xs_dir_create
);
261 args
= kmem_zalloc(sizeof(*args
), KM_SLEEP
| KM_NOFS
);
265 args
->geo
= dp
->i_mount
->m_dir_geo
;
266 args
->name
= name
->name
;
267 args
->namelen
= name
->len
;
268 args
->filetype
= name
->type
;
269 args
->hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
270 args
->inumber
= inum
;
273 args
->whichfork
= XFS_DATA_FORK
;
275 args
->op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
277 args
->op_flags
|= XFS_DA_OP_JUSTCHECK
;
279 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
280 rval
= xfs_dir2_sf_addname(args
);
284 rval
= xfs_dir2_isblock(args
, &v
);
288 rval
= xfs_dir2_block_addname(args
);
292 rval
= xfs_dir2_isleaf(args
, &v
);
296 rval
= xfs_dir2_leaf_addname(args
);
298 rval
= xfs_dir2_node_addname(args
);
306 * If doing a CI lookup and case-insensitive match, dup actual name into
307 * args.value. Return EEXIST for success (ie. name found) or an error.
310 xfs_dir_cilookup_result(
311 struct xfs_da_args
*args
,
312 const unsigned char *name
,
315 if (args
->cmpresult
== XFS_CMP_DIFFERENT
)
317 if (args
->cmpresult
!= XFS_CMP_CASE
||
318 !(args
->op_flags
& XFS_DA_OP_CILOOKUP
))
321 args
->value
= kmem_alloc(len
, KM_NOFS
| KM_MAYFAIL
);
325 memcpy(args
->value
, name
, len
);
326 args
->valuelen
= len
;
331 * Lookup a name in a directory, give back the inode number.
332 * If ci_name is not NULL, returns the actual name in ci_name if it differs
333 * to name, or ci_name->name is set to NULL for an exact match.
340 struct xfs_name
*name
,
341 xfs_ino_t
*inum
, /* out: inode number */
342 struct xfs_name
*ci_name
) /* out: actual name if CI match */
344 struct xfs_da_args
*args
;
346 int v
; /* type-checking value */
349 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
350 XFS_STATS_INC(dp
->i_mount
, xs_dir_lookup
);
353 * We need to use KM_NOFS here so that lockdep will not throw false
354 * positive deadlock warnings on a non-transactional lookup path. It is
355 * safe to recurse into inode recalim in that case, but lockdep can't
356 * easily be taught about it. Hence KM_NOFS avoids having to add more
357 * lockdep Doing this avoids having to add a bunch of lockdep class
358 * annotations into the reclaim path for the ilock.
360 args
= kmem_zalloc(sizeof(*args
), KM_SLEEP
| KM_NOFS
);
361 args
->geo
= dp
->i_mount
->m_dir_geo
;
362 args
->name
= name
->name
;
363 args
->namelen
= name
->len
;
364 args
->filetype
= name
->type
;
365 args
->hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
367 args
->whichfork
= XFS_DATA_FORK
;
369 args
->op_flags
= XFS_DA_OP_OKNOENT
;
371 args
->op_flags
|= XFS_DA_OP_CILOOKUP
;
373 lock_mode
= xfs_ilock_data_map_shared(dp
);
374 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
375 rval
= xfs_dir2_sf_lookup(args
);
379 rval
= xfs_dir2_isblock(args
, &v
);
383 rval
= xfs_dir2_block_lookup(args
);
387 rval
= xfs_dir2_isleaf(args
, &v
);
391 rval
= xfs_dir2_leaf_lookup(args
);
393 rval
= xfs_dir2_node_lookup(args
);
399 *inum
= args
->inumber
;
401 ci_name
->name
= args
->value
;
402 ci_name
->len
= args
->valuelen
;
406 xfs_iunlock(dp
, lock_mode
);
412 * Remove an entry from a directory.
416 struct xfs_trans
*tp
,
417 struct xfs_inode
*dp
,
418 struct xfs_name
*name
,
420 xfs_extlen_t total
) /* bmap's total block count */
422 struct xfs_da_args
*args
;
424 int v
; /* type-checking value */
426 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
427 XFS_STATS_INC(dp
->i_mount
, xs_dir_remove
);
429 args
= kmem_zalloc(sizeof(*args
), KM_SLEEP
| KM_NOFS
);
433 args
->geo
= dp
->i_mount
->m_dir_geo
;
434 args
->name
= name
->name
;
435 args
->namelen
= name
->len
;
436 args
->filetype
= name
->type
;
437 args
->hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
441 args
->whichfork
= XFS_DATA_FORK
;
444 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
445 rval
= xfs_dir2_sf_removename(args
);
449 rval
= xfs_dir2_isblock(args
, &v
);
453 rval
= xfs_dir2_block_removename(args
);
457 rval
= xfs_dir2_isleaf(args
, &v
);
461 rval
= xfs_dir2_leaf_removename(args
);
463 rval
= xfs_dir2_node_removename(args
);
470 * Replace the inode number of a directory entry.
474 struct xfs_trans
*tp
,
475 struct xfs_inode
*dp
,
476 struct xfs_name
*name
, /* name of entry to replace */
477 xfs_ino_t inum
, /* new inode number */
478 xfs_extlen_t total
) /* bmap's total block count */
480 struct xfs_da_args
*args
;
482 int v
; /* type-checking value */
484 ASSERT(S_ISDIR(VFS_I(dp
)->i_mode
));
486 rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
);
490 args
= kmem_zalloc(sizeof(*args
), KM_SLEEP
| KM_NOFS
);
494 args
->geo
= dp
->i_mount
->m_dir_geo
;
495 args
->name
= name
->name
;
496 args
->namelen
= name
->len
;
497 args
->filetype
= name
->type
;
498 args
->hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
499 args
->inumber
= inum
;
502 args
->whichfork
= XFS_DATA_FORK
;
505 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
506 rval
= xfs_dir2_sf_replace(args
);
510 rval
= xfs_dir2_isblock(args
, &v
);
514 rval
= xfs_dir2_block_replace(args
);
518 rval
= xfs_dir2_isleaf(args
, &v
);
522 rval
= xfs_dir2_leaf_replace(args
);
524 rval
= xfs_dir2_node_replace(args
);
531 * See if this entry can be added to the directory without allocating space.
537 struct xfs_name
*name
) /* name of entry to add */
539 return xfs_dir_createname(tp
, dp
, name
, 0, 0);
547 * Add a block to the directory.
549 * This routine is for data and free blocks, not leaf/node blocks which are
550 * handled by xfs_da_grow_inode.
554 struct xfs_da_args
*args
,
555 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
556 xfs_dir2_db_t
*dbp
) /* out: block number added */
558 struct xfs_inode
*dp
= args
->dp
;
559 struct xfs_mount
*mp
= dp
->i_mount
;
560 xfs_fileoff_t bno
; /* directory offset of new block */
561 int count
; /* count of filesystem blocks */
564 trace_xfs_dir2_grow_inode(args
, space
);
567 * Set lowest possible block in the space requested.
569 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
570 count
= args
->geo
->fsbcount
;
572 error
= xfs_da_grow_inode_int(args
, &bno
, count
);
576 *dbp
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)bno
);
579 * Update file's size if this is the data space and it grew.
581 if (space
== XFS_DIR2_DATA_SPACE
) {
582 xfs_fsize_t size
; /* directory file (data) size */
584 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
585 if (size
> dp
->i_d
.di_size
) {
586 dp
->i_d
.di_size
= size
;
587 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
594 * See if the directory is a single-block form directory.
598 struct xfs_da_args
*args
,
599 int *vp
) /* out: 1 is block, 0 is not block */
601 xfs_fileoff_t last
; /* last file offset */
604 if ((rval
= xfs_bmap_last_offset(args
->dp
, &last
, XFS_DATA_FORK
)))
606 rval
= XFS_FSB_TO_B(args
->dp
->i_mount
, last
) == args
->geo
->blksize
;
607 if (rval
!= 0 && args
->dp
->i_d
.di_size
!= args
->geo
->blksize
)
608 return -EFSCORRUPTED
;
614 * See if the directory is a single-leaf form directory.
618 struct xfs_da_args
*args
,
619 int *vp
) /* out: 1 is block, 0 is not block */
621 xfs_fileoff_t last
; /* last file offset */
624 if ((rval
= xfs_bmap_last_offset(args
->dp
, &last
, XFS_DATA_FORK
)))
626 *vp
= last
== args
->geo
->leafblk
+ args
->geo
->fsbcount
;
631 * Remove the given block from the directory.
632 * This routine is used for data and free blocks, leaf/node are done
633 * by xfs_da_shrink_inode.
636 xfs_dir2_shrink_inode(
637 struct xfs_da_args
*args
,
641 xfs_fileoff_t bno
; /* directory file offset */
642 xfs_dablk_t da
; /* directory file offset */
643 int done
; /* bunmap is finished */
644 struct xfs_inode
*dp
;
646 struct xfs_mount
*mp
;
647 struct xfs_trans
*tp
;
649 trace_xfs_dir2_shrink_inode(args
, db
);
654 da
= xfs_dir2_db_to_da(args
->geo
, db
);
656 /* Unmap the fsblock(s). */
657 error
= xfs_bunmapi(tp
, dp
, da
, args
->geo
->fsbcount
, 0, 0, &done
);
660 * ENOSPC actually can happen if we're in a removename with no
661 * space reservation, and the resulting block removal would
662 * cause a bmap btree split or conversion from extents to btree.
663 * This can only happen for un-fragmented directory blocks,
664 * since you need to be punching out the middle of an extent.
665 * In this case we need to leave the block in the file, and not
666 * binval it. So the block has to be in a consistent empty
667 * state and appropriately logged. We don't free up the buffer,
668 * the caller can tell it hasn't happened since it got an error
675 * Invalidate the buffer from the transaction.
677 xfs_trans_binval(tp
, bp
);
679 * If it's not a data block, we're done.
681 if (db
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
))
684 * If the block isn't the last one in the directory, we're done.
686 if (dp
->i_d
.di_size
> xfs_dir2_db_off_to_byte(args
->geo
, db
+ 1, 0))
689 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
691 * This can't really happen unless there's kernel corruption.
695 if (db
== args
->geo
->datablk
)
700 * Set the size to the new last block.
702 dp
->i_d
.di_size
= XFS_FSB_TO_B(mp
, bno
);
703 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);