xfs: merge xfs_ag.h into xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / libxfs / 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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_inum.h"
24 #include "xfs_sb.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_bmap.h"
32 #include "xfs_dir2.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36
37 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
38
39
40 /*
41  * ASCII case-insensitive (ie. A-Z) support for directories that was
42  * used in IRIX.
43  */
44 STATIC xfs_dahash_t
45 xfs_ascii_ci_hashname(
46         struct xfs_name *name)
47 {
48         xfs_dahash_t    hash;
49         int             i;
50
51         for (i = 0, hash = 0; i < name->len; i++)
52                 hash = tolower(name->name[i]) ^ rol32(hash, 7);
53
54         return hash;
55 }
56
57 STATIC enum xfs_dacmp
58 xfs_ascii_ci_compname(
59         struct xfs_da_args *args,
60         const unsigned char *name,
61         int             len)
62 {
63         enum xfs_dacmp  result;
64         int             i;
65
66         if (args->namelen != len)
67                 return XFS_CMP_DIFFERENT;
68
69         result = XFS_CMP_EXACT;
70         for (i = 0; i < len; i++) {
71                 if (args->name[i] == name[i])
72                         continue;
73                 if (tolower(args->name[i]) != tolower(name[i]))
74                         return XFS_CMP_DIFFERENT;
75                 result = XFS_CMP_CASE;
76         }
77
78         return result;
79 }
80
81 static struct xfs_nameops xfs_ascii_ci_nameops = {
82         .hashname       = xfs_ascii_ci_hashname,
83         .compname       = xfs_ascii_ci_compname,
84 };
85
86 int
87 xfs_da_mount(
88         struct xfs_mount        *mp)
89 {
90         struct xfs_da_geometry  *dageo;
91         int                     nodehdr_size;
92
93
94         ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
95         ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
96                XFS_MAX_BLOCKSIZE);
97
98         mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
99         mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
100
101         nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
102         mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
103                                     KM_SLEEP | KM_MAYFAIL);
104         mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
105                                      KM_SLEEP | KM_MAYFAIL);
106         if (!mp->m_dir_geo || !mp->m_attr_geo) {
107                 kmem_free(mp->m_dir_geo);
108                 kmem_free(mp->m_attr_geo);
109                 return -ENOMEM;
110         }
111
112         /* set up directory geometry */
113         dageo = mp->m_dir_geo;
114         dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
115         dageo->fsblog = mp->m_sb.sb_blocklog;
116         dageo->blksize = 1 << dageo->blklog;
117         dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
118
119         /*
120          * Now we've set up the block conversion variables, we can calculate the
121          * segment block constants using the geometry structure.
122          */
123         dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
124         dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
125         dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
126         dageo->node_ents = (dageo->blksize - nodehdr_size) /
127                                 (uint)sizeof(xfs_da_node_entry_t);
128         dageo->magicpct = (dageo->blksize * 37) / 100;
129
130         /* set up attribute geometry - single fsb only */
131         dageo = mp->m_attr_geo;
132         dageo->blklog = mp->m_sb.sb_blocklog;
133         dageo->fsblog = mp->m_sb.sb_blocklog;
134         dageo->blksize = 1 << dageo->blklog;
135         dageo->fsbcount = 1;
136         dageo->node_ents = (dageo->blksize - nodehdr_size) /
137                                 (uint)sizeof(xfs_da_node_entry_t);
138         dageo->magicpct = (dageo->blksize * 37) / 100;
139
140         if (xfs_sb_version_hasasciici(&mp->m_sb))
141                 mp->m_dirnameops = &xfs_ascii_ci_nameops;
142         else
143                 mp->m_dirnameops = &xfs_default_nameops;
144
145         return 0;
146 }
147
148 void
149 xfs_da_unmount(
150         struct xfs_mount        *mp)
151 {
152         kmem_free(mp->m_dir_geo);
153         kmem_free(mp->m_attr_geo);
154 }
155
156 /*
157  * Return 1 if directory contains only "." and "..".
158  */
159 int
160 xfs_dir_isempty(
161         xfs_inode_t     *dp)
162 {
163         xfs_dir2_sf_hdr_t       *sfp;
164
165         ASSERT(S_ISDIR(dp->i_d.di_mode));
166         if (dp->i_d.di_size == 0)       /* might happen during shutdown. */
167                 return 1;
168         if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
169                 return 0;
170         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
171         return !sfp->count;
172 }
173
174 /*
175  * Validate a given inode number.
176  */
177 int
178 xfs_dir_ino_validate(
179         xfs_mount_t     *mp,
180         xfs_ino_t       ino)
181 {
182         xfs_agblock_t   agblkno;
183         xfs_agino_t     agino;
184         xfs_agnumber_t  agno;
185         int             ino_ok;
186         int             ioff;
187
188         agno = XFS_INO_TO_AGNO(mp, ino);
189         agblkno = XFS_INO_TO_AGBNO(mp, ino);
190         ioff = XFS_INO_TO_OFFSET(mp, ino);
191         agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
192         ino_ok =
193                 agno < mp->m_sb.sb_agcount &&
194                 agblkno < mp->m_sb.sb_agblocks &&
195                 agblkno != 0 &&
196                 ioff < (1 << mp->m_sb.sb_inopblog) &&
197                 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
198         if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
199                         XFS_RANDOM_DIR_INO_VALIDATE))) {
200                 xfs_warn(mp, "Invalid inode number 0x%Lx",
201                                 (unsigned long long) ino);
202                 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
203                 return -EFSCORRUPTED;
204         }
205         return 0;
206 }
207
208 /*
209  * Initialize a directory with its "." and ".." entries.
210  */
211 int
212 xfs_dir_init(
213         xfs_trans_t     *tp,
214         xfs_inode_t     *dp,
215         xfs_inode_t     *pdp)
216 {
217         struct xfs_da_args *args;
218         int             error;
219
220         ASSERT(S_ISDIR(dp->i_d.di_mode));
221         error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
222         if (error)
223                 return error;
224
225         args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
226         if (!args)
227                 return -ENOMEM;
228
229         args->geo = dp->i_mount->m_dir_geo;
230         args->dp = dp;
231         args->trans = tp;
232         error = xfs_dir2_sf_create(args, pdp->i_ino);
233         kmem_free(args);
234         return error;
235 }
236
237 /*
238  * Enter a name in a directory, or check for available space.
239  * If inum is 0, only the available space test is performed.
240  */
241 int
242 xfs_dir_createname(
243         xfs_trans_t             *tp,
244         xfs_inode_t             *dp,
245         struct xfs_name         *name,
246         xfs_ino_t               inum,           /* new entry inode number */
247         xfs_fsblock_t           *first,         /* bmap's firstblock */
248         xfs_bmap_free_t         *flist,         /* bmap's freeblock list */
249         xfs_extlen_t            total)          /* bmap's total block count */
250 {
251         struct xfs_da_args      *args;
252         int                     rval;
253         int                     v;              /* type-checking value */
254
255         ASSERT(S_ISDIR(dp->i_d.di_mode));
256         if (inum) {
257                 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
258                 if (rval)
259                         return rval;
260                 XFS_STATS_INC(xs_dir_create);
261         }
262
263         args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
264         if (!args)
265                 return -ENOMEM;
266
267         args->geo = dp->i_mount->m_dir_geo;
268         args->name = name->name;
269         args->namelen = name->len;
270         args->filetype = name->type;
271         args->hashval = dp->i_mount->m_dirnameops->hashname(name);
272         args->inumber = inum;
273         args->dp = dp;
274         args->firstblock = first;
275         args->flist = flist;
276         args->total = total;
277         args->whichfork = XFS_DATA_FORK;
278         args->trans = tp;
279         args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
280         if (!inum)
281                 args->op_flags |= XFS_DA_OP_JUSTCHECK;
282
283         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
284                 rval = xfs_dir2_sf_addname(args);
285                 goto out_free;
286         }
287
288         rval = xfs_dir2_isblock(args, &v);
289         if (rval)
290                 goto out_free;
291         if (v) {
292                 rval = xfs_dir2_block_addname(args);
293                 goto out_free;
294         }
295
296         rval = xfs_dir2_isleaf(args, &v);
297         if (rval)
298                 goto out_free;
299         if (v)
300                 rval = xfs_dir2_leaf_addname(args);
301         else
302                 rval = xfs_dir2_node_addname(args);
303
304 out_free:
305         kmem_free(args);
306         return rval;
307 }
308
309 /*
310  * If doing a CI lookup and case-insensitive match, dup actual name into
311  * args.value. Return EEXIST for success (ie. name found) or an error.
312  */
313 int
314 xfs_dir_cilookup_result(
315         struct xfs_da_args *args,
316         const unsigned char *name,
317         int             len)
318 {
319         if (args->cmpresult == XFS_CMP_DIFFERENT)
320                 return -ENOENT;
321         if (args->cmpresult != XFS_CMP_CASE ||
322                                         !(args->op_flags & XFS_DA_OP_CILOOKUP))
323                 return -EEXIST;
324
325         args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
326         if (!args->value)
327                 return -ENOMEM;
328
329         memcpy(args->value, name, len);
330         args->valuelen = len;
331         return -EEXIST;
332 }
333
334 /*
335  * Lookup a name in a directory, give back the inode number.
336  * If ci_name is not NULL, returns the actual name in ci_name if it differs
337  * to name, or ci_name->name is set to NULL for an exact match.
338  */
339
340 int
341 xfs_dir_lookup(
342         xfs_trans_t     *tp,
343         xfs_inode_t     *dp,
344         struct xfs_name *name,
345         xfs_ino_t       *inum,          /* out: inode number */
346         struct xfs_name *ci_name)       /* out: actual name if CI match */
347 {
348         struct xfs_da_args *args;
349         int             rval;
350         int             v;              /* type-checking value */
351
352         ASSERT(S_ISDIR(dp->i_d.di_mode));
353         XFS_STATS_INC(xs_dir_lookup);
354
355         /*
356          * We need to use KM_NOFS here so that lockdep will not throw false
357          * positive deadlock warnings on a non-transactional lookup path. It is
358          * safe to recurse into inode recalim in that case, but lockdep can't
359          * easily be taught about it. Hence KM_NOFS avoids having to add more
360          * lockdep Doing this avoids having to add a bunch of lockdep class
361          * annotations into the reclaim path for the ilock.
362          */
363         args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
364         args->geo = dp->i_mount->m_dir_geo;
365         args->name = name->name;
366         args->namelen = name->len;
367         args->filetype = name->type;
368         args->hashval = dp->i_mount->m_dirnameops->hashname(name);
369         args->dp = dp;
370         args->whichfork = XFS_DATA_FORK;
371         args->trans = tp;
372         args->op_flags = XFS_DA_OP_OKNOENT;
373         if (ci_name)
374                 args->op_flags |= XFS_DA_OP_CILOOKUP;
375
376         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
377                 rval = xfs_dir2_sf_lookup(args);
378                 goto out_check_rval;
379         }
380
381         rval = xfs_dir2_isblock(args, &v);
382         if (rval)
383                 goto out_free;
384         if (v) {
385                 rval = xfs_dir2_block_lookup(args);
386                 goto out_check_rval;
387         }
388
389         rval = xfs_dir2_isleaf(args, &v);
390         if (rval)
391                 goto out_free;
392         if (v)
393                 rval = xfs_dir2_leaf_lookup(args);
394         else
395                 rval = xfs_dir2_node_lookup(args);
396
397 out_check_rval:
398         if (rval == -EEXIST)
399                 rval = 0;
400         if (!rval) {
401                 *inum = args->inumber;
402                 if (ci_name) {
403                         ci_name->name = args->value;
404                         ci_name->len = args->valuelen;
405                 }
406         }
407 out_free:
408         kmem_free(args);
409         return rval;
410 }
411
412 /*
413  * Remove an entry from a directory.
414  */
415 int
416 xfs_dir_removename(
417         xfs_trans_t     *tp,
418         xfs_inode_t     *dp,
419         struct xfs_name *name,
420         xfs_ino_t       ino,
421         xfs_fsblock_t   *first,         /* bmap's firstblock */
422         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
423         xfs_extlen_t    total)          /* bmap's total block count */
424 {
425         struct xfs_da_args *args;
426         int             rval;
427         int             v;              /* type-checking value */
428
429         ASSERT(S_ISDIR(dp->i_d.di_mode));
430         XFS_STATS_INC(xs_dir_remove);
431
432         args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
433         if (!args)
434                 return -ENOMEM;
435
436         args->geo = dp->i_mount->m_dir_geo;
437         args->name = name->name;
438         args->namelen = name->len;
439         args->filetype = name->type;
440         args->hashval = dp->i_mount->m_dirnameops->hashname(name);
441         args->inumber = ino;
442         args->dp = dp;
443         args->firstblock = first;
444         args->flist = flist;
445         args->total = total;
446         args->whichfork = XFS_DATA_FORK;
447         args->trans = tp;
448
449         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
450                 rval = xfs_dir2_sf_removename(args);
451                 goto out_free;
452         }
453
454         rval = xfs_dir2_isblock(args, &v);
455         if (rval)
456                 goto out_free;
457         if (v) {
458                 rval = xfs_dir2_block_removename(args);
459                 goto out_free;
460         }
461
462         rval = xfs_dir2_isleaf(args, &v);
463         if (rval)
464                 goto out_free;
465         if (v)
466                 rval = xfs_dir2_leaf_removename(args);
467         else
468                 rval = xfs_dir2_node_removename(args);
469 out_free:
470         kmem_free(args);
471         return rval;
472 }
473
474 /*
475  * Replace the inode number of a directory entry.
476  */
477 int
478 xfs_dir_replace(
479         xfs_trans_t     *tp,
480         xfs_inode_t     *dp,
481         struct xfs_name *name,          /* name of entry to replace */
482         xfs_ino_t       inum,           /* new inode number */
483         xfs_fsblock_t   *first,         /* bmap's firstblock */
484         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
485         xfs_extlen_t    total)          /* bmap's total block count */
486 {
487         struct xfs_da_args *args;
488         int             rval;
489         int             v;              /* type-checking value */
490
491         ASSERT(S_ISDIR(dp->i_d.di_mode));
492
493         rval = xfs_dir_ino_validate(tp->t_mountp, inum);
494         if (rval)
495                 return rval;
496
497         args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
498         if (!args)
499                 return -ENOMEM;
500
501         args->geo = dp->i_mount->m_dir_geo;
502         args->name = name->name;
503         args->namelen = name->len;
504         args->filetype = name->type;
505         args->hashval = dp->i_mount->m_dirnameops->hashname(name);
506         args->inumber = inum;
507         args->dp = dp;
508         args->firstblock = first;
509         args->flist = flist;
510         args->total = total;
511         args->whichfork = XFS_DATA_FORK;
512         args->trans = tp;
513
514         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
515                 rval = xfs_dir2_sf_replace(args);
516                 goto out_free;
517         }
518
519         rval = xfs_dir2_isblock(args, &v);
520         if (rval)
521                 goto out_free;
522         if (v) {
523                 rval = xfs_dir2_block_replace(args);
524                 goto out_free;
525         }
526
527         rval = xfs_dir2_isleaf(args, &v);
528         if (rval)
529                 goto out_free;
530         if (v)
531                 rval = xfs_dir2_leaf_replace(args);
532         else
533                 rval = xfs_dir2_node_replace(args);
534 out_free:
535         kmem_free(args);
536         return rval;
537 }
538
539 /*
540  * See if this entry can be added to the directory without allocating space.
541  */
542 int
543 xfs_dir_canenter(
544         xfs_trans_t     *tp,
545         xfs_inode_t     *dp,
546         struct xfs_name *name)          /* name of entry to add */
547 {
548         return xfs_dir_createname(tp, dp, name, 0, NULL, NULL, 0);
549 }
550
551 /*
552  * Utility routines.
553  */
554
555 /*
556  * Add a block to the directory.
557  *
558  * This routine is for data and free blocks, not leaf/node blocks which are
559  * handled by xfs_da_grow_inode.
560  */
561 int
562 xfs_dir2_grow_inode(
563         struct xfs_da_args      *args,
564         int                     space,  /* v2 dir's space XFS_DIR2_xxx_SPACE */
565         xfs_dir2_db_t           *dbp)   /* out: block number added */
566 {
567         struct xfs_inode        *dp = args->dp;
568         struct xfs_mount        *mp = dp->i_mount;
569         xfs_fileoff_t           bno;    /* directory offset of new block */
570         int                     count;  /* count of filesystem blocks */
571         int                     error;
572
573         trace_xfs_dir2_grow_inode(args, space);
574
575         /*
576          * Set lowest possible block in the space requested.
577          */
578         bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
579         count = args->geo->fsbcount;
580
581         error = xfs_da_grow_inode_int(args, &bno, count);
582         if (error)
583                 return error;
584
585         *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
586
587         /*
588          * Update file's size if this is the data space and it grew.
589          */
590         if (space == XFS_DIR2_DATA_SPACE) {
591                 xfs_fsize_t     size;           /* directory file (data) size */
592
593                 size = XFS_FSB_TO_B(mp, bno + count);
594                 if (size > dp->i_d.di_size) {
595                         dp->i_d.di_size = size;
596                         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
597                 }
598         }
599         return 0;
600 }
601
602 /*
603  * See if the directory is a single-block form directory.
604  */
605 int
606 xfs_dir2_isblock(
607         struct xfs_da_args      *args,
608         int                     *vp)    /* out: 1 is block, 0 is not block */
609 {
610         xfs_fileoff_t           last;   /* last file offset */
611         int                     rval;
612
613         if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
614                 return rval;
615         rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
616         ASSERT(rval == 0 || args->dp->i_d.di_size == args->geo->blksize);
617         *vp = rval;
618         return 0;
619 }
620
621 /*
622  * See if the directory is a single-leaf form directory.
623  */
624 int
625 xfs_dir2_isleaf(
626         struct xfs_da_args      *args,
627         int                     *vp)    /* out: 1 is block, 0 is not block */
628 {
629         xfs_fileoff_t           last;   /* last file offset */
630         int                     rval;
631
632         if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
633                 return rval;
634         *vp = last == args->geo->leafblk + args->geo->fsbcount;
635         return 0;
636 }
637
638 /*
639  * Remove the given block from the directory.
640  * This routine is used for data and free blocks, leaf/node are done
641  * by xfs_da_shrink_inode.
642  */
643 int
644 xfs_dir2_shrink_inode(
645         xfs_da_args_t   *args,
646         xfs_dir2_db_t   db,
647         struct xfs_buf  *bp)
648 {
649         xfs_fileoff_t   bno;            /* directory file offset */
650         xfs_dablk_t     da;             /* directory file offset */
651         int             done;           /* bunmap is finished */
652         xfs_inode_t     *dp;
653         int             error;
654         xfs_mount_t     *mp;
655         xfs_trans_t     *tp;
656
657         trace_xfs_dir2_shrink_inode(args, db);
658
659         dp = args->dp;
660         mp = dp->i_mount;
661         tp = args->trans;
662         da = xfs_dir2_db_to_da(args->geo, db);
663         /*
664          * Unmap the fsblock(s).
665          */
666         if ((error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount,
667                         XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
668                         &done))) {
669                 /*
670                  * ENOSPC actually can happen if we're in a removename with
671                  * no space reservation, and the resulting block removal
672                  * would cause a bmap btree split or conversion from extents
673                  * to btree.  This can only happen for un-fragmented
674                  * directory blocks, since you need to be punching out
675                  * the middle of an extent.
676                  * In this case we need to leave the block in the file,
677                  * and not binval it.
678                  * So the block has to be in a consistent empty state
679                  * and appropriately logged.
680                  * We don't free up the buffer, the caller can tell it
681                  * hasn't happened since it got an error back.
682                  */
683                 return error;
684         }
685         ASSERT(done);
686         /*
687          * Invalidate the buffer from the transaction.
688          */
689         xfs_trans_binval(tp, bp);
690         /*
691          * If it's not a data block, we're done.
692          */
693         if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
694                 return 0;
695         /*
696          * If the block isn't the last one in the directory, we're done.
697          */
698         if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
699                 return 0;
700         bno = da;
701         if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
702                 /*
703                  * This can't really happen unless there's kernel corruption.
704                  */
705                 return error;
706         }
707         if (db == args->geo->datablk)
708                 ASSERT(bno == 0);
709         else
710                 ASSERT(bno > 0);
711         /*
712          * Set the size to the new last block.
713          */
714         dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
715         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
716         return 0;
717 }