1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
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_error.h"
20 #include "xfs_trace.h"
21 #include "xfs_health.h"
23 const struct xfs_name xfs_name_dotdot = {
24 .name = (const unsigned char *)"..",
26 .type = XFS_DIR3_FT_DIR,
29 const struct xfs_name xfs_name_dot = {
30 .name = (const unsigned char *)".",
32 .type = XFS_DIR3_FT_DIR,
36 * Convert inode mode to directory entry filetype
42 switch (mode & S_IFMT) {
44 return XFS_DIR3_FT_REG_FILE;
46 return XFS_DIR3_FT_DIR;
48 return XFS_DIR3_FT_CHRDEV;
50 return XFS_DIR3_FT_BLKDEV;
52 return XFS_DIR3_FT_FIFO;
54 return XFS_DIR3_FT_SOCK;
56 return XFS_DIR3_FT_SYMLINK;
58 return XFS_DIR3_FT_UNKNOWN;
63 * ASCII case-insensitive (ie. A-Z) support for directories that was
67 xfs_ascii_ci_hashname(
68 const struct xfs_name *name)
73 for (i = 0, hash = 0; i < name->len; i++)
74 hash = xfs_ascii_ci_xfrm(name->name[i]) ^ rol32(hash, 7);
80 xfs_ascii_ci_compname(
81 struct xfs_da_args *args,
82 const unsigned char *name,
85 enum xfs_dacmp result;
88 if (args->namelen != len)
89 return XFS_CMP_DIFFERENT;
91 result = XFS_CMP_EXACT;
92 for (i = 0; i < len; i++) {
93 if (args->name[i] == name[i])
95 if (xfs_ascii_ci_xfrm(args->name[i]) !=
96 xfs_ascii_ci_xfrm(name[i]))
97 return XFS_CMP_DIFFERENT;
98 result = XFS_CMP_CASE;
106 struct xfs_mount *mp)
108 struct xfs_da_geometry *dageo;
111 ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
112 ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
114 mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry),
115 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
116 mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry),
117 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
118 if (!mp->m_dir_geo || !mp->m_attr_geo) {
119 kfree(mp->m_dir_geo);
120 kfree(mp->m_attr_geo);
124 /* set up directory geometry */
125 dageo = mp->m_dir_geo;
126 dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
127 dageo->fsblog = mp->m_sb.sb_blocklog;
128 dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
129 dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
130 if (xfs_has_crc(mp)) {
131 dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr);
132 dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr);
133 dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr);
134 dageo->data_entry_offset =
135 sizeof(struct xfs_dir3_data_hdr);
137 dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr);
138 dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr);
139 dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr);
140 dageo->data_entry_offset =
141 sizeof(struct xfs_dir2_data_hdr);
143 dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) /
144 sizeof(struct xfs_dir2_leaf_entry);
145 dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) /
146 sizeof(xfs_dir2_data_off_t);
148 dageo->data_first_offset = dageo->data_entry_offset +
149 xfs_dir2_data_entsize(mp, 1) +
150 xfs_dir2_data_entsize(mp, 2);
153 * Now we've set up the block conversion variables, we can calculate the
154 * segment block constants using the geometry structure.
156 dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
157 dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
158 dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
159 dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
160 (uint)sizeof(xfs_da_node_entry_t);
161 dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >>
162 mp->m_sb.sb_blocklog;
163 dageo->magicpct = (dageo->blksize * 37) / 100;
165 /* set up attribute geometry - single fsb only */
166 dageo = mp->m_attr_geo;
167 dageo->blklog = mp->m_sb.sb_blocklog;
168 dageo->fsblog = mp->m_sb.sb_blocklog;
169 dageo->blksize = 1 << dageo->blklog;
171 dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size;
172 dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
173 (uint)sizeof(xfs_da_node_entry_t);
175 if (xfs_has_large_extent_counts(mp))
176 dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
178 dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
180 dageo->magicpct = (dageo->blksize * 37) / 100;
186 struct xfs_mount *mp)
188 kfree(mp->m_dir_geo);
189 kfree(mp->m_attr_geo);
193 * Return 1 if directory contains only "." and "..".
199 xfs_dir2_sf_hdr_t *sfp;
201 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
202 if (dp->i_disk_size == 0) /* might happen during shutdown. */
204 if (dp->i_disk_size > xfs_inode_data_fork_size(dp))
206 sfp = dp->i_df.if_data;
211 * Validate a given inode number.
214 xfs_dir_ino_validate(
218 bool ino_ok = xfs_verify_dir_ino(mp, ino);
220 if (XFS_IS_CORRUPT(mp, !ino_ok) ||
221 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DIR_INO_VALIDATE)) {
222 xfs_warn(mp, "Invalid inode number 0x%Lx",
223 (unsigned long long) ino);
224 return -EFSCORRUPTED;
230 * Initialize a directory with its "." and ".." entries.
238 struct xfs_da_args *args;
241 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
242 error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
246 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
250 args->geo = dp->i_mount->m_dir_geo;
253 args->owner = dp->i_ino;
254 error = xfs_dir2_sf_create(args, pdp->i_ino);
261 struct xfs_da_args *args,
264 struct xfs_inode *dp = args->dp;
265 struct xfs_mount *mp = dp->i_mount;
266 struct xfs_da_geometry *geo = mp->m_dir_geo;
269 xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
272 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
273 return XFS_DIR2_FMT_SF;
275 *error = xfs_bmap_last_offset(dp, &eof, XFS_DATA_FORK);
277 return XFS_DIR2_FMT_ERROR;
279 if (eof == XFS_B_TO_FSB(mp, geo->blksize)) {
280 if (XFS_IS_CORRUPT(mp, dp->i_disk_size != geo->blksize)) {
281 xfs_da_mark_sick(args);
282 *error = -EFSCORRUPTED;
283 return XFS_DIR2_FMT_ERROR;
285 return XFS_DIR2_FMT_BLOCK;
287 if (eof == geo->leafblk + geo->fsbcount)
288 return XFS_DIR2_FMT_LEAF;
289 return XFS_DIR2_FMT_NODE;
293 xfs_dir_createname_args(
294 struct xfs_da_args *args)
299 args->op_flags |= XFS_DA_OP_JUSTCHECK;
301 switch (xfs_dir2_format(args, &error)) {
302 case XFS_DIR2_FMT_SF:
303 return xfs_dir2_sf_addname(args);
304 case XFS_DIR2_FMT_BLOCK:
305 return xfs_dir2_block_addname(args);
306 case XFS_DIR2_FMT_LEAF:
307 return xfs_dir2_leaf_addname(args);
308 case XFS_DIR2_FMT_NODE:
309 return xfs_dir2_node_addname(args);
316 * Enter a name in a directory, or check for available space.
317 * If inum is 0, only the available space test is performed.
321 struct xfs_trans *tp,
322 struct xfs_inode *dp,
323 const struct xfs_name *name,
324 xfs_ino_t inum, /* new entry inode number */
325 xfs_extlen_t total) /* bmap's total block count */
327 struct xfs_da_args *args;
330 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
333 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
336 XFS_STATS_INC(dp->i_mount, xs_dir_create);
339 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
343 args->geo = dp->i_mount->m_dir_geo;
344 args->name = name->name;
345 args->namelen = name->len;
346 args->filetype = name->type;
347 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
348 args->inumber = inum;
351 args->whichfork = XFS_DATA_FORK;
353 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
354 args->owner = dp->i_ino;
356 rval = xfs_dir_createname_args(args);
362 * If doing a CI lookup and case-insensitive match, dup actual name into
363 * args.value. Return EEXIST for success (ie. name found) or an error.
366 xfs_dir_cilookup_result(
367 struct xfs_da_args *args,
368 const unsigned char *name,
371 if (args->cmpresult == XFS_CMP_DIFFERENT)
373 if (args->cmpresult != XFS_CMP_CASE ||
374 !(args->op_flags & XFS_DA_OP_CILOOKUP))
377 args->value = kmalloc(len,
378 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
382 memcpy(args->value, name, len);
383 args->valuelen = len;
389 struct xfs_da_args *args)
393 switch (xfs_dir2_format(args, &error)) {
394 case XFS_DIR2_FMT_SF:
395 error = xfs_dir2_sf_lookup(args);
397 case XFS_DIR2_FMT_BLOCK:
398 error = xfs_dir2_block_lookup(args);
400 case XFS_DIR2_FMT_LEAF:
401 error = xfs_dir2_leaf_lookup(args);
403 case XFS_DIR2_FMT_NODE:
404 error = xfs_dir2_node_lookup(args);
410 if (error != -EEXIST)
416 * Lookup a name in a directory, give back the inode number.
417 * If ci_name is not NULL, returns the actual name in ci_name if it differs
418 * to name, or ci_name->name is set to NULL for an exact match.
423 struct xfs_trans *tp,
424 struct xfs_inode *dp,
425 const struct xfs_name *name,
426 xfs_ino_t *inum, /* out: inode number */
427 struct xfs_name *ci_name) /* out: actual name if CI match */
429 struct xfs_da_args *args;
433 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
434 XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
436 args = kzalloc(sizeof(*args),
437 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
438 args->geo = dp->i_mount->m_dir_geo;
439 args->name = name->name;
440 args->namelen = name->len;
441 args->filetype = name->type;
442 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
444 args->whichfork = XFS_DATA_FORK;
446 args->op_flags = XFS_DA_OP_OKNOENT;
447 args->owner = dp->i_ino;
449 args->op_flags |= XFS_DA_OP_CILOOKUP;
451 lock_mode = xfs_ilock_data_map_shared(dp);
452 rval = xfs_dir_lookup_args(args);
454 *inum = args->inumber;
456 ci_name->name = args->value;
457 ci_name->len = args->valuelen;
460 xfs_iunlock(dp, lock_mode);
466 xfs_dir_removename_args(
467 struct xfs_da_args *args)
471 switch (xfs_dir2_format(args, &error)) {
472 case XFS_DIR2_FMT_SF:
473 return xfs_dir2_sf_removename(args);
474 case XFS_DIR2_FMT_BLOCK:
475 return xfs_dir2_block_removename(args);
476 case XFS_DIR2_FMT_LEAF:
477 return xfs_dir2_leaf_removename(args);
478 case XFS_DIR2_FMT_NODE:
479 return xfs_dir2_node_removename(args);
486 * Remove an entry from a directory.
490 struct xfs_trans *tp,
491 struct xfs_inode *dp,
492 const struct xfs_name *name,
494 xfs_extlen_t total) /* bmap's total block count */
496 struct xfs_da_args *args;
499 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
500 XFS_STATS_INC(dp->i_mount, xs_dir_remove);
502 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
506 args->geo = dp->i_mount->m_dir_geo;
507 args->name = name->name;
508 args->namelen = name->len;
509 args->filetype = name->type;
510 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
514 args->whichfork = XFS_DATA_FORK;
516 args->owner = dp->i_ino;
517 rval = xfs_dir_removename_args(args);
523 xfs_dir_replace_args(
524 struct xfs_da_args *args)
528 switch (xfs_dir2_format(args, &error)) {
529 case XFS_DIR2_FMT_SF:
530 return xfs_dir2_sf_replace(args);
531 case XFS_DIR2_FMT_BLOCK:
532 return xfs_dir2_block_replace(args);
533 case XFS_DIR2_FMT_LEAF:
534 return xfs_dir2_leaf_replace(args);
535 case XFS_DIR2_FMT_NODE:
536 return xfs_dir2_node_replace(args);
543 * Replace the inode number of a directory entry.
547 struct xfs_trans *tp,
548 struct xfs_inode *dp,
549 const struct xfs_name *name, /* name of entry to replace */
550 xfs_ino_t inum, /* new inode number */
551 xfs_extlen_t total) /* bmap's total block count */
553 struct xfs_da_args *args;
556 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
558 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
562 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
566 args->geo = dp->i_mount->m_dir_geo;
567 args->name = name->name;
568 args->namelen = name->len;
569 args->filetype = name->type;
570 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
571 args->inumber = inum;
574 args->whichfork = XFS_DATA_FORK;
576 args->owner = dp->i_ino;
577 rval = xfs_dir_replace_args(args);
583 * See if this entry can be added to the directory without allocating space.
589 struct xfs_name *name) /* name of entry to add */
591 return xfs_dir_createname(tp, dp, name, 0, 0);
599 * Add a block to the directory.
601 * This routine is for data and free blocks, not leaf/node blocks which are
602 * handled by xfs_da_grow_inode.
606 struct xfs_da_args *args,
607 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
608 xfs_dir2_db_t *dbp) /* out: block number added */
610 struct xfs_inode *dp = args->dp;
611 struct xfs_mount *mp = dp->i_mount;
612 xfs_fileoff_t bno; /* directory offset of new block */
613 int count; /* count of filesystem blocks */
616 trace_xfs_dir2_grow_inode(args, space);
619 * Set lowest possible block in the space requested.
621 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
622 count = args->geo->fsbcount;
624 error = xfs_da_grow_inode_int(args, &bno, count);
628 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
631 * Update file's size if this is the data space and it grew.
633 if (space == XFS_DIR2_DATA_SPACE) {
634 xfs_fsize_t size; /* directory file (data) size */
636 size = XFS_FSB_TO_B(mp, bno + count);
637 if (size > dp->i_disk_size) {
638 dp->i_disk_size = size;
639 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
646 * Remove the given block from the directory.
647 * This routine is used for data and free blocks, leaf/node are done
648 * by xfs_da_shrink_inode.
651 xfs_dir2_shrink_inode(
652 struct xfs_da_args *args,
656 xfs_fileoff_t bno; /* directory file offset */
657 xfs_dablk_t da; /* directory file offset */
658 int done; /* bunmap is finished */
659 struct xfs_inode *dp;
661 struct xfs_mount *mp;
662 struct xfs_trans *tp;
664 trace_xfs_dir2_shrink_inode(args, db);
669 da = xfs_dir2_db_to_da(args->geo, db);
671 /* Unmap the fsblock(s). */
672 error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
675 * ENOSPC actually can happen if we're in a removename with no
676 * space reservation, and the resulting block removal would
677 * cause a bmap btree split or conversion from extents to btree.
678 * This can only happen for un-fragmented directory blocks,
679 * since you need to be punching out the middle of an extent.
680 * In this case we need to leave the block in the file, and not
681 * binval it. So the block has to be in a consistent empty
682 * state and appropriately logged. We don't free up the buffer,
683 * the caller can tell it hasn't happened since it got an error
690 * Invalidate the buffer from the transaction.
692 xfs_trans_binval(tp, bp);
694 * If it's not a data block, we're done.
696 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
699 * If the block isn't the last one in the directory, we're done.
701 if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
704 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
706 * This can't really happen unless there's kernel corruption.
710 if (db == args->geo->datablk)
715 * Set the size to the new last block.
717 dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
718 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
722 /* Returns true if the directory entry name is valid. */
729 * MAXNAMELEN includes the trailing null, but (name/length) leave it
730 * out, so use >= for the length check.
732 if (length >= MAXNAMELEN)
735 /* There shouldn't be any slashes or nulls here */
736 return !memchr(name, '/', length) && !memchr(name, 0, length);
741 struct xfs_mount *mp,
742 const struct xfs_name *name)
744 if (unlikely(xfs_has_asciici(mp)))
745 return xfs_ascii_ci_hashname(name);
746 return xfs_da_hashname(name->name, name->len);
751 struct xfs_da_args *args,
752 const unsigned char *name,
755 if (unlikely(xfs_has_asciici(args->dp->i_mount)))
756 return xfs_ascii_ci_compname(args, name, len);
757 return xfs_da_compname(args, name, len);