GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / ocfs2 / dir.c
blobeaee5a9f466635c55140225986adbabb485dbf8e
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);
83 static int ocfs2_dir_indexed(struct inode *inode);
86 * These are distinct checks because future versions of the file system will
87 * want to have a trailing dirent structure independent of indexing.
89 static int ocfs2_supports_dir_trailer(struct inode *dir)
91 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
93 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
96 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
100 * "new' here refers to the point at which we're creating a new
101 * directory via "mkdir()", but also when we're expanding an inline
102 * directory. In either case, we don't yet have the indexing bit set
103 * on the directory, so the standard checks will fail in when metaecc
104 * is turned off. Only directory-initialization type functions should
105 * use this then. Everything else wants ocfs2_supports_dir_trailer()
107 static int ocfs2_new_dir_wants_trailer(struct inode *dir)
109 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
111 return ocfs2_meta_ecc(osb) ||
112 ocfs2_supports_indexed_dirs(osb);
115 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
117 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
120 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
122 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
123 void *data)
125 char *p = data;
127 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
128 return (struct ocfs2_dir_block_trailer *)p;
131 static int ocfs2_skip_dir_trailer(struct inode *dir,
132 struct ocfs2_dir_entry *de,
133 unsigned long offset,
134 unsigned long blklen)
136 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
138 if (!ocfs2_supports_dir_trailer(dir))
139 return 0;
141 if (offset != toff)
142 return 0;
144 return 1;
147 static void ocfs2_init_dir_trailer(struct inode *inode,
148 struct buffer_head *bh, u16 rec_len)
150 struct ocfs2_dir_block_trailer *trailer;
152 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
153 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
154 trailer->db_compat_rec_len =
155 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
156 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
157 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
158 trailer->db_free_rec_len = cpu_to_le16(rec_len);
161 * Link an unindexed block with a dir trailer structure into the index free
162 * list. This function will modify dirdata_bh, but assumes you've already
163 * passed it to the journal.
165 static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
166 struct buffer_head *dx_root_bh,
167 struct buffer_head *dirdata_bh)
169 int ret;
170 struct ocfs2_dx_root_block *dx_root;
171 struct ocfs2_dir_block_trailer *trailer;
173 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
174 OCFS2_JOURNAL_ACCESS_WRITE);
175 if (ret) {
176 mlog_errno(ret);
177 goto out;
179 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
180 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
182 trailer->db_free_next = dx_root->dr_free_blk;
183 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
185 ocfs2_journal_dirty(handle, dx_root_bh);
187 out:
188 return ret;
191 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
193 return res->dl_prev_leaf_bh == NULL;
196 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
198 brelse(res->dl_dx_root_bh);
199 brelse(res->dl_leaf_bh);
200 brelse(res->dl_dx_leaf_bh);
201 brelse(res->dl_prev_leaf_bh);
204 static int ocfs2_dir_indexed(struct inode *inode)
206 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
207 return 1;
208 return 0;
211 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
213 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
217 * Hashing code adapted from ext3
219 #define DELTA 0x9E3779B9
221 static void TEA_transform(__u32 buf[4], __u32 const in[])
223 __u32 sum = 0;
224 __u32 b0 = buf[0], b1 = buf[1];
225 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
226 int n = 16;
228 do {
229 sum += DELTA;
230 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
231 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
232 } while (--n);
234 buf[0] += b0;
235 buf[1] += b1;
238 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
240 __u32 pad, val;
241 int i;
243 pad = (__u32)len | ((__u32)len << 8);
244 pad |= pad << 16;
246 val = pad;
247 if (len > num*4)
248 len = num * 4;
249 for (i = 0; i < len; i++) {
250 if ((i % 4) == 0)
251 val = pad;
252 val = msg[i] + (val << 8);
253 if ((i % 4) == 3) {
254 *buf++ = val;
255 val = pad;
256 num--;
259 if (--num >= 0)
260 *buf++ = val;
261 while (--num >= 0)
262 *buf++ = pad;
265 static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
266 struct ocfs2_dx_hinfo *hinfo)
268 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
269 const char *p;
270 __u32 in[8], buf[4];
272 if ((len == 1 && !strncmp(".", name, 1)) ||
273 (len == 2 && !strncmp("..", name, 2))) {
274 buf[0] = buf[1] = 0;
275 goto out;
278 #ifdef OCFS2_DEBUG_DX_DIRS
280 * This makes it very easy to debug indexing problems. We
281 * should never allow this to be selected without hand editing
282 * this file though.
284 buf[0] = buf[1] = len;
285 goto out;
286 #endif
288 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
290 p = name;
291 while (len > 0) {
292 str2hashbuf(p, len, in, 4);
293 TEA_transform(buf, in);
294 len -= 16;
295 p += 16;
298 out:
299 hinfo->major_hash = buf[0];
300 hinfo->minor_hash = buf[1];
304 * bh passed here can be an inode block or a dir data block, depending
305 * on the inode inline data flag.
307 static int ocfs2_check_dir_entry(struct inode * dir,
308 struct ocfs2_dir_entry * de,
309 struct buffer_head * bh,
310 unsigned long offset)
312 const char *error_msg = NULL;
313 const int rlen = le16_to_cpu(de->rec_len);
315 if (rlen < OCFS2_DIR_REC_LEN(1))
316 error_msg = "rec_len is smaller than minimal";
317 else if (rlen % 4 != 0)
318 error_msg = "rec_len % 4 != 0";
319 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
320 error_msg = "rec_len is too small for name_len";
321 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
322 error_msg = "directory entry across blocks";
324 if (error_msg != NULL)
325 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
326 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
327 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
328 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
329 de->name_len);
330 return error_msg == NULL ? 1 : 0;
333 static inline int ocfs2_match(int len,
334 const char * const name,
335 struct ocfs2_dir_entry *de)
337 if (len != de->name_len)
338 return 0;
339 if (!de->inode)
340 return 0;
341 return !memcmp(name, de->name, len);
345 * Returns 0 if not found, -1 on failure, and 1 on success
347 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
348 struct inode *dir,
349 const char *name, int namelen,
350 unsigned long offset,
351 char *first_de,
352 unsigned int bytes,
353 struct ocfs2_dir_entry **res_dir)
355 struct ocfs2_dir_entry *de;
356 char *dlimit, *de_buf;
357 int de_len;
358 int ret = 0;
360 mlog_entry_void();
362 de_buf = first_de;
363 dlimit = de_buf + bytes;
365 while (de_buf < dlimit) {
366 /* this code is executed quadratically often */
367 /* do minimal checking `by hand' */
369 de = (struct ocfs2_dir_entry *) de_buf;
371 if (de_buf + namelen <= dlimit &&
372 ocfs2_match(namelen, name, de)) {
373 /* found a match - just to be sure, do a full check */
374 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
375 ret = -1;
376 goto bail;
378 *res_dir = de;
379 ret = 1;
380 goto bail;
383 /* prevent looping on a bad block */
384 de_len = le16_to_cpu(de->rec_len);
385 if (de_len <= 0) {
386 ret = -1;
387 goto bail;
390 de_buf += de_len;
391 offset += de_len;
394 bail:
395 mlog_exit(ret);
396 return ret;
399 static struct buffer_head *ocfs2_find_entry_id(const char *name,
400 int namelen,
401 struct inode *dir,
402 struct ocfs2_dir_entry **res_dir)
404 int ret, found;
405 struct buffer_head *di_bh = NULL;
406 struct ocfs2_dinode *di;
407 struct ocfs2_inline_data *data;
409 ret = ocfs2_read_inode_block(dir, &di_bh);
410 if (ret) {
411 mlog_errno(ret);
412 goto out;
415 di = (struct ocfs2_dinode *)di_bh->b_data;
416 data = &di->id2.i_data;
418 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
419 data->id_data, i_size_read(dir), res_dir);
420 if (found == 1)
421 return di_bh;
423 brelse(di_bh);
424 out:
425 return NULL;
428 static int ocfs2_validate_dir_block(struct super_block *sb,
429 struct buffer_head *bh)
431 int rc;
432 struct ocfs2_dir_block_trailer *trailer =
433 ocfs2_trailer_from_bh(bh, sb);
437 * We don't validate dirents here, that's handled
438 * in-place when the code walks them.
440 mlog(0, "Validating dirblock %llu\n",
441 (unsigned long long)bh->b_blocknr);
443 BUG_ON(!buffer_uptodate(bh));
446 * If the ecc fails, we return the error but otherwise
447 * leave the filesystem running. We know any error is
448 * local to this block.
450 * Note that we are safe to call this even if the directory
451 * doesn't have a trailer. Filesystems without metaecc will do
452 * nothing, and filesystems with it will have one.
454 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
455 if (rc)
456 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
457 (unsigned long long)bh->b_blocknr);
459 return rc;
463 * Validate a directory trailer.
465 * We check the trailer here rather than in ocfs2_validate_dir_block()
466 * because that function doesn't have the inode to test.
468 static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
470 int rc = 0;
471 struct ocfs2_dir_block_trailer *trailer;
473 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
474 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
475 rc = -EINVAL;
476 ocfs2_error(dir->i_sb,
477 "Invalid dirblock #%llu: "
478 "signature = %.*s\n",
479 (unsigned long long)bh->b_blocknr, 7,
480 trailer->db_signature);
481 goto out;
483 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
484 rc = -EINVAL;
485 ocfs2_error(dir->i_sb,
486 "Directory block #%llu has an invalid "
487 "db_blkno of %llu",
488 (unsigned long long)bh->b_blocknr,
489 (unsigned long long)le64_to_cpu(trailer->db_blkno));
490 goto out;
492 if (le64_to_cpu(trailer->db_parent_dinode) !=
493 OCFS2_I(dir)->ip_blkno) {
494 rc = -EINVAL;
495 ocfs2_error(dir->i_sb,
496 "Directory block #%llu on dinode "
497 "#%llu has an invalid parent_dinode "
498 "of %llu",
499 (unsigned long long)bh->b_blocknr,
500 (unsigned long long)OCFS2_I(dir)->ip_blkno,
501 (unsigned long long)le64_to_cpu(trailer->db_blkno));
502 goto out;
504 out:
505 return rc;
509 * This function forces all errors to -EIO for consistency with its
510 * predecessor, ocfs2_bread(). We haven't audited what returning the
511 * real error codes would do to callers. We log the real codes with
512 * mlog_errno() before we squash them.
514 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
515 struct buffer_head **bh, int flags)
517 int rc = 0;
518 struct buffer_head *tmp = *bh;
520 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
521 ocfs2_validate_dir_block);
522 if (rc) {
523 mlog_errno(rc);
524 goto out;
527 if (!(flags & OCFS2_BH_READAHEAD) &&
528 ocfs2_supports_dir_trailer(inode)) {
529 rc = ocfs2_check_dir_trailer(inode, tmp);
530 if (rc) {
531 if (!*bh)
532 brelse(tmp);
533 mlog_errno(rc);
534 goto out;
538 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
539 if (!*bh)
540 *bh = tmp;
542 out:
543 return rc ? -EIO : 0;
547 * Read the block at 'phys' which belongs to this directory
548 * inode. This function does no virtual->physical block translation -
549 * what's passed in is assumed to be a valid directory block.
551 static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
552 struct buffer_head **bh)
554 int ret;
555 struct buffer_head *tmp = *bh;
557 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
558 ocfs2_validate_dir_block);
559 if (ret) {
560 mlog_errno(ret);
561 goto out;
564 if (ocfs2_supports_dir_trailer(dir)) {
565 ret = ocfs2_check_dir_trailer(dir, tmp);
566 if (ret) {
567 if (!*bh)
568 brelse(tmp);
569 mlog_errno(ret);
570 goto out;
574 if (!ret && !*bh)
575 *bh = tmp;
576 out:
577 return ret;
580 static int ocfs2_validate_dx_root(struct super_block *sb,
581 struct buffer_head *bh)
583 int ret;
584 struct ocfs2_dx_root_block *dx_root;
586 BUG_ON(!buffer_uptodate(bh));
588 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
590 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
591 if (ret) {
592 mlog(ML_ERROR,
593 "Checksum failed for dir index root block %llu\n",
594 (unsigned long long)bh->b_blocknr);
595 return ret;
598 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
599 ocfs2_error(sb,
600 "Dir Index Root # %llu has bad signature %.*s",
601 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
602 7, dx_root->dr_signature);
603 return -EINVAL;
606 return 0;
609 static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
610 struct buffer_head **dx_root_bh)
612 int ret;
613 u64 blkno = le64_to_cpu(di->i_dx_root);
614 struct buffer_head *tmp = *dx_root_bh;
616 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
617 ocfs2_validate_dx_root);
619 /* If ocfs2_read_block() got us a new bh, pass it up. */
620 if (!ret && !*dx_root_bh)
621 *dx_root_bh = tmp;
623 return ret;
626 static int ocfs2_validate_dx_leaf(struct super_block *sb,
627 struct buffer_head *bh)
629 int ret;
630 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
632 BUG_ON(!buffer_uptodate(bh));
634 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
635 if (ret) {
636 mlog(ML_ERROR,
637 "Checksum failed for dir index leaf block %llu\n",
638 (unsigned long long)bh->b_blocknr);
639 return ret;
642 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
643 ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
644 7, dx_leaf->dl_signature);
645 return -EROFS;
648 return 0;
651 static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
652 struct buffer_head **dx_leaf_bh)
654 int ret;
655 struct buffer_head *tmp = *dx_leaf_bh;
657 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
658 ocfs2_validate_dx_leaf);
660 /* If ocfs2_read_block() got us a new bh, pass it up. */
661 if (!ret && !*dx_leaf_bh)
662 *dx_leaf_bh = tmp;
664 return ret;
668 * Read a series of dx_leaf blocks. This expects all buffer_head
669 * pointers to be NULL on function entry.
671 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
672 struct buffer_head **dx_leaf_bhs)
674 int ret;
676 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
677 ocfs2_validate_dx_leaf);
678 if (ret)
679 mlog_errno(ret);
681 return ret;
684 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
685 struct inode *dir,
686 struct ocfs2_dir_entry **res_dir)
688 struct super_block *sb;
689 struct buffer_head *bh_use[NAMEI_RA_SIZE];
690 struct buffer_head *bh, *ret = NULL;
691 unsigned long start, block, b;
692 int ra_max = 0; /* Number of bh's in the readahead
693 buffer, bh_use[] */
694 int ra_ptr = 0; /* Current index into readahead
695 buffer */
696 int num = 0;
697 int nblocks, i, err;
699 mlog_entry_void();
701 sb = dir->i_sb;
703 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
704 start = OCFS2_I(dir)->ip_dir_start_lookup;
705 if (start >= nblocks)
706 start = 0;
707 block = start;
709 restart:
710 do {
712 * We deal with the read-ahead logic here.
714 if (ra_ptr >= ra_max) {
715 /* Refill the readahead buffer */
716 ra_ptr = 0;
717 b = block;
718 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
720 * Terminate if we reach the end of the
721 * directory and must wrap, or if our
722 * search has finished at this block.
724 if (b >= nblocks || (num && block == start)) {
725 bh_use[ra_max] = NULL;
726 break;
728 num++;
730 bh = NULL;
731 err = ocfs2_read_dir_block(dir, b++, &bh,
732 OCFS2_BH_READAHEAD);
733 bh_use[ra_max] = bh;
736 if ((bh = bh_use[ra_ptr++]) == NULL)
737 goto next;
738 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
739 /* read error, skip block & hope for the best.
740 * ocfs2_read_dir_block() has released the bh. */
741 ocfs2_error(dir->i_sb, "reading directory %llu, "
742 "offset %lu\n",
743 (unsigned long long)OCFS2_I(dir)->ip_blkno,
744 block);
745 goto next;
747 i = ocfs2_search_dirblock(bh, dir, name, namelen,
748 block << sb->s_blocksize_bits,
749 bh->b_data, sb->s_blocksize,
750 res_dir);
751 if (i == 1) {
752 OCFS2_I(dir)->ip_dir_start_lookup = block;
753 ret = bh;
754 goto cleanup_and_exit;
755 } else {
756 brelse(bh);
757 if (i < 0)
758 goto cleanup_and_exit;
760 next:
761 if (++block >= nblocks)
762 block = 0;
763 } while (block != start);
766 * If the directory has grown while we were searching, then
767 * search the last part of the directory before giving up.
769 block = nblocks;
770 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
771 if (block < nblocks) {
772 start = 0;
773 goto restart;
776 cleanup_and_exit:
777 /* Clean up the read-ahead blocks */
778 for (; ra_ptr < ra_max; ra_ptr++)
779 brelse(bh_use[ra_ptr]);
781 mlog_exit_ptr(ret);
782 return ret;
785 static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
786 struct ocfs2_extent_list *el,
787 u32 major_hash,
788 u32 *ret_cpos,
789 u64 *ret_phys_blkno,
790 unsigned int *ret_clen)
792 int ret = 0, i, found;
793 struct buffer_head *eb_bh = NULL;
794 struct ocfs2_extent_block *eb;
795 struct ocfs2_extent_rec *rec = NULL;
797 if (el->l_tree_depth) {
798 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
799 &eb_bh);
800 if (ret) {
801 mlog_errno(ret);
802 goto out;
805 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
806 el = &eb->h_list;
808 if (el->l_tree_depth) {
809 ocfs2_error(inode->i_sb,
810 "Inode %lu has non zero tree depth in "
811 "btree tree block %llu\n", inode->i_ino,
812 (unsigned long long)eb_bh->b_blocknr);
813 ret = -EROFS;
814 goto out;
818 found = 0;
819 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
820 rec = &el->l_recs[i];
822 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
823 found = 1;
824 break;
828 if (!found) {
829 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
830 "record (%u, %u, 0) in btree", inode->i_ino,
831 le32_to_cpu(rec->e_cpos),
832 ocfs2_rec_clusters(el, rec));
833 ret = -EROFS;
834 goto out;
837 if (ret_phys_blkno)
838 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
839 if (ret_cpos)
840 *ret_cpos = le32_to_cpu(rec->e_cpos);
841 if (ret_clen)
842 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
844 out:
845 brelse(eb_bh);
846 return ret;
850 * Returns the block index, from the start of the cluster which this
851 * hash belongs too.
853 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
854 u32 minor_hash)
856 return minor_hash & osb->osb_dx_mask;
859 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
860 struct ocfs2_dx_hinfo *hinfo)
862 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
865 static int ocfs2_dx_dir_lookup(struct inode *inode,
866 struct ocfs2_extent_list *el,
867 struct ocfs2_dx_hinfo *hinfo,
868 u32 *ret_cpos,
869 u64 *ret_phys_blkno)
871 int ret = 0;
872 unsigned int cend, uninitialized_var(clen);
873 u32 uninitialized_var(cpos);
874 u64 uninitialized_var(blkno);
875 u32 name_hash = hinfo->major_hash;
877 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
878 &clen);
879 if (ret) {
880 mlog_errno(ret);
881 goto out;
884 cend = cpos + clen;
885 if (name_hash >= cend) {
886 /* We want the last cluster */
887 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
888 cpos += clen - 1;
889 } else {
890 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
891 name_hash - cpos);
892 cpos = name_hash;
896 * We now have the cluster which should hold our entry. To
897 * find the exact block from the start of the cluster to
898 * search, we take the lower bits of the hash.
900 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
902 if (ret_phys_blkno)
903 *ret_phys_blkno = blkno;
904 if (ret_cpos)
905 *ret_cpos = cpos;
907 out:
909 return ret;
912 static int ocfs2_dx_dir_search(const char *name, int namelen,
913 struct inode *dir,
914 struct ocfs2_dx_root_block *dx_root,
915 struct ocfs2_dir_lookup_result *res)
917 int ret, i, found;
918 u64 uninitialized_var(phys);
919 struct buffer_head *dx_leaf_bh = NULL;
920 struct ocfs2_dx_leaf *dx_leaf;
921 struct ocfs2_dx_entry *dx_entry = NULL;
922 struct buffer_head *dir_ent_bh = NULL;
923 struct ocfs2_dir_entry *dir_ent = NULL;
924 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
925 struct ocfs2_extent_list *dr_el;
926 struct ocfs2_dx_entry_list *entry_list;
928 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
930 if (ocfs2_dx_root_inline(dx_root)) {
931 entry_list = &dx_root->dr_entries;
932 goto search;
935 dr_el = &dx_root->dr_list;
937 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
938 if (ret) {
939 mlog_errno(ret);
940 goto out;
943 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
944 "returns: %llu\n",
945 (unsigned long long)OCFS2_I(dir)->ip_blkno,
946 namelen, name, hinfo->major_hash, hinfo->minor_hash,
947 (unsigned long long)phys);
949 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
950 if (ret) {
951 mlog_errno(ret);
952 goto out;
955 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
957 mlog(0, "leaf info: num_used: %d, count: %d\n",
958 le16_to_cpu(dx_leaf->dl_list.de_num_used),
959 le16_to_cpu(dx_leaf->dl_list.de_count));
961 entry_list = &dx_leaf->dl_list;
963 search:
965 * Empty leaf is legal, so no need to check for that.
967 found = 0;
968 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
969 dx_entry = &entry_list->de_entries[i];
971 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
972 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
973 continue;
976 * Search unindexed leaf block now. We're not
977 * guaranteed to find anything.
979 ret = ocfs2_read_dir_block_direct(dir,
980 le64_to_cpu(dx_entry->dx_dirent_blk),
981 &dir_ent_bh);
982 if (ret) {
983 mlog_errno(ret);
984 goto out;
988 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
989 0, dir_ent_bh->b_data,
990 dir->i_sb->s_blocksize, &dir_ent);
991 if (found == 1)
992 break;
994 if (found == -1) {
995 /* This means we found a bad directory entry. */
996 ret = -EIO;
997 mlog_errno(ret);
998 goto out;
1001 brelse(dir_ent_bh);
1002 dir_ent_bh = NULL;
1005 if (found <= 0) {
1006 ret = -ENOENT;
1007 goto out;
1010 res->dl_leaf_bh = dir_ent_bh;
1011 res->dl_entry = dir_ent;
1012 res->dl_dx_leaf_bh = dx_leaf_bh;
1013 res->dl_dx_entry = dx_entry;
1015 ret = 0;
1016 out:
1017 if (ret) {
1018 brelse(dx_leaf_bh);
1019 brelse(dir_ent_bh);
1021 return ret;
1024 static int ocfs2_find_entry_dx(const char *name, int namelen,
1025 struct inode *dir,
1026 struct ocfs2_dir_lookup_result *lookup)
1028 int ret;
1029 struct buffer_head *di_bh = NULL;
1030 struct ocfs2_dinode *di;
1031 struct buffer_head *dx_root_bh = NULL;
1032 struct ocfs2_dx_root_block *dx_root;
1034 ret = ocfs2_read_inode_block(dir, &di_bh);
1035 if (ret) {
1036 mlog_errno(ret);
1037 goto out;
1040 di = (struct ocfs2_dinode *)di_bh->b_data;
1042 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1043 if (ret) {
1044 mlog_errno(ret);
1045 goto out;
1047 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1049 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
1050 if (ret) {
1051 if (ret != -ENOENT)
1052 mlog_errno(ret);
1053 goto out;
1056 lookup->dl_dx_root_bh = dx_root_bh;
1057 dx_root_bh = NULL;
1058 out:
1059 brelse(di_bh);
1060 brelse(dx_root_bh);
1061 return ret;
1065 * Try to find an entry of the provided name within 'dir'.
1067 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1068 * returned and the struct 'res' will contain information useful to
1069 * other directory manipulation functions.
1071 * Caller can NOT assume anything about the contents of the
1072 * buffer_heads - they are passed back only so that it can be passed
1073 * into any one of the manipulation functions (add entry, delete
1074 * entry, etc). As an example, bh in the extent directory case is a
1075 * data block, in the inline-data case it actually points to an inode,
1076 * in the indexed directory case, multiple buffers are involved.
1078 int ocfs2_find_entry(const char *name, int namelen,
1079 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1081 struct buffer_head *bh;
1082 struct ocfs2_dir_entry *res_dir = NULL;
1084 if (ocfs2_dir_indexed(dir))
1085 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1088 * The unindexed dir code only uses part of the lookup
1089 * structure, so there's no reason to push it down further
1090 * than this.
1092 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1093 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1094 else
1095 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1097 if (bh == NULL)
1098 return -ENOENT;
1100 lookup->dl_leaf_bh = bh;
1101 lookup->dl_entry = res_dir;
1102 return 0;
1106 * Update inode number and type of a previously found directory entry.
1108 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1109 struct ocfs2_dir_lookup_result *res,
1110 struct inode *new_entry_inode)
1112 int ret;
1113 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1114 struct ocfs2_dir_entry *de = res->dl_entry;
1115 struct buffer_head *de_bh = res->dl_leaf_bh;
1118 * The same code works fine for both inline-data and extent
1119 * based directories, so no need to split this up. The only
1120 * difference is the journal_access function.
1123 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1124 access = ocfs2_journal_access_di;
1126 ret = access(handle, INODE_CACHE(dir), de_bh,
1127 OCFS2_JOURNAL_ACCESS_WRITE);
1128 if (ret) {
1129 mlog_errno(ret);
1130 goto out;
1133 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1134 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1136 ocfs2_journal_dirty(handle, de_bh);
1138 out:
1139 return ret;
1143 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1144 * previous entry
1146 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1147 struct ocfs2_dir_entry *de_del,
1148 struct buffer_head *bh, char *first_de,
1149 unsigned int bytes)
1151 struct ocfs2_dir_entry *de, *pde;
1152 int i, status = -ENOENT;
1153 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1155 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1157 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1158 access = ocfs2_journal_access_di;
1160 i = 0;
1161 pde = NULL;
1162 de = (struct ocfs2_dir_entry *) first_de;
1163 while (i < bytes) {
1164 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1165 status = -EIO;
1166 mlog_errno(status);
1167 goto bail;
1169 if (de == de_del) {
1170 status = access(handle, INODE_CACHE(dir), bh,
1171 OCFS2_JOURNAL_ACCESS_WRITE);
1172 if (status < 0) {
1173 status = -EIO;
1174 mlog_errno(status);
1175 goto bail;
1177 if (pde)
1178 le16_add_cpu(&pde->rec_len,
1179 le16_to_cpu(de->rec_len));
1180 else
1181 de->inode = 0;
1182 dir->i_version++;
1183 ocfs2_journal_dirty(handle, bh);
1184 goto bail;
1186 i += le16_to_cpu(de->rec_len);
1187 pde = de;
1188 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1190 bail:
1191 mlog_exit(status);
1192 return status;
1195 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1197 unsigned int hole;
1199 if (le64_to_cpu(de->inode) == 0)
1200 hole = le16_to_cpu(de->rec_len);
1201 else
1202 hole = le16_to_cpu(de->rec_len) -
1203 OCFS2_DIR_REC_LEN(de->name_len);
1205 return hole;
1208 static int ocfs2_find_max_rec_len(struct super_block *sb,
1209 struct buffer_head *dirblock_bh)
1211 int size, this_hole, largest_hole = 0;
1212 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1213 struct ocfs2_dir_entry *de;
1215 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1216 size = ocfs2_dir_trailer_blk_off(sb);
1217 limit = start + size;
1218 de_buf = start;
1219 de = (struct ocfs2_dir_entry *)de_buf;
1220 do {
1221 if (de_buf != trailer) {
1222 this_hole = ocfs2_figure_dirent_hole(de);
1223 if (this_hole > largest_hole)
1224 largest_hole = this_hole;
1227 de_buf += le16_to_cpu(de->rec_len);
1228 de = (struct ocfs2_dir_entry *)de_buf;
1229 } while (de_buf < limit);
1231 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1232 return largest_hole;
1233 return 0;
1236 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1237 int index)
1239 int num_used = le16_to_cpu(entry_list->de_num_used);
1241 if (num_used == 1 || index == (num_used - 1))
1242 goto clear;
1244 memmove(&entry_list->de_entries[index],
1245 &entry_list->de_entries[index + 1],
1246 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1247 clear:
1248 num_used--;
1249 memset(&entry_list->de_entries[num_used], 0,
1250 sizeof(struct ocfs2_dx_entry));
1251 entry_list->de_num_used = cpu_to_le16(num_used);
1254 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1255 struct ocfs2_dir_lookup_result *lookup)
1257 int ret, index, max_rec_len, add_to_free_list = 0;
1258 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1259 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1260 struct ocfs2_dx_leaf *dx_leaf;
1261 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
1262 struct ocfs2_dir_block_trailer *trailer;
1263 struct ocfs2_dx_root_block *dx_root;
1264 struct ocfs2_dx_entry_list *entry_list;
1267 * This function gets a bit messy because we might have to
1268 * modify the root block, regardless of whether the indexed
1269 * entries are stored inline.
1273 * *Only* set 'entry_list' here, based on where we're looking
1274 * for the indexed entries. Later, we might still want to
1275 * journal both blocks, based on free list state.
1277 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1278 if (ocfs2_dx_root_inline(dx_root)) {
1279 entry_list = &dx_root->dr_entries;
1280 } else {
1281 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1282 entry_list = &dx_leaf->dl_list;
1285 /* Neither of these are a disk corruption - that should have
1286 * been caught by lookup, before we got here. */
1287 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1288 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
1290 index = (char *)dx_entry - (char *)entry_list->de_entries;
1291 index /= sizeof(*dx_entry);
1293 if (index >= le16_to_cpu(entry_list->de_num_used)) {
1294 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1295 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1296 entry_list, dx_entry);
1297 return -EIO;
1301 * We know that removal of this dirent will leave enough room
1302 * for a new one, so add this block to the free list if it
1303 * isn't already there.
1305 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1306 if (trailer->db_free_rec_len == 0)
1307 add_to_free_list = 1;
1310 * Add the block holding our index into the journal before
1311 * removing the unindexed entry. If we get an error return
1312 * from __ocfs2_delete_entry(), then it hasn't removed the
1313 * entry yet. Likewise, successful return means we *must*
1314 * remove the indexed entry.
1316 * We're also careful to journal the root tree block here as
1317 * the entry count needs to be updated. Also, we might be
1318 * adding to the start of the free list.
1320 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1321 OCFS2_JOURNAL_ACCESS_WRITE);
1322 if (ret) {
1323 mlog_errno(ret);
1324 goto out;
1327 if (!ocfs2_dx_root_inline(dx_root)) {
1328 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
1329 lookup->dl_dx_leaf_bh,
1330 OCFS2_JOURNAL_ACCESS_WRITE);
1331 if (ret) {
1332 mlog_errno(ret);
1333 goto out;
1337 mlog(0, "Dir %llu: delete entry at index: %d\n",
1338 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1340 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1341 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1342 if (ret) {
1343 mlog_errno(ret);
1344 goto out;
1347 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1348 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1349 if (add_to_free_list) {
1350 trailer->db_free_next = dx_root->dr_free_blk;
1351 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1352 ocfs2_journal_dirty(handle, dx_root_bh);
1355 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1356 ocfs2_journal_dirty(handle, leaf_bh);
1358 le32_add_cpu(&dx_root->dr_num_entries, -1);
1359 ocfs2_journal_dirty(handle, dx_root_bh);
1361 ocfs2_dx_list_remove_entry(entry_list, index);
1363 if (!ocfs2_dx_root_inline(dx_root))
1364 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
1366 out:
1367 return ret;
1370 static inline int ocfs2_delete_entry_id(handle_t *handle,
1371 struct inode *dir,
1372 struct ocfs2_dir_entry *de_del,
1373 struct buffer_head *bh)
1375 int ret;
1376 struct buffer_head *di_bh = NULL;
1377 struct ocfs2_dinode *di;
1378 struct ocfs2_inline_data *data;
1380 ret = ocfs2_read_inode_block(dir, &di_bh);
1381 if (ret) {
1382 mlog_errno(ret);
1383 goto out;
1386 di = (struct ocfs2_dinode *)di_bh->b_data;
1387 data = &di->id2.i_data;
1389 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1390 i_size_read(dir));
1392 brelse(di_bh);
1393 out:
1394 return ret;
1397 static inline int ocfs2_delete_entry_el(handle_t *handle,
1398 struct inode *dir,
1399 struct ocfs2_dir_entry *de_del,
1400 struct buffer_head *bh)
1402 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1403 bh->b_size);
1407 * Delete a directory entry. Hide the details of directory
1408 * implementation from the caller.
1410 int ocfs2_delete_entry(handle_t *handle,
1411 struct inode *dir,
1412 struct ocfs2_dir_lookup_result *res)
1414 if (ocfs2_dir_indexed(dir))
1415 return ocfs2_delete_entry_dx(handle, dir, res);
1417 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1418 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1419 res->dl_leaf_bh);
1421 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1422 res->dl_leaf_bh);
1426 * Check whether 'de' has enough room to hold an entry of
1427 * 'new_rec_len' bytes.
1429 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1430 unsigned int new_rec_len)
1432 unsigned int de_really_used;
1434 /* Check whether this is an empty record with enough space */
1435 if (le64_to_cpu(de->inode) == 0 &&
1436 le16_to_cpu(de->rec_len) >= new_rec_len)
1437 return 1;
1440 * Record might have free space at the end which we can
1441 * use.
1443 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1444 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1445 return 1;
1447 return 0;
1450 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1451 struct ocfs2_dx_entry *dx_new_entry)
1453 int i;
1455 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1456 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1458 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1461 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1462 struct ocfs2_dx_hinfo *hinfo,
1463 u64 dirent_blk)
1465 int i;
1466 struct ocfs2_dx_entry *dx_entry;
1468 i = le16_to_cpu(entry_list->de_num_used);
1469 dx_entry = &entry_list->de_entries[i];
1471 memset(dx_entry, 0, sizeof(*dx_entry));
1472 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1473 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1474 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1476 le16_add_cpu(&entry_list->de_num_used, 1);
1479 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1480 struct ocfs2_dx_hinfo *hinfo,
1481 u64 dirent_blk,
1482 struct buffer_head *dx_leaf_bh)
1484 int ret;
1485 struct ocfs2_dx_leaf *dx_leaf;
1487 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
1488 OCFS2_JOURNAL_ACCESS_WRITE);
1489 if (ret) {
1490 mlog_errno(ret);
1491 goto out;
1494 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
1495 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1496 ocfs2_journal_dirty(handle, dx_leaf_bh);
1498 out:
1499 return ret;
1502 static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1503 struct ocfs2_dx_hinfo *hinfo,
1504 u64 dirent_blk,
1505 struct ocfs2_dx_root_block *dx_root)
1507 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1510 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1511 struct ocfs2_dir_lookup_result *lookup)
1513 int ret = 0;
1514 struct ocfs2_dx_root_block *dx_root;
1515 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1517 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1518 OCFS2_JOURNAL_ACCESS_WRITE);
1519 if (ret) {
1520 mlog_errno(ret);
1521 goto out;
1524 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1525 if (ocfs2_dx_root_inline(dx_root)) {
1526 ocfs2_dx_inline_root_insert(dir, handle,
1527 &lookup->dl_hinfo,
1528 lookup->dl_leaf_bh->b_blocknr,
1529 dx_root);
1530 } else {
1531 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1532 lookup->dl_leaf_bh->b_blocknr,
1533 lookup->dl_dx_leaf_bh);
1534 if (ret)
1535 goto out;
1538 le32_add_cpu(&dx_root->dr_num_entries, 1);
1539 ocfs2_journal_dirty(handle, dx_root_bh);
1541 out:
1542 return ret;
1545 static void ocfs2_remove_block_from_free_list(struct inode *dir,
1546 handle_t *handle,
1547 struct ocfs2_dir_lookup_result *lookup)
1549 struct ocfs2_dir_block_trailer *trailer, *prev;
1550 struct ocfs2_dx_root_block *dx_root;
1551 struct buffer_head *bh;
1553 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1555 if (ocfs2_free_list_at_root(lookup)) {
1556 bh = lookup->dl_dx_root_bh;
1557 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1558 dx_root->dr_free_blk = trailer->db_free_next;
1559 } else {
1560 bh = lookup->dl_prev_leaf_bh;
1561 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1562 prev->db_free_next = trailer->db_free_next;
1565 trailer->db_free_rec_len = cpu_to_le16(0);
1566 trailer->db_free_next = cpu_to_le64(0);
1568 ocfs2_journal_dirty(handle, bh);
1569 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1573 * This expects that a journal write has been reserved on
1574 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1576 static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1577 struct ocfs2_dir_lookup_result *lookup)
1579 int max_rec_len;
1580 struct ocfs2_dir_block_trailer *trailer;
1582 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1583 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1584 if (max_rec_len) {
1586 * There's still room in this block, so no need to remove it
1587 * from the free list. In this case, we just want to update
1588 * the rec len accounting.
1590 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1591 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1592 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1593 } else {
1594 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1598 /* we don't always have a dentry for what we want to add, so people
1599 * like orphan dir can call this instead.
1601 * The lookup context must have been filled from
1602 * ocfs2_prepare_dir_for_insert.
1604 int __ocfs2_add_entry(handle_t *handle,
1605 struct inode *dir,
1606 const char *name, int namelen,
1607 struct inode *inode, u64 blkno,
1608 struct buffer_head *parent_fe_bh,
1609 struct ocfs2_dir_lookup_result *lookup)
1611 unsigned long offset;
1612 unsigned short rec_len;
1613 struct ocfs2_dir_entry *de, *de1;
1614 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1615 struct super_block *sb = dir->i_sb;
1616 int retval, status;
1617 unsigned int size = sb->s_blocksize;
1618 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
1619 char *data_start = insert_bh->b_data;
1621 mlog_entry_void();
1623 if (!namelen)
1624 return -EINVAL;
1626 if (ocfs2_dir_indexed(dir)) {
1627 struct buffer_head *bh;
1629 if (ocfs2_free_list_at_root(lookup)) {
1630 bh = lookup->dl_dx_root_bh;
1631 retval = ocfs2_journal_access_dr(handle,
1632 INODE_CACHE(dir), bh,
1633 OCFS2_JOURNAL_ACCESS_WRITE);
1634 } else {
1635 bh = lookup->dl_prev_leaf_bh;
1636 retval = ocfs2_journal_access_db(handle,
1637 INODE_CACHE(dir), bh,
1638 OCFS2_JOURNAL_ACCESS_WRITE);
1640 if (retval) {
1641 mlog_errno(retval);
1642 return retval;
1644 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1645 data_start = di->id2.i_data.id_data;
1646 size = i_size_read(dir);
1648 BUG_ON(insert_bh != parent_fe_bh);
1651 rec_len = OCFS2_DIR_REC_LEN(namelen);
1652 offset = 0;
1653 de = (struct ocfs2_dir_entry *) data_start;
1654 while (1) {
1655 BUG_ON((char *)de >= (size + data_start));
1657 /* These checks should've already been passed by the
1658 * prepare function, but I guess we can leave them
1659 * here anyway. */
1660 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1661 retval = -ENOENT;
1662 goto bail;
1664 if (ocfs2_match(namelen, name, de)) {
1665 retval = -EEXIST;
1666 goto bail;
1669 /* We're guaranteed that we should have space, so we
1670 * can't possibly have hit the trailer...right? */
1671 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1672 "Hit dir trailer trying to insert %.*s "
1673 "(namelen %d) into directory %llu. "
1674 "offset is %lu, trailer offset is %d\n",
1675 namelen, name, namelen,
1676 (unsigned long long)parent_fe_bh->b_blocknr,
1677 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1679 if (ocfs2_dirent_would_fit(de, rec_len)) {
1680 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1681 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1682 if (retval < 0) {
1683 mlog_errno(retval);
1684 goto bail;
1687 if (insert_bh == parent_fe_bh)
1688 status = ocfs2_journal_access_di(handle,
1689 INODE_CACHE(dir),
1690 insert_bh,
1691 OCFS2_JOURNAL_ACCESS_WRITE);
1692 else {
1693 status = ocfs2_journal_access_db(handle,
1694 INODE_CACHE(dir),
1695 insert_bh,
1696 OCFS2_JOURNAL_ACCESS_WRITE);
1698 if (ocfs2_dir_indexed(dir)) {
1699 status = ocfs2_dx_dir_insert(dir,
1700 handle,
1701 lookup);
1702 if (status) {
1703 mlog_errno(status);
1704 goto bail;
1709 /* By now the buffer is marked for journaling */
1710 offset += le16_to_cpu(de->rec_len);
1711 if (le64_to_cpu(de->inode)) {
1712 de1 = (struct ocfs2_dir_entry *)((char *) de +
1713 OCFS2_DIR_REC_LEN(de->name_len));
1714 de1->rec_len =
1715 cpu_to_le16(le16_to_cpu(de->rec_len) -
1716 OCFS2_DIR_REC_LEN(de->name_len));
1717 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1718 de = de1;
1720 de->file_type = OCFS2_FT_UNKNOWN;
1721 if (blkno) {
1722 de->inode = cpu_to_le64(blkno);
1723 ocfs2_set_de_type(de, inode->i_mode);
1724 } else
1725 de->inode = 0;
1726 de->name_len = namelen;
1727 memcpy(de->name, name, namelen);
1729 if (ocfs2_dir_indexed(dir))
1730 ocfs2_recalc_free_list(dir, handle, lookup);
1732 dir->i_version++;
1733 ocfs2_journal_dirty(handle, insert_bh);
1734 retval = 0;
1735 goto bail;
1738 offset += le16_to_cpu(de->rec_len);
1739 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1742 /* when you think about it, the assert above should prevent us
1743 * from ever getting here. */
1744 retval = -ENOSPC;
1745 bail:
1747 mlog_exit(retval);
1748 return retval;
1751 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
1752 u64 *f_version,
1753 loff_t *f_pos, void *priv,
1754 filldir_t filldir, int *filldir_err)
1756 int ret, i, filldir_ret;
1757 unsigned long offset = *f_pos;
1758 struct buffer_head *di_bh = NULL;
1759 struct ocfs2_dinode *di;
1760 struct ocfs2_inline_data *data;
1761 struct ocfs2_dir_entry *de;
1763 ret = ocfs2_read_inode_block(inode, &di_bh);
1764 if (ret) {
1765 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1766 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1767 goto out;
1770 di = (struct ocfs2_dinode *)di_bh->b_data;
1771 data = &di->id2.i_data;
1773 while (*f_pos < i_size_read(inode)) {
1774 revalidate:
1775 /* If the dir block has changed since the last call to
1776 * readdir(2), then we might be pointing to an invalid
1777 * dirent right now. Scan from the start of the block
1778 * to make sure. */
1779 if (*f_version != inode->i_version) {
1780 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1781 de = (struct ocfs2_dir_entry *)
1782 (data->id_data + i);
1783 /* It's too expensive to do a full
1784 * dirent test each time round this
1785 * loop, but we do have to test at
1786 * least that it is non-zero. A
1787 * failure will be detected in the
1788 * dirent test below. */
1789 if (le16_to_cpu(de->rec_len) <
1790 OCFS2_DIR_REC_LEN(1))
1791 break;
1792 i += le16_to_cpu(de->rec_len);
1794 *f_pos = offset = i;
1795 *f_version = inode->i_version;
1798 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
1799 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
1800 /* On error, skip the f_pos to the end. */
1801 *f_pos = i_size_read(inode);
1802 goto out;
1804 offset += le16_to_cpu(de->rec_len);
1805 if (le64_to_cpu(de->inode)) {
1806 /* We might block in the next section
1807 * if the data destination is
1808 * currently swapped out. So, use a
1809 * version stamp to detect whether or
1810 * not the directory has been modified
1811 * during the copy operation.
1813 u64 version = *f_version;
1814 unsigned char d_type = DT_UNKNOWN;
1816 if (de->file_type < OCFS2_FT_MAX)
1817 d_type = ocfs2_filetype_table[de->file_type];
1819 filldir_ret = filldir(priv, de->name,
1820 de->name_len,
1821 *f_pos,
1822 le64_to_cpu(de->inode),
1823 d_type);
1824 if (filldir_ret) {
1825 if (filldir_err)
1826 *filldir_err = filldir_ret;
1827 break;
1829 if (version != *f_version)
1830 goto revalidate;
1832 *f_pos += le16_to_cpu(de->rec_len);
1835 out:
1836 brelse(di_bh);
1838 return 0;
1842 * NOTE: This function can be called against unindexed directories,
1843 * and indexed ones.
1845 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
1846 u64 *f_version,
1847 loff_t *f_pos, void *priv,
1848 filldir_t filldir, int *filldir_err)
1850 int error = 0;
1851 unsigned long offset, blk, last_ra_blk = 0;
1852 int i, stored;
1853 struct buffer_head * bh, * tmp;
1854 struct ocfs2_dir_entry * de;
1855 struct super_block * sb = inode->i_sb;
1856 unsigned int ra_sectors = 16;
1858 stored = 0;
1859 bh = NULL;
1861 offset = (*f_pos) & (sb->s_blocksize - 1);
1863 while (!error && !stored && *f_pos < i_size_read(inode)) {
1864 blk = (*f_pos) >> sb->s_blocksize_bits;
1865 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1866 /* Skip the corrupt dirblock and keep trying */
1867 *f_pos += sb->s_blocksize - offset;
1868 continue;
1871 /* The idea here is to begin with 8k read-ahead and to stay
1872 * 4k ahead of our current position.
1874 * TODO: Use the pagecache for this. We just need to
1875 * make sure it's cluster-safe... */
1876 if (!last_ra_blk
1877 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1878 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
1879 i > 0; i--) {
1880 tmp = NULL;
1881 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1882 OCFS2_BH_READAHEAD))
1883 brelse(tmp);
1885 last_ra_blk = blk;
1886 ra_sectors = 8;
1889 revalidate:
1890 /* If the dir block has changed since the last call to
1891 * readdir(2), then we might be pointing to an invalid
1892 * dirent right now. Scan from the start of the block
1893 * to make sure. */
1894 if (*f_version != inode->i_version) {
1895 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1896 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1897 /* It's too expensive to do a full
1898 * dirent test each time round this
1899 * loop, but we do have to test at
1900 * least that it is non-zero. A
1901 * failure will be detected in the
1902 * dirent test below. */
1903 if (le16_to_cpu(de->rec_len) <
1904 OCFS2_DIR_REC_LEN(1))
1905 break;
1906 i += le16_to_cpu(de->rec_len);
1908 offset = i;
1909 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
1910 | offset;
1911 *f_version = inode->i_version;
1914 while (!error && *f_pos < i_size_read(inode)
1915 && offset < sb->s_blocksize) {
1916 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1917 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1918 /* On error, skip the f_pos to the
1919 next block. */
1920 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
1921 brelse(bh);
1922 goto out;
1924 offset += le16_to_cpu(de->rec_len);
1925 if (le64_to_cpu(de->inode)) {
1926 /* We might block in the next section
1927 * if the data destination is
1928 * currently swapped out. So, use a
1929 * version stamp to detect whether or
1930 * not the directory has been modified
1931 * during the copy operation.
1933 unsigned long version = *f_version;
1934 unsigned char d_type = DT_UNKNOWN;
1936 if (de->file_type < OCFS2_FT_MAX)
1937 d_type = ocfs2_filetype_table[de->file_type];
1938 error = filldir(priv, de->name,
1939 de->name_len,
1940 *f_pos,
1941 le64_to_cpu(de->inode),
1942 d_type);
1943 if (error) {
1944 if (filldir_err)
1945 *filldir_err = error;
1946 break;
1948 if (version != *f_version)
1949 goto revalidate;
1950 stored ++;
1952 *f_pos += le16_to_cpu(de->rec_len);
1954 offset = 0;
1955 brelse(bh);
1956 bh = NULL;
1959 stored = 0;
1960 out:
1961 return stored;
1964 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1965 loff_t *f_pos, void *priv, filldir_t filldir,
1966 int *filldir_err)
1968 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1969 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
1970 filldir, filldir_err);
1972 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1973 filldir_err);
1977 * This is intended to be called from inside other kernel functions,
1978 * so we fake some arguments.
1980 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1981 filldir_t filldir)
1983 int ret = 0, filldir_err = 0;
1984 u64 version = inode->i_version;
1986 while (*f_pos < i_size_read(inode)) {
1987 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
1988 filldir, &filldir_err);
1989 if (ret || filldir_err)
1990 break;
1993 if (ret > 0)
1994 ret = -EIO;
1996 return 0;
2000 * ocfs2_readdir()
2003 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
2005 int error = 0;
2006 struct inode *inode = filp->f_path.dentry->d_inode;
2007 int lock_level = 0;
2009 mlog_entry("dirino=%llu\n",
2010 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2012 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
2013 if (lock_level && error >= 0) {
2014 /* We release EX lock which used to update atime
2015 * and get PR lock again to reduce contention
2016 * on commonly accessed directories. */
2017 ocfs2_inode_unlock(inode, 1);
2018 lock_level = 0;
2019 error = ocfs2_inode_lock(inode, NULL, 0);
2021 if (error < 0) {
2022 if (error != -ENOENT)
2023 mlog_errno(error);
2024 /* we haven't got any yet, so propagate the error. */
2025 goto bail_nolock;
2028 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
2029 dirent, filldir, NULL);
2031 ocfs2_inode_unlock(inode, lock_level);
2033 bail_nolock:
2034 mlog_exit(error);
2036 return error;
2040 * NOTE: this should always be called with parent dir i_mutex taken.
2042 int ocfs2_find_files_on_disk(const char *name,
2043 int namelen,
2044 u64 *blkno,
2045 struct inode *inode,
2046 struct ocfs2_dir_lookup_result *lookup)
2048 int status = -ENOENT;
2050 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
2051 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2053 status = ocfs2_find_entry(name, namelen, inode, lookup);
2054 if (status)
2055 goto leave;
2057 *blkno = le64_to_cpu(lookup->dl_entry->inode);
2059 status = 0;
2060 leave:
2062 return status;
2066 * Convenience function for callers which just want the block number
2067 * mapped to a name and don't require the full dirent info, etc.
2069 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2070 int namelen, u64 *blkno)
2072 int ret;
2073 struct ocfs2_dir_lookup_result lookup = { NULL, };
2075 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2076 ocfs2_free_dir_lookup_result(&lookup);
2078 return ret;
2081 /* Check for a name within a directory.
2083 * Return 0 if the name does not exist
2084 * Return -EEXIST if the directory contains the name
2086 * Callers should have i_mutex + a cluster lock on dir
2088 int ocfs2_check_dir_for_entry(struct inode *dir,
2089 const char *name,
2090 int namelen)
2092 int ret;
2093 struct ocfs2_dir_lookup_result lookup = { NULL, };
2095 mlog_entry("dir %llu, name '%.*s'\n",
2096 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
2098 ret = -EEXIST;
2099 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
2100 goto bail;
2102 ret = 0;
2103 bail:
2104 ocfs2_free_dir_lookup_result(&lookup);
2106 mlog_exit(ret);
2107 return ret;
2110 struct ocfs2_empty_dir_priv {
2111 unsigned seen_dot;
2112 unsigned seen_dot_dot;
2113 unsigned seen_other;
2114 unsigned dx_dir;
2116 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
2117 loff_t pos, u64 ino, unsigned type)
2119 struct ocfs2_empty_dir_priv *p = priv;
2122 * Check the positions of "." and ".." records to be sure
2123 * they're in the correct place.
2125 * Indexed directories don't need to proceed past the first
2126 * two entries, so we end the scan after seeing '..'. Despite
2127 * that, we allow the scan to proceed In the event that we
2128 * have a corrupted indexed directory (no dot or dot dot
2129 * entries). This allows us to double check for existing
2130 * entries which might not have been found in the index.
2132 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2133 p->seen_dot = 1;
2134 return 0;
2137 if (name_len == 2 && !strncmp("..", name, 2) &&
2138 pos == OCFS2_DIR_REC_LEN(1)) {
2139 p->seen_dot_dot = 1;
2141 if (p->dx_dir && p->seen_dot)
2142 return 1;
2144 return 0;
2147 p->seen_other = 1;
2148 return 1;
2151 static int ocfs2_empty_dir_dx(struct inode *inode,
2152 struct ocfs2_empty_dir_priv *priv)
2154 int ret;
2155 struct buffer_head *di_bh = NULL;
2156 struct buffer_head *dx_root_bh = NULL;
2157 struct ocfs2_dinode *di;
2158 struct ocfs2_dx_root_block *dx_root;
2160 priv->dx_dir = 1;
2162 ret = ocfs2_read_inode_block(inode, &di_bh);
2163 if (ret) {
2164 mlog_errno(ret);
2165 goto out;
2167 di = (struct ocfs2_dinode *)di_bh->b_data;
2169 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2170 if (ret) {
2171 mlog_errno(ret);
2172 goto out;
2174 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2176 if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2177 priv->seen_other = 1;
2179 out:
2180 brelse(di_bh);
2181 brelse(dx_root_bh);
2182 return ret;
2185 int ocfs2_empty_dir(struct inode *inode)
2187 int ret;
2188 loff_t start = 0;
2189 struct ocfs2_empty_dir_priv priv;
2191 memset(&priv, 0, sizeof(priv));
2193 if (ocfs2_dir_indexed(inode)) {
2194 ret = ocfs2_empty_dir_dx(inode, &priv);
2195 if (ret)
2196 mlog_errno(ret);
2198 * We still run ocfs2_dir_foreach to get the checks
2199 * for "." and "..".
2203 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
2204 if (ret)
2205 mlog_errno(ret);
2207 if (!priv.seen_dot || !priv.seen_dot_dot) {
2208 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
2209 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2210 return 1;
2213 return !priv.seen_other;
2217 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2218 * "..", which might be used during creation of a directory with a trailing
2219 * header. It is otherwise safe to ignore the return code.
2221 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2222 struct inode *parent,
2223 char *start,
2224 unsigned int size)
2226 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2228 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2229 de->name_len = 1;
2230 de->rec_len =
2231 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2232 strcpy(de->name, ".");
2233 ocfs2_set_de_type(de, S_IFDIR);
2235 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2236 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2237 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2238 de->name_len = 2;
2239 strcpy(de->name, "..");
2240 ocfs2_set_de_type(de, S_IFDIR);
2242 return de;
2246 * This works together with code in ocfs2_mknod_locked() which sets
2247 * the inline-data flag and initializes the inline-data section.
2249 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2250 handle_t *handle,
2251 struct inode *parent,
2252 struct inode *inode,
2253 struct buffer_head *di_bh)
2255 int ret;
2256 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2257 struct ocfs2_inline_data *data = &di->id2.i_data;
2258 unsigned int size = le16_to_cpu(data->id_count);
2260 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2261 OCFS2_JOURNAL_ACCESS_WRITE);
2262 if (ret) {
2263 mlog_errno(ret);
2264 goto out;
2267 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2268 ocfs2_journal_dirty(handle, di_bh);
2270 i_size_write(inode, size);
2271 inode->i_nlink = 2;
2272 inode->i_blocks = ocfs2_inode_sector_count(inode);
2274 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2275 if (ret < 0)
2276 mlog_errno(ret);
2278 out:
2279 return ret;
2282 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2283 handle_t *handle,
2284 struct inode *parent,
2285 struct inode *inode,
2286 struct buffer_head *fe_bh,
2287 struct ocfs2_alloc_context *data_ac,
2288 struct buffer_head **ret_new_bh)
2290 int status;
2291 unsigned int size = osb->sb->s_blocksize;
2292 struct buffer_head *new_bh = NULL;
2293 struct ocfs2_dir_entry *de;
2295 mlog_entry_void();
2297 if (ocfs2_new_dir_wants_trailer(inode))
2298 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2300 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2301 data_ac, NULL, &new_bh);
2302 if (status < 0) {
2303 mlog_errno(status);
2304 goto bail;
2307 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2309 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
2310 OCFS2_JOURNAL_ACCESS_CREATE);
2311 if (status < 0) {
2312 mlog_errno(status);
2313 goto bail;
2315 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2317 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2318 if (ocfs2_new_dir_wants_trailer(inode)) {
2319 int size = le16_to_cpu(de->rec_len);
2322 * Figure out the size of the hole left over after
2323 * insertion of '.' and '..'. The trailer wants this
2324 * information.
2326 size -= OCFS2_DIR_REC_LEN(2);
2327 size -= sizeof(struct ocfs2_dir_block_trailer);
2329 ocfs2_init_dir_trailer(inode, new_bh, size);
2332 ocfs2_journal_dirty(handle, new_bh);
2334 i_size_write(inode, inode->i_sb->s_blocksize);
2335 inode->i_nlink = 2;
2336 inode->i_blocks = ocfs2_inode_sector_count(inode);
2337 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2338 if (status < 0) {
2339 mlog_errno(status);
2340 goto bail;
2343 status = 0;
2344 if (ret_new_bh) {
2345 *ret_new_bh = new_bh;
2346 new_bh = NULL;
2348 bail:
2349 brelse(new_bh);
2351 mlog_exit(status);
2352 return status;
2355 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2356 handle_t *handle, struct inode *dir,
2357 struct buffer_head *di_bh,
2358 struct buffer_head *dirdata_bh,
2359 struct ocfs2_alloc_context *meta_ac,
2360 int dx_inline, u32 num_entries,
2361 struct buffer_head **ret_dx_root_bh)
2363 int ret;
2364 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2365 u16 dr_suballoc_bit;
2366 u64 suballoc_loc, dr_blkno;
2367 unsigned int num_bits;
2368 struct buffer_head *dx_root_bh = NULL;
2369 struct ocfs2_dx_root_block *dx_root;
2370 struct ocfs2_dir_block_trailer *trailer =
2371 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
2373 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2374 &dr_suballoc_bit, &num_bits, &dr_blkno);
2375 if (ret) {
2376 mlog_errno(ret);
2377 goto out;
2380 mlog(0, "Dir %llu, attach new index block: %llu\n",
2381 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2382 (unsigned long long)dr_blkno);
2384 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2385 if (dx_root_bh == NULL) {
2386 ret = -EIO;
2387 goto out;
2389 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
2391 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
2392 OCFS2_JOURNAL_ACCESS_CREATE);
2393 if (ret < 0) {
2394 mlog_errno(ret);
2395 goto out;
2398 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2399 memset(dx_root, 0, osb->sb->s_blocksize);
2400 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2401 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2402 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
2403 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2404 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2405 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2406 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
2407 dx_root->dr_num_entries = cpu_to_le32(num_entries);
2408 if (le16_to_cpu(trailer->db_free_rec_len))
2409 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2410 else
2411 dx_root->dr_free_blk = cpu_to_le64(0);
2413 if (dx_inline) {
2414 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2415 dx_root->dr_entries.de_count =
2416 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2417 } else {
2418 dx_root->dr_list.l_count =
2419 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2421 ocfs2_journal_dirty(handle, dx_root_bh);
2423 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
2424 OCFS2_JOURNAL_ACCESS_CREATE);
2425 if (ret) {
2426 mlog_errno(ret);
2427 goto out;
2430 di->i_dx_root = cpu_to_le64(dr_blkno);
2432 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2433 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2435 ocfs2_journal_dirty(handle, di_bh);
2437 *ret_dx_root_bh = dx_root_bh;
2438 dx_root_bh = NULL;
2440 out:
2441 brelse(dx_root_bh);
2442 return ret;
2445 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2446 handle_t *handle, struct inode *dir,
2447 struct buffer_head **dx_leaves,
2448 int num_dx_leaves, u64 start_blk)
2450 int ret, i;
2451 struct ocfs2_dx_leaf *dx_leaf;
2452 struct buffer_head *bh;
2454 for (i = 0; i < num_dx_leaves; i++) {
2455 bh = sb_getblk(osb->sb, start_blk + i);
2456 if (bh == NULL) {
2457 ret = -EIO;
2458 goto out;
2460 dx_leaves[i] = bh;
2462 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
2464 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
2465 OCFS2_JOURNAL_ACCESS_CREATE);
2466 if (ret < 0) {
2467 mlog_errno(ret);
2468 goto out;
2471 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2473 memset(dx_leaf, 0, osb->sb->s_blocksize);
2474 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2475 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2476 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2477 dx_leaf->dl_list.de_count =
2478 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2480 mlog(0,
2481 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2482 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2483 (unsigned long long)bh->b_blocknr,
2484 le16_to_cpu(dx_leaf->dl_list.de_count));
2486 ocfs2_journal_dirty(handle, bh);
2489 ret = 0;
2490 out:
2491 return ret;
2495 * Allocates and formats a new cluster for use in an indexed dir
2496 * leaf. This version will not do the extent insert, so that it can be
2497 * used by operations which need careful ordering.
2499 static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2500 u32 cpos, handle_t *handle,
2501 struct ocfs2_alloc_context *data_ac,
2502 struct buffer_head **dx_leaves,
2503 int num_dx_leaves, u64 *ret_phys_blkno)
2505 int ret;
2506 u32 phys, num;
2507 u64 phys_blkno;
2508 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2510 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
2511 if (ret) {
2512 mlog_errno(ret);
2513 goto out;
2517 * Format the new cluster first. That way, we're inserting
2518 * valid data.
2520 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2521 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2522 num_dx_leaves, phys_blkno);
2523 if (ret) {
2524 mlog_errno(ret);
2525 goto out;
2528 *ret_phys_blkno = phys_blkno;
2529 out:
2530 return ret;
2533 static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2534 struct ocfs2_extent_tree *et,
2535 u32 cpos, handle_t *handle,
2536 struct ocfs2_alloc_context *data_ac,
2537 struct ocfs2_alloc_context *meta_ac,
2538 struct buffer_head **dx_leaves,
2539 int num_dx_leaves)
2541 int ret;
2542 u64 phys_blkno;
2544 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2545 num_dx_leaves, &phys_blkno);
2546 if (ret) {
2547 mlog_errno(ret);
2548 goto out;
2551 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
2552 meta_ac);
2553 if (ret)
2554 mlog_errno(ret);
2555 out:
2556 return ret;
2559 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2560 int *ret_num_leaves)
2562 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2563 struct buffer_head **dx_leaves;
2565 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2566 GFP_NOFS);
2567 if (dx_leaves && ret_num_leaves)
2568 *ret_num_leaves = num_dx_leaves;
2570 return dx_leaves;
2573 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2574 handle_t *handle,
2575 struct inode *parent,
2576 struct inode *inode,
2577 struct buffer_head *di_bh,
2578 struct ocfs2_alloc_context *data_ac,
2579 struct ocfs2_alloc_context *meta_ac)
2581 int ret;
2582 struct buffer_head *leaf_bh = NULL;
2583 struct buffer_head *dx_root_bh = NULL;
2584 struct ocfs2_dx_hinfo hinfo;
2585 struct ocfs2_dx_root_block *dx_root;
2586 struct ocfs2_dx_entry_list *entry_list;
2589 * Our strategy is to create the directory as though it were
2590 * unindexed, then add the index block. This works with very
2591 * little complication since the state of a new directory is a
2592 * very well known quantity.
2594 * Essentially, we have two dirents ("." and ".."), in the 1st
2595 * block which need indexing. These are easily inserted into
2596 * the index block.
2599 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2600 data_ac, &leaf_bh);
2601 if (ret) {
2602 mlog_errno(ret);
2603 goto out;
2606 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
2607 meta_ac, 1, 2, &dx_root_bh);
2608 if (ret) {
2609 mlog_errno(ret);
2610 goto out;
2612 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2613 entry_list = &dx_root->dr_entries;
2615 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2616 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
2617 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2619 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
2620 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2622 out:
2623 brelse(dx_root_bh);
2624 brelse(leaf_bh);
2625 return ret;
2628 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2629 handle_t *handle,
2630 struct inode *parent,
2631 struct inode *inode,
2632 struct buffer_head *fe_bh,
2633 struct ocfs2_alloc_context *data_ac,
2634 struct ocfs2_alloc_context *meta_ac)
2637 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2639 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2640 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2642 if (ocfs2_supports_indexed_dirs(osb))
2643 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2644 data_ac, meta_ac);
2646 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2647 data_ac, NULL);
2650 static int ocfs2_dx_dir_index_block(struct inode *dir,
2651 handle_t *handle,
2652 struct buffer_head **dx_leaves,
2653 int num_dx_leaves,
2654 u32 *num_dx_entries,
2655 struct buffer_head *dirent_bh)
2657 int ret = 0, namelen, i;
2658 char *de_buf, *limit;
2659 struct ocfs2_dir_entry *de;
2660 struct buffer_head *dx_leaf_bh;
2661 struct ocfs2_dx_hinfo hinfo;
2662 u64 dirent_blk = dirent_bh->b_blocknr;
2664 de_buf = dirent_bh->b_data;
2665 limit = de_buf + dir->i_sb->s_blocksize;
2667 while (de_buf < limit) {
2668 de = (struct ocfs2_dir_entry *)de_buf;
2670 namelen = de->name_len;
2671 if (!namelen || !de->inode)
2672 goto inc;
2674 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2676 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2677 dx_leaf_bh = dx_leaves[i];
2679 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2680 dirent_blk, dx_leaf_bh);
2681 if (ret) {
2682 mlog_errno(ret);
2683 goto out;
2686 *num_dx_entries = *num_dx_entries + 1;
2688 inc:
2689 de_buf += le16_to_cpu(de->rec_len);
2692 out:
2693 return ret;
2696 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2697 struct buffer_head *dx_root_bh,
2698 struct buffer_head *dirent_bh)
2700 char *de_buf, *limit;
2701 struct ocfs2_dx_root_block *dx_root;
2702 struct ocfs2_dir_entry *de;
2703 struct ocfs2_dx_hinfo hinfo;
2704 u64 dirent_blk = dirent_bh->b_blocknr;
2706 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2708 de_buf = dirent_bh->b_data;
2709 limit = de_buf + dir->i_sb->s_blocksize;
2711 while (de_buf < limit) {
2712 de = (struct ocfs2_dir_entry *)de_buf;
2714 if (!de->name_len || !de->inode)
2715 goto inc;
2717 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2719 mlog(0,
2720 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2721 (unsigned long long)dir->i_ino, hinfo.major_hash,
2722 hinfo.minor_hash,
2723 le16_to_cpu(dx_root->dr_entries.de_num_used),
2724 de->name_len, de->name);
2726 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2727 dirent_blk);
2729 le32_add_cpu(&dx_root->dr_num_entries, 1);
2730 inc:
2731 de_buf += le16_to_cpu(de->rec_len);
2736 * Count the number of inline directory entries in di_bh and compare
2737 * them against the number of entries we can hold in an inline dx root
2738 * block.
2740 static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2741 struct buffer_head *di_bh)
2743 int dirent_count = 0;
2744 char *de_buf, *limit;
2745 struct ocfs2_dir_entry *de;
2746 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2748 de_buf = di->id2.i_data.id_data;
2749 limit = de_buf + i_size_read(dir);
2751 while (de_buf < limit) {
2752 de = (struct ocfs2_dir_entry *)de_buf;
2754 if (de->name_len && de->inode)
2755 dirent_count++;
2757 de_buf += le16_to_cpu(de->rec_len);
2760 /* We are careful to leave room for one extra record. */
2761 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2765 * Expand rec_len of the rightmost dirent in a directory block so that it
2766 * contains the end of our valid space for dirents. We do this during
2767 * expansion from an inline directory to one with extents. The first dir block
2768 * in that case is taken from the inline data portion of the inode block.
2770 * This will also return the largest amount of contiguous space for a dirent
2771 * in the block. That value is *not* necessarily the last dirent, even after
2772 * expansion. The directory indexing code wants this value for free space
2773 * accounting. We do this here since we're already walking the entire dir
2774 * block.
2776 * We add the dir trailer if this filesystem wants it.
2778 static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2779 struct inode *dir)
2781 struct super_block *sb = dir->i_sb;
2782 struct ocfs2_dir_entry *de;
2783 struct ocfs2_dir_entry *prev_de;
2784 char *de_buf, *limit;
2785 unsigned int new_size = sb->s_blocksize;
2786 unsigned int bytes, this_hole;
2787 unsigned int largest_hole = 0;
2789 if (ocfs2_new_dir_wants_trailer(dir))
2790 new_size = ocfs2_dir_trailer_blk_off(sb);
2792 bytes = new_size - old_size;
2794 limit = start + old_size;
2795 de_buf = start;
2796 de = (struct ocfs2_dir_entry *)de_buf;
2797 do {
2798 this_hole = ocfs2_figure_dirent_hole(de);
2799 if (this_hole > largest_hole)
2800 largest_hole = this_hole;
2802 prev_de = de;
2803 de_buf += le16_to_cpu(de->rec_len);
2804 de = (struct ocfs2_dir_entry *)de_buf;
2805 } while (de_buf < limit);
2807 le16_add_cpu(&prev_de->rec_len, bytes);
2809 /* We need to double check this after modification of the final
2810 * dirent. */
2811 this_hole = ocfs2_figure_dirent_hole(prev_de);
2812 if (this_hole > largest_hole)
2813 largest_hole = this_hole;
2815 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2816 return largest_hole;
2817 return 0;
2821 * We allocate enough clusters to fulfill "blocks_wanted", but set
2822 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2823 * rest automatically for us.
2825 * *first_block_bh is a pointer to the 1st data block allocated to the
2826 * directory.
2828 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2829 unsigned int blocks_wanted,
2830 struct ocfs2_dir_lookup_result *lookup,
2831 struct buffer_head **first_block_bh)
2833 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
2834 struct super_block *sb = dir->i_sb;
2835 int ret, i, num_dx_leaves = 0, dx_inline = 0,
2836 credits = ocfs2_inline_to_extents_credits(sb);
2837 u64 dx_insert_blkno, blkno,
2838 bytes = blocks_wanted << sb->s_blocksize_bits;
2839 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2840 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2841 struct ocfs2_alloc_context *data_ac;
2842 struct ocfs2_alloc_context *meta_ac = NULL;
2843 struct buffer_head *dirdata_bh = NULL;
2844 struct buffer_head *dx_root_bh = NULL;
2845 struct buffer_head **dx_leaves = NULL;
2846 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2847 handle_t *handle;
2848 struct ocfs2_extent_tree et;
2849 struct ocfs2_extent_tree dx_et;
2850 int did_quota = 0, bytes_allocated = 0;
2852 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
2854 alloc = ocfs2_clusters_for_bytes(sb, bytes);
2855 dx_alloc = 0;
2857 down_write(&oi->ip_alloc_sem);
2859 if (ocfs2_supports_indexed_dirs(osb)) {
2860 credits += ocfs2_add_dir_index_credits(sb);
2862 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2863 if (!dx_inline) {
2864 /* Add one more cluster for an index leaf */
2865 dx_alloc++;
2866 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2867 &num_dx_leaves);
2868 if (!dx_leaves) {
2869 ret = -ENOMEM;
2870 mlog_errno(ret);
2871 goto out;
2875 /* This gets us the dx_root */
2876 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2877 if (ret) {
2878 mlog_errno(ret);
2879 goto out;
2884 * We should never need more than 2 clusters for the unindexed
2885 * tree - maximum dirent size is far less than one block. In
2886 * fact, the only time we'd need more than one cluster is if
2887 * blocksize == clustersize and the dirent won't fit in the
2888 * extra space that the expansion to a single block gives. As
2889 * of today, that only happens on 4k/4k file systems.
2891 BUG_ON(alloc > 2);
2893 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
2894 if (ret) {
2895 mlog_errno(ret);
2896 goto out;
2900 * Prepare for worst case allocation scenario of two separate
2901 * extents in the unindexed tree.
2903 if (alloc == 2)
2904 credits += OCFS2_SUBALLOC_ALLOC;
2906 handle = ocfs2_start_trans(osb, credits);
2907 if (IS_ERR(handle)) {
2908 ret = PTR_ERR(handle);
2909 mlog_errno(ret);
2910 goto out;
2913 ret = dquot_alloc_space_nodirty(dir,
2914 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2915 if (ret)
2916 goto out_commit;
2917 did_quota = 1;
2919 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2921 * Allocate our index cluster first, to maximize the
2922 * possibility that unindexed leaves grow
2923 * contiguously.
2925 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2926 dx_leaves, num_dx_leaves,
2927 &dx_insert_blkno);
2928 if (ret) {
2929 mlog_errno(ret);
2930 goto out_commit;
2932 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2936 * Try to claim as many clusters as the bitmap can give though
2937 * if we only get one now, that's enough to continue. The rest
2938 * will be claimed after the conversion to extents.
2940 if (ocfs2_dir_resv_allowed(osb))
2941 data_ac->ac_resv = &oi->ip_la_data_resv;
2942 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
2943 if (ret) {
2944 mlog_errno(ret);
2945 goto out_commit;
2947 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2950 * Operations are carefully ordered so that we set up the new
2951 * data block first. The conversion from inline data to
2952 * extents follows.
2954 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2955 dirdata_bh = sb_getblk(sb, blkno);
2956 if (!dirdata_bh) {
2957 ret = -EIO;
2958 mlog_errno(ret);
2959 goto out_commit;
2962 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
2964 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
2965 OCFS2_JOURNAL_ACCESS_CREATE);
2966 if (ret) {
2967 mlog_errno(ret);
2968 goto out_commit;
2971 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2972 memset(dirdata_bh->b_data + i_size_read(dir), 0,
2973 sb->s_blocksize - i_size_read(dir));
2974 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
2975 if (ocfs2_new_dir_wants_trailer(dir)) {
2977 * Prepare the dir trailer up front. It will otherwise look
2978 * like a valid dirent. Even if inserting the index fails
2979 * (unlikely), then all we'll have done is given first dir
2980 * block a small amount of fragmentation.
2982 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
2985 ocfs2_journal_dirty(handle, dirdata_bh);
2987 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2989 * Dx dirs with an external cluster need to do this up
2990 * front. Inline dx root's get handled later, after
2991 * we've allocated our root block. We get passed back
2992 * a total number of items so that dr_num_entries can
2993 * be correctly set once the dx_root has been
2994 * allocated.
2996 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
2997 num_dx_leaves, &num_dx_entries,
2998 dirdata_bh);
2999 if (ret) {
3000 mlog_errno(ret);
3001 goto out_commit;
3006 * Set extent, i_size, etc on the directory. After this, the
3007 * inode should contain the same exact dirents as before and
3008 * be fully accessible from system calls.
3010 * We let the later dirent insert modify c/mtime - to the user
3011 * the data hasn't changed.
3013 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
3014 OCFS2_JOURNAL_ACCESS_CREATE);
3015 if (ret) {
3016 mlog_errno(ret);
3017 goto out_commit;
3020 spin_lock(&oi->ip_lock);
3021 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
3022 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
3023 spin_unlock(&oi->ip_lock);
3025 ocfs2_dinode_new_extent_list(dir, di);
3027 i_size_write(dir, sb->s_blocksize);
3028 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3030 di->i_size = cpu_to_le64(sb->s_blocksize);
3031 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
3032 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
3035 * This should never fail as our extent list is empty and all
3036 * related blocks have been journaled already.
3038 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
3039 0, NULL);
3040 if (ret) {
3041 mlog_errno(ret);
3042 goto out_commit;
3046 * Set i_blocks after the extent insert for the most up to
3047 * date ip_clusters value.
3049 dir->i_blocks = ocfs2_inode_sector_count(dir);
3051 ocfs2_journal_dirty(handle, di_bh);
3053 if (ocfs2_supports_indexed_dirs(osb)) {
3054 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
3055 dirdata_bh, meta_ac, dx_inline,
3056 num_dx_entries, &dx_root_bh);
3057 if (ret) {
3058 mlog_errno(ret);
3059 goto out_commit;
3062 if (dx_inline) {
3063 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3064 dirdata_bh);
3065 } else {
3066 ocfs2_init_dx_root_extent_tree(&dx_et,
3067 INODE_CACHE(dir),
3068 dx_root_bh);
3069 ret = ocfs2_insert_extent(handle, &dx_et, 0,
3070 dx_insert_blkno, 1, 0, NULL);
3071 if (ret)
3072 mlog_errno(ret);
3077 * We asked for two clusters, but only got one in the 1st
3078 * pass. Claim the 2nd cluster as a separate extent.
3080 if (alloc > len) {
3081 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
3082 &len);
3083 if (ret) {
3084 mlog_errno(ret);
3085 goto out_commit;
3087 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3089 ret = ocfs2_insert_extent(handle, &et, 1,
3090 blkno, len, 0, NULL);
3091 if (ret) {
3092 mlog_errno(ret);
3093 goto out_commit;
3095 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
3098 *first_block_bh = dirdata_bh;
3099 dirdata_bh = NULL;
3100 if (ocfs2_supports_indexed_dirs(osb)) {
3101 unsigned int off;
3103 if (!dx_inline) {
3105 * We need to return the correct block within the
3106 * cluster which should hold our entry.
3108 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
3109 &lookup->dl_hinfo);
3110 get_bh(dx_leaves[off]);
3111 lookup->dl_dx_leaf_bh = dx_leaves[off];
3113 lookup->dl_dx_root_bh = dx_root_bh;
3114 dx_root_bh = NULL;
3117 out_commit:
3118 if (ret < 0 && did_quota)
3119 dquot_free_space_nodirty(dir, bytes_allocated);
3121 ocfs2_commit_trans(osb, handle);
3123 out:
3124 up_write(&oi->ip_alloc_sem);
3125 if (data_ac)
3126 ocfs2_free_alloc_context(data_ac);
3127 if (meta_ac)
3128 ocfs2_free_alloc_context(meta_ac);
3130 if (dx_leaves) {
3131 for (i = 0; i < num_dx_leaves; i++)
3132 brelse(dx_leaves[i]);
3133 kfree(dx_leaves);
3136 brelse(dirdata_bh);
3137 brelse(dx_root_bh);
3139 return ret;
3142 /* returns a bh of the 1st new block in the allocation. */
3143 static int ocfs2_do_extend_dir(struct super_block *sb,
3144 handle_t *handle,
3145 struct inode *dir,
3146 struct buffer_head *parent_fe_bh,
3147 struct ocfs2_alloc_context *data_ac,
3148 struct ocfs2_alloc_context *meta_ac,
3149 struct buffer_head **new_bh)
3151 int status;
3152 int extend, did_quota = 0;
3153 u64 p_blkno, v_blkno;
3155 spin_lock(&OCFS2_I(dir)->ip_lock);
3156 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3157 spin_unlock(&OCFS2_I(dir)->ip_lock);
3159 if (extend) {
3160 u32 offset = OCFS2_I(dir)->ip_clusters;
3162 status = dquot_alloc_space_nodirty(dir,
3163 ocfs2_clusters_to_bytes(sb, 1));
3164 if (status)
3165 goto bail;
3166 did_quota = 1;
3168 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3169 1, 0, parent_fe_bh, handle,
3170 data_ac, meta_ac, NULL);
3171 BUG_ON(status == -EAGAIN);
3172 if (status < 0) {
3173 mlog_errno(status);
3174 goto bail;
3178 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3179 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
3180 if (status < 0) {
3181 mlog_errno(status);
3182 goto bail;
3185 *new_bh = sb_getblk(sb, p_blkno);
3186 if (!*new_bh) {
3187 status = -EIO;
3188 mlog_errno(status);
3189 goto bail;
3191 status = 0;
3192 bail:
3193 if (did_quota && status < 0)
3194 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
3195 mlog_exit(status);
3196 return status;
3200 * Assumes you already have a cluster lock on the directory.
3202 * 'blocks_wanted' is only used if we have an inline directory which
3203 * is to be turned into an extent based one. The size of the dirent to
3204 * insert might be larger than the space gained by growing to just one
3205 * block, so we may have to grow the inode by two blocks in that case.
3207 * If the directory is already indexed, dx_root_bh must be provided.
3209 static int ocfs2_extend_dir(struct ocfs2_super *osb,
3210 struct inode *dir,
3211 struct buffer_head *parent_fe_bh,
3212 unsigned int blocks_wanted,
3213 struct ocfs2_dir_lookup_result *lookup,
3214 struct buffer_head **new_de_bh)
3216 int status = 0;
3217 int credits, num_free_extents, drop_alloc_sem = 0;
3218 loff_t dir_i_size;
3219 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
3220 struct ocfs2_extent_list *el = &fe->id2.i_list;
3221 struct ocfs2_alloc_context *data_ac = NULL;
3222 struct ocfs2_alloc_context *meta_ac = NULL;
3223 handle_t *handle = NULL;
3224 struct buffer_head *new_bh = NULL;
3225 struct ocfs2_dir_entry * de;
3226 struct super_block *sb = osb->sb;
3227 struct ocfs2_extent_tree et;
3228 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
3230 mlog_entry_void();
3232 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
3234 * This would be a code error as an inline directory should
3235 * never have an index root.
3237 BUG_ON(dx_root_bh);
3239 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
3240 blocks_wanted, lookup,
3241 &new_bh);
3242 if (status) {
3243 mlog_errno(status);
3244 goto bail;
3247 /* Expansion from inline to an indexed directory will
3248 * have given us this. */
3249 dx_root_bh = lookup->dl_dx_root_bh;
3251 if (blocks_wanted == 1) {
3253 * If the new dirent will fit inside the space
3254 * created by pushing out to one block, then
3255 * we can complete the operation
3256 * here. Otherwise we have to expand i_size
3257 * and format the 2nd block below.
3259 BUG_ON(new_bh == NULL);
3260 goto bail_bh;
3264 * Get rid of 'new_bh' - we want to format the 2nd
3265 * data block and return that instead.
3267 brelse(new_bh);
3268 new_bh = NULL;
3270 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3271 drop_alloc_sem = 1;
3272 dir_i_size = i_size_read(dir);
3273 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3274 goto do_extend;
3277 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3278 drop_alloc_sem = 1;
3279 dir_i_size = i_size_read(dir);
3280 mlog(0, "extending dir %llu (i_size = %lld)\n",
3281 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
3283 /* dir->i_size is always block aligned. */
3284 spin_lock(&OCFS2_I(dir)->ip_lock);
3285 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3286 spin_unlock(&OCFS2_I(dir)->ip_lock);
3287 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3288 parent_fe_bh);
3289 num_free_extents = ocfs2_num_free_extents(osb, &et);
3290 if (num_free_extents < 0) {
3291 status = num_free_extents;
3292 mlog_errno(status);
3293 goto bail;
3296 if (!num_free_extents) {
3297 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
3298 if (status < 0) {
3299 if (status != -ENOSPC)
3300 mlog_errno(status);
3301 goto bail;
3305 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
3306 if (status < 0) {
3307 if (status != -ENOSPC)
3308 mlog_errno(status);
3309 goto bail;
3312 if (ocfs2_dir_resv_allowed(osb))
3313 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
3315 credits = ocfs2_calc_extend_credits(sb, el, 1);
3316 } else {
3317 spin_unlock(&OCFS2_I(dir)->ip_lock);
3318 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3321 do_extend:
3322 if (ocfs2_dir_indexed(dir))
3323 credits++; /* For attaching the new dirent block to the
3324 * dx_root */
3326 handle = ocfs2_start_trans(osb, credits);
3327 if (IS_ERR(handle)) {
3328 status = PTR_ERR(handle);
3329 handle = NULL;
3330 mlog_errno(status);
3331 goto bail;
3334 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3335 data_ac, meta_ac, &new_bh);
3336 if (status < 0) {
3337 mlog_errno(status);
3338 goto bail;
3341 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
3343 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
3344 OCFS2_JOURNAL_ACCESS_CREATE);
3345 if (status < 0) {
3346 mlog_errno(status);
3347 goto bail;
3349 memset(new_bh->b_data, 0, sb->s_blocksize);
3351 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3352 de->inode = 0;
3353 if (ocfs2_supports_dir_trailer(dir)) {
3354 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
3356 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3358 if (ocfs2_dir_indexed(dir)) {
3359 status = ocfs2_dx_dir_link_trailer(dir, handle,
3360 dx_root_bh, new_bh);
3361 if (status) {
3362 mlog_errno(status);
3363 goto bail;
3366 } else {
3367 de->rec_len = cpu_to_le16(sb->s_blocksize);
3369 ocfs2_journal_dirty(handle, new_bh);
3371 dir_i_size += dir->i_sb->s_blocksize;
3372 i_size_write(dir, dir_i_size);
3373 dir->i_blocks = ocfs2_inode_sector_count(dir);
3374 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3375 if (status < 0) {
3376 mlog_errno(status);
3377 goto bail;
3380 bail_bh:
3381 *new_de_bh = new_bh;
3382 get_bh(*new_de_bh);
3383 bail:
3384 if (handle)
3385 ocfs2_commit_trans(osb, handle);
3386 if (drop_alloc_sem)
3387 up_write(&OCFS2_I(dir)->ip_alloc_sem);
3389 if (data_ac)
3390 ocfs2_free_alloc_context(data_ac);
3391 if (meta_ac)
3392 ocfs2_free_alloc_context(meta_ac);
3394 brelse(new_bh);
3396 mlog_exit(status);
3397 return status;
3400 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3401 const char *name, int namelen,
3402 struct buffer_head **ret_de_bh,
3403 unsigned int *blocks_wanted)
3405 int ret;
3406 struct super_block *sb = dir->i_sb;
3407 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3408 struct ocfs2_dir_entry *de, *last_de = NULL;
3409 char *de_buf, *limit;
3410 unsigned long offset = 0;
3411 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3414 * This calculates how many free bytes we'd have in block zero, should
3415 * this function force expansion to an extent tree.
3417 if (ocfs2_new_dir_wants_trailer(dir))
3418 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3419 else
3420 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
3422 de_buf = di->id2.i_data.id_data;
3423 limit = de_buf + i_size_read(dir);
3424 rec_len = OCFS2_DIR_REC_LEN(namelen);
3426 while (de_buf < limit) {
3427 de = (struct ocfs2_dir_entry *)de_buf;
3429 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3430 ret = -ENOENT;
3431 goto out;
3433 if (ocfs2_match(namelen, name, de)) {
3434 ret = -EEXIST;
3435 goto out;
3438 * No need to check for a trailing dirent record here as
3439 * they're not used for inline dirs.
3442 if (ocfs2_dirent_would_fit(de, rec_len)) {
3443 /* Ok, we found a spot. Return this bh and let
3444 * the caller actually fill it in. */
3445 *ret_de_bh = di_bh;
3446 get_bh(*ret_de_bh);
3447 ret = 0;
3448 goto out;
3451 last_de = de;
3452 de_buf += le16_to_cpu(de->rec_len);
3453 offset += le16_to_cpu(de->rec_len);
3457 * We're going to require expansion of the directory - figure
3458 * out how many blocks we'll need so that a place for the
3459 * dirent can be found.
3461 *blocks_wanted = 1;
3462 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
3463 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3464 *blocks_wanted = 2;
3466 ret = -ENOSPC;
3467 out:
3468 return ret;
3471 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3472 int namelen, struct buffer_head **ret_de_bh)
3474 unsigned long offset;
3475 struct buffer_head *bh = NULL;
3476 unsigned short rec_len;
3477 struct ocfs2_dir_entry *de;
3478 struct super_block *sb = dir->i_sb;
3479 int status;
3480 int blocksize = dir->i_sb->s_blocksize;
3482 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3483 if (status) {
3484 mlog_errno(status);
3485 goto bail;
3488 rec_len = OCFS2_DIR_REC_LEN(namelen);
3489 offset = 0;
3490 de = (struct ocfs2_dir_entry *) bh->b_data;
3491 while (1) {
3492 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3493 brelse(bh);
3494 bh = NULL;
3496 if (i_size_read(dir) <= offset) {
3498 * Caller will have to expand this
3499 * directory.
3501 status = -ENOSPC;
3502 goto bail;
3504 status = ocfs2_read_dir_block(dir,
3505 offset >> sb->s_blocksize_bits,
3506 &bh, 0);
3507 if (status) {
3508 mlog_errno(status);
3509 goto bail;
3511 /* move to next block */
3512 de = (struct ocfs2_dir_entry *) bh->b_data;
3514 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3515 status = -ENOENT;
3516 goto bail;
3518 if (ocfs2_match(namelen, name, de)) {
3519 status = -EEXIST;
3520 goto bail;
3523 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3524 blocksize))
3525 goto next;
3527 if (ocfs2_dirent_would_fit(de, rec_len)) {
3528 /* Ok, we found a spot. Return this bh and let
3529 * the caller actually fill it in. */
3530 *ret_de_bh = bh;
3531 get_bh(*ret_de_bh);
3532 status = 0;
3533 goto bail;
3535 next:
3536 offset += le16_to_cpu(de->rec_len);
3537 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3540 status = 0;
3541 bail:
3542 brelse(bh);
3544 mlog_exit(status);
3545 return status;
3548 static int dx_leaf_sort_cmp(const void *a, const void *b)
3550 const struct ocfs2_dx_entry *entry1 = a;
3551 const struct ocfs2_dx_entry *entry2 = b;
3552 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3553 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3554 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3555 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3557 if (major_hash1 > major_hash2)
3558 return 1;
3559 if (major_hash1 < major_hash2)
3560 return -1;
3563 * It is not strictly necessary to sort by minor
3565 if (minor_hash1 > minor_hash2)
3566 return 1;
3567 if (minor_hash1 < minor_hash2)
3568 return -1;
3569 return 0;
3572 static void dx_leaf_sort_swap(void *a, void *b, int size)
3574 struct ocfs2_dx_entry *entry1 = a;
3575 struct ocfs2_dx_entry *entry2 = b;
3576 struct ocfs2_dx_entry tmp;
3578 BUG_ON(size != sizeof(*entry1));
3580 tmp = *entry1;
3581 *entry1 = *entry2;
3582 *entry2 = tmp;
3585 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3587 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3588 int i, num = le16_to_cpu(dl_list->de_num_used);
3590 for (i = 0; i < (num - 1); i++) {
3591 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3592 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3593 return 0;
3596 return 1;
3600 * Find the optimal value to split this leaf on. This expects the leaf
3601 * entries to be in sorted order.
3603 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3604 * the hash we want to insert.
3606 * This function is only concerned with the major hash - that which
3607 * determines which cluster an item belongs to.
3609 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3610 u32 leaf_cpos, u32 insert_hash,
3611 u32 *split_hash)
3613 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3614 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3615 int allsame;
3618 * There's a couple rare, but nasty corner cases we have to
3619 * check for here. All of them involve a leaf where all value
3620 * have the same hash, which is what we look for first.
3622 * Most of the time, all of the above is false, and we simply
3623 * pick the median value for a split.
3625 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3626 if (allsame) {
3627 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3629 if (val == insert_hash) {
3631 * No matter where we would choose to split,
3632 * the new entry would want to occupy the same
3633 * block as these. Since there's no space left
3634 * in their existing block, we know there
3635 * won't be space after the split.
3637 return -ENOSPC;
3640 if (val == leaf_cpos) {
3642 * Because val is the same as leaf_cpos (which
3643 * is the smallest value this leaf can have),
3644 * yet is not equal to insert_hash, then we
3645 * know that insert_hash *must* be larger than
3646 * val (and leaf_cpos). At least cpos+1 in value.
3648 * We also know then, that there cannot be an
3649 * adjacent extent (otherwise we'd be looking
3650 * at it). Choosing this value gives us a
3651 * chance to get some contiguousness.
3653 *split_hash = leaf_cpos + 1;
3654 return 0;
3657 if (val > insert_hash) {
3659 * val can not be the same as insert hash, and
3660 * also must be larger than leaf_cpos. Also,
3661 * we know that there can't be a leaf between
3662 * cpos and val, otherwise the entries with
3663 * hash 'val' would be there.
3665 *split_hash = val;
3666 return 0;
3669 *split_hash = insert_hash;
3670 return 0;
3674 * Since the records are sorted and the checks above
3675 * guaranteed that not all records in this block are the same,
3676 * we simple travel forward, from the median, and pick the 1st
3677 * record whose value is larger than leaf_cpos.
3679 for (i = (num_used / 2); i < num_used; i++)
3680 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3681 leaf_cpos)
3682 break;
3684 BUG_ON(i == num_used); /* Should be impossible */
3685 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3686 return 0;
3690 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3691 * larger than split_hash into new_dx_leaves. We use a temporary
3692 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3694 * Since the block offset inside a leaf (cluster) is a constant mask
3695 * of minor_hash, we can optimize - an item at block offset X within
3696 * the original cluster, will be at offset X within the new cluster.
3698 static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3699 handle_t *handle,
3700 struct ocfs2_dx_leaf *tmp_dx_leaf,
3701 struct buffer_head **orig_dx_leaves,
3702 struct buffer_head **new_dx_leaves,
3703 int num_dx_leaves)
3705 int i, j, num_used;
3706 u32 major_hash;
3707 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3708 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3709 struct ocfs2_dx_entry *dx_entry;
3711 tmp_list = &tmp_dx_leaf->dl_list;
3713 for (i = 0; i < num_dx_leaves; i++) {
3714 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3715 orig_list = &orig_dx_leaf->dl_list;
3716 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3717 new_list = &new_dx_leaf->dl_list;
3719 num_used = le16_to_cpu(orig_list->de_num_used);
3721 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3722 tmp_list->de_num_used = cpu_to_le16(0);
3723 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3725 for (j = 0; j < num_used; j++) {
3726 dx_entry = &orig_list->de_entries[j];
3727 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3728 if (major_hash >= split_hash)
3729 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3730 dx_entry);
3731 else
3732 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3733 dx_entry);
3735 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3737 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3738 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3742 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3743 struct ocfs2_dx_root_block *dx_root)
3745 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3747 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
3748 credits += ocfs2_quota_trans_credits(osb->sb);
3749 return credits;
3753 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3754 * half our entries into.
3756 static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3757 struct buffer_head *dx_root_bh,
3758 struct buffer_head *dx_leaf_bh,
3759 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3760 u64 leaf_blkno)
3762 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3763 int credits, ret, i, num_used, did_quota = 0;
3764 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3765 u64 orig_leaves_start;
3766 int num_dx_leaves;
3767 struct buffer_head **orig_dx_leaves = NULL;
3768 struct buffer_head **new_dx_leaves = NULL;
3769 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3770 struct ocfs2_extent_tree et;
3771 handle_t *handle = NULL;
3772 struct ocfs2_dx_root_block *dx_root;
3773 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3775 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3776 (unsigned long long)OCFS2_I(dir)->ip_blkno,
3777 (unsigned long long)leaf_blkno, insert_hash);
3779 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
3781 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3782 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3783 return -ENOSPC;
3785 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3786 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3787 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3788 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3789 (unsigned long long)leaf_blkno, num_used);
3790 ret = -EIO;
3791 goto out;
3794 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3795 if (!orig_dx_leaves) {
3796 ret = -ENOMEM;
3797 mlog_errno(ret);
3798 goto out;
3801 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3802 if (!new_dx_leaves) {
3803 ret = -ENOMEM;
3804 mlog_errno(ret);
3805 goto out;
3808 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3809 if (ret) {
3810 if (ret != -ENOSPC)
3811 mlog_errno(ret);
3812 goto out;
3815 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3816 handle = ocfs2_start_trans(osb, credits);
3817 if (IS_ERR(handle)) {
3818 ret = PTR_ERR(handle);
3819 handle = NULL;
3820 mlog_errno(ret);
3821 goto out;
3824 ret = dquot_alloc_space_nodirty(dir,
3825 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3826 if (ret)
3827 goto out_commit;
3828 did_quota = 1;
3830 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
3831 OCFS2_JOURNAL_ACCESS_WRITE);
3832 if (ret) {
3833 mlog_errno(ret);
3834 goto out_commit;
3838 * This block is changing anyway, so we can sort it in place.
3840 sort(dx_leaf->dl_list.de_entries, num_used,
3841 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3842 dx_leaf_sort_swap);
3844 ocfs2_journal_dirty(handle, dx_leaf_bh);
3846 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3847 &split_hash);
3848 if (ret) {
3849 mlog_errno(ret);
3850 goto out_commit;
3853 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3854 leaf_cpos, split_hash, insert_hash);
3857 * We have to carefully order operations here. There are items
3858 * which want to be in the new cluster before insert, but in
3859 * order to put those items in the new cluster, we alter the
3860 * old cluster. A failure to insert gets nasty.
3862 * So, start by reserving writes to the old
3863 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3864 * the new cluster for us, before inserting it. The insert
3865 * won't happen if there's an error before that. Once the
3866 * insert is done then, we can transfer from one leaf into the
3867 * other without fear of hitting any error.
3871 * The leaf transfer wants some scratch space so that we don't
3872 * wind up doing a bunch of expensive memmove().
3874 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3875 if (!tmp_dx_leaf) {
3876 ret = -ENOMEM;
3877 mlog_errno(ret);
3878 goto out_commit;
3881 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
3882 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3883 orig_dx_leaves);
3884 if (ret) {
3885 mlog_errno(ret);
3886 goto out_commit;
3889 cpos = split_hash;
3890 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3891 data_ac, meta_ac, new_dx_leaves,
3892 num_dx_leaves);
3893 if (ret) {
3894 mlog_errno(ret);
3895 goto out_commit;
3898 for (i = 0; i < num_dx_leaves; i++) {
3899 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3900 orig_dx_leaves[i],
3901 OCFS2_JOURNAL_ACCESS_WRITE);
3902 if (ret) {
3903 mlog_errno(ret);
3904 goto out_commit;
3907 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3908 new_dx_leaves[i],
3909 OCFS2_JOURNAL_ACCESS_WRITE);
3910 if (ret) {
3911 mlog_errno(ret);
3912 goto out_commit;
3916 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3917 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3919 out_commit:
3920 if (ret < 0 && did_quota)
3921 dquot_free_space_nodirty(dir,
3922 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3924 ocfs2_commit_trans(osb, handle);
3926 out:
3927 if (orig_dx_leaves || new_dx_leaves) {
3928 for (i = 0; i < num_dx_leaves; i++) {
3929 if (orig_dx_leaves)
3930 brelse(orig_dx_leaves[i]);
3931 if (new_dx_leaves)
3932 brelse(new_dx_leaves[i]);
3934 kfree(orig_dx_leaves);
3935 kfree(new_dx_leaves);
3938 if (meta_ac)
3939 ocfs2_free_alloc_context(meta_ac);
3940 if (data_ac)
3941 ocfs2_free_alloc_context(data_ac);
3943 kfree(tmp_dx_leaf);
3944 return ret;
3947 static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3948 struct buffer_head *di_bh,
3949 struct buffer_head *dx_root_bh,
3950 const char *name, int namelen,
3951 struct ocfs2_dir_lookup_result *lookup)
3953 int ret, rebalanced = 0;
3954 struct ocfs2_dx_root_block *dx_root;
3955 struct buffer_head *dx_leaf_bh = NULL;
3956 struct ocfs2_dx_leaf *dx_leaf;
3957 u64 blkno;
3958 u32 leaf_cpos;
3960 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3962 restart_search:
3963 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3964 &leaf_cpos, &blkno);
3965 if (ret) {
3966 mlog_errno(ret);
3967 goto out;
3970 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3971 if (ret) {
3972 mlog_errno(ret);
3973 goto out;
3976 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3978 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3979 le16_to_cpu(dx_leaf->dl_list.de_count)) {
3980 if (rebalanced) {
3981 ret = -ENOSPC;
3982 goto out;
3985 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3986 &lookup->dl_hinfo, leaf_cpos,
3987 blkno);
3988 if (ret) {
3989 if (ret != -ENOSPC)
3990 mlog_errno(ret);
3991 goto out;
3995 * Restart the lookup. The rebalance might have
3996 * changed which block our item fits into. Mark our
3997 * progress, so we only execute this once.
3999 brelse(dx_leaf_bh);
4000 dx_leaf_bh = NULL;
4001 rebalanced = 1;
4002 goto restart_search;
4005 lookup->dl_dx_leaf_bh = dx_leaf_bh;
4006 dx_leaf_bh = NULL;
4008 out:
4009 brelse(dx_leaf_bh);
4010 return ret;
4013 static int ocfs2_search_dx_free_list(struct inode *dir,
4014 struct buffer_head *dx_root_bh,
4015 int namelen,
4016 struct ocfs2_dir_lookup_result *lookup)
4018 int ret = -ENOSPC;
4019 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
4020 struct ocfs2_dir_block_trailer *db;
4021 u64 next_block;
4022 int rec_len = OCFS2_DIR_REC_LEN(namelen);
4023 struct ocfs2_dx_root_block *dx_root;
4025 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4026 next_block = le64_to_cpu(dx_root->dr_free_blk);
4028 while (next_block) {
4029 brelse(prev_leaf_bh);
4030 prev_leaf_bh = leaf_bh;
4031 leaf_bh = NULL;
4033 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
4034 if (ret) {
4035 mlog_errno(ret);
4036 goto out;
4039 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
4040 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
4041 lookup->dl_leaf_bh = leaf_bh;
4042 lookup->dl_prev_leaf_bh = prev_leaf_bh;
4043 leaf_bh = NULL;
4044 prev_leaf_bh = NULL;
4045 break;
4048 next_block = le64_to_cpu(db->db_free_next);
4051 if (!next_block)
4052 ret = -ENOSPC;
4054 out:
4056 brelse(leaf_bh);
4057 brelse(prev_leaf_bh);
4058 return ret;
4061 static int ocfs2_expand_inline_dx_root(struct inode *dir,
4062 struct buffer_head *dx_root_bh)
4064 int ret, num_dx_leaves, i, j, did_quota = 0;
4065 struct buffer_head **dx_leaves = NULL;
4066 struct ocfs2_extent_tree et;
4067 u64 insert_blkno;
4068 struct ocfs2_alloc_context *data_ac = NULL;
4069 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4070 handle_t *handle = NULL;
4071 struct ocfs2_dx_root_block *dx_root;
4072 struct ocfs2_dx_entry_list *entry_list;
4073 struct ocfs2_dx_entry *dx_entry;
4074 struct ocfs2_dx_leaf *target_leaf;
4076 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4077 if (ret) {
4078 mlog_errno(ret);
4079 goto out;
4082 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4083 if (!dx_leaves) {
4084 ret = -ENOMEM;
4085 mlog_errno(ret);
4086 goto out;
4089 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4090 if (IS_ERR(handle)) {
4091 ret = PTR_ERR(handle);
4092 mlog_errno(ret);
4093 goto out;
4096 ret = dquot_alloc_space_nodirty(dir,
4097 ocfs2_clusters_to_bytes(osb->sb, 1));
4098 if (ret)
4099 goto out_commit;
4100 did_quota = 1;
4103 * We do this up front, before the allocation, so that a
4104 * failure to add the dx_root_bh to the journal won't result
4105 * us losing clusters.
4107 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4108 OCFS2_JOURNAL_ACCESS_WRITE);
4109 if (ret) {
4110 mlog_errno(ret);
4111 goto out_commit;
4114 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4115 num_dx_leaves, &insert_blkno);
4116 if (ret) {
4117 mlog_errno(ret);
4118 goto out_commit;
4122 * Transfer the entries from our dx_root into the appropriate
4123 * block
4125 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4126 entry_list = &dx_root->dr_entries;
4128 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4129 dx_entry = &entry_list->de_entries[i];
4131 j = __ocfs2_dx_dir_hash_idx(osb,
4132 le32_to_cpu(dx_entry->dx_minor_hash));
4133 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4135 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4137 /* Each leaf has been passed to the journal already
4138 * via __ocfs2_dx_dir_new_cluster() */
4141 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4142 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
4143 offsetof(struct ocfs2_dx_root_block, dr_list));
4144 dx_root->dr_list.l_count =
4145 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4147 /* This should never fail considering we start with an empty
4148 * dx_root. */
4149 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4150 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4151 if (ret)
4152 mlog_errno(ret);
4153 did_quota = 0;
4155 ocfs2_journal_dirty(handle, dx_root_bh);
4157 out_commit:
4158 if (ret < 0 && did_quota)
4159 dquot_free_space_nodirty(dir,
4160 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4162 ocfs2_commit_trans(osb, handle);
4164 out:
4165 if (data_ac)
4166 ocfs2_free_alloc_context(data_ac);
4168 if (dx_leaves) {
4169 for (i = 0; i < num_dx_leaves; i++)
4170 brelse(dx_leaves[i]);
4171 kfree(dx_leaves);
4173 return ret;
4176 static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4178 struct ocfs2_dx_root_block *dx_root;
4179 struct ocfs2_dx_entry_list *entry_list;
4181 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4182 entry_list = &dx_root->dr_entries;
4184 if (le16_to_cpu(entry_list->de_num_used) >=
4185 le16_to_cpu(entry_list->de_count))
4186 return -ENOSPC;
4188 return 0;
4191 static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4192 struct buffer_head *di_bh,
4193 const char *name,
4194 int namelen,
4195 struct ocfs2_dir_lookup_result *lookup)
4197 int ret, free_dx_root = 1;
4198 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4199 struct buffer_head *dx_root_bh = NULL;
4200 struct buffer_head *leaf_bh = NULL;
4201 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4202 struct ocfs2_dx_root_block *dx_root;
4204 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4205 if (ret) {
4206 mlog_errno(ret);
4207 goto out;
4210 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4211 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4212 ret = -ENOSPC;
4213 mlog_errno(ret);
4214 goto out;
4217 if (ocfs2_dx_root_inline(dx_root)) {
4218 ret = ocfs2_inline_dx_has_space(dx_root_bh);
4220 if (ret == 0)
4221 goto search_el;
4224 * We ran out of room in the root block. Expand it to
4225 * an extent, then allow ocfs2_find_dir_space_dx to do
4226 * the rest.
4228 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4229 if (ret) {
4230 mlog_errno(ret);
4231 goto out;
4236 * Insert preparation for an indexed directory is split into two
4237 * steps. The call to find_dir_space_dx reserves room in the index for
4238 * an additional item. If we run out of space there, it's a real error
4239 * we can't continue on.
4241 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4242 namelen, lookup);
4243 if (ret) {
4244 mlog_errno(ret);
4245 goto out;
4248 search_el:
4250 * Next, we need to find space in the unindexed tree. This call
4251 * searches using the free space linked list. If the unindexed tree
4252 * lacks sufficient space, we'll expand it below. The expansion code
4253 * is smart enough to add any new blocks to the free space list.
4255 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4256 if (ret && ret != -ENOSPC) {
4257 mlog_errno(ret);
4258 goto out;
4261 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4262 lookup->dl_dx_root_bh = dx_root_bh;
4263 free_dx_root = 0;
4265 if (ret == -ENOSPC) {
4266 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
4268 if (ret) {
4269 mlog_errno(ret);
4270 goto out;
4274 * We make the assumption here that new leaf blocks are added
4275 * to the front of our free list.
4277 lookup->dl_prev_leaf_bh = NULL;
4278 lookup->dl_leaf_bh = leaf_bh;
4281 out:
4282 if (free_dx_root)
4283 brelse(dx_root_bh);
4284 return ret;
4288 * Get a directory ready for insert. Any directory allocation required
4289 * happens here. Success returns zero, and enough context in the dir
4290 * lookup result that ocfs2_add_entry() will be able complete the task
4291 * with minimal performance impact.
4293 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4294 struct inode *dir,
4295 struct buffer_head *parent_fe_bh,
4296 const char *name,
4297 int namelen,
4298 struct ocfs2_dir_lookup_result *lookup)
4300 int ret;
4301 unsigned int blocks_wanted = 1;
4302 struct buffer_head *bh = NULL;
4304 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
4305 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
4307 if (!namelen) {
4308 ret = -EINVAL;
4309 mlog_errno(ret);
4310 goto out;
4314 * Do this up front to reduce confusion.
4316 * The directory might start inline, then be turned into an
4317 * indexed one, in which case we'd need to hash deep inside
4318 * ocfs2_find_dir_space_id(). Since
4319 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4320 * done, there seems no point in spreading out the calls. We
4321 * can optimize away the case where the file system doesn't
4322 * support indexing.
4324 if (ocfs2_supports_indexed_dirs(osb))
4325 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4327 if (ocfs2_dir_indexed(dir)) {
4328 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4329 name, namelen, lookup);
4330 if (ret)
4331 mlog_errno(ret);
4332 goto out;
4335 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4336 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4337 namelen, &bh, &blocks_wanted);
4338 } else
4339 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4341 if (ret && ret != -ENOSPC) {
4342 mlog_errno(ret);
4343 goto out;
4346 if (ret == -ENOSPC) {
4348 * We have to expand the directory to add this name.
4350 BUG_ON(bh);
4352 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
4353 lookup, &bh);
4354 if (ret) {
4355 if (ret != -ENOSPC)
4356 mlog_errno(ret);
4357 goto out;
4360 BUG_ON(!bh);
4363 lookup->dl_leaf_bh = bh;
4364 bh = NULL;
4365 out:
4366 brelse(bh);
4367 return ret;
4370 static int ocfs2_dx_dir_remove_index(struct inode *dir,
4371 struct buffer_head *di_bh,
4372 struct buffer_head *dx_root_bh)
4374 int ret;
4375 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4376 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4377 struct ocfs2_dx_root_block *dx_root;
4378 struct inode *dx_alloc_inode = NULL;
4379 struct buffer_head *dx_alloc_bh = NULL;
4380 handle_t *handle;
4381 u64 blk;
4382 u16 bit;
4383 u64 bg_blkno;
4385 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4387 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4388 EXTENT_ALLOC_SYSTEM_INODE,
4389 le16_to_cpu(dx_root->dr_suballoc_slot));
4390 if (!dx_alloc_inode) {
4391 ret = -ENOMEM;
4392 mlog_errno(ret);
4393 goto out;
4395 mutex_lock(&dx_alloc_inode->i_mutex);
4397 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4398 if (ret) {
4399 mlog_errno(ret);
4400 goto out_mutex;
4403 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4404 if (IS_ERR(handle)) {
4405 ret = PTR_ERR(handle);
4406 mlog_errno(ret);
4407 goto out_unlock;
4410 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
4411 OCFS2_JOURNAL_ACCESS_WRITE);
4412 if (ret) {
4413 mlog_errno(ret);
4414 goto out_commit;
4417 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4418 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4419 di->i_dx_root = cpu_to_le64(0ULL);
4421 ocfs2_journal_dirty(handle, di_bh);
4423 blk = le64_to_cpu(dx_root->dr_blkno);
4424 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4425 if (dx_root->dr_suballoc_loc)
4426 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4427 else
4428 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4429 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4430 bit, bg_blkno, 1);
4431 if (ret)
4432 mlog_errno(ret);
4434 out_commit:
4435 ocfs2_commit_trans(osb, handle);
4437 out_unlock:
4438 ocfs2_inode_unlock(dx_alloc_inode, 1);
4440 out_mutex:
4441 mutex_unlock(&dx_alloc_inode->i_mutex);
4442 brelse(dx_alloc_bh);
4443 out:
4444 iput(dx_alloc_inode);
4445 return ret;
4448 int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4450 int ret;
4451 unsigned int uninitialized_var(clen);
4452 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4453 u64 uninitialized_var(blkno);
4454 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4455 struct buffer_head *dx_root_bh = NULL;
4456 struct ocfs2_dx_root_block *dx_root;
4457 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4458 struct ocfs2_cached_dealloc_ctxt dealloc;
4459 struct ocfs2_extent_tree et;
4461 ocfs2_init_dealloc_ctxt(&dealloc);
4463 if (!ocfs2_dir_indexed(dir))
4464 return 0;
4466 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4467 if (ret) {
4468 mlog_errno(ret);
4469 goto out;
4471 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4473 if (ocfs2_dx_root_inline(dx_root))
4474 goto remove_index;
4476 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4478 while (le32_to_cpu(dx_root->dr_clusters)) {
4479 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4480 major_hash, &cpos, &blkno, &clen);
4481 if (ret) {
4482 mlog_errno(ret);
4483 goto out;
4486 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4488 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
4489 &dealloc, 0);
4490 if (ret) {
4491 mlog_errno(ret);
4492 goto out;
4495 if (cpos == 0)
4496 break;
4498 major_hash = cpos - 1;
4501 remove_index:
4502 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4503 if (ret) {
4504 mlog_errno(ret);
4505 goto out;
4508 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
4509 out:
4510 ocfs2_schedule_truncate_log_flush(osb, 1);
4511 ocfs2_run_deallocs(osb, &dealloc);
4513 brelse(dx_root_bh);
4514 return ret;