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 Linus 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>
45 #include <linux/iversion.h>
47 #include <cluster/masklog.h>
52 #include "blockcheck.h"
55 #include "extent_map.h"
64 #include "ocfs2_trace.h"
66 #include "buffer_head_io.h"
68 #define NAMEI_RA_CHUNKS 2
69 #define NAMEI_RA_BLOCKS 4
70 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
72 static int ocfs2_do_extend_dir(struct super_block *sb,
75 struct buffer_head *parent_fe_bh,
76 struct ocfs2_alloc_context *data_ac,
77 struct ocfs2_alloc_context *meta_ac,
78 struct buffer_head **new_bh);
79 static int ocfs2_dir_indexed(struct inode *inode);
82 * These are distinct checks because future versions of the file system will
83 * want to have a trailing dirent structure independent of indexing.
85 static int ocfs2_supports_dir_trailer(struct inode *dir)
87 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
89 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
92 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
96 * "new' here refers to the point at which we're creating a new
97 * directory via "mkdir()", but also when we're expanding an inline
98 * directory. In either case, we don't yet have the indexing bit set
99 * on the directory, so the standard checks will fail in when metaecc
100 * is turned off. Only directory-initialization type functions should
101 * use this then. Everything else wants ocfs2_supports_dir_trailer()
103 static int ocfs2_new_dir_wants_trailer(struct inode *dir)
105 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
107 return ocfs2_meta_ecc(osb) ||
108 ocfs2_supports_indexed_dirs(osb);
111 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
113 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
116 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
118 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
119 * them more consistent? */
120 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
125 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
126 return (struct ocfs2_dir_block_trailer *)p;
130 * XXX: This is executed once on every dirent. We should consider optimizing
133 static int ocfs2_skip_dir_trailer(struct inode *dir,
134 struct ocfs2_dir_entry *de,
135 unsigned long offset,
136 unsigned long blklen)
138 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
140 if (!ocfs2_supports_dir_trailer(dir))
149 static void ocfs2_init_dir_trailer(struct inode *inode,
150 struct buffer_head *bh, u16 rec_len)
152 struct ocfs2_dir_block_trailer *trailer;
154 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
155 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
156 trailer->db_compat_rec_len =
157 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
158 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
159 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
160 trailer->db_free_rec_len = cpu_to_le16(rec_len);
163 * Link an unindexed block with a dir trailer structure into the index free
164 * list. This function will modify dirdata_bh, but assumes you've already
165 * passed it to the journal.
167 static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
168 struct buffer_head *dx_root_bh,
169 struct buffer_head *dirdata_bh)
172 struct ocfs2_dx_root_block *dx_root;
173 struct ocfs2_dir_block_trailer *trailer;
175 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
176 OCFS2_JOURNAL_ACCESS_WRITE);
181 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
182 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
184 trailer->db_free_next = dx_root->dr_free_blk;
185 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
187 ocfs2_journal_dirty(handle, dx_root_bh);
193 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
195 return res->dl_prev_leaf_bh == NULL;
198 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
200 brelse(res->dl_dx_root_bh);
201 brelse(res->dl_leaf_bh);
202 brelse(res->dl_dx_leaf_bh);
203 brelse(res->dl_prev_leaf_bh);
206 static int ocfs2_dir_indexed(struct inode *inode)
208 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
213 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
215 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
219 * Hashing code adapted from ext3
221 #define DELTA 0x9E3779B9
223 static void TEA_transform(__u32 buf[4], __u32 const in[])
226 __u32 b0 = buf[0], b1 = buf[1];
227 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
232 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
233 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
240 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
245 pad = (__u32)len | ((__u32)len << 8);
251 for (i = 0; i < len; i++) {
254 val = msg[i] + (val << 8);
267 static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
268 struct ocfs2_dx_hinfo *hinfo)
270 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
275 * XXX: Is this really necessary, if the index is never looked
276 * at by readdir? Is a hash value of '0' a bad idea?
278 if ((len == 1 && !strncmp(".", name, 1)) ||
279 (len == 2 && !strncmp("..", name, 2))) {
284 #ifdef OCFS2_DEBUG_DX_DIRS
286 * This makes it very easy to debug indexing problems. We
287 * should never allow this to be selected without hand editing
290 buf[0] = buf[1] = len;
294 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
298 str2hashbuf(p, len, in, 4);
299 TEA_transform(buf, in);
305 hinfo->major_hash = buf[0];
306 hinfo->minor_hash = buf[1];
310 * bh passed here can be an inode block or a dir data block, depending
311 * on the inode inline data flag.
313 static int ocfs2_check_dir_entry(struct inode * dir,
314 struct ocfs2_dir_entry * de,
315 struct buffer_head * bh,
316 unsigned long offset)
318 const char *error_msg = NULL;
319 const int rlen = le16_to_cpu(de->rec_len);
321 if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
322 error_msg = "rec_len is smaller than minimal";
323 else if (unlikely(rlen % 4 != 0))
324 error_msg = "rec_len % 4 != 0";
325 else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
326 error_msg = "rec_len is too small for name_len";
328 ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
329 error_msg = "directory entry across blocks";
331 if (unlikely(error_msg != NULL))
332 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
333 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
334 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
335 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
338 return error_msg == NULL ? 1 : 0;
341 static inline int ocfs2_match(int len,
342 const char * const name,
343 struct ocfs2_dir_entry *de)
345 if (len != de->name_len)
349 return !memcmp(name, de->name, len);
353 * Returns 0 if not found, -1 on failure, and 1 on success
355 static inline int ocfs2_search_dirblock(struct buffer_head *bh,
357 const char *name, int namelen,
358 unsigned long offset,
361 struct ocfs2_dir_entry **res_dir)
363 struct ocfs2_dir_entry *de;
364 char *dlimit, *de_buf;
369 dlimit = de_buf + bytes;
371 while (de_buf < dlimit) {
372 /* this code is executed quadratically often */
373 /* do minimal checking `by hand' */
375 de = (struct ocfs2_dir_entry *) de_buf;
377 if (de_buf + namelen <= dlimit &&
378 ocfs2_match(namelen, name, de)) {
379 /* found a match - just to be sure, do a full check */
380 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
389 /* prevent looping on a bad block */
390 de_len = le16_to_cpu(de->rec_len);
401 trace_ocfs2_search_dirblock(ret);
405 static struct buffer_head *ocfs2_find_entry_id(const char *name,
408 struct ocfs2_dir_entry **res_dir)
411 struct buffer_head *di_bh = NULL;
412 struct ocfs2_dinode *di;
413 struct ocfs2_inline_data *data;
415 ret = ocfs2_read_inode_block(dir, &di_bh);
421 di = (struct ocfs2_dinode *)di_bh->b_data;
422 data = &di->id2.i_data;
424 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
425 data->id_data, i_size_read(dir), res_dir);
434 static int ocfs2_validate_dir_block(struct super_block *sb,
435 struct buffer_head *bh)
438 struct ocfs2_dir_block_trailer *trailer =
439 ocfs2_trailer_from_bh(bh, sb);
443 * We don't validate dirents here, that's handled
444 * in-place when the code walks them.
446 trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
448 BUG_ON(!buffer_uptodate(bh));
451 * If the ecc fails, we return the error but otherwise
452 * leave the filesystem running. We know any error is
453 * local to this block.
455 * Note that we are safe to call this even if the directory
456 * doesn't have a trailer. Filesystems without metaecc will do
457 * nothing, and filesystems with it will have one.
459 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
461 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
462 (unsigned long long)bh->b_blocknr);
468 * Validate a directory trailer.
470 * We check the trailer here rather than in ocfs2_validate_dir_block()
471 * because that function doesn't have the inode to test.
473 static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
476 struct ocfs2_dir_block_trailer *trailer;
478 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
479 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
480 rc = ocfs2_error(dir->i_sb,
481 "Invalid dirblock #%llu: signature = %.*s\n",
482 (unsigned long long)bh->b_blocknr, 7,
483 trailer->db_signature);
486 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
487 rc = ocfs2_error(dir->i_sb,
488 "Directory block #%llu has an invalid db_blkno of %llu\n",
489 (unsigned long long)bh->b_blocknr,
490 (unsigned long long)le64_to_cpu(trailer->db_blkno));
493 if (le64_to_cpu(trailer->db_parent_dinode) !=
494 OCFS2_I(dir)->ip_blkno) {
495 rc = ocfs2_error(dir->i_sb,
496 "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu\n",
497 (unsigned long long)bh->b_blocknr,
498 (unsigned long long)OCFS2_I(dir)->ip_blkno,
499 (unsigned long long)le64_to_cpu(trailer->db_blkno));
507 * This function forces all errors to -EIO for consistency with its
508 * predecessor, ocfs2_bread(). We haven't audited what returning the
509 * real error codes would do to callers. We log the real codes with
510 * mlog_errno() before we squash them.
512 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
513 struct buffer_head **bh, int flags)
516 struct buffer_head *tmp = *bh;
518 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
519 ocfs2_validate_dir_block);
525 if (!(flags & OCFS2_BH_READAHEAD) &&
526 ocfs2_supports_dir_trailer(inode)) {
527 rc = ocfs2_check_dir_trailer(inode, tmp);
536 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
541 return rc ? -EIO : 0;
545 * Read the block at 'phys' which belongs to this directory
546 * inode. This function does no virtual->physical block translation -
547 * what's passed in is assumed to be a valid directory block.
549 static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
550 struct buffer_head **bh)
553 struct buffer_head *tmp = *bh;
555 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
556 ocfs2_validate_dir_block);
562 if (ocfs2_supports_dir_trailer(dir)) {
563 ret = ocfs2_check_dir_trailer(dir, tmp);
578 static int ocfs2_validate_dx_root(struct super_block *sb,
579 struct buffer_head *bh)
582 struct ocfs2_dx_root_block *dx_root;
584 BUG_ON(!buffer_uptodate(bh));
586 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
588 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
591 "Checksum failed for dir index root block %llu\n",
592 (unsigned long long)bh->b_blocknr);
596 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
597 ret = ocfs2_error(sb,
598 "Dir Index Root # %llu has bad signature %.*s\n",
599 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
600 7, dx_root->dr_signature);
606 static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
607 struct buffer_head **dx_root_bh)
610 u64 blkno = le64_to_cpu(di->i_dx_root);
611 struct buffer_head *tmp = *dx_root_bh;
613 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
614 ocfs2_validate_dx_root);
616 /* If ocfs2_read_block() got us a new bh, pass it up. */
617 if (!ret && !*dx_root_bh)
623 static int ocfs2_validate_dx_leaf(struct super_block *sb,
624 struct buffer_head *bh)
627 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
629 BUG_ON(!buffer_uptodate(bh));
631 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
634 "Checksum failed for dir index leaf block %llu\n",
635 (unsigned long long)bh->b_blocknr);
639 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
640 ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s\n",
641 7, dx_leaf->dl_signature);
647 static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
648 struct buffer_head **dx_leaf_bh)
651 struct buffer_head *tmp = *dx_leaf_bh;
653 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
654 ocfs2_validate_dx_leaf);
656 /* If ocfs2_read_block() got us a new bh, pass it up. */
657 if (!ret && !*dx_leaf_bh)
664 * Read a series of dx_leaf blocks. This expects all buffer_head
665 * pointers to be NULL on function entry.
667 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
668 struct buffer_head **dx_leaf_bhs)
672 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
673 ocfs2_validate_dx_leaf);
680 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
682 struct ocfs2_dir_entry **res_dir)
684 struct super_block *sb;
685 struct buffer_head *bh_use[NAMEI_RA_SIZE];
686 struct buffer_head *bh, *ret = NULL;
687 unsigned long start, block, b;
688 int ra_max = 0; /* Number of bh's in the readahead
690 int ra_ptr = 0; /* Current index into readahead
697 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
698 start = OCFS2_I(dir)->ip_dir_start_lookup;
699 if (start >= nblocks)
706 * We deal with the read-ahead logic here.
708 if (ra_ptr >= ra_max) {
709 /* Refill the readahead buffer */
712 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
714 * Terminate if we reach the end of the
715 * directory and must wrap, or if our
716 * search has finished at this block.
718 if (b >= nblocks || (num && block == start)) {
719 bh_use[ra_max] = NULL;
725 err = ocfs2_read_dir_block(dir, b++, &bh,
730 if ((bh = bh_use[ra_ptr++]) == NULL)
732 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
733 /* read error, skip block & hope for the best.
734 * ocfs2_read_dir_block() has released the bh. */
735 mlog(ML_ERROR, "reading directory %llu, "
737 (unsigned long long)OCFS2_I(dir)->ip_blkno,
741 i = ocfs2_search_dirblock(bh, dir, name, namelen,
742 block << sb->s_blocksize_bits,
743 bh->b_data, sb->s_blocksize,
746 OCFS2_I(dir)->ip_dir_start_lookup = block;
748 goto cleanup_and_exit;
752 goto cleanup_and_exit;
755 if (++block >= nblocks)
757 } while (block != start);
760 * If the directory has grown while we were searching, then
761 * search the last part of the directory before giving up.
764 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
765 if (block < nblocks) {
771 /* Clean up the read-ahead blocks */
772 for (; ra_ptr < ra_max; ra_ptr++)
773 brelse(bh_use[ra_ptr]);
775 trace_ocfs2_find_entry_el(ret);
779 static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
780 struct ocfs2_extent_list *el,
784 unsigned int *ret_clen)
786 int ret = 0, i, found;
787 struct buffer_head *eb_bh = NULL;
788 struct ocfs2_extent_block *eb;
789 struct ocfs2_extent_rec *rec = NULL;
791 if (el->l_tree_depth) {
792 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
799 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
802 if (el->l_tree_depth) {
803 ret = ocfs2_error(inode->i_sb,
804 "Inode %lu has non zero tree depth in btree tree block %llu\n",
806 (unsigned long long)eb_bh->b_blocknr);
812 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
813 rec = &el->l_recs[i];
815 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
822 ret = ocfs2_error(inode->i_sb,
823 "Inode %lu has bad extent record (%u, %u, 0) in btree\n",
825 le32_to_cpu(rec->e_cpos),
826 ocfs2_rec_clusters(el, rec));
831 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
833 *ret_cpos = le32_to_cpu(rec->e_cpos);
835 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
843 * Returns the block index, from the start of the cluster which this
846 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
849 return minor_hash & osb->osb_dx_mask;
852 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
853 struct ocfs2_dx_hinfo *hinfo)
855 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
858 static int ocfs2_dx_dir_lookup(struct inode *inode,
859 struct ocfs2_extent_list *el,
860 struct ocfs2_dx_hinfo *hinfo,
865 unsigned int cend, uninitialized_var(clen);
866 u32 uninitialized_var(cpos);
867 u64 uninitialized_var(blkno);
868 u32 name_hash = hinfo->major_hash;
870 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
878 if (name_hash >= cend) {
879 /* We want the last cluster */
880 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
883 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
889 * We now have the cluster which should hold our entry. To
890 * find the exact block from the start of the cluster to
891 * search, we take the lower bits of the hash.
893 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
896 *ret_phys_blkno = blkno;
905 static int ocfs2_dx_dir_search(const char *name, int namelen,
907 struct ocfs2_dx_root_block *dx_root,
908 struct ocfs2_dir_lookup_result *res)
911 u64 uninitialized_var(phys);
912 struct buffer_head *dx_leaf_bh = NULL;
913 struct ocfs2_dx_leaf *dx_leaf;
914 struct ocfs2_dx_entry *dx_entry = NULL;
915 struct buffer_head *dir_ent_bh = NULL;
916 struct ocfs2_dir_entry *dir_ent = NULL;
917 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
918 struct ocfs2_extent_list *dr_el;
919 struct ocfs2_dx_entry_list *entry_list;
921 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
923 if (ocfs2_dx_root_inline(dx_root)) {
924 entry_list = &dx_root->dr_entries;
928 dr_el = &dx_root->dr_list;
930 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
936 trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
937 namelen, name, hinfo->major_hash,
938 hinfo->minor_hash, (unsigned long long)phys);
940 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
946 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
948 trace_ocfs2_dx_dir_search_leaf_info(
949 le16_to_cpu(dx_leaf->dl_list.de_num_used),
950 le16_to_cpu(dx_leaf->dl_list.de_count));
952 entry_list = &dx_leaf->dl_list;
956 * Empty leaf is legal, so no need to check for that.
959 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
960 dx_entry = &entry_list->de_entries[i];
962 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
963 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
967 * Search unindexed leaf block now. We're not
968 * guaranteed to find anything.
970 ret = ocfs2_read_dir_block_direct(dir,
971 le64_to_cpu(dx_entry->dx_dirent_blk),
979 * XXX: We should check the unindexed block here,
983 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
984 0, dir_ent_bh->b_data,
985 dir->i_sb->s_blocksize, &dir_ent);
990 /* This means we found a bad directory entry. */
1005 res->dl_leaf_bh = dir_ent_bh;
1006 res->dl_entry = dir_ent;
1007 res->dl_dx_leaf_bh = dx_leaf_bh;
1008 res->dl_dx_entry = dx_entry;
1019 static int ocfs2_find_entry_dx(const char *name, int namelen,
1021 struct ocfs2_dir_lookup_result *lookup)
1024 struct buffer_head *di_bh = NULL;
1025 struct ocfs2_dinode *di;
1026 struct buffer_head *dx_root_bh = NULL;
1027 struct ocfs2_dx_root_block *dx_root;
1029 ret = ocfs2_read_inode_block(dir, &di_bh);
1035 di = (struct ocfs2_dinode *)di_bh->b_data;
1037 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1042 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1044 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
1051 lookup->dl_dx_root_bh = dx_root_bh;
1060 * Try to find an entry of the provided name within 'dir'.
1062 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1063 * returned and the struct 'res' will contain information useful to
1064 * other directory manipulation functions.
1066 * Caller can NOT assume anything about the contents of the
1067 * buffer_heads - they are passed back only so that it can be passed
1068 * into any one of the manipulation functions (add entry, delete
1069 * entry, etc). As an example, bh in the extent directory case is a
1070 * data block, in the inline-data case it actually points to an inode,
1071 * in the indexed directory case, multiple buffers are involved.
1073 int ocfs2_find_entry(const char *name, int namelen,
1074 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1076 struct buffer_head *bh;
1077 struct ocfs2_dir_entry *res_dir = NULL;
1079 if (ocfs2_dir_indexed(dir))
1080 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1083 * The unindexed dir code only uses part of the lookup
1084 * structure, so there's no reason to push it down further
1087 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1088 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1090 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1095 lookup->dl_leaf_bh = bh;
1096 lookup->dl_entry = res_dir;
1101 * Update inode number and type of a previously found directory entry.
1103 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1104 struct ocfs2_dir_lookup_result *res,
1105 struct inode *new_entry_inode)
1108 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1109 struct ocfs2_dir_entry *de = res->dl_entry;
1110 struct buffer_head *de_bh = res->dl_leaf_bh;
1113 * The same code works fine for both inline-data and extent
1114 * based directories, so no need to split this up. The only
1115 * difference is the journal_access function.
1118 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1119 access = ocfs2_journal_access_di;
1121 ret = access(handle, INODE_CACHE(dir), de_bh,
1122 OCFS2_JOURNAL_ACCESS_WRITE);
1128 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1129 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1131 ocfs2_journal_dirty(handle, de_bh);
1138 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1141 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1142 struct ocfs2_dir_entry *de_del,
1143 struct buffer_head *bh, char *first_de,
1146 struct ocfs2_dir_entry *de, *pde;
1147 int i, status = -ENOENT;
1148 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1150 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1151 access = ocfs2_journal_access_di;
1155 de = (struct ocfs2_dir_entry *) first_de;
1157 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1163 status = access(handle, INODE_CACHE(dir), bh,
1164 OCFS2_JOURNAL_ACCESS_WRITE);
1171 le16_add_cpu(&pde->rec_len,
1172 le16_to_cpu(de->rec_len));
1174 inode_inc_iversion(dir);
1175 ocfs2_journal_dirty(handle, bh);
1178 i += le16_to_cpu(de->rec_len);
1180 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1186 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1190 if (le64_to_cpu(de->inode) == 0)
1191 hole = le16_to_cpu(de->rec_len);
1193 hole = le16_to_cpu(de->rec_len) -
1194 OCFS2_DIR_REC_LEN(de->name_len);
1199 static int ocfs2_find_max_rec_len(struct super_block *sb,
1200 struct buffer_head *dirblock_bh)
1202 int size, this_hole, largest_hole = 0;
1203 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1204 struct ocfs2_dir_entry *de;
1206 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1207 size = ocfs2_dir_trailer_blk_off(sb);
1208 limit = start + size;
1210 de = (struct ocfs2_dir_entry *)de_buf;
1212 if (de_buf != trailer) {
1213 this_hole = ocfs2_figure_dirent_hole(de);
1214 if (this_hole > largest_hole)
1215 largest_hole = this_hole;
1218 de_buf += le16_to_cpu(de->rec_len);
1219 de = (struct ocfs2_dir_entry *)de_buf;
1220 } while (de_buf < limit);
1222 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1223 return largest_hole;
1227 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1230 int num_used = le16_to_cpu(entry_list->de_num_used);
1232 if (num_used == 1 || index == (num_used - 1))
1235 memmove(&entry_list->de_entries[index],
1236 &entry_list->de_entries[index + 1],
1237 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1240 memset(&entry_list->de_entries[num_used], 0,
1241 sizeof(struct ocfs2_dx_entry));
1242 entry_list->de_num_used = cpu_to_le16(num_used);
1245 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1246 struct ocfs2_dir_lookup_result *lookup)
1248 int ret, index, max_rec_len, add_to_free_list = 0;
1249 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1250 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1251 struct ocfs2_dx_leaf *dx_leaf;
1252 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
1253 struct ocfs2_dir_block_trailer *trailer;
1254 struct ocfs2_dx_root_block *dx_root;
1255 struct ocfs2_dx_entry_list *entry_list;
1258 * This function gets a bit messy because we might have to
1259 * modify the root block, regardless of whether the indexed
1260 * entries are stored inline.
1264 * *Only* set 'entry_list' here, based on where we're looking
1265 * for the indexed entries. Later, we might still want to
1266 * journal both blocks, based on free list state.
1268 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1269 if (ocfs2_dx_root_inline(dx_root)) {
1270 entry_list = &dx_root->dr_entries;
1272 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1273 entry_list = &dx_leaf->dl_list;
1276 /* Neither of these are a disk corruption - that should have
1277 * been caught by lookup, before we got here. */
1278 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1279 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
1281 index = (char *)dx_entry - (char *)entry_list->de_entries;
1282 index /= sizeof(*dx_entry);
1284 if (index >= le16_to_cpu(entry_list->de_num_used)) {
1285 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1286 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1287 entry_list, dx_entry);
1292 * We know that removal of this dirent will leave enough room
1293 * for a new one, so add this block to the free list if it
1294 * isn't already there.
1296 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1297 if (trailer->db_free_rec_len == 0)
1298 add_to_free_list = 1;
1301 * Add the block holding our index into the journal before
1302 * removing the unindexed entry. If we get an error return
1303 * from __ocfs2_delete_entry(), then it hasn't removed the
1304 * entry yet. Likewise, successful return means we *must*
1305 * remove the indexed entry.
1307 * We're also careful to journal the root tree block here as
1308 * the entry count needs to be updated. Also, we might be
1309 * adding to the start of the free list.
1311 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1312 OCFS2_JOURNAL_ACCESS_WRITE);
1318 if (!ocfs2_dx_root_inline(dx_root)) {
1319 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
1320 lookup->dl_dx_leaf_bh,
1321 OCFS2_JOURNAL_ACCESS_WRITE);
1328 trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
1331 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1332 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1338 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1339 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1340 if (add_to_free_list) {
1341 trailer->db_free_next = dx_root->dr_free_blk;
1342 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1343 ocfs2_journal_dirty(handle, dx_root_bh);
1346 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1347 ocfs2_journal_dirty(handle, leaf_bh);
1349 le32_add_cpu(&dx_root->dr_num_entries, -1);
1350 ocfs2_journal_dirty(handle, dx_root_bh);
1352 ocfs2_dx_list_remove_entry(entry_list, index);
1354 if (!ocfs2_dx_root_inline(dx_root))
1355 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
1361 static inline int ocfs2_delete_entry_id(handle_t *handle,
1363 struct ocfs2_dir_entry *de_del,
1364 struct buffer_head *bh)
1367 struct buffer_head *di_bh = NULL;
1368 struct ocfs2_dinode *di;
1369 struct ocfs2_inline_data *data;
1371 ret = ocfs2_read_inode_block(dir, &di_bh);
1377 di = (struct ocfs2_dinode *)di_bh->b_data;
1378 data = &di->id2.i_data;
1380 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1388 static inline int ocfs2_delete_entry_el(handle_t *handle,
1390 struct ocfs2_dir_entry *de_del,
1391 struct buffer_head *bh)
1393 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1398 * Delete a directory entry. Hide the details of directory
1399 * implementation from the caller.
1401 int ocfs2_delete_entry(handle_t *handle,
1403 struct ocfs2_dir_lookup_result *res)
1405 if (ocfs2_dir_indexed(dir))
1406 return ocfs2_delete_entry_dx(handle, dir, res);
1408 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1409 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1412 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1417 * Check whether 'de' has enough room to hold an entry of
1418 * 'new_rec_len' bytes.
1420 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1421 unsigned int new_rec_len)
1423 unsigned int de_really_used;
1425 /* Check whether this is an empty record with enough space */
1426 if (le64_to_cpu(de->inode) == 0 &&
1427 le16_to_cpu(de->rec_len) >= new_rec_len)
1431 * Record might have free space at the end which we can
1434 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1435 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1441 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1442 struct ocfs2_dx_entry *dx_new_entry)
1446 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1447 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1449 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1452 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1453 struct ocfs2_dx_hinfo *hinfo,
1457 struct ocfs2_dx_entry *dx_entry;
1459 i = le16_to_cpu(entry_list->de_num_used);
1460 dx_entry = &entry_list->de_entries[i];
1462 memset(dx_entry, 0, sizeof(*dx_entry));
1463 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1464 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1465 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1467 le16_add_cpu(&entry_list->de_num_used, 1);
1470 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1471 struct ocfs2_dx_hinfo *hinfo,
1473 struct buffer_head *dx_leaf_bh)
1476 struct ocfs2_dx_leaf *dx_leaf;
1478 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
1479 OCFS2_JOURNAL_ACCESS_WRITE);
1485 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
1486 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1487 ocfs2_journal_dirty(handle, dx_leaf_bh);
1493 static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1494 struct ocfs2_dx_hinfo *hinfo,
1496 struct ocfs2_dx_root_block *dx_root)
1498 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1501 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1502 struct ocfs2_dir_lookup_result *lookup)
1505 struct ocfs2_dx_root_block *dx_root;
1506 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1508 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1509 OCFS2_JOURNAL_ACCESS_WRITE);
1515 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1516 if (ocfs2_dx_root_inline(dx_root)) {
1517 ocfs2_dx_inline_root_insert(dir, handle,
1519 lookup->dl_leaf_bh->b_blocknr,
1522 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1523 lookup->dl_leaf_bh->b_blocknr,
1524 lookup->dl_dx_leaf_bh);
1529 le32_add_cpu(&dx_root->dr_num_entries, 1);
1530 ocfs2_journal_dirty(handle, dx_root_bh);
1536 static void ocfs2_remove_block_from_free_list(struct inode *dir,
1538 struct ocfs2_dir_lookup_result *lookup)
1540 struct ocfs2_dir_block_trailer *trailer, *prev;
1541 struct ocfs2_dx_root_block *dx_root;
1542 struct buffer_head *bh;
1544 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1546 if (ocfs2_free_list_at_root(lookup)) {
1547 bh = lookup->dl_dx_root_bh;
1548 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1549 dx_root->dr_free_blk = trailer->db_free_next;
1551 bh = lookup->dl_prev_leaf_bh;
1552 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1553 prev->db_free_next = trailer->db_free_next;
1556 trailer->db_free_rec_len = cpu_to_le16(0);
1557 trailer->db_free_next = cpu_to_le64(0);
1559 ocfs2_journal_dirty(handle, bh);
1560 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1564 * This expects that a journal write has been reserved on
1565 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1567 static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1568 struct ocfs2_dir_lookup_result *lookup)
1571 struct ocfs2_dir_block_trailer *trailer;
1573 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1574 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1577 * There's still room in this block, so no need to remove it
1578 * from the free list. In this case, we just want to update
1579 * the rec len accounting.
1581 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1582 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1583 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1585 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1589 /* we don't always have a dentry for what we want to add, so people
1590 * like orphan dir can call this instead.
1592 * The lookup context must have been filled from
1593 * ocfs2_prepare_dir_for_insert.
1595 int __ocfs2_add_entry(handle_t *handle,
1597 const char *name, int namelen,
1598 struct inode *inode, u64 blkno,
1599 struct buffer_head *parent_fe_bh,
1600 struct ocfs2_dir_lookup_result *lookup)
1602 unsigned long offset;
1603 unsigned short rec_len;
1604 struct ocfs2_dir_entry *de, *de1;
1605 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1606 struct super_block *sb = dir->i_sb;
1608 unsigned int size = sb->s_blocksize;
1609 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
1610 char *data_start = insert_bh->b_data;
1615 if (ocfs2_dir_indexed(dir)) {
1616 struct buffer_head *bh;
1619 * An indexed dir may require that we update the free space
1620 * list. Reserve a write to the previous node in the list so
1621 * that we don't fail later.
1623 * XXX: This can be either a dx_root_block, or an unindexed
1624 * directory tree leaf block.
1626 if (ocfs2_free_list_at_root(lookup)) {
1627 bh = lookup->dl_dx_root_bh;
1628 retval = ocfs2_journal_access_dr(handle,
1629 INODE_CACHE(dir), bh,
1630 OCFS2_JOURNAL_ACCESS_WRITE);
1632 bh = lookup->dl_prev_leaf_bh;
1633 retval = ocfs2_journal_access_db(handle,
1634 INODE_CACHE(dir), bh,
1635 OCFS2_JOURNAL_ACCESS_WRITE);
1641 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1642 data_start = di->id2.i_data.id_data;
1643 size = i_size_read(dir);
1645 BUG_ON(insert_bh != parent_fe_bh);
1648 rec_len = OCFS2_DIR_REC_LEN(namelen);
1650 de = (struct ocfs2_dir_entry *) data_start;
1652 BUG_ON((char *)de >= (size + data_start));
1654 /* These checks should've already been passed by the
1655 * prepare function, but I guess we can leave them
1657 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1661 if (ocfs2_match(namelen, name, de)) {
1666 /* We're guaranteed that we should have space, so we
1667 * can't possibly have hit the trailer...right? */
1668 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1669 "Hit dir trailer trying to insert %.*s "
1670 "(namelen %d) into directory %llu. "
1671 "offset is %lu, trailer offset is %d\n",
1672 namelen, name, namelen,
1673 (unsigned long long)parent_fe_bh->b_blocknr,
1674 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1676 if (ocfs2_dirent_would_fit(de, rec_len)) {
1677 dir->i_mtime = dir->i_ctime = current_time(dir);
1678 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1684 if (insert_bh == parent_fe_bh)
1685 retval = ocfs2_journal_access_di(handle,
1688 OCFS2_JOURNAL_ACCESS_WRITE);
1690 retval = ocfs2_journal_access_db(handle,
1693 OCFS2_JOURNAL_ACCESS_WRITE);
1695 if (!retval && ocfs2_dir_indexed(dir))
1696 retval = ocfs2_dx_dir_insert(dir,
1706 /* By now the buffer is marked for journaling */
1707 offset += le16_to_cpu(de->rec_len);
1708 if (le64_to_cpu(de->inode)) {
1709 de1 = (struct ocfs2_dir_entry *)((char *) de +
1710 OCFS2_DIR_REC_LEN(de->name_len));
1712 cpu_to_le16(le16_to_cpu(de->rec_len) -
1713 OCFS2_DIR_REC_LEN(de->name_len));
1714 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1717 de->file_type = FT_UNKNOWN;
1719 de->inode = cpu_to_le64(blkno);
1720 ocfs2_set_de_type(de, inode->i_mode);
1723 de->name_len = namelen;
1724 memcpy(de->name, name, namelen);
1726 if (ocfs2_dir_indexed(dir))
1727 ocfs2_recalc_free_list(dir, handle, lookup);
1729 inode_inc_iversion(dir);
1730 ocfs2_journal_dirty(handle, insert_bh);
1735 offset += le16_to_cpu(de->rec_len);
1736 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1739 /* when you think about it, the assert above should prevent us
1740 * from ever getting here. */
1749 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
1751 struct dir_context *ctx)
1754 unsigned long offset = ctx->pos;
1755 struct buffer_head *di_bh = NULL;
1756 struct ocfs2_dinode *di;
1757 struct ocfs2_inline_data *data;
1758 struct ocfs2_dir_entry *de;
1760 ret = ocfs2_read_inode_block(inode, &di_bh);
1762 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1763 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1767 di = (struct ocfs2_dinode *)di_bh->b_data;
1768 data = &di->id2.i_data;
1770 while (ctx->pos < i_size_read(inode)) {
1771 /* If the dir block has changed since the last call to
1772 * readdir(2), then we might be pointing to an invalid
1773 * dirent right now. Scan from the start of the block
1775 if (!inode_eq_iversion(inode, *f_version)) {
1776 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1777 de = (struct ocfs2_dir_entry *)
1778 (data->id_data + i);
1779 /* It's too expensive to do a full
1780 * dirent test each time round this
1781 * loop, but we do have to test at
1782 * least that it is non-zero. A
1783 * failure will be detected in the
1784 * dirent test below. */
1785 if (le16_to_cpu(de->rec_len) <
1786 OCFS2_DIR_REC_LEN(1))
1788 i += le16_to_cpu(de->rec_len);
1790 ctx->pos = offset = i;
1791 *f_version = inode_query_iversion(inode);
1794 de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
1795 if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
1796 /* On error, skip the f_pos to the end. */
1797 ctx->pos = i_size_read(inode);
1800 offset += le16_to_cpu(de->rec_len);
1801 if (le64_to_cpu(de->inode)) {
1802 if (!dir_emit(ctx, de->name, de->name_len,
1803 le64_to_cpu(de->inode),
1804 fs_ftype_to_dtype(de->file_type)))
1807 ctx->pos += le16_to_cpu(de->rec_len);
1815 * NOTE: This function can be called against unindexed directories,
1818 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
1820 struct dir_context *ctx,
1823 unsigned long offset, blk, last_ra_blk = 0;
1825 struct buffer_head * bh, * tmp;
1826 struct ocfs2_dir_entry * de;
1827 struct super_block * sb = inode->i_sb;
1828 unsigned int ra_sectors = 16;
1833 offset = ctx->pos & (sb->s_blocksize - 1);
1835 while (ctx->pos < i_size_read(inode)) {
1836 blk = ctx->pos >> sb->s_blocksize_bits;
1837 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1838 /* Skip the corrupt dirblock and keep trying */
1839 ctx->pos += sb->s_blocksize - offset;
1843 /* The idea here is to begin with 8k read-ahead and to stay
1844 * 4k ahead of our current position.
1846 * TODO: Use the pagecache for this. We just need to
1847 * make sure it's cluster-safe... */
1849 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1850 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
1853 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1854 OCFS2_BH_READAHEAD))
1861 /* If the dir block has changed since the last call to
1862 * readdir(2), then we might be pointing to an invalid
1863 * dirent right now. Scan from the start of the block
1865 if (!inode_eq_iversion(inode, *f_version)) {
1866 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1867 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1868 /* It's too expensive to do a full
1869 * dirent test each time round this
1870 * loop, but we do have to test at
1871 * least that it is non-zero. A
1872 * failure will be detected in the
1873 * dirent test below. */
1874 if (le16_to_cpu(de->rec_len) <
1875 OCFS2_DIR_REC_LEN(1))
1877 i += le16_to_cpu(de->rec_len);
1880 ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
1882 *f_version = inode_query_iversion(inode);
1885 while (ctx->pos < i_size_read(inode)
1886 && offset < sb->s_blocksize) {
1887 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1888 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1889 /* On error, skip the f_pos to the
1891 ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
1894 if (le64_to_cpu(de->inode)) {
1895 if (!dir_emit(ctx, de->name,
1897 le64_to_cpu(de->inode),
1898 fs_ftype_to_dtype(de->file_type))) {
1904 offset += le16_to_cpu(de->rec_len);
1905 ctx->pos += le16_to_cpu(de->rec_len);
1910 if (!persist && stored)
1916 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1917 struct dir_context *ctx,
1920 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1921 return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
1922 return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
1926 * This is intended to be called from inside other kernel functions,
1927 * so we fake some arguments.
1929 int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
1931 u64 version = inode_query_iversion(inode);
1932 ocfs2_dir_foreach_blk(inode, &version, ctx, true);
1940 int ocfs2_readdir(struct file *file, struct dir_context *ctx)
1943 struct inode *inode = file_inode(file);
1946 trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
1948 error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
1949 if (lock_level && error >= 0) {
1950 /* We release EX lock which used to update atime
1951 * and get PR lock again to reduce contention
1952 * on commonly accessed directories. */
1953 ocfs2_inode_unlock(inode, 1);
1955 error = ocfs2_inode_lock(inode, NULL, 0);
1958 if (error != -ENOENT)
1960 /* we haven't got any yet, so propagate the error. */
1964 error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
1966 ocfs2_inode_unlock(inode, lock_level);
1976 * NOTE: this should always be called with parent dir i_mutex taken.
1978 int ocfs2_find_files_on_disk(const char *name,
1981 struct inode *inode,
1982 struct ocfs2_dir_lookup_result *lookup)
1984 int status = -ENOENT;
1986 trace_ocfs2_find_files_on_disk(namelen, name, blkno,
1987 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1989 status = ocfs2_find_entry(name, namelen, inode, lookup);
1993 *blkno = le64_to_cpu(lookup->dl_entry->inode);
2002 * Convenience function for callers which just want the block number
2003 * mapped to a name and don't require the full dirent info, etc.
2005 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2006 int namelen, u64 *blkno)
2009 struct ocfs2_dir_lookup_result lookup = { NULL, };
2011 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2012 ocfs2_free_dir_lookup_result(&lookup);
2017 /* Check for a name within a directory.
2019 * Return 0 if the name does not exist
2020 * Return -EEXIST if the directory contains the name
2022 * Callers should have i_mutex + a cluster lock on dir
2024 int ocfs2_check_dir_for_entry(struct inode *dir,
2029 struct ocfs2_dir_lookup_result lookup = { NULL, };
2031 trace_ocfs2_check_dir_for_entry(
2032 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
2034 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
2039 ocfs2_free_dir_lookup_result(&lookup);
2044 struct ocfs2_empty_dir_priv {
2045 struct dir_context ctx;
2047 unsigned seen_dot_dot;
2048 unsigned seen_other;
2051 static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
2052 int name_len, loff_t pos, u64 ino,
2055 struct ocfs2_empty_dir_priv *p =
2056 container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
2059 * Check the positions of "." and ".." records to be sure
2060 * they're in the correct place.
2062 * Indexed directories don't need to proceed past the first
2063 * two entries, so we end the scan after seeing '..'. Despite
2064 * that, we allow the scan to proceed In the event that we
2065 * have a corrupted indexed directory (no dot or dot dot
2066 * entries). This allows us to double check for existing
2067 * entries which might not have been found in the index.
2069 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2074 if (name_len == 2 && !strncmp("..", name, 2) &&
2075 pos == OCFS2_DIR_REC_LEN(1)) {
2076 p->seen_dot_dot = 1;
2078 if (p->dx_dir && p->seen_dot)
2088 static int ocfs2_empty_dir_dx(struct inode *inode,
2089 struct ocfs2_empty_dir_priv *priv)
2092 struct buffer_head *di_bh = NULL;
2093 struct buffer_head *dx_root_bh = NULL;
2094 struct ocfs2_dinode *di;
2095 struct ocfs2_dx_root_block *dx_root;
2099 ret = ocfs2_read_inode_block(inode, &di_bh);
2104 di = (struct ocfs2_dinode *)di_bh->b_data;
2106 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2111 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2113 if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2114 priv->seen_other = 1;
2123 * routine to check that the specified directory is empty (for rmdir)
2125 * Returns 1 if dir is empty, zero otherwise.
2127 * XXX: This is a performance problem for unindexed directories.
2129 int ocfs2_empty_dir(struct inode *inode)
2132 struct ocfs2_empty_dir_priv priv = {
2133 .ctx.actor = ocfs2_empty_dir_filldir,
2136 if (ocfs2_dir_indexed(inode)) {
2137 ret = ocfs2_empty_dir_dx(inode, &priv);
2141 * We still run ocfs2_dir_foreach to get the checks
2146 ret = ocfs2_dir_foreach(inode, &priv.ctx);
2150 if (!priv.seen_dot || !priv.seen_dot_dot) {
2151 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
2152 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2154 * XXX: Is it really safe to allow an unlink to continue?
2159 return !priv.seen_other;
2163 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2164 * "..", which might be used during creation of a directory with a trailing
2165 * header. It is otherwise safe to ignore the return code.
2167 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2168 struct inode *parent,
2172 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2174 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2177 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2178 strcpy(de->name, ".");
2179 ocfs2_set_de_type(de, S_IFDIR);
2181 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2182 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2183 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2185 strcpy(de->name, "..");
2186 ocfs2_set_de_type(de, S_IFDIR);
2192 * This works together with code in ocfs2_mknod_locked() which sets
2193 * the inline-data flag and initializes the inline-data section.
2195 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2197 struct inode *parent,
2198 struct inode *inode,
2199 struct buffer_head *di_bh)
2202 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2203 struct ocfs2_inline_data *data = &di->id2.i_data;
2204 unsigned int size = le16_to_cpu(data->id_count);
2206 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2207 OCFS2_JOURNAL_ACCESS_WRITE);
2213 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2214 ocfs2_journal_dirty(handle, di_bh);
2216 i_size_write(inode, size);
2217 set_nlink(inode, 2);
2218 inode->i_blocks = ocfs2_inode_sector_count(inode);
2220 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2228 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2230 struct inode *parent,
2231 struct inode *inode,
2232 struct buffer_head *fe_bh,
2233 struct ocfs2_alloc_context *data_ac,
2234 struct buffer_head **ret_new_bh)
2237 unsigned int size = osb->sb->s_blocksize;
2238 struct buffer_head *new_bh = NULL;
2239 struct ocfs2_dir_entry *de;
2241 if (ocfs2_new_dir_wants_trailer(inode))
2242 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2244 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2245 data_ac, NULL, &new_bh);
2251 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2253 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
2254 OCFS2_JOURNAL_ACCESS_CREATE);
2259 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2261 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2262 if (ocfs2_new_dir_wants_trailer(inode)) {
2263 int size = le16_to_cpu(de->rec_len);
2266 * Figure out the size of the hole left over after
2267 * insertion of '.' and '..'. The trailer wants this
2270 size -= OCFS2_DIR_REC_LEN(2);
2271 size -= sizeof(struct ocfs2_dir_block_trailer);
2273 ocfs2_init_dir_trailer(inode, new_bh, size);
2276 ocfs2_journal_dirty(handle, new_bh);
2278 i_size_write(inode, inode->i_sb->s_blocksize);
2279 set_nlink(inode, 2);
2280 inode->i_blocks = ocfs2_inode_sector_count(inode);
2281 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2289 *ret_new_bh = new_bh;
2298 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2299 handle_t *handle, struct inode *dir,
2300 struct buffer_head *di_bh,
2301 struct buffer_head *dirdata_bh,
2302 struct ocfs2_alloc_context *meta_ac,
2303 int dx_inline, u32 num_entries,
2304 struct buffer_head **ret_dx_root_bh)
2307 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2308 u16 dr_suballoc_bit;
2309 u64 suballoc_loc, dr_blkno;
2310 unsigned int num_bits;
2311 struct buffer_head *dx_root_bh = NULL;
2312 struct ocfs2_dx_root_block *dx_root;
2313 struct ocfs2_dir_block_trailer *trailer =
2314 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
2316 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2317 &dr_suballoc_bit, &num_bits, &dr_blkno);
2323 trace_ocfs2_dx_dir_attach_index(
2324 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2325 (unsigned long long)dr_blkno);
2327 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2328 if (dx_root_bh == NULL) {
2332 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
2334 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
2335 OCFS2_JOURNAL_ACCESS_CREATE);
2341 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2342 memset(dx_root, 0, osb->sb->s_blocksize);
2343 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2344 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2345 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
2346 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2347 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2348 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2349 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
2350 dx_root->dr_num_entries = cpu_to_le32(num_entries);
2351 if (le16_to_cpu(trailer->db_free_rec_len))
2352 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2354 dx_root->dr_free_blk = cpu_to_le64(0);
2357 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2358 dx_root->dr_entries.de_count =
2359 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2361 dx_root->dr_list.l_count =
2362 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2364 ocfs2_journal_dirty(handle, dx_root_bh);
2366 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
2367 OCFS2_JOURNAL_ACCESS_CREATE);
2373 di->i_dx_root = cpu_to_le64(dr_blkno);
2375 spin_lock(&OCFS2_I(dir)->ip_lock);
2376 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2377 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2378 spin_unlock(&OCFS2_I(dir)->ip_lock);
2380 ocfs2_journal_dirty(handle, di_bh);
2382 *ret_dx_root_bh = dx_root_bh;
2390 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2391 handle_t *handle, struct inode *dir,
2392 struct buffer_head **dx_leaves,
2393 int num_dx_leaves, u64 start_blk)
2396 struct ocfs2_dx_leaf *dx_leaf;
2397 struct buffer_head *bh;
2399 for (i = 0; i < num_dx_leaves; i++) {
2400 bh = sb_getblk(osb->sb, start_blk + i);
2407 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
2409 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
2410 OCFS2_JOURNAL_ACCESS_CREATE);
2416 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2418 memset(dx_leaf, 0, osb->sb->s_blocksize);
2419 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2420 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2421 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2422 dx_leaf->dl_list.de_count =
2423 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2425 trace_ocfs2_dx_dir_format_cluster(
2426 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2427 (unsigned long long)bh->b_blocknr,
2428 le16_to_cpu(dx_leaf->dl_list.de_count));
2430 ocfs2_journal_dirty(handle, bh);
2439 * Allocates and formats a new cluster for use in an indexed dir
2440 * leaf. This version will not do the extent insert, so that it can be
2441 * used by operations which need careful ordering.
2443 static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2444 u32 cpos, handle_t *handle,
2445 struct ocfs2_alloc_context *data_ac,
2446 struct buffer_head **dx_leaves,
2447 int num_dx_leaves, u64 *ret_phys_blkno)
2452 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2455 * XXX: For create, this should claim cluster for the index
2456 * *before* the unindexed insert so that we have a better
2457 * chance of contiguousness as the directory grows in number
2460 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
2467 * Format the new cluster first. That way, we're inserting
2470 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2471 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2472 num_dx_leaves, phys_blkno);
2478 *ret_phys_blkno = phys_blkno;
2483 static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2484 struct ocfs2_extent_tree *et,
2485 u32 cpos, handle_t *handle,
2486 struct ocfs2_alloc_context *data_ac,
2487 struct ocfs2_alloc_context *meta_ac,
2488 struct buffer_head **dx_leaves,
2494 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2495 num_dx_leaves, &phys_blkno);
2501 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
2509 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2510 int *ret_num_leaves)
2512 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2513 struct buffer_head **dx_leaves;
2515 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2517 if (dx_leaves && ret_num_leaves)
2518 *ret_num_leaves = num_dx_leaves;
2523 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2525 struct inode *parent,
2526 struct inode *inode,
2527 struct buffer_head *di_bh,
2528 struct ocfs2_alloc_context *data_ac,
2529 struct ocfs2_alloc_context *meta_ac)
2532 struct buffer_head *leaf_bh = NULL;
2533 struct buffer_head *dx_root_bh = NULL;
2534 struct ocfs2_dx_hinfo hinfo;
2535 struct ocfs2_dx_root_block *dx_root;
2536 struct ocfs2_dx_entry_list *entry_list;
2539 * Our strategy is to create the directory as though it were
2540 * unindexed, then add the index block. This works with very
2541 * little complication since the state of a new directory is a
2542 * very well known quantity.
2544 * Essentially, we have two dirents ("." and ".."), in the 1st
2545 * block which need indexing. These are easily inserted into
2549 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2556 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
2557 meta_ac, 1, 2, &dx_root_bh);
2562 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2563 entry_list = &dx_root->dr_entries;
2565 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2566 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
2567 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2569 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
2570 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2578 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2580 struct inode *parent,
2581 struct inode *inode,
2582 struct buffer_head *fe_bh,
2583 struct ocfs2_alloc_context *data_ac,
2584 struct ocfs2_alloc_context *meta_ac)
2587 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2589 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2590 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2592 if (ocfs2_supports_indexed_dirs(osb))
2593 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2596 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2600 static int ocfs2_dx_dir_index_block(struct inode *dir,
2602 struct buffer_head **dx_leaves,
2604 u32 *num_dx_entries,
2605 struct buffer_head *dirent_bh)
2607 int ret = 0, namelen, i;
2608 char *de_buf, *limit;
2609 struct ocfs2_dir_entry *de;
2610 struct buffer_head *dx_leaf_bh;
2611 struct ocfs2_dx_hinfo hinfo;
2612 u64 dirent_blk = dirent_bh->b_blocknr;
2614 de_buf = dirent_bh->b_data;
2615 limit = de_buf + dir->i_sb->s_blocksize;
2617 while (de_buf < limit) {
2618 de = (struct ocfs2_dir_entry *)de_buf;
2620 namelen = de->name_len;
2621 if (!namelen || !de->inode)
2624 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2626 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2627 dx_leaf_bh = dx_leaves[i];
2629 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2630 dirent_blk, dx_leaf_bh);
2636 *num_dx_entries = *num_dx_entries + 1;
2639 de_buf += le16_to_cpu(de->rec_len);
2647 * XXX: This expects dx_root_bh to already be part of the transaction.
2649 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2650 struct buffer_head *dx_root_bh,
2651 struct buffer_head *dirent_bh)
2653 char *de_buf, *limit;
2654 struct ocfs2_dx_root_block *dx_root;
2655 struct ocfs2_dir_entry *de;
2656 struct ocfs2_dx_hinfo hinfo;
2657 u64 dirent_blk = dirent_bh->b_blocknr;
2659 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2661 de_buf = dirent_bh->b_data;
2662 limit = de_buf + dir->i_sb->s_blocksize;
2664 while (de_buf < limit) {
2665 de = (struct ocfs2_dir_entry *)de_buf;
2667 if (!de->name_len || !de->inode)
2670 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2672 trace_ocfs2_dx_dir_index_root_block(
2673 (unsigned long long)dir->i_ino,
2674 hinfo.major_hash, hinfo.minor_hash,
2675 de->name_len, de->name,
2676 le16_to_cpu(dx_root->dr_entries.de_num_used));
2678 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2681 le32_add_cpu(&dx_root->dr_num_entries, 1);
2683 de_buf += le16_to_cpu(de->rec_len);
2688 * Count the number of inline directory entries in di_bh and compare
2689 * them against the number of entries we can hold in an inline dx root
2692 static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2693 struct buffer_head *di_bh)
2695 int dirent_count = 0;
2696 char *de_buf, *limit;
2697 struct ocfs2_dir_entry *de;
2698 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2700 de_buf = di->id2.i_data.id_data;
2701 limit = de_buf + i_size_read(dir);
2703 while (de_buf < limit) {
2704 de = (struct ocfs2_dir_entry *)de_buf;
2706 if (de->name_len && de->inode)
2709 de_buf += le16_to_cpu(de->rec_len);
2712 /* We are careful to leave room for one extra record. */
2713 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2717 * Expand rec_len of the rightmost dirent in a directory block so that it
2718 * contains the end of our valid space for dirents. We do this during
2719 * expansion from an inline directory to one with extents. The first dir block
2720 * in that case is taken from the inline data portion of the inode block.
2722 * This will also return the largest amount of contiguous space for a dirent
2723 * in the block. That value is *not* necessarily the last dirent, even after
2724 * expansion. The directory indexing code wants this value for free space
2725 * accounting. We do this here since we're already walking the entire dir
2728 * We add the dir trailer if this filesystem wants it.
2730 static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2733 struct super_block *sb = dir->i_sb;
2734 struct ocfs2_dir_entry *de;
2735 struct ocfs2_dir_entry *prev_de;
2736 char *de_buf, *limit;
2737 unsigned int new_size = sb->s_blocksize;
2738 unsigned int bytes, this_hole;
2739 unsigned int largest_hole = 0;
2741 if (ocfs2_new_dir_wants_trailer(dir))
2742 new_size = ocfs2_dir_trailer_blk_off(sb);
2744 bytes = new_size - old_size;
2746 limit = start + old_size;
2748 de = (struct ocfs2_dir_entry *)de_buf;
2750 this_hole = ocfs2_figure_dirent_hole(de);
2751 if (this_hole > largest_hole)
2752 largest_hole = this_hole;
2755 de_buf += le16_to_cpu(de->rec_len);
2756 de = (struct ocfs2_dir_entry *)de_buf;
2757 } while (de_buf < limit);
2759 le16_add_cpu(&prev_de->rec_len, bytes);
2761 /* We need to double check this after modification of the final
2763 this_hole = ocfs2_figure_dirent_hole(prev_de);
2764 if (this_hole > largest_hole)
2765 largest_hole = this_hole;
2767 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2768 return largest_hole;
2773 * We allocate enough clusters to fulfill "blocks_wanted", but set
2774 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2775 * rest automatically for us.
2777 * *first_block_bh is a pointer to the 1st data block allocated to the
2780 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2781 unsigned int blocks_wanted,
2782 struct ocfs2_dir_lookup_result *lookup,
2783 struct buffer_head **first_block_bh)
2785 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
2786 struct super_block *sb = dir->i_sb;
2787 int ret, i, num_dx_leaves = 0, dx_inline = 0,
2788 credits = ocfs2_inline_to_extents_credits(sb);
2789 u64 dx_insert_blkno, blkno,
2790 bytes = blocks_wanted << sb->s_blocksize_bits;
2791 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2792 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2793 struct ocfs2_alloc_context *data_ac = NULL;
2794 struct ocfs2_alloc_context *meta_ac = NULL;
2795 struct buffer_head *dirdata_bh = NULL;
2796 struct buffer_head *dx_root_bh = NULL;
2797 struct buffer_head **dx_leaves = NULL;
2798 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2800 struct ocfs2_extent_tree et;
2801 struct ocfs2_extent_tree dx_et;
2802 int did_quota = 0, bytes_allocated = 0;
2804 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
2806 alloc = ocfs2_clusters_for_bytes(sb, bytes);
2809 down_write(&oi->ip_alloc_sem);
2811 if (ocfs2_supports_indexed_dirs(osb)) {
2812 credits += ocfs2_add_dir_index_credits(sb);
2814 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2816 /* Add one more cluster for an index leaf */
2818 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2827 /* This gets us the dx_root */
2828 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2836 * We should never need more than 2 clusters for the unindexed
2837 * tree - maximum dirent size is far less than one block. In
2838 * fact, the only time we'd need more than one cluster is if
2839 * blocksize == clustersize and the dirent won't fit in the
2840 * extra space that the expansion to a single block gives. As
2841 * of today, that only happens on 4k/4k file systems.
2845 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
2852 * Prepare for worst case allocation scenario of two separate
2853 * extents in the unindexed tree.
2856 credits += OCFS2_SUBALLOC_ALLOC;
2858 handle = ocfs2_start_trans(osb, credits);
2859 if (IS_ERR(handle)) {
2860 ret = PTR_ERR(handle);
2865 ret = dquot_alloc_space_nodirty(dir,
2866 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2871 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2873 * Allocate our index cluster first, to maximize the
2874 * possibility that unindexed leaves grow
2877 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2878 dx_leaves, num_dx_leaves,
2884 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2888 * Try to claim as many clusters as the bitmap can give though
2889 * if we only get one now, that's enough to continue. The rest
2890 * will be claimed after the conversion to extents.
2892 if (ocfs2_dir_resv_allowed(osb))
2893 data_ac->ac_resv = &oi->ip_la_data_resv;
2894 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
2899 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2902 * Operations are carefully ordered so that we set up the new
2903 * data block first. The conversion from inline data to
2906 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2907 dirdata_bh = sb_getblk(sb, blkno);
2914 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
2916 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
2917 OCFS2_JOURNAL_ACCESS_CREATE);
2923 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2924 memset(dirdata_bh->b_data + i_size_read(dir), 0,
2925 sb->s_blocksize - i_size_read(dir));
2926 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
2927 if (ocfs2_new_dir_wants_trailer(dir)) {
2929 * Prepare the dir trailer up front. It will otherwise look
2930 * like a valid dirent. Even if inserting the index fails
2931 * (unlikely), then all we'll have done is given first dir
2932 * block a small amount of fragmentation.
2934 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
2937 ocfs2_update_inode_fsync_trans(handle, dir, 1);
2938 ocfs2_journal_dirty(handle, dirdata_bh);
2940 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2942 * Dx dirs with an external cluster need to do this up
2943 * front. Inline dx root's get handled later, after
2944 * we've allocated our root block. We get passed back
2945 * a total number of items so that dr_num_entries can
2946 * be correctly set once the dx_root has been
2949 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
2950 num_dx_leaves, &num_dx_entries,
2959 * Set extent, i_size, etc on the directory. After this, the
2960 * inode should contain the same exact dirents as before and
2961 * be fully accessible from system calls.
2963 * We let the later dirent insert modify c/mtime - to the user
2964 * the data hasn't changed.
2966 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
2967 OCFS2_JOURNAL_ACCESS_CREATE);
2973 spin_lock(&oi->ip_lock);
2974 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
2975 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2976 spin_unlock(&oi->ip_lock);
2978 ocfs2_dinode_new_extent_list(dir, di);
2980 i_size_write(dir, sb->s_blocksize);
2981 dir->i_mtime = dir->i_ctime = current_time(dir);
2983 di->i_size = cpu_to_le64(sb->s_blocksize);
2984 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
2985 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
2986 ocfs2_update_inode_fsync_trans(handle, dir, 1);
2989 * This should never fail as our extent list is empty and all
2990 * related blocks have been journaled already.
2992 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
3000 * Set i_blocks after the extent insert for the most up to
3001 * date ip_clusters value.
3003 dir->i_blocks = ocfs2_inode_sector_count(dir);
3005 ocfs2_journal_dirty(handle, di_bh);
3007 if (ocfs2_supports_indexed_dirs(osb)) {
3008 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
3009 dirdata_bh, meta_ac, dx_inline,
3010 num_dx_entries, &dx_root_bh);
3017 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3020 ocfs2_init_dx_root_extent_tree(&dx_et,
3023 ret = ocfs2_insert_extent(handle, &dx_et, 0,
3024 dx_insert_blkno, 1, 0, NULL);
3031 * We asked for two clusters, but only got one in the 1st
3032 * pass. Claim the 2nd cluster as a separate extent.
3035 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
3041 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3043 ret = ocfs2_insert_extent(handle, &et, 1,
3044 blkno, len, 0, NULL);
3049 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
3052 *first_block_bh = dirdata_bh;
3054 if (ocfs2_supports_indexed_dirs(osb)) {
3059 * We need to return the correct block within the
3060 * cluster which should hold our entry.
3062 off = ocfs2_dx_dir_hash_idx(osb,
3064 get_bh(dx_leaves[off]);
3065 lookup->dl_dx_leaf_bh = dx_leaves[off];
3067 lookup->dl_dx_root_bh = dx_root_bh;
3072 if (ret < 0 && did_quota)
3073 dquot_free_space_nodirty(dir, bytes_allocated);
3075 ocfs2_commit_trans(osb, handle);
3078 up_write(&oi->ip_alloc_sem);
3080 ocfs2_free_alloc_context(data_ac);
3082 ocfs2_free_alloc_context(meta_ac);
3085 for (i = 0; i < num_dx_leaves; i++)
3086 brelse(dx_leaves[i]);
3096 /* returns a bh of the 1st new block in the allocation. */
3097 static int ocfs2_do_extend_dir(struct super_block *sb,
3100 struct buffer_head *parent_fe_bh,
3101 struct ocfs2_alloc_context *data_ac,
3102 struct ocfs2_alloc_context *meta_ac,
3103 struct buffer_head **new_bh)
3106 int extend, did_quota = 0;
3107 u64 p_blkno, v_blkno;
3109 spin_lock(&OCFS2_I(dir)->ip_lock);
3110 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3111 spin_unlock(&OCFS2_I(dir)->ip_lock);
3114 u32 offset = OCFS2_I(dir)->ip_clusters;
3116 status = dquot_alloc_space_nodirty(dir,
3117 ocfs2_clusters_to_bytes(sb, 1));
3122 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3123 1, 0, parent_fe_bh, handle,
3124 data_ac, meta_ac, NULL);
3125 BUG_ON(status == -EAGAIN);
3132 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3133 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
3139 *new_bh = sb_getblk(sb, p_blkno);
3147 if (did_quota && status < 0)
3148 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
3153 * Assumes you already have a cluster lock on the directory.
3155 * 'blocks_wanted' is only used if we have an inline directory which
3156 * is to be turned into an extent based one. The size of the dirent to
3157 * insert might be larger than the space gained by growing to just one
3158 * block, so we may have to grow the inode by two blocks in that case.
3160 * If the directory is already indexed, dx_root_bh must be provided.
3162 static int ocfs2_extend_dir(struct ocfs2_super *osb,
3164 struct buffer_head *parent_fe_bh,
3165 unsigned int blocks_wanted,
3166 struct ocfs2_dir_lookup_result *lookup,
3167 struct buffer_head **new_de_bh)
3170 int credits, num_free_extents, drop_alloc_sem = 0;
3172 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
3173 struct ocfs2_extent_list *el = &fe->id2.i_list;
3174 struct ocfs2_alloc_context *data_ac = NULL;
3175 struct ocfs2_alloc_context *meta_ac = NULL;
3176 handle_t *handle = NULL;
3177 struct buffer_head *new_bh = NULL;
3178 struct ocfs2_dir_entry * de;
3179 struct super_block *sb = osb->sb;
3180 struct ocfs2_extent_tree et;
3181 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
3183 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
3185 * This would be a code error as an inline directory should
3186 * never have an index root.
3190 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
3191 blocks_wanted, lookup,
3198 /* Expansion from inline to an indexed directory will
3199 * have given us this. */
3200 dx_root_bh = lookup->dl_dx_root_bh;
3202 if (blocks_wanted == 1) {
3204 * If the new dirent will fit inside the space
3205 * created by pushing out to one block, then
3206 * we can complete the operation
3207 * here. Otherwise we have to expand i_size
3208 * and format the 2nd block below.
3210 BUG_ON(new_bh == NULL);
3215 * Get rid of 'new_bh' - we want to format the 2nd
3216 * data block and return that instead.
3221 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3223 dir_i_size = i_size_read(dir);
3224 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3228 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3230 dir_i_size = i_size_read(dir);
3231 trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
3234 /* dir->i_size is always block aligned. */
3235 spin_lock(&OCFS2_I(dir)->ip_lock);
3236 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3237 spin_unlock(&OCFS2_I(dir)->ip_lock);
3238 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3240 num_free_extents = ocfs2_num_free_extents(&et);
3241 if (num_free_extents < 0) {
3242 status = num_free_extents;
3247 if (!num_free_extents) {
3248 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
3250 if (status != -ENOSPC)
3256 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
3258 if (status != -ENOSPC)
3263 if (ocfs2_dir_resv_allowed(osb))
3264 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
3266 credits = ocfs2_calc_extend_credits(sb, el);
3268 spin_unlock(&OCFS2_I(dir)->ip_lock);
3269 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3273 if (ocfs2_dir_indexed(dir))
3274 credits++; /* For attaching the new dirent block to the
3277 handle = ocfs2_start_trans(osb, credits);
3278 if (IS_ERR(handle)) {
3279 status = PTR_ERR(handle);
3285 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3286 data_ac, meta_ac, &new_bh);
3292 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
3294 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
3295 OCFS2_JOURNAL_ACCESS_CREATE);
3300 memset(new_bh->b_data, 0, sb->s_blocksize);
3302 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3304 if (ocfs2_supports_dir_trailer(dir)) {
3305 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
3307 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3309 if (ocfs2_dir_indexed(dir)) {
3310 status = ocfs2_dx_dir_link_trailer(dir, handle,
3311 dx_root_bh, new_bh);
3318 de->rec_len = cpu_to_le16(sb->s_blocksize);
3320 ocfs2_update_inode_fsync_trans(handle, dir, 1);
3321 ocfs2_journal_dirty(handle, new_bh);
3323 dir_i_size += dir->i_sb->s_blocksize;
3324 i_size_write(dir, dir_i_size);
3325 dir->i_blocks = ocfs2_inode_sector_count(dir);
3326 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3333 *new_de_bh = new_bh;
3337 ocfs2_commit_trans(osb, handle);
3339 up_write(&OCFS2_I(dir)->ip_alloc_sem);
3342 ocfs2_free_alloc_context(data_ac);
3344 ocfs2_free_alloc_context(meta_ac);
3351 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3352 const char *name, int namelen,
3353 struct buffer_head **ret_de_bh,
3354 unsigned int *blocks_wanted)
3357 struct super_block *sb = dir->i_sb;
3358 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3359 struct ocfs2_dir_entry *de, *last_de = NULL;
3360 char *de_buf, *limit;
3361 unsigned long offset = 0;
3362 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3365 * This calculates how many free bytes we'd have in block zero, should
3366 * this function force expansion to an extent tree.
3368 if (ocfs2_new_dir_wants_trailer(dir))
3369 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3371 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
3373 de_buf = di->id2.i_data.id_data;
3374 limit = de_buf + i_size_read(dir);
3375 rec_len = OCFS2_DIR_REC_LEN(namelen);
3377 while (de_buf < limit) {
3378 de = (struct ocfs2_dir_entry *)de_buf;
3380 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3384 if (ocfs2_match(namelen, name, de)) {
3389 * No need to check for a trailing dirent record here as
3390 * they're not used for inline dirs.
3393 if (ocfs2_dirent_would_fit(de, rec_len)) {
3394 /* Ok, we found a spot. Return this bh and let
3395 * the caller actually fill it in. */
3403 de_buf += le16_to_cpu(de->rec_len);
3404 offset += le16_to_cpu(de->rec_len);
3408 * We're going to require expansion of the directory - figure
3409 * out how many blocks we'll need so that a place for the
3410 * dirent can be found.
3413 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
3414 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3422 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3423 int namelen, struct buffer_head **ret_de_bh)
3425 unsigned long offset;
3426 struct buffer_head *bh = NULL;
3427 unsigned short rec_len;
3428 struct ocfs2_dir_entry *de;
3429 struct super_block *sb = dir->i_sb;
3431 int blocksize = dir->i_sb->s_blocksize;
3433 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3437 rec_len = OCFS2_DIR_REC_LEN(namelen);
3439 de = (struct ocfs2_dir_entry *) bh->b_data;
3441 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3445 if (i_size_read(dir) <= offset) {
3447 * Caller will have to expand this
3453 status = ocfs2_read_dir_block(dir,
3454 offset >> sb->s_blocksize_bits,
3459 /* move to next block */
3460 de = (struct ocfs2_dir_entry *) bh->b_data;
3462 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3466 if (ocfs2_match(namelen, name, de)) {
3471 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3475 if (ocfs2_dirent_would_fit(de, rec_len)) {
3476 /* Ok, we found a spot. Return this bh and let
3477 * the caller actually fill it in. */
3484 offset += le16_to_cpu(de->rec_len);
3485 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3496 static int dx_leaf_sort_cmp(const void *a, const void *b)
3498 const struct ocfs2_dx_entry *entry1 = a;
3499 const struct ocfs2_dx_entry *entry2 = b;
3500 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3501 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3502 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3503 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3505 if (major_hash1 > major_hash2)
3507 if (major_hash1 < major_hash2)
3511 * It is not strictly necessary to sort by minor
3513 if (minor_hash1 > minor_hash2)
3515 if (minor_hash1 < minor_hash2)
3520 static void dx_leaf_sort_swap(void *a, void *b, int size)
3522 struct ocfs2_dx_entry *entry1 = a;
3523 struct ocfs2_dx_entry *entry2 = b;
3525 BUG_ON(size != sizeof(*entry1));
3527 swap(*entry1, *entry2);
3530 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3532 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3533 int i, num = le16_to_cpu(dl_list->de_num_used);
3535 for (i = 0; i < (num - 1); i++) {
3536 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3537 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3545 * Find the optimal value to split this leaf on. This expects the leaf
3546 * entries to be in sorted order.
3548 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3549 * the hash we want to insert.
3551 * This function is only concerned with the major hash - that which
3552 * determines which cluster an item belongs to.
3554 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3555 u32 leaf_cpos, u32 insert_hash,
3558 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3559 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3563 * There's a couple rare, but nasty corner cases we have to
3564 * check for here. All of them involve a leaf where all value
3565 * have the same hash, which is what we look for first.
3567 * Most of the time, all of the above is false, and we simply
3568 * pick the median value for a split.
3570 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3572 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3574 if (val == insert_hash) {
3576 * No matter where we would choose to split,
3577 * the new entry would want to occupy the same
3578 * block as these. Since there's no space left
3579 * in their existing block, we know there
3580 * won't be space after the split.
3585 if (val == leaf_cpos) {
3587 * Because val is the same as leaf_cpos (which
3588 * is the smallest value this leaf can have),
3589 * yet is not equal to insert_hash, then we
3590 * know that insert_hash *must* be larger than
3591 * val (and leaf_cpos). At least cpos+1 in value.
3593 * We also know then, that there cannot be an
3594 * adjacent extent (otherwise we'd be looking
3595 * at it). Choosing this value gives us a
3596 * chance to get some contiguousness.
3598 *split_hash = leaf_cpos + 1;
3602 if (val > insert_hash) {
3604 * val can not be the same as insert hash, and
3605 * also must be larger than leaf_cpos. Also,
3606 * we know that there can't be a leaf between
3607 * cpos and val, otherwise the entries with
3608 * hash 'val' would be there.
3614 *split_hash = insert_hash;
3619 * Since the records are sorted and the checks above
3620 * guaranteed that not all records in this block are the same,
3621 * we simple travel forward, from the median, and pick the 1st
3622 * record whose value is larger than leaf_cpos.
3624 for (i = (num_used / 2); i < num_used; i++)
3625 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3629 BUG_ON(i == num_used); /* Should be impossible */
3630 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3635 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3636 * larger than split_hash into new_dx_leaves. We use a temporary
3637 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3639 * Since the block offset inside a leaf (cluster) is a constant mask
3640 * of minor_hash, we can optimize - an item at block offset X within
3641 * the original cluster, will be at offset X within the new cluster.
3643 static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3645 struct ocfs2_dx_leaf *tmp_dx_leaf,
3646 struct buffer_head **orig_dx_leaves,
3647 struct buffer_head **new_dx_leaves,
3652 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3653 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3654 struct ocfs2_dx_entry *dx_entry;
3656 tmp_list = &tmp_dx_leaf->dl_list;
3658 for (i = 0; i < num_dx_leaves; i++) {
3659 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3660 orig_list = &orig_dx_leaf->dl_list;
3661 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3662 new_list = &new_dx_leaf->dl_list;
3664 num_used = le16_to_cpu(orig_list->de_num_used);
3666 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3667 tmp_list->de_num_used = cpu_to_le16(0);
3668 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3670 for (j = 0; j < num_used; j++) {
3671 dx_entry = &orig_list->de_entries[j];
3672 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3673 if (major_hash >= split_hash)
3674 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3677 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3680 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3682 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3683 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3687 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3688 struct ocfs2_dx_root_block *dx_root)
3690 int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
3692 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
3693 credits += ocfs2_quota_trans_credits(osb->sb);
3698 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3699 * half our entries into.
3701 static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3702 struct buffer_head *dx_root_bh,
3703 struct buffer_head *dx_leaf_bh,
3704 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3707 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3708 int credits, ret, i, num_used, did_quota = 0;
3709 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3710 u64 orig_leaves_start;
3712 struct buffer_head **orig_dx_leaves = NULL;
3713 struct buffer_head **new_dx_leaves = NULL;
3714 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3715 struct ocfs2_extent_tree et;
3716 handle_t *handle = NULL;
3717 struct ocfs2_dx_root_block *dx_root;
3718 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3720 trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
3721 (unsigned long long)leaf_blkno,
3724 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
3726 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3728 * XXX: This is a rather large limit. We should use a more
3731 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3734 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3735 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3736 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3737 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3738 (unsigned long long)leaf_blkno, num_used);
3743 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3744 if (!orig_dx_leaves) {
3750 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3751 if (!new_dx_leaves) {
3757 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3764 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3765 handle = ocfs2_start_trans(osb, credits);
3766 if (IS_ERR(handle)) {
3767 ret = PTR_ERR(handle);
3773 ret = dquot_alloc_space_nodirty(dir,
3774 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3779 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
3780 OCFS2_JOURNAL_ACCESS_WRITE);
3787 * This block is changing anyway, so we can sort it in place.
3789 sort(dx_leaf->dl_list.de_entries, num_used,
3790 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3793 ocfs2_journal_dirty(handle, dx_leaf_bh);
3795 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3802 trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
3805 * We have to carefully order operations here. There are items
3806 * which want to be in the new cluster before insert, but in
3807 * order to put those items in the new cluster, we alter the
3808 * old cluster. A failure to insert gets nasty.
3810 * So, start by reserving writes to the old
3811 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3812 * the new cluster for us, before inserting it. The insert
3813 * won't happen if there's an error before that. Once the
3814 * insert is done then, we can transfer from one leaf into the
3815 * other without fear of hitting any error.
3819 * The leaf transfer wants some scratch space so that we don't
3820 * wind up doing a bunch of expensive memmove().
3822 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3829 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
3830 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3838 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3839 data_ac, meta_ac, new_dx_leaves,
3846 for (i = 0; i < num_dx_leaves; i++) {
3847 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3849 OCFS2_JOURNAL_ACCESS_WRITE);
3855 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3857 OCFS2_JOURNAL_ACCESS_WRITE);
3864 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3865 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3868 if (ret < 0 && did_quota)
3869 dquot_free_space_nodirty(dir,
3870 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3872 ocfs2_update_inode_fsync_trans(handle, dir, 1);
3873 ocfs2_commit_trans(osb, handle);
3876 if (orig_dx_leaves || new_dx_leaves) {
3877 for (i = 0; i < num_dx_leaves; i++) {
3879 brelse(orig_dx_leaves[i]);
3881 brelse(new_dx_leaves[i]);
3883 kfree(orig_dx_leaves);
3884 kfree(new_dx_leaves);
3888 ocfs2_free_alloc_context(meta_ac);
3890 ocfs2_free_alloc_context(data_ac);
3896 static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3897 struct buffer_head *di_bh,
3898 struct buffer_head *dx_root_bh,
3899 const char *name, int namelen,
3900 struct ocfs2_dir_lookup_result *lookup)
3902 int ret, rebalanced = 0;
3903 struct ocfs2_dx_root_block *dx_root;
3904 struct buffer_head *dx_leaf_bh = NULL;
3905 struct ocfs2_dx_leaf *dx_leaf;
3909 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3912 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3913 &leaf_cpos, &blkno);
3919 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3925 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3927 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3928 le16_to_cpu(dx_leaf->dl_list.de_count)) {
3931 * Rebalancing should have provided us with
3932 * space in an appropriate leaf.
3934 * XXX: Is this an abnormal condition then?
3935 * Should we print a message here?
3941 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3942 &lookup->dl_hinfo, leaf_cpos,
3951 * Restart the lookup. The rebalance might have
3952 * changed which block our item fits into. Mark our
3953 * progress, so we only execute this once.
3958 goto restart_search;
3961 lookup->dl_dx_leaf_bh = dx_leaf_bh;
3969 static int ocfs2_search_dx_free_list(struct inode *dir,
3970 struct buffer_head *dx_root_bh,
3972 struct ocfs2_dir_lookup_result *lookup)
3975 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
3976 struct ocfs2_dir_block_trailer *db;
3978 int rec_len = OCFS2_DIR_REC_LEN(namelen);
3979 struct ocfs2_dx_root_block *dx_root;
3981 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3982 next_block = le64_to_cpu(dx_root->dr_free_blk);
3984 while (next_block) {
3985 brelse(prev_leaf_bh);
3986 prev_leaf_bh = leaf_bh;
3989 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
3995 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
3996 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
3997 lookup->dl_leaf_bh = leaf_bh;
3998 lookup->dl_prev_leaf_bh = prev_leaf_bh;
4000 prev_leaf_bh = NULL;
4004 next_block = le64_to_cpu(db->db_free_next);
4013 brelse(prev_leaf_bh);
4017 static int ocfs2_expand_inline_dx_root(struct inode *dir,
4018 struct buffer_head *dx_root_bh)
4020 int ret, num_dx_leaves, i, j, did_quota = 0;
4021 struct buffer_head **dx_leaves = NULL;
4022 struct ocfs2_extent_tree et;
4024 struct ocfs2_alloc_context *data_ac = NULL;
4025 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4026 handle_t *handle = NULL;
4027 struct ocfs2_dx_root_block *dx_root;
4028 struct ocfs2_dx_entry_list *entry_list;
4029 struct ocfs2_dx_entry *dx_entry;
4030 struct ocfs2_dx_leaf *target_leaf;
4032 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4038 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4045 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4046 if (IS_ERR(handle)) {
4047 ret = PTR_ERR(handle);
4052 ret = dquot_alloc_space_nodirty(dir,
4053 ocfs2_clusters_to_bytes(osb->sb, 1));
4059 * We do this up front, before the allocation, so that a
4060 * failure to add the dx_root_bh to the journal won't result
4061 * us losing clusters.
4063 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4064 OCFS2_JOURNAL_ACCESS_WRITE);
4070 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4071 num_dx_leaves, &insert_blkno);
4078 * Transfer the entries from our dx_root into the appropriate
4081 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4082 entry_list = &dx_root->dr_entries;
4084 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4085 dx_entry = &entry_list->de_entries[i];
4087 j = __ocfs2_dx_dir_hash_idx(osb,
4088 le32_to_cpu(dx_entry->dx_minor_hash));
4089 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4091 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4093 /* Each leaf has been passed to the journal already
4094 * via __ocfs2_dx_dir_new_cluster() */
4097 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4098 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
4099 offsetof(struct ocfs2_dx_root_block, dr_list));
4100 dx_root->dr_list.l_count =
4101 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4103 /* This should never fail considering we start with an empty
4105 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4106 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4111 ocfs2_update_inode_fsync_trans(handle, dir, 1);
4112 ocfs2_journal_dirty(handle, dx_root_bh);
4115 if (ret < 0 && did_quota)
4116 dquot_free_space_nodirty(dir,
4117 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4119 ocfs2_commit_trans(osb, handle);
4123 ocfs2_free_alloc_context(data_ac);
4126 for (i = 0; i < num_dx_leaves; i++)
4127 brelse(dx_leaves[i]);
4133 static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4135 struct ocfs2_dx_root_block *dx_root;
4136 struct ocfs2_dx_entry_list *entry_list;
4138 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4139 entry_list = &dx_root->dr_entries;
4141 if (le16_to_cpu(entry_list->de_num_used) >=
4142 le16_to_cpu(entry_list->de_count))
4148 static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4149 struct buffer_head *di_bh,
4152 struct ocfs2_dir_lookup_result *lookup)
4154 int ret, free_dx_root = 1;
4155 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4156 struct buffer_head *dx_root_bh = NULL;
4157 struct buffer_head *leaf_bh = NULL;
4158 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4159 struct ocfs2_dx_root_block *dx_root;
4161 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4167 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4168 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4174 if (ocfs2_dx_root_inline(dx_root)) {
4175 ret = ocfs2_inline_dx_has_space(dx_root_bh);
4181 * We ran out of room in the root block. Expand it to
4182 * an extent, then allow ocfs2_find_dir_space_dx to do
4185 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4193 * Insert preparation for an indexed directory is split into two
4194 * steps. The call to find_dir_space_dx reserves room in the index for
4195 * an additional item. If we run out of space there, it's a real error
4196 * we can't continue on.
4198 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4207 * Next, we need to find space in the unindexed tree. This call
4208 * searches using the free space linked list. If the unindexed tree
4209 * lacks sufficient space, we'll expand it below. The expansion code
4210 * is smart enough to add any new blocks to the free space list.
4212 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4213 if (ret && ret != -ENOSPC) {
4218 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4219 lookup->dl_dx_root_bh = dx_root_bh;
4222 if (ret == -ENOSPC) {
4223 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
4231 * We make the assumption here that new leaf blocks are added
4232 * to the front of our free list.
4234 lookup->dl_prev_leaf_bh = NULL;
4235 lookup->dl_leaf_bh = leaf_bh;
4245 * Get a directory ready for insert. Any directory allocation required
4246 * happens here. Success returns zero, and enough context in the dir
4247 * lookup result that ocfs2_add_entry() will be able complete the task
4248 * with minimal performance impact.
4250 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4252 struct buffer_head *parent_fe_bh,
4255 struct ocfs2_dir_lookup_result *lookup)
4258 unsigned int blocks_wanted = 1;
4259 struct buffer_head *bh = NULL;
4261 trace_ocfs2_prepare_dir_for_insert(
4262 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
4271 * Do this up front to reduce confusion.
4273 * The directory might start inline, then be turned into an
4274 * indexed one, in which case we'd need to hash deep inside
4275 * ocfs2_find_dir_space_id(). Since
4276 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4277 * done, there seems no point in spreading out the calls. We
4278 * can optimize away the case where the file system doesn't
4281 if (ocfs2_supports_indexed_dirs(osb))
4282 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4284 if (ocfs2_dir_indexed(dir)) {
4285 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4286 name, namelen, lookup);
4292 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4293 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4294 namelen, &bh, &blocks_wanted);
4296 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4298 if (ret && ret != -ENOSPC) {
4303 if (ret == -ENOSPC) {
4305 * We have to expand the directory to add this name.
4309 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
4320 lookup->dl_leaf_bh = bh;
4327 static int ocfs2_dx_dir_remove_index(struct inode *dir,
4328 struct buffer_head *di_bh,
4329 struct buffer_head *dx_root_bh)
4332 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4333 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4334 struct ocfs2_dx_root_block *dx_root;
4335 struct inode *dx_alloc_inode = NULL;
4336 struct buffer_head *dx_alloc_bh = NULL;
4342 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4344 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4345 EXTENT_ALLOC_SYSTEM_INODE,
4346 le16_to_cpu(dx_root->dr_suballoc_slot));
4347 if (!dx_alloc_inode) {
4352 inode_lock(dx_alloc_inode);
4354 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4360 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4361 if (IS_ERR(handle)) {
4362 ret = PTR_ERR(handle);
4367 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
4368 OCFS2_JOURNAL_ACCESS_WRITE);
4374 spin_lock(&OCFS2_I(dir)->ip_lock);
4375 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4376 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4377 spin_unlock(&OCFS2_I(dir)->ip_lock);
4378 di->i_dx_root = cpu_to_le64(0ULL);
4379 ocfs2_update_inode_fsync_trans(handle, dir, 1);
4381 ocfs2_journal_dirty(handle, di_bh);
4383 blk = le64_to_cpu(dx_root->dr_blkno);
4384 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4385 if (dx_root->dr_suballoc_loc)
4386 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4388 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4389 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4395 ocfs2_commit_trans(osb, handle);
4398 ocfs2_inode_unlock(dx_alloc_inode, 1);
4401 inode_unlock(dx_alloc_inode);
4402 brelse(dx_alloc_bh);
4404 iput(dx_alloc_inode);
4408 int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4411 unsigned int uninitialized_var(clen);
4412 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4413 u64 uninitialized_var(blkno);
4414 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4415 struct buffer_head *dx_root_bh = NULL;
4416 struct ocfs2_dx_root_block *dx_root;
4417 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4418 struct ocfs2_cached_dealloc_ctxt dealloc;
4419 struct ocfs2_extent_tree et;
4421 ocfs2_init_dealloc_ctxt(&dealloc);
4423 if (!ocfs2_dir_indexed(dir))
4426 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4431 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4433 if (ocfs2_dx_root_inline(dx_root))
4436 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4438 /* XXX: What if dr_clusters is too large? */
4439 while (le32_to_cpu(dx_root->dr_clusters)) {
4440 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4441 major_hash, &cpos, &blkno, &clen);
4447 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4449 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
4450 &dealloc, 0, false);
4459 major_hash = cpos - 1;
4463 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4469 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
4471 ocfs2_schedule_truncate_log_flush(osb, 1);
4472 ocfs2_run_deallocs(osb, &dealloc);