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"
46 static int xfs_dir2_put_dirent64_direct(xfs_dir2_put_args_t
*pa
);
47 static int xfs_dir2_put_dirent64_uio(xfs_dir2_put_args_t
*pa
);
53 ASSERT(XFS_SB_VERSION_HASDIRV2(&mp
->m_sb
));
54 ASSERT((1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
)) <=
56 mp
->m_dirblksize
= 1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
);
57 mp
->m_dirblkfsbs
= 1 << mp
->m_sb
.sb_dirblklog
;
58 mp
->m_dirdatablk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_DATA_FIRSTDB(mp
));
59 mp
->m_dirleafblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_LEAF_FIRSTDB(mp
));
60 mp
->m_dirfreeblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_FREE_FIRSTDB(mp
));
61 mp
->m_attr_node_ents
=
62 (mp
->m_sb
.sb_blocksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
63 (uint
)sizeof(xfs_da_node_entry_t
);
65 (mp
->m_dirblksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
66 (uint
)sizeof(xfs_da_node_entry_t
);
67 mp
->m_dir_magicpct
= (mp
->m_dirblksize
* 37) / 100;
71 * Return 1 if directory contains only "." and "..".
79 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
80 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
82 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
84 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
85 return !sfp
->hdr
.count
;
89 * Validate a given inode number.
96 xfs_agblock_t agblkno
;
102 agno
= XFS_INO_TO_AGNO(mp
, ino
);
103 agblkno
= XFS_INO_TO_AGBNO(mp
, ino
);
104 ioff
= XFS_INO_TO_OFFSET(mp
, ino
);
105 agino
= XFS_OFFBNO_TO_AGINO(mp
, agblkno
, ioff
);
107 agno
< mp
->m_sb
.sb_agcount
&&
108 agblkno
< mp
->m_sb
.sb_agblocks
&&
110 ioff
< (1 << mp
->m_sb
.sb_inopblog
) &&
111 XFS_AGINO_TO_INO(mp
, agno
, agino
) == ino
;
112 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
,
113 XFS_RANDOM_DIR_INO_VALIDATE
))) {
114 xfs_fs_cmn_err(CE_WARN
, mp
, "Invalid inode number 0x%Lx",
115 (unsigned long long) ino
);
116 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
117 return XFS_ERROR(EFSCORRUPTED
);
123 * Initialize a directory with its "." and ".." entries.
134 memset((char *)&args
, 0, sizeof(args
));
137 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
138 if ((error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
)))
140 return xfs_dir2_sf_create(&args
, pdp
->i_ino
);
144 Enter a name in a directory.
152 xfs_ino_t inum
, /* new entry inode number */
153 xfs_fsblock_t
*first
, /* bmap's firstblock */
154 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
155 xfs_extlen_t total
) /* bmap's total block count */
159 int v
; /* type-checking value */
161 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
162 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
164 XFS_STATS_INC(xs_dir_create
);
167 args
.namelen
= namelen
;
168 args
.hashval
= xfs_da_hashname(name
, namelen
);
171 args
.firstblock
= first
;
174 args
.whichfork
= XFS_DATA_FORK
;
177 args
.addname
= args
.oknoent
= 1;
179 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
180 rval
= xfs_dir2_sf_addname(&args
);
181 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
184 rval
= xfs_dir2_block_addname(&args
);
185 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
188 rval
= xfs_dir2_leaf_addname(&args
);
190 rval
= xfs_dir2_node_addname(&args
);
195 * Lookup a name in a directory, give back the inode number.
203 xfs_ino_t
*inum
) /* out: inode number */
207 int v
; /* type-checking value */
209 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
210 XFS_STATS_INC(xs_dir_lookup
);
213 args
.namelen
= namelen
;
214 args
.hashval
= xfs_da_hashname(name
, namelen
);
217 args
.firstblock
= NULL
;
220 args
.whichfork
= XFS_DATA_FORK
;
222 args
.justcheck
= args
.addname
= 0;
225 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
226 rval
= xfs_dir2_sf_lookup(&args
);
227 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
230 rval
= xfs_dir2_block_lookup(&args
);
231 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
234 rval
= xfs_dir2_leaf_lookup(&args
);
236 rval
= xfs_dir2_node_lookup(&args
);
240 *inum
= args
.inumber
;
245 * Remove an entry from a directory.
254 xfs_fsblock_t
*first
, /* bmap's firstblock */
255 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
256 xfs_extlen_t total
) /* bmap's total block count */
260 int v
; /* type-checking value */
262 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
263 XFS_STATS_INC(xs_dir_remove
);
266 args
.namelen
= namelen
;
267 args
.hashval
= xfs_da_hashname(name
, namelen
);
270 args
.firstblock
= first
;
273 args
.whichfork
= XFS_DATA_FORK
;
275 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
277 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
278 rval
= xfs_dir2_sf_removename(&args
);
279 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
282 rval
= xfs_dir2_block_removename(&args
);
283 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
286 rval
= xfs_dir2_leaf_removename(&args
);
288 rval
= xfs_dir2_node_removename(&args
);
299 uio_t
*uio
, /* caller's buffer control */
300 int *eofp
) /* out: eof reached */
302 int alignment
; /* alignment required for ABI */
303 xfs_dirent_t
*dbp
; /* malloc'ed buffer */
304 xfs_dir2_put_t put
; /* entry formatting routine */
305 int rval
; /* return value */
306 int v
; /* type-checking value */
308 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
309 XFS_STATS_INC(xs_dir_getdents
);
311 * If our caller has given us a single contiguous aligned memory buffer,
312 * just work directly within that buffer. If it's in user memory,
313 * lock it down first.
315 alignment
= sizeof(xfs_off_t
) - 1;
316 if ((uio
->uio_iovcnt
== 1) &&
317 (((__psint_t
)uio
->uio_iov
[0].iov_base
& alignment
) == 0) &&
318 ((uio
->uio_iov
[0].iov_len
& alignment
) == 0)) {
320 put
= xfs_dir2_put_dirent64_direct
;
322 dbp
= kmem_alloc(sizeof(*dbp
) + MAXNAMELEN
, KM_SLEEP
);
323 put
= xfs_dir2_put_dirent64_uio
;
327 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
328 rval
= xfs_dir2_sf_getdents(dp
, uio
, eofp
, dbp
, put
);
329 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
332 rval
= xfs_dir2_block_getdents(tp
, dp
, uio
, eofp
, dbp
, put
);
334 rval
= xfs_dir2_leaf_getdents(tp
, dp
, uio
, eofp
, dbp
, put
);
336 kmem_free(dbp
, sizeof(*dbp
) + MAXNAMELEN
);
341 * Replace the inode number of a directory entry.
347 char *name
, /* name of entry to replace */
349 xfs_ino_t inum
, /* new inode number */
350 xfs_fsblock_t
*first
, /* bmap's firstblock */
351 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
352 xfs_extlen_t total
) /* bmap's total block count */
356 int v
; /* type-checking value */
358 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
360 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
364 args
.namelen
= namelen
;
365 args
.hashval
= xfs_da_hashname(name
, namelen
);
368 args
.firstblock
= first
;
371 args
.whichfork
= XFS_DATA_FORK
;
373 args
.justcheck
= args
.addname
= args
.oknoent
= 0;
375 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
376 rval
= xfs_dir2_sf_replace(&args
);
377 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
380 rval
= xfs_dir2_block_replace(&args
);
381 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
384 rval
= xfs_dir2_leaf_replace(&args
);
386 rval
= xfs_dir2_node_replace(&args
);
391 * See if this entry can be added to the directory without allocating space.
397 char *name
, /* name of entry to add */
402 int v
; /* type-checking value */
404 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
407 args
.namelen
= namelen
;
408 args
.hashval
= xfs_da_hashname(name
, namelen
);
411 args
.firstblock
= NULL
;
414 args
.whichfork
= XFS_DATA_FORK
;
416 args
.justcheck
= args
.addname
= args
.oknoent
= 1;
418 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
419 rval
= xfs_dir2_sf_addname(&args
);
420 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
423 rval
= xfs_dir2_block_addname(&args
);
424 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
427 rval
= xfs_dir2_leaf_addname(&args
);
429 rval
= xfs_dir2_node_addname(&args
);
438 * Add a block to the directory.
439 * This routine is for data and free blocks, not leaf/node blocks
440 * which are handled by xfs_da_grow_inode.
445 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
446 xfs_dir2_db_t
*dbp
) /* out: block number added */
448 xfs_fileoff_t bno
; /* directory offset of new block */
449 int count
; /* count of filesystem blocks */
450 xfs_inode_t
*dp
; /* incore directory inode */
452 int got
; /* blocks actually mapped */
454 xfs_bmbt_irec_t map
; /* single structure for bmap */
455 int mapi
; /* mapping index */
456 xfs_bmbt_irec_t
*mapp
; /* bmap mapping structure(s) */
458 int nmap
; /* number of bmap entries */
461 xfs_dir2_trace_args_s("grow_inode", args
, space
);
466 * Set lowest possible block in the space requested.
468 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
469 count
= mp
->m_dirblkfsbs
;
471 * Find the first hole for our block.
473 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, XFS_DATA_FORK
)))
476 ASSERT(args
->firstblock
!= NULL
);
478 * Try mapping the new block contiguously (one extent).
480 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
481 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
482 args
->firstblock
, args
->total
, &map
, &nmap
,
491 * Didn't work and this is a multiple-fsb directory block.
492 * Try again with contiguous flag turned on.
494 else if (nmap
== 0 && count
> 1) {
495 xfs_fileoff_t b
; /* current file offset */
498 * Space for maximum number of mappings.
500 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
502 * Iterate until we get to the end of our block.
504 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
505 int c
; /* current fsb count */
508 * Can't map more than MAX_NMAP at once.
510 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
511 c
= (int)(bno
+ count
- b
);
512 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
513 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
,
514 args
->firstblock
, args
->total
,
515 &mapp
[mapi
], &nmap
, args
->flist
,
517 kmem_free(mapp
, sizeof(*mapp
) * count
);
523 * Add this bunch into our table, go to the next offset.
526 b
= mapp
[mapi
- 1].br_startoff
+
527 mapp
[mapi
- 1].br_blockcount
;
538 * See how many fsb's we got.
540 for (i
= 0, got
= 0; i
< mapi
; i
++)
541 got
+= mapp
[i
].br_blockcount
;
543 * Didn't get enough fsb's, or the first/last block's are wrong.
545 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
546 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
549 kmem_free(mapp
, sizeof(*mapp
) * count
);
550 return XFS_ERROR(ENOSPC
);
553 * Done with the temporary mapping table.
556 kmem_free(mapp
, sizeof(*mapp
) * count
);
557 *dbp
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)bno
);
559 * Update file's size if this is the data space and it grew.
561 if (space
== XFS_DIR2_DATA_SPACE
) {
562 xfs_fsize_t size
; /* directory file (data) size */
564 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
565 if (size
> dp
->i_d
.di_size
) {
566 dp
->i_d
.di_size
= size
;
567 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
574 * See if the directory is a single-block form directory.
580 int *vp
) /* out: 1 is block, 0 is not block */
582 xfs_fileoff_t last
; /* last file offset */
587 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
589 rval
= XFS_FSB_TO_B(mp
, last
) == mp
->m_dirblksize
;
590 ASSERT(rval
== 0 || dp
->i_d
.di_size
== mp
->m_dirblksize
);
596 * See if the directory is a single-leaf form directory.
602 int *vp
) /* out: 1 is leaf, 0 is not leaf */
604 xfs_fileoff_t last
; /* last file offset */
609 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
611 *vp
= last
== mp
->m_dirleafblk
+ (1 << mp
->m_sb
.sb_dirblklog
);
616 * Getdents put routine for 64-bit ABI, direct form.
619 xfs_dir2_put_dirent64_direct(
620 xfs_dir2_put_args_t
*pa
)
622 xfs_dirent_t
*idbp
; /* dirent pointer */
623 iovec_t
*iovp
; /* io vector */
624 int namelen
; /* entry name length */
625 int reclen
; /* entry total length */
626 uio_t
*uio
; /* I/O control */
628 namelen
= pa
->namelen
;
629 reclen
= DIRENTSIZE(namelen
);
632 * Won't fit in the remaining space.
634 if (reclen
> uio
->uio_resid
) {
639 idbp
= (xfs_dirent_t
*)iovp
->iov_base
;
640 iovp
->iov_base
= (char *)idbp
+ reclen
;
641 iovp
->iov_len
-= reclen
;
642 uio
->uio_resid
-= reclen
;
643 idbp
->d_reclen
= reclen
;
644 idbp
->d_ino
= pa
->ino
;
645 idbp
->d_off
= pa
->cook
;
646 idbp
->d_name
[namelen
] = '\0';
648 memcpy(idbp
->d_name
, pa
->name
, namelen
);
653 * Getdents put routine for 64-bit ABI, uio form.
656 xfs_dir2_put_dirent64_uio(
657 xfs_dir2_put_args_t
*pa
)
659 xfs_dirent_t
*idbp
; /* dirent pointer */
660 int namelen
; /* entry name length */
661 int reclen
; /* entry total length */
662 int rval
; /* return value */
663 uio_t
*uio
; /* I/O control */
665 namelen
= pa
->namelen
;
666 reclen
= DIRENTSIZE(namelen
);
669 * Won't fit in the remaining space.
671 if (reclen
> uio
->uio_resid
) {
676 idbp
->d_reclen
= reclen
;
677 idbp
->d_ino
= pa
->ino
;
678 idbp
->d_off
= pa
->cook
;
679 idbp
->d_name
[namelen
] = '\0';
680 memcpy(idbp
->d_name
, pa
->name
, namelen
);
681 rval
= xfs_uio_read((caddr_t
)idbp
, reclen
, uio
);
682 pa
->done
= (rval
== 0);
687 * Remove the given block from the directory.
688 * This routine is used for data and free blocks, leaf/node are done
689 * by xfs_da_shrink_inode.
692 xfs_dir2_shrink_inode(
697 xfs_fileoff_t bno
; /* directory file offset */
698 xfs_dablk_t da
; /* directory file offset */
699 int done
; /* bunmap is finished */
705 xfs_dir2_trace_args_db("shrink_inode", args
, db
, bp
);
709 da
= xfs_dir2_db_to_da(mp
, db
);
711 * Unmap the fsblock(s).
713 if ((error
= xfs_bunmapi(tp
, dp
, da
, mp
->m_dirblkfsbs
,
714 XFS_BMAPI_METADATA
, 0, args
->firstblock
, args
->flist
,
717 * ENOSPC actually can happen if we're in a removename with
718 * no space reservation, and the resulting block removal
719 * would cause a bmap btree split or conversion from extents
720 * to btree. This can only happen for un-fragmented
721 * directory blocks, since you need to be punching out
722 * the middle of an extent.
723 * In this case we need to leave the block in the file,
725 * So the block has to be in a consistent empty state
726 * and appropriately logged.
727 * We don't free up the buffer, the caller can tell it
728 * hasn't happened since it got an error back.
734 * Invalidate the buffer from the transaction.
736 xfs_da_binval(tp
, bp
);
738 * If it's not a data block, we're done.
740 if (db
>= XFS_DIR2_LEAF_FIRSTDB(mp
))
743 * If the block isn't the last one in the directory, we're done.
745 if (dp
->i_d
.di_size
> xfs_dir2_db_off_to_byte(mp
, db
+ 1, 0))
748 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
750 * This can't really happen unless there's kernel corruption.
754 if (db
== mp
->m_dirdatablk
)
759 * Set the size to the new last block.
761 dp
->i_d
.di_size
= XFS_FSB_TO_B(mp
, bno
);
762 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);