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"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
39 #include "xfs_dir2_data.h"
40 #include "xfs_dir2_leaf.h"
41 #include "xfs_dir2_block.h"
42 #include "xfs_dir2_node.h"
43 #include "xfs_dir2_trace.h"
44 #include "xfs_error.h"
45 #include "xfs_vnodeops.h"
47 struct xfs_name xfs_name_dotdot = {"..", 2};
53 ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
54 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
56 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
57 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
58 mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
59 mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
60 mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
61 mp->m_attr_node_ents =
62 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
63 (uint)sizeof(xfs_da_node_entry_t);
65 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
66 (uint)sizeof(xfs_da_node_entry_t);
67 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
68 mp->m_dirnameops = &xfs_default_nameops;
72 * Return 1 if directory contains only "." and "..".
80 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
81 if (dp->i_d.di_size == 0) /* might happen during shutdown. */
83 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
85 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
86 return !sfp->hdr.count;
90 * Validate a given inode number.
97 xfs_agblock_t agblkno;
103 agno = XFS_INO_TO_AGNO(mp, ino);
104 agblkno = XFS_INO_TO_AGBNO(mp, ino);
105 ioff = XFS_INO_TO_OFFSET(mp, ino);
106 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
108 agno < mp->m_sb.sb_agcount &&
109 agblkno < mp->m_sb.sb_agblocks &&
111 ioff < (1 << mp->m_sb.sb_inopblog) &&
112 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
113 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
114 XFS_RANDOM_DIR_INO_VALIDATE))) {
115 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
116 (unsigned long long) ino);
117 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
118 return XFS_ERROR(EFSCORRUPTED);
124 * Initialize a directory with its "." and ".." entries.
135 memset((char *)&args, 0, sizeof(args));
138 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
139 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
141 return xfs_dir2_sf_create(&args, pdp->i_ino);
145 Enter a name in a directory.
151 struct xfs_name *name,
152 xfs_ino_t inum, /* new entry inode number */
153 xfs_fsblock_t *first, /* bmap's firstblock */
154 xfs_bmap_free_t *flist, /* bmap's freeblock list */
155 xfs_extlen_t total) /* bmap's total block count */
159 int v; /* type-checking value */
161 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
162 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
164 XFS_STATS_INC(xs_dir_create);
166 args.name = name->name;
167 args.namelen = name->len;
168 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
171 args.firstblock = first;
174 args.whichfork = XFS_DATA_FORK;
177 args.addname = args.oknoent = 1;
179 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
180 rval = xfs_dir2_sf_addname(&args);
181 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
184 rval = xfs_dir2_block_addname(&args);
185 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
188 rval = xfs_dir2_leaf_addname(&args);
190 rval = xfs_dir2_node_addname(&args);
195 * Lookup a name in a directory, give back the inode number.
201 struct xfs_name *name,
202 xfs_ino_t *inum) /* out: inode number */
206 int v; /* type-checking value */
208 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
209 XFS_STATS_INC(xs_dir_lookup);
210 memset(&args, 0, sizeof(xfs_da_args_t));
212 args.name = name->name;
213 args.namelen = name->len;
214 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
216 args.whichfork = XFS_DATA_FORK;
219 args.cmpresult = XFS_CMP_DIFFERENT;
221 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
222 rval = xfs_dir2_sf_lookup(&args);
223 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
226 rval = xfs_dir2_block_lookup(&args);
227 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
230 rval = xfs_dir2_leaf_lookup(&args);
232 rval = xfs_dir2_node_lookup(&args);
236 *inum = args.inumber;
241 * Remove an entry from a directory.
247 struct xfs_name *name,
249 xfs_fsblock_t *first, /* bmap's firstblock */
250 xfs_bmap_free_t *flist, /* bmap's freeblock list */
251 xfs_extlen_t total) /* bmap's total block count */
255 int v; /* type-checking value */
257 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
258 XFS_STATS_INC(xs_dir_remove);
260 args.name = name->name;
261 args.namelen = name->len;
262 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
265 args.firstblock = first;
268 args.whichfork = XFS_DATA_FORK;
270 args.justcheck = args.addname = args.oknoent = 0;
272 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
273 rval = xfs_dir2_sf_removename(&args);
274 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
277 rval = xfs_dir2_block_removename(&args);
278 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
281 rval = xfs_dir2_leaf_removename(&args);
283 rval = xfs_dir2_node_removename(&args);
298 int rval; /* return value */
299 int v; /* type-checking value */
301 xfs_itrace_entry(dp);
303 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
304 return XFS_ERROR(EIO);
306 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
307 XFS_STATS_INC(xs_dir_getdents);
309 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
310 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
311 else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
314 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
316 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
322 * Replace the inode number of a directory entry.
328 struct xfs_name *name, /* name of entry to replace */
329 xfs_ino_t inum, /* new inode number */
330 xfs_fsblock_t *first, /* bmap's firstblock */
331 xfs_bmap_free_t *flist, /* bmap's freeblock list */
332 xfs_extlen_t total) /* bmap's total block count */
336 int v; /* type-checking value */
338 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
340 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
343 args.name = name->name;
344 args.namelen = name->len;
345 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
348 args.firstblock = first;
351 args.whichfork = XFS_DATA_FORK;
353 args.justcheck = args.addname = args.oknoent = 0;
355 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
356 rval = xfs_dir2_sf_replace(&args);
357 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
360 rval = xfs_dir2_block_replace(&args);
361 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
364 rval = xfs_dir2_leaf_replace(&args);
366 rval = xfs_dir2_node_replace(&args);
371 * See if this entry can be added to the directory without allocating space.
372 * First checks that the caller couldn't reserve enough space (resblks = 0).
378 struct xfs_name *name, /* name of entry to add */
383 int v; /* type-checking value */
388 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
389 memset(&args, 0, sizeof(xfs_da_args_t));
391 args.name = name->name;
392 args.namelen = name->len;
393 args.hashval = dp->i_mount->m_dirnameops->hashname(name);
395 args.whichfork = XFS_DATA_FORK;
397 args.justcheck = args.addname = args.oknoent = 1;
399 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
400 rval = xfs_dir2_sf_addname(&args);
401 else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
404 rval = xfs_dir2_block_addname(&args);
405 else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
408 rval = xfs_dir2_leaf_addname(&args);
410 rval = xfs_dir2_node_addname(&args);
419 * Add a block to the directory.
420 * This routine is for data and free blocks, not leaf/node blocks
421 * which are handled by xfs_da_grow_inode.
426 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
427 xfs_dir2_db_t *dbp) /* out: block number added */
429 xfs_fileoff_t bno; /* directory offset of new block */
430 int count; /* count of filesystem blocks */
431 xfs_inode_t *dp; /* incore directory inode */
433 int got; /* blocks actually mapped */
435 xfs_bmbt_irec_t map; /* single structure for bmap */
436 int mapi; /* mapping index */
437 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
439 int nmap; /* number of bmap entries */
442 xfs_dir2_trace_args_s("grow_inode", args, space);
447 * Set lowest possible block in the space requested.
449 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
450 count = mp->m_dirblkfsbs;
452 * Find the first hole for our block.
454 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
457 ASSERT(args->firstblock != NULL);
459 * Try mapping the new block contiguously (one extent).
461 if ((error = xfs_bmapi(tp, dp, bno, count,
462 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
463 args->firstblock, args->total, &map, &nmap,
472 * Didn't work and this is a multiple-fsb directory block.
473 * Try again with contiguous flag turned on.
475 else if (nmap == 0 && count > 1) {
476 xfs_fileoff_t b; /* current file offset */
479 * Space for maximum number of mappings.
481 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
483 * Iterate until we get to the end of our block.
485 for (b = bno, mapi = 0; b < bno + count; ) {
486 int c; /* current fsb count */
489 * Can't map more than MAX_NMAP at once.
491 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
492 c = (int)(bno + count - b);
493 if ((error = xfs_bmapi(tp, dp, b, c,
494 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
495 args->firstblock, args->total,
496 &mapp[mapi], &nmap, args->flist,
504 * Add this bunch into our table, go to the next offset.
507 b = mapp[mapi - 1].br_startoff +
508 mapp[mapi - 1].br_blockcount;
519 * See how many fsb's we got.
521 for (i = 0, got = 0; i < mapi; i++)
522 got += mapp[i].br_blockcount;
524 * Didn't get enough fsb's, or the first/last block's are wrong.
526 if (got != count || mapp[0].br_startoff != bno ||
527 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
531 return XFS_ERROR(ENOSPC);
534 * Done with the temporary mapping table.
538 *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
540 * Update file's size if this is the data space and it grew.
542 if (space == XFS_DIR2_DATA_SPACE) {
543 xfs_fsize_t size; /* directory file (data) size */
545 size = XFS_FSB_TO_B(mp, bno + count);
546 if (size > dp->i_d.di_size) {
547 dp->i_d.di_size = size;
548 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
555 * See if the directory is a single-block form directory.
561 int *vp) /* out: 1 is block, 0 is not block */
563 xfs_fileoff_t last; /* last file offset */
568 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
570 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
571 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
577 * See if the directory is a single-leaf form directory.
583 int *vp) /* out: 1 is leaf, 0 is not leaf */
585 xfs_fileoff_t last; /* last file offset */
590 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
592 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
597 * Remove the given block from the directory.
598 * This routine is used for data and free blocks, leaf/node are done
599 * by xfs_da_shrink_inode.
602 xfs_dir2_shrink_inode(
607 xfs_fileoff_t bno; /* directory file offset */
608 xfs_dablk_t da; /* directory file offset */
609 int done; /* bunmap is finished */
615 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
619 da = xfs_dir2_db_to_da(mp, db);
621 * Unmap the fsblock(s).
623 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
624 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
627 * ENOSPC actually can happen if we're in a removename with
628 * no space reservation, and the resulting block removal
629 * would cause a bmap btree split or conversion from extents
630 * to btree. This can only happen for un-fragmented
631 * directory blocks, since you need to be punching out
632 * the middle of an extent.
633 * In this case we need to leave the block in the file,
635 * So the block has to be in a consistent empty state
636 * and appropriately logged.
637 * We don't free up the buffer, the caller can tell it
638 * hasn't happened since it got an error back.
644 * Invalidate the buffer from the transaction.
646 xfs_da_binval(tp, bp);
648 * If it's not a data block, we're done.
650 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
653 * If the block isn't the last one in the directory, we're done.
655 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
658 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
660 * This can't really happen unless there's kernel corruption.
664 if (db == mp->m_dirdatablk)
669 * Set the size to the new last block.
671 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
672 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);