ocfs2: Store dir index records inline
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / dir.c
blob47de64988b1182f2d57ff48ee906dcf629b2b65c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dir.c
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)
17 * from
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
39 #include <linux/fs.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43 #include <linux/quotaops.h>
44 #include <linux/sort.h>
46 #define MLOG_MASK_PREFIX ML_NAMEI
47 #include <cluster/masklog.h>
49 #include "ocfs2.h"
51 #include "alloc.h"
52 #include "blockcheck.h"
53 #include "dir.h"
54 #include "dlmglue.h"
55 #include "extent_map.h"
56 #include "file.h"
57 #include "inode.h"
58 #include "journal.h"
59 #include "namei.h"
60 #include "suballoc.h"
61 #include "super.h"
62 #include "sysfile.h"
63 #include "uptodate.h"
65 #include "buffer_head_io.h"
67 #define NAMEI_RA_CHUNKS 2
68 #define NAMEI_RA_BLOCKS 4
69 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
72 static unsigned char ocfs2_filetype_table[] = {
73 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
76 static int ocfs2_do_extend_dir(struct super_block *sb,
77 handle_t *handle,
78 struct inode *dir,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
85 * These are distinct checks because future versions of the file system will
86 * want to have a trailing dirent structure independent of indexing.
88 static int ocfs2_dir_has_trailer(struct inode *dir)
90 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
91 return 0;
93 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
96 static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
98 return ocfs2_meta_ecc(osb);
101 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
103 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
106 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
108 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
109 * them more consistent? */
110 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
111 void *data)
113 char *p = data;
115 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
116 return (struct ocfs2_dir_block_trailer *)p;
120 * XXX: This is executed once on every dirent. We should consider optimizing
121 * it.
123 static int ocfs2_skip_dir_trailer(struct inode *dir,
124 struct ocfs2_dir_entry *de,
125 unsigned long offset,
126 unsigned long blklen)
128 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
130 if (!ocfs2_dir_has_trailer(dir))
131 return 0;
133 if (offset != toff)
134 return 0;
136 return 1;
139 static void ocfs2_init_dir_trailer(struct inode *inode,
140 struct buffer_head *bh)
142 struct ocfs2_dir_block_trailer *trailer;
144 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
145 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
146 trailer->db_compat_rec_len =
147 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
148 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
149 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
152 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
154 brelse(res->dl_dx_root_bh);
155 brelse(res->dl_leaf_bh);
156 brelse(res->dl_dx_leaf_bh);
159 static int ocfs2_dir_indexed(struct inode *inode)
161 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
162 return 1;
163 return 0;
166 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
168 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
172 * Hashing code adapted from ext3
174 #define DELTA 0x9E3779B9
176 static void TEA_transform(__u32 buf[4], __u32 const in[])
178 __u32 sum = 0;
179 __u32 b0 = buf[0], b1 = buf[1];
180 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
181 int n = 16;
183 do {
184 sum += DELTA;
185 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
186 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
187 } while (--n);
189 buf[0] += b0;
190 buf[1] += b1;
193 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
195 __u32 pad, val;
196 int i;
198 pad = (__u32)len | ((__u32)len << 8);
199 pad |= pad << 16;
201 val = pad;
202 if (len > num*4)
203 len = num * 4;
204 for (i = 0; i < len; i++) {
205 if ((i % 4) == 0)
206 val = pad;
207 val = msg[i] + (val << 8);
208 if ((i % 4) == 3) {
209 *buf++ = val;
210 val = pad;
211 num--;
214 if (--num >= 0)
215 *buf++ = val;
216 while (--num >= 0)
217 *buf++ = pad;
220 static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
221 struct ocfs2_dx_hinfo *hinfo)
223 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
224 const char *p;
225 __u32 in[8], buf[4];
228 * XXX: Is this really necessary, if the index is never looked
229 * at by readdir? Is a hash value of '0' a bad idea?
231 if ((len == 1 && !strncmp(".", name, 1)) ||
232 (len == 2 && !strncmp("..", name, 2))) {
233 buf[0] = buf[1] = 0;
234 goto out;
237 #ifdef OCFS2_DEBUG_DX_DIRS
239 * This makes it very easy to debug indexing problems. We
240 * should never allow this to be selected without hand editing
241 * this file though.
243 buf[0] = buf[1] = len;
244 goto out;
245 #endif
247 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
249 p = name;
250 while (len > 0) {
251 str2hashbuf(p, len, in, 4);
252 TEA_transform(buf, in);
253 len -= 16;
254 p += 16;
257 out:
258 hinfo->major_hash = buf[0];
259 hinfo->minor_hash = buf[1];
263 * bh passed here can be an inode block or a dir data block, depending
264 * on the inode inline data flag.
266 static int ocfs2_check_dir_entry(struct inode * dir,
267 struct ocfs2_dir_entry * de,
268 struct buffer_head * bh,
269 unsigned long offset)
271 const char *error_msg = NULL;
272 const int rlen = le16_to_cpu(de->rec_len);
274 if (rlen < OCFS2_DIR_REC_LEN(1))
275 error_msg = "rec_len is smaller than minimal";
276 else if (rlen % 4 != 0)
277 error_msg = "rec_len % 4 != 0";
278 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
279 error_msg = "rec_len is too small for name_len";
280 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
281 error_msg = "directory entry across blocks";
283 if (error_msg != NULL)
284 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
285 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
286 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
287 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
288 de->name_len);
289 return error_msg == NULL ? 1 : 0;
292 static inline int ocfs2_match(int len,
293 const char * const name,
294 struct ocfs2_dir_entry *de)
296 if (len != de->name_len)
297 return 0;
298 if (!de->inode)
299 return 0;
300 return !memcmp(name, de->name, len);
304 * Returns 0 if not found, -1 on failure, and 1 on success
306 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
307 struct inode *dir,
308 const char *name, int namelen,
309 unsigned long offset,
310 char *first_de,
311 unsigned int bytes,
312 struct ocfs2_dir_entry **res_dir)
314 struct ocfs2_dir_entry *de;
315 char *dlimit, *de_buf;
316 int de_len;
317 int ret = 0;
319 mlog_entry_void();
321 de_buf = first_de;
322 dlimit = de_buf + bytes;
324 while (de_buf < dlimit) {
325 /* this code is executed quadratically often */
326 /* do minimal checking `by hand' */
328 de = (struct ocfs2_dir_entry *) de_buf;
330 if (de_buf + namelen <= dlimit &&
331 ocfs2_match(namelen, name, de)) {
332 /* found a match - just to be sure, do a full check */
333 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
334 ret = -1;
335 goto bail;
337 *res_dir = de;
338 ret = 1;
339 goto bail;
342 /* prevent looping on a bad block */
343 de_len = le16_to_cpu(de->rec_len);
344 if (de_len <= 0) {
345 ret = -1;
346 goto bail;
349 de_buf += de_len;
350 offset += de_len;
353 bail:
354 mlog_exit(ret);
355 return ret;
358 static struct buffer_head *ocfs2_find_entry_id(const char *name,
359 int namelen,
360 struct inode *dir,
361 struct ocfs2_dir_entry **res_dir)
363 int ret, found;
364 struct buffer_head *di_bh = NULL;
365 struct ocfs2_dinode *di;
366 struct ocfs2_inline_data *data;
368 ret = ocfs2_read_inode_block(dir, &di_bh);
369 if (ret) {
370 mlog_errno(ret);
371 goto out;
374 di = (struct ocfs2_dinode *)di_bh->b_data;
375 data = &di->id2.i_data;
377 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
378 data->id_data, i_size_read(dir), res_dir);
379 if (found == 1)
380 return di_bh;
382 brelse(di_bh);
383 out:
384 return NULL;
387 static int ocfs2_validate_dir_block(struct super_block *sb,
388 struct buffer_head *bh)
390 int rc;
391 struct ocfs2_dir_block_trailer *trailer =
392 ocfs2_trailer_from_bh(bh, sb);
396 * We don't validate dirents here, that's handled
397 * in-place when the code walks them.
399 mlog(0, "Validating dirblock %llu\n",
400 (unsigned long long)bh->b_blocknr);
402 BUG_ON(!buffer_uptodate(bh));
405 * If the ecc fails, we return the error but otherwise
406 * leave the filesystem running. We know any error is
407 * local to this block.
409 * Note that we are safe to call this even if the directory
410 * doesn't have a trailer. Filesystems without metaecc will do
411 * nothing, and filesystems with it will have one.
413 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
414 if (rc)
415 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
416 (unsigned long long)bh->b_blocknr);
418 return rc;
422 * Validate a directory trailer.
424 * We check the trailer here rather than in ocfs2_validate_dir_block()
425 * because that function doesn't have the inode to test.
427 static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
429 int rc = 0;
430 struct ocfs2_dir_block_trailer *trailer;
432 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
433 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
434 rc = -EINVAL;
435 ocfs2_error(dir->i_sb,
436 "Invalid dirblock #%llu: "
437 "signature = %.*s\n",
438 (unsigned long long)bh->b_blocknr, 7,
439 trailer->db_signature);
440 goto out;
442 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
443 rc = -EINVAL;
444 ocfs2_error(dir->i_sb,
445 "Directory block #%llu has an invalid "
446 "db_blkno of %llu",
447 (unsigned long long)bh->b_blocknr,
448 (unsigned long long)le64_to_cpu(trailer->db_blkno));
449 goto out;
451 if (le64_to_cpu(trailer->db_parent_dinode) !=
452 OCFS2_I(dir)->ip_blkno) {
453 rc = -EINVAL;
454 ocfs2_error(dir->i_sb,
455 "Directory block #%llu on dinode "
456 "#%llu has an invalid parent_dinode "
457 "of %llu",
458 (unsigned long long)bh->b_blocknr,
459 (unsigned long long)OCFS2_I(dir)->ip_blkno,
460 (unsigned long long)le64_to_cpu(trailer->db_blkno));
461 goto out;
463 out:
464 return rc;
468 * This function forces all errors to -EIO for consistency with its
469 * predecessor, ocfs2_bread(). We haven't audited what returning the
470 * real error codes would do to callers. We log the real codes with
471 * mlog_errno() before we squash them.
473 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
474 struct buffer_head **bh, int flags)
476 int rc = 0;
477 struct buffer_head *tmp = *bh;
479 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
480 ocfs2_validate_dir_block);
481 if (rc) {
482 mlog_errno(rc);
483 goto out;
486 if (!(flags & OCFS2_BH_READAHEAD) &&
487 ocfs2_dir_has_trailer(inode)) {
488 rc = ocfs2_check_dir_trailer(inode, tmp);
489 if (rc) {
490 if (!*bh)
491 brelse(tmp);
492 mlog_errno(rc);
493 goto out;
497 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
498 if (!*bh)
499 *bh = tmp;
501 out:
502 return rc ? -EIO : 0;
506 * Read the block at 'phys' which belongs to this directory
507 * inode. This function does no virtual->physical block translation -
508 * what's passed in is assumed to be a valid directory block.
510 static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
511 struct buffer_head **bh)
513 int ret;
514 struct buffer_head *tmp = *bh;
516 ret = ocfs2_read_block(dir, phys, &tmp, ocfs2_validate_dir_block);
517 if (ret) {
518 mlog_errno(ret);
519 goto out;
522 if (ocfs2_supports_dir_trailer(dir)) {
523 ret = ocfs2_check_dir_trailer(dir, tmp);
524 if (ret) {
525 if (!*bh)
526 brelse(tmp);
527 mlog_errno(ret);
528 goto out;
532 if (!ret && !*bh)
533 *bh = tmp;
534 out:
535 return ret;
538 static int ocfs2_validate_dx_root(struct super_block *sb,
539 struct buffer_head *bh)
541 int ret;
542 struct ocfs2_dx_root_block *dx_root;
544 BUG_ON(!buffer_uptodate(bh));
546 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
548 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
549 if (ret) {
550 mlog(ML_ERROR,
551 "Checksum failed for dir index root block %llu\n",
552 (unsigned long long)bh->b_blocknr);
553 return ret;
556 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
557 ocfs2_error(sb,
558 "Dir Index Root # %llu has bad signature %.*s",
559 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
560 7, dx_root->dr_signature);
561 return -EINVAL;
564 return 0;
567 static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
568 struct buffer_head **dx_root_bh)
570 int ret;
571 u64 blkno = le64_to_cpu(di->i_dx_root);
572 struct buffer_head *tmp = *dx_root_bh;
574 ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_root);
576 /* If ocfs2_read_block() got us a new bh, pass it up. */
577 if (!ret && !*dx_root_bh)
578 *dx_root_bh = tmp;
580 return ret;
583 static int ocfs2_validate_dx_leaf(struct super_block *sb,
584 struct buffer_head *bh)
586 int ret;
587 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
589 BUG_ON(!buffer_uptodate(bh));
591 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
592 if (ret) {
593 mlog(ML_ERROR,
594 "Checksum failed for dir index leaf block %llu\n",
595 (unsigned long long)bh->b_blocknr);
596 return ret;
599 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
600 ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
601 7, dx_leaf->dl_signature);
602 return -EROFS;
605 return 0;
608 static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
609 struct buffer_head **dx_leaf_bh)
611 int ret;
612 struct buffer_head *tmp = *dx_leaf_bh;
614 ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_leaf);
616 /* If ocfs2_read_block() got us a new bh, pass it up. */
617 if (!ret && !*dx_leaf_bh)
618 *dx_leaf_bh = tmp;
620 return ret;
624 * Read a series of dx_leaf blocks. This expects all buffer_head
625 * pointers to be NULL on function entry.
627 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
628 struct buffer_head **dx_leaf_bhs)
630 int ret;
632 ret = ocfs2_read_blocks(dir, start, num, dx_leaf_bhs, 0,
633 ocfs2_validate_dx_leaf);
634 if (ret)
635 mlog_errno(ret);
637 return ret;
640 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
641 struct inode *dir,
642 struct ocfs2_dir_entry **res_dir)
644 struct super_block *sb;
645 struct buffer_head *bh_use[NAMEI_RA_SIZE];
646 struct buffer_head *bh, *ret = NULL;
647 unsigned long start, block, b;
648 int ra_max = 0; /* Number of bh's in the readahead
649 buffer, bh_use[] */
650 int ra_ptr = 0; /* Current index into readahead
651 buffer */
652 int num = 0;
653 int nblocks, i, err;
655 mlog_entry_void();
657 sb = dir->i_sb;
659 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
660 start = OCFS2_I(dir)->ip_dir_start_lookup;
661 if (start >= nblocks)
662 start = 0;
663 block = start;
665 restart:
666 do {
668 * We deal with the read-ahead logic here.
670 if (ra_ptr >= ra_max) {
671 /* Refill the readahead buffer */
672 ra_ptr = 0;
673 b = block;
674 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
676 * Terminate if we reach the end of the
677 * directory and must wrap, or if our
678 * search has finished at this block.
680 if (b >= nblocks || (num && block == start)) {
681 bh_use[ra_max] = NULL;
682 break;
684 num++;
686 bh = NULL;
687 err = ocfs2_read_dir_block(dir, b++, &bh,
688 OCFS2_BH_READAHEAD);
689 bh_use[ra_max] = bh;
692 if ((bh = bh_use[ra_ptr++]) == NULL)
693 goto next;
694 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
695 /* read error, skip block & hope for the best.
696 * ocfs2_read_dir_block() has released the bh. */
697 ocfs2_error(dir->i_sb, "reading directory %llu, "
698 "offset %lu\n",
699 (unsigned long long)OCFS2_I(dir)->ip_blkno,
700 block);
701 goto next;
703 i = ocfs2_search_dirblock(bh, dir, name, namelen,
704 block << sb->s_blocksize_bits,
705 bh->b_data, sb->s_blocksize,
706 res_dir);
707 if (i == 1) {
708 OCFS2_I(dir)->ip_dir_start_lookup = block;
709 ret = bh;
710 goto cleanup_and_exit;
711 } else {
712 brelse(bh);
713 if (i < 0)
714 goto cleanup_and_exit;
716 next:
717 if (++block >= nblocks)
718 block = 0;
719 } while (block != start);
722 * If the directory has grown while we were searching, then
723 * search the last part of the directory before giving up.
725 block = nblocks;
726 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
727 if (block < nblocks) {
728 start = 0;
729 goto restart;
732 cleanup_and_exit:
733 /* Clean up the read-ahead blocks */
734 for (; ra_ptr < ra_max; ra_ptr++)
735 brelse(bh_use[ra_ptr]);
737 mlog_exit_ptr(ret);
738 return ret;
741 static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
742 struct ocfs2_extent_list *el,
743 u32 major_hash,
744 u32 *ret_cpos,
745 u64 *ret_phys_blkno,
746 unsigned int *ret_clen)
748 int ret = 0, i, found;
749 struct buffer_head *eb_bh = NULL;
750 struct ocfs2_extent_block *eb;
751 struct ocfs2_extent_rec *rec = NULL;
753 if (el->l_tree_depth) {
754 ret = ocfs2_find_leaf(inode, el, major_hash, &eb_bh);
755 if (ret) {
756 mlog_errno(ret);
757 goto out;
760 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
761 el = &eb->h_list;
763 if (el->l_tree_depth) {
764 ocfs2_error(inode->i_sb,
765 "Inode %lu has non zero tree depth in "
766 "btree tree block %llu\n", inode->i_ino,
767 (unsigned long long)eb_bh->b_blocknr);
768 ret = -EROFS;
769 goto out;
773 found = 0;
774 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
775 rec = &el->l_recs[i];
777 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
778 found = 1;
779 break;
783 if (!found) {
784 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
785 "record (%u, %u, 0) in btree", inode->i_ino,
786 le32_to_cpu(rec->e_cpos),
787 ocfs2_rec_clusters(el, rec));
788 ret = -EROFS;
789 goto out;
792 if (ret_phys_blkno)
793 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
794 if (ret_cpos)
795 *ret_cpos = le32_to_cpu(rec->e_cpos);
796 if (ret_clen)
797 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
799 out:
800 brelse(eb_bh);
801 return ret;
805 * Returns the block index, from the start of the cluster which this
806 * hash belongs too.
808 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
809 u32 minor_hash)
811 return minor_hash & osb->osb_dx_mask;
814 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
815 struct ocfs2_dx_hinfo *hinfo)
817 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
820 static int ocfs2_dx_dir_lookup(struct inode *inode,
821 struct ocfs2_extent_list *el,
822 struct ocfs2_dx_hinfo *hinfo,
823 u32 *ret_cpos,
824 u64 *ret_phys_blkno)
826 int ret = 0;
827 unsigned int cend, uninitialized_var(clen);
828 u32 uninitialized_var(cpos);
829 u64 uninitialized_var(blkno);
830 u32 name_hash = hinfo->major_hash;
832 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
833 &clen);
834 if (ret) {
835 mlog_errno(ret);
836 goto out;
839 cend = cpos + clen;
840 if (name_hash >= cend) {
841 /* We want the last cluster */
842 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
843 cpos += clen - 1;
844 } else {
845 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
846 name_hash - cpos);
847 cpos = name_hash;
851 * We now have the cluster which should hold our entry. To
852 * find the exact block from the start of the cluster to
853 * search, we take the lower bits of the hash.
855 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
857 if (ret_phys_blkno)
858 *ret_phys_blkno = blkno;
859 if (ret_cpos)
860 *ret_cpos = cpos;
862 out:
864 return ret;
867 static int ocfs2_dx_dir_search(const char *name, int namelen,
868 struct inode *dir,
869 struct ocfs2_dx_root_block *dx_root,
870 struct ocfs2_dir_lookup_result *res)
872 int ret, i, found;
873 u64 uninitialized_var(phys);
874 struct buffer_head *dx_leaf_bh = NULL;
875 struct ocfs2_dx_leaf *dx_leaf;
876 struct ocfs2_dx_entry *dx_entry = NULL;
877 struct buffer_head *dir_ent_bh = NULL;
878 struct ocfs2_dir_entry *dir_ent = NULL;
879 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
880 struct ocfs2_extent_list *dr_el;
881 struct ocfs2_dx_entry_list *entry_list;
883 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
885 if (ocfs2_dx_root_inline(dx_root)) {
886 entry_list = &dx_root->dr_entries;
887 goto search;
890 dr_el = &dx_root->dr_list;
892 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
893 if (ret) {
894 mlog_errno(ret);
895 goto out;
898 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
899 "returns: %llu\n",
900 (unsigned long long)OCFS2_I(dir)->ip_blkno,
901 namelen, name, hinfo->major_hash, hinfo->minor_hash,
902 (unsigned long long)phys);
904 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
905 if (ret) {
906 mlog_errno(ret);
907 goto out;
910 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
912 mlog(0, "leaf info: num_used: %d, count: %d\n",
913 le16_to_cpu(dx_leaf->dl_list.de_num_used),
914 le16_to_cpu(dx_leaf->dl_list.de_count));
916 entry_list = &dx_leaf->dl_list;
918 search:
920 * Empty leaf is legal, so no need to check for that.
922 found = 0;
923 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
924 dx_entry = &entry_list->de_entries[i];
926 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
927 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
928 continue;
931 * Search unindexed leaf block now. We're not
932 * guaranteed to find anything.
934 ret = ocfs2_read_dir_block_direct(dir,
935 le64_to_cpu(dx_entry->dx_dirent_blk),
936 &dir_ent_bh);
937 if (ret) {
938 mlog_errno(ret);
939 goto out;
943 * XXX: We should check the unindexed block here,
944 * before using it.
947 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
948 0, dir_ent_bh->b_data,
949 dir->i_sb->s_blocksize, &dir_ent);
950 if (found == 1)
951 break;
953 if (found == -1) {
954 /* This means we found a bad directory entry. */
955 ret = -EIO;
956 mlog_errno(ret);
957 goto out;
960 brelse(dir_ent_bh);
961 dir_ent_bh = NULL;
964 if (found <= 0) {
965 ret = -ENOENT;
966 goto out;
969 res->dl_leaf_bh = dir_ent_bh;
970 res->dl_entry = dir_ent;
971 res->dl_dx_leaf_bh = dx_leaf_bh;
972 res->dl_dx_entry = dx_entry;
974 ret = 0;
975 out:
976 if (ret) {
977 brelse(dx_leaf_bh);
978 brelse(dir_ent_bh);
980 return ret;
983 static int ocfs2_find_entry_dx(const char *name, int namelen,
984 struct inode *dir,
985 struct ocfs2_dir_lookup_result *lookup)
987 int ret;
988 struct buffer_head *di_bh = NULL;
989 struct ocfs2_dinode *di;
990 struct buffer_head *dx_root_bh = NULL;
991 struct ocfs2_dx_root_block *dx_root;
993 ret = ocfs2_read_inode_block(dir, &di_bh);
994 if (ret) {
995 mlog_errno(ret);
996 goto out;
999 di = (struct ocfs2_dinode *)di_bh->b_data;
1001 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1002 if (ret) {
1003 mlog_errno(ret);
1004 goto out;
1006 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1008 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
1009 if (ret) {
1010 if (ret != -ENOENT)
1011 mlog_errno(ret);
1012 goto out;
1015 lookup->dl_dx_root_bh = dx_root_bh;
1016 dx_root_bh = NULL;
1017 out:
1018 brelse(di_bh);
1019 brelse(dx_root_bh);
1020 return ret;
1024 * Try to find an entry of the provided name within 'dir'.
1026 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1027 * returned and the struct 'res' will contain information useful to
1028 * other directory manipulation functions.
1030 * Caller can NOT assume anything about the contents of the
1031 * buffer_heads - they are passed back only so that it can be passed
1032 * into any one of the manipulation functions (add entry, delete
1033 * entry, etc). As an example, bh in the extent directory case is a
1034 * data block, in the inline-data case it actually points to an inode,
1035 * in the indexed directory case, multiple buffers are involved.
1037 int ocfs2_find_entry(const char *name, int namelen,
1038 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1040 struct buffer_head *bh;
1041 struct ocfs2_dir_entry *res_dir = NULL;
1043 if (ocfs2_dir_indexed(dir))
1044 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1047 * The unindexed dir code only uses part of the lookup
1048 * structure, so there's no reason to push it down further
1049 * than this.
1051 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1052 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1053 else
1054 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1056 if (bh == NULL)
1057 return -ENOENT;
1059 lookup->dl_leaf_bh = bh;
1060 lookup->dl_entry = res_dir;
1061 return 0;
1065 * Update inode number and type of a previously found directory entry.
1067 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1068 struct ocfs2_dir_lookup_result *res,
1069 struct inode *new_entry_inode)
1071 int ret;
1072 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1073 struct ocfs2_dir_entry *de = res->dl_entry;
1074 struct buffer_head *de_bh = res->dl_leaf_bh;
1077 * The same code works fine for both inline-data and extent
1078 * based directories, so no need to split this up. The only
1079 * difference is the journal_access function.
1082 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1083 access = ocfs2_journal_access_di;
1085 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1086 if (ret) {
1087 mlog_errno(ret);
1088 goto out;
1091 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1092 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1094 ocfs2_journal_dirty(handle, de_bh);
1096 out:
1097 return ret;
1101 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1102 * previous entry
1104 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1105 struct ocfs2_dir_entry *de_del,
1106 struct buffer_head *bh, char *first_de,
1107 unsigned int bytes)
1109 struct ocfs2_dir_entry *de, *pde;
1110 int i, status = -ENOENT;
1111 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1113 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1115 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1116 access = ocfs2_journal_access_di;
1118 i = 0;
1119 pde = NULL;
1120 de = (struct ocfs2_dir_entry *) first_de;
1121 while (i < bytes) {
1122 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1123 status = -EIO;
1124 mlog_errno(status);
1125 goto bail;
1127 if (de == de_del) {
1128 status = access(handle, dir, bh,
1129 OCFS2_JOURNAL_ACCESS_WRITE);
1130 if (status < 0) {
1131 status = -EIO;
1132 mlog_errno(status);
1133 goto bail;
1135 if (pde)
1136 le16_add_cpu(&pde->rec_len,
1137 le16_to_cpu(de->rec_len));
1138 else
1139 de->inode = 0;
1140 dir->i_version++;
1141 status = ocfs2_journal_dirty(handle, bh);
1142 goto bail;
1144 i += le16_to_cpu(de->rec_len);
1145 pde = de;
1146 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1148 bail:
1149 mlog_exit(status);
1150 return status;
1153 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1154 int index)
1156 int num_used = le16_to_cpu(entry_list->de_num_used);
1158 if (num_used == 1 || index == (num_used - 1))
1159 goto clear;
1161 memmove(&entry_list->de_entries[index],
1162 &entry_list->de_entries[index + 1],
1163 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1164 clear:
1165 num_used--;
1166 memset(&entry_list->de_entries[num_used], 0,
1167 sizeof(struct ocfs2_dx_entry));
1168 entry_list->de_num_used = cpu_to_le16(num_used);
1171 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1172 struct ocfs2_dir_lookup_result *lookup)
1174 int ret, index;
1175 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1176 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1177 struct ocfs2_dx_leaf *dx_leaf;
1178 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
1179 struct ocfs2_dx_root_block *dx_root;
1180 struct ocfs2_dx_entry_list *entry_list;
1182 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1183 if (ocfs2_dx_root_inline(dx_root)) {
1184 entry_list = &dx_root->dr_entries;
1185 } else {
1186 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1187 entry_list = &dx_leaf->dl_list;
1190 /* Neither of these are a disk corruption - that should have
1191 * been caught by lookup, before we got here. */
1192 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1193 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
1195 index = (char *)dx_entry - (char *)entry_list->de_entries;
1196 index /= sizeof(*dx_entry);
1198 if (index >= le16_to_cpu(entry_list->de_num_used)) {
1199 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1200 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1201 entry_list, dx_entry);
1202 return -EIO;
1206 * Add the block holding our index into the journal before
1207 * removing the unindexed entry. If we get an error return
1208 * from __ocfs2_delete_entry(), then it hasn't removed the
1209 * entry yet. Likewise, successful return means we *must*
1210 * remove the indexed entry.
1212 * We're also careful to journal the root tree block here if
1213 * we're going to be adding to the start of the free list.
1215 if (ocfs2_dx_root_inline(dx_root)) {
1216 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1217 OCFS2_JOURNAL_ACCESS_WRITE);
1218 if (ret) {
1219 mlog_errno(ret);
1220 goto out;
1222 } else {
1223 ret = ocfs2_journal_access_dl(handle, dir,
1224 lookup->dl_dx_leaf_bh,
1225 OCFS2_JOURNAL_ACCESS_WRITE);
1226 if (ret) {
1227 mlog_errno(ret);
1228 goto out;
1232 mlog(0, "Dir %llu: delete entry at index: %d\n",
1233 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1235 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1236 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1237 if (ret) {
1238 mlog_errno(ret);
1239 goto out;
1242 ocfs2_dx_list_remove_entry(entry_list, index);
1244 if (ocfs2_dx_root_inline(dx_root))
1245 ocfs2_journal_dirty(handle, dx_root_bh);
1246 else
1247 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
1249 out:
1250 return ret;
1253 static inline int ocfs2_delete_entry_id(handle_t *handle,
1254 struct inode *dir,
1255 struct ocfs2_dir_entry *de_del,
1256 struct buffer_head *bh)
1258 int ret;
1259 struct buffer_head *di_bh = NULL;
1260 struct ocfs2_dinode *di;
1261 struct ocfs2_inline_data *data;
1263 ret = ocfs2_read_inode_block(dir, &di_bh);
1264 if (ret) {
1265 mlog_errno(ret);
1266 goto out;
1269 di = (struct ocfs2_dinode *)di_bh->b_data;
1270 data = &di->id2.i_data;
1272 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1273 i_size_read(dir));
1275 brelse(di_bh);
1276 out:
1277 return ret;
1280 static inline int ocfs2_delete_entry_el(handle_t *handle,
1281 struct inode *dir,
1282 struct ocfs2_dir_entry *de_del,
1283 struct buffer_head *bh)
1285 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1286 bh->b_size);
1290 * Delete a directory entry. Hide the details of directory
1291 * implementation from the caller.
1293 int ocfs2_delete_entry(handle_t *handle,
1294 struct inode *dir,
1295 struct ocfs2_dir_lookup_result *res)
1297 if (ocfs2_dir_indexed(dir))
1298 return ocfs2_delete_entry_dx(handle, dir, res);
1300 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1301 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1302 res->dl_leaf_bh);
1304 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1305 res->dl_leaf_bh);
1309 * Check whether 'de' has enough room to hold an entry of
1310 * 'new_rec_len' bytes.
1312 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1313 unsigned int new_rec_len)
1315 unsigned int de_really_used;
1317 /* Check whether this is an empty record with enough space */
1318 if (le64_to_cpu(de->inode) == 0 &&
1319 le16_to_cpu(de->rec_len) >= new_rec_len)
1320 return 1;
1323 * Record might have free space at the end which we can
1324 * use.
1326 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1327 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1328 return 1;
1330 return 0;
1333 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1334 struct ocfs2_dx_entry *dx_new_entry)
1336 int i;
1338 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1339 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1341 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1344 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1345 struct ocfs2_dx_hinfo *hinfo,
1346 u64 dirent_blk)
1348 int i;
1349 struct ocfs2_dx_entry *dx_entry;
1351 i = le16_to_cpu(entry_list->de_num_used);
1352 dx_entry = &entry_list->de_entries[i];
1354 memset(dx_entry, 0, sizeof(*dx_entry));
1355 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1356 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1357 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1359 le16_add_cpu(&entry_list->de_num_used, 1);
1362 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1363 struct ocfs2_dx_hinfo *hinfo,
1364 u64 dirent_blk,
1365 struct buffer_head *dx_leaf_bh)
1367 int ret;
1368 struct ocfs2_dx_leaf *dx_leaf;
1370 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret) {
1373 mlog_errno(ret);
1374 goto out;
1377 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
1378 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1379 ocfs2_journal_dirty(handle, dx_leaf_bh);
1381 out:
1382 return ret;
1385 static int ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1386 struct ocfs2_dx_hinfo *hinfo,
1387 u64 dirent_blk,
1388 struct buffer_head *dx_root_bh)
1390 int ret;
1391 struct ocfs2_dx_root_block *dx_root;
1393 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1394 OCFS2_JOURNAL_ACCESS_WRITE);
1395 if (ret) {
1396 mlog_errno(ret);
1397 goto out;
1400 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1401 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1402 ocfs2_journal_dirty(handle, dx_root_bh);
1404 out:
1405 return ret;
1408 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1409 struct ocfs2_dir_lookup_result *lookup)
1411 struct ocfs2_dx_root_block *dx_root;
1413 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1414 if (ocfs2_dx_root_inline(dx_root))
1415 return ocfs2_dx_inline_root_insert(dir, handle,
1416 &lookup->dl_hinfo,
1417 lookup->dl_leaf_bh->b_blocknr,
1418 lookup->dl_dx_root_bh);
1420 return __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1421 lookup->dl_leaf_bh->b_blocknr,
1422 lookup->dl_dx_leaf_bh);
1425 /* we don't always have a dentry for what we want to add, so people
1426 * like orphan dir can call this instead.
1428 * The lookup context must have been filled from
1429 * ocfs2_prepare_dir_for_insert.
1431 int __ocfs2_add_entry(handle_t *handle,
1432 struct inode *dir,
1433 const char *name, int namelen,
1434 struct inode *inode, u64 blkno,
1435 struct buffer_head *parent_fe_bh,
1436 struct ocfs2_dir_lookup_result *lookup)
1438 unsigned long offset;
1439 unsigned short rec_len;
1440 struct ocfs2_dir_entry *de, *de1;
1441 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1442 struct super_block *sb = dir->i_sb;
1443 int retval, status;
1444 unsigned int size = sb->s_blocksize;
1445 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
1446 char *data_start = insert_bh->b_data;
1448 mlog_entry_void();
1450 if (!namelen)
1451 return -EINVAL;
1453 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1454 data_start = di->id2.i_data.id_data;
1455 size = i_size_read(dir);
1457 BUG_ON(insert_bh != parent_fe_bh);
1460 rec_len = OCFS2_DIR_REC_LEN(namelen);
1461 offset = 0;
1462 de = (struct ocfs2_dir_entry *) data_start;
1463 while (1) {
1464 BUG_ON((char *)de >= (size + data_start));
1466 /* These checks should've already been passed by the
1467 * prepare function, but I guess we can leave them
1468 * here anyway. */
1469 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1470 retval = -ENOENT;
1471 goto bail;
1473 if (ocfs2_match(namelen, name, de)) {
1474 retval = -EEXIST;
1475 goto bail;
1478 /* We're guaranteed that we should have space, so we
1479 * can't possibly have hit the trailer...right? */
1480 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1481 "Hit dir trailer trying to insert %.*s "
1482 "(namelen %d) into directory %llu. "
1483 "offset is %lu, trailer offset is %d\n",
1484 namelen, name, namelen,
1485 (unsigned long long)parent_fe_bh->b_blocknr,
1486 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1488 if (ocfs2_dirent_would_fit(de, rec_len)) {
1489 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1490 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1491 if (retval < 0) {
1492 mlog_errno(retval);
1493 goto bail;
1496 if (insert_bh == parent_fe_bh)
1497 status = ocfs2_journal_access_di(handle, dir,
1498 insert_bh,
1499 OCFS2_JOURNAL_ACCESS_WRITE);
1500 else {
1501 status = ocfs2_journal_access_db(handle, dir,
1502 insert_bh,
1503 OCFS2_JOURNAL_ACCESS_WRITE);
1505 if (ocfs2_dir_indexed(dir)) {
1506 status = ocfs2_dx_dir_insert(dir,
1507 handle,
1508 lookup);
1509 if (status) {
1510 mlog_errno(status);
1511 goto bail;
1516 /* By now the buffer is marked for journaling */
1517 offset += le16_to_cpu(de->rec_len);
1518 if (le64_to_cpu(de->inode)) {
1519 de1 = (struct ocfs2_dir_entry *)((char *) de +
1520 OCFS2_DIR_REC_LEN(de->name_len));
1521 de1->rec_len =
1522 cpu_to_le16(le16_to_cpu(de->rec_len) -
1523 OCFS2_DIR_REC_LEN(de->name_len));
1524 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1525 de = de1;
1527 de->file_type = OCFS2_FT_UNKNOWN;
1528 if (blkno) {
1529 de->inode = cpu_to_le64(blkno);
1530 ocfs2_set_de_type(de, inode->i_mode);
1531 } else
1532 de->inode = 0;
1533 de->name_len = namelen;
1534 memcpy(de->name, name, namelen);
1536 dir->i_version++;
1537 status = ocfs2_journal_dirty(handle, insert_bh);
1538 retval = 0;
1539 goto bail;
1542 offset += le16_to_cpu(de->rec_len);
1543 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1546 /* when you think about it, the assert above should prevent us
1547 * from ever getting here. */
1548 retval = -ENOSPC;
1549 bail:
1551 mlog_exit(retval);
1552 return retval;
1555 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
1556 u64 *f_version,
1557 loff_t *f_pos, void *priv,
1558 filldir_t filldir, int *filldir_err)
1560 int ret, i, filldir_ret;
1561 unsigned long offset = *f_pos;
1562 struct buffer_head *di_bh = NULL;
1563 struct ocfs2_dinode *di;
1564 struct ocfs2_inline_data *data;
1565 struct ocfs2_dir_entry *de;
1567 ret = ocfs2_read_inode_block(inode, &di_bh);
1568 if (ret) {
1569 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1570 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1571 goto out;
1574 di = (struct ocfs2_dinode *)di_bh->b_data;
1575 data = &di->id2.i_data;
1577 while (*f_pos < i_size_read(inode)) {
1578 revalidate:
1579 /* If the dir block has changed since the last call to
1580 * readdir(2), then we might be pointing to an invalid
1581 * dirent right now. Scan from the start of the block
1582 * to make sure. */
1583 if (*f_version != inode->i_version) {
1584 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1585 de = (struct ocfs2_dir_entry *)
1586 (data->id_data + i);
1587 /* It's too expensive to do a full
1588 * dirent test each time round this
1589 * loop, but we do have to test at
1590 * least that it is non-zero. A
1591 * failure will be detected in the
1592 * dirent test below. */
1593 if (le16_to_cpu(de->rec_len) <
1594 OCFS2_DIR_REC_LEN(1))
1595 break;
1596 i += le16_to_cpu(de->rec_len);
1598 *f_pos = offset = i;
1599 *f_version = inode->i_version;
1602 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
1603 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
1604 /* On error, skip the f_pos to the end. */
1605 *f_pos = i_size_read(inode);
1606 goto out;
1608 offset += le16_to_cpu(de->rec_len);
1609 if (le64_to_cpu(de->inode)) {
1610 /* We might block in the next section
1611 * if the data destination is
1612 * currently swapped out. So, use a
1613 * version stamp to detect whether or
1614 * not the directory has been modified
1615 * during the copy operation.
1617 u64 version = *f_version;
1618 unsigned char d_type = DT_UNKNOWN;
1620 if (de->file_type < OCFS2_FT_MAX)
1621 d_type = ocfs2_filetype_table[de->file_type];
1623 filldir_ret = filldir(priv, de->name,
1624 de->name_len,
1625 *f_pos,
1626 le64_to_cpu(de->inode),
1627 d_type);
1628 if (filldir_ret) {
1629 if (filldir_err)
1630 *filldir_err = filldir_ret;
1631 break;
1633 if (version != *f_version)
1634 goto revalidate;
1636 *f_pos += le16_to_cpu(de->rec_len);
1639 out:
1640 brelse(di_bh);
1642 return 0;
1646 * NOTE: This function can be called against unindexed directories,
1647 * and indexed ones.
1649 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
1650 u64 *f_version,
1651 loff_t *f_pos, void *priv,
1652 filldir_t filldir, int *filldir_err)
1654 int error = 0;
1655 unsigned long offset, blk, last_ra_blk = 0;
1656 int i, stored;
1657 struct buffer_head * bh, * tmp;
1658 struct ocfs2_dir_entry * de;
1659 struct super_block * sb = inode->i_sb;
1660 unsigned int ra_sectors = 16;
1662 stored = 0;
1663 bh = NULL;
1665 offset = (*f_pos) & (sb->s_blocksize - 1);
1667 while (!error && !stored && *f_pos < i_size_read(inode)) {
1668 blk = (*f_pos) >> sb->s_blocksize_bits;
1669 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1670 /* Skip the corrupt dirblock and keep trying */
1671 *f_pos += sb->s_blocksize - offset;
1672 continue;
1675 /* The idea here is to begin with 8k read-ahead and to stay
1676 * 4k ahead of our current position.
1678 * TODO: Use the pagecache for this. We just need to
1679 * make sure it's cluster-safe... */
1680 if (!last_ra_blk
1681 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1682 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
1683 i > 0; i--) {
1684 tmp = NULL;
1685 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1686 OCFS2_BH_READAHEAD))
1687 brelse(tmp);
1689 last_ra_blk = blk;
1690 ra_sectors = 8;
1693 revalidate:
1694 /* If the dir block has changed since the last call to
1695 * readdir(2), then we might be pointing to an invalid
1696 * dirent right now. Scan from the start of the block
1697 * to make sure. */
1698 if (*f_version != inode->i_version) {
1699 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1700 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1701 /* It's too expensive to do a full
1702 * dirent test each time round this
1703 * loop, but we do have to test at
1704 * least that it is non-zero. A
1705 * failure will be detected in the
1706 * dirent test below. */
1707 if (le16_to_cpu(de->rec_len) <
1708 OCFS2_DIR_REC_LEN(1))
1709 break;
1710 i += le16_to_cpu(de->rec_len);
1712 offset = i;
1713 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
1714 | offset;
1715 *f_version = inode->i_version;
1718 while (!error && *f_pos < i_size_read(inode)
1719 && offset < sb->s_blocksize) {
1720 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1721 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1722 /* On error, skip the f_pos to the
1723 next block. */
1724 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
1725 brelse(bh);
1726 goto out;
1728 offset += le16_to_cpu(de->rec_len);
1729 if (le64_to_cpu(de->inode)) {
1730 /* We might block in the next section
1731 * if the data destination is
1732 * currently swapped out. So, use a
1733 * version stamp to detect whether or
1734 * not the directory has been modified
1735 * during the copy operation.
1737 unsigned long version = *f_version;
1738 unsigned char d_type = DT_UNKNOWN;
1740 if (de->file_type < OCFS2_FT_MAX)
1741 d_type = ocfs2_filetype_table[de->file_type];
1742 error = filldir(priv, de->name,
1743 de->name_len,
1744 *f_pos,
1745 le64_to_cpu(de->inode),
1746 d_type);
1747 if (error) {
1748 if (filldir_err)
1749 *filldir_err = error;
1750 break;
1752 if (version != *f_version)
1753 goto revalidate;
1754 stored ++;
1756 *f_pos += le16_to_cpu(de->rec_len);
1758 offset = 0;
1759 brelse(bh);
1760 bh = NULL;
1763 stored = 0;
1764 out:
1765 return stored;
1768 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1769 loff_t *f_pos, void *priv, filldir_t filldir,
1770 int *filldir_err)
1772 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1773 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
1774 filldir, filldir_err);
1776 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1777 filldir_err);
1781 * This is intended to be called from inside other kernel functions,
1782 * so we fake some arguments.
1784 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1785 filldir_t filldir)
1787 int ret = 0, filldir_err = 0;
1788 u64 version = inode->i_version;
1790 while (*f_pos < i_size_read(inode)) {
1791 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
1792 filldir, &filldir_err);
1793 if (ret || filldir_err)
1794 break;
1797 if (ret > 0)
1798 ret = -EIO;
1800 return 0;
1804 * ocfs2_readdir()
1807 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1809 int error = 0;
1810 struct inode *inode = filp->f_path.dentry->d_inode;
1811 int lock_level = 0;
1813 mlog_entry("dirino=%llu\n",
1814 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1816 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1817 if (lock_level && error >= 0) {
1818 /* We release EX lock which used to update atime
1819 * and get PR lock again to reduce contention
1820 * on commonly accessed directories. */
1821 ocfs2_inode_unlock(inode, 1);
1822 lock_level = 0;
1823 error = ocfs2_inode_lock(inode, NULL, 0);
1825 if (error < 0) {
1826 if (error != -ENOENT)
1827 mlog_errno(error);
1828 /* we haven't got any yet, so propagate the error. */
1829 goto bail_nolock;
1832 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
1833 dirent, filldir, NULL);
1835 ocfs2_inode_unlock(inode, lock_level);
1837 bail_nolock:
1838 mlog_exit(error);
1840 return error;
1844 * NOTE: this should always be called with parent dir i_mutex taken.
1846 int ocfs2_find_files_on_disk(const char *name,
1847 int namelen,
1848 u64 *blkno,
1849 struct inode *inode,
1850 struct ocfs2_dir_lookup_result *lookup)
1852 int status = -ENOENT;
1854 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
1855 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1857 status = ocfs2_find_entry(name, namelen, inode, lookup);
1858 if (status)
1859 goto leave;
1861 *blkno = le64_to_cpu(lookup->dl_entry->inode);
1863 status = 0;
1864 leave:
1866 return status;
1870 * Convenience function for callers which just want the block number
1871 * mapped to a name and don't require the full dirent info, etc.
1873 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1874 int namelen, u64 *blkno)
1876 int ret;
1877 struct ocfs2_dir_lookup_result lookup = { NULL, };
1879 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
1880 ocfs2_free_dir_lookup_result(&lookup);
1882 return ret;
1885 /* Check for a name within a directory.
1887 * Return 0 if the name does not exist
1888 * Return -EEXIST if the directory contains the name
1890 * Callers should have i_mutex + a cluster lock on dir
1892 int ocfs2_check_dir_for_entry(struct inode *dir,
1893 const char *name,
1894 int namelen)
1896 int ret;
1897 struct ocfs2_dir_lookup_result lookup = { NULL, };
1899 mlog_entry("dir %llu, name '%.*s'\n",
1900 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
1902 ret = -EEXIST;
1903 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
1904 goto bail;
1906 ret = 0;
1907 bail:
1908 ocfs2_free_dir_lookup_result(&lookup);
1910 mlog_exit(ret);
1911 return ret;
1914 struct ocfs2_empty_dir_priv {
1915 unsigned seen_dot;
1916 unsigned seen_dot_dot;
1917 unsigned seen_other;
1919 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1920 loff_t pos, u64 ino, unsigned type)
1922 struct ocfs2_empty_dir_priv *p = priv;
1925 * Check the positions of "." and ".." records to be sure
1926 * they're in the correct place.
1928 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1929 p->seen_dot = 1;
1930 return 0;
1933 if (name_len == 2 && !strncmp("..", name, 2) &&
1934 pos == OCFS2_DIR_REC_LEN(1)) {
1935 p->seen_dot_dot = 1;
1936 return 0;
1939 p->seen_other = 1;
1940 return 1;
1943 * routine to check that the specified directory is empty (for rmdir)
1945 * Returns 1 if dir is empty, zero otherwise.
1947 * XXX: This is a performance problem
1949 int ocfs2_empty_dir(struct inode *inode)
1951 int ret;
1952 loff_t start = 0;
1953 struct ocfs2_empty_dir_priv priv;
1955 memset(&priv, 0, sizeof(priv));
1957 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1958 if (ret)
1959 mlog_errno(ret);
1961 if (!priv.seen_dot || !priv.seen_dot_dot) {
1962 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1963 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1965 * XXX: Is it really safe to allow an unlink to continue?
1967 return 1;
1970 return !priv.seen_other;
1974 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1975 * "..", which might be used during creation of a directory with a trailing
1976 * header. It is otherwise safe to ignore the return code.
1978 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1979 struct inode *parent,
1980 char *start,
1981 unsigned int size)
1983 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1985 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1986 de->name_len = 1;
1987 de->rec_len =
1988 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1989 strcpy(de->name, ".");
1990 ocfs2_set_de_type(de, S_IFDIR);
1992 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1993 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1994 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1995 de->name_len = 2;
1996 strcpy(de->name, "..");
1997 ocfs2_set_de_type(de, S_IFDIR);
1999 return de;
2003 * This works together with code in ocfs2_mknod_locked() which sets
2004 * the inline-data flag and initializes the inline-data section.
2006 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2007 handle_t *handle,
2008 struct inode *parent,
2009 struct inode *inode,
2010 struct buffer_head *di_bh)
2012 int ret;
2013 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2014 struct ocfs2_inline_data *data = &di->id2.i_data;
2015 unsigned int size = le16_to_cpu(data->id_count);
2017 ret = ocfs2_journal_access_di(handle, inode, di_bh,
2018 OCFS2_JOURNAL_ACCESS_WRITE);
2019 if (ret) {
2020 mlog_errno(ret);
2021 goto out;
2024 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2026 ocfs2_journal_dirty(handle, di_bh);
2027 if (ret) {
2028 mlog_errno(ret);
2029 goto out;
2032 i_size_write(inode, size);
2033 inode->i_nlink = 2;
2034 inode->i_blocks = ocfs2_inode_sector_count(inode);
2036 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2037 if (ret < 0)
2038 mlog_errno(ret);
2040 out:
2041 return ret;
2044 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2045 handle_t *handle,
2046 struct inode *parent,
2047 struct inode *inode,
2048 struct buffer_head *fe_bh,
2049 struct ocfs2_alloc_context *data_ac,
2050 struct buffer_head **ret_new_bh)
2052 int status;
2053 unsigned int size = osb->sb->s_blocksize;
2054 struct buffer_head *new_bh = NULL;
2055 struct ocfs2_dir_entry *de;
2057 mlog_entry_void();
2059 if (ocfs2_supports_dir_trailer(osb))
2060 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2062 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2063 data_ac, NULL, &new_bh);
2064 if (status < 0) {
2065 mlog_errno(status);
2066 goto bail;
2069 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2071 status = ocfs2_journal_access_db(handle, inode, new_bh,
2072 OCFS2_JOURNAL_ACCESS_CREATE);
2073 if (status < 0) {
2074 mlog_errno(status);
2075 goto bail;
2077 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2079 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2080 if (ocfs2_supports_dir_trailer(osb))
2081 ocfs2_init_dir_trailer(inode, new_bh);
2083 status = ocfs2_journal_dirty(handle, new_bh);
2084 if (status < 0) {
2085 mlog_errno(status);
2086 goto bail;
2089 i_size_write(inode, inode->i_sb->s_blocksize);
2090 inode->i_nlink = 2;
2091 inode->i_blocks = ocfs2_inode_sector_count(inode);
2092 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2093 if (status < 0) {
2094 mlog_errno(status);
2095 goto bail;
2098 status = 0;
2099 if (ret_new_bh) {
2100 *ret_new_bh = new_bh;
2101 new_bh = NULL;
2103 bail:
2104 brelse(new_bh);
2106 mlog_exit(status);
2107 return status;
2110 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2111 handle_t *handle, struct inode *dir,
2112 struct buffer_head *di_bh,
2113 struct ocfs2_alloc_context *meta_ac,
2114 int dx_inline,
2115 struct buffer_head **ret_dx_root_bh)
2117 int ret;
2118 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2119 u16 dr_suballoc_bit;
2120 u64 dr_blkno;
2121 unsigned int num_bits;
2122 struct buffer_head *dx_root_bh = NULL;
2123 struct ocfs2_dx_root_block *dx_root;
2125 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit,
2126 &num_bits, &dr_blkno);
2127 if (ret) {
2128 mlog_errno(ret);
2129 goto out;
2132 mlog(0, "Dir %llu, attach new index block: %llu\n",
2133 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2134 (unsigned long long)dr_blkno);
2136 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2137 if (dx_root_bh == NULL) {
2138 ret = -EIO;
2139 goto out;
2141 ocfs2_set_new_buffer_uptodate(dir, dx_root_bh);
2143 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
2144 OCFS2_JOURNAL_ACCESS_CREATE);
2145 if (ret < 0) {
2146 mlog_errno(ret);
2147 goto out;
2150 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2151 memset(dx_root, 0, osb->sb->s_blocksize);
2152 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2153 dx_root->dr_suballoc_slot = cpu_to_le16(osb->slot_num);
2154 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2155 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2156 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2157 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
2159 if (dx_inline) {
2160 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2161 dx_root->dr_entries.de_count =
2162 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2163 } else {
2164 dx_root->dr_list.l_count =
2165 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2168 ret = ocfs2_journal_dirty(handle, dx_root_bh);
2169 if (ret)
2170 mlog_errno(ret);
2172 ret = ocfs2_journal_access_di(handle, dir, di_bh,
2173 OCFS2_JOURNAL_ACCESS_CREATE);
2174 if (ret) {
2175 mlog_errno(ret);
2176 goto out;
2179 di->i_dx_root = cpu_to_le64(dr_blkno);
2181 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2182 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2184 ret = ocfs2_journal_dirty(handle, di_bh);
2185 if (ret)
2186 mlog_errno(ret);
2188 *ret_dx_root_bh = dx_root_bh;
2189 dx_root_bh = NULL;
2191 out:
2192 brelse(dx_root_bh);
2193 return ret;
2196 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2197 handle_t *handle, struct inode *dir,
2198 struct buffer_head **dx_leaves,
2199 int num_dx_leaves, u64 start_blk)
2201 int ret, i;
2202 struct ocfs2_dx_leaf *dx_leaf;
2203 struct buffer_head *bh;
2205 for (i = 0; i < num_dx_leaves; i++) {
2206 bh = sb_getblk(osb->sb, start_blk + i);
2207 if (bh == NULL) {
2208 ret = -EIO;
2209 goto out;
2211 dx_leaves[i] = bh;
2213 ocfs2_set_new_buffer_uptodate(dir, bh);
2215 ret = ocfs2_journal_access_dl(handle, dir, bh,
2216 OCFS2_JOURNAL_ACCESS_CREATE);
2217 if (ret < 0) {
2218 mlog_errno(ret);
2219 goto out;
2222 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2224 memset(dx_leaf, 0, osb->sb->s_blocksize);
2225 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2226 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2227 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2228 dx_leaf->dl_list.de_count =
2229 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2231 mlog(0,
2232 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2233 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2234 (unsigned long long)bh->b_blocknr,
2235 le16_to_cpu(dx_leaf->dl_list.de_count));
2237 ocfs2_journal_dirty(handle, bh);
2240 ret = 0;
2241 out:
2242 return ret;
2246 * Allocates and formats a new cluster for use in an indexed dir
2247 * leaf. This version will not do the extent insert, so that it can be
2248 * used by operations which need careful ordering.
2250 static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2251 u32 cpos, handle_t *handle,
2252 struct ocfs2_alloc_context *data_ac,
2253 struct buffer_head **dx_leaves,
2254 int num_dx_leaves, u64 *ret_phys_blkno)
2256 int ret;
2257 u32 phys, num;
2258 u64 phys_blkno;
2259 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2262 * XXX: For create, this should claim cluster for the index
2263 * *before* the unindexed insert so that we have a better
2264 * chance of contiguousness as the directory grows in number
2265 * of entries.
2267 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 1, &phys, &num);
2268 if (ret) {
2269 mlog_errno(ret);
2270 goto out;
2274 * Format the new cluster first. That way, we're inserting
2275 * valid data.
2277 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2278 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2279 num_dx_leaves, phys_blkno);
2280 if (ret) {
2281 mlog_errno(ret);
2282 goto out;
2285 *ret_phys_blkno = phys_blkno;
2286 out:
2287 return ret;
2290 static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2291 struct ocfs2_extent_tree *et,
2292 u32 cpos, handle_t *handle,
2293 struct ocfs2_alloc_context *data_ac,
2294 struct ocfs2_alloc_context *meta_ac,
2295 struct buffer_head **dx_leaves,
2296 int num_dx_leaves)
2298 int ret;
2299 u64 phys_blkno;
2300 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2302 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2303 num_dx_leaves, &phys_blkno);
2304 if (ret) {
2305 mlog_errno(ret);
2306 goto out;
2309 ret = ocfs2_insert_extent(osb, handle, dir, et, cpos, phys_blkno, 1, 0,
2310 meta_ac);
2311 if (ret)
2312 mlog_errno(ret);
2313 out:
2314 return ret;
2317 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2318 int *ret_num_leaves)
2320 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2321 struct buffer_head **dx_leaves;
2323 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2324 GFP_NOFS);
2325 if (dx_leaves && ret_num_leaves)
2326 *ret_num_leaves = num_dx_leaves;
2328 return dx_leaves;
2331 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2332 handle_t *handle,
2333 struct inode *parent,
2334 struct inode *inode,
2335 struct buffer_head *di_bh,
2336 struct ocfs2_alloc_context *data_ac,
2337 struct ocfs2_alloc_context *meta_ac)
2339 int ret;
2340 struct buffer_head *leaf_bh = NULL;
2341 struct buffer_head *dx_root_bh = NULL;
2342 struct ocfs2_dx_hinfo hinfo;
2343 struct ocfs2_dx_root_block *dx_root;
2344 struct ocfs2_dx_entry_list *entry_list;
2347 * Our strategy is to create the directory as though it were
2348 * unindexed, then add the index block. This works with very
2349 * little complication since the state of a new directory is a
2350 * very well known quantity.
2352 * Essentially, we have two dirents ("." and ".."), in the 1st
2353 * block which need indexing. These are easily inserted into
2354 * the index block.
2357 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2358 data_ac, &leaf_bh);
2359 if (ret) {
2360 mlog_errno(ret);
2361 goto out;
2364 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh,
2365 meta_ac, 1, &dx_root_bh);
2366 if (ret) {
2367 mlog_errno(ret);
2368 goto out;
2370 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2371 entry_list = &dx_root->dr_entries;
2373 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2374 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2376 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
2377 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2379 out:
2380 brelse(dx_root_bh);
2381 brelse(leaf_bh);
2382 return ret;
2385 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2386 handle_t *handle,
2387 struct inode *parent,
2388 struct inode *inode,
2389 struct buffer_head *fe_bh,
2390 struct ocfs2_alloc_context *data_ac,
2391 struct ocfs2_alloc_context *meta_ac)
2394 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2396 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2397 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2399 if (ocfs2_supports_indexed_dirs(osb))
2400 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2401 data_ac, meta_ac);
2403 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2404 data_ac, NULL);
2407 static int ocfs2_dx_dir_index_block(struct inode *dir,
2408 handle_t *handle,
2409 struct buffer_head **dx_leaves,
2410 int num_dx_leaves,
2411 struct buffer_head *dirent_bh)
2413 int ret, namelen, i;
2414 char *de_buf, *limit;
2415 struct ocfs2_dir_entry *de;
2416 struct buffer_head *dx_leaf_bh;
2417 struct ocfs2_dx_hinfo hinfo;
2418 u64 dirent_blk = dirent_bh->b_blocknr;
2420 de_buf = dirent_bh->b_data;
2421 limit = de_buf + dir->i_sb->s_blocksize;
2423 while (de_buf < limit) {
2424 de = (struct ocfs2_dir_entry *)de_buf;
2426 namelen = de->name_len;
2427 if (!namelen || !de->inode)
2428 goto inc;
2430 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2432 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2433 dx_leaf_bh = dx_leaves[i];
2435 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2436 dirent_blk, dx_leaf_bh);
2437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2442 inc:
2443 de_buf += le16_to_cpu(de->rec_len);
2446 out:
2447 return ret;
2450 * XXX: This expects dx_root_bh to already be part of the transaction.
2452 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2453 struct buffer_head *dx_root_bh,
2454 struct buffer_head *dirent_bh)
2456 char *de_buf, *limit;
2457 struct ocfs2_dx_root_block *dx_root;
2458 struct ocfs2_dir_entry *de;
2459 struct ocfs2_dx_hinfo hinfo;
2460 u64 dirent_blk = dirent_bh->b_blocknr;
2462 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2464 de_buf = dirent_bh->b_data;
2465 limit = de_buf + dir->i_sb->s_blocksize;
2467 while (de_buf < limit) {
2468 de = (struct ocfs2_dir_entry *)de_buf;
2470 if (!de->name_len || !de->inode)
2471 goto inc;
2473 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2475 mlog(0,
2476 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2477 (unsigned long long)dir->i_ino, hinfo.major_hash,
2478 hinfo.minor_hash,
2479 le16_to_cpu(dx_root->dr_entries.de_num_used),
2480 de->name_len, de->name);
2482 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2483 dirent_blk);
2484 inc:
2485 de_buf += le16_to_cpu(de->rec_len);
2490 * Count the number of inline directory entries in di_bh and compare
2491 * them against the number of entries we can hold in an inline dx root
2492 * block.
2494 static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2495 struct buffer_head *di_bh)
2497 int dirent_count = 0;
2498 char *de_buf, *limit;
2499 struct ocfs2_dir_entry *de;
2500 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2502 de_buf = di->id2.i_data.id_data;
2503 limit = de_buf + i_size_read(dir);
2505 while (de_buf < limit) {
2506 de = (struct ocfs2_dir_entry *)de_buf;
2508 if (de->name_len && de->inode)
2509 dirent_count++;
2511 de_buf += le16_to_cpu(de->rec_len);
2514 /* We are careful to leave room for one extra record. */
2515 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2519 * Expand rec_len of the rightmost dirent in a directory block so that it
2520 * contains the end of our valid space for dirents. We do this during
2521 * expansion from an inline directory to one with extents. The first dir block
2522 * in that case is taken from the inline data portion of the inode block.
2524 * We add the dir trailer if this filesystem wants it.
2526 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2527 struct super_block *sb)
2529 struct ocfs2_dir_entry *de;
2530 struct ocfs2_dir_entry *prev_de;
2531 char *de_buf, *limit;
2532 unsigned int new_size = sb->s_blocksize;
2533 unsigned int bytes;
2535 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
2536 new_size = ocfs2_dir_trailer_blk_off(sb);
2538 bytes = new_size - old_size;
2540 limit = start + old_size;
2541 de_buf = start;
2542 de = (struct ocfs2_dir_entry *)de_buf;
2543 do {
2544 prev_de = de;
2545 de_buf += le16_to_cpu(de->rec_len);
2546 de = (struct ocfs2_dir_entry *)de_buf;
2547 } while (de_buf < limit);
2549 le16_add_cpu(&prev_de->rec_len, bytes);
2553 * We allocate enough clusters to fulfill "blocks_wanted", but set
2554 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2555 * rest automatically for us.
2557 * *first_block_bh is a pointer to the 1st data block allocated to the
2558 * directory.
2560 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2561 unsigned int blocks_wanted,
2562 struct ocfs2_dir_lookup_result *lookup,
2563 struct buffer_head **first_block_bh)
2565 u32 alloc, dx_alloc, bit_off, len;
2566 struct super_block *sb = dir->i_sb;
2567 int ret, i, num_dx_leaves = 0, dx_inline = 0,
2568 credits = ocfs2_inline_to_extents_credits(sb);
2569 u64 dx_insert_blkno, blkno,
2570 bytes = blocks_wanted << sb->s_blocksize_bits;
2571 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2572 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2573 struct ocfs2_alloc_context *data_ac;
2574 struct ocfs2_alloc_context *meta_ac = NULL;
2575 struct buffer_head *dirdata_bh = NULL;
2576 struct buffer_head *dx_root_bh = NULL;
2577 struct buffer_head **dx_leaves = NULL;
2578 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2579 handle_t *handle;
2580 struct ocfs2_extent_tree et;
2581 struct ocfs2_extent_tree dx_et;
2582 int did_quota = 0, bytes_allocated = 0;
2584 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
2586 alloc = ocfs2_clusters_for_bytes(sb, bytes);
2587 dx_alloc = 0;
2589 if (ocfs2_supports_indexed_dirs(osb)) {
2590 credits += ocfs2_add_dir_index_credits(sb);
2592 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2593 if (!dx_inline) {
2594 /* Add one more cluster for an index leaf */
2595 dx_alloc++;
2596 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2597 &num_dx_leaves);
2598 if (!dx_leaves) {
2599 ret = -ENOMEM;
2600 mlog_errno(ret);
2601 goto out;
2605 /* This gets us the dx_root */
2606 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2607 if (ret) {
2608 mlog_errno(ret);
2609 goto out;
2614 * We should never need more than 2 clusters for the unindexed
2615 * tree - maximum dirent size is far less than one block. In
2616 * fact, the only time we'd need more than one cluster is if
2617 * blocksize == clustersize and the dirent won't fit in the
2618 * extra space that the expansion to a single block gives. As
2619 * of today, that only happens on 4k/4k file systems.
2621 BUG_ON(alloc > 2);
2623 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
2624 if (ret) {
2625 mlog_errno(ret);
2626 goto out;
2629 down_write(&oi->ip_alloc_sem);
2632 * Prepare for worst case allocation scenario of two separate
2633 * extents in the unindexed tree.
2635 if (alloc == 2)
2636 credits += OCFS2_SUBALLOC_ALLOC;
2638 handle = ocfs2_start_trans(osb, credits);
2639 if (IS_ERR(handle)) {
2640 ret = PTR_ERR(handle);
2641 mlog_errno(ret);
2642 goto out_sem;
2645 if (vfs_dq_alloc_space_nodirty(dir,
2646 ocfs2_clusters_to_bytes(osb->sb,
2647 alloc + dx_alloc))) {
2648 ret = -EDQUOT;
2649 goto out_commit;
2651 did_quota = 1;
2653 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2655 * Allocate our index cluster first, to maximize the
2656 * possibility that unindexed leaves grow
2657 * contiguously.
2659 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2660 dx_leaves, num_dx_leaves,
2661 &dx_insert_blkno);
2662 if (ret) {
2663 mlog_errno(ret);
2664 goto out_commit;
2666 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2670 * Try to claim as many clusters as the bitmap can give though
2671 * if we only get one now, that's enough to continue. The rest
2672 * will be claimed after the conversion to extents.
2674 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2675 if (ret) {
2676 mlog_errno(ret);
2677 goto out_commit;
2679 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2682 * Operations are carefully ordered so that we set up the new
2683 * data block first. The conversion from inline data to
2684 * extents follows.
2686 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2687 dirdata_bh = sb_getblk(sb, blkno);
2688 if (!dirdata_bh) {
2689 ret = -EIO;
2690 mlog_errno(ret);
2691 goto out_commit;
2694 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
2696 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
2697 OCFS2_JOURNAL_ACCESS_CREATE);
2698 if (ret) {
2699 mlog_errno(ret);
2700 goto out_commit;
2703 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2704 memset(dirdata_bh->b_data + i_size_read(dir), 0,
2705 sb->s_blocksize - i_size_read(dir));
2706 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
2707 if (ocfs2_supports_dir_trailer(osb))
2708 ocfs2_init_dir_trailer(dir, dirdata_bh);
2710 ret = ocfs2_journal_dirty(handle, dirdata_bh);
2711 if (ret) {
2712 mlog_errno(ret);
2713 goto out_commit;
2716 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2718 * Dx dirs with an external cluster need to do this up
2719 * front. Inline dx root's get handled later, after
2720 * we've allocated our root block.
2722 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
2723 num_dx_leaves, dirdata_bh);
2724 if (ret) {
2725 mlog_errno(ret);
2726 goto out_commit;
2731 * Set extent, i_size, etc on the directory. After this, the
2732 * inode should contain the same exact dirents as before and
2733 * be fully accessible from system calls.
2735 * We let the later dirent insert modify c/mtime - to the user
2736 * the data hasn't changed.
2738 ret = ocfs2_journal_access_di(handle, dir, di_bh,
2739 OCFS2_JOURNAL_ACCESS_CREATE);
2740 if (ret) {
2741 mlog_errno(ret);
2742 goto out_commit;
2745 spin_lock(&oi->ip_lock);
2746 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
2747 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2748 spin_unlock(&oi->ip_lock);
2750 ocfs2_dinode_new_extent_list(dir, di);
2752 i_size_write(dir, sb->s_blocksize);
2753 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2755 di->i_size = cpu_to_le64(sb->s_blocksize);
2756 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
2757 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
2760 * This should never fail as our extent list is empty and all
2761 * related blocks have been journaled already.
2763 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
2764 0, NULL);
2765 if (ret) {
2766 mlog_errno(ret);
2767 goto out_commit;
2771 * Set i_blocks after the extent insert for the most up to
2772 * date ip_clusters value.
2774 dir->i_blocks = ocfs2_inode_sector_count(dir);
2776 ret = ocfs2_journal_dirty(handle, di_bh);
2777 if (ret) {
2778 mlog_errno(ret);
2779 goto out_commit;
2782 if (ocfs2_supports_indexed_dirs(osb)) {
2783 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
2784 meta_ac, dx_inline,
2785 &dx_root_bh);
2786 if (ret) {
2787 mlog_errno(ret);
2788 goto out_commit;
2791 if (dx_inline) {
2792 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
2793 dirdata_bh);
2794 } else {
2795 ocfs2_init_dx_root_extent_tree(&dx_et, dir, dx_root_bh);
2796 ret = ocfs2_insert_extent(osb, handle, dir, &dx_et, 0,
2797 dx_insert_blkno, 1, 0, NULL);
2798 if (ret)
2799 mlog_errno(ret);
2804 * We asked for two clusters, but only got one in the 1st
2805 * pass. Claim the 2nd cluster as a separate extent.
2807 if (alloc > len) {
2808 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
2809 &len);
2810 if (ret) {
2811 mlog_errno(ret);
2812 goto out_commit;
2814 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2816 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
2817 blkno, len, 0, NULL);
2818 if (ret) {
2819 mlog_errno(ret);
2820 goto out_commit;
2822 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2825 *first_block_bh = dirdata_bh;
2826 dirdata_bh = NULL;
2827 if (ocfs2_supports_indexed_dirs(osb)) {
2828 unsigned int off;
2830 if (!dx_inline) {
2832 * We need to return the correct block within the
2833 * cluster which should hold our entry.
2835 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
2836 &lookup->dl_hinfo);
2837 get_bh(dx_leaves[off]);
2838 lookup->dl_dx_leaf_bh = dx_leaves[off];
2840 lookup->dl_dx_root_bh = dx_root_bh;
2841 dx_root_bh = NULL;
2844 out_commit:
2845 if (ret < 0 && did_quota)
2846 vfs_dq_free_space_nodirty(dir, bytes_allocated);
2848 ocfs2_commit_trans(osb, handle);
2850 out_sem:
2851 up_write(&oi->ip_alloc_sem);
2853 out:
2854 if (data_ac)
2855 ocfs2_free_alloc_context(data_ac);
2856 if (meta_ac)
2857 ocfs2_free_alloc_context(meta_ac);
2859 if (dx_leaves) {
2860 for (i = 0; i < num_dx_leaves; i++)
2861 brelse(dx_leaves[i]);
2862 kfree(dx_leaves);
2865 brelse(dirdata_bh);
2866 brelse(dx_root_bh);
2868 return ret;
2871 /* returns a bh of the 1st new block in the allocation. */
2872 static int ocfs2_do_extend_dir(struct super_block *sb,
2873 handle_t *handle,
2874 struct inode *dir,
2875 struct buffer_head *parent_fe_bh,
2876 struct ocfs2_alloc_context *data_ac,
2877 struct ocfs2_alloc_context *meta_ac,
2878 struct buffer_head **new_bh)
2880 int status;
2881 int extend, did_quota = 0;
2882 u64 p_blkno, v_blkno;
2884 spin_lock(&OCFS2_I(dir)->ip_lock);
2885 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
2886 spin_unlock(&OCFS2_I(dir)->ip_lock);
2888 if (extend) {
2889 u32 offset = OCFS2_I(dir)->ip_clusters;
2891 if (vfs_dq_alloc_space_nodirty(dir,
2892 ocfs2_clusters_to_bytes(sb, 1))) {
2893 status = -EDQUOT;
2894 goto bail;
2896 did_quota = 1;
2898 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
2899 1, 0, parent_fe_bh, handle,
2900 data_ac, meta_ac, NULL);
2901 BUG_ON(status == -EAGAIN);
2902 if (status < 0) {
2903 mlog_errno(status);
2904 goto bail;
2908 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
2909 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
2910 if (status < 0) {
2911 mlog_errno(status);
2912 goto bail;
2915 *new_bh = sb_getblk(sb, p_blkno);
2916 if (!*new_bh) {
2917 status = -EIO;
2918 mlog_errno(status);
2919 goto bail;
2921 status = 0;
2922 bail:
2923 if (did_quota && status < 0)
2924 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
2925 mlog_exit(status);
2926 return status;
2930 * Assumes you already have a cluster lock on the directory.
2932 * 'blocks_wanted' is only used if we have an inline directory which
2933 * is to be turned into an extent based one. The size of the dirent to
2934 * insert might be larger than the space gained by growing to just one
2935 * block, so we may have to grow the inode by two blocks in that case.
2937 static int ocfs2_extend_dir(struct ocfs2_super *osb,
2938 struct inode *dir,
2939 struct buffer_head *parent_fe_bh,
2940 unsigned int blocks_wanted,
2941 struct ocfs2_dir_lookup_result *lookup,
2942 struct buffer_head **new_de_bh)
2944 int status = 0;
2945 int credits, num_free_extents, drop_alloc_sem = 0;
2946 loff_t dir_i_size;
2947 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
2948 struct ocfs2_extent_list *el = &fe->id2.i_list;
2949 struct ocfs2_alloc_context *data_ac = NULL;
2950 struct ocfs2_alloc_context *meta_ac = NULL;
2951 handle_t *handle = NULL;
2952 struct buffer_head *new_bh = NULL;
2953 struct ocfs2_dir_entry * de;
2954 struct super_block *sb = osb->sb;
2955 struct ocfs2_extent_tree et;
2957 mlog_entry_void();
2959 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2960 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
2961 blocks_wanted, lookup,
2962 &new_bh);
2963 if (status) {
2964 mlog_errno(status);
2965 goto bail;
2968 if (blocks_wanted == 1) {
2970 * If the new dirent will fit inside the space
2971 * created by pushing out to one block, then
2972 * we can complete the operation
2973 * here. Otherwise we have to expand i_size
2974 * and format the 2nd block below.
2976 BUG_ON(new_bh == NULL);
2977 goto bail_bh;
2981 * Get rid of 'new_bh' - we want to format the 2nd
2982 * data block and return that instead.
2984 brelse(new_bh);
2985 new_bh = NULL;
2987 dir_i_size = i_size_read(dir);
2988 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
2989 goto do_extend;
2992 dir_i_size = i_size_read(dir);
2993 mlog(0, "extending dir %llu (i_size = %lld)\n",
2994 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
2996 /* dir->i_size is always block aligned. */
2997 spin_lock(&OCFS2_I(dir)->ip_lock);
2998 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
2999 spin_unlock(&OCFS2_I(dir)->ip_lock);
3000 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
3001 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
3002 if (num_free_extents < 0) {
3003 status = num_free_extents;
3004 mlog_errno(status);
3005 goto bail;
3008 if (!num_free_extents) {
3009 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
3010 if (status < 0) {
3011 if (status != -ENOSPC)
3012 mlog_errno(status);
3013 goto bail;
3017 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
3018 if (status < 0) {
3019 if (status != -ENOSPC)
3020 mlog_errno(status);
3021 goto bail;
3024 credits = ocfs2_calc_extend_credits(sb, el, 1);
3025 } else {
3026 spin_unlock(&OCFS2_I(dir)->ip_lock);
3027 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3030 do_extend:
3031 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3032 drop_alloc_sem = 1;
3034 handle = ocfs2_start_trans(osb, credits);
3035 if (IS_ERR(handle)) {
3036 status = PTR_ERR(handle);
3037 handle = NULL;
3038 mlog_errno(status);
3039 goto bail;
3042 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3043 data_ac, meta_ac, &new_bh);
3044 if (status < 0) {
3045 mlog_errno(status);
3046 goto bail;
3049 ocfs2_set_new_buffer_uptodate(dir, new_bh);
3051 status = ocfs2_journal_access_db(handle, dir, new_bh,
3052 OCFS2_JOURNAL_ACCESS_CREATE);
3053 if (status < 0) {
3054 mlog_errno(status);
3055 goto bail;
3057 memset(new_bh->b_data, 0, sb->s_blocksize);
3059 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3060 de->inode = 0;
3061 if (ocfs2_dir_has_trailer(dir)) {
3062 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
3063 ocfs2_init_dir_trailer(dir, new_bh);
3064 } else {
3065 de->rec_len = cpu_to_le16(sb->s_blocksize);
3067 status = ocfs2_journal_dirty(handle, new_bh);
3068 if (status < 0) {
3069 mlog_errno(status);
3070 goto bail;
3073 dir_i_size += dir->i_sb->s_blocksize;
3074 i_size_write(dir, dir_i_size);
3075 dir->i_blocks = ocfs2_inode_sector_count(dir);
3076 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3077 if (status < 0) {
3078 mlog_errno(status);
3079 goto bail;
3082 bail_bh:
3083 *new_de_bh = new_bh;
3084 get_bh(*new_de_bh);
3085 bail:
3086 if (drop_alloc_sem)
3087 up_write(&OCFS2_I(dir)->ip_alloc_sem);
3088 if (handle)
3089 ocfs2_commit_trans(osb, handle);
3091 if (data_ac)
3092 ocfs2_free_alloc_context(data_ac);
3093 if (meta_ac)
3094 ocfs2_free_alloc_context(meta_ac);
3096 brelse(new_bh);
3098 mlog_exit(status);
3099 return status;
3102 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3103 const char *name, int namelen,
3104 struct buffer_head **ret_de_bh,
3105 unsigned int *blocks_wanted)
3107 int ret;
3108 struct super_block *sb = dir->i_sb;
3109 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3110 struct ocfs2_dir_entry *de, *last_de = NULL;
3111 char *de_buf, *limit;
3112 unsigned long offset = 0;
3113 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3116 * This calculates how many free bytes we'd have in block zero, should
3117 * this function force expansion to an extent tree.
3119 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
3120 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3121 else
3122 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
3124 de_buf = di->id2.i_data.id_data;
3125 limit = de_buf + i_size_read(dir);
3126 rec_len = OCFS2_DIR_REC_LEN(namelen);
3128 while (de_buf < limit) {
3129 de = (struct ocfs2_dir_entry *)de_buf;
3131 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3132 ret = -ENOENT;
3133 goto out;
3135 if (ocfs2_match(namelen, name, de)) {
3136 ret = -EEXIST;
3137 goto out;
3140 * No need to check for a trailing dirent record here as
3141 * they're not used for inline dirs.
3144 if (ocfs2_dirent_would_fit(de, rec_len)) {
3145 /* Ok, we found a spot. Return this bh and let
3146 * the caller actually fill it in. */
3147 *ret_de_bh = di_bh;
3148 get_bh(*ret_de_bh);
3149 ret = 0;
3150 goto out;
3153 last_de = de;
3154 de_buf += le16_to_cpu(de->rec_len);
3155 offset += le16_to_cpu(de->rec_len);
3159 * We're going to require expansion of the directory - figure
3160 * out how many blocks we'll need so that a place for the
3161 * dirent can be found.
3163 *blocks_wanted = 1;
3164 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
3165 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3166 *blocks_wanted = 2;
3168 ret = -ENOSPC;
3169 out:
3170 return ret;
3173 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3174 int namelen, struct buffer_head **ret_de_bh)
3176 unsigned long offset;
3177 struct buffer_head *bh = NULL;
3178 unsigned short rec_len;
3179 struct ocfs2_dir_entry *de;
3180 struct super_block *sb = dir->i_sb;
3181 int status;
3182 int blocksize = dir->i_sb->s_blocksize;
3184 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3185 if (status) {
3186 mlog_errno(status);
3187 goto bail;
3190 rec_len = OCFS2_DIR_REC_LEN(namelen);
3191 offset = 0;
3192 de = (struct ocfs2_dir_entry *) bh->b_data;
3193 while (1) {
3194 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3195 brelse(bh);
3196 bh = NULL;
3198 if (i_size_read(dir) <= offset) {
3200 * Caller will have to expand this
3201 * directory.
3203 status = -ENOSPC;
3204 goto bail;
3206 status = ocfs2_read_dir_block(dir,
3207 offset >> sb->s_blocksize_bits,
3208 &bh, 0);
3209 if (status) {
3210 mlog_errno(status);
3211 goto bail;
3213 /* move to next block */
3214 de = (struct ocfs2_dir_entry *) bh->b_data;
3216 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3217 status = -ENOENT;
3218 goto bail;
3220 if (ocfs2_match(namelen, name, de)) {
3221 status = -EEXIST;
3222 goto bail;
3225 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3226 blocksize))
3227 goto next;
3229 if (ocfs2_dirent_would_fit(de, rec_len)) {
3230 /* Ok, we found a spot. Return this bh and let
3231 * the caller actually fill it in. */
3232 *ret_de_bh = bh;
3233 get_bh(*ret_de_bh);
3234 status = 0;
3235 goto bail;
3237 next:
3238 offset += le16_to_cpu(de->rec_len);
3239 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3242 status = 0;
3243 bail:
3244 brelse(bh);
3246 mlog_exit(status);
3247 return status;
3250 static int dx_leaf_sort_cmp(const void *a, const void *b)
3252 const struct ocfs2_dx_entry *entry1 = a;
3253 const struct ocfs2_dx_entry *entry2 = b;
3254 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3255 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3256 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3257 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3259 if (major_hash1 > major_hash2)
3260 return 1;
3261 if (major_hash1 < major_hash2)
3262 return -1;
3265 * It is not strictly necessary to sort by minor
3267 if (minor_hash1 > minor_hash2)
3268 return 1;
3269 if (minor_hash1 < minor_hash2)
3270 return -1;
3271 return 0;
3274 static void dx_leaf_sort_swap(void *a, void *b, int size)
3276 struct ocfs2_dx_entry *entry1 = a;
3277 struct ocfs2_dx_entry *entry2 = b;
3278 struct ocfs2_dx_entry tmp;
3280 BUG_ON(size != sizeof(*entry1));
3282 tmp = *entry1;
3283 *entry1 = *entry2;
3284 *entry2 = tmp;
3287 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3289 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3290 int i, num = le16_to_cpu(dl_list->de_num_used);
3292 for (i = 0; i < (num - 1); i++) {
3293 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3294 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3295 return 0;
3298 return 1;
3302 * Find the optimal value to split this leaf on. This expects the leaf
3303 * entries to be in sorted order.
3305 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3306 * the hash we want to insert.
3308 * This function is only concerned with the major hash - that which
3309 * determines which cluster an item belongs to.
3311 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3312 u32 leaf_cpos, u32 insert_hash,
3313 u32 *split_hash)
3315 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3316 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3317 int allsame;
3320 * There's a couple rare, but nasty corner cases we have to
3321 * check for here. All of them involve a leaf where all value
3322 * have the same hash, which is what we look for first.
3324 * Most of the time, all of the above is false, and we simply
3325 * pick the median value for a split.
3327 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3328 if (allsame) {
3329 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3331 if (val == insert_hash) {
3333 * No matter where we would choose to split,
3334 * the new entry would want to occupy the same
3335 * block as these. Since there's no space left
3336 * in their existing block, we know there
3337 * won't be space after the split.
3339 return -ENOSPC;
3342 if (val == leaf_cpos) {
3344 * Because val is the same as leaf_cpos (which
3345 * is the smallest value this leaf can have),
3346 * yet is not equal to insert_hash, then we
3347 * know that insert_hash *must* be larger than
3348 * val (and leaf_cpos). At least cpos+1 in value.
3350 * We also know then, that there cannot be an
3351 * adjacent extent (otherwise we'd be looking
3352 * at it). Choosing this value gives us a
3353 * chance to get some contiguousness.
3355 *split_hash = leaf_cpos + 1;
3356 return 0;
3359 if (val > insert_hash) {
3361 * val can not be the same as insert hash, and
3362 * also must be larger than leaf_cpos. Also,
3363 * we know that there can't be a leaf between
3364 * cpos and val, otherwise the entries with
3365 * hash 'val' would be there.
3367 *split_hash = val;
3368 return 0;
3371 *split_hash = insert_hash;
3372 return 0;
3376 * Since the records are sorted and the checks above
3377 * guaranteed that not all records in this block are the same,
3378 * we simple travel forward, from the median, and pick the 1st
3379 * record whose value is larger than leaf_cpos.
3381 for (i = (num_used / 2); i < num_used; i++)
3382 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3383 leaf_cpos)
3384 break;
3386 BUG_ON(i == num_used); /* Should be impossible */
3387 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3388 return 0;
3392 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3393 * larger than split_hash into new_dx_leaves. We use a temporary
3394 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3396 * Since the block offset inside a leaf (cluster) is a constant mask
3397 * of minor_hash, we can optimize - an item at block offset X within
3398 * the original cluster, will be at offset X within the new cluster.
3400 static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3401 handle_t *handle,
3402 struct ocfs2_dx_leaf *tmp_dx_leaf,
3403 struct buffer_head **orig_dx_leaves,
3404 struct buffer_head **new_dx_leaves,
3405 int num_dx_leaves)
3407 int i, j, num_used;
3408 u32 major_hash;
3409 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3410 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3411 struct ocfs2_dx_entry *dx_entry;
3413 tmp_list = &tmp_dx_leaf->dl_list;
3415 for (i = 0; i < num_dx_leaves; i++) {
3416 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3417 orig_list = &orig_dx_leaf->dl_list;
3418 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3419 new_list = &new_dx_leaf->dl_list;
3421 num_used = le16_to_cpu(orig_list->de_num_used);
3423 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3424 tmp_list->de_num_used = cpu_to_le16(0);
3425 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3427 for (j = 0; j < num_used; j++) {
3428 dx_entry = &orig_list->de_entries[j];
3429 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3430 if (major_hash >= split_hash)
3431 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3432 dx_entry);
3433 else
3434 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3435 dx_entry);
3437 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3439 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3440 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3444 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3445 struct ocfs2_dx_root_block *dx_root)
3447 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3449 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
3450 credits += ocfs2_quota_trans_credits(osb->sb);
3451 return credits;
3455 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3456 * half our entries into.
3458 static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3459 struct buffer_head *dx_root_bh,
3460 struct buffer_head *dx_leaf_bh,
3461 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3462 u64 leaf_blkno)
3464 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3465 int credits, ret, i, num_used, did_quota = 0;
3466 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3467 u64 orig_leaves_start;
3468 int num_dx_leaves;
3469 struct buffer_head **orig_dx_leaves = NULL;
3470 struct buffer_head **new_dx_leaves = NULL;
3471 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3472 struct ocfs2_extent_tree et;
3473 handle_t *handle = NULL;
3474 struct ocfs2_dx_root_block *dx_root;
3475 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3477 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3478 (unsigned long long)OCFS2_I(dir)->ip_blkno,
3479 (unsigned long long)leaf_blkno, insert_hash);
3481 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
3483 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3485 * XXX: This is a rather large limit. We should use a more
3486 * realistic value.
3488 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3489 return -ENOSPC;
3491 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3492 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3493 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3494 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3495 (unsigned long long)leaf_blkno, num_used);
3496 ret = -EIO;
3497 goto out;
3500 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3501 if (!orig_dx_leaves) {
3502 ret = -ENOMEM;
3503 mlog_errno(ret);
3504 goto out;
3507 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3508 if (!new_dx_leaves) {
3509 ret = -ENOMEM;
3510 mlog_errno(ret);
3511 goto out;
3514 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3515 if (ret) {
3516 if (ret != -ENOSPC)
3517 mlog_errno(ret);
3518 goto out;
3521 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3522 handle = ocfs2_start_trans(osb, credits);
3523 if (IS_ERR(handle)) {
3524 ret = PTR_ERR(handle);
3525 handle = NULL;
3526 mlog_errno(ret);
3527 goto out;
3530 if (vfs_dq_alloc_space_nodirty(dir,
3531 ocfs2_clusters_to_bytes(dir->i_sb, 1))) {
3532 ret = -EDQUOT;
3533 goto out_commit;
3535 did_quota = 1;
3537 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
3538 OCFS2_JOURNAL_ACCESS_WRITE);
3539 if (ret) {
3540 mlog_errno(ret);
3541 goto out_commit;
3545 * This block is changing anyway, so we can sort it in place.
3547 sort(dx_leaf->dl_list.de_entries, num_used,
3548 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3549 dx_leaf_sort_swap);
3551 ret = ocfs2_journal_dirty(handle, dx_leaf_bh);
3552 if (ret) {
3553 mlog_errno(ret);
3554 goto out_commit;
3557 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3558 &split_hash);
3559 if (ret) {
3560 mlog_errno(ret);
3561 goto out_commit;
3564 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3565 leaf_cpos, split_hash, insert_hash);
3568 * We have to carefully order operations here. There are items
3569 * which want to be in the new cluster before insert, but in
3570 * order to put those items in the new cluster, we alter the
3571 * old cluster. A failure to insert gets nasty.
3573 * So, start by reserving writes to the old
3574 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3575 * the new cluster for us, before inserting it. The insert
3576 * won't happen if there's an error before that. Once the
3577 * insert is done then, we can transfer from one leaf into the
3578 * other without fear of hitting any error.
3582 * The leaf transfer wants some scratch space so that we don't
3583 * wind up doing a bunch of expensive memmove().
3585 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3586 if (!tmp_dx_leaf) {
3587 ret = -ENOMEM;
3588 mlog_errno(ret);
3589 goto out_commit;
3592 orig_leaves_start = leaf_blkno & ~(osb->s_clustersize_bits -
3593 osb->sb->s_blocksize_bits);
3594 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3595 orig_dx_leaves);
3596 if (ret) {
3597 mlog_errno(ret);
3598 goto out_commit;
3601 for (i = 0; i < num_dx_leaves; i++) {
3602 ret = ocfs2_journal_access_dl(handle, dir, orig_dx_leaves[i],
3603 OCFS2_JOURNAL_ACCESS_WRITE);
3604 if (ret) {
3605 mlog_errno(ret);
3606 goto out_commit;
3610 cpos = split_hash;
3611 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3612 data_ac, meta_ac, new_dx_leaves,
3613 num_dx_leaves);
3614 if (ret) {
3615 mlog_errno(ret);
3616 goto out_commit;
3619 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3620 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3622 out_commit:
3623 if (ret < 0 && did_quota)
3624 vfs_dq_free_space_nodirty(dir,
3625 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3627 ocfs2_commit_trans(osb, handle);
3629 out:
3630 if (orig_dx_leaves || new_dx_leaves) {
3631 for (i = 0; i < num_dx_leaves; i++) {
3632 if (orig_dx_leaves)
3633 brelse(orig_dx_leaves[i]);
3634 if (new_dx_leaves)
3635 brelse(new_dx_leaves[i]);
3637 kfree(orig_dx_leaves);
3638 kfree(new_dx_leaves);
3641 if (meta_ac)
3642 ocfs2_free_alloc_context(meta_ac);
3643 if (data_ac)
3644 ocfs2_free_alloc_context(data_ac);
3646 kfree(tmp_dx_leaf);
3647 return ret;
3650 static int ocfs2_expand_inline_dx_root(struct inode *dir,
3651 struct buffer_head *dx_root_bh)
3653 int ret, num_dx_leaves, i, j, did_quota = 0;
3654 struct buffer_head **dx_leaves = NULL;
3655 struct ocfs2_extent_tree et;
3656 u64 insert_blkno;
3657 struct ocfs2_alloc_context *data_ac = NULL;
3658 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
3659 handle_t *handle = NULL;
3660 struct ocfs2_dx_root_block *dx_root;
3661 struct ocfs2_dx_entry_list *entry_list;
3662 struct ocfs2_dx_entry *dx_entry;
3663 struct ocfs2_dx_leaf *target_leaf;
3665 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
3666 if (ret) {
3667 mlog_errno(ret);
3668 goto out;
3671 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3672 if (!dx_leaves) {
3673 ret = -ENOMEM;
3674 mlog_errno(ret);
3675 goto out;
3678 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
3679 if (IS_ERR(handle)) {
3680 ret = PTR_ERR(handle);
3681 mlog_errno(ret);
3682 goto out;
3685 if (vfs_dq_alloc_space_nodirty(dir,
3686 ocfs2_clusters_to_bytes(osb->sb, 1))) {
3687 ret = -EDQUOT;
3688 goto out_commit;
3690 did_quota = 1;
3693 * We do this up front, before the allocation, so that a
3694 * failure to add the dx_root_bh to the journal won't result
3695 * us losing clusters.
3697 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
3698 OCFS2_JOURNAL_ACCESS_WRITE);
3699 if (ret) {
3700 mlog_errno(ret);
3701 goto out_commit;
3704 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
3705 num_dx_leaves, &insert_blkno);
3706 if (ret) {
3707 mlog_errno(ret);
3708 goto out_commit;
3712 * Transfer the entries from our dx_root into the appropriate
3713 * block
3715 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3716 entry_list = &dx_root->dr_entries;
3718 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
3719 dx_entry = &entry_list->de_entries[i];
3721 j = __ocfs2_dx_dir_hash_idx(osb,
3722 le32_to_cpu(dx_entry->dx_minor_hash));
3723 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
3725 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
3727 /* Each leaf has been passed to the journal already
3728 * via __ocfs2_dx_dir_new_cluster() */
3731 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
3732 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
3733 offsetof(struct ocfs2_dx_root_block, dr_list));
3734 dx_root->dr_list.l_count =
3735 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
3737 /* This should never fail considering we start with an empty
3738 * dx_root. */
3739 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
3740 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0,
3741 insert_blkno, 1, 0, NULL);
3742 if (ret)
3743 mlog_errno(ret);
3744 did_quota = 0;
3746 ocfs2_journal_dirty(handle, dx_root_bh);
3748 out_commit:
3749 if (ret < 0 && did_quota)
3750 vfs_dq_free_space_nodirty(dir,
3751 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3753 ocfs2_commit_trans(osb, handle);
3755 out:
3756 if (data_ac)
3757 ocfs2_free_alloc_context(data_ac);
3759 if (dx_leaves) {
3760 for (i = 0; i < num_dx_leaves; i++)
3761 brelse(dx_leaves[i]);
3762 kfree(dx_leaves);
3764 return ret;
3767 static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
3769 struct ocfs2_dx_root_block *dx_root;
3770 struct ocfs2_dx_entry_list *entry_list;
3772 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3773 entry_list = &dx_root->dr_entries;
3775 if (le16_to_cpu(entry_list->de_num_used) >=
3776 le16_to_cpu(entry_list->de_count))
3777 return -ENOSPC;
3779 return 0;
3782 static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3783 struct buffer_head *di_bh, const char *name,
3784 int namelen,
3785 struct ocfs2_dir_lookup_result *lookup)
3787 int ret, rebalanced = 0;
3788 struct buffer_head *dx_root_bh = NULL;
3789 struct ocfs2_dx_root_block *dx_root;
3790 struct buffer_head *dx_leaf_bh = NULL;
3791 struct ocfs2_dx_leaf *dx_leaf;
3792 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3793 u64 blkno;
3794 u32 leaf_cpos;
3796 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
3797 if (ret) {
3798 mlog_errno(ret);
3799 goto out;
3802 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3803 if (ocfs2_dx_root_inline(dx_root)) {
3804 ret = ocfs2_inline_dx_has_space(dx_root_bh);
3806 if (ret == 0)
3807 goto search_el;
3810 * We ran out of room in the root block. Expand it to
3811 * an extent, then allow ocfs2_find_dir_space_dx to do
3812 * the rest.
3814 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
3815 if (ret) {
3816 mlog_errno(ret);
3817 goto out;
3821 restart_search:
3822 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3823 &leaf_cpos, &blkno);
3824 if (ret) {
3825 mlog_errno(ret);
3826 goto out;
3829 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3830 if (ret) {
3831 mlog_errno(ret);
3832 goto out;
3835 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3837 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3838 le16_to_cpu(dx_leaf->dl_list.de_count)) {
3839 if (rebalanced) {
3841 * Rebalancing should have provided us with
3842 * space in an appropriate leaf.
3844 * XXX: Is this an abnormal condition then?
3845 * Should we print a message here?
3847 ret = -ENOSPC;
3848 goto out;
3851 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3852 &lookup->dl_hinfo, leaf_cpos,
3853 blkno);
3854 if (ret) {
3855 if (ret != -ENOSPC)
3856 mlog_errno(ret);
3857 goto out;
3861 * Restart the lookup. The rebalance might have
3862 * changed which block our item fits into. Mark our
3863 * progress, so we only execute this once.
3865 brelse(dx_leaf_bh);
3866 dx_leaf_bh = NULL;
3867 rebalanced = 1;
3868 goto restart_search;
3871 search_el:
3872 lookup->dl_dx_leaf_bh = dx_leaf_bh;
3873 dx_leaf_bh = NULL;
3874 lookup->dl_dx_root_bh = dx_root_bh;
3875 dx_root_bh = NULL;
3877 out:
3878 brelse(dx_leaf_bh);
3879 brelse(dx_root_bh);
3880 return ret;
3884 * Get a directory ready for insert. Any directory allocation required
3885 * happens here. Success returns zero, and enough context in the dir
3886 * lookup result that ocfs2_add_entry() will be able complete the task
3887 * with minimal performance impact.
3889 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
3890 struct inode *dir,
3891 struct buffer_head *parent_fe_bh,
3892 const char *name,
3893 int namelen,
3894 struct ocfs2_dir_lookup_result *lookup)
3896 int ret;
3897 unsigned int blocks_wanted = 1;
3898 struct buffer_head *bh = NULL;
3900 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
3901 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
3903 if (!namelen) {
3904 ret = -EINVAL;
3905 mlog_errno(ret);
3906 goto out;
3910 * Do this up front to reduce confusion.
3912 * The directory might start inline, then be turned into an
3913 * indexed one, in which case we'd need to hash deep inside
3914 * ocfs2_find_dir_space_id(). Since
3915 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
3916 * done, there seems no point in spreading out the calls. We
3917 * can optimize away the case where the file system doesn't
3918 * support indexing.
3920 if (ocfs2_supports_indexed_dirs(osb))
3921 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
3923 if (ocfs2_dir_indexed(dir)) {
3924 ret = ocfs2_find_dir_space_dx(osb, dir, parent_fe_bh, name,
3925 namelen, lookup);
3926 if (ret) {
3927 mlog_errno(ret);
3928 goto out;
3932 * We intentionally fall through so that the unindexed
3933 * tree can also be prepared.
3937 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
3938 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
3939 namelen, &bh, &blocks_wanted);
3940 } else
3941 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
3943 if (ret && ret != -ENOSPC) {
3944 mlog_errno(ret);
3945 goto out;
3948 if (ret == -ENOSPC) {
3950 * We have to expand the directory to add this name.
3952 BUG_ON(bh);
3954 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
3955 lookup, &bh);
3956 if (ret) {
3957 if (ret != -ENOSPC)
3958 mlog_errno(ret);
3959 goto out;
3962 BUG_ON(!bh);
3965 lookup->dl_leaf_bh = bh;
3966 bh = NULL;
3967 out:
3968 brelse(bh);
3969 return ret;
3972 static int ocfs2_dx_dir_remove_index(struct inode *dir,
3973 struct buffer_head *di_bh,
3974 struct buffer_head *dx_root_bh)
3976 int ret;
3977 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
3978 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3979 struct ocfs2_dx_root_block *dx_root;
3980 struct inode *dx_alloc_inode = NULL;
3981 struct buffer_head *dx_alloc_bh = NULL;
3982 handle_t *handle;
3983 u64 blk;
3984 u16 bit;
3985 u64 bg_blkno;
3987 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3989 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
3990 EXTENT_ALLOC_SYSTEM_INODE,
3991 le16_to_cpu(dx_root->dr_suballoc_slot));
3992 if (!dx_alloc_inode) {
3993 ret = -ENOMEM;
3994 mlog_errno(ret);
3995 goto out;
3997 mutex_lock(&dx_alloc_inode->i_mutex);
3999 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4000 if (ret) {
4001 mlog_errno(ret);
4002 goto out_mutex;
4005 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4006 if (IS_ERR(handle)) {
4007 ret = PTR_ERR(handle);
4008 mlog_errno(ret);
4009 goto out_unlock;
4012 ret = ocfs2_journal_access_di(handle, dir, di_bh,
4013 OCFS2_JOURNAL_ACCESS_WRITE);
4014 if (ret) {
4015 mlog_errno(ret);
4016 goto out_commit;
4019 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4020 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4021 di->i_dx_root = cpu_to_le64(0ULL);
4023 ocfs2_journal_dirty(handle, di_bh);
4025 blk = le64_to_cpu(dx_root->dr_blkno);
4026 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4027 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4028 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4029 bit, bg_blkno, 1);
4030 if (ret)
4031 mlog_errno(ret);
4033 out_commit:
4034 ocfs2_commit_trans(osb, handle);
4036 out_unlock:
4037 ocfs2_inode_unlock(dx_alloc_inode, 1);
4039 out_mutex:
4040 mutex_unlock(&dx_alloc_inode->i_mutex);
4041 brelse(dx_alloc_bh);
4042 out:
4043 iput(dx_alloc_inode);
4044 return ret;
4047 int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4049 int ret;
4050 unsigned int uninitialized_var(clen);
4051 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4052 u64 uninitialized_var(blkno);
4053 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4054 struct buffer_head *dx_root_bh = NULL;
4055 struct ocfs2_dx_root_block *dx_root;
4056 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4057 struct ocfs2_cached_dealloc_ctxt dealloc;
4058 struct ocfs2_extent_tree et;
4060 ocfs2_init_dealloc_ctxt(&dealloc);
4062 if (!ocfs2_dir_indexed(dir))
4063 return 0;
4065 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4066 if (ret) {
4067 mlog_errno(ret);
4068 goto out;
4070 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4072 if (ocfs2_dx_root_inline(dx_root))
4073 goto remove_index;
4075 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
4077 /* XXX: What if dr_clusters is too large? */
4078 while (le32_to_cpu(dx_root->dr_clusters)) {
4079 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4080 major_hash, &cpos, &blkno, &clen);
4081 if (ret) {
4082 mlog_errno(ret);
4083 goto out;
4086 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4088 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen,
4089 &dealloc);
4090 if (ret) {
4091 mlog_errno(ret);
4092 goto out;
4095 if (cpos == 0)
4096 break;
4098 major_hash = cpos - 1;
4101 remove_index:
4102 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4103 if (ret) {
4104 mlog_errno(ret);
4105 goto out;
4108 ocfs2_remove_from_cache(dir, dx_root_bh);
4109 out:
4110 ocfs2_schedule_truncate_log_flush(osb, 1);
4111 ocfs2_run_deallocs(osb, &dealloc);
4113 brelse(dx_root_bh);
4114 return ret;