2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_da_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_inode_item.h"
55 #include "xfs_dir_leaf.h"
56 #include "xfs_dir2_data.h"
57 #include "xfs_dir2_leaf.h"
58 #include "xfs_dir2_block.h"
59 #include "xfs_dir2_node.h"
60 #include "xfs_dir2_trace.h"
61 #include "xfs_error.h"
64 * Declarations for interface routines.
66 static void xfs_dir2_mount(xfs_mount_t *mp);
67 static int xfs_dir2_isempty(xfs_inode_t *dp);
68 static int xfs_dir2_init(xfs_trans_t *tp, xfs_inode_t *dp,
70 static int xfs_dir2_createname(xfs_trans_t *tp, xfs_inode_t *dp,
71 char *name, int namelen, xfs_ino_t inum,
73 xfs_bmap_free_t *flist, xfs_extlen_t total);
74 static int xfs_dir2_lookup(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
75 int namelen, xfs_ino_t *inum);
76 static int xfs_dir2_removename(xfs_trans_t *tp, xfs_inode_t *dp,
77 char *name, int namelen, xfs_ino_t ino,
79 xfs_bmap_free_t *flist, xfs_extlen_t total);
80 static int xfs_dir2_getdents(xfs_trans_t *tp, xfs_inode_t *dp, uio_t *uio,
82 static int xfs_dir2_replace(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
83 int namelen, xfs_ino_t inum,
84 xfs_fsblock_t *first, xfs_bmap_free_t *flist,
86 static int xfs_dir2_canenter(xfs_trans_t *tp, xfs_inode_t *dp, char *name,
88 static int xfs_dir2_shortform_validate_ondisk(xfs_mount_t *mp,
92 * Utility routine declarations.
94 static int xfs_dir2_put_dirent64_direct(xfs_dir2_put_args_t *pa);
95 static int xfs_dir2_put_dirent64_uio(xfs_dir2_put_args_t *pa);
98 * Directory operations vector.
100 xfs_dirops_t xfsv2_dirops = {
101 .xd_mount = xfs_dir2_mount,
102 .xd_isempty = xfs_dir2_isempty,
103 .xd_init = xfs_dir2_init,
104 .xd_createname = xfs_dir2_createname,
105 .xd_lookup = xfs_dir2_lookup,
106 .xd_removename = xfs_dir2_removename,
107 .xd_getdents = xfs_dir2_getdents,
108 .xd_replace = xfs_dir2_replace,
109 .xd_canenter = xfs_dir2_canenter,
110 .xd_shortform_validate_ondisk = xfs_dir2_shortform_validate_ondisk,
111 .xd_shortform_to_single = xfs_dir2_sf_to_block,
115 * Interface routines.
119 * Initialize directory-related fields in the mount structure.
123 xfs_mount_t *mp) /* filesystem mount point */
125 mp->m_dirversion = 2;
126 ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
128 mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
129 mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
130 mp->m_dirdatablk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_DATA_FIRSTDB(mp));
131 mp->m_dirleafblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
132 mp->m_dirfreeblk = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_FREE_FIRSTDB(mp));
133 mp->m_attr_node_ents =
134 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
135 (uint)sizeof(xfs_da_node_entry_t);
136 mp->m_dir_node_ents =
137 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
138 (uint)sizeof(xfs_da_node_entry_t);
139 mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
143 * Return 1 if directory contains only "." and "..".
145 static int /* return code */
147 xfs_inode_t *dp) /* incore inode structure */
149 xfs_dir2_sf_t *sfp; /* shortform directory structure */
151 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
153 * Might happen during shutdown.
155 if (dp->i_d.di_size == 0) {
158 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
160 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
161 return !sfp->hdr.count;
165 * Initialize a directory with its "." and ".." entries.
167 static int /* error */
169 xfs_trans_t *tp, /* transaction pointer */
170 xfs_inode_t *dp, /* incore directory inode */
171 xfs_inode_t *pdp) /* incore parent directory inode */
173 xfs_da_args_t args; /* operation arguments */
174 int error; /* error return value */
176 memset((char *)&args, 0, sizeof(args));
179 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
180 if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) {
183 return xfs_dir2_sf_create(&args, pdp->i_ino);
187 Enter a name in a directory.
189 static int /* error */
191 xfs_trans_t *tp, /* transaction pointer */
192 xfs_inode_t *dp, /* incore directory inode */
193 char *name, /* new entry name */
194 int namelen, /* new entry name length */
195 xfs_ino_t inum, /* new entry inode number */
196 xfs_fsblock_t *first, /* bmap's firstblock */
197 xfs_bmap_free_t *flist, /* bmap's freeblock list */
198 xfs_extlen_t total) /* bmap's total block count */
200 xfs_da_args_t args; /* operation arguments */
201 int rval; /* return value */
202 int v; /* type-checking value */
204 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
205 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
208 XFS_STATS_INC(xs_dir_create);
210 * Fill in the arg structure for this request.
213 args.namelen = namelen;
214 args.hashval = xfs_da_hashname(name, namelen);
217 args.firstblock = first;
220 args.whichfork = XFS_DATA_FORK;
223 args.addname = args.oknoent = 1;
225 * Decide on what work routines to call based on the inode size.
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))) {
232 rval = xfs_dir2_block_addname(&args);
233 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
236 rval = xfs_dir2_leaf_addname(&args);
238 rval = xfs_dir2_node_addname(&args);
243 * Lookup a name in a directory, give back the inode number.
245 static int /* error */
247 xfs_trans_t *tp, /* transaction pointer */
248 xfs_inode_t *dp, /* incore directory inode */
249 char *name, /* lookup name */
250 int namelen, /* lookup name length */
251 xfs_ino_t *inum) /* out: inode number */
253 xfs_da_args_t args; /* operation arguments */
254 int rval; /* return value */
255 int v; /* type-checking value */
257 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
258 XFS_STATS_INC(xs_dir_lookup);
261 * Fill in the arg structure for this request.
264 args.namelen = namelen;
265 args.hashval = xfs_da_hashname(name, namelen);
268 args.firstblock = NULL;
271 args.whichfork = XFS_DATA_FORK;
273 args.justcheck = args.addname = 0;
276 * Decide on what work routines to call based on the inode size.
278 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
279 rval = xfs_dir2_sf_lookup(&args);
280 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
283 rval = xfs_dir2_block_lookup(&args);
284 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
287 rval = xfs_dir2_leaf_lookup(&args);
289 rval = xfs_dir2_node_lookup(&args);
293 *inum = args.inumber;
298 * Remove an entry from a directory.
300 static int /* error */
302 xfs_trans_t *tp, /* transaction pointer */
303 xfs_inode_t *dp, /* incore directory inode */
304 char *name, /* name of entry to remove */
305 int namelen, /* name length of entry to remove */
306 xfs_ino_t ino, /* inode number of entry to remove */
307 xfs_fsblock_t *first, /* bmap's firstblock */
308 xfs_bmap_free_t *flist, /* bmap's freeblock list */
309 xfs_extlen_t total) /* bmap's total block count */
311 xfs_da_args_t args; /* operation arguments */
312 int rval; /* return value */
313 int v; /* type-checking value */
315 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
316 XFS_STATS_INC(xs_dir_remove);
318 * Fill in the arg structure for this request.
321 args.namelen = namelen;
322 args.hashval = xfs_da_hashname(name, namelen);
325 args.firstblock = first;
328 args.whichfork = XFS_DATA_FORK;
330 args.justcheck = args.addname = args.oknoent = 0;
332 * Decide on what work routines to call based on the inode size.
334 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
335 rval = xfs_dir2_sf_removename(&args);
336 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
339 rval = xfs_dir2_block_removename(&args);
340 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
343 rval = xfs_dir2_leaf_removename(&args);
345 rval = xfs_dir2_node_removename(&args);
352 static int /* error */
354 xfs_trans_t *tp, /* transaction pointer */
355 xfs_inode_t *dp, /* incore directory inode */
356 uio_t *uio, /* caller's buffer control */
357 int *eofp) /* out: eof reached */
359 int alignment; /* alignment required for ABI */
360 xfs_dirent_t *dbp; /* malloc'ed buffer */
361 xfs_dir2_put_t put; /* entry formatting routine */
362 int rval; /* return value */
363 int v; /* type-checking value */
365 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
366 XFS_STATS_INC(xs_dir_getdents);
368 * If our caller has given us a single contiguous aligned memory buffer,
369 * just work directly within that buffer. If it's in user memory,
370 * lock it down first.
372 alignment = sizeof(xfs_off_t) - 1;
373 if ((uio->uio_iovcnt == 1) &&
374 (((__psint_t)uio->uio_iov[0].iov_base & alignment) == 0) &&
375 ((uio->uio_iov[0].iov_len & alignment) == 0)) {
377 put = xfs_dir2_put_dirent64_direct;
379 dbp = kmem_alloc(sizeof(*dbp) + MAXNAMELEN, KM_SLEEP);
380 put = xfs_dir2_put_dirent64_uio;
385 * Decide on what work routines to call based on the inode size.
387 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
388 rval = xfs_dir2_sf_getdents(dp, uio, eofp, dbp, put);
389 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
392 rval = xfs_dir2_block_getdents(tp, dp, uio, eofp, dbp, put);
394 rval = xfs_dir2_leaf_getdents(tp, dp, uio, eofp, dbp, put);
396 kmem_free(dbp, sizeof(*dbp) + MAXNAMELEN);
401 * Replace the inode number of a directory entry.
403 static int /* error */
405 xfs_trans_t *tp, /* transaction pointer */
406 xfs_inode_t *dp, /* incore directory inode */
407 char *name, /* name of entry to replace */
408 int namelen, /* name length of entry to replace */
409 xfs_ino_t inum, /* new inode number */
410 xfs_fsblock_t *first, /* bmap's firstblock */
411 xfs_bmap_free_t *flist, /* bmap's freeblock list */
412 xfs_extlen_t total) /* bmap's total block count */
414 xfs_da_args_t args; /* operation arguments */
415 int rval; /* return value */
416 int v; /* type-checking value */
418 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
420 if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) {
424 * Fill in the arg structure for this request.
427 args.namelen = namelen;
428 args.hashval = xfs_da_hashname(name, namelen);
431 args.firstblock = first;
434 args.whichfork = XFS_DATA_FORK;
436 args.justcheck = args.addname = args.oknoent = 0;
438 * Decide on what work routines to call based on the inode size.
440 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
441 rval = xfs_dir2_sf_replace(&args);
442 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
445 rval = xfs_dir2_block_replace(&args);
446 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
449 rval = xfs_dir2_leaf_replace(&args);
451 rval = xfs_dir2_node_replace(&args);
456 * See if this entry can be added to the directory without allocating space.
458 static int /* error */
460 xfs_trans_t *tp, /* transaction pointer */
461 xfs_inode_t *dp, /* incore directory inode */
462 char *name, /* name of entry to add */
463 int namelen) /* name length of entry to add */
465 xfs_da_args_t args; /* operation arguments */
466 int rval; /* return value */
467 int v; /* type-checking value */
469 ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
471 * Fill in the arg structure for this request.
474 args.namelen = namelen;
475 args.hashval = xfs_da_hashname(name, namelen);
478 args.firstblock = NULL;
481 args.whichfork = XFS_DATA_FORK;
483 args.justcheck = args.addname = args.oknoent = 1;
485 * Decide on what work routines to call based on the inode size.
487 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
488 rval = xfs_dir2_sf_addname(&args);
489 else if ((rval = xfs_dir2_isblock(tp, dp, &v))) {
492 rval = xfs_dir2_block_addname(&args);
493 else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) {
496 rval = xfs_dir2_leaf_addname(&args);
498 rval = xfs_dir2_node_addname(&args);
503 * Dummy routine for shortform inode validation.
504 * Can't really do this.
507 static int /* error */
508 xfs_dir2_shortform_validate_ondisk(
509 xfs_mount_t *mp, /* filesystem mount point */
510 xfs_dinode_t *dip) /* ondisk inode */
520 * Add a block to the directory.
521 * This routine is for data and free blocks, not leaf/node blocks
522 * which are handled by xfs_da_grow_inode.
526 xfs_da_args_t *args, /* operation arguments */
527 int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
528 xfs_dir2_db_t *dbp) /* out: block number added */
530 xfs_fileoff_t bno; /* directory offset of new block */
531 int count; /* count of filesystem blocks */
532 xfs_inode_t *dp; /* incore directory inode */
533 int error; /* error return value */
534 int got; /* blocks actually mapped */
535 int i; /* temp mapping index */
536 xfs_bmbt_irec_t map; /* single structure for bmap */
537 int mapi; /* mapping index */
538 xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
539 xfs_mount_t *mp; /* filesystem mount point */
540 int nmap; /* number of bmap entries */
541 xfs_trans_t *tp; /* transaction pointer */
543 xfs_dir2_trace_args_s("grow_inode", args, space);
548 * Set lowest possible block in the space requested.
550 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
551 count = mp->m_dirblkfsbs;
553 * Find the first hole for our block.
555 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK))) {
559 ASSERT(args->firstblock != NULL);
561 * Try mapping the new block contiguously (one extent).
563 if ((error = xfs_bmapi(tp, dp, bno, count,
564 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
565 args->firstblock, args->total, &map, &nmap,
578 * Didn't work and this is a multiple-fsb directory block.
579 * Try again with contiguous flag turned on.
581 else if (nmap == 0 && count > 1) {
582 xfs_fileoff_t b; /* current file offset */
585 * Space for maximum number of mappings.
587 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
589 * Iterate until we get to the end of our block.
591 for (b = bno, mapi = 0; b < bno + count; ) {
592 int c; /* current fsb count */
595 * Can't map more than MAX_NMAP at once.
597 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
598 c = (int)(bno + count - b);
599 if ((error = xfs_bmapi(tp, dp, b, c,
600 XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
601 args->firstblock, args->total,
602 &mapp[mapi], &nmap, args->flist))) {
603 kmem_free(mapp, sizeof(*mapp) * count);
609 * Add this bunch into our table, go to the next offset.
612 b = mapp[mapi - 1].br_startoff +
613 mapp[mapi - 1].br_blockcount;
624 * See how many fsb's we got.
626 for (i = 0, got = 0; i < mapi; i++)
627 got += mapp[i].br_blockcount;
629 * Didn't get enough fsb's, or the first/last block's are wrong.
631 if (got != count || mapp[0].br_startoff != bno ||
632 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
635 kmem_free(mapp, sizeof(*mapp) * count);
636 return XFS_ERROR(ENOSPC);
639 * Done with the temporary mapping table.
642 kmem_free(mapp, sizeof(*mapp) * count);
643 *dbp = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)bno);
645 * Update file's size if this is the data space and it grew.
647 if (space == XFS_DIR2_DATA_SPACE) {
648 xfs_fsize_t size; /* directory file (data) size */
650 size = XFS_FSB_TO_B(mp, bno + count);
651 if (size > dp->i_d.di_size) {
652 dp->i_d.di_size = size;
653 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
660 * See if the directory is a single-block form directory.
664 xfs_trans_t *tp, /* transaction pointer */
665 xfs_inode_t *dp, /* incore directory inode */
666 int *vp) /* out: 1 is block, 0 is not block */
668 xfs_fileoff_t last; /* last file offset */
669 xfs_mount_t *mp; /* filesystem mount point */
670 int rval; /* return value */
673 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
676 rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
677 ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
683 * See if the directory is a single-leaf form directory.
687 xfs_trans_t *tp, /* transaction pointer */
688 xfs_inode_t *dp, /* incore directory inode */
689 int *vp) /* out: 1 is leaf, 0 is not leaf */
691 xfs_fileoff_t last; /* last file offset */
692 xfs_mount_t *mp; /* filesystem mount point */
693 int rval; /* return value */
696 if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
699 *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
704 * Getdents put routine for 64-bit ABI, direct form.
706 static int /* error */
707 xfs_dir2_put_dirent64_direct(
708 xfs_dir2_put_args_t *pa) /* argument bundle */
710 xfs_dirent_t *idbp; /* dirent pointer */
711 iovec_t *iovp; /* io vector */
712 int namelen; /* entry name length */
713 int reclen; /* entry total length */
714 uio_t *uio; /* I/O control */
716 namelen = pa->namelen;
717 reclen = DIRENTSIZE(namelen);
720 * Won't fit in the remaining space.
722 if (reclen > uio->uio_resid) {
727 idbp = (xfs_dirent_t *)iovp->iov_base;
728 iovp->iov_base = (char *)idbp + reclen;
729 iovp->iov_len -= reclen;
730 uio->uio_resid -= reclen;
731 idbp->d_reclen = reclen;
732 idbp->d_ino = pa->ino;
733 idbp->d_off = pa->cook;
734 idbp->d_name[namelen] = '\0';
736 memcpy(idbp->d_name, pa->name, namelen);
741 * Getdents put routine for 64-bit ABI, uio form.
743 static int /* error */
744 xfs_dir2_put_dirent64_uio(
745 xfs_dir2_put_args_t *pa) /* argument bundle */
747 xfs_dirent_t *idbp; /* dirent pointer */
748 int namelen; /* entry name length */
749 int reclen; /* entry total length */
750 int rval; /* return value */
751 uio_t *uio; /* I/O control */
753 namelen = pa->namelen;
754 reclen = DIRENTSIZE(namelen);
757 * Won't fit in the remaining space.
759 if (reclen > uio->uio_resid) {
764 idbp->d_reclen = reclen;
765 idbp->d_ino = pa->ino;
766 idbp->d_off = pa->cook;
767 idbp->d_name[namelen] = '\0';
768 memcpy(idbp->d_name, pa->name, namelen);
769 rval = uio_read((caddr_t)idbp, reclen, uio);
770 pa->done = (rval == 0);
775 * Remove the given block from the directory.
776 * This routine is used for data and free blocks, leaf/node are done
777 * by xfs_da_shrink_inode.
780 xfs_dir2_shrink_inode(
781 xfs_da_args_t *args, /* operation arguments */
782 xfs_dir2_db_t db, /* directory block number */
783 xfs_dabuf_t *bp) /* block's buffer */
785 xfs_fileoff_t bno; /* directory file offset */
786 xfs_dablk_t da; /* directory file offset */
787 int done; /* bunmap is finished */
788 xfs_inode_t *dp; /* incore directory inode */
789 int error; /* error return value */
790 xfs_mount_t *mp; /* filesystem mount point */
791 xfs_trans_t *tp; /* transaction pointer */
793 xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
797 da = XFS_DIR2_DB_TO_DA(mp, db);
799 * Unmap the fsblock(s).
801 if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
802 XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
805 * ENOSPC actually can happen if we're in a removename with
806 * no space reservation, and the resulting block removal
807 * would cause a bmap btree split or conversion from extents
808 * to btree. This can only happen for un-fragmented
809 * directory blocks, since you need to be punching out
810 * the middle of an extent.
811 * In this case we need to leave the block in the file,
813 * So the block has to be in a consistent empty state
814 * and appropriately logged.
815 * We don't free up the buffer, the caller can tell it
816 * hasn't happened since it got an error back.
822 * Invalidate the buffer from the transaction.
824 xfs_da_binval(tp, bp);
826 * If it's not a data block, we're done.
828 if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
831 * If the block isn't the last one in the directory, we're done.
833 if (dp->i_d.di_size > XFS_DIR2_DB_OFF_TO_BYTE(mp, db + 1, 0))
836 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
838 * This can't really happen unless there's kernel corruption.
842 if (db == mp->m_dirdatablk)
847 * Set the size to the new last block.
849 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
850 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);