2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
36 #include "xfs_dir2_format.h"
37 #include "xfs_dir2_priv.h"
38 #include "xfs_error.h"
39 #include "xfs_vnodeops.h"
40 #include "xfs_trace.h"
42 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2};
45 * ASCII case-insensitive (ie. A-Z) support for directories that was
49 xfs_ascii_ci_hashname(
50 struct xfs_name *name)
55 for (i = 0, hash = 0; i < name->len; i++)
56 hash = tolower(name->name[i]) ^ rol32(hash, 7);
62 xfs_ascii_ci_compname(
63 struct xfs_da_args *args,
64 const unsigned char *name,
67 enum xfs_dacmp result;
70 if (args->namelen != len)
71 return XFS_CMP_DIFFERENT;
73 result = XFS_CMP_EXACT;
74 for (i = 0; i < len; i++) {
75 if (args->name[i] == name[i])
77 if (tolower(args->name[i]) != tolower(name[i]))
78 return XFS_CMP_DIFFERENT;
79 result = XFS_CMP_CASE;
85 static struct xfs_nameops xfs_ascii_ci_nameops = {
86 .hashname = xfs_ascii_ci_hashname,
87 .compname = xfs_ascii_ci_compname,
94 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
95 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
97 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
98 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
99 mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
100 mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
101 mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
102 mp->m_attr_node_ents =
103 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
104 (uint)sizeof(xfs_da_node_entry_t);
105 mp->m_dir_node_ents =
106 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
107 (uint)sizeof(xfs_da_node_entry_t);
108 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
109 if (xfs_sb_version_hasasciici(&mp->m_sb))
110 mp->m_dirnameops = &xfs_ascii_ci_nameops;
112 mp->m_dirnameops = &xfs_default_nameops;
116 * Return 1 if directory contains only "." and "..".
122 xfs_dir2_sf_hdr_t *sfp;
124 ASSERT(S_ISDIR(dp->i_d.di_mode));
125 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
127 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
129 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
134 * Validate a given inode number.
137 xfs_dir_ino_validate(
141 xfs_agblock_t agblkno;
147 agno = XFS_INO_TO_AGNO(mp, ino);
148 agblkno = XFS_INO_TO_AGBNO(mp, ino);
149 ioff = XFS_INO_TO_OFFSET(mp, ino);
150 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
152 agno < mp->m_sb.sb_agcount &&
153 agblkno < mp->m_sb.sb_agblocks &&
155 ioff < (1 << mp->m_sb.sb_inopblog) &&
156 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
157 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
158 XFS_RANDOM_DIR_INO_VALIDATE))) {
159 xfs_warn(mp, "Invalid inode number 0x%Lx",
160 (unsigned long long) ino);
161 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
162 return XFS_ERROR(EFSCORRUPTED);
168 * Initialize a directory with its "." and ".." entries.
179 memset((char *)&args, 0, sizeof(args));
182 ASSERT(S_ISDIR(dp->i_d.di_mode));
183 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
185 return xfs_dir2_sf_create(&args, pdp->i_ino);
189 Enter a name in a directory.
195 struct xfs_name *name,
196 xfs_ino_t inum, /* new entry inode number */
197 xfs_fsblock_t *first, /* bmap's firstblock */
198 xfs_bmap_free_t *flist, /* bmap's freeblock list */
199 xfs_extlen_t total) /* bmap's total block count */
203 int v; /* type-checking value */
205 ASSERT(S_ISDIR(dp->i_d.di_mode));
206 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
208 XFS_STATS_INC(xs_dir_create);
210 memset(&args, 0, sizeof(xfs_da_args_t));
211 args.name = name->name;
212 args.namelen = name->len;
213 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
216 args.firstblock = first;
219 args.whichfork = XFS_DATA_FORK;
221 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
223 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
224 rval = xfs_dir2_sf_addname(&args);
225 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
228 rval = xfs_dir2_block_addname(&args);
229 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
232 rval = xfs_dir2_leaf_addname(&args);
234 rval = xfs_dir2_node_addname(&args);
239 * If doing a CI lookup and case-insensitive match, dup actual name into
240 * args.value. Return EEXIST for success (ie. name found) or an error.
243 xfs_dir_cilookup_result(
244 struct xfs_da_args *args,
245 const unsigned char *name,
248 if (args->cmpresult == XFS_CMP_DIFFERENT)
250 if (args->cmpresult != XFS_CMP_CASE ||
251 !(args->op_flags & XFS_DA_OP_CILOOKUP))
254 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
258 memcpy(args->value, name, len);
259 args->valuelen = len;
264 * Lookup a name in a directory, give back the inode number.
265 * If ci_name is not NULL, returns the actual name in ci_name if it differs
266 * to name, or ci_name->name is set to NULL for an exact match.
273 struct xfs_name *name,
274 xfs_ino_t *inum, /* out: inode number */
275 struct xfs_name *ci_name) /* out: actual name if CI match */
279 int v; /* type-checking value */
281 ASSERT(S_ISDIR(dp->i_d.di_mode));
282 XFS_STATS_INC(xs_dir_lookup);
284 memset(&args, 0, sizeof(xfs_da_args_t));
285 args.name = name->name;
286 args.namelen = name->len;
287 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
289 args.whichfork = XFS_DATA_FORK;
291 args.op_flags = XFS_DA_OP_OKNOENT;
293 args.op_flags |= XFS_DA_OP_CILOOKUP;
295 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
296 rval = xfs_dir2_sf_lookup(&args);
297 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
300 rval = xfs_dir2_block_lookup(&args);
301 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
304 rval = xfs_dir2_leaf_lookup(&args);
306 rval = xfs_dir2_node_lookup(&args);
310 *inum = args.inumber;
312 ci_name->name = args.value;
313 ci_name->len = args.valuelen;
320 * Remove an entry from a directory.
326 struct xfs_name *name,
328 xfs_fsblock_t *first, /* bmap's firstblock */
329 xfs_bmap_free_t *flist, /* bmap's freeblock list */
330 xfs_extlen_t total) /* bmap's total block count */
334 int v; /* type-checking value */
336 ASSERT(S_ISDIR(dp->i_d.di_mode));
337 XFS_STATS_INC(xs_dir_remove);
339 memset(&args, 0, sizeof(xfs_da_args_t));
340 args.name = name->name;
341 args.namelen = name->len;
342 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
345 args.firstblock = first;
348 args.whichfork = XFS_DATA_FORK;
351 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
352 rval = xfs_dir2_sf_removename(&args);
353 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
356 rval = xfs_dir2_block_removename(&args);
357 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
360 rval = xfs_dir2_leaf_removename(&args);
362 rval = xfs_dir2_node_removename(&args);
377 int rval; /* return value */
378 int v; /* type-checking value */
380 trace_xfs_readdir(dp);
382 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
383 return XFS_ERROR(EIO);
385 ASSERT(S_ISDIR(dp->i_d.di_mode));
386 XFS_STATS_INC(xs_dir_getdents);
388 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
389 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
390 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
393 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
395 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
401 * Replace the inode number of a directory entry.
407 struct xfs_name *name, /* name of entry to replace */
408 xfs_ino_t inum, /* new inode number */
409 xfs_fsblock_t *first, /* bmap's firstblock */
410 xfs_bmap_free_t *flist, /* bmap's freeblock list */
411 xfs_extlen_t total) /* bmap's total block count */
415 int v; /* type-checking value */
417 ASSERT(S_ISDIR(dp->i_d.di_mode));
419 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
422 memset(&args, 0, sizeof(xfs_da_args_t));
423 args.name = name->name;
424 args.namelen = name->len;
425 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
428 args.firstblock = first;
431 args.whichfork = XFS_DATA_FORK;
434 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
435 rval = xfs_dir2_sf_replace(&args);
436 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
439 rval = xfs_dir2_block_replace(&args);
440 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
443 rval = xfs_dir2_leaf_replace(&args);
445 rval = xfs_dir2_node_replace(&args);
450 * See if this entry can be added to the directory without allocating space.
451 * First checks that the caller couldn't reserve enough space (resblks = 0).
457 struct xfs_name *name, /* name of entry to add */
462 int v; /* type-checking value */
467 ASSERT(S_ISDIR(dp->i_d.di_mode));
469 memset(&args, 0, sizeof(xfs_da_args_t));
470 args.name = name->name;
471 args.namelen = name->len;
472 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
474 args.whichfork = XFS_DATA_FORK;
476 args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
479 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
480 rval = xfs_dir2_sf_addname(&args);
481 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
484 rval = xfs_dir2_block_addname(&args);
485 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
488 rval = xfs_dir2_leaf_addname(&args);
490 rval = xfs_dir2_node_addname(&args);
499 * Add a block to the directory.
501 * This routine is for data and free blocks, not leaf/node blocks which are
502 * handled by xfs_da_grow_inode.
506 struct xfs_da_args *args,
507 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
508 xfs_dir2_db_t *dbp) /* out: block number added */
510 struct xfs_inode *dp = args->dp;
511 struct xfs_mount *mp = dp->i_mount;
512 xfs_fileoff_t bno; /* directory offset of new block */
513 int count; /* count of filesystem blocks */
516 trace_xfs_dir2_grow_inode(args, space);
519 * Set lowest possible block in the space requested.
521 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
522 count = mp->m_dirblkfsbs;
524 error = xfs_da_grow_inode_int(args, &bno, count);
528 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
531 * Update file's size if this is the data space and it grew.
533 if (space == XFS_DIR2_DATA_SPACE) {
534 xfs_fsize_t size; /* directory file (data) size */
536 size = XFS_FSB_TO_B(mp, bno + count);
537 if (size > dp->i_d.di_size) {
538 dp->i_d.di_size = size;
539 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
546 * See if the directory is a single-block form directory.
552 int *vp) /* out: 1 is block, 0 is not block */
554 xfs_fileoff_t last; /* last file offset */
559 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
561 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
562 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
568 * See if the directory is a single-leaf form directory.
574 int *vp) /* out: 1 is leaf, 0 is not leaf */
576 xfs_fileoff_t last; /* last file offset */
581 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
583 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
588 * Remove the given block from the directory.
589 * This routine is used for data and free blocks, leaf/node are done
590 * by xfs_da_shrink_inode.
593 xfs_dir2_shrink_inode(
598 xfs_fileoff_t bno; /* directory file offset */
599 xfs_dablk_t da; /* directory file offset */
600 int done; /* bunmap is finished */
606 trace_xfs_dir2_shrink_inode(args, db);
611 da = xfs_dir2_db_to_da(mp, db);
613 * Unmap the fsblock(s).
615 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
616 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
619 * ENOSPC actually can happen if we're in a removename with
620 * no space reservation, and the resulting block removal
621 * would cause a bmap btree split or conversion from extents
622 * to btree. This can only happen for un-fragmented
623 * directory blocks, since you need to be punching out
624 * the middle of an extent.
625 * In this case we need to leave the block in the file,
627 * So the block has to be in a consistent empty state
628 * and appropriately logged.
629 * We don't free up the buffer, the caller can tell it
630 * hasn't happened since it got an error back.
636 * Invalidate the buffer from the transaction.
638 xfs_da_binval(tp, bp);
640 * If it's not a data block, we're done.
642 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
645 * If the block isn't the last one in the directory, we're done.
647 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
650 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
652 * This can't really happen unless there's kernel corruption.
656 if (db == mp->m_dirdatablk)
661 * Set the size to the new last block.
663 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
664 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);