]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/xfs/xfs_dir2.c
xfs: drop dmapi hooks
[net-next-2.6.git] / fs / xfs / xfs_dir2.c
1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_inode_item.h"
37 #include "xfs_bmap.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_dir2_node.h"
42 #include "xfs_error.h"
43 #include "xfs_vnodeops.h"
44 #include "xfs_trace.h"
45
46 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2};
47
48 /*
49  * ASCII case-insensitive (ie. A-Z) support for directories that was
50  * used in IRIX.
51  */
52 STATIC xfs_dahash_t
53 xfs_ascii_ci_hashname(
54         struct xfs_name *name)
55 {
56         xfs_dahash_t    hash;
57         int             i;
58
59         for (i = 0, hash = 0; i < name->len; i++)
60                 hash = tolower(name->name[i]) ^ rol32(hash, 7);
61
62         return hash;
63 }
64
65 STATIC enum xfs_dacmp
66 xfs_ascii_ci_compname(
67         struct xfs_da_args *args,
68         const unsigned char *name,
69         int             len)
70 {
71         enum xfs_dacmp  result;
72         int             i;
73
74         if (args->namelen != len)
75                 return XFS_CMP_DIFFERENT;
76
77         result = XFS_CMP_EXACT;
78         for (i = 0; i < len; i++) {
79                 if (args->name[i] == name[i])
80                         continue;
81                 if (tolower(args->name[i]) != tolower(name[i]))
82                         return XFS_CMP_DIFFERENT;
83                 result = XFS_CMP_CASE;
84         }
85
86         return result;
87 }
88
89 static struct xfs_nameops xfs_ascii_ci_nameops = {
90         .hashname       = xfs_ascii_ci_hashname,
91         .compname       = xfs_ascii_ci_compname,
92 };
93
94 void
95 xfs_dir_mount(
96         xfs_mount_t     *mp)
97 {
98         ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
99         ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
100                XFS_MAX_BLOCKSIZE);
101         mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
102         mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
103         mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
104         mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
105         mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
106         mp->m_attr_node_ents =
107                 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
108                 (uint)sizeof(xfs_da_node_entry_t);
109         mp->m_dir_node_ents =
110                 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
111                 (uint)sizeof(xfs_da_node_entry_t);
112         mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
113         if (xfs_sb_version_hasasciici(&mp->m_sb))
114                 mp->m_dirnameops = &xfs_ascii_ci_nameops;
115         else
116                 mp->m_dirnameops = &xfs_default_nameops;
117 }
118
119 /*
120  * Return 1 if directory contains only "." and "..".
121  */
122 int
123 xfs_dir_isempty(
124         xfs_inode_t     *dp)
125 {
126         xfs_dir2_sf_t   *sfp;
127
128         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
129         if (dp->i_d.di_size == 0)       /* might happen during shutdown. */
130                 return 1;
131         if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
132                 return 0;
133         sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
134         return !sfp->hdr.count;
135 }
136
137 /*
138  * Validate a given inode number.
139  */
140 int
141 xfs_dir_ino_validate(
142         xfs_mount_t     *mp,
143         xfs_ino_t       ino)
144 {
145         xfs_agblock_t   agblkno;
146         xfs_agino_t     agino;
147         xfs_agnumber_t  agno;
148         int             ino_ok;
149         int             ioff;
150
151         agno = XFS_INO_TO_AGNO(mp, ino);
152         agblkno = XFS_INO_TO_AGBNO(mp, ino);
153         ioff = XFS_INO_TO_OFFSET(mp, ino);
154         agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
155         ino_ok =
156                 agno < mp->m_sb.sb_agcount &&
157                 agblkno < mp->m_sb.sb_agblocks &&
158                 agblkno != 0 &&
159                 ioff < (1 << mp->m_sb.sb_inopblog) &&
160                 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
161         if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
162                         XFS_RANDOM_DIR_INO_VALIDATE))) {
163                 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
164                                 (unsigned long long) ino);
165                 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
166                 return XFS_ERROR(EFSCORRUPTED);
167         }
168         return 0;
169 }
170
171 /*
172  * Initialize a directory with its "." and ".." entries.
173  */
174 int
175 xfs_dir_init(
176         xfs_trans_t     *tp,
177         xfs_inode_t     *dp,
178         xfs_inode_t     *pdp)
179 {
180         xfs_da_args_t   args;
181         int             error;
182
183         memset((char *)&args, 0, sizeof(args));
184         args.dp = dp;
185         args.trans = tp;
186         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
187         if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
188                 return error;
189         return xfs_dir2_sf_create(&args, pdp->i_ino);
190 }
191
192 /*
193   Enter a name in a directory.
194  */
195 int
196 xfs_dir_createname(
197         xfs_trans_t             *tp,
198         xfs_inode_t             *dp,
199         struct xfs_name         *name,
200         xfs_ino_t               inum,           /* new entry inode number */
201         xfs_fsblock_t           *first,         /* bmap's firstblock */
202         xfs_bmap_free_t         *flist,         /* bmap's freeblock list */
203         xfs_extlen_t            total)          /* bmap's total block count */
204 {
205         xfs_da_args_t           args;
206         int                     rval;
207         int                     v;              /* type-checking value */
208
209         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
210         if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
211                 return rval;
212         XFS_STATS_INC(xs_dir_create);
213
214         memset(&args, 0, sizeof(xfs_da_args_t));
215         args.name = name->name;
216         args.namelen = name->len;
217         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
218         args.inumber = inum;
219         args.dp = dp;
220         args.firstblock = first;
221         args.flist = flist;
222         args.total = total;
223         args.whichfork = XFS_DATA_FORK;
224         args.trans = tp;
225         args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
226
227         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
228                 rval = xfs_dir2_sf_addname(&args);
229         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
230                 return rval;
231         else if (v)
232                 rval = xfs_dir2_block_addname(&args);
233         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
234                 return rval;
235         else if (v)
236                 rval = xfs_dir2_leaf_addname(&args);
237         else
238                 rval = xfs_dir2_node_addname(&args);
239         return rval;
240 }
241
242 /*
243  * If doing a CI lookup and case-insensitive match, dup actual name into
244  * args.value. Return EEXIST for success (ie. name found) or an error.
245  */
246 int
247 xfs_dir_cilookup_result(
248         struct xfs_da_args *args,
249         const unsigned char *name,
250         int             len)
251 {
252         if (args->cmpresult == XFS_CMP_DIFFERENT)
253                 return ENOENT;
254         if (args->cmpresult != XFS_CMP_CASE ||
255                                         !(args->op_flags & XFS_DA_OP_CILOOKUP))
256                 return EEXIST;
257
258         args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
259         if (!args->value)
260                 return ENOMEM;
261
262         memcpy(args->value, name, len);
263         args->valuelen = len;
264         return EEXIST;
265 }
266
267 /*
268  * Lookup a name in a directory, give back the inode number.
269  * If ci_name is not NULL, returns the actual name in ci_name if it differs
270  * to name, or ci_name->name is set to NULL for an exact match.
271  */
272
273 int
274 xfs_dir_lookup(
275         xfs_trans_t     *tp,
276         xfs_inode_t     *dp,
277         struct xfs_name *name,
278         xfs_ino_t       *inum,          /* out: inode number */
279         struct xfs_name *ci_name)       /* out: actual name if CI match */
280 {
281         xfs_da_args_t   args;
282         int             rval;
283         int             v;              /* type-checking value */
284
285         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
286         XFS_STATS_INC(xs_dir_lookup);
287
288         memset(&args, 0, sizeof(xfs_da_args_t));
289         args.name = name->name;
290         args.namelen = name->len;
291         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
292         args.dp = dp;
293         args.whichfork = XFS_DATA_FORK;
294         args.trans = tp;
295         args.op_flags = XFS_DA_OP_OKNOENT;
296         if (ci_name)
297                 args.op_flags |= XFS_DA_OP_CILOOKUP;
298
299         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
300                 rval = xfs_dir2_sf_lookup(&args);
301         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
302                 return rval;
303         else if (v)
304                 rval = xfs_dir2_block_lookup(&args);
305         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
306                 return rval;
307         else if (v)
308                 rval = xfs_dir2_leaf_lookup(&args);
309         else
310                 rval = xfs_dir2_node_lookup(&args);
311         if (rval == EEXIST)
312                 rval = 0;
313         if (!rval) {
314                 *inum = args.inumber;
315                 if (ci_name) {
316                         ci_name->name = args.value;
317                         ci_name->len = args.valuelen;
318                 }
319         }
320         return rval;
321 }
322
323 /*
324  * Remove an entry from a directory.
325  */
326 int
327 xfs_dir_removename(
328         xfs_trans_t     *tp,
329         xfs_inode_t     *dp,
330         struct xfs_name *name,
331         xfs_ino_t       ino,
332         xfs_fsblock_t   *first,         /* bmap's firstblock */
333         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
334         xfs_extlen_t    total)          /* bmap's total block count */
335 {
336         xfs_da_args_t   args;
337         int             rval;
338         int             v;              /* type-checking value */
339
340         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
341         XFS_STATS_INC(xs_dir_remove);
342
343         memset(&args, 0, sizeof(xfs_da_args_t));
344         args.name = name->name;
345         args.namelen = name->len;
346         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
347         args.inumber = ino;
348         args.dp = dp;
349         args.firstblock = first;
350         args.flist = flist;
351         args.total = total;
352         args.whichfork = XFS_DATA_FORK;
353         args.trans = tp;
354
355         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
356                 rval = xfs_dir2_sf_removename(&args);
357         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
358                 return rval;
359         else if (v)
360                 rval = xfs_dir2_block_removename(&args);
361         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
362                 return rval;
363         else if (v)
364                 rval = xfs_dir2_leaf_removename(&args);
365         else
366                 rval = xfs_dir2_node_removename(&args);
367         return rval;
368 }
369
370 /*
371  * Read a directory.
372  */
373 int
374 xfs_readdir(
375         xfs_inode_t     *dp,
376         void            *dirent,
377         size_t          bufsize,
378         xfs_off_t       *offset,
379         filldir_t       filldir)
380 {
381         int             rval;           /* return value */
382         int             v;              /* type-checking value */
383
384         xfs_itrace_entry(dp);
385
386         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
387                 return XFS_ERROR(EIO);
388
389         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
390         XFS_STATS_INC(xs_dir_getdents);
391
392         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
393                 rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
394         else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
395                 ;
396         else if (v)
397                 rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
398         else
399                 rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
400                                               filldir);
401         return rval;
402 }
403
404 /*
405  * Replace the inode number of a directory entry.
406  */
407 int
408 xfs_dir_replace(
409         xfs_trans_t     *tp,
410         xfs_inode_t     *dp,
411         struct xfs_name *name,          /* name of entry to replace */
412         xfs_ino_t       inum,           /* new inode number */
413         xfs_fsblock_t   *first,         /* bmap's firstblock */
414         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
415         xfs_extlen_t    total)          /* bmap's total block count */
416 {
417         xfs_da_args_t   args;
418         int             rval;
419         int             v;              /* type-checking value */
420
421         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
422
423         if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
424                 return rval;
425
426         memset(&args, 0, sizeof(xfs_da_args_t));
427         args.name = name->name;
428         args.namelen = name->len;
429         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
430         args.inumber = inum;
431         args.dp = dp;
432         args.firstblock = first;
433         args.flist = flist;
434         args.total = total;
435         args.whichfork = XFS_DATA_FORK;
436         args.trans = tp;
437
438         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
439                 rval = xfs_dir2_sf_replace(&args);
440         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
441                 return rval;
442         else if (v)
443                 rval = xfs_dir2_block_replace(&args);
444         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
445                 return rval;
446         else if (v)
447                 rval = xfs_dir2_leaf_replace(&args);
448         else
449                 rval = xfs_dir2_node_replace(&args);
450         return rval;
451 }
452
453 /*
454  * See if this entry can be added to the directory without allocating space.
455  * First checks that the caller couldn't reserve enough space (resblks = 0).
456  */
457 int
458 xfs_dir_canenter(
459         xfs_trans_t     *tp,
460         xfs_inode_t     *dp,
461         struct xfs_name *name,          /* name of entry to add */
462         uint            resblks)
463 {
464         xfs_da_args_t   args;
465         int             rval;
466         int             v;              /* type-checking value */
467
468         if (resblks)
469                 return 0;
470
471         ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
472
473         memset(&args, 0, sizeof(xfs_da_args_t));
474         args.name = name->name;
475         args.namelen = name->len;
476         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
477         args.dp = dp;
478         args.whichfork = XFS_DATA_FORK;
479         args.trans = tp;
480         args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
481                                                         XFS_DA_OP_OKNOENT;
482
483         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
484                 rval = xfs_dir2_sf_addname(&args);
485         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
486                 return rval;
487         else if (v)
488                 rval = xfs_dir2_block_addname(&args);
489         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
490                 return rval;
491         else if (v)
492                 rval = xfs_dir2_leaf_addname(&args);
493         else
494                 rval = xfs_dir2_node_addname(&args);
495         return rval;
496 }
497
498 /*
499  * Utility routines.
500  */
501
502 /*
503  * Add a block to the directory.
504  * This routine is for data and free blocks, not leaf/node blocks
505  * which are handled by xfs_da_grow_inode.
506  */
507 int
508 xfs_dir2_grow_inode(
509         xfs_da_args_t   *args,
510         int             space,          /* v2 dir's space XFS_DIR2_xxx_SPACE */
511         xfs_dir2_db_t   *dbp)           /* out: block number added */
512 {
513         xfs_fileoff_t   bno;            /* directory offset of new block */
514         int             count;          /* count of filesystem blocks */
515         xfs_inode_t     *dp;            /* incore directory inode */
516         int             error;
517         int             got;            /* blocks actually mapped */
518         int             i;
519         xfs_bmbt_irec_t map;            /* single structure for bmap */
520         int             mapi;           /* mapping index */
521         xfs_bmbt_irec_t *mapp;          /* bmap mapping structure(s) */
522         xfs_mount_t     *mp;
523         int             nmap;           /* number of bmap entries */
524         xfs_trans_t     *tp;
525         xfs_drfsbno_t   nblks;
526
527         trace_xfs_dir2_grow_inode(args, space);
528
529         dp = args->dp;
530         tp = args->trans;
531         mp = dp->i_mount;
532         nblks = dp->i_d.di_nblocks;
533         /*
534          * Set lowest possible block in the space requested.
535          */
536         bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
537         count = mp->m_dirblkfsbs;
538         /*
539          * Find the first hole for our block.
540          */
541         if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
542                 return error;
543         nmap = 1;
544         ASSERT(args->firstblock != NULL);
545         /*
546          * Try mapping the new block contiguously (one extent).
547          */
548         if ((error = xfs_bmapi(tp, dp, bno, count,
549                         XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
550                         args->firstblock, args->total, &map, &nmap,
551                         args->flist, NULL)))
552                 return error;
553         ASSERT(nmap <= 1);
554         if (nmap == 1) {
555                 mapp = &map;
556                 mapi = 1;
557         }
558         /*
559          * Didn't work and this is a multiple-fsb directory block.
560          * Try again with contiguous flag turned on.
561          */
562         else if (nmap == 0 && count > 1) {
563                 xfs_fileoff_t   b;      /* current file offset */
564
565                 /*
566                  * Space for maximum number of mappings.
567                  */
568                 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
569                 /*
570                  * Iterate until we get to the end of our block.
571                  */
572                 for (b = bno, mapi = 0; b < bno + count; ) {
573                         int     c;      /* current fsb count */
574
575                         /*
576                          * Can't map more than MAX_NMAP at once.
577                          */
578                         nmap = MIN(XFS_BMAP_MAX_NMAP, count);
579                         c = (int)(bno + count - b);
580                         if ((error = xfs_bmapi(tp, dp, b, c,
581                                         XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
582                                         args->firstblock, args->total,
583                                         &mapp[mapi], &nmap, args->flist,
584                                         NULL))) {
585                                 kmem_free(mapp);
586                                 return error;
587                         }
588                         if (nmap < 1)
589                                 break;
590                         /*
591                          * Add this bunch into our table, go to the next offset.
592                          */
593                         mapi += nmap;
594                         b = mapp[mapi - 1].br_startoff +
595                             mapp[mapi - 1].br_blockcount;
596                 }
597         }
598         /*
599          * Didn't work.
600          */
601         else {
602                 mapi = 0;
603                 mapp = NULL;
604         }
605         /*
606          * See how many fsb's we got.
607          */
608         for (i = 0, got = 0; i < mapi; i++)
609                 got += mapp[i].br_blockcount;
610         /*
611          * Didn't get enough fsb's, or the first/last block's are wrong.
612          */
613         if (got != count || mapp[0].br_startoff != bno ||
614             mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
615             bno + count) {
616                 if (mapp != &map)
617                         kmem_free(mapp);
618                 return XFS_ERROR(ENOSPC);
619         }
620         /*
621          * Done with the temporary mapping table.
622          */
623         if (mapp != &map)
624                 kmem_free(mapp);
625
626         /* account for newly allocated blocks in reserved blocks total */
627         args->total -= dp->i_d.di_nblocks - nblks;
628         *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
629
630         /*
631          * Update file's size if this is the data space and it grew.
632          */
633         if (space == XFS_DIR2_DATA_SPACE) {
634                 xfs_fsize_t     size;           /* directory file (data) size */
635
636                 size = XFS_FSB_TO_B(mp, bno + count);
637                 if (size > dp->i_d.di_size) {
638                         dp->i_d.di_size = size;
639                         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
640                 }
641         }
642         return 0;
643 }
644
645 /*
646  * See if the directory is a single-block form directory.
647  */
648 int
649 xfs_dir2_isblock(
650         xfs_trans_t     *tp,
651         xfs_inode_t     *dp,
652         int             *vp)            /* out: 1 is block, 0 is not block */
653 {
654         xfs_fileoff_t   last;           /* last file offset */
655         xfs_mount_t     *mp;
656         int             rval;
657
658         mp = dp->i_mount;
659         if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
660                 return rval;
661         rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
662         ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
663         *vp = rval;
664         return 0;
665 }
666
667 /*
668  * See if the directory is a single-leaf form directory.
669  */
670 int
671 xfs_dir2_isleaf(
672         xfs_trans_t     *tp,
673         xfs_inode_t     *dp,
674         int             *vp)            /* out: 1 is leaf, 0 is not leaf */
675 {
676         xfs_fileoff_t   last;           /* last file offset */
677         xfs_mount_t     *mp;
678         int             rval;
679
680         mp = dp->i_mount;
681         if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
682                 return rval;
683         *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
684         return 0;
685 }
686
687 /*
688  * Remove the given block from the directory.
689  * This routine is used for data and free blocks, leaf/node are done
690  * by xfs_da_shrink_inode.
691  */
692 int
693 xfs_dir2_shrink_inode(
694         xfs_da_args_t   *args,
695         xfs_dir2_db_t   db,
696         xfs_dabuf_t     *bp)
697 {
698         xfs_fileoff_t   bno;            /* directory file offset */
699         xfs_dablk_t     da;             /* directory file offset */
700         int             done;           /* bunmap is finished */
701         xfs_inode_t     *dp;
702         int             error;
703         xfs_mount_t     *mp;
704         xfs_trans_t     *tp;
705
706         trace_xfs_dir2_shrink_inode(args, db);
707
708         dp = args->dp;
709         mp = dp->i_mount;
710         tp = args->trans;
711         da = xfs_dir2_db_to_da(mp, db);
712         /*
713          * Unmap the fsblock(s).
714          */
715         if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
716                         XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
717                         NULL, &done))) {
718                 /*
719                  * ENOSPC actually can happen if we're in a removename with
720                  * no space reservation, and the resulting block removal
721                  * would cause a bmap btree split or conversion from extents
722                  * to btree.  This can only happen for un-fragmented
723                  * directory blocks, since you need to be punching out
724                  * the middle of an extent.
725                  * In this case we need to leave the block in the file,
726                  * and not binval it.
727                  * So the block has to be in a consistent empty state
728                  * and appropriately logged.
729                  * We don't free up the buffer, the caller can tell it
730                  * hasn't happened since it got an error back.
731                  */
732                 return error;
733         }
734         ASSERT(done);
735         /*
736          * Invalidate the buffer from the transaction.
737          */
738         xfs_da_binval(tp, bp);
739         /*
740          * If it's not a data block, we're done.
741          */
742         if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
743                 return 0;
744         /*
745          * If the block isn't the last one in the directory, we're done.
746          */
747         if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
748                 return 0;
749         bno = da;
750         if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
751                 /*
752                  * This can't really happen unless there's kernel corruption.
753                  */
754                 return error;
755         }
756         if (db == mp->m_dirdatablk)
757                 ASSERT(bno == 0);
758         else
759                 ASSERT(bno > 0);
760         /*
761          * Set the size to the new last block.
762          */
763         dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
764         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
765         return 0;
766 }