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"
51 ASSERT(XFS_SB_VERSION_HASDIRV2(&mp
->m_sb
));
52 ASSERT((1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
)) <=
54 mp
->m_dirblksize
= 1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
);
55 mp
->m_dirblkfsbs
= 1 << mp
->m_sb
.sb_dirblklog
;
56 mp
->m_dirdatablk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_DATA_FIRSTDB(mp
));
57 mp
->m_dirleafblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_LEAF_FIRSTDB(mp
));
58 mp
->m_dirfreeblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_FREE_FIRSTDB(mp
));
59 mp
->m_attr_node_ents
=
60 (mp
->m_sb
.sb_blocksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
61 (uint
)sizeof(xfs_da_node_entry_t
);
63 (mp
->m_dirblksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
64 (uint
)sizeof(xfs_da_node_entry_t
);
65 mp
->m_dir_magicpct
= (mp
->m_dirblksize
* 37) / 100;
69 * Return 1 if directory contains only "." and "..".
77 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
78 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
80 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
82 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
83 return !sfp
->hdr
.count
;
87 * Validate a given inode number.
94 xfs_agblock_t agblkno
;
100 agno
= XFS_INO_TO_AGNO(mp
, ino
);
101 agblkno
= XFS_INO_TO_AGBNO(mp
, ino
);
102 ioff
= XFS_INO_TO_OFFSET(mp
, ino
);
103 agino
= XFS_OFFBNO_TO_AGINO(mp
, agblkno
, ioff
);
105 agno
< mp
->m_sb
.sb_agcount
&&
106 agblkno
< mp
->m_sb
.sb_agblocks
&&
108 ioff
< (1 << mp
->m_sb
.sb_inopblog
) &&
109 XFS_AGINO_TO_INO(mp
, agno
, agino
) == ino
;
110 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
,
111 XFS_RANDOM_DIR_INO_VALIDATE
))) {
112 xfs_fs_cmn_err(CE_WARN
, mp
, "Invalid inode number 0x%Lx",
113 (unsigned long long) ino
);
114 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
115 return XFS_ERROR(EFSCORRUPTED
);
121 * Initialize a directory with its "." and ".." entries.
132 memset((char *)&args
, 0, sizeof(args
));
135 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
136 if ((error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
)))
138 return xfs_dir2_sf_create(&args
, pdp
->i_ino
);
142 Enter a name in a directory.
150 xfs_ino_t inum
, /* new entry inode number */
151 xfs_fsblock_t
*first
, /* bmap's firstblock */
152 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
153 xfs_extlen_t total
) /* bmap's total block count */
157 int v
; /* type-checking value */
159 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
160 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
162 XFS_STATS_INC(xs_dir_create
);
165 args
.namelen
= namelen
;
166 args
.hashval
= xfs_da_hashname(name
, namelen
);
169 args
.firstblock
= first
;
172 args
.whichfork
= XFS_DATA_FORK
;
175 args
.addname
= args
.oknoent
= 1;
177 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
178 rval
= xfs_dir2_sf_addname(&args
);
179 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
182 rval
= xfs_dir2_block_addname(&args
);
183 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
186 rval
= xfs_dir2_leaf_addname(&args
);
188 rval
= xfs_dir2_node_addname(&args
);
193 * Lookup a name in a directory, give back the inode number.
201 xfs_ino_t
*inum
) /* out: inode number */
205 int v
; /* type-checking value */
207 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
208 XFS_STATS_INC(xs_dir_lookup
);
211 args
.namelen
= namelen
;
212 args
.hashval
= xfs_da_hashname(name
, namelen
);
215 args
.firstblock
= NULL
;
218 args
.whichfork
= XFS_DATA_FORK
;
220 args
.justcheck
= args
.addname
= 0;
223 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
224 rval
= xfs_dir2_sf_lookup(&args
);
225 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
228 rval
= xfs_dir2_block_lookup(&args
);
229 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
232 rval
= xfs_dir2_leaf_lookup(&args
);
234 rval
= xfs_dir2_node_lookup(&args
);
238 *inum
= args
.inumber
;
243 * Remove an entry from a directory.
252 xfs_fsblock_t
*first
, /* bmap's firstblock */
253 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
254 xfs_extlen_t total
) /* bmap's total block count */
258 int v
; /* type-checking value */
260 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
261 XFS_STATS_INC(xs_dir_remove
);
264 args
.namelen
= namelen
;
265 args
.hashval
= xfs_da_hashname(name
, namelen
);
268 args
.firstblock
= first
;
271 args
.whichfork
= XFS_DATA_FORK
;
273 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
275 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
276 rval
= xfs_dir2_sf_removename(&args
);
277 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
280 rval
= xfs_dir2_block_removename(&args
);
281 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
284 rval
= xfs_dir2_leaf_removename(&args
);
286 rval
= xfs_dir2_node_removename(&args
);
301 int rval
; /* return value */
302 int v
; /* type-checking value */
304 vn_trace_entry(dp
, __FUNCTION__
, (inst_t
*)__return_address
);
306 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
307 return XFS_ERROR(EIO
);
309 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
310 XFS_STATS_INC(xs_dir_getdents
);
312 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
313 rval
= xfs_dir2_sf_getdents(dp
, dirent
, offset
, filldir
);
314 else if ((rval
= xfs_dir2_isblock(NULL
, dp
, &v
)))
317 rval
= xfs_dir2_block_getdents(dp
, dirent
, offset
, filldir
);
319 rval
= xfs_dir2_leaf_getdents(dp
, dirent
, bufsize
, offset
,
325 * Replace the inode number of a directory entry.
331 char *name
, /* name of entry to replace */
333 xfs_ino_t inum
, /* new inode number */
334 xfs_fsblock_t
*first
, /* bmap's firstblock */
335 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
336 xfs_extlen_t total
) /* bmap's total block count */
340 int v
; /* type-checking value */
342 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
344 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
348 args
.namelen
= namelen
;
349 args
.hashval
= xfs_da_hashname(name
, namelen
);
352 args
.firstblock
= first
;
355 args
.whichfork
= XFS_DATA_FORK
;
357 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
359 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
360 rval
= xfs_dir2_sf_replace(&args
);
361 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
364 rval
= xfs_dir2_block_replace(&args
);
365 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
368 rval
= xfs_dir2_leaf_replace(&args
);
370 rval
= xfs_dir2_node_replace(&args
);
375 * See if this entry can be added to the directory without allocating space.
381 char *name
, /* name of entry to add */
386 int v
; /* type-checking value */
388 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
391 args
.namelen
= namelen
;
392 args
.hashval
= xfs_da_hashname(name
, namelen
);
395 args
.firstblock
= NULL
;
398 args
.whichfork
= XFS_DATA_FORK
;
400 args
.justcheck
= args
.addname
= args
.oknoent
= 1;
402 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
403 rval
= xfs_dir2_sf_addname(&args
);
404 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
407 rval
= xfs_dir2_block_addname(&args
);
408 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
411 rval
= xfs_dir2_leaf_addname(&args
);
413 rval
= xfs_dir2_node_addname(&args
);
422 * Add a block to the directory.
423 * This routine is for data and free blocks, not leaf/node blocks
424 * which are handled by xfs_da_grow_inode.
429 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
430 xfs_dir2_db_t
*dbp
) /* out: block number added */
432 xfs_fileoff_t bno
; /* directory offset of new block */
433 int count
; /* count of filesystem blocks */
434 xfs_inode_t
*dp
; /* incore directory inode */
436 int got
; /* blocks actually mapped */
438 xfs_bmbt_irec_t map
; /* single structure for bmap */
439 int mapi
; /* mapping index */
440 xfs_bmbt_irec_t
*mapp
; /* bmap mapping structure(s) */
442 int nmap
; /* number of bmap entries */
445 xfs_dir2_trace_args_s("grow_inode", args
, space
);
450 * Set lowest possible block in the space requested.
452 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
453 count
= mp
->m_dirblkfsbs
;
455 * Find the first hole for our block.
457 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, XFS_DATA_FORK
)))
460 ASSERT(args
->firstblock
!= NULL
);
462 * Try mapping the new block contiguously (one extent).
464 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
465 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
466 args
->firstblock
, args
->total
, &map
, &nmap
,
475 * Didn't work and this is a multiple-fsb directory block.
476 * Try again with contiguous flag turned on.
478 else if (nmap
== 0 && count
> 1) {
479 xfs_fileoff_t b
; /* current file offset */
482 * Space for maximum number of mappings.
484 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
486 * Iterate until we get to the end of our block.
488 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
489 int c
; /* current fsb count */
492 * Can't map more than MAX_NMAP at once.
494 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
495 c
= (int)(bno
+ count
- b
);
496 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
497 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
,
498 args
->firstblock
, args
->total
,
499 &mapp
[mapi
], &nmap
, args
->flist
,
501 kmem_free(mapp
, sizeof(*mapp
) * count
);
507 * Add this bunch into our table, go to the next offset.
510 b
= mapp
[mapi
- 1].br_startoff
+
511 mapp
[mapi
- 1].br_blockcount
;
522 * See how many fsb's we got.
524 for (i
= 0, got
= 0; i
< mapi
; i
++)
525 got
+= mapp
[i
].br_blockcount
;
527 * Didn't get enough fsb's, or the first/last block's are wrong.
529 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
530 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
533 kmem_free(mapp
, sizeof(*mapp
) * count
);
534 return XFS_ERROR(ENOSPC
);
537 * Done with the temporary mapping table.
540 kmem_free(mapp
, sizeof(*mapp
) * count
);
541 *dbp
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)bno
);
543 * Update file's size if this is the data space and it grew.
545 if (space
== XFS_DIR2_DATA_SPACE
) {
546 xfs_fsize_t size
; /* directory file (data) size */
548 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
549 if (size
> dp
->i_d
.di_size
) {
550 dp
->i_d
.di_size
= size
;
551 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
558 * See if the directory is a single-block form directory.
564 int *vp
) /* out: 1 is block, 0 is not block */
566 xfs_fileoff_t last
; /* last file offset */
571 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
573 rval
= XFS_FSB_TO_B(mp
, last
) == mp
->m_dirblksize
;
574 ASSERT(rval
== 0 || dp
->i_d
.di_size
== mp
->m_dirblksize
);
580 * See if the directory is a single-leaf form directory.
586 int *vp
) /* out: 1 is leaf, 0 is not leaf */
588 xfs_fileoff_t last
; /* last file offset */
593 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
595 *vp
= last
== mp
->m_dirleafblk
+ (1 << mp
->m_sb
.sb_dirblklog
);
600 * Remove the given block from the directory.
601 * This routine is used for data and free blocks, leaf/node are done
602 * by xfs_da_shrink_inode.
605 xfs_dir2_shrink_inode(
610 xfs_fileoff_t bno
; /* directory file offset */
611 xfs_dablk_t da
; /* directory file offset */
612 int done
; /* bunmap is finished */
618 xfs_dir2_trace_args_db("shrink_inode", args
, db
, bp
);
622 da
= xfs_dir2_db_to_da(mp
, db
);
624 * Unmap the fsblock(s).
626 if ((error
= xfs_bunmapi(tp
, dp
, da
, mp
->m_dirblkfsbs
,
627 XFS_BMAPI_METADATA
, 0, args
->firstblock
, args
->flist
,
630 * ENOSPC actually can happen if we're in a removename with
631 * no space reservation, and the resulting block removal
632 * would cause a bmap btree split or conversion from extents
633 * to btree. This can only happen for un-fragmented
634 * directory blocks, since you need to be punching out
635 * the middle of an extent.
636 * In this case we need to leave the block in the file,
638 * So the block has to be in a consistent empty state
639 * and appropriately logged.
640 * We don't free up the buffer, the caller can tell it
641 * hasn't happened since it got an error back.
647 * Invalidate the buffer from the transaction.
649 xfs_da_binval(tp
, bp
);
651 * If it's not a data block, we're done.
653 if (db
>= XFS_DIR2_LEAF_FIRSTDB(mp
))
656 * If the block isn't the last one in the directory, we're done.
658 if (dp
->i_d
.di_size
> xfs_dir2_db_off_to_byte(mp
, db
+ 1, 0))
661 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
663 * This can't really happen unless there's kernel corruption.
667 if (db
== mp
->m_dirdatablk
)
672 * Set the size to the new last block.
674 dp
->i_d
.di_size
= XFS_FSB_TO_B(mp
, bno
);
675 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);