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"
22 const struct xfs_name xfs_name_dotdot = {
23 .name = (const unsigned char *)"..",
25 .type = 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 const struct xfs_name *name)
66 for (i = 0, hash = 0; i < name->len; i++)
67 hash = xfs_ascii_ci_xfrm(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 (xfs_ascii_ci_xfrm(args->name[i]) !=
89 xfs_ascii_ci_xfrm(name[i]))
90 return XFS_CMP_DIFFERENT;
91 result = XFS_CMP_CASE;
101 struct xfs_da_geometry *dageo;
104 ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
105 ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
107 mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
109 mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
111 if (!mp->m_dir_geo || !mp->m_attr_geo) {
112 kmem_free(mp->m_dir_geo);
113 kmem_free(mp->m_attr_geo);
117 /* set up directory geometry */
118 dageo = mp->m_dir_geo;
119 dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
120 dageo->fsblog = mp->m_sb.sb_blocklog;
121 dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
122 dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
123 if (xfs_has_crc(mp)) {
124 dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr);
125 dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr);
126 dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr);
127 dageo->data_entry_offset =
128 sizeof(struct xfs_dir3_data_hdr);
130 dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr);
131 dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr);
132 dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr);
133 dageo->data_entry_offset =
134 sizeof(struct xfs_dir2_data_hdr);
136 dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) /
137 sizeof(struct xfs_dir2_leaf_entry);
138 dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) /
139 sizeof(xfs_dir2_data_off_t);
141 dageo->data_first_offset = dageo->data_entry_offset +
142 xfs_dir2_data_entsize(mp, 1) +
143 xfs_dir2_data_entsize(mp, 2);
146 * Now we've set up the block conversion variables, we can calculate the
147 * segment block constants using the geometry structure.
149 dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
150 dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
151 dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
152 dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
153 (uint)sizeof(xfs_da_node_entry_t);
154 dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >>
155 mp->m_sb.sb_blocklog;
156 dageo->magicpct = (dageo->blksize * 37) / 100;
158 /* set up attribute geometry - single fsb only */
159 dageo = mp->m_attr_geo;
160 dageo->blklog = mp->m_sb.sb_blocklog;
161 dageo->fsblog = mp->m_sb.sb_blocklog;
162 dageo->blksize = 1 << dageo->blklog;
164 dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size;
165 dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
166 (uint)sizeof(xfs_da_node_entry_t);
168 if (xfs_has_large_extent_counts(mp))
169 dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
171 dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
173 dageo->magicpct = (dageo->blksize * 37) / 100;
179 struct xfs_mount *mp)
181 kmem_free(mp->m_dir_geo);
182 kmem_free(mp->m_attr_geo);
186 * Return 1 if directory contains only "." and "..".
192 xfs_dir2_sf_hdr_t *sfp;
194 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
195 if (dp->i_disk_size == 0) /* might happen during shutdown. */
197 if (dp->i_disk_size > xfs_inode_data_fork_size(dp))
199 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
204 * Validate a given inode number.
207 xfs_dir_ino_validate(
211 bool ino_ok = xfs_verify_dir_ino(mp, ino);
213 if (XFS_IS_CORRUPT(mp, !ino_ok) ||
214 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DIR_INO_VALIDATE)) {
215 xfs_warn(mp, "Invalid inode number 0x%Lx",
216 (unsigned long long) ino);
217 return -EFSCORRUPTED;
223 * Initialize a directory with its "." and ".." entries.
231 struct xfs_da_args *args;
234 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
235 error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
239 args = kmem_zalloc(sizeof(*args), KM_NOFS);
243 args->geo = dp->i_mount->m_dir_geo;
246 error = xfs_dir2_sf_create(args, pdp->i_ino);
252 * Enter a name in a directory, or check for available space.
253 * If inum is 0, only the available space test is performed.
257 struct xfs_trans *tp,
258 struct xfs_inode *dp,
259 const struct xfs_name *name,
260 xfs_ino_t inum, /* new entry inode number */
261 xfs_extlen_t total) /* bmap's total block count */
263 struct xfs_da_args *args;
267 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
270 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
273 XFS_STATS_INC(dp->i_mount, xs_dir_create);
276 args = kmem_zalloc(sizeof(*args), KM_NOFS);
280 args->geo = dp->i_mount->m_dir_geo;
281 args->name = name->name;
282 args->namelen = name->len;
283 args->filetype = name->type;
284 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
285 args->inumber = inum;
288 args->whichfork = XFS_DATA_FORK;
290 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
292 args->op_flags |= XFS_DA_OP_JUSTCHECK;
294 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
295 rval = xfs_dir2_sf_addname(args);
299 rval = xfs_dir2_isblock(args, &v);
303 rval = xfs_dir2_block_addname(args);
307 rval = xfs_dir2_isleaf(args, &v);
311 rval = xfs_dir2_leaf_addname(args);
313 rval = xfs_dir2_node_addname(args);
321 * If doing a CI lookup and case-insensitive match, dup actual name into
322 * args.value. Return EEXIST for success (ie. name found) or an error.
325 xfs_dir_cilookup_result(
326 struct xfs_da_args *args,
327 const unsigned char *name,
330 if (args->cmpresult == XFS_CMP_DIFFERENT)
332 if (args->cmpresult != XFS_CMP_CASE ||
333 !(args->op_flags & XFS_DA_OP_CILOOKUP))
336 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
340 memcpy(args->value, name, len);
341 args->valuelen = len;
346 * Lookup a name in a directory, give back the inode number.
347 * If ci_name is not NULL, returns the actual name in ci_name if it differs
348 * to name, or ci_name->name is set to NULL for an exact match.
353 struct xfs_trans *tp,
354 struct xfs_inode *dp,
355 const struct xfs_name *name,
356 xfs_ino_t *inum, /* out: inode number */
357 struct xfs_name *ci_name) /* out: actual name if CI match */
359 struct xfs_da_args *args;
364 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
365 XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
368 * We need to use KM_NOFS here so that lockdep will not throw false
369 * positive deadlock warnings on a non-transactional lookup path. It is
370 * safe to recurse into inode recalim in that case, but lockdep can't
371 * easily be taught about it. Hence KM_NOFS avoids having to add more
372 * lockdep Doing this avoids having to add a bunch of lockdep class
373 * annotations into the reclaim path for the ilock.
375 args = kmem_zalloc(sizeof(*args), KM_NOFS);
376 args->geo = dp->i_mount->m_dir_geo;
377 args->name = name->name;
378 args->namelen = name->len;
379 args->filetype = name->type;
380 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
382 args->whichfork = XFS_DATA_FORK;
384 args->op_flags = XFS_DA_OP_OKNOENT;
386 args->op_flags |= XFS_DA_OP_CILOOKUP;
388 lock_mode = xfs_ilock_data_map_shared(dp);
389 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
390 rval = xfs_dir2_sf_lookup(args);
394 rval = xfs_dir2_isblock(args, &v);
398 rval = xfs_dir2_block_lookup(args);
402 rval = xfs_dir2_isleaf(args, &v);
406 rval = xfs_dir2_leaf_lookup(args);
408 rval = xfs_dir2_node_lookup(args);
414 *inum = args->inumber;
416 ci_name->name = args->value;
417 ci_name->len = args->valuelen;
421 xfs_iunlock(dp, lock_mode);
427 * Remove an entry from a directory.
431 struct xfs_trans *tp,
432 struct xfs_inode *dp,
433 struct xfs_name *name,
435 xfs_extlen_t total) /* bmap's total block count */
437 struct xfs_da_args *args;
441 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
442 XFS_STATS_INC(dp->i_mount, xs_dir_remove);
444 args = kmem_zalloc(sizeof(*args), KM_NOFS);
448 args->geo = dp->i_mount->m_dir_geo;
449 args->name = name->name;
450 args->namelen = name->len;
451 args->filetype = name->type;
452 args->hashval = xfs_dir2_hashname(dp->i_mount, name);
456 args->whichfork = XFS_DATA_FORK;
459 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
460 rval = xfs_dir2_sf_removename(args);
464 rval = xfs_dir2_isblock(args, &v);
468 rval = xfs_dir2_block_removename(args);
472 rval = xfs_dir2_isleaf(args, &v);
476 rval = xfs_dir2_leaf_removename(args);
478 rval = xfs_dir2_node_removename(args);
485 * Replace the inode number of a directory entry.
489 struct xfs_trans *tp,
490 struct xfs_inode *dp,
491 const struct xfs_name *name, /* name of entry to replace */
492 xfs_ino_t inum, /* new inode number */
493 xfs_extlen_t total) /* bmap's total block count */
495 struct xfs_da_args *args;
499 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
501 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
505 args = kmem_zalloc(sizeof(*args), KM_NOFS);
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);
514 args->inumber = inum;
517 args->whichfork = XFS_DATA_FORK;
520 if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
521 rval = xfs_dir2_sf_replace(args);
525 rval = xfs_dir2_isblock(args, &v);
529 rval = xfs_dir2_block_replace(args);
533 rval = xfs_dir2_isleaf(args, &v);
537 rval = xfs_dir2_leaf_replace(args);
539 rval = xfs_dir2_node_replace(args);
546 * See if this entry can be added to the directory without allocating space.
552 struct xfs_name *name) /* name of entry to add */
554 return xfs_dir_createname(tp, dp, name, 0, 0);
562 * Add a block to the directory.
564 * This routine is for data and free blocks, not leaf/node blocks which are
565 * handled by xfs_da_grow_inode.
569 struct xfs_da_args *args,
570 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
571 xfs_dir2_db_t *dbp) /* out: block number added */
573 struct xfs_inode *dp = args->dp;
574 struct xfs_mount *mp = dp->i_mount;
575 xfs_fileoff_t bno; /* directory offset of new block */
576 int count; /* count of filesystem blocks */
579 trace_xfs_dir2_grow_inode(args, space);
582 * Set lowest possible block in the space requested.
584 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
585 count = args->geo->fsbcount;
587 error = xfs_da_grow_inode_int(args, &bno, count);
591 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
594 * Update file's size if this is the data space and it grew.
596 if (space == XFS_DIR2_DATA_SPACE) {
597 xfs_fsize_t size; /* directory file (data) size */
599 size = XFS_FSB_TO_B(mp, bno + count);
600 if (size > dp->i_disk_size) {
601 dp->i_disk_size = size;
602 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
609 * See if the directory is a single-block form directory.
613 struct xfs_da_args *args,
616 struct xfs_mount *mp = args->dp->i_mount;
620 error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
625 if (XFS_FSB_TO_B(mp, eof) != args->geo->blksize)
629 if (XFS_IS_CORRUPT(mp, args->dp->i_disk_size != args->geo->blksize))
630 return -EFSCORRUPTED;
635 * See if the directory is a single-leaf form directory.
639 struct xfs_da_args *args,
645 error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
650 if (eof != args->geo->leafblk + args->geo->fsbcount)
658 * Remove the given block from the directory.
659 * This routine is used for data and free blocks, leaf/node are done
660 * by xfs_da_shrink_inode.
663 xfs_dir2_shrink_inode(
664 struct xfs_da_args *args,
668 xfs_fileoff_t bno; /* directory file offset */
669 xfs_dablk_t da; /* directory file offset */
670 int done; /* bunmap is finished */
671 struct xfs_inode *dp;
673 struct xfs_mount *mp;
674 struct xfs_trans *tp;
676 trace_xfs_dir2_shrink_inode(args, db);
681 da = xfs_dir2_db_to_da(args->geo, db);
683 /* Unmap the fsblock(s). */
684 error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
687 * ENOSPC actually can happen if we're in a removename with no
688 * space reservation, and the resulting block removal would
689 * cause a bmap btree split or conversion from extents to btree.
690 * This can only happen for un-fragmented directory blocks,
691 * since you need to be punching out the middle of an extent.
692 * In this case we need to leave the block in the file, and not
693 * binval it. So the block has to be in a consistent empty
694 * state and appropriately logged. We don't free up the buffer,
695 * the caller can tell it hasn't happened since it got an error
702 * Invalidate the buffer from the transaction.
704 xfs_trans_binval(tp, bp);
706 * If it's not a data block, we're done.
708 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
711 * If the block isn't the last one in the directory, we're done.
713 if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
716 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
718 * This can't really happen unless there's kernel corruption.
722 if (db == args->geo->datablk)
727 * Set the size to the new last block.
729 dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
730 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
734 /* Returns true if the directory entry name is valid. */
741 * MAXNAMELEN includes the trailing null, but (name/length) leave it
742 * out, so use >= for the length check.
744 if (length >= MAXNAMELEN)
747 /* There shouldn't be any slashes or nulls here */
748 return !memchr(name, '/', length) && !memchr(name, 0, length);
753 struct xfs_mount *mp,
754 const struct xfs_name *name)
756 if (unlikely(xfs_has_asciici(mp)))
757 return xfs_ascii_ci_hashname(name);
758 return xfs_da_hashname(name->name, name->len);
763 struct xfs_da_args *args,
764 const unsigned char *name,
767 if (unlikely(xfs_has_asciici(args->dp->i_mount)))
768 return xfs_ascii_ci_compname(args, name, len);
769 return xfs_da_compname(args, name, len);