2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
39 #include "xfs_dir2_data.h"
40 #include "xfs_dir2_leaf.h"
41 #include "xfs_dir2_block.h"
42 #include "xfs_dir2_node.h"
43 #include "xfs_dir2_trace.h"
44 #include "xfs_error.h"
45 #include "xfs_vnodeops.h"
52 ASSERT(XFS_SB_VERSION_HASDIRV2(&mp
->m_sb
));
53 ASSERT((1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
)) <=
55 mp
->m_dirblksize
= 1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
);
56 mp
->m_dirblkfsbs
= 1 << mp
->m_sb
.sb_dirblklog
;
57 mp
->m_dirdatablk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_DATA_FIRSTDB(mp
));
58 mp
->m_dirleafblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_LEAF_FIRSTDB(mp
));
59 mp
->m_dirfreeblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_FREE_FIRSTDB(mp
));
60 mp
->m_attr_node_ents
=
61 (mp
->m_sb
.sb_blocksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
62 (uint
)sizeof(xfs_da_node_entry_t
);
64 (mp
->m_dirblksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
65 (uint
)sizeof(xfs_da_node_entry_t
);
66 mp
->m_dir_magicpct
= (mp
->m_dirblksize
* 37) / 100;
70 * Return 1 if directory contains only "." and "..".
78 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
79 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
81 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
83 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
84 return !sfp
->hdr
.count
;
88 * Validate a given inode number.
95 xfs_agblock_t agblkno
;
101 agno
= XFS_INO_TO_AGNO(mp
, ino
);
102 agblkno
= XFS_INO_TO_AGBNO(mp
, ino
);
103 ioff
= XFS_INO_TO_OFFSET(mp
, ino
);
104 agino
= XFS_OFFBNO_TO_AGINO(mp
, agblkno
, ioff
);
106 agno
< mp
->m_sb
.sb_agcount
&&
107 agblkno
< mp
->m_sb
.sb_agblocks
&&
109 ioff
< (1 << mp
->m_sb
.sb_inopblog
) &&
110 XFS_AGINO_TO_INO(mp
, agno
, agino
) == ino
;
111 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
,
112 XFS_RANDOM_DIR_INO_VALIDATE
))) {
113 xfs_fs_cmn_err(CE_WARN
, mp
, "Invalid inode number 0x%Lx",
114 (unsigned long long) ino
);
115 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
116 return XFS_ERROR(EFSCORRUPTED
);
122 * Initialize a directory with its "." and ".." entries.
133 memset((char *)&args
, 0, sizeof(args
));
136 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
137 if ((error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
)))
139 return xfs_dir2_sf_create(&args
, pdp
->i_ino
);
143 Enter a name in a directory.
151 xfs_ino_t inum
, /* new entry inode number */
152 xfs_fsblock_t
*first
, /* bmap's firstblock */
153 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
154 xfs_extlen_t total
) /* bmap's total block count */
158 int v
; /* type-checking value */
160 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
161 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
163 XFS_STATS_INC(xs_dir_create
);
166 args
.namelen
= namelen
;
167 args
.hashval
= xfs_da_hashname(name
, namelen
);
170 args
.firstblock
= first
;
173 args
.whichfork
= XFS_DATA_FORK
;
176 args
.addname
= args
.oknoent
= 1;
178 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
179 rval
= xfs_dir2_sf_addname(&args
);
180 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
183 rval
= xfs_dir2_block_addname(&args
);
184 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
187 rval
= xfs_dir2_leaf_addname(&args
);
189 rval
= xfs_dir2_node_addname(&args
);
194 * Lookup a name in a directory, give back the inode number.
202 xfs_ino_t
*inum
) /* out: inode number */
206 int v
; /* type-checking value */
208 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
209 XFS_STATS_INC(xs_dir_lookup
);
212 args
.namelen
= namelen
;
213 args
.hashval
= xfs_da_hashname(name
, namelen
);
216 args
.firstblock
= NULL
;
219 args
.whichfork
= XFS_DATA_FORK
;
221 args
.justcheck
= args
.addname
= 0;
224 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
225 rval
= xfs_dir2_sf_lookup(&args
);
226 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
229 rval
= xfs_dir2_block_lookup(&args
);
230 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
233 rval
= xfs_dir2_leaf_lookup(&args
);
235 rval
= xfs_dir2_node_lookup(&args
);
239 *inum
= args
.inumber
;
244 * Remove an entry from a directory.
253 xfs_fsblock_t
*first
, /* bmap's firstblock */
254 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
255 xfs_extlen_t total
) /* bmap's total block count */
259 int v
; /* type-checking value */
261 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
262 XFS_STATS_INC(xs_dir_remove
);
265 args
.namelen
= namelen
;
266 args
.hashval
= xfs_da_hashname(name
, namelen
);
269 args
.firstblock
= first
;
272 args
.whichfork
= XFS_DATA_FORK
;
274 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
276 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
277 rval
= xfs_dir2_sf_removename(&args
);
278 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
281 rval
= xfs_dir2_block_removename(&args
);
282 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
285 rval
= xfs_dir2_leaf_removename(&args
);
287 rval
= xfs_dir2_node_removename(&args
);
302 int rval
; /* return value */
303 int v
; /* type-checking value */
305 xfs_itrace_entry(dp
);
307 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
308 return XFS_ERROR(EIO
);
310 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
311 XFS_STATS_INC(xs_dir_getdents
);
313 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
314 rval
= xfs_dir2_sf_getdents(dp
, dirent
, offset
, filldir
);
315 else if ((rval
= xfs_dir2_isblock(NULL
, dp
, &v
)))
318 rval
= xfs_dir2_block_getdents(dp
, dirent
, offset
, filldir
);
320 rval
= xfs_dir2_leaf_getdents(dp
, dirent
, bufsize
, offset
,
326 * Replace the inode number of a directory entry.
332 char *name
, /* name of entry to replace */
334 xfs_ino_t inum
, /* new inode number */
335 xfs_fsblock_t
*first
, /* bmap's firstblock */
336 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
337 xfs_extlen_t total
) /* bmap's total block count */
341 int v
; /* type-checking value */
343 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
345 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
349 args
.namelen
= namelen
;
350 args
.hashval
= xfs_da_hashname(name
, namelen
);
353 args
.firstblock
= first
;
356 args
.whichfork
= XFS_DATA_FORK
;
358 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
360 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
361 rval
= xfs_dir2_sf_replace(&args
);
362 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
365 rval
= xfs_dir2_block_replace(&args
);
366 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
369 rval
= xfs_dir2_leaf_replace(&args
);
371 rval
= xfs_dir2_node_replace(&args
);
376 * See if this entry can be added to the directory without allocating space.
382 char *name
, /* name of entry to add */
387 int v
; /* type-checking value */
389 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
392 args
.namelen
= namelen
;
393 args
.hashval
= xfs_da_hashname(name
, namelen
);
396 args
.firstblock
= NULL
;
399 args
.whichfork
= XFS_DATA_FORK
;
401 args
.justcheck
= args
.addname
= args
.oknoent
= 1;
403 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
404 rval
= xfs_dir2_sf_addname(&args
);
405 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
408 rval
= xfs_dir2_block_addname(&args
);
409 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
412 rval
= xfs_dir2_leaf_addname(&args
);
414 rval
= xfs_dir2_node_addname(&args
);
423 * Add a block to the directory.
424 * This routine is for data and free blocks, not leaf/node blocks
425 * which are handled by xfs_da_grow_inode.
430 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
431 xfs_dir2_db_t
*dbp
) /* out: block number added */
433 xfs_fileoff_t bno
; /* directory offset of new block */
434 int count
; /* count of filesystem blocks */
435 xfs_inode_t
*dp
; /* incore directory inode */
437 int got
; /* blocks actually mapped */
439 xfs_bmbt_irec_t map
; /* single structure for bmap */
440 int mapi
; /* mapping index */
441 xfs_bmbt_irec_t
*mapp
; /* bmap mapping structure(s) */
443 int nmap
; /* number of bmap entries */
446 xfs_dir2_trace_args_s("grow_inode", args
, space
);
451 * Set lowest possible block in the space requested.
453 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
454 count
= mp
->m_dirblkfsbs
;
456 * Find the first hole for our block.
458 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, XFS_DATA_FORK
)))
461 ASSERT(args
->firstblock
!= NULL
);
463 * Try mapping the new block contiguously (one extent).
465 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
466 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
467 args
->firstblock
, args
->total
, &map
, &nmap
,
476 * Didn't work and this is a multiple-fsb directory block.
477 * Try again with contiguous flag turned on.
479 else if (nmap
== 0 && count
> 1) {
480 xfs_fileoff_t b
; /* current file offset */
483 * Space for maximum number of mappings.
485 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
487 * Iterate until we get to the end of our block.
489 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
490 int c
; /* current fsb count */
493 * Can't map more than MAX_NMAP at once.
495 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
496 c
= (int)(bno
+ count
- b
);
497 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
498 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
,
499 args
->firstblock
, args
->total
,
500 &mapp
[mapi
], &nmap
, args
->flist
,
502 kmem_free(mapp
, sizeof(*mapp
) * count
);
508 * Add this bunch into our table, go to the next offset.
511 b
= mapp
[mapi
- 1].br_startoff
+
512 mapp
[mapi
- 1].br_blockcount
;
523 * See how many fsb's we got.
525 for (i
= 0, got
= 0; i
< mapi
; i
++)
526 got
+= mapp
[i
].br_blockcount
;
528 * Didn't get enough fsb's, or the first/last block's are wrong.
530 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
531 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
534 kmem_free(mapp
, sizeof(*mapp
) * count
);
535 return XFS_ERROR(ENOSPC
);
538 * Done with the temporary mapping table.
541 kmem_free(mapp
, sizeof(*mapp
) * count
);
542 *dbp
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)bno
);
544 * Update file's size if this is the data space and it grew.
546 if (space
== XFS_DIR2_DATA_SPACE
) {
547 xfs_fsize_t size
; /* directory file (data) size */
549 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
550 if (size
> dp
->i_d
.di_size
) {
551 dp
->i_d
.di_size
= size
;
552 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
559 * See if the directory is a single-block form directory.
565 int *vp
) /* out: 1 is block, 0 is not block */
567 xfs_fileoff_t last
; /* last file offset */
572 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
574 rval
= XFS_FSB_TO_B(mp
, last
) == mp
->m_dirblksize
;
575 ASSERT(rval
== 0 || dp
->i_d
.di_size
== mp
->m_dirblksize
);
581 * See if the directory is a single-leaf form directory.
587 int *vp
) /* out: 1 is leaf, 0 is not leaf */
589 xfs_fileoff_t last
; /* last file offset */
594 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
596 *vp
= last
== mp
->m_dirleafblk
+ (1 << mp
->m_sb
.sb_dirblklog
);
601 * Remove the given block from the directory.
602 * This routine is used for data and free blocks, leaf/node are done
603 * by xfs_da_shrink_inode.
606 xfs_dir2_shrink_inode(
611 xfs_fileoff_t bno
; /* directory file offset */
612 xfs_dablk_t da
; /* directory file offset */
613 int done
; /* bunmap is finished */
619 xfs_dir2_trace_args_db("shrink_inode", args
, db
, bp
);
623 da
= xfs_dir2_db_to_da(mp
, db
);
625 * Unmap the fsblock(s).
627 if ((error
= xfs_bunmapi(tp
, dp
, da
, mp
->m_dirblkfsbs
,
628 XFS_BMAPI_METADATA
, 0, args
->firstblock
, args
->flist
,
631 * ENOSPC actually can happen if we're in a removename with
632 * no space reservation, and the resulting block removal
633 * would cause a bmap btree split or conversion from extents
634 * to btree. This can only happen for un-fragmented
635 * directory blocks, since you need to be punching out
636 * the middle of an extent.
637 * In this case we need to leave the block in the file,
639 * So the block has to be in a consistent empty state
640 * and appropriately logged.
641 * We don't free up the buffer, the caller can tell it
642 * hasn't happened since it got an error back.
648 * Invalidate the buffer from the transaction.
650 xfs_da_binval(tp
, bp
);
652 * If it's not a data block, we're done.
654 if (db
>= XFS_DIR2_LEAF_FIRSTDB(mp
))
657 * If the block isn't the last one in the directory, we're done.
659 if (dp
->i_d
.di_size
> xfs_dir2_db_off_to_byte(mp
, db
+ 1, 0))
662 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
664 * This can't really happen unless there's kernel corruption.
668 if (db
== mp
->m_dirdatablk
)
673 * Set the size to the new last block.
675 dp
->i_d
.di_size
= XFS_FSB_TO_B(mp
, bno
);
676 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);