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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
37 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
41 * ASCII case-insensitive (ie. A-Z) support for directories that was
45 xfs_ascii_ci_hashname(
46 struct xfs_name *name)
51 for (i = 0, hash = 0; i < name->len; i++)
52 hash = tolower(name->name[i]) ^ rol32(hash, 7);
58 xfs_ascii_ci_compname(
59 struct xfs_da_args *args,
60 const unsigned char *name,
63 enum xfs_dacmp result;
66 if (args->namelen != len)
67 return XFS_CMP_DIFFERENT;
69 result = XFS_CMP_EXACT;
70 for (i = 0; i < len; i++) {
71 if (args->name[i] == name[i])
73 if (tolower(args->name[i]) != tolower(name[i]))
74 return XFS_CMP_DIFFERENT;
75 result = XFS_CMP_CASE;
81 static struct xfs_nameops xfs_ascii_ci_nameops = {
82 .hashname = xfs_ascii_ci_hashname,
83 .compname = xfs_ascii_ci_compname,
90 struct xfs_da_geometry *dageo;
94 ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
95 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
98 mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
99 mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
101 nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
102 mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
103 KM_SLEEP | KM_MAYFAIL);
104 mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
105 KM_SLEEP | KM_MAYFAIL);
106 if (!mp->m_dir_geo || !mp->m_attr_geo) {
107 kmem_free(mp->m_dir_geo);
108 kmem_free(mp->m_attr_geo);
112 /* set up directory geometry */
113 dageo = mp->m_dir_geo;
114 dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
115 dageo->fsblog = mp->m_sb.sb_blocklog;
116 dageo->blksize = 1 << dageo->blklog;
117 dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
120 * Now we've set up the block conversion variables, we can calculate the
121 * segment block constants using the geometry structure.
123 dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
124 dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
125 dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
126 dageo->node_ents = (dageo->blksize - nodehdr_size) /
127 (uint)sizeof(xfs_da_node_entry_t);
128 dageo->magicpct = (dageo->blksize * 37) / 100;
130 /* set up attribute geometry - single fsb only */
131 dageo = mp->m_attr_geo;
132 dageo->blklog = mp->m_sb.sb_blocklog;
133 dageo->fsblog = mp->m_sb.sb_blocklog;
134 dageo->blksize = 1 << dageo->blklog;
136 dageo->node_ents = (dageo->blksize - nodehdr_size) /
137 (uint)sizeof(xfs_da_node_entry_t);
138 dageo->magicpct = (dageo->blksize * 37) / 100;
140 if (xfs_sb_version_hasasciici(&mp->m_sb))
141 mp->m_dirnameops = &xfs_ascii_ci_nameops;
143 mp->m_dirnameops = &xfs_default_nameops;
150 struct xfs_mount *mp)
152 kmem_free(mp->m_dir_geo);
153 kmem_free(mp->m_attr_geo);
157 * Return 1 if directory contains only "." and "..".
163 xfs_dir2_sf_hdr_t *sfp;
165 ASSERT(S_ISDIR(dp->i_d.di_mode));
166 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
168 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
170 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
175 * Validate a given inode number.
178 xfs_dir_ino_validate(
182 xfs_agblock_t agblkno;
188 agno = XFS_INO_TO_AGNO(mp, ino);
189 agblkno = XFS_INO_TO_AGBNO(mp, ino);
190 ioff = XFS_INO_TO_OFFSET(mp, ino);
191 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
193 agno < mp->m_sb.sb_agcount &&
194 agblkno < mp->m_sb.sb_agblocks &&
196 ioff < (1 << mp->m_sb.sb_inopblog) &&
197 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
198 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
199 XFS_RANDOM_DIR_INO_VALIDATE))) {
200 xfs_warn(mp, "Invalid inode number 0x%Lx",
201 (unsigned long long) ino);
202 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
203 return -EFSCORRUPTED;
209 * Initialize a directory with its "." and ".." entries.
217 struct xfs_da_args *args;
220 ASSERT(S_ISDIR(dp->i_d.di_mode));
221 error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
225 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
229 args->geo = dp->i_mount->m_dir_geo;
232 error = xfs_dir2_sf_create(args, pdp->i_ino);
238 * Enter a name in a directory, or check for available space.
239 * If inum is 0, only the available space test is performed.
245 struct xfs_name *name,
246 xfs_ino_t inum, /* new entry inode number */
247 xfs_fsblock_t *first, /* bmap's firstblock */
248 xfs_bmap_free_t *flist, /* bmap's freeblock list */
249 xfs_extlen_t total) /* bmap's total block count */
251 struct xfs_da_args *args;
253 int v; /* type-checking value */
255 ASSERT(S_ISDIR(dp->i_d.di_mode));
257 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
260 XFS_STATS_INC(xs_dir_create);
263 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
267 args->geo = dp->i_mount->m_dir_geo;
268 args->name = name->name;
269 args->namelen = name->len;
270 args->filetype = name->type;
271 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
272 args->inumber = inum;
274 args->firstblock = first;
277 args->whichfork = XFS_DATA_FORK;
279 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
281 args->op_flags |= XFS_DA_OP_JUSTCHECK;
283 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
284 rval = xfs_dir2_sf_addname(args);
288 rval = xfs_dir2_isblock(args, &v);
292 rval = xfs_dir2_block_addname(args);
296 rval = xfs_dir2_isleaf(args, &v);
300 rval = xfs_dir2_leaf_addname(args);
302 rval = xfs_dir2_node_addname(args);
310 * If doing a CI lookup and case-insensitive match, dup actual name into
311 * args.value. Return EEXIST for success (ie. name found) or an error.
314 xfs_dir_cilookup_result(
315 struct xfs_da_args *args,
316 const unsigned char *name,
319 if (args->cmpresult == XFS_CMP_DIFFERENT)
321 if (args->cmpresult != XFS_CMP_CASE ||
322 !(args->op_flags & XFS_DA_OP_CILOOKUP))
325 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
329 memcpy(args->value, name, len);
330 args->valuelen = len;
335 * Lookup a name in a directory, give back the inode number.
336 * If ci_name is not NULL, returns the actual name in ci_name if it differs
337 * to name, or ci_name->name is set to NULL for an exact match.
344 struct xfs_name *name,
345 xfs_ino_t *inum, /* out: inode number */
346 struct xfs_name *ci_name) /* out: actual name if CI match */
348 struct xfs_da_args *args;
350 int v; /* type-checking value */
352 ASSERT(S_ISDIR(dp->i_d.di_mode));
353 XFS_STATS_INC(xs_dir_lookup);
356 * We need to use KM_NOFS here so that lockdep will not throw false
357 * positive deadlock warnings on a non-transactional lookup path. It is
358 * safe to recurse into inode recalim in that case, but lockdep can't
359 * easily be taught about it. Hence KM_NOFS avoids having to add more
360 * lockdep Doing this avoids having to add a bunch of lockdep class
361 * annotations into the reclaim path for the ilock.
363 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
364 args->geo = dp->i_mount->m_dir_geo;
365 args->name = name->name;
366 args->namelen = name->len;
367 args->filetype = name->type;
368 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
370 args->whichfork = XFS_DATA_FORK;
372 args->op_flags = XFS_DA_OP_OKNOENT;
374 args->op_flags |= XFS_DA_OP_CILOOKUP;
376 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
377 rval = xfs_dir2_sf_lookup(args);
381 rval = xfs_dir2_isblock(args, &v);
385 rval = xfs_dir2_block_lookup(args);
389 rval = xfs_dir2_isleaf(args, &v);
393 rval = xfs_dir2_leaf_lookup(args);
395 rval = xfs_dir2_node_lookup(args);
401 *inum = args->inumber;
403 ci_name->name = args->value;
404 ci_name->len = args->valuelen;
413 * Remove an entry from a directory.
419 struct xfs_name *name,
421 xfs_fsblock_t *first, /* bmap's firstblock */
422 xfs_bmap_free_t *flist, /* bmap's freeblock list */
423 xfs_extlen_t total) /* bmap's total block count */
425 struct xfs_da_args *args;
427 int v; /* type-checking value */
429 ASSERT(S_ISDIR(dp->i_d.di_mode));
430 XFS_STATS_INC(xs_dir_remove);
432 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
436 args->geo = dp->i_mount->m_dir_geo;
437 args->name = name->name;
438 args->namelen = name->len;
439 args->filetype = name->type;
440 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
443 args->firstblock = first;
446 args->whichfork = XFS_DATA_FORK;
449 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
450 rval = xfs_dir2_sf_removename(args);
454 rval = xfs_dir2_isblock(args, &v);
458 rval = xfs_dir2_block_removename(args);
462 rval = xfs_dir2_isleaf(args, &v);
466 rval = xfs_dir2_leaf_removename(args);
468 rval = xfs_dir2_node_removename(args);
475 * Replace the inode number of a directory entry.
481 struct xfs_name *name, /* name of entry to replace */
482 xfs_ino_t inum, /* new inode number */
483 xfs_fsblock_t *first, /* bmap's firstblock */
484 xfs_bmap_free_t *flist, /* bmap's freeblock list */
485 xfs_extlen_t total) /* bmap's total block count */
487 struct xfs_da_args *args;
489 int v; /* type-checking value */
491 ASSERT(S_ISDIR(dp->i_d.di_mode));
493 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
497 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
501 args->geo = dp->i_mount->m_dir_geo;
502 args->name = name->name;
503 args->namelen = name->len;
504 args->filetype = name->type;
505 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
506 args->inumber = inum;
508 args->firstblock = first;
511 args->whichfork = XFS_DATA_FORK;
514 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
515 rval = xfs_dir2_sf_replace(args);
519 rval = xfs_dir2_isblock(args, &v);
523 rval = xfs_dir2_block_replace(args);
527 rval = xfs_dir2_isleaf(args, &v);
531 rval = xfs_dir2_leaf_replace(args);
533 rval = xfs_dir2_node_replace(args);
540 * See if this entry can be added to the directory without allocating space.
546 struct xfs_name *name) /* name of entry to add */
548 return xfs_dir_createname(tp, dp, name, 0, NULL, NULL, 0);
556 * Add a block to the directory.
558 * This routine is for data and free blocks, not leaf/node blocks which are
559 * handled by xfs_da_grow_inode.
563 struct xfs_da_args *args,
564 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
565 xfs_dir2_db_t *dbp) /* out: block number added */
567 struct xfs_inode *dp = args->dp;
568 struct xfs_mount *mp = dp->i_mount;
569 xfs_fileoff_t bno; /* directory offset of new block */
570 int count; /* count of filesystem blocks */
573 trace_xfs_dir2_grow_inode(args, space);
576 * Set lowest possible block in the space requested.
578 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
579 count = args->geo->fsbcount;
581 error = xfs_da_grow_inode_int(args, &bno, count);
585 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
588 * Update file's size if this is the data space and it grew.
590 if (space == XFS_DIR2_DATA_SPACE) {
591 xfs_fsize_t size; /* directory file (data) size */
593 size = XFS_FSB_TO_B(mp, bno + count);
594 if (size > dp->i_d.di_size) {
595 dp->i_d.di_size = size;
596 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
603 * See if the directory is a single-block form directory.
607 struct xfs_da_args *args,
608 int *vp) /* out: 1 is block, 0 is not block */
610 xfs_fileoff_t last; /* last file offset */
613 if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
615 rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
616 ASSERT(rval == 0 || args->dp->i_d.di_size == args->geo->blksize);
622 * See if the directory is a single-leaf form directory.
626 struct xfs_da_args *args,
627 int *vp) /* out: 1 is block, 0 is not block */
629 xfs_fileoff_t last; /* last file offset */
632 if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
634 *vp = last == args->geo->leafblk + args->geo->fsbcount;
639 * Remove the given block from the directory.
640 * This routine is used for data and free blocks, leaf/node are done
641 * by xfs_da_shrink_inode.
644 xfs_dir2_shrink_inode(
649 xfs_fileoff_t bno; /* directory file offset */
650 xfs_dablk_t da; /* directory file offset */
651 int done; /* bunmap is finished */
657 trace_xfs_dir2_shrink_inode(args, db);
662 da = xfs_dir2_db_to_da(args->geo, db);
664 * Unmap the fsblock(s).
666 if ((error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount,
667 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
670 * ENOSPC actually can happen if we're in a removename with
671 * no space reservation, and the resulting block removal
672 * would cause a bmap btree split or conversion from extents
673 * to btree. This can only happen for un-fragmented
674 * directory blocks, since you need to be punching out
675 * the middle of an extent.
676 * In this case we need to leave the block in the file,
678 * So the block has to be in a consistent empty state
679 * and appropriately logged.
680 * We don't free up the buffer, the caller can tell it
681 * hasn't happened since it got an error back.
687 * Invalidate the buffer from the transaction.
689 xfs_trans_binval(tp, bp);
691 * If it's not a data block, we're done.
693 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
696 * If the block isn't the last one in the directory, we're done.
698 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
701 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
703 * This can't really happen unless there's kernel corruption.
707 if (db == args->geo->datablk)
712 * Set the size to the new last block.
714 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
715 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);