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_error.h"
44 #include "xfs_vnodeops.h"
45 #include "xfs_trace.h"
47 struct xfs_name xfs_name_dotdot
= {"..", 2};
50 * ASCII case-insensitive (ie. A-Z) support for directories that was
54 xfs_ascii_ci_hashname(
55 struct xfs_name
*name
)
60 for (i
= 0, hash
= 0; i
< name
->len
; i
++)
61 hash
= tolower(name
->name
[i
]) ^ rol32(hash
, 7);
67 xfs_ascii_ci_compname(
68 struct xfs_da_args
*args
,
72 enum xfs_dacmp result
;
75 if (args
->namelen
!= len
)
76 return XFS_CMP_DIFFERENT
;
78 result
= XFS_CMP_EXACT
;
79 for (i
= 0; i
< len
; i
++) {
80 if (args
->name
[i
] == name
[i
])
82 if (tolower(args
->name
[i
]) != tolower(name
[i
]))
83 return XFS_CMP_DIFFERENT
;
84 result
= XFS_CMP_CASE
;
90 static struct xfs_nameops xfs_ascii_ci_nameops
= {
91 .hashname
= xfs_ascii_ci_hashname
,
92 .compname
= xfs_ascii_ci_compname
,
99 ASSERT(xfs_sb_version_hasdirv2(&mp
->m_sb
));
100 ASSERT((1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
)) <=
102 mp
->m_dirblksize
= 1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
);
103 mp
->m_dirblkfsbs
= 1 << mp
->m_sb
.sb_dirblklog
;
104 mp
->m_dirdatablk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_DATA_FIRSTDB(mp
));
105 mp
->m_dirleafblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_LEAF_FIRSTDB(mp
));
106 mp
->m_dirfreeblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_FREE_FIRSTDB(mp
));
107 mp
->m_attr_node_ents
=
108 (mp
->m_sb
.sb_blocksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
109 (uint
)sizeof(xfs_da_node_entry_t
);
110 mp
->m_dir_node_ents
=
111 (mp
->m_dirblksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
112 (uint
)sizeof(xfs_da_node_entry_t
);
113 mp
->m_dir_magicpct
= (mp
->m_dirblksize
* 37) / 100;
114 if (xfs_sb_version_hasasciici(&mp
->m_sb
))
115 mp
->m_dirnameops
= &xfs_ascii_ci_nameops
;
117 mp
->m_dirnameops
= &xfs_default_nameops
;
121 * Return 1 if directory contains only "." and "..".
129 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
130 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
132 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
134 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
135 return !sfp
->hdr
.count
;
139 * Validate a given inode number.
142 xfs_dir_ino_validate(
146 xfs_agblock_t agblkno
;
152 agno
= XFS_INO_TO_AGNO(mp
, ino
);
153 agblkno
= XFS_INO_TO_AGBNO(mp
, ino
);
154 ioff
= XFS_INO_TO_OFFSET(mp
, ino
);
155 agino
= XFS_OFFBNO_TO_AGINO(mp
, agblkno
, ioff
);
157 agno
< mp
->m_sb
.sb_agcount
&&
158 agblkno
< mp
->m_sb
.sb_agblocks
&&
160 ioff
< (1 << mp
->m_sb
.sb_inopblog
) &&
161 XFS_AGINO_TO_INO(mp
, agno
, agino
) == ino
;
162 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
,
163 XFS_RANDOM_DIR_INO_VALIDATE
))) {
164 xfs_fs_cmn_err(CE_WARN
, mp
, "Invalid inode number 0x%Lx",
165 (unsigned long long) ino
);
166 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
167 return XFS_ERROR(EFSCORRUPTED
);
173 * Initialize a directory with its "." and ".." entries.
184 memset((char *)&args
, 0, sizeof(args
));
187 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
188 if ((error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
)))
190 return xfs_dir2_sf_create(&args
, pdp
->i_ino
);
194 Enter a name in a directory.
200 struct xfs_name
*name
,
201 xfs_ino_t inum
, /* new entry inode number */
202 xfs_fsblock_t
*first
, /* bmap's firstblock */
203 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
204 xfs_extlen_t total
) /* bmap's total block count */
208 int v
; /* type-checking value */
210 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
211 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
213 XFS_STATS_INC(xs_dir_create
);
215 memset(&args
, 0, sizeof(xfs_da_args_t
));
216 args
.name
= name
->name
;
217 args
.namelen
= name
->len
;
218 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
221 args
.firstblock
= first
;
224 args
.whichfork
= XFS_DATA_FORK
;
226 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
228 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
229 rval
= xfs_dir2_sf_addname(&args
);
230 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
233 rval
= xfs_dir2_block_addname(&args
);
234 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
237 rval
= xfs_dir2_leaf_addname(&args
);
239 rval
= xfs_dir2_node_addname(&args
);
244 * If doing a CI lookup and case-insensitive match, dup actual name into
245 * args.value. Return EEXIST for success (ie. name found) or an error.
248 xfs_dir_cilookup_result(
249 struct xfs_da_args
*args
,
253 if (args
->cmpresult
== XFS_CMP_DIFFERENT
)
255 if (args
->cmpresult
!= XFS_CMP_CASE
||
256 !(args
->op_flags
& XFS_DA_OP_CILOOKUP
))
259 args
->value
= kmem_alloc(len
, KM_NOFS
| KM_MAYFAIL
);
263 memcpy(args
->value
, name
, len
);
264 args
->valuelen
= len
;
269 * Lookup a name in a directory, give back the inode number.
270 * If ci_name is not NULL, returns the actual name in ci_name if it differs
271 * to name, or ci_name->name is set to NULL for an exact match.
278 struct xfs_name
*name
,
279 xfs_ino_t
*inum
, /* out: inode number */
280 struct xfs_name
*ci_name
) /* out: actual name if CI match */
284 int v
; /* type-checking value */
286 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
287 XFS_STATS_INC(xs_dir_lookup
);
289 memset(&args
, 0, sizeof(xfs_da_args_t
));
290 args
.name
= name
->name
;
291 args
.namelen
= name
->len
;
292 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
294 args
.whichfork
= XFS_DATA_FORK
;
296 args
.op_flags
= XFS_DA_OP_OKNOENT
;
298 args
.op_flags
|= XFS_DA_OP_CILOOKUP
;
300 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
301 rval
= xfs_dir2_sf_lookup(&args
);
302 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
305 rval
= xfs_dir2_block_lookup(&args
);
306 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
309 rval
= xfs_dir2_leaf_lookup(&args
);
311 rval
= xfs_dir2_node_lookup(&args
);
315 *inum
= args
.inumber
;
317 ci_name
->name
= args
.value
;
318 ci_name
->len
= args
.valuelen
;
325 * Remove an entry from a directory.
331 struct xfs_name
*name
,
333 xfs_fsblock_t
*first
, /* bmap's firstblock */
334 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
335 xfs_extlen_t total
) /* bmap's total block count */
339 int v
; /* type-checking value */
341 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
342 XFS_STATS_INC(xs_dir_remove
);
344 memset(&args
, 0, sizeof(xfs_da_args_t
));
345 args
.name
= name
->name
;
346 args
.namelen
= name
->len
;
347 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
350 args
.firstblock
= first
;
353 args
.whichfork
= XFS_DATA_FORK
;
356 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
357 rval
= xfs_dir2_sf_removename(&args
);
358 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
361 rval
= xfs_dir2_block_removename(&args
);
362 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
365 rval
= xfs_dir2_leaf_removename(&args
);
367 rval
= xfs_dir2_node_removename(&args
);
382 int rval
; /* return value */
383 int v
; /* type-checking value */
385 xfs_itrace_entry(dp
);
387 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
388 return XFS_ERROR(EIO
);
390 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
391 XFS_STATS_INC(xs_dir_getdents
);
393 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
394 rval
= xfs_dir2_sf_getdents(dp
, dirent
, offset
, filldir
);
395 else if ((rval
= xfs_dir2_isblock(NULL
, dp
, &v
)))
398 rval
= xfs_dir2_block_getdents(dp
, dirent
, offset
, filldir
);
400 rval
= xfs_dir2_leaf_getdents(dp
, dirent
, bufsize
, offset
,
406 * Replace the inode number of a directory entry.
412 struct xfs_name
*name
, /* name of entry to replace */
413 xfs_ino_t inum
, /* new inode number */
414 xfs_fsblock_t
*first
, /* bmap's firstblock */
415 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
416 xfs_extlen_t total
) /* bmap's total block count */
420 int v
; /* type-checking value */
422 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
424 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
427 memset(&args
, 0, sizeof(xfs_da_args_t
));
428 args
.name
= name
->name
;
429 args
.namelen
= name
->len
;
430 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
433 args
.firstblock
= first
;
436 args
.whichfork
= XFS_DATA_FORK
;
439 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
440 rval
= xfs_dir2_sf_replace(&args
);
441 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
444 rval
= xfs_dir2_block_replace(&args
);
445 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
448 rval
= xfs_dir2_leaf_replace(&args
);
450 rval
= xfs_dir2_node_replace(&args
);
455 * See if this entry can be added to the directory without allocating space.
456 * First checks that the caller couldn't reserve enough space (resblks = 0).
462 struct xfs_name
*name
, /* name of entry to add */
467 int v
; /* type-checking value */
472 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
474 memset(&args
, 0, sizeof(xfs_da_args_t
));
475 args
.name
= name
->name
;
476 args
.namelen
= name
->len
;
477 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
479 args
.whichfork
= XFS_DATA_FORK
;
481 args
.op_flags
= XFS_DA_OP_JUSTCHECK
| XFS_DA_OP_ADDNAME
|
484 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
485 rval
= xfs_dir2_sf_addname(&args
);
486 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
489 rval
= xfs_dir2_block_addname(&args
);
490 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
493 rval
= xfs_dir2_leaf_addname(&args
);
495 rval
= xfs_dir2_node_addname(&args
);
504 * Add a block to the directory.
505 * This routine is for data and free blocks, not leaf/node blocks
506 * which are handled by xfs_da_grow_inode.
511 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
512 xfs_dir2_db_t
*dbp
) /* out: block number added */
514 xfs_fileoff_t bno
; /* directory offset of new block */
515 int count
; /* count of filesystem blocks */
516 xfs_inode_t
*dp
; /* incore directory inode */
518 int got
; /* blocks actually mapped */
520 xfs_bmbt_irec_t map
; /* single structure for bmap */
521 int mapi
; /* mapping index */
522 xfs_bmbt_irec_t
*mapp
; /* bmap mapping structure(s) */
524 int nmap
; /* number of bmap entries */
528 trace_xfs_dir2_grow_inode(args
, space
);
533 nblks
= dp
->i_d
.di_nblocks
;
535 * Set lowest possible block in the space requested.
537 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
538 count
= mp
->m_dirblkfsbs
;
540 * Find the first hole for our block.
542 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, XFS_DATA_FORK
)))
545 ASSERT(args
->firstblock
!= NULL
);
547 * Try mapping the new block contiguously (one extent).
549 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
550 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
551 args
->firstblock
, args
->total
, &map
, &nmap
,
560 * Didn't work and this is a multiple-fsb directory block.
561 * Try again with contiguous flag turned on.
563 else if (nmap
== 0 && count
> 1) {
564 xfs_fileoff_t b
; /* current file offset */
567 * Space for maximum number of mappings.
569 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
571 * Iterate until we get to the end of our block.
573 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
574 int c
; /* current fsb count */
577 * Can't map more than MAX_NMAP at once.
579 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
580 c
= (int)(bno
+ count
- b
);
581 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
582 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
,
583 args
->firstblock
, args
->total
,
584 &mapp
[mapi
], &nmap
, args
->flist
,
592 * Add this bunch into our table, go to the next offset.
595 b
= mapp
[mapi
- 1].br_startoff
+
596 mapp
[mapi
- 1].br_blockcount
;
607 * See how many fsb's we got.
609 for (i
= 0, got
= 0; i
< mapi
; i
++)
610 got
+= mapp
[i
].br_blockcount
;
612 * Didn't get enough fsb's, or the first/last block's are wrong.
614 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
615 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
619 return XFS_ERROR(ENOSPC
);
622 * Done with the temporary mapping table.
627 /* account for newly allocated blocks in reserved blocks total */
628 args
->total
-= dp
->i_d
.di_nblocks
- nblks
;
629 *dbp
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)bno
);
632 * Update file's size if this is the data space and it grew.
634 if (space
== XFS_DIR2_DATA_SPACE
) {
635 xfs_fsize_t size
; /* directory file (data) size */
637 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
638 if (size
> dp
->i_d
.di_size
) {
639 dp
->i_d
.di_size
= size
;
640 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
647 * See if the directory is a single-block form directory.
653 int *vp
) /* out: 1 is block, 0 is not block */
655 xfs_fileoff_t last
; /* last file offset */
660 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
662 rval
= XFS_FSB_TO_B(mp
, last
) == mp
->m_dirblksize
;
663 ASSERT(rval
== 0 || dp
->i_d
.di_size
== mp
->m_dirblksize
);
669 * See if the directory is a single-leaf form directory.
675 int *vp
) /* out: 1 is leaf, 0 is not leaf */
677 xfs_fileoff_t last
; /* last file offset */
682 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
684 *vp
= last
== mp
->m_dirleafblk
+ (1 << mp
->m_sb
.sb_dirblklog
);
689 * Remove the given block from the directory.
690 * This routine is used for data and free blocks, leaf/node are done
691 * by xfs_da_shrink_inode.
694 xfs_dir2_shrink_inode(
699 xfs_fileoff_t bno
; /* directory file offset */
700 xfs_dablk_t da
; /* directory file offset */
701 int done
; /* bunmap is finished */
707 trace_xfs_dir2_shrink_inode(args
, db
);
712 da
= xfs_dir2_db_to_da(mp
, db
);
714 * Unmap the fsblock(s).
716 if ((error
= xfs_bunmapi(tp
, dp
, da
, mp
->m_dirblkfsbs
,
717 XFS_BMAPI_METADATA
, 0, args
->firstblock
, args
->flist
,
720 * ENOSPC actually can happen if we're in a removename with
721 * no space reservation, and the resulting block removal
722 * would cause a bmap btree split or conversion from extents
723 * to btree. This can only happen for un-fragmented
724 * directory blocks, since you need to be punching out
725 * the middle of an extent.
726 * In this case we need to leave the block in the file,
728 * So the block has to be in a consistent empty state
729 * and appropriately logged.
730 * We don't free up the buffer, the caller can tell it
731 * hasn't happened since it got an error back.
737 * Invalidate the buffer from the transaction.
739 xfs_da_binval(tp
, bp
);
741 * If it's not a data block, we're done.
743 if (db
>= XFS_DIR2_LEAF_FIRSTDB(mp
))
746 * If the block isn't the last one in the directory, we're done.
748 if (dp
->i_d
.di_size
> xfs_dir2_db_off_to_byte(mp
, db
+ 1, 0))
751 if ((error
= xfs_bmap_last_before(tp
, dp
, &bno
, XFS_DATA_FORK
))) {
753 * This can't really happen unless there's kernel corruption.
757 if (db
== mp
->m_dirdatablk
)
762 * Set the size to the new last block.
764 dp
->i_d
.di_size
= XFS_FSB_TO_B(mp
, bno
);
765 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);