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/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_ialloc_btree.h"
47 #include "xfs_alloc.h"
48 #include "xfs_btree.h"
49 #include "xfs_dir_sf.h"
50 #include "xfs_dir2_sf.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dinode.h"
53 #include "xfs_inode.h"
55 #include "xfs_dir_leaf.h"
56 #include "xfs_error.h"
61 * Provide the external interfaces to manage directories.
64 /*========================================================================
65 * Function prototypes for the kernel.
66 *========================================================================*/
69 * Functions for the dirops interfaces.
71 static void xfs_dir_mount(struct xfs_mount *mp);
73 static int xfs_dir_isempty(struct xfs_inode *dp);
75 static int xfs_dir_init(struct xfs_trans *trans,
76 struct xfs_inode *dir,
77 struct xfs_inode *parent_dir);
79 static int xfs_dir_createname(struct xfs_trans *trans,
83 xfs_ino_t inode_number,
84 xfs_fsblock_t *firstblock,
85 xfs_bmap_free_t *flist,
88 static int xfs_dir_lookup(struct xfs_trans *tp,
92 xfs_ino_t *inode_number);
94 static int xfs_dir_removename(struct xfs_trans *trans,
99 xfs_fsblock_t *firstblock,
100 xfs_bmap_free_t *flist,
103 static int xfs_dir_getdents(struct xfs_trans *tp,
104 struct xfs_inode *dp,
108 static int xfs_dir_replace(struct xfs_trans *tp,
109 struct xfs_inode *dp,
112 xfs_ino_t inode_number,
113 xfs_fsblock_t *firstblock,
114 xfs_bmap_free_t *flist,
117 static int xfs_dir_canenter(struct xfs_trans *tp,
118 struct xfs_inode *dp,
122 static int xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp,
125 xfs_dirops_t xfsv1_dirops = {
126 .xd_mount = xfs_dir_mount,
127 .xd_isempty = xfs_dir_isempty,
128 .xd_init = xfs_dir_init,
129 .xd_createname = xfs_dir_createname,
130 .xd_lookup = xfs_dir_lookup,
131 .xd_removename = xfs_dir_removename,
132 .xd_getdents = xfs_dir_getdents,
133 .xd_replace = xfs_dir_replace,
134 .xd_canenter = xfs_dir_canenter,
135 .xd_shortform_validate_ondisk = xfs_dir_shortform_validate_ondisk,
136 .xd_shortform_to_single = xfs_dir_shortform_to_leaf,
140 * Internal routines when dirsize == XFS_LBSIZE(mp).
142 STATIC int xfs_dir_leaf_lookup(xfs_da_args_t *args);
143 STATIC int xfs_dir_leaf_removename(xfs_da_args_t *args, int *number_entries,
144 int *total_namebytes);
145 STATIC int xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
146 uio_t *uio, int *eofp,
149 STATIC int xfs_dir_leaf_replace(xfs_da_args_t *args);
152 * Internal routines when dirsize > XFS_LBSIZE(mp).
154 STATIC int xfs_dir_node_addname(xfs_da_args_t *args);
155 STATIC int xfs_dir_node_lookup(xfs_da_args_t *args);
156 STATIC int xfs_dir_node_removename(xfs_da_args_t *args);
157 STATIC int xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
158 uio_t *uio, int *eofp,
161 STATIC int xfs_dir_node_replace(xfs_da_args_t *args);
163 #if defined(XFS_DIR_TRACE)
164 ktrace_t *xfs_dir_trace_buf;
168 /*========================================================================
169 * Overall external interface routines.
170 *========================================================================*/
172 xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
175 * One-time startup routine called from xfs_init().
178 xfs_dir_startup(void)
180 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
181 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
185 * Initialize directory-related fields in the mount structure.
188 xfs_dir_mount(xfs_mount_t *mp)
190 uint shortcount, leafcount, count;
192 mp->m_dirversion = 1;
193 if (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) {
194 shortcount = (mp->m_attroffset -
195 (uint)sizeof(xfs_dir_sf_hdr_t)) /
196 (uint)sizeof(xfs_dir_sf_entry_t);
197 leafcount = (XFS_LBSIZE(mp) -
198 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
199 ((uint)sizeof(xfs_dir_leaf_entry_t) +
200 (uint)sizeof(xfs_dir_leaf_name_t));
202 shortcount = (XFS_BMDR_SPACE_CALC(MINABTPTRS) -
203 (uint)sizeof(xfs_dir_sf_hdr_t)) /
204 (uint)sizeof(xfs_dir_sf_entry_t);
205 leafcount = (XFS_LBSIZE(mp) -
206 (uint)sizeof(xfs_dir_leaf_hdr_t)) /
207 ((uint)sizeof(xfs_dir_leaf_entry_t) +
208 (uint)sizeof(xfs_dir_leaf_name_t));
210 count = shortcount > leafcount ? shortcount : leafcount;
211 mp->m_dircook_elog = xfs_da_log2_roundup(count + 1);
212 ASSERT(mp->m_dircook_elog <= mp->m_sb.sb_blocklog);
213 mp->m_dir_node_ents = mp->m_attr_node_ents =
214 (XFS_LBSIZE(mp) - (uint)sizeof(xfs_da_node_hdr_t)) /
215 (uint)sizeof(xfs_da_node_entry_t);
216 mp->m_dir_magicpct = (XFS_LBSIZE(mp) * 37) / 100;
217 mp->m_dirblksize = mp->m_sb.sb_blocksize;
218 mp->m_dirblkfsbs = 1;
222 * Return 1 if directory contains only "." and "..".
225 xfs_dir_isempty(xfs_inode_t *dp)
227 xfs_dir_sf_hdr_t *hdr;
229 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
230 if (dp->i_d.di_size == 0)
232 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
234 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
235 return(hdr->count == 0);
239 * Initialize a directory with its "." and ".." entries.
242 xfs_dir_init(xfs_trans_t *trans, xfs_inode_t *dir, xfs_inode_t *parent_dir)
247 memset((char *)&args, 0, sizeof(args));
251 ASSERT((dir->i_d.di_mode & S_IFMT) == S_IFDIR);
252 if ((error = xfs_dir_ino_validate(trans->t_mountp, parent_dir->i_ino)))
255 return(xfs_dir_shortform_create(&args, parent_dir->i_ino));
259 * Generic handler routine to add a name to a directory.
260 * Transitions directory from shortform to Btree as necessary.
262 static int /* error */
263 xfs_dir_createname(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
264 int namelen, xfs_ino_t inum, xfs_fsblock_t *firstblock,
265 xfs_bmap_free_t *flist, xfs_extlen_t total)
268 int retval, newsize, done;
270 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
272 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
275 XFS_STATS_INC(xs_dir_create);
277 * Fill in the arg structure for this request.
280 args.namelen = namelen;
281 args.hashval = xfs_da_hashname(name, namelen);
284 args.firstblock = firstblock;
287 args.whichfork = XFS_DATA_FORK;
290 args.addname = args.oknoent = 1;
293 * Decide on what work routines to call based on the inode size.
296 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
297 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
298 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp)) {
299 retval = xfs_dir_shortform_addname(&args);
303 return XFS_ERROR(ENOSPC);
304 retval = xfs_dir_shortform_to_leaf(&args);
308 if (!done && xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
309 retval = xfs_dir_leaf_addname(&args);
310 done = retval != ENOSPC;
313 return XFS_ERROR(ENOSPC);
314 retval = xfs_dir_leaf_to_node(&args);
319 retval = xfs_dir_node_addname(&args);
325 * Generic handler routine to check if a name can be added to a directory,
326 * without adding any blocks to the directory.
328 static int /* error */
329 xfs_dir_canenter(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen)
334 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
336 * Fill in the arg structure for this request.
339 args.namelen = namelen;
340 args.hashval = xfs_da_hashname(name, namelen);
343 args.firstblock = NULL;
346 args.whichfork = XFS_DATA_FORK;
348 args.justcheck = args.addname = args.oknoent = 1;
351 * Decide on what work routines to call based on the inode size.
353 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
354 newsize = XFS_DIR_SF_ENTSIZE_BYNAME(args.namelen);
355 if ((dp->i_d.di_size + newsize) <= XFS_IFORK_DSIZE(dp))
358 retval = XFS_ERROR(ENOSPC);
359 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
360 retval = xfs_dir_leaf_addname(&args);
362 retval = xfs_dir_node_addname(&args);
368 * Generic handler routine to remove a name from a directory.
369 * Transitions directory from Btree to shortform as necessary.
371 static int /* error */
372 xfs_dir_removename(xfs_trans_t *trans, xfs_inode_t *dp, char *name,
373 int namelen, xfs_ino_t ino, xfs_fsblock_t *firstblock,
374 xfs_bmap_free_t *flist, xfs_extlen_t total)
377 int count, totallen, newsize, retval;
379 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
380 XFS_STATS_INC(xs_dir_remove);
382 * Fill in the arg structure for this request.
385 args.namelen = namelen;
386 args.hashval = xfs_da_hashname(name, namelen);
389 args.firstblock = firstblock;
392 args.whichfork = XFS_DATA_FORK;
394 args.justcheck = args.addname = args.oknoent = 0;
397 * Decide on what work routines to call based on the inode size.
399 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
400 retval = xfs_dir_shortform_removename(&args);
401 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
402 retval = xfs_dir_leaf_removename(&args, &count, &totallen);
404 newsize = XFS_DIR_SF_ALLFIT(count, totallen);
405 if (newsize <= XFS_IFORK_DSIZE(dp)) {
406 retval = xfs_dir_leaf_to_shortform(&args);
410 retval = xfs_dir_node_removename(&args);
415 static int /* error */
416 xfs_dir_lookup(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
422 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
424 XFS_STATS_INC(xs_dir_lookup);
426 * Fill in the arg structure for this request.
429 args.namelen = namelen;
430 args.hashval = xfs_da_hashname(name, namelen);
433 args.firstblock = NULL;
436 args.whichfork = XFS_DATA_FORK;
438 args.justcheck = args.addname = 0;
442 * Decide on what work routines to call based on the inode size.
444 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
445 retval = xfs_dir_shortform_lookup(&args);
446 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
447 retval = xfs_dir_leaf_lookup(&args);
449 retval = xfs_dir_node_lookup(&args);
451 if (retval == EEXIST)
453 *inum = args.inumber;
460 static int /* error */
461 xfs_dir_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio, int *eofp)
464 int alignment, retval;
467 XFS_STATS_INC(xs_dir_getdents);
468 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
471 * If our caller has given us a single contiguous memory buffer,
472 * just work directly within that buffer. If it's in user memory,
473 * lock it down first.
475 alignment = sizeof(xfs_off_t) - 1;
476 if ((uio->uio_iovcnt == 1) &&
477 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
478 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
480 put = xfs_dir_put_dirent64_direct;
482 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
483 put = xfs_dir_put_dirent64_uio;
487 * Decide on what work routines to call based on the inode size.
491 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
492 retval = xfs_dir_shortform_getdents(dp, uio, eofp, dbp, put);
493 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
494 retval = xfs_dir_leaf_getdents(trans, dp, uio, eofp, dbp, put);
496 retval = xfs_dir_node_getdents(trans, dp, uio, eofp, dbp, put);
499 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
504 static int /* error */
505 xfs_dir_replace(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
506 xfs_ino_t inum, xfs_fsblock_t *firstblock,
507 xfs_bmap_free_t *flist, xfs_extlen_t total)
512 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
514 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
518 * Fill in the arg structure for this request.
521 args.namelen = namelen;
522 args.hashval = xfs_da_hashname(name, namelen);
525 args.firstblock = firstblock;
528 args.whichfork = XFS_DATA_FORK;
530 args.justcheck = args.addname = args.oknoent = 0;
533 * Decide on what work routines to call based on the inode size.
535 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
536 retval = xfs_dir_shortform_replace(&args);
537 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
538 retval = xfs_dir_leaf_replace(&args);
540 retval = xfs_dir_node_replace(&args);
547 xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp, xfs_dinode_t *dp)
552 xfs_dir_shortform_t *sf;
553 xfs_dir_sf_entry_t *sfe;
558 if ((INT_GET(dp->di_core.di_mode, ARCH_CONVERT) & S_IFMT) != S_IFDIR) {
561 if (INT_GET(dp->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_LOCAL) {
564 if (INT_GET(dp->di_core.di_size, ARCH_CONVERT) < sizeof(sf->hdr)) {
565 xfs_fs_cmn_err(CE_WARN, mp, "Invalid shortform size: dp 0x%p",
569 sf = (xfs_dir_shortform_t *)(&dp->di_u.di_dirsf);
570 ino = XFS_GET_DIR_INO8(sf->hdr.parent);
571 if (xfs_dir_ino_validate(mp, ino))
574 count = sf->hdr.count;
575 if ((count < 0) || ((count * 10) > XFS_LITINO(mp))) {
576 xfs_fs_cmn_err(CE_WARN, mp,
577 "Invalid shortform count: dp 0x%p", dp);
587 for (i = sf->hdr.count - 1; i >= 0; i--) {
588 ino = XFS_GET_DIR_INO8(sfe->inumber);
589 xfs_dir_ino_validate(mp, ino);
590 if (sfe->namelen >= XFS_LITINO(mp)) {
591 xfs_fs_cmn_err(CE_WARN, mp,
592 "Invalid shortform namelen: dp 0x%p", dp);
595 namelen_sum += sfe->namelen;
596 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
598 if (namelen_sum >= XFS_LITINO(mp)) {
599 xfs_fs_cmn_err(CE_WARN, mp,
600 "Invalid shortform namelen: dp 0x%p", dp);
607 /*========================================================================
608 * External routines when dirsize == XFS_LBSIZE(dp->i_mount).
609 *========================================================================*/
612 * Add a name to the leaf directory structure
613 * This is the external routine.
616 xfs_dir_leaf_addname(xfs_da_args_t *args)
621 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
627 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
628 if (retval == ENOENT)
629 retval = xfs_dir_leaf_add(bp, args, index);
635 * Remove a name from the leaf directory structure
636 * This is the external routine.
639 xfs_dir_leaf_removename(xfs_da_args_t *args, int *count, int *totallen)
641 xfs_dir_leafblock_t *leaf;
645 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
651 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
652 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
653 if (retval == EEXIST) {
654 (void)xfs_dir_leaf_remove(args->trans, bp, index);
655 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
656 *totallen = INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
664 * Look up a name in a leaf directory structure.
665 * This is the external routine.
668 xfs_dir_leaf_lookup(xfs_da_args_t *args)
673 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
678 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
679 xfs_da_brelse(args->trans, bp);
684 * Copy out directory entries for getdents(), for leaf directories.
687 xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
688 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
693 retval = xfs_da_read_buf(dp->i_transp, dp, 0, -1, &bp, XFS_DATA_FORK);
697 retval = xfs_dir_leaf_getdents_int(bp, dp, 0, uio, &eob, dbp, put, -1);
698 xfs_da_brelse(trans, bp);
704 * Look up a name in a leaf directory structure, replace the inode number.
705 * This is the external routine.
708 xfs_dir_leaf_replace(xfs_da_args_t *args)
713 xfs_dir_leafblock_t *leaf;
714 xfs_dir_leaf_entry_t *entry;
715 xfs_dir_leaf_name_t *namest;
717 inum = args->inumber;
718 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
723 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
724 if (retval == EEXIST) {
726 entry = &leaf->entries[index];
727 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
728 /* XXX - replace assert? */
729 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
730 xfs_da_log_buf(args->trans, bp,
731 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
735 xfs_da_brelse(args->trans, bp);
740 /*========================================================================
741 * External routines when dirsize > XFS_LBSIZE(mp).
742 *========================================================================*/
745 * Add a name to a Btree-format directory.
747 * This will involve walking down the Btree, and may involve splitting
748 * leaf nodes and even splitting intermediate nodes up to and including
749 * the root node (a special case of an intermediate node).
752 xfs_dir_node_addname(xfs_da_args_t *args)
754 xfs_da_state_t *state;
755 xfs_da_state_blk_t *blk;
759 * Fill in bucket of arguments/results/context to carry around.
761 state = xfs_da_state_alloc();
763 state->mp = args->dp->i_mount;
764 state->blocksize = state->mp->m_sb.sb_blocksize;
765 state->node_ents = state->mp->m_dir_node_ents;
768 * Search to see if name already exists, and get back a pointer
769 * to where it should go.
771 error = xfs_da_node_lookup_int(state, &retval);
774 if (retval != ENOENT)
776 blk = &state->path.blk[ state->path.active-1 ];
777 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
778 retval = xfs_dir_leaf_add(blk->bp, args, blk->index);
781 * Addition succeeded, update Btree hashvals.
783 if (!args->justcheck)
784 xfs_da_fixhashpath(state, &state->path);
787 * Addition failed, split as many Btree elements as required.
789 if (args->total == 0) {
790 ASSERT(retval == ENOSPC);
793 retval = xfs_da_split(state);
796 xfs_da_state_free(state);
802 * Remove a name from a B-tree directory.
804 * This will involve walking down the Btree, and may involve joining
805 * leaf nodes and even joining intermediate nodes up to and including
806 * the root node (a special case of an intermediate node).
809 xfs_dir_node_removename(xfs_da_args_t *args)
811 xfs_da_state_t *state;
812 xfs_da_state_blk_t *blk;
815 state = xfs_da_state_alloc();
817 state->mp = args->dp->i_mount;
818 state->blocksize = state->mp->m_sb.sb_blocksize;
819 state->node_ents = state->mp->m_dir_node_ents;
822 * Search to see if name exists, and get back a pointer to it.
824 error = xfs_da_node_lookup_int(state, &retval);
827 if (retval != EEXIST) {
828 xfs_da_state_free(state);
833 * Remove the name and update the hashvals in the tree.
835 blk = &state->path.blk[ state->path.active-1 ];
836 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
837 retval = xfs_dir_leaf_remove(args->trans, blk->bp, blk->index);
838 xfs_da_fixhashpath(state, &state->path);
841 * Check to see if the tree needs to be collapsed.
845 error = xfs_da_join(state);
848 xfs_da_state_free(state);
855 * Look up a filename in a int directory.
856 * Use an internal routine to actually do all the work.
859 xfs_dir_node_lookup(xfs_da_args_t *args)
861 xfs_da_state_t *state;
862 int retval, error, i;
864 state = xfs_da_state_alloc();
866 state->mp = args->dp->i_mount;
867 state->blocksize = state->mp->m_sb.sb_blocksize;
868 state->node_ents = state->mp->m_dir_node_ents;
871 * Search to see if name exists,
872 * and get back a pointer to it.
874 error = xfs_da_node_lookup_int(state, &retval);
880 * If not in a transaction, we have to release all the buffers.
882 for (i = 0; i < state->path.active; i++) {
883 xfs_da_brelse(args->trans, state->path.blk[i].bp);
884 state->path.blk[i].bp = NULL;
887 xfs_da_state_free(state);
892 xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio,
893 int *eofp, xfs_dirent_t *dbp, xfs_dir_put_t put)
895 xfs_da_intnode_t *node;
896 xfs_da_node_entry_t *btree;
897 xfs_dir_leafblock_t *leaf = NULL;
898 xfs_dablk_t bno, nextbno;
899 xfs_dahash_t cookhash;
906 * Pick up our context.
910 bno = XFS_DA_COOKIE_BNO(mp, uio->uio_offset);
911 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
913 xfs_dir_trace_g_du("node: start", dp, uio);
916 * Re-find our place, even if we're confused about what our place is.
918 * First we check the block number from the magic cookie, it is a
919 * cache of where we ended last time. If we find a leaf block, and
920 * the starting hashval in that block is less than our desired
921 * hashval, then we run with it.
924 error = xfs_da_read_buf(trans, dp, bno, -2, &bp, XFS_DATA_FORK);
925 if ((error != 0) && (error != EFSCORRUPTED))
929 if (bp && INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC) {
930 xfs_dir_trace_g_dub("node: block not a leaf",
932 xfs_da_brelse(trans, bp);
935 if (bp && INT_GET(leaf->entries[0].hashval, ARCH_CONVERT) > cookhash) {
936 xfs_dir_trace_g_dub("node: leaf hash too large",
938 xfs_da_brelse(trans, bp);
942 cookhash > INT_GET(leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)) {
943 xfs_dir_trace_g_dub("node: leaf hash too small",
945 xfs_da_brelse(trans, bp);
951 * If we did not find a leaf block from the blockno in the cookie,
952 * or we there was no blockno in the cookie (eg: first time thru),
953 * the we start at the top of the Btree and re-find our hashval.
956 xfs_dir_trace_g_du("node: start at root" , dp, uio);
959 error = xfs_da_read_buf(trans, dp, bno, -1, &bp,
964 return(XFS_ERROR(EFSCORRUPTED));
966 if (INT_GET(node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC)
968 btree = &node->btree[0];
969 xfs_dir_trace_g_dun("node: node detail", dp, uio, node);
970 for (i = 0; i < INT_GET(node->hdr.count, ARCH_CONVERT); btree++, i++) {
971 if (INT_GET(btree->hashval, ARCH_CONVERT) >= cookhash) {
972 bno = INT_GET(btree->before, ARCH_CONVERT);
976 if (i == INT_GET(node->hdr.count, ARCH_CONVERT)) {
977 xfs_da_brelse(trans, bp);
978 xfs_dir_trace_g_du("node: hash beyond EOF",
980 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0,
985 xfs_dir_trace_g_dub("node: going to block",
987 xfs_da_brelse(trans, bp);
990 ASSERT(cookhash != XFS_DA_MAXHASH);
993 * We've dropped down to the (first) leaf block that contains the
994 * hashval we are interested in. Continue rolling upward thru the
995 * leaf blocks until we fill up our buffer.
999 if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC)) {
1000 xfs_dir_trace_g_dul("node: not a leaf", dp, uio, leaf);
1001 xfs_da_brelse(trans, bp);
1002 XFS_CORRUPTION_ERROR("xfs_dir_node_getdents(1)",
1003 XFS_ERRLEVEL_LOW, mp, leaf);
1004 return XFS_ERROR(EFSCORRUPTED);
1006 xfs_dir_trace_g_dul("node: leaf detail", dp, uio, leaf);
1007 if ((nextbno = INT_GET(leaf->hdr.info.forw, ARCH_CONVERT))) {
1008 nextda = xfs_da_reada_buf(trans, dp, nextbno,
1012 error = xfs_dir_leaf_getdents_int(bp, dp, bno, uio, &eob, dbp,
1014 xfs_da_brelse(trans, bp);
1017 xfs_dir_trace_g_dub("node: E-O-B", dp, uio, bno);
1023 error = xfs_da_read_buf(trans, dp, bno, nextda, &bp,
1027 if (unlikely(bp == NULL)) {
1028 XFS_ERROR_REPORT("xfs_dir_node_getdents(2)",
1029 XFS_ERRLEVEL_LOW, mp);
1030 return(XFS_ERROR(EFSCORRUPTED));
1034 xfs_dir_trace_g_du("node: E-O-F", dp, uio);
1039 * Look up a filename in an int directory, replace the inode number.
1040 * Use an internal routine to actually do the lookup.
1043 xfs_dir_node_replace(xfs_da_args_t *args)
1045 xfs_da_state_t *state;
1046 xfs_da_state_blk_t *blk;
1047 xfs_dir_leafblock_t *leaf;
1048 xfs_dir_leaf_entry_t *entry;
1049 xfs_dir_leaf_name_t *namest;
1051 int retval, error, i;
1054 state = xfs_da_state_alloc();
1056 state->mp = args->dp->i_mount;
1057 state->blocksize = state->mp->m_sb.sb_blocksize;
1058 state->node_ents = state->mp->m_dir_node_ents;
1059 inum = args->inumber;
1062 * Search to see if name exists,
1063 * and get back a pointer to it.
1065 error = xfs_da_node_lookup_int(state, &retval);
1070 if (retval == EEXIST) {
1071 blk = &state->path.blk[state->path.active - 1];
1072 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
1075 entry = &leaf->entries[blk->index];
1076 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1077 /* XXX - replace assert ? */
1078 XFS_DIR_SF_PUT_DIRINO(&inum, &namest->inumber);
1079 xfs_da_log_buf(args->trans, bp,
1080 XFS_DA_LOGRANGE(leaf, namest, sizeof(namest->inumber)));
1081 xfs_da_buf_done(bp);
1085 i = state->path.active - 1;
1086 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1087 state->path.blk[i].bp = NULL;
1089 for (i = 0; i < state->path.active - 1; i++) {
1090 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1091 state->path.blk[i].bp = NULL;
1094 xfs_da_state_free(state);
1098 #if defined(XFS_DIR_TRACE)
1100 * Add a trace buffer entry for an inode and a uio.
1103 xfs_dir_trace_g_du(char *where, xfs_inode_t *dp, uio_t *uio)
1105 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DU, 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 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1114 * Add a trace buffer entry for an inode and a uio.
1117 xfs_dir_trace_g_dub(char *where, xfs_inode_t *dp, uio_t *uio, xfs_dablk_t bno)
1119 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUB, where,
1120 (void *)dp, (void *)dp->i_mount,
1121 (void *)((unsigned long)(uio->uio_offset >> 32)),
1122 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1123 (void *)(unsigned long)uio->uio_resid,
1124 (void *)(unsigned long)bno,
1125 NULL, NULL, NULL, NULL, NULL, NULL);
1129 * Add a trace buffer entry for an inode and a uio.
1132 xfs_dir_trace_g_dun(char *where, xfs_inode_t *dp, uio_t *uio,
1133 xfs_da_intnode_t *node)
1135 int last = INT_GET(node->hdr.count, ARCH_CONVERT) - 1;
1137 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUN, where,
1138 (void *)dp, (void *)dp->i_mount,
1139 (void *)((unsigned long)(uio->uio_offset >> 32)),
1140 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1141 (void *)(unsigned long)uio->uio_resid,
1142 (void *)(unsigned long)
1143 INT_GET(node->hdr.info.forw, ARCH_CONVERT),
1144 (void *)(unsigned long)
1145 INT_GET(node->hdr.count, ARCH_CONVERT),
1146 (void *)(unsigned long)
1147 INT_GET(node->btree[0].hashval, ARCH_CONVERT),
1148 (void *)(unsigned long)
1149 INT_GET(node->btree[last].hashval, ARCH_CONVERT),
1154 * Add a trace buffer entry for an inode and a uio.
1157 xfs_dir_trace_g_dul(char *where, xfs_inode_t *dp, uio_t *uio,
1158 xfs_dir_leafblock_t *leaf)
1160 int last = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
1162 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUL, where,
1163 (void *)dp, (void *)dp->i_mount,
1164 (void *)((unsigned long)(uio->uio_offset >> 32)),
1165 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1166 (void *)(unsigned long)uio->uio_resid,
1167 (void *)(unsigned long)
1168 INT_GET(leaf->hdr.info.forw, ARCH_CONVERT),
1169 (void *)(unsigned long)
1170 INT_GET(leaf->hdr.count, ARCH_CONVERT),
1171 (void *)(unsigned long)
1172 INT_GET(leaf->entries[0].hashval, ARCH_CONVERT),
1173 (void *)(unsigned long)
1174 INT_GET(leaf->entries[last].hashval, ARCH_CONVERT),
1179 * Add a trace buffer entry for an inode and a uio.
1182 xfs_dir_trace_g_due(char *where, xfs_inode_t *dp, uio_t *uio,
1183 xfs_dir_leaf_entry_t *entry)
1185 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUE, 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)
1191 INT_GET(entry->hashval, ARCH_CONVERT),
1192 NULL, NULL, NULL, NULL, NULL, NULL);
1196 * Add a trace buffer entry for an inode and a uio.
1199 xfs_dir_trace_g_duc(char *where, xfs_inode_t *dp, uio_t *uio, xfs_off_t cookie)
1201 xfs_dir_trace_enter(XFS_DIR_KTRACE_G_DUC, where,
1202 (void *)dp, (void *)dp->i_mount,
1203 (void *)((unsigned long)(uio->uio_offset >> 32)),
1204 (void *)((unsigned long)(uio->uio_offset & 0xFFFFFFFF)),
1205 (void *)(unsigned long)uio->uio_resid,
1206 (void *)((unsigned long)(cookie >> 32)),
1207 (void *)((unsigned long)(cookie & 0xFFFFFFFF)),
1208 NULL, NULL, NULL, NULL, NULL);
1212 * Add a trace buffer entry for the arguments given to the routine,
1216 xfs_dir_trace_enter(int type, char *where,
1217 void * a0, void * a1,
1218 void * a2, void * a3,
1219 void * a4, void * a5,
1220 void * a6, void * a7,
1221 void * a8, void * a9,
1222 void * a10, void * a11)
1224 ASSERT(xfs_dir_trace_buf);
1225 ktrace_enter(xfs_dir_trace_buf, (void *)(unsigned long)type,
1227 (void *)a0, (void *)a1, (void *)a2,
1228 (void *)a3, (void *)a4, (void *)a5,
1229 (void *)a6, (void *)a7, (void *)a8,
1230 (void *)a9, (void *)a10, (void *)a11,
1233 #endif /* XFS_DIR_TRACE */