2 * Copyright (c) 2000 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/
38 * Provide the external interfaces to manage directories.
42 xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
45 * One-time startup routine called from xfs_init().
50 xfs_dir_hash_dot
= xfs_da_hashname(".", 1);
51 xfs_dir_hash_dotdot
= xfs_da_hashname("..", 2);
55 * Initialize directory-related fields in the mount structure.
58 xfs_dir_mount(xfs_mount_t
*mp
)
60 uint shortcount
, leafcount
, count
;
63 shortcount
= (mp
->m_attroffset
- (uint
)sizeof(xfs_dir_sf_hdr_t
)) /
64 (uint
)sizeof(xfs_dir_sf_entry_t
);
65 leafcount
= (XFS_LBSIZE(mp
) - (uint
)sizeof(xfs_dir_leaf_hdr_t
)) /
66 ((uint
)sizeof(xfs_dir_leaf_entry_t
) +
67 (uint
)sizeof(xfs_dir_leaf_name_t
));
68 count
= shortcount
> leafcount
? shortcount
: leafcount
;
69 mp
->m_dircook_elog
= xfs_da_log2_roundup(count
+ 1);
70 ASSERT(mp
->m_dircook_elog
<= mp
->m_sb
.sb_blocklog
);
72 (XFS_LBSIZE(mp
) - (uint
)sizeof(xfs_da_node_hdr_t
)) /
73 (uint
)sizeof(xfs_da_node_entry_t
);
74 mp
->m_dir_magicpct
= (XFS_LBSIZE(mp
) * 37) / 100;
75 mp
->m_dirblksize
= mp
->m_sb
.sb_blocksize
;
80 * Initialize a directory with its "." and ".." entries.
83 xfs_dir_init(xfs_trans_t
*trans
, xfs_inode_t
*dir
, xfs_inode_t
*parent_dir
)
88 bzero((char *)&args
, sizeof(args
));
92 ASSERT((dir
->i_d
.di_mode
& IFMT
) == IFDIR
);
93 if ((error
= xfs_dir_ino_validate(trans
->t_mountp
, parent_dir
->i_ino
)))
96 return(xfs_dir_shortform_create(&args
, parent_dir
->i_ino
));
100 * Generic handler routine to add a name to a directory.
101 * Transitions directory from shortform to Btree as necessary.
103 STATIC
int /* error */
104 xfs_dir_createname(xfs_trans_t
*trans
, xfs_inode_t
*dp
, char *name
,
105 int namelen
, xfs_ino_t inum
, xfs_fsblock_t
*firstblock
,
106 xfs_bmap_free_t
*flist
, xfs_extlen_t total
)
109 int retval
, newsize
, done
;
111 ASSERT((dp
->i_d
.di_mode
& IFMT
) == IFDIR
);
113 if ((retval
= xfs_dir_ino_validate(trans
->t_mountp
, inum
)))
116 XFS_STATS_INC(xfsstats
.xs_dir_create
);
118 * Fill in the arg structure for this request.
121 args
.namelen
= namelen
;
122 args
.hashval
= xfs_da_hashname(name
, namelen
);
125 args
.firstblock
= firstblock
;
128 args
.whichfork
= XFS_DATA_FORK
;
131 args
.addname
= args
.oknoent
= 1;
134 * Decide on what work routines to call based on the inode size.
137 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
138 newsize
= XFS_DIR_SF_ENTSIZE_BYNAME(args
.namelen
);
139 if ((dp
->i_d
.di_size
+ newsize
) <= XFS_IFORK_DSIZE(dp
)) {
140 retval
= xfs_dir_shortform_addname(&args
);
144 return XFS_ERROR(ENOSPC
);
145 retval
= xfs_dir_shortform_to_leaf(&args
);
149 if (!done
&& xfs_bmap_one_block(dp
, XFS_DATA_FORK
)) {
150 retval
= xfs_dir_leaf_addname(&args
);
151 done
= retval
!= ENOSPC
;
154 return XFS_ERROR(ENOSPC
);
155 retval
= xfs_dir_leaf_to_node(&args
);
160 retval
= xfs_dir_node_addname(&args
);
166 * Generic handler routine to remove a name from a directory.
167 * Transitions directory from Btree to shortform as necessary.
169 STATIC
int /* error */
170 xfs_dir_removename(xfs_trans_t
*trans
, xfs_inode_t
*dp
, char *name
,
171 int namelen
, xfs_ino_t ino
, xfs_fsblock_t
*firstblock
,
172 xfs_bmap_free_t
*flist
, xfs_extlen_t total
)
175 int count
, totallen
, newsize
, retval
;
177 ASSERT((dp
->i_d
.di_mode
& IFMT
) == IFDIR
);
178 XFS_STATS_INC(xfsstats
.xs_dir_remove
);
180 * Fill in the arg structure for this request.
183 args
.namelen
= namelen
;
184 args
.hashval
= xfs_da_hashname(name
, namelen
);
187 args
.firstblock
= firstblock
;
190 args
.whichfork
= XFS_DATA_FORK
;
192 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
195 * Decide on what work routines to call based on the inode size.
197 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
198 retval
= xfs_dir_shortform_removename(&args
);
199 } else if (xfs_bmap_one_block(dp
, XFS_DATA_FORK
)) {
200 retval
= xfs_dir_leaf_removename(&args
, &count
, &totallen
);
202 newsize
= XFS_DIR_SF_ALLFIT(count
, totallen
);
203 if (newsize
<= XFS_IFORK_DSIZE(dp
)) {
204 retval
= xfs_dir_leaf_to_shortform(&args
);
208 retval
= xfs_dir_node_removename(&args
);
213 STATIC
int /* error */
214 xfs_dir_lookup(xfs_trans_t
*trans
, xfs_inode_t
*dp
, char *name
, int namelen
,
220 ASSERT((dp
->i_d
.di_mode
& IFMT
) == IFDIR
);
221 if (namelen
>= MAXNAMELEN
) {
222 return(XFS_ERROR(EINVAL
));
225 XFS_STATS_INC(xfsstats
.xs_dir_lookup
);
227 * Fill in the arg structure for this request.
230 args
.namelen
= namelen
;
231 args
.hashval
= xfs_da_hashname(name
, namelen
);
234 args
.firstblock
= NULL
;
237 args
.whichfork
= XFS_DATA_FORK
;
239 args
.justcheck
= args
.addname
= 0;
243 * Decide on what work routines to call based on the inode size.
245 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
246 retval
= xfs_dir_shortform_lookup(&args
);
247 } else if (xfs_bmap_one_block(dp
, XFS_DATA_FORK
)) {
248 retval
= xfs_dir_leaf_lookup(&args
);
250 retval
= xfs_dir_node_lookup(&args
);
252 if (retval
== EEXIST
)
254 *inum
= args
.inumber
;
258 STATIC
int /* error */
259 xfs_dir_replace(xfs_trans_t
*trans
, xfs_inode_t
*dp
, char *name
, int namelen
,
260 xfs_ino_t inum
, xfs_fsblock_t
*firstblock
,
261 xfs_bmap_free_t
*flist
, xfs_extlen_t total
)
266 ASSERT((dp
->i_d
.di_mode
& IFMT
) == IFDIR
);
267 if (namelen
>= MAXNAMELEN
) {
268 return(XFS_ERROR(EINVAL
));
271 if ((retval
= xfs_dir_ino_validate(trans
->t_mountp
, inum
)))
275 * Fill in the arg structure for this request.
278 args
.namelen
= namelen
;
279 args
.hashval
= xfs_da_hashname(name
, namelen
);
282 args
.firstblock
= firstblock
;
285 args
.whichfork
= XFS_DATA_FORK
;
287 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
290 * Decide on what work routines to call based on the inode size.
292 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
) {
293 retval
= xfs_dir_shortform_replace(&args
);
294 } else if (xfs_bmap_one_block(dp
, XFS_DATA_FORK
)) {
295 retval
= xfs_dir_leaf_replace(&args
);
297 retval
= xfs_dir_node_replace(&args
);
304 /*========================================================================
305 * External routines when dirsize == XFS_LBSIZE(dp->i_mount).
306 *========================================================================*/
309 * Add a name to the leaf directory structure
310 * This is the external routine.
313 xfs_dir_leaf_addname(xfs_da_args_t
*args
)
318 retval
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
324 retval
= xfs_dir_leaf_lookup_int(bp
, args
, &index
);
325 if (retval
== ENOENT
)
326 retval
= xfs_dir_leaf_add(bp
, args
, index
);
332 * Remove a name from the leaf directory structure
333 * This is the external routine.
336 xfs_dir_leaf_removename(xfs_da_args_t
*args
, int *count
, int *totallen
)
338 xfs_dir_leafblock_t
*leaf
;
342 retval
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
348 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR_LEAF_MAGIC
);
349 retval
= xfs_dir_leaf_lookup_int(bp
, args
, &index
);
350 if (retval
== EEXIST
) {
351 (void)xfs_dir_leaf_remove(args
->trans
, bp
, index
);
352 *count
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
353 *totallen
= INT_GET(leaf
->hdr
.namebytes
, ARCH_CONVERT
);
361 * Look up a name in a leaf directory structure.
362 * This is the external routine.
365 xfs_dir_leaf_lookup(xfs_da_args_t
*args
)
370 retval
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
375 retval
= xfs_dir_leaf_lookup_int(bp
, args
, &index
);
376 xfs_da_brelse(args
->trans
, bp
);
381 * Look up a name in a leaf directory structure, replace the inode number.
382 * This is the external routine.
385 xfs_dir_leaf_replace(xfs_da_args_t
*args
)
390 xfs_dir_leafblock_t
*leaf
;
391 xfs_dir_leaf_entry_t
*entry
;
392 xfs_dir_leaf_name_t
*namest
;
394 inum
= args
->inumber
;
395 retval
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
400 retval
= xfs_dir_leaf_lookup_int(bp
, args
, &index
);
401 if (retval
== EEXIST
) {
403 entry
= &leaf
->entries
[index
];
404 namest
= XFS_DIR_LEAF_NAMESTRUCT(leaf
, INT_GET(entry
->nameidx
, ARCH_CONVERT
));
405 /* XXX - replace assert? */
406 XFS_DIR_SF_PUT_DIRINO_ARCH(&inum
, &namest
->inumber
, ARCH_CONVERT
);
407 xfs_da_log_buf(args
->trans
, bp
,
408 XFS_DA_LOGRANGE(leaf
, namest
, sizeof(namest
->inumber
)));
412 xfs_da_brelse(args
->trans
, bp
);
417 /*========================================================================
418 * External routines when dirsize > XFS_LBSIZE(mp).
419 *========================================================================*/
422 * Add a name to a Btree-format directory.
424 * This will involve walking down the Btree, and may involve splitting
425 * leaf nodes and even splitting intermediate nodes up to and including
426 * the root node (a special case of an intermediate node).
429 xfs_dir_node_addname(xfs_da_args_t
*args
)
431 xfs_da_state_t
*state
;
432 xfs_da_state_blk_t
*blk
;
436 * Fill in bucket of arguments/results/context to carry around.
438 state
= xfs_da_state_alloc();
440 state
->mp
= args
->dp
->i_mount
;
441 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
444 * Search to see if name already exists, and get back a pointer
445 * to where it should go.
447 error
= xfs_da_node_lookup_int(state
, &retval
);
450 if (retval
!= ENOENT
)
452 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
453 ASSERT(blk
->magic
== XFS_DIR_LEAF_MAGIC
);
454 retval
= xfs_dir_leaf_add(blk
->bp
, args
, blk
->index
);
457 * Addition succeeded, update Btree hashvals.
459 if (!args
->justcheck
)
460 xfs_da_fixhashpath(state
, &state
->path
);
463 * Addition failed, split as many Btree elements as required.
465 if (args
->total
== 0) {
466 ASSERT(retval
== ENOSPC
);
469 retval
= xfs_da_split(state
);
472 xfs_da_state_free(state
);
478 * Remove a name from a B-tree directory.
480 * This will involve walking down the Btree, and may involve joining
481 * leaf nodes and even joining intermediate nodes up to and including
482 * the root node (a special case of an intermediate node).
485 xfs_dir_node_removename(xfs_da_args_t
*args
)
487 xfs_da_state_t
*state
;
488 xfs_da_state_blk_t
*blk
;
491 state
= xfs_da_state_alloc();
493 state
->mp
= args
->dp
->i_mount
;
494 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
497 * Search to see if name exists, and get back a pointer to it.
499 error
= xfs_da_node_lookup_int(state
, &retval
);
502 if (retval
!= EEXIST
) {
503 xfs_da_state_free(state
);
508 * Remove the name and update the hashvals in the tree.
510 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
511 ASSERT(blk
->magic
== XFS_DIR_LEAF_MAGIC
);
512 retval
= xfs_dir_leaf_remove(args
->trans
, blk
->bp
, blk
->index
);
513 xfs_da_fixhashpath(state
, &state
->path
);
516 * Check to see if the tree needs to be collapsed.
520 error
= xfs_da_join(state
);
523 xfs_da_state_free(state
);
530 * Look up a filename in a int directory.
531 * Use an internal routine to actually do all the work.
534 xfs_dir_node_lookup(xfs_da_args_t
*args
)
536 xfs_da_state_t
*state
;
537 int retval
, error
, i
;
539 state
= xfs_da_state_alloc();
541 state
->mp
= args
->dp
->i_mount
;
542 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
545 * Search to see if name exists,
546 * and get back a pointer to it.
548 error
= xfs_da_node_lookup_int(state
, &retval
);
554 * If not in a transaction, we have to release all the buffers.
556 for (i
= 0; i
< state
->path
.active
; i
++) {
557 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
558 state
->path
.blk
[i
].bp
= NULL
;
561 xfs_da_state_free(state
);
566 * Look up a filename in an int directory, replace the inode number.
567 * Use an internal routine to actually do the lookup.
570 xfs_dir_node_replace(xfs_da_args_t
*args
)
572 xfs_da_state_t
*state
;
573 xfs_da_state_blk_t
*blk
;
574 xfs_dir_leafblock_t
*leaf
;
575 xfs_dir_leaf_entry_t
*entry
;
576 xfs_dir_leaf_name_t
*namest
;
578 int retval
, error
, i
;
581 state
= xfs_da_state_alloc();
583 state
->mp
= args
->dp
->i_mount
;
584 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
585 inum
= args
->inumber
;
588 * Search to see if name exists,
589 * and get back a pointer to it.
591 error
= xfs_da_node_lookup_int(state
, &retval
);
596 if (retval
== EEXIST
) {
597 blk
= &state
->path
.blk
[state
->path
.active
- 1];
598 ASSERT(blk
->magic
== XFS_DIR_LEAF_MAGIC
);
601 entry
= &leaf
->entries
[blk
->index
];
602 namest
= XFS_DIR_LEAF_NAMESTRUCT(leaf
, INT_GET(entry
->nameidx
, ARCH_CONVERT
));
603 /* XXX - replace assert ? */
604 XFS_DIR_SF_PUT_DIRINO_ARCH(&inum
, &namest
->inumber
, ARCH_CONVERT
);
605 xfs_da_log_buf(args
->trans
, bp
,
606 XFS_DA_LOGRANGE(leaf
, namest
, sizeof(namest
->inumber
)));
611 i
= state
->path
.active
- 1;
612 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
613 state
->path
.blk
[i
].bp
= NULL
;
615 for (i
= 0; i
< state
->path
.active
- 1; i
++) {
616 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
617 state
->path
.blk
[i
].bp
= NULL
;
620 xfs_da_state_free(state
);