blob: 53787f35338bd19e37146c8967dd9b27298106c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include "xfs.h"
34
35#include "xfs_macros.h"
36#include "xfs_types.h"
37#include "xfs_inum.h"
38#include "xfs_log.h"
39#include "xfs_trans.h"
40#include "xfs_sb.h"
41#include "xfs_dir.h"
42#include "xfs_dir2.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"
55#include "xfs_bmap.h"
56#include "xfs_da_btree.h"
57#include "xfs_dir_leaf.h"
58#include "xfs_error.h"
59
60/*
61 * xfs_dir.c
62 *
63 * Provide the external interfaces to manage directories.
64 */
65
66/*========================================================================
67 * Function prototypes for the kernel.
68 *========================================================================*/
69
70/*
71 * Functions for the dirops interfaces.
72 */
73static void xfs_dir_mount(struct xfs_mount *mp);
74
75static int xfs_dir_isempty(struct xfs_inode *dp);
76
77static int xfs_dir_init(struct xfs_trans *trans,
78 struct xfs_inode *dir,
79 struct xfs_inode *parent_dir);
80
81static int xfs_dir_createname(struct xfs_trans *trans,
82 struct xfs_inode *dp,
83 char *name_string,
84 int name_len,
85 xfs_ino_t inode_number,
86 xfs_fsblock_t *firstblock,
87 xfs_bmap_free_t *flist,
88 xfs_extlen_t total);
89
90static int xfs_dir_lookup(struct xfs_trans *tp,
91 struct xfs_inode *dp,
92 char *name_string,
93 int name_length,
94 xfs_ino_t *inode_number);
95
96static int xfs_dir_removename(struct xfs_trans *trans,
97 struct xfs_inode *dp,
98 char *name_string,
99 int name_length,
100 xfs_ino_t ino,
101 xfs_fsblock_t *firstblock,
102 xfs_bmap_free_t *flist,
103 xfs_extlen_t total);
104
105static int xfs_dir_getdents(struct xfs_trans *tp,
106 struct xfs_inode *dp,
107 struct uio *uiop,
108 int *eofp);
109
110static int xfs_dir_replace(struct xfs_trans *tp,
111 struct xfs_inode *dp,
112 char *name_string,
113 int name_length,
114 xfs_ino_t inode_number,
115 xfs_fsblock_t *firstblock,
116 xfs_bmap_free_t *flist,
117 xfs_extlen_t total);
118
119static int xfs_dir_canenter(struct xfs_trans *tp,
120 struct xfs_inode *dp,
121 char *name_string,
122 int name_length);
123
124static int xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp,
125 xfs_dinode_t *dip);
126
127xfs_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,
139};
140
141/*
142 * Internal routines when dirsize == XFS_LBSIZE(mp).
143 */
144STATIC int xfs_dir_leaf_lookup(xfs_da_args_t *args);
145STATIC int xfs_dir_leaf_removename(xfs_da_args_t *args, int *number_entries,
146 int *total_namebytes);
147STATIC int xfs_dir_leaf_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
148 uio_t *uio, int *eofp,
149 xfs_dirent_t *dbp,
150 xfs_dir_put_t put);
151STATIC int xfs_dir_leaf_replace(xfs_da_args_t *args);
152
153/*
154 * Internal routines when dirsize > XFS_LBSIZE(mp).
155 */
156STATIC int xfs_dir_node_addname(xfs_da_args_t *args);
157STATIC int xfs_dir_node_lookup(xfs_da_args_t *args);
158STATIC int xfs_dir_node_removename(xfs_da_args_t *args);
159STATIC int xfs_dir_node_getdents(xfs_trans_t *trans, xfs_inode_t *dp,
160 uio_t *uio, int *eofp,
161 xfs_dirent_t *dbp,
162 xfs_dir_put_t put);
163STATIC int xfs_dir_node_replace(xfs_da_args_t *args);
164
165#if defined(XFS_DIR_TRACE)
166ktrace_t *xfs_dir_trace_buf;
167#endif
168
169
170/*========================================================================
171 * Overall external interface routines.
172 *========================================================================*/
173
174xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
175
176/*
177 * One-time startup routine called from xfs_init().
178 */
179void
180xfs_dir_startup(void)
181{
182 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
183 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
184}
185
186/*
187 * Initialize directory-related fields in the mount structure.
188 */
189static void
190xfs_dir_mount(xfs_mount_t *mp)
191{
192 uint shortcount, leafcount, count;
193
194 mp->m_dirversion = 1;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100195 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));
203 } else {
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));
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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;
221}
222
223/*
224 * Return 1 if directory contains only "." and "..".
225 */
226static int
227xfs_dir_isempty(xfs_inode_t *dp)
228{
229 xfs_dir_sf_hdr_t *hdr;
230
231 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
232 if (dp->i_d.di_size == 0)
233 return(1);
234 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
235 return(0);
236 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
237 return(hdr->count == 0);
238}
239
240/*
241 * Initialize a directory with its "." and ".." entries.
242 */
243static int
244xfs_dir_init(xfs_trans_t *trans, xfs_inode_t *dir, xfs_inode_t *parent_dir)
245{
246 xfs_da_args_t args;
247 int error;
248
249 memset((char *)&args, 0, sizeof(args));
250 args.dp = dir;
251 args.trans = trans;
252
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)))
255 return error;
256
257 return(xfs_dir_shortform_create(&args, parent_dir->i_ino));
258}
259
260/*
261 * Generic handler routine to add a name to a directory.
262 * Transitions directory from shortform to Btree as necessary.
263 */
264static int /* error */
265xfs_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)
268{
269 xfs_da_args_t args;
270 int retval, newsize, done;
271
272 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
273
274 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
275 return (retval);
276
277 XFS_STATS_INC(xs_dir_create);
278 /*
279 * Fill in the arg structure for this request.
280 */
281 args.name = name;
282 args.namelen = namelen;
283 args.hashval = xfs_da_hashname(name, namelen);
284 args.inumber = inum;
285 args.dp = dp;
286 args.firstblock = firstblock;
287 args.flist = flist;
288 args.total = total;
289 args.whichfork = XFS_DATA_FORK;
290 args.trans = trans;
291 args.justcheck = 0;
292 args.addname = args.oknoent = 1;
293
294 /*
295 * Decide on what work routines to call based on the inode size.
296 */
297 done = 0;
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);
302 done = 1;
303 } else {
304 if (total == 0)
305 return XFS_ERROR(ENOSPC);
306 retval = xfs_dir_shortform_to_leaf(&args);
307 done = retval != 0;
308 }
309 }
310 if (!done && xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
311 retval = xfs_dir_leaf_addname(&args);
312 done = retval != ENOSPC;
313 if (!done) {
314 if (total == 0)
315 return XFS_ERROR(ENOSPC);
316 retval = xfs_dir_leaf_to_node(&args);
317 done = retval != 0;
318 }
319 }
320 if (!done) {
321 retval = xfs_dir_node_addname(&args);
322 }
323 return(retval);
324}
325
326/*
327 * Generic handler routine to check if a name can be added to a directory,
328 * without adding any blocks to the directory.
329 */
330static int /* error */
331xfs_dir_canenter(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen)
332{
333 xfs_da_args_t args;
334 int retval, newsize;
335
336 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
337 /*
338 * Fill in the arg structure for this request.
339 */
340 args.name = name;
341 args.namelen = namelen;
342 args.hashval = xfs_da_hashname(name, namelen);
343 args.inumber = 0;
344 args.dp = dp;
345 args.firstblock = NULL;
346 args.flist = NULL;
347 args.total = 0;
348 args.whichfork = XFS_DATA_FORK;
349 args.trans = trans;
350 args.justcheck = args.addname = args.oknoent = 1;
351
352 /*
353 * Decide on what work routines to call based on the inode size.
354 */
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))
358 retval = 0;
359 else
360 retval = XFS_ERROR(ENOSPC);
361 } else if (xfs_bmap_one_block(dp, XFS_DATA_FORK)) {
362 retval = xfs_dir_leaf_addname(&args);
363 } else {
364 retval = xfs_dir_node_addname(&args);
365 }
366 return(retval);
367}
368
369/*
370 * Generic handler routine to remove a name from a directory.
371 * Transitions directory from Btree to shortform as necessary.
372 */
373static int /* error */
374xfs_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)
377{
378 xfs_da_args_t args;
379 int count, totallen, newsize, retval;
380
381 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
382 XFS_STATS_INC(xs_dir_remove);
383 /*
384 * Fill in the arg structure for this request.
385 */
386 args.name = name;
387 args.namelen = namelen;
388 args.hashval = xfs_da_hashname(name, namelen);
389 args.inumber = ino;
390 args.dp = dp;
391 args.firstblock = firstblock;
392 args.flist = flist;
393 args.total = total;
394 args.whichfork = XFS_DATA_FORK;
395 args.trans = trans;
396 args.justcheck = args.addname = args.oknoent = 0;
397
398 /*
399 * Decide on what work routines to call based on the inode size.
400 */
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);
405 if (retval == 0) {
406 newsize = XFS_DIR_SF_ALLFIT(count, totallen);
407 if (newsize <= XFS_IFORK_DSIZE(dp)) {
408 retval = xfs_dir_leaf_to_shortform(&args);
409 }
410 }
411 } else {
412 retval = xfs_dir_node_removename(&args);
413 }
414 return(retval);
415}
416
417static int /* error */
418xfs_dir_lookup(xfs_trans_t *trans, xfs_inode_t *dp, char *name, int namelen,
419 xfs_ino_t *inum)
420{
421 xfs_da_args_t args;
422 int retval;
423
424 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
425
426 XFS_STATS_INC(xs_dir_lookup);
427 /*
428 * Fill in the arg structure for this request.
429 */
430 args.name = name;
431 args.namelen = namelen;
432 args.hashval = xfs_da_hashname(name, namelen);
433 args.inumber = 0;
434 args.dp = dp;
435 args.firstblock = NULL;
436 args.flist = NULL;
437 args.total = 0;
438 args.whichfork = XFS_DATA_FORK;
439 args.trans = trans;
440 args.justcheck = args.addname = 0;
441 args.oknoent = 1;
442
443 /*
444 * Decide on what work routines to call based on the inode size.
445 */
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);
450 } else {
451 retval = xfs_dir_node_lookup(&args);
452 }
453 if (retval == EEXIST)
454 retval = 0;
455 *inum = args.inumber;
456 return(retval);
457}
458
459/*
460 * Implement readdir.
461 */
462static int /* error */
463xfs_dir_getdents(xfs_trans_t *trans, xfs_inode_t *dp, uio_t *uio, int *eofp)
464{
465 xfs_dirent_t *dbp;
466 int alignment, retval;
467 xfs_dir_put_t put;
468
469 XFS_STATS_INC(xs_dir_getdents);
470 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
471
472 /*
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.
476 */
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)) {
481 dbp = NULL;
482 put = xfs_dir_put_dirent64_direct;
483 } else {
484 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
485 put = xfs_dir_put_dirent64_uio;
486 }
487
488 /*
489 * Decide on what work routines to call based on the inode size.
490 */
491 *eofp = 0;
492
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);
497 } else {
498 retval = xfs_dir_node_getdents(trans, dp, uio, eofp, dbp, put);
499 }
500 if (dbp != NULL)
501 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
502
503 return(retval);
504}
505
506static int /* error */
507xfs_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)
510{
511 xfs_da_args_t args;
512 int retval;
513
514 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
515
516 if ((retval = xfs_dir_ino_validate(trans->t_mountp, inum)))
517 return retval;
518
519 /*
520 * Fill in the arg structure for this request.
521 */
522 args.name = name;
523 args.namelen = namelen;
524 args.hashval = xfs_da_hashname(name, namelen);
525 args.inumber = inum;
526 args.dp = dp;
527 args.firstblock = firstblock;
528 args.flist = flist;
529 args.total = total;
530 args.whichfork = XFS_DATA_FORK;
531 args.trans = trans;
532 args.justcheck = args.addname = args.oknoent = 0;
533
534 /*
535 * Decide on what work routines to call based on the inode size.
536 */
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);
541 } else {
542 retval = xfs_dir_node_replace(&args);
543 }
544
545 return(retval);
546}
547
548static int
549xfs_dir_shortform_validate_ondisk(xfs_mount_t *mp, xfs_dinode_t *dp)
550{
551 xfs_ino_t ino;
552 int namelen_sum;
553 int count;
554 xfs_dir_shortform_t *sf;
555 xfs_dir_sf_entry_t *sfe;
556 int i;
557
558
559
560 if ((INT_GET(dp->di_core.di_mode, ARCH_CONVERT) & S_IFMT) != S_IFDIR) {
561 return 0;
562 }
563 if (INT_GET(dp->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_LOCAL) {
564 return 0;
565 }
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",
568 dp);
569 return 1;
570 }
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))
574 return 1;
575
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);
580 return(1);
581 }
582
583 if (count == 0) {
584 return 0;
585 }
586
587 namelen_sum = 0;
588 sfe = &sf->list[0];
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);
595 return 1;
596 }
597 namelen_sum += sfe->namelen;
598 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
599 }
600 if (namelen_sum >= XFS_LITINO(mp)) {
601 xfs_fs_cmn_err(CE_WARN, mp,
602 "Invalid shortform namelen: dp 0x%p", dp);
603 return 1;
604 }
605
606 return 0;
607}
608
609/*========================================================================
610 * External routines when dirsize == XFS_LBSIZE(dp->i_mount).
611 *========================================================================*/
612
613/*
614 * Add a name to the leaf directory structure
615 * This is the external routine.
616 */
617int
618xfs_dir_leaf_addname(xfs_da_args_t *args)
619{
620 int index, retval;
621 xfs_dabuf_t *bp;
622
623 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
624 XFS_DATA_FORK);
625 if (retval)
626 return(retval);
627 ASSERT(bp != NULL);
628
629 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
630 if (retval == ENOENT)
631 retval = xfs_dir_leaf_add(bp, args, index);
632 xfs_da_buf_done(bp);
633 return(retval);
634}
635
636/*
637 * Remove a name from the leaf directory structure
638 * This is the external routine.
639 */
640STATIC int
641xfs_dir_leaf_removename(xfs_da_args_t *args, int *count, int *totallen)
642{
643 xfs_dir_leafblock_t *leaf;
644 int index, retval;
645 xfs_dabuf_t *bp;
646
647 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
648 XFS_DATA_FORK);
649 if (retval)
650 return(retval);
651 ASSERT(bp != NULL);
652 leaf = bp->data;
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);
659 retval = 0;
660 }
661 xfs_da_buf_done(bp);
662 return(retval);
663}
664
665/*
666 * Look up a name in a leaf directory structure.
667 * This is the external routine.
668 */
669STATIC int
670xfs_dir_leaf_lookup(xfs_da_args_t *args)
671{
672 int index, retval;
673 xfs_dabuf_t *bp;
674
675 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
676 XFS_DATA_FORK);
677 if (retval)
678 return(retval);
679 ASSERT(bp != NULL);
680 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
681 xfs_da_brelse(args->trans, bp);
682 return(retval);
683}
684
685/*
686 * Copy out directory entries for getdents(), for leaf directories.
687 */
688STATIC int
689xfs_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)
691{
692 xfs_dabuf_t *bp;
693 int retval, eob;
694
695 retval = xfs_da_read_buf(dp->i_transp, dp, 0, -1, &bp, XFS_DATA_FORK);
696 if (retval)
697 return(retval);
698 ASSERT(bp != NULL);
699 retval = xfs_dir_leaf_getdents_int(bp, dp, 0, uio, &eob, dbp, put, -1);
700 xfs_da_brelse(trans, bp);
701 *eofp = (eob == 0);
702 return(retval);
703}
704
705/*
706 * Look up a name in a leaf directory structure, replace the inode number.
707 * This is the external routine.
708 */
709STATIC int
710xfs_dir_leaf_replace(xfs_da_args_t *args)
711{
712 int index, retval;
713 xfs_dabuf_t *bp;
714 xfs_ino_t inum;
715 xfs_dir_leafblock_t *leaf;
716 xfs_dir_leaf_entry_t *entry;
717 xfs_dir_leaf_name_t *namest;
718
719 inum = args->inumber;
720 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
721 XFS_DATA_FORK);
722 if (retval)
723 return(retval);
724 ASSERT(bp != NULL);
725 retval = xfs_dir_leaf_lookup_int(bp, args, &index);
726 if (retval == EEXIST) {
727 leaf = bp->data;
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)));
734 xfs_da_buf_done(bp);
735 retval = 0;
736 } else
737 xfs_da_brelse(args->trans, bp);
738 return(retval);
739}
740
741
742/*========================================================================
743 * External routines when dirsize > XFS_LBSIZE(mp).
744 *========================================================================*/
745
746/*
747 * Add a name to a Btree-format directory.
748 *
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).
752 */
753STATIC int
754xfs_dir_node_addname(xfs_da_args_t *args)
755{
756 xfs_da_state_t *state;
757 xfs_da_state_blk_t *blk;
758 int retval, error;
759
760 /*
761 * Fill in bucket of arguments/results/context to carry around.
762 */
763 state = xfs_da_state_alloc();
764 state->args = args;
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;
768
769 /*
770 * Search to see if name already exists, and get back a pointer
771 * to where it should go.
772 */
773 error = xfs_da_node_lookup_int(state, &retval);
774 if (error)
775 retval = error;
776 if (retval != ENOENT)
777 goto error;
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);
781 if (retval == 0) {
782 /*
783 * Addition succeeded, update Btree hashvals.
784 */
785 if (!args->justcheck)
786 xfs_da_fixhashpath(state, &state->path);
787 } else {
788 /*
789 * Addition failed, split as many Btree elements as required.
790 */
791 if (args->total == 0) {
792 ASSERT(retval == ENOSPC);
793 goto error;
794 }
795 retval = xfs_da_split(state);
796 }
797error:
798 xfs_da_state_free(state);
799
800 return(retval);
801}
802
803/*
804 * Remove a name from a B-tree directory.
805 *
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).
809 */
810STATIC int
811xfs_dir_node_removename(xfs_da_args_t *args)
812{
813 xfs_da_state_t *state;
814 xfs_da_state_blk_t *blk;
815 int retval, error;
816
817 state = xfs_da_state_alloc();
818 state->args = args;
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;
822
823 /*
824 * Search to see if name exists, and get back a pointer to it.
825 */
826 error = xfs_da_node_lookup_int(state, &retval);
827 if (error)
828 retval = error;
829 if (retval != EEXIST) {
830 xfs_da_state_free(state);
831 return(retval);
832 }
833
834 /*
835 * Remove the name and update the hashvals in the tree.
836 */
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);
841
842 /*
843 * Check to see if the tree needs to be collapsed.
844 */
845 error = 0;
846 if (retval) {
847 error = xfs_da_join(state);
848 }
849
850 xfs_da_state_free(state);
851 if (error)
852 return(error);
853 return(0);
854}
855
856/*
857 * Look up a filename in a int directory.
858 * Use an internal routine to actually do all the work.
859 */
860STATIC int
861xfs_dir_node_lookup(xfs_da_args_t *args)
862{
863 xfs_da_state_t *state;
864 int retval, error, i;
865
866 state = xfs_da_state_alloc();
867 state->args = args;
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;
871
872 /*
873 * Search to see if name exists,
874 * and get back a pointer to it.
875 */
876 error = xfs_da_node_lookup_int(state, &retval);
877 if (error) {
878 retval = error;
879 }
880
881 /*
882 * If not in a transaction, we have to release all the buffers.
883 */
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;
887 }
888
889 xfs_da_state_free(state);
890 return(retval);
891}
892
893STATIC int
894xfs_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)
896{
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;
902 xfs_mount_t *mp;
903 int error, eob, i;
904 xfs_dabuf_t *bp;
905 xfs_daddr_t nextda;
906
907 /*
908 * Pick up our context.
909 */
910 mp = dp->i_mount;
911 bp = NULL;
912 bno = XFS_DA_COOKIE_BNO(mp, uio->uio_offset);
913 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
914
915 xfs_dir_trace_g_du("node: start", dp, uio);
916
917 /*
918 * Re-find our place, even if we're confused about what our place is.
919 *
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.
924 */
925 if (bno > 0) {
926 error = xfs_da_read_buf(trans, dp, bno, -2, &bp, XFS_DATA_FORK);
927 if ((error != 0) && (error != EFSCORRUPTED))
928 return(error);
929 if (bp)
930 leaf = bp->data;
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",
933 dp, uio, bno);
934 xfs_da_brelse(trans, bp);
935 bp = NULL;
936 }
937 if (bp && INT_GET(leaf->entries[0].hashval, ARCH_CONVERT) > cookhash) {
938 xfs_dir_trace_g_dub("node: leaf hash too large",
939 dp, uio, bno);
940 xfs_da_brelse(trans, bp);
941 bp = NULL;
942 }
943 if (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",
946 dp, uio, bno);
947 xfs_da_brelse(trans, bp);
948 bp = NULL;
949 }
950 }
951
952 /*
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.
956 */
957 if (bp == NULL) {
958 xfs_dir_trace_g_du("node: start at root" , dp, uio);
959 bno = 0;
960 for (;;) {
961 error = xfs_da_read_buf(trans, dp, bno, -1, &bp,
962 XFS_DATA_FORK);
963 if (error)
964 return(error);
965 if (bp == NULL)
966 return(XFS_ERROR(EFSCORRUPTED));
967 node = bp->data;
968 if (INT_GET(node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC)
969 break;
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);
975 break;
976 }
977 }
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",
981 dp, uio);
982 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0,
983 XFS_DA_MAXHASH);
984 *eofp = 1;
985 return(0);
986 }
987 xfs_dir_trace_g_dub("node: going to block",
988 dp, uio, bno);
989 xfs_da_brelse(trans, bp);
990 }
991 }
992 ASSERT(cookhash != XFS_DA_MAXHASH);
993
994 /*
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.
998 */
999 for (;;) {
1000 leaf = bp->data;
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);
1007 }
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,
1011 XFS_DATA_FORK);
1012 } else
1013 nextda = -1;
1014 error = xfs_dir_leaf_getdents_int(bp, dp, bno, uio, &eob, dbp,
1015 put, nextda);
1016 xfs_da_brelse(trans, bp);
1017 bno = nextbno;
1018 if (eob) {
1019 xfs_dir_trace_g_dub("node: E-O-B", dp, uio, bno);
1020 *eofp = 0;
1021 return(error);
1022 }
1023 if (bno == 0)
1024 break;
1025 error = xfs_da_read_buf(trans, dp, bno, nextda, &bp,
1026 XFS_DATA_FORK);
1027 if (error)
1028 return(error);
1029 if (unlikely(bp == NULL)) {
1030 XFS_ERROR_REPORT("xfs_dir_node_getdents(2)",
1031 XFS_ERRLEVEL_LOW, mp);
1032 return(XFS_ERROR(EFSCORRUPTED));
1033 }
1034 }
1035 *eofp = 1;
1036 xfs_dir_trace_g_du("node: E-O-F", dp, uio);
1037 return(0);
1038}
1039
1040/*
1041 * Look up a filename in an int directory, replace the inode number.
1042 * Use an internal routine to actually do the lookup.
1043 */
1044STATIC int
1045xfs_dir_node_replace(xfs_da_args_t *args)
1046{
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;
1052 xfs_ino_t inum;
1053 int retval, error, i;
1054 xfs_dabuf_t *bp;
1055
1056 state = xfs_da_state_alloc();
1057 state->args = args;
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;
1062
1063 /*
1064 * Search to see if name exists,
1065 * and get back a pointer to it.
1066 */
1067 error = xfs_da_node_lookup_int(state, &retval);
1068 if (error) {
1069 retval = error;
1070 }
1071
1072 if (retval == EEXIST) {
1073 blk = &state->path.blk[state->path.active - 1];
1074 ASSERT(blk->magic == XFS_DIR_LEAF_MAGIC);
1075 bp = blk->bp;
1076 leaf = bp->data;
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);
1084 blk->bp = NULL;
1085 retval = 0;
1086 } else {
1087 i = state->path.active - 1;
1088 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1089 state->path.blk[i].bp = NULL;
1090 }
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;
1094 }
1095
1096 xfs_da_state_free(state);
1097 return(retval);
1098}
1099
1100#if defined(XFS_DIR_TRACE)
1101/*
1102 * Add a trace buffer entry for an inode and a uio.
1103 */
1104void
1105xfs_dir_trace_g_du(char *where, xfs_inode_t *dp, uio_t *uio)
1106{
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);
1113}
1114
1115/*
1116 * Add a trace buffer entry for an inode and a uio.
1117 */
1118void
1119xfs_dir_trace_g_dub(char *where, xfs_inode_t *dp, uio_t *uio, xfs_dablk_t bno)
1120{
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);
1128}
1129
1130/*
1131 * Add a trace buffer entry for an inode and a uio.
1132 */
1133void
1134xfs_dir_trace_g_dun(char *where, xfs_inode_t *dp, uio_t *uio,
1135 xfs_da_intnode_t *node)
1136{
1137 int last = INT_GET(node->hdr.count, ARCH_CONVERT) - 1;
1138
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),
1152 NULL, NULL, NULL);
1153}
1154
1155/*
1156 * Add a trace buffer entry for an inode and a uio.
1157 */
1158void
1159xfs_dir_trace_g_dul(char *where, xfs_inode_t *dp, uio_t *uio,
1160 xfs_dir_leafblock_t *leaf)
1161{
1162 int last = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
1163
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),
1177 NULL, NULL, NULL);
1178}
1179
1180/*
1181 * Add a trace buffer entry for an inode and a uio.
1182 */
1183void
1184xfs_dir_trace_g_due(char *where, xfs_inode_t *dp, uio_t *uio,
1185 xfs_dir_leaf_entry_t *entry)
1186{
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);
1195}
1196
1197/*
1198 * Add a trace buffer entry for an inode and a uio.
1199 */
1200void
1201xfs_dir_trace_g_duc(char *where, xfs_inode_t *dp, uio_t *uio, xfs_off_t cookie)
1202{
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);
1211}
1212
1213/*
1214 * Add a trace buffer entry for the arguments given to the routine,
1215 * generic form.
1216 */
1217void
1218xfs_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)
1225{
1226 ASSERT(xfs_dir_trace_buf);
1227 ktrace_enter(xfs_dir_trace_buf, (void *)(unsigned long)type,
1228 (void *)where,
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,
1233 NULL, NULL);
1234}
1235#endif /* XFS_DIR_TRACE */