1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Creates, reads, walks and deletes directory-nodes
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43 #include <linux/quotaops.h>
44 #include <linux/sort.h>
46 #define MLOG_MASK_PREFIX ML_NAMEI
47 #include <cluster/masklog.h>
52 #include "blockcheck.h"
55 #include "extent_map.h"
65 #include "buffer_head_io.h"
67 #define NAMEI_RA_CHUNKS 2
68 #define NAMEI_RA_BLOCKS 4
69 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
72 static unsigned char ocfs2_filetype_table
[] = {
73 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
76 static int ocfs2_do_extend_dir(struct super_block
*sb
,
79 struct buffer_head
*parent_fe_bh
,
80 struct ocfs2_alloc_context
*data_ac
,
81 struct ocfs2_alloc_context
*meta_ac
,
82 struct buffer_head
**new_bh
);
83 static int ocfs2_dir_indexed(struct inode
*inode
);
86 * These are distinct checks because future versions of the file system will
87 * want to have a trailing dirent structure independent of indexing.
89 static int ocfs2_supports_dir_trailer(struct inode
*dir
)
91 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
93 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
96 return ocfs2_meta_ecc(osb
) || ocfs2_dir_indexed(dir
);
100 * "new' here refers to the point at which we're creating a new
101 * directory via "mkdir()", but also when we're expanding an inline
102 * directory. In either case, we don't yet have the indexing bit set
103 * on the directory, so the standard checks will fail in when metaecc
104 * is turned off. Only directory-initialization type functions should
105 * use this then. Everything else wants ocfs2_supports_dir_trailer()
107 static int ocfs2_new_dir_wants_trailer(struct inode
*dir
)
109 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
111 return ocfs2_meta_ecc(osb
) ||
112 ocfs2_supports_indexed_dirs(osb
);
115 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block
*sb
)
117 return sb
->s_blocksize
- sizeof(struct ocfs2_dir_block_trailer
);
120 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
122 struct ocfs2_dir_block_trailer
*ocfs2_dir_trailer_from_size(int blocksize
,
127 p
+= blocksize
- sizeof(struct ocfs2_dir_block_trailer
);
128 return (struct ocfs2_dir_block_trailer
*)p
;
131 static int ocfs2_skip_dir_trailer(struct inode
*dir
,
132 struct ocfs2_dir_entry
*de
,
133 unsigned long offset
,
134 unsigned long blklen
)
136 unsigned long toff
= blklen
- sizeof(struct ocfs2_dir_block_trailer
);
138 if (!ocfs2_supports_dir_trailer(dir
))
147 static void ocfs2_init_dir_trailer(struct inode
*inode
,
148 struct buffer_head
*bh
, u16 rec_len
)
150 struct ocfs2_dir_block_trailer
*trailer
;
152 trailer
= ocfs2_trailer_from_bh(bh
, inode
->i_sb
);
153 strcpy(trailer
->db_signature
, OCFS2_DIR_TRAILER_SIGNATURE
);
154 trailer
->db_compat_rec_len
=
155 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer
));
156 trailer
->db_parent_dinode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
157 trailer
->db_blkno
= cpu_to_le64(bh
->b_blocknr
);
158 trailer
->db_free_rec_len
= cpu_to_le16(rec_len
);
161 * Link an unindexed block with a dir trailer structure into the index free
162 * list. This function will modify dirdata_bh, but assumes you've already
163 * passed it to the journal.
165 static int ocfs2_dx_dir_link_trailer(struct inode
*dir
, handle_t
*handle
,
166 struct buffer_head
*dx_root_bh
,
167 struct buffer_head
*dirdata_bh
)
170 struct ocfs2_dx_root_block
*dx_root
;
171 struct ocfs2_dir_block_trailer
*trailer
;
173 ret
= ocfs2_journal_access_dr(handle
, INODE_CACHE(dir
), dx_root_bh
,
174 OCFS2_JOURNAL_ACCESS_WRITE
);
179 trailer
= ocfs2_trailer_from_bh(dirdata_bh
, dir
->i_sb
);
180 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
182 trailer
->db_free_next
= dx_root
->dr_free_blk
;
183 dx_root
->dr_free_blk
= cpu_to_le64(dirdata_bh
->b_blocknr
);
185 ocfs2_journal_dirty(handle
, dx_root_bh
);
191 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result
*res
)
193 return res
->dl_prev_leaf_bh
== NULL
;
196 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result
*res
)
198 brelse(res
->dl_dx_root_bh
);
199 brelse(res
->dl_leaf_bh
);
200 brelse(res
->dl_dx_leaf_bh
);
201 brelse(res
->dl_prev_leaf_bh
);
204 static int ocfs2_dir_indexed(struct inode
*inode
)
206 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INDEXED_DIR_FL
)
211 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block
*dx_root
)
213 return dx_root
->dr_flags
& OCFS2_DX_FLAG_INLINE
;
217 * Hashing code adapted from ext3
219 #define DELTA 0x9E3779B9
221 static void TEA_transform(__u32 buf
[4], __u32
const in
[])
224 __u32 b0
= buf
[0], b1
= buf
[1];
225 __u32 a
= in
[0], b
= in
[1], c
= in
[2], d
= in
[3];
230 b0
+= ((b1
<< 4)+a
) ^ (b1
+sum
) ^ ((b1
>> 5)+b
);
231 b1
+= ((b0
<< 4)+c
) ^ (b0
+sum
) ^ ((b0
>> 5)+d
);
238 static void str2hashbuf(const char *msg
, int len
, __u32
*buf
, int num
)
243 pad
= (__u32
)len
| ((__u32
)len
<< 8);
249 for (i
= 0; i
< len
; i
++) {
252 val
= msg
[i
] + (val
<< 8);
265 static void ocfs2_dx_dir_name_hash(struct inode
*dir
, const char *name
, int len
,
266 struct ocfs2_dx_hinfo
*hinfo
)
268 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
272 if ((len
== 1 && !strncmp(".", name
, 1)) ||
273 (len
== 2 && !strncmp("..", name
, 2))) {
278 #ifdef OCFS2_DEBUG_DX_DIRS
280 * This makes it very easy to debug indexing problems. We
281 * should never allow this to be selected without hand editing
284 buf
[0] = buf
[1] = len
;
288 memcpy(buf
, osb
->osb_dx_seed
, sizeof(buf
));
292 str2hashbuf(p
, len
, in
, 4);
293 TEA_transform(buf
, in
);
299 hinfo
->major_hash
= buf
[0];
300 hinfo
->minor_hash
= buf
[1];
304 * bh passed here can be an inode block or a dir data block, depending
305 * on the inode inline data flag.
307 static int ocfs2_check_dir_entry(struct inode
* dir
,
308 struct ocfs2_dir_entry
* de
,
309 struct buffer_head
* bh
,
310 unsigned long offset
)
312 const char *error_msg
= NULL
;
313 const int rlen
= le16_to_cpu(de
->rec_len
);
315 if (rlen
< OCFS2_DIR_REC_LEN(1))
316 error_msg
= "rec_len is smaller than minimal";
317 else if (rlen
% 4 != 0)
318 error_msg
= "rec_len % 4 != 0";
319 else if (rlen
< OCFS2_DIR_REC_LEN(de
->name_len
))
320 error_msg
= "rec_len is too small for name_len";
321 else if (((char *) de
- bh
->b_data
) + rlen
> dir
->i_sb
->s_blocksize
)
322 error_msg
= "directory entry across blocks";
324 if (error_msg
!= NULL
)
325 mlog(ML_ERROR
, "bad entry in directory #%llu: %s - "
326 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
327 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, error_msg
,
328 offset
, (unsigned long long)le64_to_cpu(de
->inode
), rlen
,
330 return error_msg
== NULL
? 1 : 0;
333 static inline int ocfs2_match(int len
,
334 const char * const name
,
335 struct ocfs2_dir_entry
*de
)
337 if (len
!= de
->name_len
)
341 return !memcmp(name
, de
->name
, len
);
345 * Returns 0 if not found, -1 on failure, and 1 on success
347 static int inline ocfs2_search_dirblock(struct buffer_head
*bh
,
349 const char *name
, int namelen
,
350 unsigned long offset
,
353 struct ocfs2_dir_entry
**res_dir
)
355 struct ocfs2_dir_entry
*de
;
356 char *dlimit
, *de_buf
;
363 dlimit
= de_buf
+ bytes
;
365 while (de_buf
< dlimit
) {
366 /* this code is executed quadratically often */
367 /* do minimal checking `by hand' */
369 de
= (struct ocfs2_dir_entry
*) de_buf
;
371 if (de_buf
+ namelen
<= dlimit
&&
372 ocfs2_match(namelen
, name
, de
)) {
373 /* found a match - just to be sure, do a full check */
374 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
383 /* prevent looping on a bad block */
384 de_len
= le16_to_cpu(de
->rec_len
);
399 static struct buffer_head
*ocfs2_find_entry_id(const char *name
,
402 struct ocfs2_dir_entry
**res_dir
)
405 struct buffer_head
*di_bh
= NULL
;
406 struct ocfs2_dinode
*di
;
407 struct ocfs2_inline_data
*data
;
409 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
415 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
416 data
= &di
->id2
.i_data
;
418 found
= ocfs2_search_dirblock(di_bh
, dir
, name
, namelen
, 0,
419 data
->id_data
, i_size_read(dir
), res_dir
);
428 static int ocfs2_validate_dir_block(struct super_block
*sb
,
429 struct buffer_head
*bh
)
432 struct ocfs2_dir_block_trailer
*trailer
=
433 ocfs2_trailer_from_bh(bh
, sb
);
437 * We don't validate dirents here, that's handled
438 * in-place when the code walks them.
440 mlog(0, "Validating dirblock %llu\n",
441 (unsigned long long)bh
->b_blocknr
);
443 BUG_ON(!buffer_uptodate(bh
));
446 * If the ecc fails, we return the error but otherwise
447 * leave the filesystem running. We know any error is
448 * local to this block.
450 * Note that we are safe to call this even if the directory
451 * doesn't have a trailer. Filesystems without metaecc will do
452 * nothing, and filesystems with it will have one.
454 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &trailer
->db_check
);
456 mlog(ML_ERROR
, "Checksum failed for dinode %llu\n",
457 (unsigned long long)bh
->b_blocknr
);
463 * Validate a directory trailer.
465 * We check the trailer here rather than in ocfs2_validate_dir_block()
466 * because that function doesn't have the inode to test.
468 static int ocfs2_check_dir_trailer(struct inode
*dir
, struct buffer_head
*bh
)
471 struct ocfs2_dir_block_trailer
*trailer
;
473 trailer
= ocfs2_trailer_from_bh(bh
, dir
->i_sb
);
474 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer
)) {
476 ocfs2_error(dir
->i_sb
,
477 "Invalid dirblock #%llu: "
478 "signature = %.*s\n",
479 (unsigned long long)bh
->b_blocknr
, 7,
480 trailer
->db_signature
);
483 if (le64_to_cpu(trailer
->db_blkno
) != bh
->b_blocknr
) {
485 ocfs2_error(dir
->i_sb
,
486 "Directory block #%llu has an invalid "
488 (unsigned long long)bh
->b_blocknr
,
489 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
492 if (le64_to_cpu(trailer
->db_parent_dinode
) !=
493 OCFS2_I(dir
)->ip_blkno
) {
495 ocfs2_error(dir
->i_sb
,
496 "Directory block #%llu on dinode "
497 "#%llu has an invalid parent_dinode "
499 (unsigned long long)bh
->b_blocknr
,
500 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
501 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
509 * This function forces all errors to -EIO for consistency with its
510 * predecessor, ocfs2_bread(). We haven't audited what returning the
511 * real error codes would do to callers. We log the real codes with
512 * mlog_errno() before we squash them.
514 static int ocfs2_read_dir_block(struct inode
*inode
, u64 v_block
,
515 struct buffer_head
**bh
, int flags
)
518 struct buffer_head
*tmp
= *bh
;
520 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, flags
,
521 ocfs2_validate_dir_block
);
527 if (!(flags
& OCFS2_BH_READAHEAD
) &&
528 ocfs2_supports_dir_trailer(inode
)) {
529 rc
= ocfs2_check_dir_trailer(inode
, tmp
);
538 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
543 return rc
? -EIO
: 0;
547 * Read the block at 'phys' which belongs to this directory
548 * inode. This function does no virtual->physical block translation -
549 * what's passed in is assumed to be a valid directory block.
551 static int ocfs2_read_dir_block_direct(struct inode
*dir
, u64 phys
,
552 struct buffer_head
**bh
)
555 struct buffer_head
*tmp
= *bh
;
557 ret
= ocfs2_read_block(INODE_CACHE(dir
), phys
, &tmp
,
558 ocfs2_validate_dir_block
);
564 if (ocfs2_supports_dir_trailer(dir
)) {
565 ret
= ocfs2_check_dir_trailer(dir
, tmp
);
580 static int ocfs2_validate_dx_root(struct super_block
*sb
,
581 struct buffer_head
*bh
)
584 struct ocfs2_dx_root_block
*dx_root
;
586 BUG_ON(!buffer_uptodate(bh
));
588 dx_root
= (struct ocfs2_dx_root_block
*) bh
->b_data
;
590 ret
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &dx_root
->dr_check
);
593 "Checksum failed for dir index root block %llu\n",
594 (unsigned long long)bh
->b_blocknr
);
598 if (!OCFS2_IS_VALID_DX_ROOT(dx_root
)) {
600 "Dir Index Root # %llu has bad signature %.*s",
601 (unsigned long long)le64_to_cpu(dx_root
->dr_blkno
),
602 7, dx_root
->dr_signature
);
609 static int ocfs2_read_dx_root(struct inode
*dir
, struct ocfs2_dinode
*di
,
610 struct buffer_head
**dx_root_bh
)
613 u64 blkno
= le64_to_cpu(di
->i_dx_root
);
614 struct buffer_head
*tmp
= *dx_root_bh
;
616 ret
= ocfs2_read_block(INODE_CACHE(dir
), blkno
, &tmp
,
617 ocfs2_validate_dx_root
);
619 /* If ocfs2_read_block() got us a new bh, pass it up. */
620 if (!ret
&& !*dx_root_bh
)
626 static int ocfs2_validate_dx_leaf(struct super_block
*sb
,
627 struct buffer_head
*bh
)
630 struct ocfs2_dx_leaf
*dx_leaf
= (struct ocfs2_dx_leaf
*)bh
->b_data
;
632 BUG_ON(!buffer_uptodate(bh
));
634 ret
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &dx_leaf
->dl_check
);
637 "Checksum failed for dir index leaf block %llu\n",
638 (unsigned long long)bh
->b_blocknr
);
642 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf
)) {
643 ocfs2_error(sb
, "Dir Index Leaf has bad signature %.*s",
644 7, dx_leaf
->dl_signature
);
651 static int ocfs2_read_dx_leaf(struct inode
*dir
, u64 blkno
,
652 struct buffer_head
**dx_leaf_bh
)
655 struct buffer_head
*tmp
= *dx_leaf_bh
;
657 ret
= ocfs2_read_block(INODE_CACHE(dir
), blkno
, &tmp
,
658 ocfs2_validate_dx_leaf
);
660 /* If ocfs2_read_block() got us a new bh, pass it up. */
661 if (!ret
&& !*dx_leaf_bh
)
668 * Read a series of dx_leaf blocks. This expects all buffer_head
669 * pointers to be NULL on function entry.
671 static int ocfs2_read_dx_leaves(struct inode
*dir
, u64 start
, int num
,
672 struct buffer_head
**dx_leaf_bhs
)
676 ret
= ocfs2_read_blocks(INODE_CACHE(dir
), start
, num
, dx_leaf_bhs
, 0,
677 ocfs2_validate_dx_leaf
);
684 static struct buffer_head
*ocfs2_find_entry_el(const char *name
, int namelen
,
686 struct ocfs2_dir_entry
**res_dir
)
688 struct super_block
*sb
;
689 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
690 struct buffer_head
*bh
, *ret
= NULL
;
691 unsigned long start
, block
, b
;
692 int ra_max
= 0; /* Number of bh's in the readahead
694 int ra_ptr
= 0; /* Current index into readahead
703 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
704 start
= OCFS2_I(dir
)->ip_dir_start_lookup
;
705 if (start
>= nblocks
)
712 * We deal with the read-ahead logic here.
714 if (ra_ptr
>= ra_max
) {
715 /* Refill the readahead buffer */
718 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
720 * Terminate if we reach the end of the
721 * directory and must wrap, or if our
722 * search has finished at this block.
724 if (b
>= nblocks
|| (num
&& block
== start
)) {
725 bh_use
[ra_max
] = NULL
;
731 err
= ocfs2_read_dir_block(dir
, b
++, &bh
,
736 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
738 if (ocfs2_read_dir_block(dir
, block
, &bh
, 0)) {
739 /* read error, skip block & hope for the best.
740 * ocfs2_read_dir_block() has released the bh. */
741 ocfs2_error(dir
->i_sb
, "reading directory %llu, "
743 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
747 i
= ocfs2_search_dirblock(bh
, dir
, name
, namelen
,
748 block
<< sb
->s_blocksize_bits
,
749 bh
->b_data
, sb
->s_blocksize
,
752 OCFS2_I(dir
)->ip_dir_start_lookup
= block
;
754 goto cleanup_and_exit
;
758 goto cleanup_and_exit
;
761 if (++block
>= nblocks
)
763 } while (block
!= start
);
766 * If the directory has grown while we were searching, then
767 * search the last part of the directory before giving up.
770 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
771 if (block
< nblocks
) {
777 /* Clean up the read-ahead blocks */
778 for (; ra_ptr
< ra_max
; ra_ptr
++)
779 brelse(bh_use
[ra_ptr
]);
785 static int ocfs2_dx_dir_lookup_rec(struct inode
*inode
,
786 struct ocfs2_extent_list
*el
,
790 unsigned int *ret_clen
)
792 int ret
= 0, i
, found
;
793 struct buffer_head
*eb_bh
= NULL
;
794 struct ocfs2_extent_block
*eb
;
795 struct ocfs2_extent_rec
*rec
= NULL
;
797 if (el
->l_tree_depth
) {
798 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, major_hash
,
805 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
808 if (el
->l_tree_depth
) {
809 ocfs2_error(inode
->i_sb
,
810 "Inode %lu has non zero tree depth in "
811 "btree tree block %llu\n", inode
->i_ino
,
812 (unsigned long long)eb_bh
->b_blocknr
);
819 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
820 rec
= &el
->l_recs
[i
];
822 if (le32_to_cpu(rec
->e_cpos
) <= major_hash
) {
829 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
830 "record (%u, %u, 0) in btree", inode
->i_ino
,
831 le32_to_cpu(rec
->e_cpos
),
832 ocfs2_rec_clusters(el
, rec
));
838 *ret_phys_blkno
= le64_to_cpu(rec
->e_blkno
);
840 *ret_cpos
= le32_to_cpu(rec
->e_cpos
);
842 *ret_clen
= le16_to_cpu(rec
->e_leaf_clusters
);
850 * Returns the block index, from the start of the cluster which this
853 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super
*osb
,
856 return minor_hash
& osb
->osb_dx_mask
;
859 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super
*osb
,
860 struct ocfs2_dx_hinfo
*hinfo
)
862 return __ocfs2_dx_dir_hash_idx(osb
, hinfo
->minor_hash
);
865 static int ocfs2_dx_dir_lookup(struct inode
*inode
,
866 struct ocfs2_extent_list
*el
,
867 struct ocfs2_dx_hinfo
*hinfo
,
872 unsigned int cend
, uninitialized_var(clen
);
873 u32
uninitialized_var(cpos
);
874 u64
uninitialized_var(blkno
);
875 u32 name_hash
= hinfo
->major_hash
;
877 ret
= ocfs2_dx_dir_lookup_rec(inode
, el
, name_hash
, &cpos
, &blkno
,
885 if (name_hash
>= cend
) {
886 /* We want the last cluster */
887 blkno
+= ocfs2_clusters_to_blocks(inode
->i_sb
, clen
- 1);
890 blkno
+= ocfs2_clusters_to_blocks(inode
->i_sb
,
896 * We now have the cluster which should hold our entry. To
897 * find the exact block from the start of the cluster to
898 * search, we take the lower bits of the hash.
900 blkno
+= ocfs2_dx_dir_hash_idx(OCFS2_SB(inode
->i_sb
), hinfo
);
903 *ret_phys_blkno
= blkno
;
912 static int ocfs2_dx_dir_search(const char *name
, int namelen
,
914 struct ocfs2_dx_root_block
*dx_root
,
915 struct ocfs2_dir_lookup_result
*res
)
918 u64
uninitialized_var(phys
);
919 struct buffer_head
*dx_leaf_bh
= NULL
;
920 struct ocfs2_dx_leaf
*dx_leaf
;
921 struct ocfs2_dx_entry
*dx_entry
= NULL
;
922 struct buffer_head
*dir_ent_bh
= NULL
;
923 struct ocfs2_dir_entry
*dir_ent
= NULL
;
924 struct ocfs2_dx_hinfo
*hinfo
= &res
->dl_hinfo
;
925 struct ocfs2_extent_list
*dr_el
;
926 struct ocfs2_dx_entry_list
*entry_list
;
928 ocfs2_dx_dir_name_hash(dir
, name
, namelen
, &res
->dl_hinfo
);
930 if (ocfs2_dx_root_inline(dx_root
)) {
931 entry_list
= &dx_root
->dr_entries
;
935 dr_el
= &dx_root
->dr_list
;
937 ret
= ocfs2_dx_dir_lookup(dir
, dr_el
, hinfo
, NULL
, &phys
);
943 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
945 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
946 namelen
, name
, hinfo
->major_hash
, hinfo
->minor_hash
,
947 (unsigned long long)phys
);
949 ret
= ocfs2_read_dx_leaf(dir
, phys
, &dx_leaf_bh
);
955 dx_leaf
= (struct ocfs2_dx_leaf
*) dx_leaf_bh
->b_data
;
957 mlog(0, "leaf info: num_used: %d, count: %d\n",
958 le16_to_cpu(dx_leaf
->dl_list
.de_num_used
),
959 le16_to_cpu(dx_leaf
->dl_list
.de_count
));
961 entry_list
= &dx_leaf
->dl_list
;
965 * Empty leaf is legal, so no need to check for that.
968 for (i
= 0; i
< le16_to_cpu(entry_list
->de_num_used
); i
++) {
969 dx_entry
= &entry_list
->de_entries
[i
];
971 if (hinfo
->major_hash
!= le32_to_cpu(dx_entry
->dx_major_hash
)
972 || hinfo
->minor_hash
!= le32_to_cpu(dx_entry
->dx_minor_hash
))
976 * Search unindexed leaf block now. We're not
977 * guaranteed to find anything.
979 ret
= ocfs2_read_dir_block_direct(dir
,
980 le64_to_cpu(dx_entry
->dx_dirent_blk
),
988 found
= ocfs2_search_dirblock(dir_ent_bh
, dir
, name
, namelen
,
989 0, dir_ent_bh
->b_data
,
990 dir
->i_sb
->s_blocksize
, &dir_ent
);
995 /* This means we found a bad directory entry. */
1010 res
->dl_leaf_bh
= dir_ent_bh
;
1011 res
->dl_entry
= dir_ent
;
1012 res
->dl_dx_leaf_bh
= dx_leaf_bh
;
1013 res
->dl_dx_entry
= dx_entry
;
1024 static int ocfs2_find_entry_dx(const char *name
, int namelen
,
1026 struct ocfs2_dir_lookup_result
*lookup
)
1029 struct buffer_head
*di_bh
= NULL
;
1030 struct ocfs2_dinode
*di
;
1031 struct buffer_head
*dx_root_bh
= NULL
;
1032 struct ocfs2_dx_root_block
*dx_root
;
1034 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
1040 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1042 ret
= ocfs2_read_dx_root(dir
, di
, &dx_root_bh
);
1047 dx_root
= (struct ocfs2_dx_root_block
*) dx_root_bh
->b_data
;
1049 ret
= ocfs2_dx_dir_search(name
, namelen
, dir
, dx_root
, lookup
);
1056 lookup
->dl_dx_root_bh
= dx_root_bh
;
1065 * Try to find an entry of the provided name within 'dir'.
1067 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1068 * returned and the struct 'res' will contain information useful to
1069 * other directory manipulation functions.
1071 * Caller can NOT assume anything about the contents of the
1072 * buffer_heads - they are passed back only so that it can be passed
1073 * into any one of the manipulation functions (add entry, delete
1074 * entry, etc). As an example, bh in the extent directory case is a
1075 * data block, in the inline-data case it actually points to an inode,
1076 * in the indexed directory case, multiple buffers are involved.
1078 int ocfs2_find_entry(const char *name
, int namelen
,
1079 struct inode
*dir
, struct ocfs2_dir_lookup_result
*lookup
)
1081 struct buffer_head
*bh
;
1082 struct ocfs2_dir_entry
*res_dir
= NULL
;
1084 if (ocfs2_dir_indexed(dir
))
1085 return ocfs2_find_entry_dx(name
, namelen
, dir
, lookup
);
1088 * The unindexed dir code only uses part of the lookup
1089 * structure, so there's no reason to push it down further
1092 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1093 bh
= ocfs2_find_entry_id(name
, namelen
, dir
, &res_dir
);
1095 bh
= ocfs2_find_entry_el(name
, namelen
, dir
, &res_dir
);
1100 lookup
->dl_leaf_bh
= bh
;
1101 lookup
->dl_entry
= res_dir
;
1106 * Update inode number and type of a previously found directory entry.
1108 int ocfs2_update_entry(struct inode
*dir
, handle_t
*handle
,
1109 struct ocfs2_dir_lookup_result
*res
,
1110 struct inode
*new_entry_inode
)
1113 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
1114 struct ocfs2_dir_entry
*de
= res
->dl_entry
;
1115 struct buffer_head
*de_bh
= res
->dl_leaf_bh
;
1118 * The same code works fine for both inline-data and extent
1119 * based directories, so no need to split this up. The only
1120 * difference is the journal_access function.
1123 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1124 access
= ocfs2_journal_access_di
;
1126 ret
= access(handle
, INODE_CACHE(dir
), de_bh
,
1127 OCFS2_JOURNAL_ACCESS_WRITE
);
1133 de
->inode
= cpu_to_le64(OCFS2_I(new_entry_inode
)->ip_blkno
);
1134 ocfs2_set_de_type(de
, new_entry_inode
->i_mode
);
1136 ocfs2_journal_dirty(handle
, de_bh
);
1143 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1146 static int __ocfs2_delete_entry(handle_t
*handle
, struct inode
*dir
,
1147 struct ocfs2_dir_entry
*de_del
,
1148 struct buffer_head
*bh
, char *first_de
,
1151 struct ocfs2_dir_entry
*de
, *pde
;
1152 int i
, status
= -ENOENT
;
1153 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
1155 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle
, dir
, de_del
, bh
);
1157 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1158 access
= ocfs2_journal_access_di
;
1162 de
= (struct ocfs2_dir_entry
*) first_de
;
1164 if (!ocfs2_check_dir_entry(dir
, de
, bh
, i
)) {
1170 status
= access(handle
, INODE_CACHE(dir
), bh
,
1171 OCFS2_JOURNAL_ACCESS_WRITE
);
1178 le16_add_cpu(&pde
->rec_len
,
1179 le16_to_cpu(de
->rec_len
));
1183 ocfs2_journal_dirty(handle
, bh
);
1186 i
+= le16_to_cpu(de
->rec_len
);
1188 de
= (struct ocfs2_dir_entry
*)((char *)de
+ le16_to_cpu(de
->rec_len
));
1195 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry
*de
)
1199 if (le64_to_cpu(de
->inode
) == 0)
1200 hole
= le16_to_cpu(de
->rec_len
);
1202 hole
= le16_to_cpu(de
->rec_len
) -
1203 OCFS2_DIR_REC_LEN(de
->name_len
);
1208 static int ocfs2_find_max_rec_len(struct super_block
*sb
,
1209 struct buffer_head
*dirblock_bh
)
1211 int size
, this_hole
, largest_hole
= 0;
1212 char *trailer
, *de_buf
, *limit
, *start
= dirblock_bh
->b_data
;
1213 struct ocfs2_dir_entry
*de
;
1215 trailer
= (char *)ocfs2_trailer_from_bh(dirblock_bh
, sb
);
1216 size
= ocfs2_dir_trailer_blk_off(sb
);
1217 limit
= start
+ size
;
1219 de
= (struct ocfs2_dir_entry
*)de_buf
;
1221 if (de_buf
!= trailer
) {
1222 this_hole
= ocfs2_figure_dirent_hole(de
);
1223 if (this_hole
> largest_hole
)
1224 largest_hole
= this_hole
;
1227 de_buf
+= le16_to_cpu(de
->rec_len
);
1228 de
= (struct ocfs2_dir_entry
*)de_buf
;
1229 } while (de_buf
< limit
);
1231 if (largest_hole
>= OCFS2_DIR_MIN_REC_LEN
)
1232 return largest_hole
;
1236 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list
*entry_list
,
1239 int num_used
= le16_to_cpu(entry_list
->de_num_used
);
1241 if (num_used
== 1 || index
== (num_used
- 1))
1244 memmove(&entry_list
->de_entries
[index
],
1245 &entry_list
->de_entries
[index
+ 1],
1246 (num_used
- index
- 1)*sizeof(struct ocfs2_dx_entry
));
1249 memset(&entry_list
->de_entries
[num_used
], 0,
1250 sizeof(struct ocfs2_dx_entry
));
1251 entry_list
->de_num_used
= cpu_to_le16(num_used
);
1254 static int ocfs2_delete_entry_dx(handle_t
*handle
, struct inode
*dir
,
1255 struct ocfs2_dir_lookup_result
*lookup
)
1257 int ret
, index
, max_rec_len
, add_to_free_list
= 0;
1258 struct buffer_head
*dx_root_bh
= lookup
->dl_dx_root_bh
;
1259 struct buffer_head
*leaf_bh
= lookup
->dl_leaf_bh
;
1260 struct ocfs2_dx_leaf
*dx_leaf
;
1261 struct ocfs2_dx_entry
*dx_entry
= lookup
->dl_dx_entry
;
1262 struct ocfs2_dir_block_trailer
*trailer
;
1263 struct ocfs2_dx_root_block
*dx_root
;
1264 struct ocfs2_dx_entry_list
*entry_list
;
1267 * This function gets a bit messy because we might have to
1268 * modify the root block, regardless of whether the indexed
1269 * entries are stored inline.
1273 * *Only* set 'entry_list' here, based on where we're looking
1274 * for the indexed entries. Later, we might still want to
1275 * journal both blocks, based on free list state.
1277 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
1278 if (ocfs2_dx_root_inline(dx_root
)) {
1279 entry_list
= &dx_root
->dr_entries
;
1281 dx_leaf
= (struct ocfs2_dx_leaf
*) lookup
->dl_dx_leaf_bh
->b_data
;
1282 entry_list
= &dx_leaf
->dl_list
;
1285 /* Neither of these are a disk corruption - that should have
1286 * been caught by lookup, before we got here. */
1287 BUG_ON(le16_to_cpu(entry_list
->de_count
) <= 0);
1288 BUG_ON(le16_to_cpu(entry_list
->de_num_used
) <= 0);
1290 index
= (char *)dx_entry
- (char *)entry_list
->de_entries
;
1291 index
/= sizeof(*dx_entry
);
1293 if (index
>= le16_to_cpu(entry_list
->de_num_used
)) {
1294 mlog(ML_ERROR
, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1295 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, index
,
1296 entry_list
, dx_entry
);
1301 * We know that removal of this dirent will leave enough room
1302 * for a new one, so add this block to the free list if it
1303 * isn't already there.
1305 trailer
= ocfs2_trailer_from_bh(leaf_bh
, dir
->i_sb
);
1306 if (trailer
->db_free_rec_len
== 0)
1307 add_to_free_list
= 1;
1310 * Add the block holding our index into the journal before
1311 * removing the unindexed entry. If we get an error return
1312 * from __ocfs2_delete_entry(), then it hasn't removed the
1313 * entry yet. Likewise, successful return means we *must*
1314 * remove the indexed entry.
1316 * We're also careful to journal the root tree block here as
1317 * the entry count needs to be updated. Also, we might be
1318 * adding to the start of the free list.
1320 ret
= ocfs2_journal_access_dr(handle
, INODE_CACHE(dir
), dx_root_bh
,
1321 OCFS2_JOURNAL_ACCESS_WRITE
);
1327 if (!ocfs2_dx_root_inline(dx_root
)) {
1328 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
),
1329 lookup
->dl_dx_leaf_bh
,
1330 OCFS2_JOURNAL_ACCESS_WRITE
);
1337 mlog(0, "Dir %llu: delete entry at index: %d\n",
1338 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, index
);
1340 ret
= __ocfs2_delete_entry(handle
, dir
, lookup
->dl_entry
,
1341 leaf_bh
, leaf_bh
->b_data
, leaf_bh
->b_size
);
1347 max_rec_len
= ocfs2_find_max_rec_len(dir
->i_sb
, leaf_bh
);
1348 trailer
->db_free_rec_len
= cpu_to_le16(max_rec_len
);
1349 if (add_to_free_list
) {
1350 trailer
->db_free_next
= dx_root
->dr_free_blk
;
1351 dx_root
->dr_free_blk
= cpu_to_le64(leaf_bh
->b_blocknr
);
1352 ocfs2_journal_dirty(handle
, dx_root_bh
);
1355 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1356 ocfs2_journal_dirty(handle
, leaf_bh
);
1358 le32_add_cpu(&dx_root
->dr_num_entries
, -1);
1359 ocfs2_journal_dirty(handle
, dx_root_bh
);
1361 ocfs2_dx_list_remove_entry(entry_list
, index
);
1363 if (!ocfs2_dx_root_inline(dx_root
))
1364 ocfs2_journal_dirty(handle
, lookup
->dl_dx_leaf_bh
);
1370 static inline int ocfs2_delete_entry_id(handle_t
*handle
,
1372 struct ocfs2_dir_entry
*de_del
,
1373 struct buffer_head
*bh
)
1376 struct buffer_head
*di_bh
= NULL
;
1377 struct ocfs2_dinode
*di
;
1378 struct ocfs2_inline_data
*data
;
1380 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
1386 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1387 data
= &di
->id2
.i_data
;
1389 ret
= __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, data
->id_data
,
1397 static inline int ocfs2_delete_entry_el(handle_t
*handle
,
1399 struct ocfs2_dir_entry
*de_del
,
1400 struct buffer_head
*bh
)
1402 return __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, bh
->b_data
,
1407 * Delete a directory entry. Hide the details of directory
1408 * implementation from the caller.
1410 int ocfs2_delete_entry(handle_t
*handle
,
1412 struct ocfs2_dir_lookup_result
*res
)
1414 if (ocfs2_dir_indexed(dir
))
1415 return ocfs2_delete_entry_dx(handle
, dir
, res
);
1417 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1418 return ocfs2_delete_entry_id(handle
, dir
, res
->dl_entry
,
1421 return ocfs2_delete_entry_el(handle
, dir
, res
->dl_entry
,
1426 * Check whether 'de' has enough room to hold an entry of
1427 * 'new_rec_len' bytes.
1429 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry
*de
,
1430 unsigned int new_rec_len
)
1432 unsigned int de_really_used
;
1434 /* Check whether this is an empty record with enough space */
1435 if (le64_to_cpu(de
->inode
) == 0 &&
1436 le16_to_cpu(de
->rec_len
) >= new_rec_len
)
1440 * Record might have free space at the end which we can
1443 de_really_used
= OCFS2_DIR_REC_LEN(de
->name_len
);
1444 if (le16_to_cpu(de
->rec_len
) >= (de_really_used
+ new_rec_len
))
1450 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf
*dx_leaf
,
1451 struct ocfs2_dx_entry
*dx_new_entry
)
1455 i
= le16_to_cpu(dx_leaf
->dl_list
.de_num_used
);
1456 dx_leaf
->dl_list
.de_entries
[i
] = *dx_new_entry
;
1458 le16_add_cpu(&dx_leaf
->dl_list
.de_num_used
, 1);
1461 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list
*entry_list
,
1462 struct ocfs2_dx_hinfo
*hinfo
,
1466 struct ocfs2_dx_entry
*dx_entry
;
1468 i
= le16_to_cpu(entry_list
->de_num_used
);
1469 dx_entry
= &entry_list
->de_entries
[i
];
1471 memset(dx_entry
, 0, sizeof(*dx_entry
));
1472 dx_entry
->dx_major_hash
= cpu_to_le32(hinfo
->major_hash
);
1473 dx_entry
->dx_minor_hash
= cpu_to_le32(hinfo
->minor_hash
);
1474 dx_entry
->dx_dirent_blk
= cpu_to_le64(dirent_blk
);
1476 le16_add_cpu(&entry_list
->de_num_used
, 1);
1479 static int __ocfs2_dx_dir_leaf_insert(struct inode
*dir
, handle_t
*handle
,
1480 struct ocfs2_dx_hinfo
*hinfo
,
1482 struct buffer_head
*dx_leaf_bh
)
1485 struct ocfs2_dx_leaf
*dx_leaf
;
1487 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
), dx_leaf_bh
,
1488 OCFS2_JOURNAL_ACCESS_WRITE
);
1494 dx_leaf
= (struct ocfs2_dx_leaf
*)dx_leaf_bh
->b_data
;
1495 ocfs2_dx_entry_list_insert(&dx_leaf
->dl_list
, hinfo
, dirent_blk
);
1496 ocfs2_journal_dirty(handle
, dx_leaf_bh
);
1502 static void ocfs2_dx_inline_root_insert(struct inode
*dir
, handle_t
*handle
,
1503 struct ocfs2_dx_hinfo
*hinfo
,
1505 struct ocfs2_dx_root_block
*dx_root
)
1507 ocfs2_dx_entry_list_insert(&dx_root
->dr_entries
, hinfo
, dirent_blk
);
1510 static int ocfs2_dx_dir_insert(struct inode
*dir
, handle_t
*handle
,
1511 struct ocfs2_dir_lookup_result
*lookup
)
1514 struct ocfs2_dx_root_block
*dx_root
;
1515 struct buffer_head
*dx_root_bh
= lookup
->dl_dx_root_bh
;
1517 ret
= ocfs2_journal_access_dr(handle
, INODE_CACHE(dir
), dx_root_bh
,
1518 OCFS2_JOURNAL_ACCESS_WRITE
);
1524 dx_root
= (struct ocfs2_dx_root_block
*)lookup
->dl_dx_root_bh
->b_data
;
1525 if (ocfs2_dx_root_inline(dx_root
)) {
1526 ocfs2_dx_inline_root_insert(dir
, handle
,
1528 lookup
->dl_leaf_bh
->b_blocknr
,
1531 ret
= __ocfs2_dx_dir_leaf_insert(dir
, handle
, &lookup
->dl_hinfo
,
1532 lookup
->dl_leaf_bh
->b_blocknr
,
1533 lookup
->dl_dx_leaf_bh
);
1538 le32_add_cpu(&dx_root
->dr_num_entries
, 1);
1539 ocfs2_journal_dirty(handle
, dx_root_bh
);
1545 static void ocfs2_remove_block_from_free_list(struct inode
*dir
,
1547 struct ocfs2_dir_lookup_result
*lookup
)
1549 struct ocfs2_dir_block_trailer
*trailer
, *prev
;
1550 struct ocfs2_dx_root_block
*dx_root
;
1551 struct buffer_head
*bh
;
1553 trailer
= ocfs2_trailer_from_bh(lookup
->dl_leaf_bh
, dir
->i_sb
);
1555 if (ocfs2_free_list_at_root(lookup
)) {
1556 bh
= lookup
->dl_dx_root_bh
;
1557 dx_root
= (struct ocfs2_dx_root_block
*)bh
->b_data
;
1558 dx_root
->dr_free_blk
= trailer
->db_free_next
;
1560 bh
= lookup
->dl_prev_leaf_bh
;
1561 prev
= ocfs2_trailer_from_bh(bh
, dir
->i_sb
);
1562 prev
->db_free_next
= trailer
->db_free_next
;
1565 trailer
->db_free_rec_len
= cpu_to_le16(0);
1566 trailer
->db_free_next
= cpu_to_le64(0);
1568 ocfs2_journal_dirty(handle
, bh
);
1569 ocfs2_journal_dirty(handle
, lookup
->dl_leaf_bh
);
1573 * This expects that a journal write has been reserved on
1574 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1576 static void ocfs2_recalc_free_list(struct inode
*dir
, handle_t
*handle
,
1577 struct ocfs2_dir_lookup_result
*lookup
)
1580 struct ocfs2_dir_block_trailer
*trailer
;
1582 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1583 max_rec_len
= ocfs2_find_max_rec_len(dir
->i_sb
, lookup
->dl_leaf_bh
);
1586 * There's still room in this block, so no need to remove it
1587 * from the free list. In this case, we just want to update
1588 * the rec len accounting.
1590 trailer
= ocfs2_trailer_from_bh(lookup
->dl_leaf_bh
, dir
->i_sb
);
1591 trailer
->db_free_rec_len
= cpu_to_le16(max_rec_len
);
1592 ocfs2_journal_dirty(handle
, lookup
->dl_leaf_bh
);
1594 ocfs2_remove_block_from_free_list(dir
, handle
, lookup
);
1598 /* we don't always have a dentry for what we want to add, so people
1599 * like orphan dir can call this instead.
1601 * The lookup context must have been filled from
1602 * ocfs2_prepare_dir_for_insert.
1604 int __ocfs2_add_entry(handle_t
*handle
,
1606 const char *name
, int namelen
,
1607 struct inode
*inode
, u64 blkno
,
1608 struct buffer_head
*parent_fe_bh
,
1609 struct ocfs2_dir_lookup_result
*lookup
)
1611 unsigned long offset
;
1612 unsigned short rec_len
;
1613 struct ocfs2_dir_entry
*de
, *de1
;
1614 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)parent_fe_bh
->b_data
;
1615 struct super_block
*sb
= dir
->i_sb
;
1617 unsigned int size
= sb
->s_blocksize
;
1618 struct buffer_head
*insert_bh
= lookup
->dl_leaf_bh
;
1619 char *data_start
= insert_bh
->b_data
;
1626 if (ocfs2_dir_indexed(dir
)) {
1627 struct buffer_head
*bh
;
1629 if (ocfs2_free_list_at_root(lookup
)) {
1630 bh
= lookup
->dl_dx_root_bh
;
1631 retval
= ocfs2_journal_access_dr(handle
,
1632 INODE_CACHE(dir
), bh
,
1633 OCFS2_JOURNAL_ACCESS_WRITE
);
1635 bh
= lookup
->dl_prev_leaf_bh
;
1636 retval
= ocfs2_journal_access_db(handle
,
1637 INODE_CACHE(dir
), bh
,
1638 OCFS2_JOURNAL_ACCESS_WRITE
);
1644 } else if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1645 data_start
= di
->id2
.i_data
.id_data
;
1646 size
= i_size_read(dir
);
1648 BUG_ON(insert_bh
!= parent_fe_bh
);
1651 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
1653 de
= (struct ocfs2_dir_entry
*) data_start
;
1655 BUG_ON((char *)de
>= (size
+ data_start
));
1657 /* These checks should've already been passed by the
1658 * prepare function, but I guess we can leave them
1660 if (!ocfs2_check_dir_entry(dir
, de
, insert_bh
, offset
)) {
1664 if (ocfs2_match(namelen
, name
, de
)) {
1669 /* We're guaranteed that we should have space, so we
1670 * can't possibly have hit the trailer...right? */
1671 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir
, de
, offset
, size
),
1672 "Hit dir trailer trying to insert %.*s "
1673 "(namelen %d) into directory %llu. "
1674 "offset is %lu, trailer offset is %d\n",
1675 namelen
, name
, namelen
,
1676 (unsigned long long)parent_fe_bh
->b_blocknr
,
1677 offset
, ocfs2_dir_trailer_blk_off(dir
->i_sb
));
1679 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
1680 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1681 retval
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
1687 if (insert_bh
== parent_fe_bh
)
1688 status
= ocfs2_journal_access_di(handle
,
1691 OCFS2_JOURNAL_ACCESS_WRITE
);
1693 status
= ocfs2_journal_access_db(handle
,
1696 OCFS2_JOURNAL_ACCESS_WRITE
);
1698 if (ocfs2_dir_indexed(dir
)) {
1699 status
= ocfs2_dx_dir_insert(dir
,
1709 /* By now the buffer is marked for journaling */
1710 offset
+= le16_to_cpu(de
->rec_len
);
1711 if (le64_to_cpu(de
->inode
)) {
1712 de1
= (struct ocfs2_dir_entry
*)((char *) de
+
1713 OCFS2_DIR_REC_LEN(de
->name_len
));
1715 cpu_to_le16(le16_to_cpu(de
->rec_len
) -
1716 OCFS2_DIR_REC_LEN(de
->name_len
));
1717 de
->rec_len
= cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
1720 de
->file_type
= OCFS2_FT_UNKNOWN
;
1722 de
->inode
= cpu_to_le64(blkno
);
1723 ocfs2_set_de_type(de
, inode
->i_mode
);
1726 de
->name_len
= namelen
;
1727 memcpy(de
->name
, name
, namelen
);
1729 if (ocfs2_dir_indexed(dir
))
1730 ocfs2_recalc_free_list(dir
, handle
, lookup
);
1733 ocfs2_journal_dirty(handle
, insert_bh
);
1738 offset
+= le16_to_cpu(de
->rec_len
);
1739 de
= (struct ocfs2_dir_entry
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
1742 /* when you think about it, the assert above should prevent us
1743 * from ever getting here. */
1751 static int ocfs2_dir_foreach_blk_id(struct inode
*inode
,
1753 loff_t
*f_pos
, void *priv
,
1754 filldir_t filldir
, int *filldir_err
)
1756 int ret
, i
, filldir_ret
;
1757 unsigned long offset
= *f_pos
;
1758 struct buffer_head
*di_bh
= NULL
;
1759 struct ocfs2_dinode
*di
;
1760 struct ocfs2_inline_data
*data
;
1761 struct ocfs2_dir_entry
*de
;
1763 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
1765 mlog(ML_ERROR
, "Unable to read inode block for dir %llu\n",
1766 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1770 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1771 data
= &di
->id2
.i_data
;
1773 while (*f_pos
< i_size_read(inode
)) {
1775 /* If the dir block has changed since the last call to
1776 * readdir(2), then we might be pointing to an invalid
1777 * dirent right now. Scan from the start of the block
1779 if (*f_version
!= inode
->i_version
) {
1780 for (i
= 0; i
< i_size_read(inode
) && i
< offset
; ) {
1781 de
= (struct ocfs2_dir_entry
*)
1782 (data
->id_data
+ i
);
1783 /* It's too expensive to do a full
1784 * dirent test each time round this
1785 * loop, but we do have to test at
1786 * least that it is non-zero. A
1787 * failure will be detected in the
1788 * dirent test below. */
1789 if (le16_to_cpu(de
->rec_len
) <
1790 OCFS2_DIR_REC_LEN(1))
1792 i
+= le16_to_cpu(de
->rec_len
);
1794 *f_pos
= offset
= i
;
1795 *f_version
= inode
->i_version
;
1798 de
= (struct ocfs2_dir_entry
*) (data
->id_data
+ *f_pos
);
1799 if (!ocfs2_check_dir_entry(inode
, de
, di_bh
, *f_pos
)) {
1800 /* On error, skip the f_pos to the end. */
1801 *f_pos
= i_size_read(inode
);
1804 offset
+= le16_to_cpu(de
->rec_len
);
1805 if (le64_to_cpu(de
->inode
)) {
1806 /* We might block in the next section
1807 * if the data destination is
1808 * currently swapped out. So, use a
1809 * version stamp to detect whether or
1810 * not the directory has been modified
1811 * during the copy operation.
1813 u64 version
= *f_version
;
1814 unsigned char d_type
= DT_UNKNOWN
;
1816 if (de
->file_type
< OCFS2_FT_MAX
)
1817 d_type
= ocfs2_filetype_table
[de
->file_type
];
1819 filldir_ret
= filldir(priv
, de
->name
,
1822 le64_to_cpu(de
->inode
),
1826 *filldir_err
= filldir_ret
;
1829 if (version
!= *f_version
)
1832 *f_pos
+= le16_to_cpu(de
->rec_len
);
1842 * NOTE: This function can be called against unindexed directories,
1845 static int ocfs2_dir_foreach_blk_el(struct inode
*inode
,
1847 loff_t
*f_pos
, void *priv
,
1848 filldir_t filldir
, int *filldir_err
)
1851 unsigned long offset
, blk
, last_ra_blk
= 0;
1853 struct buffer_head
* bh
, * tmp
;
1854 struct ocfs2_dir_entry
* de
;
1855 struct super_block
* sb
= inode
->i_sb
;
1856 unsigned int ra_sectors
= 16;
1861 offset
= (*f_pos
) & (sb
->s_blocksize
- 1);
1863 while (!error
&& !stored
&& *f_pos
< i_size_read(inode
)) {
1864 blk
= (*f_pos
) >> sb
->s_blocksize_bits
;
1865 if (ocfs2_read_dir_block(inode
, blk
, &bh
, 0)) {
1866 /* Skip the corrupt dirblock and keep trying */
1867 *f_pos
+= sb
->s_blocksize
- offset
;
1871 /* The idea here is to begin with 8k read-ahead and to stay
1872 * 4k ahead of our current position.
1874 * TODO: Use the pagecache for this. We just need to
1875 * make sure it's cluster-safe... */
1877 || (((last_ra_blk
- blk
) << 9) <= (ra_sectors
/ 2))) {
1878 for (i
= ra_sectors
>> (sb
->s_blocksize_bits
- 9);
1881 if (!ocfs2_read_dir_block(inode
, ++blk
, &tmp
,
1882 OCFS2_BH_READAHEAD
))
1890 /* If the dir block has changed since the last call to
1891 * readdir(2), then we might be pointing to an invalid
1892 * dirent right now. Scan from the start of the block
1894 if (*f_version
!= inode
->i_version
) {
1895 for (i
= 0; i
< sb
->s_blocksize
&& i
< offset
; ) {
1896 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ i
);
1897 /* It's too expensive to do a full
1898 * dirent test each time round this
1899 * loop, but we do have to test at
1900 * least that it is non-zero. A
1901 * failure will be detected in the
1902 * dirent test below. */
1903 if (le16_to_cpu(de
->rec_len
) <
1904 OCFS2_DIR_REC_LEN(1))
1906 i
+= le16_to_cpu(de
->rec_len
);
1909 *f_pos
= ((*f_pos
) & ~(sb
->s_blocksize
- 1))
1911 *f_version
= inode
->i_version
;
1914 while (!error
&& *f_pos
< i_size_read(inode
)
1915 && offset
< sb
->s_blocksize
) {
1916 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ offset
);
1917 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
1918 /* On error, skip the f_pos to the
1920 *f_pos
= ((*f_pos
) | (sb
->s_blocksize
- 1)) + 1;
1924 offset
+= le16_to_cpu(de
->rec_len
);
1925 if (le64_to_cpu(de
->inode
)) {
1926 /* We might block in the next section
1927 * if the data destination is
1928 * currently swapped out. So, use a
1929 * version stamp to detect whether or
1930 * not the directory has been modified
1931 * during the copy operation.
1933 unsigned long version
= *f_version
;
1934 unsigned char d_type
= DT_UNKNOWN
;
1936 if (de
->file_type
< OCFS2_FT_MAX
)
1937 d_type
= ocfs2_filetype_table
[de
->file_type
];
1938 error
= filldir(priv
, de
->name
,
1941 le64_to_cpu(de
->inode
),
1945 *filldir_err
= error
;
1948 if (version
!= *f_version
)
1952 *f_pos
+= le16_to_cpu(de
->rec_len
);
1964 static int ocfs2_dir_foreach_blk(struct inode
*inode
, u64
*f_version
,
1965 loff_t
*f_pos
, void *priv
, filldir_t filldir
,
1968 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1969 return ocfs2_dir_foreach_blk_id(inode
, f_version
, f_pos
, priv
,
1970 filldir
, filldir_err
);
1972 return ocfs2_dir_foreach_blk_el(inode
, f_version
, f_pos
, priv
, filldir
,
1977 * This is intended to be called from inside other kernel functions,
1978 * so we fake some arguments.
1980 int ocfs2_dir_foreach(struct inode
*inode
, loff_t
*f_pos
, void *priv
,
1983 int ret
= 0, filldir_err
= 0;
1984 u64 version
= inode
->i_version
;
1986 while (*f_pos
< i_size_read(inode
)) {
1987 ret
= ocfs2_dir_foreach_blk(inode
, &version
, f_pos
, priv
,
1988 filldir
, &filldir_err
);
1989 if (ret
|| filldir_err
)
2003 int ocfs2_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2006 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2009 mlog_entry("dirino=%llu\n",
2010 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
2012 error
= ocfs2_inode_lock_atime(inode
, filp
->f_vfsmnt
, &lock_level
);
2013 if (lock_level
&& error
>= 0) {
2014 /* We release EX lock which used to update atime
2015 * and get PR lock again to reduce contention
2016 * on commonly accessed directories. */
2017 ocfs2_inode_unlock(inode
, 1);
2019 error
= ocfs2_inode_lock(inode
, NULL
, 0);
2022 if (error
!= -ENOENT
)
2024 /* we haven't got any yet, so propagate the error. */
2028 error
= ocfs2_dir_foreach_blk(inode
, &filp
->f_version
, &filp
->f_pos
,
2029 dirent
, filldir
, NULL
);
2031 ocfs2_inode_unlock(inode
, lock_level
);
2040 * NOTE: this should always be called with parent dir i_mutex taken.
2042 int ocfs2_find_files_on_disk(const char *name
,
2045 struct inode
*inode
,
2046 struct ocfs2_dir_lookup_result
*lookup
)
2048 int status
= -ENOENT
;
2050 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen
, name
, blkno
,
2051 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
2053 status
= ocfs2_find_entry(name
, namelen
, inode
, lookup
);
2057 *blkno
= le64_to_cpu(lookup
->dl_entry
->inode
);
2066 * Convenience function for callers which just want the block number
2067 * mapped to a name and don't require the full dirent info, etc.
2069 int ocfs2_lookup_ino_from_name(struct inode
*dir
, const char *name
,
2070 int namelen
, u64
*blkno
)
2073 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
2075 ret
= ocfs2_find_files_on_disk(name
, namelen
, blkno
, dir
, &lookup
);
2076 ocfs2_free_dir_lookup_result(&lookup
);
2081 /* Check for a name within a directory.
2083 * Return 0 if the name does not exist
2084 * Return -EEXIST if the directory contains the name
2086 * Callers should have i_mutex + a cluster lock on dir
2088 int ocfs2_check_dir_for_entry(struct inode
*dir
,
2093 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
2095 mlog_entry("dir %llu, name '%.*s'\n",
2096 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, namelen
, name
);
2099 if (ocfs2_find_entry(name
, namelen
, dir
, &lookup
) == 0)
2104 ocfs2_free_dir_lookup_result(&lookup
);
2110 struct ocfs2_empty_dir_priv
{
2112 unsigned seen_dot_dot
;
2113 unsigned seen_other
;
2116 static int ocfs2_empty_dir_filldir(void *priv
, const char *name
, int name_len
,
2117 loff_t pos
, u64 ino
, unsigned type
)
2119 struct ocfs2_empty_dir_priv
*p
= priv
;
2122 * Check the positions of "." and ".." records to be sure
2123 * they're in the correct place.
2125 * Indexed directories don't need to proceed past the first
2126 * two entries, so we end the scan after seeing '..'. Despite
2127 * that, we allow the scan to proceed In the event that we
2128 * have a corrupted indexed directory (no dot or dot dot
2129 * entries). This allows us to double check for existing
2130 * entries which might not have been found in the index.
2132 if (name_len
== 1 && !strncmp(".", name
, 1) && pos
== 0) {
2137 if (name_len
== 2 && !strncmp("..", name
, 2) &&
2138 pos
== OCFS2_DIR_REC_LEN(1)) {
2139 p
->seen_dot_dot
= 1;
2141 if (p
->dx_dir
&& p
->seen_dot
)
2151 static int ocfs2_empty_dir_dx(struct inode
*inode
,
2152 struct ocfs2_empty_dir_priv
*priv
)
2155 struct buffer_head
*di_bh
= NULL
;
2156 struct buffer_head
*dx_root_bh
= NULL
;
2157 struct ocfs2_dinode
*di
;
2158 struct ocfs2_dx_root_block
*dx_root
;
2162 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
2167 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2169 ret
= ocfs2_read_dx_root(inode
, di
, &dx_root_bh
);
2174 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
2176 if (le32_to_cpu(dx_root
->dr_num_entries
) != 2)
2177 priv
->seen_other
= 1;
2185 int ocfs2_empty_dir(struct inode
*inode
)
2189 struct ocfs2_empty_dir_priv priv
;
2191 memset(&priv
, 0, sizeof(priv
));
2193 if (ocfs2_dir_indexed(inode
)) {
2194 ret
= ocfs2_empty_dir_dx(inode
, &priv
);
2198 * We still run ocfs2_dir_foreach to get the checks
2203 ret
= ocfs2_dir_foreach(inode
, &start
, &priv
, ocfs2_empty_dir_filldir
);
2207 if (!priv
.seen_dot
|| !priv
.seen_dot_dot
) {
2208 mlog(ML_ERROR
, "bad directory (dir #%llu) - no `.' or `..'\n",
2209 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
2213 return !priv
.seen_other
;
2217 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2218 * "..", which might be used during creation of a directory with a trailing
2219 * header. It is otherwise safe to ignore the return code.
2221 static struct ocfs2_dir_entry
*ocfs2_fill_initial_dirents(struct inode
*inode
,
2222 struct inode
*parent
,
2226 struct ocfs2_dir_entry
*de
= (struct ocfs2_dir_entry
*)start
;
2228 de
->inode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
2231 cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
2232 strcpy(de
->name
, ".");
2233 ocfs2_set_de_type(de
, S_IFDIR
);
2235 de
= (struct ocfs2_dir_entry
*) ((char *)de
+ le16_to_cpu(de
->rec_len
));
2236 de
->inode
= cpu_to_le64(OCFS2_I(parent
)->ip_blkno
);
2237 de
->rec_len
= cpu_to_le16(size
- OCFS2_DIR_REC_LEN(1));
2239 strcpy(de
->name
, "..");
2240 ocfs2_set_de_type(de
, S_IFDIR
);
2246 * This works together with code in ocfs2_mknod_locked() which sets
2247 * the inline-data flag and initializes the inline-data section.
2249 static int ocfs2_fill_new_dir_id(struct ocfs2_super
*osb
,
2251 struct inode
*parent
,
2252 struct inode
*inode
,
2253 struct buffer_head
*di_bh
)
2256 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2257 struct ocfs2_inline_data
*data
= &di
->id2
.i_data
;
2258 unsigned int size
= le16_to_cpu(data
->id_count
);
2260 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
2261 OCFS2_JOURNAL_ACCESS_WRITE
);
2267 ocfs2_fill_initial_dirents(inode
, parent
, data
->id_data
, size
);
2268 ocfs2_journal_dirty(handle
, di_bh
);
2270 i_size_write(inode
, size
);
2272 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
2274 ret
= ocfs2_mark_inode_dirty(handle
, inode
, di_bh
);
2282 static int ocfs2_fill_new_dir_el(struct ocfs2_super
*osb
,
2284 struct inode
*parent
,
2285 struct inode
*inode
,
2286 struct buffer_head
*fe_bh
,
2287 struct ocfs2_alloc_context
*data_ac
,
2288 struct buffer_head
**ret_new_bh
)
2291 unsigned int size
= osb
->sb
->s_blocksize
;
2292 struct buffer_head
*new_bh
= NULL
;
2293 struct ocfs2_dir_entry
*de
;
2297 if (ocfs2_new_dir_wants_trailer(inode
))
2298 size
= ocfs2_dir_trailer_blk_off(parent
->i_sb
);
2300 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, inode
, fe_bh
,
2301 data_ac
, NULL
, &new_bh
);
2307 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2309 status
= ocfs2_journal_access_db(handle
, INODE_CACHE(inode
), new_bh
,
2310 OCFS2_JOURNAL_ACCESS_CREATE
);
2315 memset(new_bh
->b_data
, 0, osb
->sb
->s_blocksize
);
2317 de
= ocfs2_fill_initial_dirents(inode
, parent
, new_bh
->b_data
, size
);
2318 if (ocfs2_new_dir_wants_trailer(inode
)) {
2319 int size
= le16_to_cpu(de
->rec_len
);
2322 * Figure out the size of the hole left over after
2323 * insertion of '.' and '..'. The trailer wants this
2326 size
-= OCFS2_DIR_REC_LEN(2);
2327 size
-= sizeof(struct ocfs2_dir_block_trailer
);
2329 ocfs2_init_dir_trailer(inode
, new_bh
, size
);
2332 ocfs2_journal_dirty(handle
, new_bh
);
2334 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
2336 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
2337 status
= ocfs2_mark_inode_dirty(handle
, inode
, fe_bh
);
2345 *ret_new_bh
= new_bh
;
2355 static int ocfs2_dx_dir_attach_index(struct ocfs2_super
*osb
,
2356 handle_t
*handle
, struct inode
*dir
,
2357 struct buffer_head
*di_bh
,
2358 struct buffer_head
*dirdata_bh
,
2359 struct ocfs2_alloc_context
*meta_ac
,
2360 int dx_inline
, u32 num_entries
,
2361 struct buffer_head
**ret_dx_root_bh
)
2364 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
2365 u16 dr_suballoc_bit
;
2366 u64 suballoc_loc
, dr_blkno
;
2367 unsigned int num_bits
;
2368 struct buffer_head
*dx_root_bh
= NULL
;
2369 struct ocfs2_dx_root_block
*dx_root
;
2370 struct ocfs2_dir_block_trailer
*trailer
=
2371 ocfs2_trailer_from_bh(dirdata_bh
, dir
->i_sb
);
2373 ret
= ocfs2_claim_metadata(handle
, meta_ac
, 1, &suballoc_loc
,
2374 &dr_suballoc_bit
, &num_bits
, &dr_blkno
);
2380 mlog(0, "Dir %llu, attach new index block: %llu\n",
2381 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
2382 (unsigned long long)dr_blkno
);
2384 dx_root_bh
= sb_getblk(osb
->sb
, dr_blkno
);
2385 if (dx_root_bh
== NULL
) {
2389 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir
), dx_root_bh
);
2391 ret
= ocfs2_journal_access_dr(handle
, INODE_CACHE(dir
), dx_root_bh
,
2392 OCFS2_JOURNAL_ACCESS_CREATE
);
2398 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
2399 memset(dx_root
, 0, osb
->sb
->s_blocksize
);
2400 strcpy(dx_root
->dr_signature
, OCFS2_DX_ROOT_SIGNATURE
);
2401 dx_root
->dr_suballoc_slot
= cpu_to_le16(meta_ac
->ac_alloc_slot
);
2402 dx_root
->dr_suballoc_loc
= cpu_to_le64(suballoc_loc
);
2403 dx_root
->dr_suballoc_bit
= cpu_to_le16(dr_suballoc_bit
);
2404 dx_root
->dr_fs_generation
= cpu_to_le32(osb
->fs_generation
);
2405 dx_root
->dr_blkno
= cpu_to_le64(dr_blkno
);
2406 dx_root
->dr_dir_blkno
= cpu_to_le64(OCFS2_I(dir
)->ip_blkno
);
2407 dx_root
->dr_num_entries
= cpu_to_le32(num_entries
);
2408 if (le16_to_cpu(trailer
->db_free_rec_len
))
2409 dx_root
->dr_free_blk
= cpu_to_le64(dirdata_bh
->b_blocknr
);
2411 dx_root
->dr_free_blk
= cpu_to_le64(0);
2414 dx_root
->dr_flags
|= OCFS2_DX_FLAG_INLINE
;
2415 dx_root
->dr_entries
.de_count
=
2416 cpu_to_le16(ocfs2_dx_entries_per_root(osb
->sb
));
2418 dx_root
->dr_list
.l_count
=
2419 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb
->sb
));
2421 ocfs2_journal_dirty(handle
, dx_root_bh
);
2423 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(dir
), di_bh
,
2424 OCFS2_JOURNAL_ACCESS_CREATE
);
2430 di
->i_dx_root
= cpu_to_le64(dr_blkno
);
2432 OCFS2_I(dir
)->ip_dyn_features
|= OCFS2_INDEXED_DIR_FL
;
2433 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(dir
)->ip_dyn_features
);
2435 ocfs2_journal_dirty(handle
, di_bh
);
2437 *ret_dx_root_bh
= dx_root_bh
;
2445 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super
*osb
,
2446 handle_t
*handle
, struct inode
*dir
,
2447 struct buffer_head
**dx_leaves
,
2448 int num_dx_leaves
, u64 start_blk
)
2451 struct ocfs2_dx_leaf
*dx_leaf
;
2452 struct buffer_head
*bh
;
2454 for (i
= 0; i
< num_dx_leaves
; i
++) {
2455 bh
= sb_getblk(osb
->sb
, start_blk
+ i
);
2462 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir
), bh
);
2464 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
), bh
,
2465 OCFS2_JOURNAL_ACCESS_CREATE
);
2471 dx_leaf
= (struct ocfs2_dx_leaf
*) bh
->b_data
;
2473 memset(dx_leaf
, 0, osb
->sb
->s_blocksize
);
2474 strcpy(dx_leaf
->dl_signature
, OCFS2_DX_LEAF_SIGNATURE
);
2475 dx_leaf
->dl_fs_generation
= cpu_to_le32(osb
->fs_generation
);
2476 dx_leaf
->dl_blkno
= cpu_to_le64(bh
->b_blocknr
);
2477 dx_leaf
->dl_list
.de_count
=
2478 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb
->sb
));
2481 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2482 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
2483 (unsigned long long)bh
->b_blocknr
,
2484 le16_to_cpu(dx_leaf
->dl_list
.de_count
));
2486 ocfs2_journal_dirty(handle
, bh
);
2495 * Allocates and formats a new cluster for use in an indexed dir
2496 * leaf. This version will not do the extent insert, so that it can be
2497 * used by operations which need careful ordering.
2499 static int __ocfs2_dx_dir_new_cluster(struct inode
*dir
,
2500 u32 cpos
, handle_t
*handle
,
2501 struct ocfs2_alloc_context
*data_ac
,
2502 struct buffer_head
**dx_leaves
,
2503 int num_dx_leaves
, u64
*ret_phys_blkno
)
2508 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
2510 ret
= __ocfs2_claim_clusters(handle
, data_ac
, 1, 1, &phys
, &num
);
2517 * Format the new cluster first. That way, we're inserting
2520 phys_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, phys
);
2521 ret
= ocfs2_dx_dir_format_cluster(osb
, handle
, dir
, dx_leaves
,
2522 num_dx_leaves
, phys_blkno
);
2528 *ret_phys_blkno
= phys_blkno
;
2533 static int ocfs2_dx_dir_new_cluster(struct inode
*dir
,
2534 struct ocfs2_extent_tree
*et
,
2535 u32 cpos
, handle_t
*handle
,
2536 struct ocfs2_alloc_context
*data_ac
,
2537 struct ocfs2_alloc_context
*meta_ac
,
2538 struct buffer_head
**dx_leaves
,
2544 ret
= __ocfs2_dx_dir_new_cluster(dir
, cpos
, handle
, data_ac
, dx_leaves
,
2545 num_dx_leaves
, &phys_blkno
);
2551 ret
= ocfs2_insert_extent(handle
, et
, cpos
, phys_blkno
, 1, 0,
2559 static struct buffer_head
**ocfs2_dx_dir_kmalloc_leaves(struct super_block
*sb
,
2560 int *ret_num_leaves
)
2562 int num_dx_leaves
= ocfs2_clusters_to_blocks(sb
, 1);
2563 struct buffer_head
**dx_leaves
;
2565 dx_leaves
= kcalloc(num_dx_leaves
, sizeof(struct buffer_head
*),
2567 if (dx_leaves
&& ret_num_leaves
)
2568 *ret_num_leaves
= num_dx_leaves
;
2573 static int ocfs2_fill_new_dir_dx(struct ocfs2_super
*osb
,
2575 struct inode
*parent
,
2576 struct inode
*inode
,
2577 struct buffer_head
*di_bh
,
2578 struct ocfs2_alloc_context
*data_ac
,
2579 struct ocfs2_alloc_context
*meta_ac
)
2582 struct buffer_head
*leaf_bh
= NULL
;
2583 struct buffer_head
*dx_root_bh
= NULL
;
2584 struct ocfs2_dx_hinfo hinfo
;
2585 struct ocfs2_dx_root_block
*dx_root
;
2586 struct ocfs2_dx_entry_list
*entry_list
;
2589 * Our strategy is to create the directory as though it were
2590 * unindexed, then add the index block. This works with very
2591 * little complication since the state of a new directory is a
2592 * very well known quantity.
2594 * Essentially, we have two dirents ("." and ".."), in the 1st
2595 * block which need indexing. These are easily inserted into
2599 ret
= ocfs2_fill_new_dir_el(osb
, handle
, parent
, inode
, di_bh
,
2606 ret
= ocfs2_dx_dir_attach_index(osb
, handle
, inode
, di_bh
, leaf_bh
,
2607 meta_ac
, 1, 2, &dx_root_bh
);
2612 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
2613 entry_list
= &dx_root
->dr_entries
;
2615 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2616 ocfs2_dx_dir_name_hash(inode
, ".", 1, &hinfo
);
2617 ocfs2_dx_entry_list_insert(entry_list
, &hinfo
, leaf_bh
->b_blocknr
);
2619 ocfs2_dx_dir_name_hash(inode
, "..", 2, &hinfo
);
2620 ocfs2_dx_entry_list_insert(entry_list
, &hinfo
, leaf_bh
->b_blocknr
);
2628 int ocfs2_fill_new_dir(struct ocfs2_super
*osb
,
2630 struct inode
*parent
,
2631 struct inode
*inode
,
2632 struct buffer_head
*fe_bh
,
2633 struct ocfs2_alloc_context
*data_ac
,
2634 struct ocfs2_alloc_context
*meta_ac
)
2637 BUG_ON(!ocfs2_supports_inline_data(osb
) && data_ac
== NULL
);
2639 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
2640 return ocfs2_fill_new_dir_id(osb
, handle
, parent
, inode
, fe_bh
);
2642 if (ocfs2_supports_indexed_dirs(osb
))
2643 return ocfs2_fill_new_dir_dx(osb
, handle
, parent
, inode
, fe_bh
,
2646 return ocfs2_fill_new_dir_el(osb
, handle
, parent
, inode
, fe_bh
,
2650 static int ocfs2_dx_dir_index_block(struct inode
*dir
,
2652 struct buffer_head
**dx_leaves
,
2654 u32
*num_dx_entries
,
2655 struct buffer_head
*dirent_bh
)
2657 int ret
= 0, namelen
, i
;
2658 char *de_buf
, *limit
;
2659 struct ocfs2_dir_entry
*de
;
2660 struct buffer_head
*dx_leaf_bh
;
2661 struct ocfs2_dx_hinfo hinfo
;
2662 u64 dirent_blk
= dirent_bh
->b_blocknr
;
2664 de_buf
= dirent_bh
->b_data
;
2665 limit
= de_buf
+ dir
->i_sb
->s_blocksize
;
2667 while (de_buf
< limit
) {
2668 de
= (struct ocfs2_dir_entry
*)de_buf
;
2670 namelen
= de
->name_len
;
2671 if (!namelen
|| !de
->inode
)
2674 ocfs2_dx_dir_name_hash(dir
, de
->name
, namelen
, &hinfo
);
2676 i
= ocfs2_dx_dir_hash_idx(OCFS2_SB(dir
->i_sb
), &hinfo
);
2677 dx_leaf_bh
= dx_leaves
[i
];
2679 ret
= __ocfs2_dx_dir_leaf_insert(dir
, handle
, &hinfo
,
2680 dirent_blk
, dx_leaf_bh
);
2686 *num_dx_entries
= *num_dx_entries
+ 1;
2689 de_buf
+= le16_to_cpu(de
->rec_len
);
2696 static void ocfs2_dx_dir_index_root_block(struct inode
*dir
,
2697 struct buffer_head
*dx_root_bh
,
2698 struct buffer_head
*dirent_bh
)
2700 char *de_buf
, *limit
;
2701 struct ocfs2_dx_root_block
*dx_root
;
2702 struct ocfs2_dir_entry
*de
;
2703 struct ocfs2_dx_hinfo hinfo
;
2704 u64 dirent_blk
= dirent_bh
->b_blocknr
;
2706 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
2708 de_buf
= dirent_bh
->b_data
;
2709 limit
= de_buf
+ dir
->i_sb
->s_blocksize
;
2711 while (de_buf
< limit
) {
2712 de
= (struct ocfs2_dir_entry
*)de_buf
;
2714 if (!de
->name_len
|| !de
->inode
)
2717 ocfs2_dx_dir_name_hash(dir
, de
->name
, de
->name_len
, &hinfo
);
2720 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2721 (unsigned long long)dir
->i_ino
, hinfo
.major_hash
,
2723 le16_to_cpu(dx_root
->dr_entries
.de_num_used
),
2724 de
->name_len
, de
->name
);
2726 ocfs2_dx_entry_list_insert(&dx_root
->dr_entries
, &hinfo
,
2729 le32_add_cpu(&dx_root
->dr_num_entries
, 1);
2731 de_buf
+= le16_to_cpu(de
->rec_len
);
2736 * Count the number of inline directory entries in di_bh and compare
2737 * them against the number of entries we can hold in an inline dx root
2740 static int ocfs2_new_dx_should_be_inline(struct inode
*dir
,
2741 struct buffer_head
*di_bh
)
2743 int dirent_count
= 0;
2744 char *de_buf
, *limit
;
2745 struct ocfs2_dir_entry
*de
;
2746 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2748 de_buf
= di
->id2
.i_data
.id_data
;
2749 limit
= de_buf
+ i_size_read(dir
);
2751 while (de_buf
< limit
) {
2752 de
= (struct ocfs2_dir_entry
*)de_buf
;
2754 if (de
->name_len
&& de
->inode
)
2757 de_buf
+= le16_to_cpu(de
->rec_len
);
2760 /* We are careful to leave room for one extra record. */
2761 return dirent_count
< ocfs2_dx_entries_per_root(dir
->i_sb
);
2765 * Expand rec_len of the rightmost dirent in a directory block so that it
2766 * contains the end of our valid space for dirents. We do this during
2767 * expansion from an inline directory to one with extents. The first dir block
2768 * in that case is taken from the inline data portion of the inode block.
2770 * This will also return the largest amount of contiguous space for a dirent
2771 * in the block. That value is *not* necessarily the last dirent, even after
2772 * expansion. The directory indexing code wants this value for free space
2773 * accounting. We do this here since we're already walking the entire dir
2776 * We add the dir trailer if this filesystem wants it.
2778 static unsigned int ocfs2_expand_last_dirent(char *start
, unsigned int old_size
,
2781 struct super_block
*sb
= dir
->i_sb
;
2782 struct ocfs2_dir_entry
*de
;
2783 struct ocfs2_dir_entry
*prev_de
;
2784 char *de_buf
, *limit
;
2785 unsigned int new_size
= sb
->s_blocksize
;
2786 unsigned int bytes
, this_hole
;
2787 unsigned int largest_hole
= 0;
2789 if (ocfs2_new_dir_wants_trailer(dir
))
2790 new_size
= ocfs2_dir_trailer_blk_off(sb
);
2792 bytes
= new_size
- old_size
;
2794 limit
= start
+ old_size
;
2796 de
= (struct ocfs2_dir_entry
*)de_buf
;
2798 this_hole
= ocfs2_figure_dirent_hole(de
);
2799 if (this_hole
> largest_hole
)
2800 largest_hole
= this_hole
;
2803 de_buf
+= le16_to_cpu(de
->rec_len
);
2804 de
= (struct ocfs2_dir_entry
*)de_buf
;
2805 } while (de_buf
< limit
);
2807 le16_add_cpu(&prev_de
->rec_len
, bytes
);
2809 /* We need to double check this after modification of the final
2811 this_hole
= ocfs2_figure_dirent_hole(prev_de
);
2812 if (this_hole
> largest_hole
)
2813 largest_hole
= this_hole
;
2815 if (largest_hole
>= OCFS2_DIR_MIN_REC_LEN
)
2816 return largest_hole
;
2821 * We allocate enough clusters to fulfill "blocks_wanted", but set
2822 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2823 * rest automatically for us.
2825 * *first_block_bh is a pointer to the 1st data block allocated to the
2828 static int ocfs2_expand_inline_dir(struct inode
*dir
, struct buffer_head
*di_bh
,
2829 unsigned int blocks_wanted
,
2830 struct ocfs2_dir_lookup_result
*lookup
,
2831 struct buffer_head
**first_block_bh
)
2833 u32 alloc
, dx_alloc
, bit_off
, len
, num_dx_entries
= 0;
2834 struct super_block
*sb
= dir
->i_sb
;
2835 int ret
, i
, num_dx_leaves
= 0, dx_inline
= 0,
2836 credits
= ocfs2_inline_to_extents_credits(sb
);
2837 u64 dx_insert_blkno
, blkno
,
2838 bytes
= blocks_wanted
<< sb
->s_blocksize_bits
;
2839 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
2840 struct ocfs2_inode_info
*oi
= OCFS2_I(dir
);
2841 struct ocfs2_alloc_context
*data_ac
;
2842 struct ocfs2_alloc_context
*meta_ac
= NULL
;
2843 struct buffer_head
*dirdata_bh
= NULL
;
2844 struct buffer_head
*dx_root_bh
= NULL
;
2845 struct buffer_head
**dx_leaves
= NULL
;
2846 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2848 struct ocfs2_extent_tree et
;
2849 struct ocfs2_extent_tree dx_et
;
2850 int did_quota
= 0, bytes_allocated
= 0;
2852 ocfs2_init_dinode_extent_tree(&et
, INODE_CACHE(dir
), di_bh
);
2854 alloc
= ocfs2_clusters_for_bytes(sb
, bytes
);
2857 down_write(&oi
->ip_alloc_sem
);
2859 if (ocfs2_supports_indexed_dirs(osb
)) {
2860 credits
+= ocfs2_add_dir_index_credits(sb
);
2862 dx_inline
= ocfs2_new_dx_should_be_inline(dir
, di_bh
);
2864 /* Add one more cluster for an index leaf */
2866 dx_leaves
= ocfs2_dx_dir_kmalloc_leaves(sb
,
2875 /* This gets us the dx_root */
2876 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
2884 * We should never need more than 2 clusters for the unindexed
2885 * tree - maximum dirent size is far less than one block. In
2886 * fact, the only time we'd need more than one cluster is if
2887 * blocksize == clustersize and the dirent won't fit in the
2888 * extra space that the expansion to a single block gives. As
2889 * of today, that only happens on 4k/4k file systems.
2893 ret
= ocfs2_reserve_clusters(osb
, alloc
+ dx_alloc
, &data_ac
);
2900 * Prepare for worst case allocation scenario of two separate
2901 * extents in the unindexed tree.
2904 credits
+= OCFS2_SUBALLOC_ALLOC
;
2906 handle
= ocfs2_start_trans(osb
, credits
);
2907 if (IS_ERR(handle
)) {
2908 ret
= PTR_ERR(handle
);
2913 ret
= dquot_alloc_space_nodirty(dir
,
2914 ocfs2_clusters_to_bytes(osb
->sb
, alloc
+ dx_alloc
));
2919 if (ocfs2_supports_indexed_dirs(osb
) && !dx_inline
) {
2921 * Allocate our index cluster first, to maximize the
2922 * possibility that unindexed leaves grow
2925 ret
= __ocfs2_dx_dir_new_cluster(dir
, 0, handle
, data_ac
,
2926 dx_leaves
, num_dx_leaves
,
2932 bytes_allocated
+= ocfs2_clusters_to_bytes(dir
->i_sb
, 1);
2936 * Try to claim as many clusters as the bitmap can give though
2937 * if we only get one now, that's enough to continue. The rest
2938 * will be claimed after the conversion to extents.
2940 if (ocfs2_dir_resv_allowed(osb
))
2941 data_ac
->ac_resv
= &oi
->ip_la_data_resv
;
2942 ret
= ocfs2_claim_clusters(handle
, data_ac
, 1, &bit_off
, &len
);
2947 bytes_allocated
+= ocfs2_clusters_to_bytes(dir
->i_sb
, 1);
2950 * Operations are carefully ordered so that we set up the new
2951 * data block first. The conversion from inline data to
2954 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
2955 dirdata_bh
= sb_getblk(sb
, blkno
);
2962 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir
), dirdata_bh
);
2964 ret
= ocfs2_journal_access_db(handle
, INODE_CACHE(dir
), dirdata_bh
,
2965 OCFS2_JOURNAL_ACCESS_CREATE
);
2971 memcpy(dirdata_bh
->b_data
, di
->id2
.i_data
.id_data
, i_size_read(dir
));
2972 memset(dirdata_bh
->b_data
+ i_size_read(dir
), 0,
2973 sb
->s_blocksize
- i_size_read(dir
));
2974 i
= ocfs2_expand_last_dirent(dirdata_bh
->b_data
, i_size_read(dir
), dir
);
2975 if (ocfs2_new_dir_wants_trailer(dir
)) {
2977 * Prepare the dir trailer up front. It will otherwise look
2978 * like a valid dirent. Even if inserting the index fails
2979 * (unlikely), then all we'll have done is given first dir
2980 * block a small amount of fragmentation.
2982 ocfs2_init_dir_trailer(dir
, dirdata_bh
, i
);
2985 ocfs2_journal_dirty(handle
, dirdata_bh
);
2987 if (ocfs2_supports_indexed_dirs(osb
) && !dx_inline
) {
2989 * Dx dirs with an external cluster need to do this up
2990 * front. Inline dx root's get handled later, after
2991 * we've allocated our root block. We get passed back
2992 * a total number of items so that dr_num_entries can
2993 * be correctly set once the dx_root has been
2996 ret
= ocfs2_dx_dir_index_block(dir
, handle
, dx_leaves
,
2997 num_dx_leaves
, &num_dx_entries
,
3006 * Set extent, i_size, etc on the directory. After this, the
3007 * inode should contain the same exact dirents as before and
3008 * be fully accessible from system calls.
3010 * We let the later dirent insert modify c/mtime - to the user
3011 * the data hasn't changed.
3013 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(dir
), di_bh
,
3014 OCFS2_JOURNAL_ACCESS_CREATE
);
3020 spin_lock(&oi
->ip_lock
);
3021 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
3022 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
3023 spin_unlock(&oi
->ip_lock
);
3025 ocfs2_dinode_new_extent_list(dir
, di
);
3027 i_size_write(dir
, sb
->s_blocksize
);
3028 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3030 di
->i_size
= cpu_to_le64(sb
->s_blocksize
);
3031 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(dir
->i_ctime
.tv_sec
);
3032 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(dir
->i_ctime
.tv_nsec
);
3035 * This should never fail as our extent list is empty and all
3036 * related blocks have been journaled already.
3038 ret
= ocfs2_insert_extent(handle
, &et
, 0, blkno
, len
,
3046 * Set i_blocks after the extent insert for the most up to
3047 * date ip_clusters value.
3049 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
3051 ocfs2_journal_dirty(handle
, di_bh
);
3053 if (ocfs2_supports_indexed_dirs(osb
)) {
3054 ret
= ocfs2_dx_dir_attach_index(osb
, handle
, dir
, di_bh
,
3055 dirdata_bh
, meta_ac
, dx_inline
,
3056 num_dx_entries
, &dx_root_bh
);
3063 ocfs2_dx_dir_index_root_block(dir
, dx_root_bh
,
3066 ocfs2_init_dx_root_extent_tree(&dx_et
,
3069 ret
= ocfs2_insert_extent(handle
, &dx_et
, 0,
3070 dx_insert_blkno
, 1, 0, NULL
);
3077 * We asked for two clusters, but only got one in the 1st
3078 * pass. Claim the 2nd cluster as a separate extent.
3081 ret
= ocfs2_claim_clusters(handle
, data_ac
, 1, &bit_off
,
3087 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
3089 ret
= ocfs2_insert_extent(handle
, &et
, 1,
3090 blkno
, len
, 0, NULL
);
3095 bytes_allocated
+= ocfs2_clusters_to_bytes(dir
->i_sb
, 1);
3098 *first_block_bh
= dirdata_bh
;
3100 if (ocfs2_supports_indexed_dirs(osb
)) {
3105 * We need to return the correct block within the
3106 * cluster which should hold our entry.
3108 off
= ocfs2_dx_dir_hash_idx(OCFS2_SB(dir
->i_sb
),
3110 get_bh(dx_leaves
[off
]);
3111 lookup
->dl_dx_leaf_bh
= dx_leaves
[off
];
3113 lookup
->dl_dx_root_bh
= dx_root_bh
;
3118 if (ret
< 0 && did_quota
)
3119 dquot_free_space_nodirty(dir
, bytes_allocated
);
3121 ocfs2_commit_trans(osb
, handle
);
3124 up_write(&oi
->ip_alloc_sem
);
3126 ocfs2_free_alloc_context(data_ac
);
3128 ocfs2_free_alloc_context(meta_ac
);
3131 for (i
= 0; i
< num_dx_leaves
; i
++)
3132 brelse(dx_leaves
[i
]);
3142 /* returns a bh of the 1st new block in the allocation. */
3143 static int ocfs2_do_extend_dir(struct super_block
*sb
,
3146 struct buffer_head
*parent_fe_bh
,
3147 struct ocfs2_alloc_context
*data_ac
,
3148 struct ocfs2_alloc_context
*meta_ac
,
3149 struct buffer_head
**new_bh
)
3152 int extend
, did_quota
= 0;
3153 u64 p_blkno
, v_blkno
;
3155 spin_lock(&OCFS2_I(dir
)->ip_lock
);
3156 extend
= (i_size_read(dir
) == ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
));
3157 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
3160 u32 offset
= OCFS2_I(dir
)->ip_clusters
;
3162 status
= dquot_alloc_space_nodirty(dir
,
3163 ocfs2_clusters_to_bytes(sb
, 1));
3168 status
= ocfs2_add_inode_data(OCFS2_SB(sb
), dir
, &offset
,
3169 1, 0, parent_fe_bh
, handle
,
3170 data_ac
, meta_ac
, NULL
);
3171 BUG_ON(status
== -EAGAIN
);
3178 v_blkno
= ocfs2_blocks_for_bytes(sb
, i_size_read(dir
));
3179 status
= ocfs2_extent_map_get_blocks(dir
, v_blkno
, &p_blkno
, NULL
, NULL
);
3185 *new_bh
= sb_getblk(sb
, p_blkno
);
3193 if (did_quota
&& status
< 0)
3194 dquot_free_space_nodirty(dir
, ocfs2_clusters_to_bytes(sb
, 1));
3200 * Assumes you already have a cluster lock on the directory.
3202 * 'blocks_wanted' is only used if we have an inline directory which
3203 * is to be turned into an extent based one. The size of the dirent to
3204 * insert might be larger than the space gained by growing to just one
3205 * block, so we may have to grow the inode by two blocks in that case.
3207 * If the directory is already indexed, dx_root_bh must be provided.
3209 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
3211 struct buffer_head
*parent_fe_bh
,
3212 unsigned int blocks_wanted
,
3213 struct ocfs2_dir_lookup_result
*lookup
,
3214 struct buffer_head
**new_de_bh
)
3217 int credits
, num_free_extents
, drop_alloc_sem
= 0;
3219 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
3220 struct ocfs2_extent_list
*el
= &fe
->id2
.i_list
;
3221 struct ocfs2_alloc_context
*data_ac
= NULL
;
3222 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3223 handle_t
*handle
= NULL
;
3224 struct buffer_head
*new_bh
= NULL
;
3225 struct ocfs2_dir_entry
* de
;
3226 struct super_block
*sb
= osb
->sb
;
3227 struct ocfs2_extent_tree et
;
3228 struct buffer_head
*dx_root_bh
= lookup
->dl_dx_root_bh
;
3232 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
3234 * This would be a code error as an inline directory should
3235 * never have an index root.
3239 status
= ocfs2_expand_inline_dir(dir
, parent_fe_bh
,
3240 blocks_wanted
, lookup
,
3247 /* Expansion from inline to an indexed directory will
3248 * have given us this. */
3249 dx_root_bh
= lookup
->dl_dx_root_bh
;
3251 if (blocks_wanted
== 1) {
3253 * If the new dirent will fit inside the space
3254 * created by pushing out to one block, then
3255 * we can complete the operation
3256 * here. Otherwise we have to expand i_size
3257 * and format the 2nd block below.
3259 BUG_ON(new_bh
== NULL
);
3264 * Get rid of 'new_bh' - we want to format the 2nd
3265 * data block and return that instead.
3270 down_write(&OCFS2_I(dir
)->ip_alloc_sem
);
3272 dir_i_size
= i_size_read(dir
);
3273 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
3277 down_write(&OCFS2_I(dir
)->ip_alloc_sem
);
3279 dir_i_size
= i_size_read(dir
);
3280 mlog(0, "extending dir %llu (i_size = %lld)\n",
3281 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, dir_i_size
);
3283 /* dir->i_size is always block aligned. */
3284 spin_lock(&OCFS2_I(dir
)->ip_lock
);
3285 if (dir_i_size
== ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
)) {
3286 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
3287 ocfs2_init_dinode_extent_tree(&et
, INODE_CACHE(dir
),
3289 num_free_extents
= ocfs2_num_free_extents(osb
, &et
);
3290 if (num_free_extents
< 0) {
3291 status
= num_free_extents
;
3296 if (!num_free_extents
) {
3297 status
= ocfs2_reserve_new_metadata(osb
, el
, &meta_ac
);
3299 if (status
!= -ENOSPC
)
3305 status
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
3307 if (status
!= -ENOSPC
)
3312 if (ocfs2_dir_resv_allowed(osb
))
3313 data_ac
->ac_resv
= &OCFS2_I(dir
)->ip_la_data_resv
;
3315 credits
= ocfs2_calc_extend_credits(sb
, el
, 1);
3317 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
3318 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
3322 if (ocfs2_dir_indexed(dir
))
3323 credits
++; /* For attaching the new dirent block to the
3326 handle
= ocfs2_start_trans(osb
, credits
);
3327 if (IS_ERR(handle
)) {
3328 status
= PTR_ERR(handle
);
3334 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, dir
, parent_fe_bh
,
3335 data_ac
, meta_ac
, &new_bh
);
3341 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir
), new_bh
);
3343 status
= ocfs2_journal_access_db(handle
, INODE_CACHE(dir
), new_bh
,
3344 OCFS2_JOURNAL_ACCESS_CREATE
);
3349 memset(new_bh
->b_data
, 0, sb
->s_blocksize
);
3351 de
= (struct ocfs2_dir_entry
*) new_bh
->b_data
;
3353 if (ocfs2_supports_dir_trailer(dir
)) {
3354 de
->rec_len
= cpu_to_le16(ocfs2_dir_trailer_blk_off(sb
));
3356 ocfs2_init_dir_trailer(dir
, new_bh
, le16_to_cpu(de
->rec_len
));
3358 if (ocfs2_dir_indexed(dir
)) {
3359 status
= ocfs2_dx_dir_link_trailer(dir
, handle
,
3360 dx_root_bh
, new_bh
);
3367 de
->rec_len
= cpu_to_le16(sb
->s_blocksize
);
3369 ocfs2_journal_dirty(handle
, new_bh
);
3371 dir_i_size
+= dir
->i_sb
->s_blocksize
;
3372 i_size_write(dir
, dir_i_size
);
3373 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
3374 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
3381 *new_de_bh
= new_bh
;
3385 ocfs2_commit_trans(osb
, handle
);
3387 up_write(&OCFS2_I(dir
)->ip_alloc_sem
);
3390 ocfs2_free_alloc_context(data_ac
);
3392 ocfs2_free_alloc_context(meta_ac
);
3400 static int ocfs2_find_dir_space_id(struct inode
*dir
, struct buffer_head
*di_bh
,
3401 const char *name
, int namelen
,
3402 struct buffer_head
**ret_de_bh
,
3403 unsigned int *blocks_wanted
)
3406 struct super_block
*sb
= dir
->i_sb
;
3407 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3408 struct ocfs2_dir_entry
*de
, *last_de
= NULL
;
3409 char *de_buf
, *limit
;
3410 unsigned long offset
= 0;
3411 unsigned int rec_len
, new_rec_len
, free_space
= dir
->i_sb
->s_blocksize
;
3414 * This calculates how many free bytes we'd have in block zero, should
3415 * this function force expansion to an extent tree.
3417 if (ocfs2_new_dir_wants_trailer(dir
))
3418 free_space
= ocfs2_dir_trailer_blk_off(sb
) - i_size_read(dir
);
3420 free_space
= dir
->i_sb
->s_blocksize
- i_size_read(dir
);
3422 de_buf
= di
->id2
.i_data
.id_data
;
3423 limit
= de_buf
+ i_size_read(dir
);
3424 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
3426 while (de_buf
< limit
) {
3427 de
= (struct ocfs2_dir_entry
*)de_buf
;
3429 if (!ocfs2_check_dir_entry(dir
, de
, di_bh
, offset
)) {
3433 if (ocfs2_match(namelen
, name
, de
)) {
3438 * No need to check for a trailing dirent record here as
3439 * they're not used for inline dirs.
3442 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
3443 /* Ok, we found a spot. Return this bh and let
3444 * the caller actually fill it in. */
3452 de_buf
+= le16_to_cpu(de
->rec_len
);
3453 offset
+= le16_to_cpu(de
->rec_len
);
3457 * We're going to require expansion of the directory - figure
3458 * out how many blocks we'll need so that a place for the
3459 * dirent can be found.
3462 new_rec_len
= le16_to_cpu(last_de
->rec_len
) + free_space
;
3463 if (new_rec_len
< (rec_len
+ OCFS2_DIR_REC_LEN(last_de
->name_len
)))
3471 static int ocfs2_find_dir_space_el(struct inode
*dir
, const char *name
,
3472 int namelen
, struct buffer_head
**ret_de_bh
)
3474 unsigned long offset
;
3475 struct buffer_head
*bh
= NULL
;
3476 unsigned short rec_len
;
3477 struct ocfs2_dir_entry
*de
;
3478 struct super_block
*sb
= dir
->i_sb
;
3480 int blocksize
= dir
->i_sb
->s_blocksize
;
3482 status
= ocfs2_read_dir_block(dir
, 0, &bh
, 0);
3488 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
3490 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
3492 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
3496 if (i_size_read(dir
) <= offset
) {
3498 * Caller will have to expand this
3504 status
= ocfs2_read_dir_block(dir
,
3505 offset
>> sb
->s_blocksize_bits
,
3511 /* move to next block */
3512 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
3514 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
3518 if (ocfs2_match(namelen
, name
, de
)) {
3523 if (ocfs2_skip_dir_trailer(dir
, de
, offset
% blocksize
,
3527 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
3528 /* Ok, we found a spot. Return this bh and let
3529 * the caller actually fill it in. */
3536 offset
+= le16_to_cpu(de
->rec_len
);
3537 de
= (struct ocfs2_dir_entry
*)((char *) de
+ le16_to_cpu(de
->rec_len
));
3548 static int dx_leaf_sort_cmp(const void *a
, const void *b
)
3550 const struct ocfs2_dx_entry
*entry1
= a
;
3551 const struct ocfs2_dx_entry
*entry2
= b
;
3552 u32 major_hash1
= le32_to_cpu(entry1
->dx_major_hash
);
3553 u32 major_hash2
= le32_to_cpu(entry2
->dx_major_hash
);
3554 u32 minor_hash1
= le32_to_cpu(entry1
->dx_minor_hash
);
3555 u32 minor_hash2
= le32_to_cpu(entry2
->dx_minor_hash
);
3557 if (major_hash1
> major_hash2
)
3559 if (major_hash1
< major_hash2
)
3563 * It is not strictly necessary to sort by minor
3565 if (minor_hash1
> minor_hash2
)
3567 if (minor_hash1
< minor_hash2
)
3572 static void dx_leaf_sort_swap(void *a
, void *b
, int size
)
3574 struct ocfs2_dx_entry
*entry1
= a
;
3575 struct ocfs2_dx_entry
*entry2
= b
;
3576 struct ocfs2_dx_entry tmp
;
3578 BUG_ON(size
!= sizeof(*entry1
));
3585 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf
*dx_leaf
)
3587 struct ocfs2_dx_entry_list
*dl_list
= &dx_leaf
->dl_list
;
3588 int i
, num
= le16_to_cpu(dl_list
->de_num_used
);
3590 for (i
= 0; i
< (num
- 1); i
++) {
3591 if (le32_to_cpu(dl_list
->de_entries
[i
].dx_major_hash
) !=
3592 le32_to_cpu(dl_list
->de_entries
[i
+ 1].dx_major_hash
))
3600 * Find the optimal value to split this leaf on. This expects the leaf
3601 * entries to be in sorted order.
3603 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3604 * the hash we want to insert.
3606 * This function is only concerned with the major hash - that which
3607 * determines which cluster an item belongs to.
3609 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf
*dx_leaf
,
3610 u32 leaf_cpos
, u32 insert_hash
,
3613 struct ocfs2_dx_entry_list
*dl_list
= &dx_leaf
->dl_list
;
3614 int i
, num_used
= le16_to_cpu(dl_list
->de_num_used
);
3618 * There's a couple rare, but nasty corner cases we have to
3619 * check for here. All of them involve a leaf where all value
3620 * have the same hash, which is what we look for first.
3622 * Most of the time, all of the above is false, and we simply
3623 * pick the median value for a split.
3625 allsame
= ocfs2_dx_leaf_same_major(dx_leaf
);
3627 u32 val
= le32_to_cpu(dl_list
->de_entries
[0].dx_major_hash
);
3629 if (val
== insert_hash
) {
3631 * No matter where we would choose to split,
3632 * the new entry would want to occupy the same
3633 * block as these. Since there's no space left
3634 * in their existing block, we know there
3635 * won't be space after the split.
3640 if (val
== leaf_cpos
) {
3642 * Because val is the same as leaf_cpos (which
3643 * is the smallest value this leaf can have),
3644 * yet is not equal to insert_hash, then we
3645 * know that insert_hash *must* be larger than
3646 * val (and leaf_cpos). At least cpos+1 in value.
3648 * We also know then, that there cannot be an
3649 * adjacent extent (otherwise we'd be looking
3650 * at it). Choosing this value gives us a
3651 * chance to get some contiguousness.
3653 *split_hash
= leaf_cpos
+ 1;
3657 if (val
> insert_hash
) {
3659 * val can not be the same as insert hash, and
3660 * also must be larger than leaf_cpos. Also,
3661 * we know that there can't be a leaf between
3662 * cpos and val, otherwise the entries with
3663 * hash 'val' would be there.
3669 *split_hash
= insert_hash
;
3674 * Since the records are sorted and the checks above
3675 * guaranteed that not all records in this block are the same,
3676 * we simple travel forward, from the median, and pick the 1st
3677 * record whose value is larger than leaf_cpos.
3679 for (i
= (num_used
/ 2); i
< num_used
; i
++)
3680 if (le32_to_cpu(dl_list
->de_entries
[i
].dx_major_hash
) >
3684 BUG_ON(i
== num_used
); /* Should be impossible */
3685 *split_hash
= le32_to_cpu(dl_list
->de_entries
[i
].dx_major_hash
);
3690 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3691 * larger than split_hash into new_dx_leaves. We use a temporary
3692 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3694 * Since the block offset inside a leaf (cluster) is a constant mask
3695 * of minor_hash, we can optimize - an item at block offset X within
3696 * the original cluster, will be at offset X within the new cluster.
3698 static void ocfs2_dx_dir_transfer_leaf(struct inode
*dir
, u32 split_hash
,
3700 struct ocfs2_dx_leaf
*tmp_dx_leaf
,
3701 struct buffer_head
**orig_dx_leaves
,
3702 struct buffer_head
**new_dx_leaves
,
3707 struct ocfs2_dx_leaf
*orig_dx_leaf
, *new_dx_leaf
;
3708 struct ocfs2_dx_entry_list
*orig_list
, *new_list
, *tmp_list
;
3709 struct ocfs2_dx_entry
*dx_entry
;
3711 tmp_list
= &tmp_dx_leaf
->dl_list
;
3713 for (i
= 0; i
< num_dx_leaves
; i
++) {
3714 orig_dx_leaf
= (struct ocfs2_dx_leaf
*) orig_dx_leaves
[i
]->b_data
;
3715 orig_list
= &orig_dx_leaf
->dl_list
;
3716 new_dx_leaf
= (struct ocfs2_dx_leaf
*) new_dx_leaves
[i
]->b_data
;
3717 new_list
= &new_dx_leaf
->dl_list
;
3719 num_used
= le16_to_cpu(orig_list
->de_num_used
);
3721 memcpy(tmp_dx_leaf
, orig_dx_leaf
, dir
->i_sb
->s_blocksize
);
3722 tmp_list
->de_num_used
= cpu_to_le16(0);
3723 memset(&tmp_list
->de_entries
, 0, sizeof(*dx_entry
)*num_used
);
3725 for (j
= 0; j
< num_used
; j
++) {
3726 dx_entry
= &orig_list
->de_entries
[j
];
3727 major_hash
= le32_to_cpu(dx_entry
->dx_major_hash
);
3728 if (major_hash
>= split_hash
)
3729 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf
,
3732 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf
,
3735 memcpy(orig_dx_leaf
, tmp_dx_leaf
, dir
->i_sb
->s_blocksize
);
3737 ocfs2_journal_dirty(handle
, orig_dx_leaves
[i
]);
3738 ocfs2_journal_dirty(handle
, new_dx_leaves
[i
]);
3742 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super
*osb
,
3743 struct ocfs2_dx_root_block
*dx_root
)
3745 int credits
= ocfs2_clusters_to_blocks(osb
->sb
, 2);
3747 credits
+= ocfs2_calc_extend_credits(osb
->sb
, &dx_root
->dr_list
, 1);
3748 credits
+= ocfs2_quota_trans_credits(osb
->sb
);
3753 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3754 * half our entries into.
3756 static int ocfs2_dx_dir_rebalance(struct ocfs2_super
*osb
, struct inode
*dir
,
3757 struct buffer_head
*dx_root_bh
,
3758 struct buffer_head
*dx_leaf_bh
,
3759 struct ocfs2_dx_hinfo
*hinfo
, u32 leaf_cpos
,
3762 struct ocfs2_dx_leaf
*dx_leaf
= (struct ocfs2_dx_leaf
*)dx_leaf_bh
->b_data
;
3763 int credits
, ret
, i
, num_used
, did_quota
= 0;
3764 u32 cpos
, split_hash
, insert_hash
= hinfo
->major_hash
;
3765 u64 orig_leaves_start
;
3767 struct buffer_head
**orig_dx_leaves
= NULL
;
3768 struct buffer_head
**new_dx_leaves
= NULL
;
3769 struct ocfs2_alloc_context
*data_ac
= NULL
, *meta_ac
= NULL
;
3770 struct ocfs2_extent_tree et
;
3771 handle_t
*handle
= NULL
;
3772 struct ocfs2_dx_root_block
*dx_root
;
3773 struct ocfs2_dx_leaf
*tmp_dx_leaf
= NULL
;
3775 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3776 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
3777 (unsigned long long)leaf_blkno
, insert_hash
);
3779 ocfs2_init_dx_root_extent_tree(&et
, INODE_CACHE(dir
), dx_root_bh
);
3781 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
3782 if (le32_to_cpu(dx_root
->dr_clusters
) == UINT_MAX
)
3785 num_used
= le16_to_cpu(dx_leaf
->dl_list
.de_num_used
);
3786 if (num_used
< le16_to_cpu(dx_leaf
->dl_list
.de_count
)) {
3787 mlog(ML_ERROR
, "DX Dir: %llu, Asked to rebalance empty leaf: "
3788 "%llu, %d\n", (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
3789 (unsigned long long)leaf_blkno
, num_used
);
3794 orig_dx_leaves
= ocfs2_dx_dir_kmalloc_leaves(osb
->sb
, &num_dx_leaves
);
3795 if (!orig_dx_leaves
) {
3801 new_dx_leaves
= ocfs2_dx_dir_kmalloc_leaves(osb
->sb
, NULL
);
3802 if (!new_dx_leaves
) {
3808 ret
= ocfs2_lock_allocators(dir
, &et
, 1, 0, &data_ac
, &meta_ac
);
3815 credits
= ocfs2_dx_dir_rebalance_credits(osb
, dx_root
);
3816 handle
= ocfs2_start_trans(osb
, credits
);
3817 if (IS_ERR(handle
)) {
3818 ret
= PTR_ERR(handle
);
3824 ret
= dquot_alloc_space_nodirty(dir
,
3825 ocfs2_clusters_to_bytes(dir
->i_sb
, 1));
3830 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
), dx_leaf_bh
,
3831 OCFS2_JOURNAL_ACCESS_WRITE
);
3838 * This block is changing anyway, so we can sort it in place.
3840 sort(dx_leaf
->dl_list
.de_entries
, num_used
,
3841 sizeof(struct ocfs2_dx_entry
), dx_leaf_sort_cmp
,
3844 ocfs2_journal_dirty(handle
, dx_leaf_bh
);
3846 ret
= ocfs2_dx_dir_find_leaf_split(dx_leaf
, leaf_cpos
, insert_hash
,
3853 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3854 leaf_cpos
, split_hash
, insert_hash
);
3857 * We have to carefully order operations here. There are items
3858 * which want to be in the new cluster before insert, but in
3859 * order to put those items in the new cluster, we alter the
3860 * old cluster. A failure to insert gets nasty.
3862 * So, start by reserving writes to the old
3863 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3864 * the new cluster for us, before inserting it. The insert
3865 * won't happen if there's an error before that. Once the
3866 * insert is done then, we can transfer from one leaf into the
3867 * other without fear of hitting any error.
3871 * The leaf transfer wants some scratch space so that we don't
3872 * wind up doing a bunch of expensive memmove().
3874 tmp_dx_leaf
= kmalloc(osb
->sb
->s_blocksize
, GFP_NOFS
);
3881 orig_leaves_start
= ocfs2_block_to_cluster_start(dir
->i_sb
, leaf_blkno
);
3882 ret
= ocfs2_read_dx_leaves(dir
, orig_leaves_start
, num_dx_leaves
,
3890 ret
= ocfs2_dx_dir_new_cluster(dir
, &et
, cpos
, handle
,
3891 data_ac
, meta_ac
, new_dx_leaves
,
3898 for (i
= 0; i
< num_dx_leaves
; i
++) {
3899 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
),
3901 OCFS2_JOURNAL_ACCESS_WRITE
);
3907 ret
= ocfs2_journal_access_dl(handle
, INODE_CACHE(dir
),
3909 OCFS2_JOURNAL_ACCESS_WRITE
);
3916 ocfs2_dx_dir_transfer_leaf(dir
, split_hash
, handle
, tmp_dx_leaf
,
3917 orig_dx_leaves
, new_dx_leaves
, num_dx_leaves
);
3920 if (ret
< 0 && did_quota
)
3921 dquot_free_space_nodirty(dir
,
3922 ocfs2_clusters_to_bytes(dir
->i_sb
, 1));
3924 ocfs2_commit_trans(osb
, handle
);
3927 if (orig_dx_leaves
|| new_dx_leaves
) {
3928 for (i
= 0; i
< num_dx_leaves
; i
++) {
3930 brelse(orig_dx_leaves
[i
]);
3932 brelse(new_dx_leaves
[i
]);
3934 kfree(orig_dx_leaves
);
3935 kfree(new_dx_leaves
);
3939 ocfs2_free_alloc_context(meta_ac
);
3941 ocfs2_free_alloc_context(data_ac
);
3947 static int ocfs2_find_dir_space_dx(struct ocfs2_super
*osb
, struct inode
*dir
,
3948 struct buffer_head
*di_bh
,
3949 struct buffer_head
*dx_root_bh
,
3950 const char *name
, int namelen
,
3951 struct ocfs2_dir_lookup_result
*lookup
)
3953 int ret
, rebalanced
= 0;
3954 struct ocfs2_dx_root_block
*dx_root
;
3955 struct buffer_head
*dx_leaf_bh
= NULL
;
3956 struct ocfs2_dx_leaf
*dx_leaf
;
3960 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
3963 ret
= ocfs2_dx_dir_lookup(dir
, &dx_root
->dr_list
, &lookup
->dl_hinfo
,
3964 &leaf_cpos
, &blkno
);
3970 ret
= ocfs2_read_dx_leaf(dir
, blkno
, &dx_leaf_bh
);
3976 dx_leaf
= (struct ocfs2_dx_leaf
*)dx_leaf_bh
->b_data
;
3978 if (le16_to_cpu(dx_leaf
->dl_list
.de_num_used
) >=
3979 le16_to_cpu(dx_leaf
->dl_list
.de_count
)) {
3985 ret
= ocfs2_dx_dir_rebalance(osb
, dir
, dx_root_bh
, dx_leaf_bh
,
3986 &lookup
->dl_hinfo
, leaf_cpos
,
3995 * Restart the lookup. The rebalance might have
3996 * changed which block our item fits into. Mark our
3997 * progress, so we only execute this once.
4002 goto restart_search
;
4005 lookup
->dl_dx_leaf_bh
= dx_leaf_bh
;
4013 static int ocfs2_search_dx_free_list(struct inode
*dir
,
4014 struct buffer_head
*dx_root_bh
,
4016 struct ocfs2_dir_lookup_result
*lookup
)
4019 struct buffer_head
*leaf_bh
= NULL
, *prev_leaf_bh
= NULL
;
4020 struct ocfs2_dir_block_trailer
*db
;
4022 int rec_len
= OCFS2_DIR_REC_LEN(namelen
);
4023 struct ocfs2_dx_root_block
*dx_root
;
4025 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
4026 next_block
= le64_to_cpu(dx_root
->dr_free_blk
);
4028 while (next_block
) {
4029 brelse(prev_leaf_bh
);
4030 prev_leaf_bh
= leaf_bh
;
4033 ret
= ocfs2_read_dir_block_direct(dir
, next_block
, &leaf_bh
);
4039 db
= ocfs2_trailer_from_bh(leaf_bh
, dir
->i_sb
);
4040 if (rec_len
<= le16_to_cpu(db
->db_free_rec_len
)) {
4041 lookup
->dl_leaf_bh
= leaf_bh
;
4042 lookup
->dl_prev_leaf_bh
= prev_leaf_bh
;
4044 prev_leaf_bh
= NULL
;
4048 next_block
= le64_to_cpu(db
->db_free_next
);
4057 brelse(prev_leaf_bh
);
4061 static int ocfs2_expand_inline_dx_root(struct inode
*dir
,
4062 struct buffer_head
*dx_root_bh
)
4064 int ret
, num_dx_leaves
, i
, j
, did_quota
= 0;
4065 struct buffer_head
**dx_leaves
= NULL
;
4066 struct ocfs2_extent_tree et
;
4068 struct ocfs2_alloc_context
*data_ac
= NULL
;
4069 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
4070 handle_t
*handle
= NULL
;
4071 struct ocfs2_dx_root_block
*dx_root
;
4072 struct ocfs2_dx_entry_list
*entry_list
;
4073 struct ocfs2_dx_entry
*dx_entry
;
4074 struct ocfs2_dx_leaf
*target_leaf
;
4076 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
4082 dx_leaves
= ocfs2_dx_dir_kmalloc_leaves(osb
->sb
, &num_dx_leaves
);
4089 handle
= ocfs2_start_trans(osb
, ocfs2_calc_dxi_expand_credits(osb
->sb
));
4090 if (IS_ERR(handle
)) {
4091 ret
= PTR_ERR(handle
);
4096 ret
= dquot_alloc_space_nodirty(dir
,
4097 ocfs2_clusters_to_bytes(osb
->sb
, 1));
4103 * We do this up front, before the allocation, so that a
4104 * failure to add the dx_root_bh to the journal won't result
4105 * us losing clusters.
4107 ret
= ocfs2_journal_access_dr(handle
, INODE_CACHE(dir
), dx_root_bh
,
4108 OCFS2_JOURNAL_ACCESS_WRITE
);
4114 ret
= __ocfs2_dx_dir_new_cluster(dir
, 0, handle
, data_ac
, dx_leaves
,
4115 num_dx_leaves
, &insert_blkno
);
4122 * Transfer the entries from our dx_root into the appropriate
4125 dx_root
= (struct ocfs2_dx_root_block
*) dx_root_bh
->b_data
;
4126 entry_list
= &dx_root
->dr_entries
;
4128 for (i
= 0; i
< le16_to_cpu(entry_list
->de_num_used
); i
++) {
4129 dx_entry
= &entry_list
->de_entries
[i
];
4131 j
= __ocfs2_dx_dir_hash_idx(osb
,
4132 le32_to_cpu(dx_entry
->dx_minor_hash
));
4133 target_leaf
= (struct ocfs2_dx_leaf
*)dx_leaves
[j
]->b_data
;
4135 ocfs2_dx_dir_leaf_insert_tail(target_leaf
, dx_entry
);
4137 /* Each leaf has been passed to the journal already
4138 * via __ocfs2_dx_dir_new_cluster() */
4141 dx_root
->dr_flags
&= ~OCFS2_DX_FLAG_INLINE
;
4142 memset(&dx_root
->dr_list
, 0, osb
->sb
->s_blocksize
-
4143 offsetof(struct ocfs2_dx_root_block
, dr_list
));
4144 dx_root
->dr_list
.l_count
=
4145 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb
->sb
));
4147 /* This should never fail considering we start with an empty
4149 ocfs2_init_dx_root_extent_tree(&et
, INODE_CACHE(dir
), dx_root_bh
);
4150 ret
= ocfs2_insert_extent(handle
, &et
, 0, insert_blkno
, 1, 0, NULL
);
4155 ocfs2_journal_dirty(handle
, dx_root_bh
);
4158 if (ret
< 0 && did_quota
)
4159 dquot_free_space_nodirty(dir
,
4160 ocfs2_clusters_to_bytes(dir
->i_sb
, 1));
4162 ocfs2_commit_trans(osb
, handle
);
4166 ocfs2_free_alloc_context(data_ac
);
4169 for (i
= 0; i
< num_dx_leaves
; i
++)
4170 brelse(dx_leaves
[i
]);
4176 static int ocfs2_inline_dx_has_space(struct buffer_head
*dx_root_bh
)
4178 struct ocfs2_dx_root_block
*dx_root
;
4179 struct ocfs2_dx_entry_list
*entry_list
;
4181 dx_root
= (struct ocfs2_dx_root_block
*) dx_root_bh
->b_data
;
4182 entry_list
= &dx_root
->dr_entries
;
4184 if (le16_to_cpu(entry_list
->de_num_used
) >=
4185 le16_to_cpu(entry_list
->de_count
))
4191 static int ocfs2_prepare_dx_dir_for_insert(struct inode
*dir
,
4192 struct buffer_head
*di_bh
,
4195 struct ocfs2_dir_lookup_result
*lookup
)
4197 int ret
, free_dx_root
= 1;
4198 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
4199 struct buffer_head
*dx_root_bh
= NULL
;
4200 struct buffer_head
*leaf_bh
= NULL
;
4201 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4202 struct ocfs2_dx_root_block
*dx_root
;
4204 ret
= ocfs2_read_dx_root(dir
, di
, &dx_root_bh
);
4210 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
4211 if (le32_to_cpu(dx_root
->dr_num_entries
) == OCFS2_DX_ENTRIES_MAX
) {
4217 if (ocfs2_dx_root_inline(dx_root
)) {
4218 ret
= ocfs2_inline_dx_has_space(dx_root_bh
);
4224 * We ran out of room in the root block. Expand it to
4225 * an extent, then allow ocfs2_find_dir_space_dx to do
4228 ret
= ocfs2_expand_inline_dx_root(dir
, dx_root_bh
);
4236 * Insert preparation for an indexed directory is split into two
4237 * steps. The call to find_dir_space_dx reserves room in the index for
4238 * an additional item. If we run out of space there, it's a real error
4239 * we can't continue on.
4241 ret
= ocfs2_find_dir_space_dx(osb
, dir
, di_bh
, dx_root_bh
, name
,
4250 * Next, we need to find space in the unindexed tree. This call
4251 * searches using the free space linked list. If the unindexed tree
4252 * lacks sufficient space, we'll expand it below. The expansion code
4253 * is smart enough to add any new blocks to the free space list.
4255 ret
= ocfs2_search_dx_free_list(dir
, dx_root_bh
, namelen
, lookup
);
4256 if (ret
&& ret
!= -ENOSPC
) {
4261 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4262 lookup
->dl_dx_root_bh
= dx_root_bh
;
4265 if (ret
== -ENOSPC
) {
4266 ret
= ocfs2_extend_dir(osb
, dir
, di_bh
, 1, lookup
, &leaf_bh
);
4274 * We make the assumption here that new leaf blocks are added
4275 * to the front of our free list.
4277 lookup
->dl_prev_leaf_bh
= NULL
;
4278 lookup
->dl_leaf_bh
= leaf_bh
;
4288 * Get a directory ready for insert. Any directory allocation required
4289 * happens here. Success returns zero, and enough context in the dir
4290 * lookup result that ocfs2_add_entry() will be able complete the task
4291 * with minimal performance impact.
4293 int ocfs2_prepare_dir_for_insert(struct ocfs2_super
*osb
,
4295 struct buffer_head
*parent_fe_bh
,
4298 struct ocfs2_dir_lookup_result
*lookup
)
4301 unsigned int blocks_wanted
= 1;
4302 struct buffer_head
*bh
= NULL
;
4304 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
4305 namelen
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
4314 * Do this up front to reduce confusion.
4316 * The directory might start inline, then be turned into an
4317 * indexed one, in which case we'd need to hash deep inside
4318 * ocfs2_find_dir_space_id(). Since
4319 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4320 * done, there seems no point in spreading out the calls. We
4321 * can optimize away the case where the file system doesn't
4324 if (ocfs2_supports_indexed_dirs(osb
))
4325 ocfs2_dx_dir_name_hash(dir
, name
, namelen
, &lookup
->dl_hinfo
);
4327 if (ocfs2_dir_indexed(dir
)) {
4328 ret
= ocfs2_prepare_dx_dir_for_insert(dir
, parent_fe_bh
,
4329 name
, namelen
, lookup
);
4335 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
4336 ret
= ocfs2_find_dir_space_id(dir
, parent_fe_bh
, name
,
4337 namelen
, &bh
, &blocks_wanted
);
4339 ret
= ocfs2_find_dir_space_el(dir
, name
, namelen
, &bh
);
4341 if (ret
&& ret
!= -ENOSPC
) {
4346 if (ret
== -ENOSPC
) {
4348 * We have to expand the directory to add this name.
4352 ret
= ocfs2_extend_dir(osb
, dir
, parent_fe_bh
, blocks_wanted
,
4363 lookup
->dl_leaf_bh
= bh
;
4370 static int ocfs2_dx_dir_remove_index(struct inode
*dir
,
4371 struct buffer_head
*di_bh
,
4372 struct buffer_head
*dx_root_bh
)
4375 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
4376 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4377 struct ocfs2_dx_root_block
*dx_root
;
4378 struct inode
*dx_alloc_inode
= NULL
;
4379 struct buffer_head
*dx_alloc_bh
= NULL
;
4385 dx_root
= (struct ocfs2_dx_root_block
*) dx_root_bh
->b_data
;
4387 dx_alloc_inode
= ocfs2_get_system_file_inode(osb
,
4388 EXTENT_ALLOC_SYSTEM_INODE
,
4389 le16_to_cpu(dx_root
->dr_suballoc_slot
));
4390 if (!dx_alloc_inode
) {
4395 mutex_lock(&dx_alloc_inode
->i_mutex
);
4397 ret
= ocfs2_inode_lock(dx_alloc_inode
, &dx_alloc_bh
, 1);
4403 handle
= ocfs2_start_trans(osb
, OCFS2_DX_ROOT_REMOVE_CREDITS
);
4404 if (IS_ERR(handle
)) {
4405 ret
= PTR_ERR(handle
);
4410 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(dir
), di_bh
,
4411 OCFS2_JOURNAL_ACCESS_WRITE
);
4417 OCFS2_I(dir
)->ip_dyn_features
&= ~OCFS2_INDEXED_DIR_FL
;
4418 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(dir
)->ip_dyn_features
);
4419 di
->i_dx_root
= cpu_to_le64(0ULL);
4421 ocfs2_journal_dirty(handle
, di_bh
);
4423 blk
= le64_to_cpu(dx_root
->dr_blkno
);
4424 bit
= le16_to_cpu(dx_root
->dr_suballoc_bit
);
4425 if (dx_root
->dr_suballoc_loc
)
4426 bg_blkno
= le64_to_cpu(dx_root
->dr_suballoc_loc
);
4428 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
4429 ret
= ocfs2_free_suballoc_bits(handle
, dx_alloc_inode
, dx_alloc_bh
,
4435 ocfs2_commit_trans(osb
, handle
);
4438 ocfs2_inode_unlock(dx_alloc_inode
, 1);
4441 mutex_unlock(&dx_alloc_inode
->i_mutex
);
4442 brelse(dx_alloc_bh
);
4444 iput(dx_alloc_inode
);
4448 int ocfs2_dx_dir_truncate(struct inode
*dir
, struct buffer_head
*di_bh
)
4451 unsigned int uninitialized_var(clen
);
4452 u32 major_hash
= UINT_MAX
, p_cpos
, uninitialized_var(cpos
);
4453 u64
uninitialized_var(blkno
);
4454 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
4455 struct buffer_head
*dx_root_bh
= NULL
;
4456 struct ocfs2_dx_root_block
*dx_root
;
4457 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4458 struct ocfs2_cached_dealloc_ctxt dealloc
;
4459 struct ocfs2_extent_tree et
;
4461 ocfs2_init_dealloc_ctxt(&dealloc
);
4463 if (!ocfs2_dir_indexed(dir
))
4466 ret
= ocfs2_read_dx_root(dir
, di
, &dx_root_bh
);
4471 dx_root
= (struct ocfs2_dx_root_block
*)dx_root_bh
->b_data
;
4473 if (ocfs2_dx_root_inline(dx_root
))
4476 ocfs2_init_dx_root_extent_tree(&et
, INODE_CACHE(dir
), dx_root_bh
);
4478 while (le32_to_cpu(dx_root
->dr_clusters
)) {
4479 ret
= ocfs2_dx_dir_lookup_rec(dir
, &dx_root
->dr_list
,
4480 major_hash
, &cpos
, &blkno
, &clen
);
4486 p_cpos
= ocfs2_blocks_to_clusters(dir
->i_sb
, blkno
);
4488 ret
= ocfs2_remove_btree_range(dir
, &et
, cpos
, p_cpos
, clen
, 0,
4498 major_hash
= cpos
- 1;
4502 ret
= ocfs2_dx_dir_remove_index(dir
, di_bh
, dx_root_bh
);
4508 ocfs2_remove_from_cache(INODE_CACHE(dir
), dx_root_bh
);
4510 ocfs2_schedule_truncate_log_flush(osb
, 1);
4511 ocfs2_run_deallocs(osb
, &dealloc
);