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 error = xfs_dir2_sf_create(args, pdp->i_ino);
259 * Enter a name in a directory, or check for available space.
260 * If inum is 0, only the available space test is performed.
264 struct xfs_trans *tp,
265 struct xfs_inode *dp,
266 const struct xfs_name *name,
267 xfs_ino_t inum, /* new entry inode number */
268 xfs_extlen_t total) /* bmap's total block count */
270 struct xfs_da_args *args;
274 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
277 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
280 XFS_STATS_INC(dp->i_mount, xs_dir_create);
283 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
287 args->geo = dp->i_mount->m_dir_geo;
288 args->name = name->name;
289 args->namelen = name->len;
290 args->filetype = name->type;
291 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
292 args->inumber = inum;
295 args->whichfork = XFS_DATA_FORK;
297 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
299 args->op_flags |= XFS_DA_OP_JUSTCHECK;
301 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
302 rval = xfs_dir2_sf_addname(args);
306 rval = xfs_dir2_isblock(args, &v);
310 rval = xfs_dir2_block_addname(args);
314 rval = xfs_dir2_isleaf(args, &v);
318 rval = xfs_dir2_leaf_addname(args);
320 rval = xfs_dir2_node_addname(args);
328 * If doing a CI lookup and case-insensitive match, dup actual name into
329 * args.value. Return EEXIST for success (ie. name found) or an error.
332 xfs_dir_cilookup_result(
333 struct xfs_da_args *args,
334 const unsigned char *name,
337 if (args->cmpresult == XFS_CMP_DIFFERENT)
339 if (args->cmpresult != XFS_CMP_CASE ||
340 !(args->op_flags & XFS_DA_OP_CILOOKUP))
343 args->value = kmalloc(len,
344 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
348 memcpy(args->value, name, len);
349 args->valuelen = len;
354 * Lookup a name in a directory, give back the inode number.
355 * If ci_name is not NULL, returns the actual name in ci_name if it differs
356 * to name, or ci_name->name is set to NULL for an exact match.
361 struct xfs_trans *tp,
362 struct xfs_inode *dp,
363 const struct xfs_name *name,
364 xfs_ino_t *inum, /* out: inode number */
365 struct xfs_name *ci_name) /* out: actual name if CI match */
367 struct xfs_da_args *args;
372 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
373 XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
375 args = kzalloc(sizeof(*args),
376 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
377 args->geo = dp->i_mount->m_dir_geo;
378 args->name = name->name;
379 args->namelen = name->len;
380 args->filetype = name->type;
381 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
383 args->whichfork = XFS_DATA_FORK;
385 args->op_flags = XFS_DA_OP_OKNOENT;
387 args->op_flags |= XFS_DA_OP_CILOOKUP;
389 lock_mode = xfs_ilock_data_map_shared(dp);
390 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
391 rval = xfs_dir2_sf_lookup(args);
395 rval = xfs_dir2_isblock(args, &v);
399 rval = xfs_dir2_block_lookup(args);
403 rval = xfs_dir2_isleaf(args, &v);
407 rval = xfs_dir2_leaf_lookup(args);
409 rval = xfs_dir2_node_lookup(args);
415 *inum = args->inumber;
417 ci_name->name = args->value;
418 ci_name->len = args->valuelen;
422 xfs_iunlock(dp, lock_mode);
428 * Remove an entry from a directory.
432 struct xfs_trans *tp,
433 struct xfs_inode *dp,
434 struct xfs_name *name,
436 xfs_extlen_t total) /* bmap's total block count */
438 struct xfs_da_args *args;
442 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
443 XFS_STATS_INC(dp->i_mount, xs_dir_remove);
445 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
449 args->geo = dp->i_mount->m_dir_geo;
450 args->name = name->name;
451 args->namelen = name->len;
452 args->filetype = name->type;
453 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
457 args->whichfork = XFS_DATA_FORK;
460 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
461 rval = xfs_dir2_sf_removename(args);
465 rval = xfs_dir2_isblock(args, &v);
469 rval = xfs_dir2_block_removename(args);
473 rval = xfs_dir2_isleaf(args, &v);
477 rval = xfs_dir2_leaf_removename(args);
479 rval = xfs_dir2_node_removename(args);
486 * Replace the inode number of a directory entry.
490 struct xfs_trans *tp,
491 struct xfs_inode *dp,
492 const struct xfs_name *name, /* name of entry to replace */
493 xfs_ino_t inum, /* new inode number */
494 xfs_extlen_t total) /* bmap's total block count */
496 struct xfs_da_args *args;
500 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
502 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
506 args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
510 args->geo = dp->i_mount->m_dir_geo;
511 args->name = name->name;
512 args->namelen = name->len;
513 args->filetype = name->type;
514 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
515 args->inumber = inum;
518 args->whichfork = XFS_DATA_FORK;
521 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
522 rval = xfs_dir2_sf_replace(args);
526 rval = xfs_dir2_isblock(args, &v);
530 rval = xfs_dir2_block_replace(args);
534 rval = xfs_dir2_isleaf(args, &v);
538 rval = xfs_dir2_leaf_replace(args);
540 rval = xfs_dir2_node_replace(args);
547 * See if this entry can be added to the directory without allocating space.
553 struct xfs_name *name) /* name of entry to add */
555 return xfs_dir_createname(tp, dp, name, 0, 0);
563 * Add a block to the directory.
565 * This routine is for data and free blocks, not leaf/node blocks which are
566 * handled by xfs_da_grow_inode.
570 struct xfs_da_args *args,
571 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
572 xfs_dir2_db_t *dbp) /* out: block number added */
574 struct xfs_inode *dp = args->dp;
575 struct xfs_mount *mp = dp->i_mount;
576 xfs_fileoff_t bno; /* directory offset of new block */
577 int count; /* count of filesystem blocks */
580 trace_xfs_dir2_grow_inode(args, space);
583 * Set lowest possible block in the space requested.
585 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
586 count = args->geo->fsbcount;
588 error = xfs_da_grow_inode_int(args, &bno, count);
592 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
595 * Update file's size if this is the data space and it grew.
597 if (space == XFS_DIR2_DATA_SPACE) {
598 xfs_fsize_t size; /* directory file (data) size */
600 size = XFS_FSB_TO_B(mp, bno + count);
601 if (size > dp->i_disk_size) {
602 dp->i_disk_size = size;
603 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
610 * See if the directory is a single-block form directory.
614 struct xfs_da_args *args,
617 struct xfs_mount *mp = args->dp->i_mount;
621 error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
626 if (XFS_FSB_TO_B(mp, eof) != args->geo->blksize)
630 if (XFS_IS_CORRUPT(mp, args->dp->i_disk_size != args->geo->blksize)) {
631 xfs_da_mark_sick(args);
632 return -EFSCORRUPTED;
638 * See if the directory is a single-leaf form directory.
642 struct xfs_da_args *args,
648 error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
653 if (eof != args->geo->leafblk + args->geo->fsbcount)
661 * Remove the given block from the directory.
662 * This routine is used for data and free blocks, leaf/node are done
663 * by xfs_da_shrink_inode.
666 xfs_dir2_shrink_inode(
667 struct xfs_da_args *args,
671 xfs_fileoff_t bno; /* directory file offset */
672 xfs_dablk_t da; /* directory file offset */
673 int done; /* bunmap is finished */
674 struct xfs_inode *dp;
676 struct xfs_mount *mp;
677 struct xfs_trans *tp;
679 trace_xfs_dir2_shrink_inode(args, db);
684 da = xfs_dir2_db_to_da(args->geo, db);
686 /* Unmap the fsblock(s). */
687 error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
690 * ENOSPC actually can happen if we're in a removename with no
691 * space reservation, and the resulting block removal would
692 * cause a bmap btree split or conversion from extents to btree.
693 * This can only happen for un-fragmented directory blocks,
694 * since you need to be punching out the middle of an extent.
695 * In this case we need to leave the block in the file, and not
696 * binval it. So the block has to be in a consistent empty
697 * state and appropriately logged. We don't free up the buffer,
698 * the caller can tell it hasn't happened since it got an error
705 * Invalidate the buffer from the transaction.
707 xfs_trans_binval(tp, bp);
709 * If it's not a data block, we're done.
711 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
714 * If the block isn't the last one in the directory, we're done.
716 if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
719 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
721 * This can't really happen unless there's kernel corruption.
725 if (db == args->geo->datablk)
730 * Set the size to the new last block.
732 dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
733 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
737 /* Returns true if the directory entry name is valid. */
744 * MAXNAMELEN includes the trailing null, but (name/length) leave it
745 * out, so use >= for the length check.
747 if (length >= MAXNAMELEN)
750 /* There shouldn't be any slashes or nulls here */
751 return !memchr(name, '/', length) && !memchr(name, 0, length);
756 struct xfs_mount *mp,
757 const struct xfs_name *name)
759 if (unlikely(xfs_has_asciici(mp)))
760 return xfs_ascii_ci_hashname(name);
761 return xfs_da_hashname(name->name, name->len);
766 struct xfs_da_args *args,
767 const unsigned char *name,
770 if (unlikely(xfs_has_asciici(args->dp->i_mount)))
771 return xfs_ascii_ci_compname(args, name, len);
772 return xfs_da_compname(args, name, len);