2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_alloc.h"
49 #include "xfs_btree.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dir_sf.h"
52 #include "xfs_dir2_sf.h"
53 #include "xfs_dinode.h"
54 #include "xfs_inode.h"
56 #include "xfs_da_btree.h"
57 #include "xfs_dir_leaf.h"
58 #include "xfs_error.h"
63 * Provide the external interfaces to manage directories.
66 /*========================================================================
67 * Function prototypes for the kernel.
68 *========================================================================*/
71 * Functions for the dirops interfaces.
73 static void xfs_dir_mount(struct xfs_mount *mp);
75 static int xfs_dir_isempty(struct xfs_inode *dp);
77 static int xfs_dir_init(struct xfs_trans *trans,
78 struct xfs_inode *dir,
79 struct xfs_inode *parent_dir);
81 static int xfs_dir_createname(struct xfs_trans *trans,
85 xfs_ino_t inode_number,
86 xfs_fsblock_t *firstblock,
87 xfs_bmap_free_t *flist,
90 static int xfs_dir_lookup(struct xfs_trans *tp,
94 xfs_ino_t *inode_number);
96 static int xfs_dir_removename(struct xfs_trans *trans,
101 xfs_fsblock_t *firstblock,
102 xfs_bmap_free_t *flist,
105 static int xfs_dir_getdents(struct xfs_trans *tp,
106 struct xfs_inode *dp,
110 static int xfs_dir_replace(struct xfs_trans *tp,
111 struct xfs_inode *dp,
114 xfs_ino_t inode_number,
115 xfs_fsblock_t *firstblock,
116 xfs_bmap_free_t *flist,
119 static int xfs_dir_canenter(struct xfs_trans *tp,
120 struct xfs_inode *dp,
124 static int xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp,
127 xfs_dirops_t xfsv1_dirops = {
128 .xd_mount = xfs_dir_mount,
129 .xd_isempty = xfs_dir_isempty,
130 .xd_init = xfs_dir_init,
131 .xd_createname = xfs_dir_createname,
132 .xd_lookup = xfs_dir_lookup,
133 .xd_removename = xfs_dir_removename,
134 .xd_getdents = xfs_dir_getdents,
135 .xd_replace = xfs_dir_replace,
136 .xd_canenter = xfs_dir_canenter,
137 .xd_shortform_validate_ondisk = xfs_dir_shortform_validate_ondisk,
138 .xd_shortform_to_single = xfs_dir_shortform_to_leaf,
142 * Internal routines when dirsize == XFS_LBSIZE(mp).
144 STATIC int xfs_dir_leaf_lookup(xfs_da_args_t *args);
145 STATIC int xfs_dir_leaf_removename(xfs_da_args_t *args, int *number_entries,
146 int *total_namebytes);
147 STATIC int xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
148 uio_t *uio, int *eofp,
151 STATIC int xfs_dir_leaf_replace(xfs_da_args_t *args);
154 * Internal routines when dirsize > XFS_LBSIZE(mp).
156 STATIC int xfs_dir_node_addname(xfs_da_args_t *args);
157 STATIC int xfs_dir_node_lookup(xfs_da_args_t *args);
158 STATIC int xfs_dir_node_removename(xfs_da_args_t *args);
159 STATIC int xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
160 uio_t *uio, int *eofp,
163 STATIC int xfs_dir_node_replace(xfs_da_args_t *args);
165 #if defined(XFS_DIR_TRACE)
166 ktrace_t *xfs_dir_trace_buf;
170 /*========================================================================
171 * Overall external interface routines.
172 *========================================================================*/
174 xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
177 * One-time startup routine called from xfs_init().
180 xfs_dir_startup(void)
182 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
183 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
187 * Initialize directory-related fields in the mount structure.
190 xfs_dir_mount(xfs_mount_t *mp)
192 uint shortcount, leafcount, count;
194 mp->m_dirversion = 1;
195 if (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) {
196 shortcount = (mp->m_attroffset -
197 (uint)sizeof(xfs_dir_sf_hdr_t)) /
198 (uint)sizeof(xfs_dir_sf_entry_t);
199 leafcount = (XFS_LBSIZE(mp) -
200 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
201 ((uint)sizeof(xfs_dir_leaf_entry_t) +
202 (uint)sizeof(xfs_dir_leaf_name_t));
204 shortcount = (XFS_BMDR_SPACE_CALC(MINABTPTRS) -
205 (uint)sizeof(xfs_dir_sf_hdr_t)) /
206 (uint)sizeof(xfs_dir_sf_entry_t);
207 leafcount = (XFS_LBSIZE(mp) -
208 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
209 ((uint)sizeof(xfs_dir_leaf_entry_t) +
210 (uint)sizeof(xfs_dir_leaf_name_t));
212 count = shortcount > leafcount ? shortcount : leafcount;
213 mp->m_dircook_elog = xfs_da_log2_roundup(count + 1);
214 ASSERT(mp->m_dircook_elog <= mp->m_sb.sb_blocklog);
215 mp->m_dir_node_ents = mp->m_attr_node_ents =
216 (XFS_LBSIZE(mp) - (uint)sizeof(xfs_da_node_hdr_t)) /
217 (uint)sizeof(xfs_da_node_entry_t);
218 mp->m_dir_magicpct = (XFS_LBSIZE(mp) * 37) / 100;
219 mp->m_dirblksize = mp->m_sb.sb_blocksize;
220 mp->m_dirblkfsbs = 1;
224 * Return 1 if directory contains only "." and "..".
227 xfs_dir_isempty(xfs_inode_t *dp)
229 xfs_dir_sf_hdr_t *hdr;
231 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
232 if (dp->i_d.di_size == 0)
234 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
236 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
237 return(hdr->count == 0);
241 * Initialize a directory with its "." and ".." entries.
244 xfs_dir_init(xfs_trans_t *trans, xfs_inode_t *dir, xfs_inode_t *parent_dir)
249 memset((char *)&args, 0, sizeof(args));
253 ASSERT((dir->i_d.di_mode & S_IFMT) == S_IFDIR);
254 if ((error = xfs_dir_ino_validate(trans->t_mountp, parent_dir->i_ino)))
257 return(xfs_dir_shortform_create(&args, parent_dir->i_ino));
261 * Generic handler routine to add a name to a directory.
262 * Transitions directory from shortform to Btree as necessary.
264 static int /* error */
265 xfs_dir_createname(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
266 int namelen, xfs_ino_t inum, xfs_fsblock_t *firstblock,
267 xfs_bmap_free_t *flist, xfs_extlen_t total)
270 int retval, newsize, done;
272 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
274 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
277 XFS_STATS_INC(xs_dir_create);
279 * Fill in the arg structure for this request.
282 args.namelen = namelen;
283 args.hashval = xfs_da_hashname(name, namelen);
286 args.firstblock = firstblock;
289 args.whichfork = XFS_DATA_FORK;
292 args.addname = args.oknoent = 1;
295 * Decide on what work routines to call based on the inode size.
298 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
299 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
300 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp)) {
301 retval = xfs_dir_shortform_addname(&args);
305 return XFS_ERROR(ENOSPC);
306 retval = xfs_dir_shortform_to_leaf(&args);
310 if (!done && xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
311 retval = xfs_dir_leaf_addname(&args);
312 done = retval != ENOSPC;
315 return XFS_ERROR(ENOSPC);
316 retval = xfs_dir_leaf_to_node(&args);
321 retval = xfs_dir_node_addname(&args);
327 * Generic handler routine to check if a name can be added to a directory,
328 * without adding any blocks to the directory.
330 static int /* error */
331 xfs_dir_canenter(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen)
336 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
338 * Fill in the arg structure for this request.
341 args.namelen = namelen;
342 args.hashval = xfs_da_hashname(name, namelen);
345 args.firstblock = NULL;
348 args.whichfork = XFS_DATA_FORK;
350 args.justcheck = args.addname = args.oknoent = 1;
353 * Decide on what work routines to call based on the inode size.
355 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
356 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
357 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp))
360 retval = XFS_ERROR(ENOSPC);
361 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
362 retval = xfs_dir_leaf_addname(&args);
364 retval = xfs_dir_node_addname(&args);
370 * Generic handler routine to remove a name from a directory.
371 * Transitions directory from Btree to shortform as necessary.
373 static int /* error */
374 xfs_dir_removename(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
375 int namelen, xfs_ino_t ino, xfs_fsblock_t *firstblock,
376 xfs_bmap_free_t *flist, xfs_extlen_t total)
379 int count, totallen, newsize, retval;
381 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
382 XFS_STATS_INC(xs_dir_remove);
384 * Fill in the arg structure for this request.
387 args.namelen = namelen;
388 args.hashval = xfs_da_hashname(name, namelen);
391 args.firstblock = firstblock;
394 args.whichfork = XFS_DATA_FORK;
396 args.justcheck = args.addname = args.oknoent = 0;
399 * Decide on what work routines to call based on the inode size.
401 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
402 retval = xfs_dir_shortform_removename(&args);
403 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
404 retval = xfs_dir_leaf_removename(&args, &count, &totallen);
406 newsize = XFS_DIR_SF_ALLFIT(count, totallen);
407 if (newsize <= XFS_IFORK_DSIZE(dp)) {
408 retval = xfs_dir_leaf_to_shortform(&args);
412 retval = xfs_dir_node_removename(&args);
417 static int /* error */
418 xfs_dir_lookup(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
424 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
426 XFS_STATS_INC(xs_dir_lookup);
428 * Fill in the arg structure for this request.
431 args.namelen = namelen;
432 args.hashval = xfs_da_hashname(name, namelen);
435 args.firstblock = NULL;
438 args.whichfork = XFS_DATA_FORK;
440 args.justcheck = args.addname = 0;
444 * Decide on what work routines to call based on the inode size.
446 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
447 retval = xfs_dir_shortform_lookup(&args);
448 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
449 retval = xfs_dir_leaf_lookup(&args);
451 retval = xfs_dir_node_lookup(&args);
453 if (retval == EEXIST)
455 *inum = args.inumber;
462 static int /* error */
463 xfs_dir_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio, int *eofp)
466 int alignment, retval;
469 XFS_STATS_INC(xs_dir_getdents);
470 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
473 * If our caller has given us a single contiguous memory buffer,
474 * just work directly within that buffer. If it's in user memory,
475 * lock it down first.
477 alignment = sizeof(xfs_off_t) - 1;
478 if ((uio->uio_iovcnt == 1) &&
479 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
480 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
482 put = xfs_dir_put_dirent64_direct;
484 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
485 put = xfs_dir_put_dirent64_uio;
489 * Decide on what work routines to call based on the inode size.
493 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
494 retval = xfs_dir_shortform_getdents(dp, uio, eofp, dbp, put);
495 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
496 retval = xfs_dir_leaf_getdents(trans, dp, uio, eofp, dbp, put);
498 retval = xfs_dir_node_getdents(trans, dp, uio, eofp, dbp, put);
501 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
506 static int /* error */
507 xfs_dir_replace(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
508 xfs_ino_t inum, xfs_fsblock_t *firstblock,
509 xfs_bmap_free_t *flist, xfs_extlen_t total)
514 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
516 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
520 * Fill in the arg structure for this request.
523 args.namelen = namelen;
524 args.hashval = xfs_da_hashname(name, namelen);
527 args.firstblock = firstblock;
530 args.whichfork = XFS_DATA_FORK;
532 args.justcheck = args.addname = args.oknoent = 0;
535 * Decide on what work routines to call based on the inode size.
537 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
538 retval = xfs_dir_shortform_replace(&args);
539 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
540 retval = xfs_dir_leaf_replace(&args);
542 retval = xfs_dir_node_replace(&args);
549 xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp, xfs_dinode_t *dp)
554 xfs_dir_shortform_t *sf;
555 xfs_dir_sf_entry_t *sfe;
560 if ((INT_GET(dp->di_core.di_mode, ARCH_CONVERT) & S_IFMT) != S_IFDIR) {
563 if (INT_GET(dp->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_LOCAL) {
566 if (INT_GET(dp->di_core.di_size, ARCH_CONVERT) < sizeof(sf->hdr)) {
567 xfs_fs_cmn_err(CE_WARN, mp, "Invalid shortform size: dp 0x%p",
571 sf = (xfs_dir_shortform_t *)(&dp->di_u.di_dirsf);
572 ino = XFS_GET_DIR_INO8(sf->hdr.parent);
573 if (xfs_dir_ino_validate(mp, ino))
576 count = sf->hdr.count;
577 if ((count < 0) || ((count * 10) > XFS_LITINO(mp))) {
578 xfs_fs_cmn_err(CE_WARN, mp,
579 "Invalid shortform count: dp 0x%p", dp);
589 for (i = sf->hdr.count - 1; i >= 0; i--) {
590 ino = XFS_GET_DIR_INO8(sfe->inumber);
591 xfs_dir_ino_validate(mp, ino);
592 if (sfe->namelen >= XFS_LITINO(mp)) {
593 xfs_fs_cmn_err(CE_WARN, mp,
594 "Invalid shortform namelen: dp 0x%p", dp);
597 namelen_sum += sfe->namelen;
598 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
600 if (namelen_sum >= XFS_LITINO(mp)) {
601 xfs_fs_cmn_err(CE_WARN, mp,
602 "Invalid shortform namelen: dp 0x%p", dp);
609 /*========================================================================
610 * External routines when dirsize == XFS_LBSIZE(dp->i_mount).
611 *========================================================================*/
614 * Add a name to the leaf directory structure
615 * This is the external routine.
618 xfs_dir_leaf_addname(xfs_da_args_t *args)
623 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
629 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
630 if (retval == ENOENT)
631 retval = xfs_dir_leaf_add(bp, args, index);
637 * Remove a name from the leaf directory structure
638 * This is the external routine.
641 xfs_dir_leaf_removename(xfs_da_args_t *args, int *count, int *totallen)
643 xfs_dir_leafblock_t *leaf;
647 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
653 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
654 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
655 if (retval == EEXIST) {
656 (void)xfs_dir_leaf_remove(args->trans, bp, index);
657 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
658 *totallen = INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
666 * Look up a name in a leaf directory structure.
667 * This is the external routine.
670 xfs_dir_leaf_lookup(xfs_da_args_t *args)
675 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
680 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
681 xfs_da_brelse(args->trans, bp);
686 * Copy out directory entries for getdents(), for leaf directories.
689 xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
690 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
695 retval = xfs_da_read_buf(dp->i_transp, dp, 0, -1, &bp, XFS_DATA_FORK);
699 retval = xfs_dir_leaf_getdents_int(bp, dp, 0, uio, &eob, dbp, put, -1);
700 xfs_da_brelse(trans, bp);
706 * Look up a name in a leaf directory structure, replace the inode number.
707 * This is the external routine.
710 xfs_dir_leaf_replace(xfs_da_args_t *args)
715 xfs_dir_leafblock_t *leaf;
716 xfs_dir_leaf_entry_t *entry;
717 xfs_dir_leaf_name_t *namest;
719 inum = args->inumber;
720 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
725 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
726 if (retval == EEXIST) {
728 entry = &leaf->entries[index];
729 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
730 /* XXX - replace assert? */
731 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
732 xfs_da_log_buf(args->trans, bp,
733 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
737 xfs_da_brelse(args->trans, bp);
742 /*========================================================================
743 * External routines when dirsize > XFS_LBSIZE(mp).
744 *========================================================================*/
747 * Add a name to a Btree-format directory.
749 * This will involve walking down the Btree, and may involve splitting
750 * leaf nodes and even splitting intermediate nodes up to and including
751 * the root node (a special case of an intermediate node).
754 xfs_dir_node_addname(xfs_da_args_t *args)
756 xfs_da_state_t *state;
757 xfs_da_state_blk_t *blk;
761 * Fill in bucket of arguments/results/context to carry around.
763 state = xfs_da_state_alloc();
765 state->mp = args->dp->i_mount;
766 state->blocksize = state->mp->m_sb.sb_blocksize;
767 state->node_ents = state->mp->m_dir_node_ents;
770 * Search to see if name already exists, and get back a pointer
771 * to where it should go.
773 error = xfs_da_node_lookup_int(state, &retval);
776 if (retval != ENOENT)
778 blk = &state->path.blk[ state->path.active-1 ];
779 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
780 retval = xfs_dir_leaf_add(blk->bp, args, blk->index);
783 * Addition succeeded, update Btree hashvals.
785 if (!args->justcheck)
786 xfs_da_fixhashpath(state, &state->path);
789 * Addition failed, split as many Btree elements as required.
791 if (args->total == 0) {
792 ASSERT(retval == ENOSPC);
795 retval = xfs_da_split(state);
798 xfs_da_state_free(state);
804 * Remove a name from a B-tree directory.
806 * This will involve walking down the Btree, and may involve joining
807 * leaf nodes and even joining intermediate nodes up to and including
808 * the root node (a special case of an intermediate node).
811 xfs_dir_node_removename(xfs_da_args_t *args)
813 xfs_da_state_t *state;
814 xfs_da_state_blk_t *blk;
817 state = xfs_da_state_alloc();
819 state->mp = args->dp->i_mount;
820 state->blocksize = state->mp->m_sb.sb_blocksize;
821 state->node_ents = state->mp->m_dir_node_ents;
824 * Search to see if name exists, and get back a pointer to it.
826 error = xfs_da_node_lookup_int(state, &retval);
829 if (retval != EEXIST) {
830 xfs_da_state_free(state);
835 * Remove the name and update the hashvals in the tree.
837 blk = &state->path.blk[ state->path.active-1 ];
838 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
839 retval = xfs_dir_leaf_remove(args->trans, blk->bp, blk->index);
840 xfs_da_fixhashpath(state, &state->path);
843 * Check to see if the tree needs to be collapsed.
847 error = xfs_da_join(state);
850 xfs_da_state_free(state);
857 * Look up a filename in a int directory.
858 * Use an internal routine to actually do all the work.
861 xfs_dir_node_lookup(xfs_da_args_t *args)
863 xfs_da_state_t *state;
864 int retval, error, i;
866 state = xfs_da_state_alloc();
868 state->mp = args->dp->i_mount;
869 state->blocksize = state->mp->m_sb.sb_blocksize;
870 state->node_ents = state->mp->m_dir_node_ents;
873 * Search to see if name exists,
874 * and get back a pointer to it.
876 error = xfs_da_node_lookup_int(state, &retval);
882 * If not in a transaction, we have to release all the buffers.
884 for (i = 0; i < state->path.active; i++) {
885 xfs_da_brelse(args->trans, state->path.blk[i].bp);
886 state->path.blk[i].bp = NULL;
889 xfs_da_state_free(state);
894 xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
895 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
897 xfs_da_intnode_t *node;
898 xfs_da_node_entry_t *btree;
899 xfs_dir_leafblock_t *leaf = NULL;
900 xfs_dablk_t bno, nextbno;
901 xfs_dahash_t cookhash;
908 * Pick up our context.
912 bno = XFS_DA_COOKIE_BNO(mp, uio->uio_offset);
913 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
915 xfs_dir_trace_g_du("node: start", dp, uio);
918 * Re-find our place, even if we're confused about what our place is.
920 * First we check the block number from the magic cookie, it is a
921 * cache of where we ended last time. If we find a leaf block, and
922 * the starting hashval in that block is less than our desired
923 * hashval, then we run with it.
926 error = xfs_da_read_buf(trans, dp, bno, -2, &bp, XFS_DATA_FORK);
927 if ((error != 0) && (error != EFSCORRUPTED))
931 if (bp && INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC) {
932 xfs_dir_trace_g_dub("node: block not a leaf",
934 xfs_da_brelse(trans, bp);
937 if (bp && INT_GET(leaf->entries[0].hashval, ARCH_CONVERT) > cookhash) {
938 xfs_dir_trace_g_dub("node: leaf hash too large",
940 xfs_da_brelse(trans, bp);
944 cookhash > INT_GET(leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)) {
945 xfs_dir_trace_g_dub("node: leaf hash too small",
947 xfs_da_brelse(trans, bp);
953 * If we did not find a leaf block from the blockno in the cookie,
954 * or we there was no blockno in the cookie (eg: first time thru),
955 * the we start at the top of the Btree and re-find our hashval.
958 xfs_dir_trace_g_du("node: start at root" , dp, uio);
961 error = xfs_da_read_buf(trans, dp, bno, -1, &bp,
966 return(XFS_ERROR(EFSCORRUPTED));
968 if (INT_GET(node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC)
970 btree = &node->btree[0];
971 xfs_dir_trace_g_dun("node: node detail", dp, uio, node);
972 for (i = 0; i < INT_GET(node->hdr.count, ARCH_CONVERT); btree++, i++) {
973 if (INT_GET(btree->hashval, ARCH_CONVERT) >= cookhash) {
974 bno = INT_GET(btree->before, ARCH_CONVERT);
978 if (i == INT_GET(node->hdr.count, ARCH_CONVERT)) {
979 xfs_da_brelse(trans, bp);
980 xfs_dir_trace_g_du("node: hash beyond EOF",
982 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0,
987 xfs_dir_trace_g_dub("node: going to block",
989 xfs_da_brelse(trans, bp);
992 ASSERT(cookhash != XFS_DA_MAXHASH);
995 * We've dropped down to the (first) leaf block that contains the
996 * hashval we are interested in. Continue rolling upward thru the
997 * leaf blocks until we fill up our buffer.
1001 if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC)) {
1002 xfs_dir_trace_g_dul("node: not a leaf", dp, uio, leaf);
1003 xfs_da_brelse(trans, bp);
1004 XFS_CORRUPTION_ERROR("xfs_dir_node_getdents(1)",
1005 XFS_ERRLEVEL_LOW, mp, leaf);
1006 return XFS_ERROR(EFSCORRUPTED);
1008 xfs_dir_trace_g_dul("node: leaf detail", dp, uio, leaf);
1009 if ((nextbno = INT_GET(leaf->hdr.info.forw, ARCH_CONVERT))) {
1010 nextda = xfs_da_reada_buf(trans, dp, nextbno,
1014 error = xfs_dir_leaf_getdents_int(bp, dp, bno, uio, &eob, dbp,
1016 xfs_da_brelse(trans, bp);
1019 xfs_dir_trace_g_dub("node: E-O-B", dp, uio, bno);
1025 error = xfs_da_read_buf(trans, dp, bno, nextda, &bp,
1029 if (unlikely(bp == NULL)) {
1030 XFS_ERROR_REPORT("xfs_dir_node_getdents(2)",
1031 XFS_ERRLEVEL_LOW, mp);
1032 return(XFS_ERROR(EFSCORRUPTED));
1036 xfs_dir_trace_g_du("node: E-O-F", dp, uio);
1041 * Look up a filename in an int directory, replace the inode number.
1042 * Use an internal routine to actually do the lookup.
1045 xfs_dir_node_replace(xfs_da_args_t *args)
1047 xfs_da_state_t *state;
1048 xfs_da_state_blk_t *blk;
1049 xfs_dir_leafblock_t *leaf;
1050 xfs_dir_leaf_entry_t *entry;
1051 xfs_dir_leaf_name_t *namest;
1053 int retval, error, i;
1056 state = xfs_da_state_alloc();
1058 state->mp = args->dp->i_mount;
1059 state->blocksize = state->mp->m_sb.sb_blocksize;
1060 state->node_ents = state->mp->m_dir_node_ents;
1061 inum = args->inumber;
1064 * Search to see if name exists,
1065 * and get back a pointer to it.
1067 error = xfs_da_node_lookup_int(state, &retval);
1072 if (retval == EEXIST) {
1073 blk = &state->path.blk[state->path.active - 1];
1074 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
1077 entry = &leaf->entries[blk->index];
1078 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1079 /* XXX - replace assert ? */
1080 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
1081 xfs_da_log_buf(args->trans, bp,
1082 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
1083 xfs_da_buf_done(bp);
1087 i = state->path.active - 1;
1088 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1089 state->path.blk[i].bp = NULL;
1091 for (i = 0; i < state->path.active - 1; i++) {
1092 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1093 state->path.blk[i].bp = NULL;
1096 xfs_da_state_free(state);
1100 #if defined(XFS_DIR_TRACE)
1102 * Add a trace buffer entry for an inode and a uio.
1105 xfs_dir_trace_g_du(char *where, xfs_inode_t *dp, uio_t *uio)
1107 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DU, where,
1108 (void *)dp, (void *)dp->i_mount,
1109 (void *)((unsigned long)(uio->uio_offset >> 32)),
1110 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1111 (void *)(unsigned long)uio->uio_resid,
1112 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1116 * Add a trace buffer entry for an inode and a uio.
1119 xfs_dir_trace_g_dub(char *where, xfs_inode_t *dp, uio_t *uio, xfs_dablk_t bno)
1121 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUB, where,
1122 (void *)dp, (void *)dp->i_mount,
1123 (void *)((unsigned long)(uio->uio_offset >> 32)),
1124 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1125 (void *)(unsigned long)uio->uio_resid,
1126 (void *)(unsigned long)bno,
1127 NULL, NULL, NULL, NULL, NULL, NULL);
1131 * Add a trace buffer entry for an inode and a uio.
1134 xfs_dir_trace_g_dun(char *where, xfs_inode_t *dp, uio_t *uio,
1135 xfs_da_intnode_t *node)
1137 int last = INT_GET(node->hdr.count, ARCH_CONVERT) - 1;
1139 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUN, where,
1140 (void *)dp, (void *)dp->i_mount,
1141 (void *)((unsigned long)(uio->uio_offset >> 32)),
1142 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1143 (void *)(unsigned long)uio->uio_resid,
1144 (void *)(unsigned long)
1145 INT_GET(node->hdr.info.forw, ARCH_CONVERT),
1146 (void *)(unsigned long)
1147 INT_GET(node->hdr.count, ARCH_CONVERT),
1148 (void *)(unsigned long)
1149 INT_GET(node->btree[0].hashval, ARCH_CONVERT),
1150 (void *)(unsigned long)
1151 INT_GET(node->btree[last].hashval, ARCH_CONVERT),
1156 * Add a trace buffer entry for an inode and a uio.
1159 xfs_dir_trace_g_dul(char *where, xfs_inode_t *dp, uio_t *uio,
1160 xfs_dir_leafblock_t *leaf)
1162 int last = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
1164 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUL, where,
1165 (void *)dp, (void *)dp->i_mount,
1166 (void *)((unsigned long)(uio->uio_offset >> 32)),
1167 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1168 (void *)(unsigned long)uio->uio_resid,
1169 (void *)(unsigned long)
1170 INT_GET(leaf->hdr.info.forw, ARCH_CONVERT),
1171 (void *)(unsigned long)
1172 INT_GET(leaf->hdr.count, ARCH_CONVERT),
1173 (void *)(unsigned long)
1174 INT_GET(leaf->entries[0].hashval, ARCH_CONVERT),
1175 (void *)(unsigned long)
1176 INT_GET(leaf->entries[last].hashval, ARCH_CONVERT),
1181 * Add a trace buffer entry for an inode and a uio.
1184 xfs_dir_trace_g_due(char *where, xfs_inode_t *dp, uio_t *uio,
1185 xfs_dir_leaf_entry_t *entry)
1187 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUE, where,
1188 (void *)dp, (void *)dp->i_mount,
1189 (void *)((unsigned long)(uio->uio_offset >> 32)),
1190 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1191 (void *)(unsigned long)uio->uio_resid,
1192 (void *)(unsigned long)
1193 INT_GET(entry->hashval, ARCH_CONVERT),
1194 NULL, NULL, NULL, NULL, NULL, NULL);
1198 * Add a trace buffer entry for an inode and a uio.
1201 xfs_dir_trace_g_duc(char *where, xfs_inode_t *dp, uio_t *uio, xfs_off_t cookie)
1203 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUC, where,
1204 (void *)dp, (void *)dp->i_mount,
1205 (void *)((unsigned long)(uio->uio_offset >> 32)),
1206 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1207 (void *)(unsigned long)uio->uio_resid,
1208 (void *)((unsigned long)(cookie >> 32)),
1209 (void *)((unsigned long)(cookie & 0xFFFFFFFF)),
1210 NULL, NULL, NULL, NULL, NULL);
1214 * Add a trace buffer entry for the arguments given to the routine,
1218 xfs_dir_trace_enter(int type, char *where,
1219 void * a0, void * a1,
1220 void * a2, void * a3,
1221 void * a4, void * a5,
1222 void * a6, void * a7,
1223 void * a8, void * a9,
1224 void * a10, void * a11)
1226 ASSERT(xfs_dir_trace_buf);
1227 ktrace_enter(xfs_dir_trace_buf, (void *)(unsigned long)type,
1229 (void *)a0, (void *)a1, (void *)a2,
1230 (void *)a3, (void *)a4, (void *)a5,
1231 (void *)a6, (void *)a7, (void *)a8,
1232 (void *)a9, (void *)a10, (void *)a11,
1235 #endif /* XFS_DIR_TRACE */