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"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_alloc.h"
34 #include "xfs_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
41 #include "xfs_dir_leaf.h"
42 #include "xfs_error.h"
47 * Provide the external interfaces to manage directories.
50 /*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
55 * Functions for the dirops interfaces.
57 static void xfs_dir_mount(struct xfs_mount *mp);
59 static int xfs_dir_isempty(struct xfs_inode *dp);
61 static int xfs_dir_init(struct xfs_trans *trans,
62 struct xfs_inode *dir,
63 struct xfs_inode *parent_dir);
65 static int xfs_dir_createname(struct xfs_trans *trans,
69 xfs_ino_t inode_number,
70 xfs_fsblock_t *firstblock,
71 xfs_bmap_free_t *flist,
74 static int xfs_dir_lookup(struct xfs_trans *tp,
78 xfs_ino_t *inode_number);
80 static int xfs_dir_removename(struct xfs_trans *trans,
85 xfs_fsblock_t *firstblock,
86 xfs_bmap_free_t *flist,
89 static int xfs_dir_getdents(struct xfs_trans *tp,
94 static int xfs_dir_replace(struct xfs_trans *tp,
98 xfs_ino_t inode_number,
99 xfs_fsblock_t *firstblock,
100 xfs_bmap_free_t *flist,
103 static int xfs_dir_canenter(struct xfs_trans *tp,
104 struct xfs_inode *dp,
108 static int xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp,
111 xfs_dirops_t xfsv1_dirops = {
112 .xd_mount = xfs_dir_mount,
113 .xd_isempty = xfs_dir_isempty,
114 .xd_init = xfs_dir_init,
115 .xd_createname = xfs_dir_createname,
116 .xd_lookup = xfs_dir_lookup,
117 .xd_removename = xfs_dir_removename,
118 .xd_getdents = xfs_dir_getdents,
119 .xd_replace = xfs_dir_replace,
120 .xd_canenter = xfs_dir_canenter,
121 .xd_shortform_validate_ondisk = xfs_dir_shortform_validate_ondisk,
122 .xd_shortform_to_single = xfs_dir_shortform_to_leaf,
126 * Internal routines when dirsize == XFS_LBSIZE(mp).
128 STATIC int xfs_dir_leaf_lookup(xfs_da_args_t *args);
129 STATIC int xfs_dir_leaf_removename(xfs_da_args_t *args, int *number_entries,
130 int *total_namebytes);
131 STATIC int xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
132 uio_t *uio, int *eofp,
135 STATIC int xfs_dir_leaf_replace(xfs_da_args_t *args);
138 * Internal routines when dirsize > XFS_LBSIZE(mp).
140 STATIC int xfs_dir_node_addname(xfs_da_args_t *args);
141 STATIC int xfs_dir_node_lookup(xfs_da_args_t *args);
142 STATIC int xfs_dir_node_removename(xfs_da_args_t *args);
143 STATIC int xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
144 uio_t *uio, int *eofp,
147 STATIC int xfs_dir_node_replace(xfs_da_args_t *args);
149 #if defined(XFS_DIR_TRACE)
150 ktrace_t *xfs_dir_trace_buf;
154 /*========================================================================
155 * Overall external interface routines.
156 *========================================================================*/
158 xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
161 * One-time startup routine called from xfs_init().
164 xfs_dir_startup(void)
166 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
167 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
171 * Initialize directory-related fields in the mount structure.
174 xfs_dir_mount(xfs_mount_t *mp)
176 uint shortcount, leafcount, count;
178 mp->m_dirversion = 1;
179 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
180 shortcount = (mp->m_attroffset -
181 (uint)sizeof(xfs_dir_sf_hdr_t)) /
182 (uint)sizeof(xfs_dir_sf_entry_t);
183 leafcount = (XFS_LBSIZE(mp) -
184 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
185 ((uint)sizeof(xfs_dir_leaf_entry_t) +
186 (uint)sizeof(xfs_dir_leaf_name_t));
188 shortcount = (XFS_BMDR_SPACE_CALC(MINABTPTRS) -
189 (uint)sizeof(xfs_dir_sf_hdr_t)) /
190 (uint)sizeof(xfs_dir_sf_entry_t);
191 leafcount = (XFS_LBSIZE(mp) -
192 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
193 ((uint)sizeof(xfs_dir_leaf_entry_t) +
194 (uint)sizeof(xfs_dir_leaf_name_t));
196 count = shortcount > leafcount ? shortcount : leafcount;
197 mp->m_dircook_elog = xfs_da_log2_roundup(count + 1);
198 ASSERT(mp->m_dircook_elog <= mp->m_sb.sb_blocklog);
199 mp->m_dir_node_ents = mp->m_attr_node_ents =
200 (XFS_LBSIZE(mp) - (uint)sizeof(xfs_da_node_hdr_t)) /
201 (uint)sizeof(xfs_da_node_entry_t);
202 mp->m_dir_magicpct = (XFS_LBSIZE(mp) * 37) / 100;
203 mp->m_dirblksize = mp->m_sb.sb_blocksize;
204 mp->m_dirblkfsbs = 1;
208 * Return 1 if directory contains only "." and "..".
211 xfs_dir_isempty(xfs_inode_t *dp)
213 xfs_dir_sf_hdr_t *hdr;
215 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
216 if (dp->i_d.di_size == 0)
218 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
220 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
221 return(hdr->count == 0);
225 * Initialize a directory with its "." and ".." entries.
228 xfs_dir_init(xfs_trans_t *trans, xfs_inode_t *dir, xfs_inode_t *parent_dir)
233 memset((char *)&args, 0, sizeof(args));
237 ASSERT((dir->i_d.di_mode & S_IFMT) == S_IFDIR);
238 if ((error = xfs_dir_ino_validate(trans->t_mountp, parent_dir->i_ino)))
241 return(xfs_dir_shortform_create(&args, parent_dir->i_ino));
245 * Generic handler routine to add a name to a directory.
246 * Transitions directory from shortform to Btree as necessary.
248 static int /* error */
249 xfs_dir_createname(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
250 int namelen, xfs_ino_t inum, xfs_fsblock_t *firstblock,
251 xfs_bmap_free_t *flist, xfs_extlen_t total)
254 int retval, newsize, done;
256 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
258 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
261 XFS_STATS_INC(xs_dir_create);
263 * Fill in the arg structure for this request.
266 args.namelen = namelen;
267 args.hashval = xfs_da_hashname(name, namelen);
270 args.firstblock = firstblock;
273 args.whichfork = XFS_DATA_FORK;
276 args.addname = args.oknoent = 1;
279 * Decide on what work routines to call based on the inode size.
282 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
283 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
284 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp)) {
285 retval = xfs_dir_shortform_addname(&args);
289 return XFS_ERROR(ENOSPC);
290 retval = xfs_dir_shortform_to_leaf(&args);
294 if (!done && xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
295 retval = xfs_dir_leaf_addname(&args);
296 done = retval != ENOSPC;
299 return XFS_ERROR(ENOSPC);
300 retval = xfs_dir_leaf_to_node(&args);
305 retval = xfs_dir_node_addname(&args);
311 * Generic handler routine to check if a name can be added to a directory,
312 * without adding any blocks to the directory.
314 static int /* error */
315 xfs_dir_canenter(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen)
320 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
322 * Fill in the arg structure for this request.
325 args.namelen = namelen;
326 args.hashval = xfs_da_hashname(name, namelen);
329 args.firstblock = NULL;
332 args.whichfork = XFS_DATA_FORK;
334 args.justcheck = args.addname = args.oknoent = 1;
337 * Decide on what work routines to call based on the inode size.
339 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
340 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
341 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp))
344 retval = XFS_ERROR(ENOSPC);
345 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
346 retval = xfs_dir_leaf_addname(&args);
348 retval = xfs_dir_node_addname(&args);
354 * Generic handler routine to remove a name from a directory.
355 * Transitions directory from Btree to shortform as necessary.
357 static int /* error */
358 xfs_dir_removename(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
359 int namelen, xfs_ino_t ino, xfs_fsblock_t *firstblock,
360 xfs_bmap_free_t *flist, xfs_extlen_t total)
363 int count, totallen, newsize, retval;
365 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
366 XFS_STATS_INC(xs_dir_remove);
368 * Fill in the arg structure for this request.
371 args.namelen = namelen;
372 args.hashval = xfs_da_hashname(name, namelen);
375 args.firstblock = firstblock;
378 args.whichfork = XFS_DATA_FORK;
380 args.justcheck = args.addname = args.oknoent = 0;
383 * Decide on what work routines to call based on the inode size.
385 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
386 retval = xfs_dir_shortform_removename(&args);
387 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
388 retval = xfs_dir_leaf_removename(&args, &count, &totallen);
390 newsize = XFS_DIR_SF_ALLFIT(count, totallen);
391 if (newsize <= XFS_IFORK_DSIZE(dp)) {
392 retval = xfs_dir_leaf_to_shortform(&args);
396 retval = xfs_dir_node_removename(&args);
401 static int /* error */
402 xfs_dir_lookup(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
408 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
410 XFS_STATS_INC(xs_dir_lookup);
412 * Fill in the arg structure for this request.
415 args.namelen = namelen;
416 args.hashval = xfs_da_hashname(name, namelen);
419 args.firstblock = NULL;
422 args.whichfork = XFS_DATA_FORK;
424 args.justcheck = args.addname = 0;
428 * Decide on what work routines to call based on the inode size.
430 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
431 retval = xfs_dir_shortform_lookup(&args);
432 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
433 retval = xfs_dir_leaf_lookup(&args);
435 retval = xfs_dir_node_lookup(&args);
437 if (retval == EEXIST)
439 *inum = args.inumber;
446 static int /* error */
447 xfs_dir_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio, int *eofp)
450 int alignment, retval;
453 XFS_STATS_INC(xs_dir_getdents);
454 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
457 * If our caller has given us a single contiguous memory buffer,
458 * just work directly within that buffer. If it's in user memory,
459 * lock it down first.
461 alignment = sizeof(xfs_off_t) - 1;
462 if ((uio->uio_iovcnt == 1) &&
463 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
464 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
466 put = xfs_dir_put_dirent64_direct;
468 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
469 put = xfs_dir_put_dirent64_uio;
473 * Decide on what work routines to call based on the inode size.
477 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
478 retval = xfs_dir_shortform_getdents(dp, uio, eofp, dbp, put);
479 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
480 retval = xfs_dir_leaf_getdents(trans, dp, uio, eofp, dbp, put);
482 retval = xfs_dir_node_getdents(trans, dp, uio, eofp, dbp, put);
485 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
490 static int /* error */
491 xfs_dir_replace(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
492 xfs_ino_t inum, xfs_fsblock_t *firstblock,
493 xfs_bmap_free_t *flist, xfs_extlen_t total)
498 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
500 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
504 * Fill in the arg structure for this request.
507 args.namelen = namelen;
508 args.hashval = xfs_da_hashname(name, namelen);
511 args.firstblock = firstblock;
514 args.whichfork = XFS_DATA_FORK;
516 args.justcheck = args.addname = args.oknoent = 0;
519 * Decide on what work routines to call based on the inode size.
521 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
522 retval = xfs_dir_shortform_replace(&args);
523 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
524 retval = xfs_dir_leaf_replace(&args);
526 retval = xfs_dir_node_replace(&args);
533 xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp, xfs_dinode_t *dp)
538 xfs_dir_shortform_t *sf;
539 xfs_dir_sf_entry_t *sfe;
544 if ((INT_GET(dp->di_core.di_mode, ARCH_CONVERT) & S_IFMT) != S_IFDIR) {
547 if (INT_GET(dp->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_LOCAL) {
550 if (INT_GET(dp->di_core.di_size, ARCH_CONVERT) < sizeof(sf->hdr)) {
551 xfs_fs_cmn_err(CE_WARN, mp, "Invalid shortform size: dp 0x%p",
555 sf = (xfs_dir_shortform_t *)(&dp->di_u.di_dirsf);
556 ino = XFS_GET_DIR_INO8(sf->hdr.parent);
557 if (xfs_dir_ino_validate(mp, ino))
560 count = sf->hdr.count;
561 if ((count < 0) || ((count * 10) > XFS_LITINO(mp))) {
562 xfs_fs_cmn_err(CE_WARN, mp,
563 "Invalid shortform count: dp 0x%p", dp);
573 for (i = sf->hdr.count - 1; i >= 0; i--) {
574 ino = XFS_GET_DIR_INO8(sfe->inumber);
575 xfs_dir_ino_validate(mp, ino);
576 if (sfe->namelen >= XFS_LITINO(mp)) {
577 xfs_fs_cmn_err(CE_WARN, mp,
578 "Invalid shortform namelen: dp 0x%p", dp);
581 namelen_sum += sfe->namelen;
582 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
584 if (namelen_sum >= XFS_LITINO(mp)) {
585 xfs_fs_cmn_err(CE_WARN, mp,
586 "Invalid shortform namelen: dp 0x%p", dp);
593 /*========================================================================
594 * External routines when dirsize == XFS_LBSIZE(dp->i_mount).
595 *========================================================================*/
598 * Add a name to the leaf directory structure
599 * This is the external routine.
602 xfs_dir_leaf_addname(xfs_da_args_t *args)
607 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
613 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
614 if (retval == ENOENT)
615 retval = xfs_dir_leaf_add(bp, args, index);
621 * Remove a name from the leaf directory structure
622 * This is the external routine.
625 xfs_dir_leaf_removename(xfs_da_args_t *args, int *count, int *totallen)
627 xfs_dir_leafblock_t *leaf;
631 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
637 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
638 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
639 if (retval == EEXIST) {
640 (void)xfs_dir_leaf_remove(args->trans, bp, index);
641 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
642 *totallen = INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
650 * Look up a name in a leaf directory structure.
651 * This is the external routine.
654 xfs_dir_leaf_lookup(xfs_da_args_t *args)
659 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
664 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
665 xfs_da_brelse(args->trans, bp);
670 * Copy out directory entries for getdents(), for leaf directories.
673 xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
674 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
679 retval = xfs_da_read_buf(dp->i_transp, dp, 0, -1, &bp, XFS_DATA_FORK);
683 retval = xfs_dir_leaf_getdents_int(bp, dp, 0, uio, &eob, dbp, put, -1);
684 xfs_da_brelse(trans, bp);
690 * Look up a name in a leaf directory structure, replace the inode number.
691 * This is the external routine.
694 xfs_dir_leaf_replace(xfs_da_args_t *args)
699 xfs_dir_leafblock_t *leaf;
700 xfs_dir_leaf_entry_t *entry;
701 xfs_dir_leaf_name_t *namest;
703 inum = args->inumber;
704 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
709 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
710 if (retval == EEXIST) {
712 entry = &leaf->entries[index];
713 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
714 /* XXX - replace assert? */
715 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
716 xfs_da_log_buf(args->trans, bp,
717 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
721 xfs_da_brelse(args->trans, bp);
726 /*========================================================================
727 * External routines when dirsize > XFS_LBSIZE(mp).
728 *========================================================================*/
731 * Add a name to a Btree-format directory.
733 * This will involve walking down the Btree, and may involve splitting
734 * leaf nodes and even splitting intermediate nodes up to and including
735 * the root node (a special case of an intermediate node).
738 xfs_dir_node_addname(xfs_da_args_t *args)
740 xfs_da_state_t *state;
741 xfs_da_state_blk_t *blk;
745 * Fill in bucket of arguments/results/context to carry around.
747 state = xfs_da_state_alloc();
749 state->mp = args->dp->i_mount;
750 state->blocksize = state->mp->m_sb.sb_blocksize;
751 state->node_ents = state->mp->m_dir_node_ents;
754 * Search to see if name already exists, and get back a pointer
755 * to where it should go.
757 error = xfs_da_node_lookup_int(state, &retval);
760 if (retval != ENOENT)
762 blk = &state->path.blk[ state->path.active-1 ];
763 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
764 retval = xfs_dir_leaf_add(blk->bp, args, blk->index);
767 * Addition succeeded, update Btree hashvals.
769 if (!args->justcheck)
770 xfs_da_fixhashpath(state, &state->path);
773 * Addition failed, split as many Btree elements as required.
775 if (args->total == 0) {
776 ASSERT(retval == ENOSPC);
779 retval = xfs_da_split(state);
782 xfs_da_state_free(state);
788 * Remove a name from a B-tree directory.
790 * This will involve walking down the Btree, and may involve joining
791 * leaf nodes and even joining intermediate nodes up to and including
792 * the root node (a special case of an intermediate node).
795 xfs_dir_node_removename(xfs_da_args_t *args)
797 xfs_da_state_t *state;
798 xfs_da_state_blk_t *blk;
801 state = xfs_da_state_alloc();
803 state->mp = args->dp->i_mount;
804 state->blocksize = state->mp->m_sb.sb_blocksize;
805 state->node_ents = state->mp->m_dir_node_ents;
808 * Search to see if name exists, and get back a pointer to it.
810 error = xfs_da_node_lookup_int(state, &retval);
813 if (retval != EEXIST) {
814 xfs_da_state_free(state);
819 * Remove the name and update the hashvals in the tree.
821 blk = &state->path.blk[ state->path.active-1 ];
822 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
823 retval = xfs_dir_leaf_remove(args->trans, blk->bp, blk->index);
824 xfs_da_fixhashpath(state, &state->path);
827 * Check to see if the tree needs to be collapsed.
831 error = xfs_da_join(state);
834 xfs_da_state_free(state);
841 * Look up a filename in a int directory.
842 * Use an internal routine to actually do all the work.
845 xfs_dir_node_lookup(xfs_da_args_t *args)
847 xfs_da_state_t *state;
848 int retval, error, i;
850 state = xfs_da_state_alloc();
852 state->mp = args->dp->i_mount;
853 state->blocksize = state->mp->m_sb.sb_blocksize;
854 state->node_ents = state->mp->m_dir_node_ents;
857 * Search to see if name exists,
858 * and get back a pointer to it.
860 error = xfs_da_node_lookup_int(state, &retval);
866 * If not in a transaction, we have to release all the buffers.
868 for (i = 0; i < state->path.active; i++) {
869 xfs_da_brelse(args->trans, state->path.blk[i].bp);
870 state->path.blk[i].bp = NULL;
873 xfs_da_state_free(state);
878 xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
879 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
881 xfs_da_intnode_t *node;
882 xfs_da_node_entry_t *btree;
883 xfs_dir_leafblock_t *leaf = NULL;
884 xfs_dablk_t bno, nextbno;
885 xfs_dahash_t cookhash;
892 * Pick up our context.
896 bno = XFS_DA_COOKIE_BNO(mp, uio->uio_offset);
897 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
899 xfs_dir_trace_g_du("node: start", dp, uio);
902 * Re-find our place, even if we're confused about what our place is.
904 * First we check the block number from the magic cookie, it is a
905 * cache of where we ended last time. If we find a leaf block, and
906 * the starting hashval in that block is less than our desired
907 * hashval, then we run with it.
910 error = xfs_da_read_buf(trans, dp, bno, -2, &bp, XFS_DATA_FORK);
911 if ((error != 0) && (error != EFSCORRUPTED))
915 if (bp && be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC) {
916 xfs_dir_trace_g_dub("node: block not a leaf",
918 xfs_da_brelse(trans, bp);
921 if (bp && INT_GET(leaf->entries[0].hashval, ARCH_CONVERT) > cookhash) {
922 xfs_dir_trace_g_dub("node: leaf hash too large",
924 xfs_da_brelse(trans, bp);
928 cookhash > INT_GET(leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)) {
929 xfs_dir_trace_g_dub("node: leaf hash too small",
931 xfs_da_brelse(trans, bp);
937 * If we did not find a leaf block from the blockno in the cookie,
938 * or we there was no blockno in the cookie (eg: first time thru),
939 * the we start at the top of the Btree and re-find our hashval.
942 xfs_dir_trace_g_du("node: start at root" , dp, uio);
945 error = xfs_da_read_buf(trans, dp, bno, -1, &bp,
950 return(XFS_ERROR(EFSCORRUPTED));
952 if (be16_to_cpu(node->hdr.info.magic) != XFS_DA_NODE_MAGIC)
954 btree = &node->btree[0];
955 xfs_dir_trace_g_dun("node: node detail", dp, uio, node);
956 for (i = 0; i < be16_to_cpu(node->hdr.count); btree++, i++) {
957 if (be32_to_cpu(btree->hashval) >= cookhash) {
958 bno = be32_to_cpu(btree->before);
962 if (i == be16_to_cpu(node->hdr.count)) {
963 xfs_da_brelse(trans, bp);
964 xfs_dir_trace_g_du("node: hash beyond EOF",
966 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0,
971 xfs_dir_trace_g_dub("node: going to block",
973 xfs_da_brelse(trans, bp);
976 ASSERT(cookhash != XFS_DA_MAXHASH);
979 * We've dropped down to the (first) leaf block that contains the
980 * hashval we are interested in. Continue rolling upward thru the
981 * leaf blocks until we fill up our buffer.
985 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC)) {
986 xfs_dir_trace_g_dul("node: not a leaf", dp, uio, leaf);
987 xfs_da_brelse(trans, bp);
988 XFS_CORRUPTION_ERROR("xfs_dir_node_getdents(1)",
989 XFS_ERRLEVEL_LOW, mp, leaf);
990 return XFS_ERROR(EFSCORRUPTED);
992 xfs_dir_trace_g_dul("node: leaf detail", dp, uio, leaf);
993 if ((nextbno = be32_to_cpu(leaf->hdr.info.forw))) {
994 nextda = xfs_da_reada_buf(trans, dp, nextbno,
998 error = xfs_dir_leaf_getdents_int(bp, dp, bno, uio, &eob, dbp,
1000 xfs_da_brelse(trans, bp);
1003 xfs_dir_trace_g_dub("node: E-O-B", dp, uio, bno);
1009 error = xfs_da_read_buf(trans, dp, bno, nextda, &bp,
1013 if (unlikely(bp == NULL)) {
1014 XFS_ERROR_REPORT("xfs_dir_node_getdents(2)",
1015 XFS_ERRLEVEL_LOW, mp);
1016 return(XFS_ERROR(EFSCORRUPTED));
1020 xfs_dir_trace_g_du("node: E-O-F", dp, uio);
1025 * Look up a filename in an int directory, replace the inode number.
1026 * Use an internal routine to actually do the lookup.
1029 xfs_dir_node_replace(xfs_da_args_t *args)
1031 xfs_da_state_t *state;
1032 xfs_da_state_blk_t *blk;
1033 xfs_dir_leafblock_t *leaf;
1034 xfs_dir_leaf_entry_t *entry;
1035 xfs_dir_leaf_name_t *namest;
1037 int retval, error, i;
1040 state = xfs_da_state_alloc();
1042 state->mp = args->dp->i_mount;
1043 state->blocksize = state->mp->m_sb.sb_blocksize;
1044 state->node_ents = state->mp->m_dir_node_ents;
1045 inum = args->inumber;
1048 * Search to see if name exists,
1049 * and get back a pointer to it.
1051 error = xfs_da_node_lookup_int(state, &retval);
1056 if (retval == EEXIST) {
1057 blk = &state->path.blk[state->path.active - 1];
1058 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
1061 entry = &leaf->entries[blk->index];
1062 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1063 /* XXX - replace assert ? */
1064 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
1065 xfs_da_log_buf(args->trans, bp,
1066 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
1067 xfs_da_buf_done(bp);
1071 i = state->path.active - 1;
1072 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1073 state->path.blk[i].bp = NULL;
1075 for (i = 0; i < state->path.active - 1; i++) {
1076 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1077 state->path.blk[i].bp = NULL;
1080 xfs_da_state_free(state);
1084 #if defined(XFS_DIR_TRACE)
1086 * Add a trace buffer entry for an inode and a uio.
1089 xfs_dir_trace_g_du(char *where, xfs_inode_t *dp, uio_t *uio)
1091 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DU, where,
1092 (void *)dp, (void *)dp->i_mount,
1093 (void *)((unsigned long)(uio->uio_offset >> 32)),
1094 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1095 (void *)(unsigned long)uio->uio_resid,
1096 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1100 * Add a trace buffer entry for an inode and a uio.
1103 xfs_dir_trace_g_dub(char *where, xfs_inode_t *dp, uio_t *uio, xfs_dablk_t bno)
1105 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUB, where,
1106 (void *)dp, (void *)dp->i_mount,
1107 (void *)((unsigned long)(uio->uio_offset >> 32)),
1108 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1109 (void *)(unsigned long)uio->uio_resid,
1110 (void *)(unsigned long)bno,
1111 NULL, NULL, NULL, NULL, NULL, NULL);
1115 * Add a trace buffer entry for an inode and a uio.
1118 xfs_dir_trace_g_dun(char *where, xfs_inode_t *dp, uio_t *uio,
1119 xfs_da_intnode_t *node)
1121 int last = be16_to_cpu(node->hdr.count) - 1;
1123 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUN, where,
1124 (void *)dp, (void *)dp->i_mount,
1125 (void *)((unsigned long)(uio->uio_offset >> 32)),
1126 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1127 (void *)(unsigned long)uio->uio_resid,
1128 (void *)(unsigned long)be32_to_cpu(node->hdr.info.forw),
1129 (void *)(unsigned long)
1130 be16_to_cpu(node->hdr.count),
1131 (void *)(unsigned long)
1132 be32_to_cpu(node->btree[0].hashval),
1133 (void *)(unsigned long)
1134 be32_to_cpu(node->btree[last].hashval),
1139 * Add a trace buffer entry for an inode and a uio.
1142 xfs_dir_trace_g_dul(char *where, xfs_inode_t *dp, uio_t *uio,
1143 xfs_dir_leafblock_t *leaf)
1145 int last = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
1147 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUL, where,
1148 (void *)dp, (void *)dp->i_mount,
1149 (void *)((unsigned long)(uio->uio_offset >> 32)),
1150 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1151 (void *)(unsigned long)uio->uio_resid,
1152 (void *)(unsigned long)be32_to_cpu(leaf->hdr.info.forw),
1153 (void *)(unsigned long)
1154 INT_GET(leaf->hdr.count, ARCH_CONVERT),
1155 (void *)(unsigned long)
1156 INT_GET(leaf->entries[0].hashval, ARCH_CONVERT),
1157 (void *)(unsigned long)
1158 INT_GET(leaf->entries[last].hashval, ARCH_CONVERT),
1163 * Add a trace buffer entry for an inode and a uio.
1166 xfs_dir_trace_g_due(char *where, xfs_inode_t *dp, uio_t *uio,
1167 xfs_dir_leaf_entry_t *entry)
1169 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUE, where,
1170 (void *)dp, (void *)dp->i_mount,
1171 (void *)((unsigned long)(uio->uio_offset >> 32)),
1172 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1173 (void *)(unsigned long)uio->uio_resid,
1174 (void *)(unsigned long)
1175 INT_GET(entry->hashval, ARCH_CONVERT),
1176 NULL, NULL, NULL, NULL, NULL, NULL);
1180 * Add a trace buffer entry for an inode and a uio.
1183 xfs_dir_trace_g_duc(char *where, xfs_inode_t *dp, uio_t *uio, xfs_off_t cookie)
1185 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUC, where,
1186 (void *)dp, (void *)dp->i_mount,
1187 (void *)((unsigned long)(uio->uio_offset >> 32)),
1188 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1189 (void *)(unsigned long)uio->uio_resid,
1190 (void *)((unsigned long)(cookie >> 32)),
1191 (void *)((unsigned long)(cookie & 0xFFFFFFFF)),
1192 NULL, NULL, NULL, NULL, NULL);
1196 * Add a trace buffer entry for the arguments given to the routine,
1200 xfs_dir_trace_enter(int type, char *where,
1201 void * a0, void * a1,
1202 void * a2, void * a3,
1203 void * a4, void * a5,
1204 void * a6, void * a7,
1205 void * a8, void * a9,
1206 void * a10, void * a11)
1208 ASSERT(xfs_dir_trace_buf);
1209 ktrace_enter(xfs_dir_trace_buf, (void *)(unsigned long)type,
1211 (void *)a0, (void *)a1, (void *)a2,
1212 (void *)a3, (void *)a4, (void *)a5,
1213 (void *)a6, (void *)a7, (void *)a8,
1214 (void *)a9, (void *)a10, (void *)a11,
1217 #endif /* XFS_DIR_TRACE */