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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
34 #include "xfs_dir2_format.h"
36 #include "xfs_dir2_priv.h"
37 #include "xfs_error.h"
38 #include "xfs_trace.h"
40 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2};
43 * ASCII case-insensitive (ie. A-Z) support for directories that was
47 xfs_ascii_ci_hashname(
48 struct xfs_name *name)
53 for (i = 0, hash = 0; i < name->len; i++)
54 hash = tolower(name->name[i]) ^ rol32(hash, 7);
60 xfs_ascii_ci_compname(
61 struct xfs_da_args *args,
62 const unsigned char *name,
65 enum xfs_dacmp result;
68 if (args->namelen != len)
69 return XFS_CMP_DIFFERENT;
71 result = XFS_CMP_EXACT;
72 for (i = 0; i < len; i++) {
73 if (args->name[i] == name[i])
75 if (tolower(args->name[i]) != tolower(name[i]))
76 return XFS_CMP_DIFFERENT;
77 result = XFS_CMP_CASE;
83 static struct xfs_nameops xfs_ascii_ci_nameops = {
84 .hashname = xfs_ascii_ci_hashname,
85 .compname = xfs_ascii_ci_compname,
92 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
93 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
95 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
96 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
97 mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
98 mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
99 mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
100 mp->m_attr_node_ents =
101 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
102 (uint)sizeof(xfs_da_node_entry_t);
103 mp->m_dir_node_ents =
104 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
105 (uint)sizeof(xfs_da_node_entry_t);
106 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
107 if (xfs_sb_version_hasasciici(&mp->m_sb))
108 mp->m_dirnameops = &xfs_ascii_ci_nameops;
110 mp->m_dirnameops = &xfs_default_nameops;
114 * Return 1 if directory contains only "." and "..".
120 xfs_dir2_sf_hdr_t *sfp;
122 ASSERT(S_ISDIR(dp->i_d.di_mode));
123 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
125 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
127 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
132 * Validate a given inode number.
135 xfs_dir_ino_validate(
139 xfs_agblock_t agblkno;
145 agno = XFS_INO_TO_AGNO(mp, ino);
146 agblkno = XFS_INO_TO_AGBNO(mp, ino);
147 ioff = XFS_INO_TO_OFFSET(mp, ino);
148 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
150 agno < mp->m_sb.sb_agcount &&
151 agblkno < mp->m_sb.sb_agblocks &&
153 ioff < (1 << mp->m_sb.sb_inopblog) &&
154 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
155 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
156 XFS_RANDOM_DIR_INO_VALIDATE))) {
157 xfs_warn(mp, "Invalid inode number 0x%Lx",
158 (unsigned long long) ino);
159 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
160 return XFS_ERROR(EFSCORRUPTED);
166 * Initialize a directory with its "." and ".." entries.
177 memset((char *)&args, 0, sizeof(args));
180 ASSERT(S_ISDIR(dp->i_d.di_mode));
181 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
183 return xfs_dir2_sf_create(&args, pdp->i_ino);
187 Enter a name in a directory.
193 struct xfs_name *name,
194 xfs_ino_t inum, /* new entry inode number */
195 xfs_fsblock_t *first, /* bmap's firstblock */
196 xfs_bmap_free_t *flist, /* bmap's freeblock list */
197 xfs_extlen_t total) /* bmap's total block count */
201 int v; /* type-checking value */
203 ASSERT(S_ISDIR(dp->i_d.di_mode));
204 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
206 XFS_STATS_INC(xs_dir_create);
208 memset(&args, 0, sizeof(xfs_da_args_t));
209 args.name = name->name;
210 args.namelen = name->len;
211 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
214 args.firstblock = first;
217 args.whichfork = XFS_DATA_FORK;
219 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
221 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
222 rval = xfs_dir2_sf_addname(&args);
223 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
226 rval = xfs_dir2_block_addname(&args);
227 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
230 rval = xfs_dir2_leaf_addname(&args);
232 rval = xfs_dir2_node_addname(&args);
237 * If doing a CI lookup and case-insensitive match, dup actual name into
238 * args.value. Return EEXIST for success (ie. name found) or an error.
241 xfs_dir_cilookup_result(
242 struct xfs_da_args *args,
243 const unsigned char *name,
246 if (args->cmpresult == XFS_CMP_DIFFERENT)
248 if (args->cmpresult != XFS_CMP_CASE ||
249 !(args->op_flags & XFS_DA_OP_CILOOKUP))
252 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
256 memcpy(args->value, name, len);
257 args->valuelen = len;
262 * Lookup a name in a directory, give back the inode number.
263 * If ci_name is not NULL, returns the actual name in ci_name if it differs
264 * to name, or ci_name->name is set to NULL for an exact match.
271 struct xfs_name *name,
272 xfs_ino_t *inum, /* out: inode number */
273 struct xfs_name *ci_name) /* out: actual name if CI match */
277 int v; /* type-checking value */
279 ASSERT(S_ISDIR(dp->i_d.di_mode));
280 XFS_STATS_INC(xs_dir_lookup);
282 memset(&args, 0, sizeof(xfs_da_args_t));
283 args.name = name->name;
284 args.namelen = name->len;
285 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
287 args.whichfork = XFS_DATA_FORK;
289 args.op_flags = XFS_DA_OP_OKNOENT;
291 args.op_flags |= XFS_DA_OP_CILOOKUP;
293 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
294 rval = xfs_dir2_sf_lookup(&args);
295 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
298 rval = xfs_dir2_block_lookup(&args);
299 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
302 rval = xfs_dir2_leaf_lookup(&args);
304 rval = xfs_dir2_node_lookup(&args);
308 *inum = args.inumber;
310 ci_name->name = args.value;
311 ci_name->len = args.valuelen;
318 * Remove an entry from a directory.
324 struct xfs_name *name,
326 xfs_fsblock_t *first, /* bmap's firstblock */
327 xfs_bmap_free_t *flist, /* bmap's freeblock list */
328 xfs_extlen_t total) /* bmap's total block count */
332 int v; /* type-checking value */
334 ASSERT(S_ISDIR(dp->i_d.di_mode));
335 XFS_STATS_INC(xs_dir_remove);
337 memset(&args, 0, sizeof(xfs_da_args_t));
338 args.name = name->name;
339 args.namelen = name->len;
340 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
343 args.firstblock = first;
346 args.whichfork = XFS_DATA_FORK;
349 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
350 rval = xfs_dir2_sf_removename(&args);
351 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
354 rval = xfs_dir2_block_removename(&args);
355 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
358 rval = xfs_dir2_leaf_removename(&args);
360 rval = xfs_dir2_node_removename(&args);
365 * Replace the inode number of a directory entry.
371 struct xfs_name *name, /* name of entry to replace */
372 xfs_ino_t inum, /* new inode number */
373 xfs_fsblock_t *first, /* bmap's firstblock */
374 xfs_bmap_free_t *flist, /* bmap's freeblock list */
375 xfs_extlen_t total) /* bmap's total block count */
379 int v; /* type-checking value */
381 ASSERT(S_ISDIR(dp->i_d.di_mode));
383 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
386 memset(&args, 0, sizeof(xfs_da_args_t));
387 args.name = name->name;
388 args.namelen = name->len;
389 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
392 args.firstblock = first;
395 args.whichfork = XFS_DATA_FORK;
398 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
399 rval = xfs_dir2_sf_replace(&args);
400 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
403 rval = xfs_dir2_block_replace(&args);
404 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
407 rval = xfs_dir2_leaf_replace(&args);
409 rval = xfs_dir2_node_replace(&args);
414 * See if this entry can be added to the directory without allocating space.
415 * First checks that the caller couldn't reserve enough space (resblks = 0).
421 struct xfs_name *name, /* name of entry to add */
426 int v; /* type-checking value */
431 ASSERT(S_ISDIR(dp->i_d.di_mode));
433 memset(&args, 0, sizeof(xfs_da_args_t));
434 args.name = name->name;
435 args.namelen = name->len;
436 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
438 args.whichfork = XFS_DATA_FORK;
440 args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
443 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
444 rval = xfs_dir2_sf_addname(&args);
445 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
448 rval = xfs_dir2_block_addname(&args);
449 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
452 rval = xfs_dir2_leaf_addname(&args);
454 rval = xfs_dir2_node_addname(&args);
463 * Add a block to the directory.
465 * This routine is for data and free blocks, not leaf/node blocks which are
466 * handled by xfs_da_grow_inode.
470 struct xfs_da_args *args,
471 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
472 xfs_dir2_db_t *dbp) /* out: block number added */
474 struct xfs_inode *dp = args->dp;
475 struct xfs_mount *mp = dp->i_mount;
476 xfs_fileoff_t bno; /* directory offset of new block */
477 int count; /* count of filesystem blocks */
480 trace_xfs_dir2_grow_inode(args, space);
483 * Set lowest possible block in the space requested.
485 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
486 count = mp->m_dirblkfsbs;
488 error = xfs_da_grow_inode_int(args, &bno, count);
492 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
495 * Update file's size if this is the data space and it grew.
497 if (space == XFS_DIR2_DATA_SPACE) {
498 xfs_fsize_t size; /* directory file (data) size */
500 size = XFS_FSB_TO_B(mp, bno + count);
501 if (size > dp->i_d.di_size) {
502 dp->i_d.di_size = size;
503 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
510 * See if the directory is a single-block form directory.
516 int *vp) /* out: 1 is block, 0 is not block */
518 xfs_fileoff_t last; /* last file offset */
523 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
525 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
526 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
532 * See if the directory is a single-leaf form directory.
538 int *vp) /* out: 1 is leaf, 0 is not leaf */
540 xfs_fileoff_t last; /* last file offset */
545 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
547 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
552 * Remove the given block from the directory.
553 * This routine is used for data and free blocks, leaf/node are done
554 * by xfs_da_shrink_inode.
557 xfs_dir2_shrink_inode(
562 xfs_fileoff_t bno; /* directory file offset */
563 xfs_dablk_t da; /* directory file offset */
564 int done; /* bunmap is finished */
570 trace_xfs_dir2_shrink_inode(args, db);
575 da = xfs_dir2_db_to_da(mp, db);
577 * Unmap the fsblock(s).
579 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
580 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
583 * ENOSPC actually can happen if we're in a removename with
584 * no space reservation, and the resulting block removal
585 * would cause a bmap btree split or conversion from extents
586 * to btree. This can only happen for un-fragmented
587 * directory blocks, since you need to be punching out
588 * the middle of an extent.
589 * In this case we need to leave the block in the file,
591 * So the block has to be in a consistent empty state
592 * and appropriately logged.
593 * We don't free up the buffer, the caller can tell it
594 * hasn't happened since it got an error back.
600 * Invalidate the buffer from the transaction.
602 xfs_trans_binval(tp, bp);
604 * If it's not a data block, we're done.
606 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
609 * If the block isn't the last one in the directory, we're done.
611 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
614 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
616 * This can't really happen unless there's kernel corruption.
620 if (db == mp->m_dirdatablk)
625 * Set the size to the new last block.
627 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
628 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);