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