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_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
37 #include "xfs_dir2_data.h"
38 #include "xfs_dir2_leaf.h"
39 #include "xfs_dir2_block.h"
40 #include "xfs_dir2_node.h"
41 #include "xfs_error.h"
42 #include "xfs_vnodeops.h"
43 #include "xfs_trace.h"
45 struct xfs_name xfs_name_dotdot
= { (unsigned char *)"..", 2};
48 * ASCII case-insensitive (ie. A-Z) support for directories that was
52 xfs_ascii_ci_hashname(
53 struct xfs_name
*name
)
58 for (i
= 0, hash
= 0; i
< name
->len
; i
++)
59 hash
= tolower(name
->name
[i
]) ^ rol32(hash
, 7);
65 xfs_ascii_ci_compname(
66 struct xfs_da_args
*args
,
67 const unsigned char *name
,
70 enum xfs_dacmp result
;
73 if (args
->namelen
!= len
)
74 return XFS_CMP_DIFFERENT
;
76 result
= XFS_CMP_EXACT
;
77 for (i
= 0; i
< len
; i
++) {
78 if (args
->name
[i
] == name
[i
])
80 if (tolower(args
->name
[i
]) != tolower(name
[i
]))
81 return XFS_CMP_DIFFERENT
;
82 result
= XFS_CMP_CASE
;
88 static struct xfs_nameops xfs_ascii_ci_nameops
= {
89 .hashname
= xfs_ascii_ci_hashname
,
90 .compname
= xfs_ascii_ci_compname
,
97 ASSERT(xfs_sb_version_hasdirv2(&mp
->m_sb
));
98 ASSERT((1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
)) <=
100 mp
->m_dirblksize
= 1 << (mp
->m_sb
.sb_blocklog
+ mp
->m_sb
.sb_dirblklog
);
101 mp
->m_dirblkfsbs
= 1 << mp
->m_sb
.sb_dirblklog
;
102 mp
->m_dirdatablk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_DATA_FIRSTDB(mp
));
103 mp
->m_dirleafblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_LEAF_FIRSTDB(mp
));
104 mp
->m_dirfreeblk
= xfs_dir2_db_to_da(mp
, XFS_DIR2_FREE_FIRSTDB(mp
));
105 mp
->m_attr_node_ents
=
106 (mp
->m_sb
.sb_blocksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
107 (uint
)sizeof(xfs_da_node_entry_t
);
108 mp
->m_dir_node_ents
=
109 (mp
->m_dirblksize
- (uint
)sizeof(xfs_da_node_hdr_t
)) /
110 (uint
)sizeof(xfs_da_node_entry_t
);
111 mp
->m_dir_magicpct
= (mp
->m_dirblksize
* 37) / 100;
112 if (xfs_sb_version_hasasciici(&mp
->m_sb
))
113 mp
->m_dirnameops
= &xfs_ascii_ci_nameops
;
115 mp
->m_dirnameops
= &xfs_default_nameops
;
119 * Return 1 if directory contains only "." and "..".
127 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
128 if (dp
->i_d
.di_size
== 0) /* might happen during shutdown. */
130 if (dp
->i_d
.di_size
> XFS_IFORK_DSIZE(dp
))
132 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
133 return !sfp
->hdr
.count
;
137 * Validate a given inode number.
140 xfs_dir_ino_validate(
144 xfs_agblock_t agblkno
;
150 agno
= XFS_INO_TO_AGNO(mp
, ino
);
151 agblkno
= XFS_INO_TO_AGBNO(mp
, ino
);
152 ioff
= XFS_INO_TO_OFFSET(mp
, ino
);
153 agino
= XFS_OFFBNO_TO_AGINO(mp
, agblkno
, ioff
);
155 agno
< mp
->m_sb
.sb_agcount
&&
156 agblkno
< mp
->m_sb
.sb_agblocks
&&
158 ioff
< (1 << mp
->m_sb
.sb_inopblog
) &&
159 XFS_AGINO_TO_INO(mp
, agno
, agino
) == ino
;
160 if (unlikely(XFS_TEST_ERROR(!ino_ok
, mp
, XFS_ERRTAG_DIR_INO_VALIDATE
,
161 XFS_RANDOM_DIR_INO_VALIDATE
))) {
162 xfs_warn(mp
, "Invalid inode number 0x%Lx",
163 (unsigned long long) ino
);
164 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW
, mp
);
165 return XFS_ERROR(EFSCORRUPTED
);
171 * Initialize a directory with its "." and ".." entries.
182 memset((char *)&args
, 0, sizeof(args
));
185 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
186 if ((error
= xfs_dir_ino_validate(tp
->t_mountp
, pdp
->i_ino
)))
188 return xfs_dir2_sf_create(&args
, pdp
->i_ino
);
192 Enter a name in a directory.
198 struct xfs_name
*name
,
199 xfs_ino_t inum
, /* new entry inode number */
200 xfs_fsblock_t
*first
, /* bmap's firstblock */
201 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
202 xfs_extlen_t total
) /* bmap's total block count */
206 int v
; /* type-checking value */
208 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
209 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
211 XFS_STATS_INC(xs_dir_create
);
213 memset(&args
, 0, sizeof(xfs_da_args_t
));
214 args
.name
= name
->name
;
215 args
.namelen
= name
->len
;
216 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
219 args
.firstblock
= first
;
222 args
.whichfork
= XFS_DATA_FORK
;
224 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
226 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
227 rval
= xfs_dir2_sf_addname(&args
);
228 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
231 rval
= xfs_dir2_block_addname(&args
);
232 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
235 rval
= xfs_dir2_leaf_addname(&args
);
237 rval
= xfs_dir2_node_addname(&args
);
242 * If doing a CI lookup and case-insensitive match, dup actual name into
243 * args.value. Return EEXIST for success (ie. name found) or an error.
246 xfs_dir_cilookup_result(
247 struct xfs_da_args
*args
,
248 const unsigned char *name
,
251 if (args
->cmpresult
== XFS_CMP_DIFFERENT
)
253 if (args
->cmpresult
!= XFS_CMP_CASE
||
254 !(args
->op_flags
& XFS_DA_OP_CILOOKUP
))
257 args
->value
= kmem_alloc(len
, KM_NOFS
| KM_MAYFAIL
);
261 memcpy(args
->value
, name
, len
);
262 args
->valuelen
= len
;
267 * Lookup a name in a directory, give back the inode number.
268 * If ci_name is not NULL, returns the actual name in ci_name if it differs
269 * to name, or ci_name->name is set to NULL for an exact match.
276 struct xfs_name
*name
,
277 xfs_ino_t
*inum
, /* out: inode number */
278 struct xfs_name
*ci_name
) /* out: actual name if CI match */
282 int v
; /* type-checking value */
284 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
285 XFS_STATS_INC(xs_dir_lookup
);
287 memset(&args
, 0, sizeof(xfs_da_args_t
));
288 args
.name
= name
->name
;
289 args
.namelen
= name
->len
;
290 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
292 args
.whichfork
= XFS_DATA_FORK
;
294 args
.op_flags
= XFS_DA_OP_OKNOENT
;
296 args
.op_flags
|= XFS_DA_OP_CILOOKUP
;
298 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
299 rval
= xfs_dir2_sf_lookup(&args
);
300 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
303 rval
= xfs_dir2_block_lookup(&args
);
304 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
307 rval
= xfs_dir2_leaf_lookup(&args
);
309 rval
= xfs_dir2_node_lookup(&args
);
313 *inum
= args
.inumber
;
315 ci_name
->name
= args
.value
;
316 ci_name
->len
= args
.valuelen
;
323 * Remove an entry from a directory.
329 struct xfs_name
*name
,
331 xfs_fsblock_t
*first
, /* bmap's firstblock */
332 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
333 xfs_extlen_t total
) /* bmap's total block count */
337 int v
; /* type-checking value */
339 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
340 XFS_STATS_INC(xs_dir_remove
);
342 memset(&args
, 0, sizeof(xfs_da_args_t
));
343 args
.name
= name
->name
;
344 args
.namelen
= name
->len
;
345 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
348 args
.firstblock
= first
;
351 args
.whichfork
= XFS_DATA_FORK
;
354 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
355 rval
= xfs_dir2_sf_removename(&args
);
356 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
359 rval
= xfs_dir2_block_removename(&args
);
360 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
363 rval
= xfs_dir2_leaf_removename(&args
);
365 rval
= xfs_dir2_node_removename(&args
);
380 int rval
; /* return value */
381 int v
; /* type-checking value */
383 trace_xfs_readdir(dp
);
385 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
386 return XFS_ERROR(EIO
);
388 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
389 XFS_STATS_INC(xs_dir_getdents
);
391 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
392 rval
= xfs_dir2_sf_getdents(dp
, dirent
, offset
, filldir
);
393 else if ((rval
= xfs_dir2_isblock(NULL
, dp
, &v
)))
396 rval
= xfs_dir2_block_getdents(dp
, dirent
, offset
, filldir
);
398 rval
= xfs_dir2_leaf_getdents(dp
, dirent
, bufsize
, offset
,
404 * Replace the inode number of a directory entry.
410 struct xfs_name
*name
, /* name of entry to replace */
411 xfs_ino_t inum
, /* new inode number */
412 xfs_fsblock_t
*first
, /* bmap's firstblock */
413 xfs_bmap_free_t
*flist
, /* bmap's freeblock list */
414 xfs_extlen_t total
) /* bmap's total block count */
418 int v
; /* type-checking value */
420 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
422 if ((rval
= xfs_dir_ino_validate(tp
->t_mountp
, inum
)))
425 memset(&args
, 0, sizeof(xfs_da_args_t
));
426 args
.name
= name
->name
;
427 args
.namelen
= name
->len
;
428 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
431 args
.firstblock
= first
;
434 args
.whichfork
= XFS_DATA_FORK
;
437 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
438 rval
= xfs_dir2_sf_replace(&args
);
439 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
442 rval
= xfs_dir2_block_replace(&args
);
443 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
446 rval
= xfs_dir2_leaf_replace(&args
);
448 rval
= xfs_dir2_node_replace(&args
);
453 * See if this entry can be added to the directory without allocating space.
454 * First checks that the caller couldn't reserve enough space (resblks = 0).
460 struct xfs_name
*name
, /* name of entry to add */
465 int v
; /* type-checking value */
470 ASSERT((dp
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
);
472 memset(&args
, 0, sizeof(xfs_da_args_t
));
473 args
.name
= name
->name
;
474 args
.namelen
= name
->len
;
475 args
.hashval
= dp
->i_mount
->m_dirnameops
->hashname(name
);
477 args
.whichfork
= XFS_DATA_FORK
;
479 args
.op_flags
= XFS_DA_OP_JUSTCHECK
| XFS_DA_OP_ADDNAME
|
482 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
483 rval
= xfs_dir2_sf_addname(&args
);
484 else if ((rval
= xfs_dir2_isblock(tp
, dp
, &v
)))
487 rval
= xfs_dir2_block_addname(&args
);
488 else if ((rval
= xfs_dir2_isleaf(tp
, dp
, &v
)))
491 rval
= xfs_dir2_leaf_addname(&args
);
493 rval
= xfs_dir2_node_addname(&args
);
502 * Add a block to the directory.
503 * This routine is for data and free blocks, not leaf/node blocks
504 * which are handled by xfs_da_grow_inode.
509 int space
, /* v2 dir's space XFS_DIR2_xxx_SPACE */
510 xfs_dir2_db_t
*dbp
) /* out: block number added */
512 xfs_fileoff_t bno
; /* directory offset of new block */
513 int count
; /* count of filesystem blocks */
514 xfs_inode_t
*dp
; /* incore directory inode */
516 int got
; /* blocks actually mapped */
518 xfs_bmbt_irec_t map
; /* single structure for bmap */
519 int mapi
; /* mapping index */
520 xfs_bmbt_irec_t
*mapp
; /* bmap mapping structure(s) */
522 int nmap
; /* number of bmap entries */
526 trace_xfs_dir2_grow_inode(args
, space
);
531 nblks
= dp
->i_d
.di_nblocks
;
533 * Set lowest possible block in the space requested.
535 bno
= XFS_B_TO_FSBT(mp
, space
* XFS_DIR2_SPACE_SIZE
);
536 count
= mp
->m_dirblkfsbs
;
538 * Find the first hole for our block.
540 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, XFS_DATA_FORK
)))
543 ASSERT(args
->firstblock
!= NULL
);
545 * Try mapping the new block contiguously (one extent).
547 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
548 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
549 args
->firstblock
, args
->total
, &map
, &nmap
,
558 * Didn't work and this is a multiple-fsb directory block.
559 * Try again with contiguous flag turned on.
561 else if (nmap
== 0 && count
> 1) {
562 xfs_fileoff_t b
; /* current file offset */
565 * Space for maximum number of mappings.
567 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
569 * Iterate until we get to the end of our block.
571 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
572 int c
; /* current fsb count */
575 * Can't map more than MAX_NMAP at once.
577 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
578 c
= (int)(bno
+ count
- b
);
579 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
580 XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
,
581 args
->firstblock
, args
->total
,
582 &mapp
[mapi
], &nmap
, args
->flist
))) {
589 * Add this bunch into our table, go to the next offset.
592 b
= mapp
[mapi
- 1].br_startoff
+
593 mapp
[mapi
- 1].br_blockcount
;
604 * See how many fsb's we got.
606 for (i
= 0, got
= 0; i
< mapi
; i
++)
607 got
+= mapp
[i
].br_blockcount
;
609 * Didn't get enough fsb's, or the first/last block's are wrong.
611 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
612 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
616 return XFS_ERROR(ENOSPC
);
619 * Done with the temporary mapping table.
624 /* account for newly allocated blocks in reserved blocks total */
625 args
->total
-= dp
->i_d
.di_nblocks
- nblks
;
626 *dbp
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)bno
);
629 * Update file's size if this is the data space and it grew.
631 if (space
== XFS_DIR2_DATA_SPACE
) {
632 xfs_fsize_t size
; /* directory file (data) size */
634 size
= XFS_FSB_TO_B(mp
, bno
+ count
);
635 if (size
> dp
->i_d
.di_size
) {
636 dp
->i_d
.di_size
= size
;
637 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
644 * See if the directory is a single-block form directory.
650 int *vp
) /* out: 1 is block, 0 is not block */
652 xfs_fileoff_t last
; /* last file offset */
657 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
659 rval
= XFS_FSB_TO_B(mp
, last
) == mp
->m_dirblksize
;
660 ASSERT(rval
== 0 || dp
->i_d
.di_size
== mp
->m_dirblksize
);
666 * See if the directory is a single-leaf form directory.
672 int *vp
) /* out: 1 is leaf, 0 is not leaf */
674 xfs_fileoff_t last
; /* last file offset */
679 if ((rval
= xfs_bmap_last_offset(tp
, dp
, &last
, XFS_DATA_FORK
)))
681 *vp
= last
== mp
->m_dirleafblk
+ (1 << mp
->m_sb
.sb_dirblklog
);
686 * Remove the given block from the directory.
687 * This routine is used for data and free blocks, leaf/node are done
688 * by xfs_da_shrink_inode.
691 xfs_dir2_shrink_inode(
696 xfs_fileoff_t bno
; /* directory file offset */
697 xfs_dablk_t da
; /* directory file offset */
698 int done
; /* bunmap is finished */
704 trace_xfs_dir2_shrink_inode(args
, db
);
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
);