MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / fs / ocfs2 / dir.c
blobf2c4098cf337fd077b1d77835144cc454ce5e8dc
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>
45 #define MLOG_MASK_PREFIX ML_NAMEI
46 #include <cluster/masklog.h>
48 #include "ocfs2.h"
50 #include "alloc.h"
51 #include "blockcheck.h"
52 #include "dir.h"
53 #include "dlmglue.h"
54 #include "extent_map.h"
55 #include "file.h"
56 #include "inode.h"
57 #include "journal.h"
58 #include "namei.h"
59 #include "suballoc.h"
60 #include "super.h"
61 #include "uptodate.h"
63 #include "buffer_head_io.h"
65 #define NAMEI_RA_CHUNKS 2
66 #define NAMEI_RA_BLOCKS 4
67 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
68 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
70 static unsigned char ocfs2_filetype_table[] = {
71 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
74 static int ocfs2_extend_dir(struct ocfs2_super *osb,
75 struct inode *dir,
76 struct buffer_head *parent_fe_bh,
77 unsigned int blocks_wanted,
78 struct buffer_head **new_de_bh);
79 static int ocfs2_do_extend_dir(struct super_block *sb,
80 handle_t *handle,
81 struct inode *dir,
82 struct buffer_head *parent_fe_bh,
83 struct ocfs2_alloc_context *data_ac,
84 struct ocfs2_alloc_context *meta_ac,
85 struct buffer_head **new_bh);
88 * These are distinct checks because future versions of the file system will
89 * want to have a trailing dirent structure independent of indexing.
91 static int ocfs2_dir_has_trailer(struct inode *dir)
93 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
96 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
99 static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
101 return ocfs2_meta_ecc(osb);
104 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
106 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
109 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
111 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
112 * them more consistent? */
113 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
114 void *data)
116 char *p = data;
118 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
119 return (struct ocfs2_dir_block_trailer *)p;
123 * XXX: This is executed once on every dirent. We should consider optimizing
124 * it.
126 static int ocfs2_skip_dir_trailer(struct inode *dir,
127 struct ocfs2_dir_entry *de,
128 unsigned long offset,
129 unsigned long blklen)
131 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
133 if (!ocfs2_dir_has_trailer(dir))
134 return 0;
136 if (offset != toff)
137 return 0;
139 return 1;
142 static void ocfs2_init_dir_trailer(struct inode *inode,
143 struct buffer_head *bh)
145 struct ocfs2_dir_block_trailer *trailer;
147 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
148 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
149 trailer->db_compat_rec_len =
150 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
151 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
152 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
156 * bh passed here can be an inode block or a dir data block, depending
157 * on the inode inline data flag.
159 static int ocfs2_check_dir_entry(struct inode * dir,
160 struct ocfs2_dir_entry * de,
161 struct buffer_head * bh,
162 unsigned long offset)
164 const char *error_msg = NULL;
165 const int rlen = le16_to_cpu(de->rec_len);
167 if (rlen < OCFS2_DIR_REC_LEN(1))
168 error_msg = "rec_len is smaller than minimal";
169 else if (rlen % 4 != 0)
170 error_msg = "rec_len % 4 != 0";
171 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
172 error_msg = "rec_len is too small for name_len";
173 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
174 error_msg = "directory entry across blocks";
176 if (error_msg != NULL)
177 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
178 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
179 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
180 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
181 de->name_len);
182 return error_msg == NULL ? 1 : 0;
185 static inline int ocfs2_match(int len,
186 const char * const name,
187 struct ocfs2_dir_entry *de)
189 if (len != de->name_len)
190 return 0;
191 if (!de->inode)
192 return 0;
193 return !memcmp(name, de->name, len);
197 * Returns 0 if not found, -1 on failure, and 1 on success
199 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
200 struct inode *dir,
201 const char *name, int namelen,
202 unsigned long offset,
203 char *first_de,
204 unsigned int bytes,
205 struct ocfs2_dir_entry **res_dir)
207 struct ocfs2_dir_entry *de;
208 char *dlimit, *de_buf;
209 int de_len;
210 int ret = 0;
212 mlog_entry_void();
214 de_buf = first_de;
215 dlimit = de_buf + bytes;
217 while (de_buf < dlimit) {
218 /* this code is executed quadratically often */
219 /* do minimal checking `by hand' */
221 de = (struct ocfs2_dir_entry *) de_buf;
223 if (de_buf + namelen <= dlimit &&
224 ocfs2_match(namelen, name, de)) {
225 /* found a match - just to be sure, do a full check */
226 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
227 ret = -1;
228 goto bail;
230 *res_dir = de;
231 ret = 1;
232 goto bail;
235 /* prevent looping on a bad block */
236 de_len = le16_to_cpu(de->rec_len);
237 if (de_len <= 0) {
238 ret = -1;
239 goto bail;
242 de_buf += de_len;
243 offset += de_len;
246 bail:
247 mlog_exit(ret);
248 return ret;
251 static struct buffer_head *ocfs2_find_entry_id(const char *name,
252 int namelen,
253 struct inode *dir,
254 struct ocfs2_dir_entry **res_dir)
256 int ret, found;
257 struct buffer_head *di_bh = NULL;
258 struct ocfs2_dinode *di;
259 struct ocfs2_inline_data *data;
261 ret = ocfs2_read_inode_block(dir, &di_bh);
262 if (ret) {
263 mlog_errno(ret);
264 goto out;
267 di = (struct ocfs2_dinode *)di_bh->b_data;
268 data = &di->id2.i_data;
270 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
271 data->id_data, i_size_read(dir), res_dir);
272 if (found == 1)
273 return di_bh;
275 brelse(di_bh);
276 out:
277 return NULL;
280 static int ocfs2_validate_dir_block(struct super_block *sb,
281 struct buffer_head *bh)
283 int rc;
284 struct ocfs2_dir_block_trailer *trailer =
285 ocfs2_trailer_from_bh(bh, sb);
289 * We don't validate dirents here, that's handled
290 * in-place when the code walks them.
292 mlog(0, "Validating dirblock %llu\n",
293 (unsigned long long)bh->b_blocknr);
295 BUG_ON(!buffer_uptodate(bh));
298 * If the ecc fails, we return the error but otherwise
299 * leave the filesystem running. We know any error is
300 * local to this block.
302 * Note that we are safe to call this even if the directory
303 * doesn't have a trailer. Filesystems without metaecc will do
304 * nothing, and filesystems with it will have one.
306 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
307 if (rc)
308 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
309 (unsigned long long)bh->b_blocknr);
311 return rc;
315 * This function forces all errors to -EIO for consistency with its
316 * predecessor, ocfs2_bread(). We haven't audited what returning the
317 * real error codes would do to callers. We log the real codes with
318 * mlog_errno() before we squash them.
320 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
321 struct buffer_head **bh, int flags)
323 int rc = 0;
324 struct buffer_head *tmp = *bh;
325 struct ocfs2_dir_block_trailer *trailer;
327 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
328 ocfs2_validate_dir_block);
329 if (rc) {
330 mlog_errno(rc);
331 goto out;
335 * We check the trailer here rather than in
336 * ocfs2_validate_dir_block() because that function doesn't have
337 * the inode to test.
339 if (!(flags & OCFS2_BH_READAHEAD) &&
340 ocfs2_dir_has_trailer(inode)) {
341 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
342 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
343 rc = -EINVAL;
344 ocfs2_error(inode->i_sb,
345 "Invalid dirblock #%llu: "
346 "signature = %.*s\n",
347 (unsigned long long)tmp->b_blocknr, 7,
348 trailer->db_signature);
349 goto out;
351 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
352 rc = -EINVAL;
353 ocfs2_error(inode->i_sb,
354 "Directory block #%llu has an invalid "
355 "db_blkno of %llu",
356 (unsigned long long)tmp->b_blocknr,
357 (unsigned long long)le64_to_cpu(trailer->db_blkno));
358 goto out;
360 if (le64_to_cpu(trailer->db_parent_dinode) !=
361 OCFS2_I(inode)->ip_blkno) {
362 rc = -EINVAL;
363 ocfs2_error(inode->i_sb,
364 "Directory block #%llu on dinode "
365 "#%llu has an invalid parent_dinode "
366 "of %llu",
367 (unsigned long long)tmp->b_blocknr,
368 (unsigned long long)OCFS2_I(inode)->ip_blkno,
369 (unsigned long long)le64_to_cpu(trailer->db_blkno));
370 goto out;
374 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
375 if (!*bh)
376 *bh = tmp;
378 out:
379 return rc ? -EIO : 0;
382 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
383 struct inode *dir,
384 struct ocfs2_dir_entry **res_dir)
386 struct super_block *sb;
387 struct buffer_head *bh_use[NAMEI_RA_SIZE];
388 struct buffer_head *bh, *ret = NULL;
389 unsigned long start, block, b;
390 int ra_max = 0; /* Number of bh's in the readahead
391 buffer, bh_use[] */
392 int ra_ptr = 0; /* Current index into readahead
393 buffer */
394 int num = 0;
395 int nblocks, i, err;
397 mlog_entry_void();
399 sb = dir->i_sb;
401 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
402 start = OCFS2_I(dir)->ip_dir_start_lookup;
403 if (start >= nblocks)
404 start = 0;
405 block = start;
407 restart:
408 do {
410 * We deal with the read-ahead logic here.
412 if (ra_ptr >= ra_max) {
413 /* Refill the readahead buffer */
414 ra_ptr = 0;
415 b = block;
416 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
418 * Terminate if we reach the end of the
419 * directory and must wrap, or if our
420 * search has finished at this block.
422 if (b >= nblocks || (num && block == start)) {
423 bh_use[ra_max] = NULL;
424 break;
426 num++;
428 bh = NULL;
429 err = ocfs2_read_dir_block(dir, b++, &bh,
430 OCFS2_BH_READAHEAD);
431 bh_use[ra_max] = bh;
434 if ((bh = bh_use[ra_ptr++]) == NULL)
435 goto next;
436 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
437 /* read error, skip block & hope for the best.
438 * ocfs2_read_dir_block() has released the bh. */
439 ocfs2_error(dir->i_sb, "reading directory %llu, "
440 "offset %lu\n",
441 (unsigned long long)OCFS2_I(dir)->ip_blkno,
442 block);
443 goto next;
445 i = ocfs2_search_dirblock(bh, dir, name, namelen,
446 block << sb->s_blocksize_bits,
447 bh->b_data, sb->s_blocksize,
448 res_dir);
449 if (i == 1) {
450 OCFS2_I(dir)->ip_dir_start_lookup = block;
451 ret = bh;
452 goto cleanup_and_exit;
453 } else {
454 brelse(bh);
455 if (i < 0)
456 goto cleanup_and_exit;
458 next:
459 if (++block >= nblocks)
460 block = 0;
461 } while (block != start);
464 * If the directory has grown while we were searching, then
465 * search the last part of the directory before giving up.
467 block = nblocks;
468 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
469 if (block < nblocks) {
470 start = 0;
471 goto restart;
474 cleanup_and_exit:
475 /* Clean up the read-ahead blocks */
476 for (; ra_ptr < ra_max; ra_ptr++)
477 brelse(bh_use[ra_ptr]);
479 mlog_exit_ptr(ret);
480 return ret;
484 * Try to find an entry of the provided name within 'dir'.
486 * If nothing was found, NULL is returned. Otherwise, a buffer_head
487 * and pointer to the dir entry are passed back.
489 * Caller can NOT assume anything about the contents of the
490 * buffer_head - it is passed back only so that it can be passed into
491 * any one of the manipulation functions (add entry, delete entry,
492 * etc). As an example, bh in the extent directory case is a data
493 * block, in the inline-data case it actually points to an inode.
495 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
496 struct inode *dir,
497 struct ocfs2_dir_entry **res_dir)
499 *res_dir = NULL;
501 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
502 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
504 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
508 * Update inode number and type of a previously found directory entry.
510 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
511 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
512 struct inode *new_entry_inode)
514 int ret;
515 ocfs2_journal_access_func access = ocfs2_journal_access_db;
518 * The same code works fine for both inline-data and extent
519 * based directories, so no need to split this up. The only
520 * difference is the journal_access function.
523 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
524 access = ocfs2_journal_access_di;
526 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
527 if (ret) {
528 mlog_errno(ret);
529 goto out;
532 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
533 ocfs2_set_de_type(de, new_entry_inode->i_mode);
535 ocfs2_journal_dirty(handle, de_bh);
537 out:
538 return ret;
541 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
542 struct ocfs2_dir_entry *de_del,
543 struct buffer_head *bh, char *first_de,
544 unsigned int bytes)
546 struct ocfs2_dir_entry *de, *pde;
547 int i, status = -ENOENT;
548 ocfs2_journal_access_func access = ocfs2_journal_access_db;
550 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
552 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
553 access = ocfs2_journal_access_di;
555 i = 0;
556 pde = NULL;
557 de = (struct ocfs2_dir_entry *) first_de;
558 while (i < bytes) {
559 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
560 status = -EIO;
561 mlog_errno(status);
562 goto bail;
564 if (de == de_del) {
565 status = access(handle, dir, bh,
566 OCFS2_JOURNAL_ACCESS_WRITE);
567 if (status < 0) {
568 status = -EIO;
569 mlog_errno(status);
570 goto bail;
572 if (pde)
573 le16_add_cpu(&pde->rec_len,
574 le16_to_cpu(de->rec_len));
575 else
576 de->inode = 0;
577 dir->i_version++;
578 status = ocfs2_journal_dirty(handle, bh);
579 goto bail;
581 i += le16_to_cpu(de->rec_len);
582 pde = de;
583 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
585 bail:
586 mlog_exit(status);
587 return status;
590 static inline int ocfs2_delete_entry_id(handle_t *handle,
591 struct inode *dir,
592 struct ocfs2_dir_entry *de_del,
593 struct buffer_head *bh)
595 int ret;
596 struct buffer_head *di_bh = NULL;
597 struct ocfs2_dinode *di;
598 struct ocfs2_inline_data *data;
600 ret = ocfs2_read_inode_block(dir, &di_bh);
601 if (ret) {
602 mlog_errno(ret);
603 goto out;
606 di = (struct ocfs2_dinode *)di_bh->b_data;
607 data = &di->id2.i_data;
609 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
610 i_size_read(dir));
612 brelse(di_bh);
613 out:
614 return ret;
617 static inline int ocfs2_delete_entry_el(handle_t *handle,
618 struct inode *dir,
619 struct ocfs2_dir_entry *de_del,
620 struct buffer_head *bh)
622 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
623 bh->b_size);
627 * ocfs2_delete_entry deletes a directory entry by merging it with the
628 * previous entry
630 int ocfs2_delete_entry(handle_t *handle,
631 struct inode *dir,
632 struct ocfs2_dir_entry *de_del,
633 struct buffer_head *bh)
635 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
636 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
638 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
642 * Check whether 'de' has enough room to hold an entry of
643 * 'new_rec_len' bytes.
645 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
646 unsigned int new_rec_len)
648 unsigned int de_really_used;
650 /* Check whether this is an empty record with enough space */
651 if (le64_to_cpu(de->inode) == 0 &&
652 le16_to_cpu(de->rec_len) >= new_rec_len)
653 return 1;
656 * Record might have free space at the end which we can
657 * use.
659 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
660 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
661 return 1;
663 return 0;
666 /* we don't always have a dentry for what we want to add, so people
667 * like orphan dir can call this instead.
669 * If you pass me insert_bh, I'll skip the search of the other dir
670 * blocks and put the record in there.
672 int __ocfs2_add_entry(handle_t *handle,
673 struct inode *dir,
674 const char *name, int namelen,
675 struct inode *inode, u64 blkno,
676 struct buffer_head *parent_fe_bh,
677 struct buffer_head *insert_bh)
679 unsigned long offset;
680 unsigned short rec_len;
681 struct ocfs2_dir_entry *de, *de1;
682 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
683 struct super_block *sb = dir->i_sb;
684 int retval, status;
685 unsigned int size = sb->s_blocksize;
686 char *data_start = insert_bh->b_data;
688 mlog_entry_void();
690 if (!namelen)
691 return -EINVAL;
693 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
694 data_start = di->id2.i_data.id_data;
695 size = i_size_read(dir);
697 BUG_ON(insert_bh != parent_fe_bh);
700 rec_len = OCFS2_DIR_REC_LEN(namelen);
701 offset = 0;
702 de = (struct ocfs2_dir_entry *) data_start;
703 while (1) {
704 BUG_ON((char *)de >= (size + data_start));
706 /* These checks should've already been passed by the
707 * prepare function, but I guess we can leave them
708 * here anyway. */
709 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
710 retval = -ENOENT;
711 goto bail;
713 if (ocfs2_match(namelen, name, de)) {
714 retval = -EEXIST;
715 goto bail;
718 /* We're guaranteed that we should have space, so we
719 * can't possibly have hit the trailer...right? */
720 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
721 "Hit dir trailer trying to insert %.*s "
722 "(namelen %d) into directory %llu. "
723 "offset is %lu, trailer offset is %d\n",
724 namelen, name, namelen,
725 (unsigned long long)parent_fe_bh->b_blocknr,
726 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
728 if (ocfs2_dirent_would_fit(de, rec_len)) {
729 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
730 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
731 if (retval < 0) {
732 mlog_errno(retval);
733 goto bail;
736 if (insert_bh == parent_fe_bh)
737 status = ocfs2_journal_access_di(handle, dir,
738 insert_bh,
739 OCFS2_JOURNAL_ACCESS_WRITE);
740 else
741 status = ocfs2_journal_access_db(handle, dir,
742 insert_bh,
743 OCFS2_JOURNAL_ACCESS_WRITE);
744 /* By now the buffer is marked for journaling */
745 offset += le16_to_cpu(de->rec_len);
746 if (le64_to_cpu(de->inode)) {
747 de1 = (struct ocfs2_dir_entry *)((char *) de +
748 OCFS2_DIR_REC_LEN(de->name_len));
749 de1->rec_len =
750 cpu_to_le16(le16_to_cpu(de->rec_len) -
751 OCFS2_DIR_REC_LEN(de->name_len));
752 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
753 de = de1;
755 de->file_type = OCFS2_FT_UNKNOWN;
756 if (blkno) {
757 de->inode = cpu_to_le64(blkno);
758 ocfs2_set_de_type(de, inode->i_mode);
759 } else
760 de->inode = 0;
761 de->name_len = namelen;
762 memcpy(de->name, name, namelen);
764 dir->i_version++;
765 status = ocfs2_journal_dirty(handle, insert_bh);
766 retval = 0;
767 goto bail;
770 offset += le16_to_cpu(de->rec_len);
771 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
774 /* when you think about it, the assert above should prevent us
775 * from ever getting here. */
776 retval = -ENOSPC;
777 bail:
779 mlog_exit(retval);
780 return retval;
783 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
784 u64 *f_version,
785 loff_t *f_pos, void *priv,
786 filldir_t filldir, int *filldir_err)
788 int ret, i, filldir_ret;
789 unsigned long offset = *f_pos;
790 struct buffer_head *di_bh = NULL;
791 struct ocfs2_dinode *di;
792 struct ocfs2_inline_data *data;
793 struct ocfs2_dir_entry *de;
795 ret = ocfs2_read_inode_block(inode, &di_bh);
796 if (ret) {
797 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
798 (unsigned long long)OCFS2_I(inode)->ip_blkno);
799 goto out;
802 di = (struct ocfs2_dinode *)di_bh->b_data;
803 data = &di->id2.i_data;
805 while (*f_pos < i_size_read(inode)) {
806 revalidate:
807 /* If the dir block has changed since the last call to
808 * readdir(2), then we might be pointing to an invalid
809 * dirent right now. Scan from the start of the block
810 * to make sure. */
811 if (*f_version != inode->i_version) {
812 for (i = 0; i < i_size_read(inode) && i < offset; ) {
813 de = (struct ocfs2_dir_entry *)
814 (data->id_data + i);
815 /* It's too expensive to do a full
816 * dirent test each time round this
817 * loop, but we do have to test at
818 * least that it is non-zero. A
819 * failure will be detected in the
820 * dirent test below. */
821 if (le16_to_cpu(de->rec_len) <
822 OCFS2_DIR_REC_LEN(1))
823 break;
824 i += le16_to_cpu(de->rec_len);
826 *f_pos = offset = i;
827 *f_version = inode->i_version;
830 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
831 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
832 /* On error, skip the f_pos to the end. */
833 *f_pos = i_size_read(inode);
834 goto out;
836 offset += le16_to_cpu(de->rec_len);
837 if (le64_to_cpu(de->inode)) {
838 /* We might block in the next section
839 * if the data destination is
840 * currently swapped out. So, use a
841 * version stamp to detect whether or
842 * not the directory has been modified
843 * during the copy operation.
845 u64 version = *f_version;
846 unsigned char d_type = DT_UNKNOWN;
848 if (de->file_type < OCFS2_FT_MAX)
849 d_type = ocfs2_filetype_table[de->file_type];
851 filldir_ret = filldir(priv, de->name,
852 de->name_len,
853 *f_pos,
854 le64_to_cpu(de->inode),
855 d_type);
856 if (filldir_ret) {
857 if (filldir_err)
858 *filldir_err = filldir_ret;
859 break;
861 if (version != *f_version)
862 goto revalidate;
864 *f_pos += le16_to_cpu(de->rec_len);
867 out:
868 brelse(di_bh);
870 return 0;
873 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
874 u64 *f_version,
875 loff_t *f_pos, void *priv,
876 filldir_t filldir, int *filldir_err)
878 int error = 0;
879 unsigned long offset, blk, last_ra_blk = 0;
880 int i, stored;
881 struct buffer_head * bh, * tmp;
882 struct ocfs2_dir_entry * de;
883 struct super_block * sb = inode->i_sb;
884 unsigned int ra_sectors = 16;
886 stored = 0;
887 bh = NULL;
889 offset = (*f_pos) & (sb->s_blocksize - 1);
891 while (!error && !stored && *f_pos < i_size_read(inode)) {
892 blk = (*f_pos) >> sb->s_blocksize_bits;
893 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
894 /* Skip the corrupt dirblock and keep trying */
895 *f_pos += sb->s_blocksize - offset;
896 continue;
899 /* The idea here is to begin with 8k read-ahead and to stay
900 * 4k ahead of our current position.
902 * TODO: Use the pagecache for this. We just need to
903 * make sure it's cluster-safe... */
904 if (!last_ra_blk
905 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
906 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
907 i > 0; i--) {
908 tmp = NULL;
909 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
910 OCFS2_BH_READAHEAD))
911 brelse(tmp);
913 last_ra_blk = blk;
914 ra_sectors = 8;
917 revalidate:
918 /* If the dir block has changed since the last call to
919 * readdir(2), then we might be pointing to an invalid
920 * dirent right now. Scan from the start of the block
921 * to make sure. */
922 if (*f_version != inode->i_version) {
923 for (i = 0; i < sb->s_blocksize && i < offset; ) {
924 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
925 /* It's too expensive to do a full
926 * dirent test each time round this
927 * loop, but we do have to test at
928 * least that it is non-zero. A
929 * failure will be detected in the
930 * dirent test below. */
931 if (le16_to_cpu(de->rec_len) <
932 OCFS2_DIR_REC_LEN(1))
933 break;
934 i += le16_to_cpu(de->rec_len);
936 offset = i;
937 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
938 | offset;
939 *f_version = inode->i_version;
942 while (!error && *f_pos < i_size_read(inode)
943 && offset < sb->s_blocksize) {
944 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
945 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
946 /* On error, skip the f_pos to the
947 next block. */
948 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
949 brelse(bh);
950 goto out;
952 offset += le16_to_cpu(de->rec_len);
953 if (le64_to_cpu(de->inode)) {
954 /* We might block in the next section
955 * if the data destination is
956 * currently swapped out. So, use a
957 * version stamp to detect whether or
958 * not the directory has been modified
959 * during the copy operation.
961 unsigned long version = *f_version;
962 unsigned char d_type = DT_UNKNOWN;
964 if (de->file_type < OCFS2_FT_MAX)
965 d_type = ocfs2_filetype_table[de->file_type];
966 error = filldir(priv, de->name,
967 de->name_len,
968 *f_pos,
969 le64_to_cpu(de->inode),
970 d_type);
971 if (error) {
972 if (filldir_err)
973 *filldir_err = error;
974 break;
976 if (version != *f_version)
977 goto revalidate;
978 stored ++;
980 *f_pos += le16_to_cpu(de->rec_len);
982 offset = 0;
983 brelse(bh);
984 bh = NULL;
987 stored = 0;
988 out:
989 return stored;
992 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
993 loff_t *f_pos, void *priv, filldir_t filldir,
994 int *filldir_err)
996 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
997 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
998 filldir, filldir_err);
1000 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1001 filldir_err);
1005 * This is intended to be called from inside other kernel functions,
1006 * so we fake some arguments.
1008 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1009 filldir_t filldir)
1011 int ret = 0, filldir_err = 0;
1012 u64 version = inode->i_version;
1014 while (*f_pos < i_size_read(inode)) {
1015 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
1016 filldir, &filldir_err);
1017 if (ret || filldir_err)
1018 break;
1021 if (ret > 0)
1022 ret = -EIO;
1024 return 0;
1028 * ocfs2_readdir()
1031 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1033 int error = 0;
1034 struct inode *inode = filp->f_path.dentry->d_inode;
1035 int lock_level = 0;
1037 mlog_entry("dirino=%llu\n",
1038 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1040 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1041 if (lock_level && error >= 0) {
1042 /* We release EX lock which used to update atime
1043 * and get PR lock again to reduce contention
1044 * on commonly accessed directories. */
1045 ocfs2_inode_unlock(inode, 1);
1046 lock_level = 0;
1047 error = ocfs2_inode_lock(inode, NULL, 0);
1049 if (error < 0) {
1050 if (error != -ENOENT)
1051 mlog_errno(error);
1052 /* we haven't got any yet, so propagate the error. */
1053 goto bail_nolock;
1056 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
1057 dirent, filldir, NULL);
1059 ocfs2_inode_unlock(inode, lock_level);
1061 bail_nolock:
1062 mlog_exit(error);
1064 return error;
1068 * NOTE: this should always be called with parent dir i_mutex taken.
1070 int ocfs2_find_files_on_disk(const char *name,
1071 int namelen,
1072 u64 *blkno,
1073 struct inode *inode,
1074 struct buffer_head **dirent_bh,
1075 struct ocfs2_dir_entry **dirent)
1077 int status = -ENOENT;
1079 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1080 namelen, name, blkno, inode, dirent_bh, dirent);
1082 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
1083 if (!*dirent_bh || !*dirent) {
1084 status = -ENOENT;
1085 goto leave;
1088 *blkno = le64_to_cpu((*dirent)->inode);
1090 status = 0;
1091 leave:
1092 if (status < 0) {
1093 *dirent = NULL;
1094 brelse(*dirent_bh);
1095 *dirent_bh = NULL;
1098 mlog_exit(status);
1099 return status;
1103 * Convenience function for callers which just want the block number
1104 * mapped to a name and don't require the full dirent info, etc.
1106 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1107 int namelen, u64 *blkno)
1109 int ret;
1110 struct buffer_head *bh = NULL;
1111 struct ocfs2_dir_entry *dirent = NULL;
1113 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
1114 brelse(bh);
1116 return ret;
1119 /* Check for a name within a directory.
1121 * Return 0 if the name does not exist
1122 * Return -EEXIST if the directory contains the name
1124 * Callers should have i_mutex + a cluster lock on dir
1126 int ocfs2_check_dir_for_entry(struct inode *dir,
1127 const char *name,
1128 int namelen)
1130 int ret;
1131 struct buffer_head *dirent_bh = NULL;
1132 struct ocfs2_dir_entry *dirent = NULL;
1134 mlog_entry("dir %llu, name '%.*s'\n",
1135 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
1137 ret = -EEXIST;
1138 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1139 if (dirent_bh)
1140 goto bail;
1142 ret = 0;
1143 bail:
1144 brelse(dirent_bh);
1146 mlog_exit(ret);
1147 return ret;
1150 struct ocfs2_empty_dir_priv {
1151 unsigned seen_dot;
1152 unsigned seen_dot_dot;
1153 unsigned seen_other;
1155 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1156 loff_t pos, u64 ino, unsigned type)
1158 struct ocfs2_empty_dir_priv *p = priv;
1161 * Check the positions of "." and ".." records to be sure
1162 * they're in the correct place.
1164 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1165 p->seen_dot = 1;
1166 return 0;
1169 if (name_len == 2 && !strncmp("..", name, 2) &&
1170 pos == OCFS2_DIR_REC_LEN(1)) {
1171 p->seen_dot_dot = 1;
1172 return 0;
1175 p->seen_other = 1;
1176 return 1;
1179 * routine to check that the specified directory is empty (for rmdir)
1181 * Returns 1 if dir is empty, zero otherwise.
1183 int ocfs2_empty_dir(struct inode *inode)
1185 int ret;
1186 loff_t start = 0;
1187 struct ocfs2_empty_dir_priv priv;
1189 memset(&priv, 0, sizeof(priv));
1191 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1192 if (ret)
1193 mlog_errno(ret);
1195 if (!priv.seen_dot || !priv.seen_dot_dot) {
1196 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1197 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1199 * XXX: Is it really safe to allow an unlink to continue?
1201 return 1;
1204 return !priv.seen_other;
1208 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1209 * "..", which might be used during creation of a directory with a trailing
1210 * header. It is otherwise safe to ignore the return code.
1212 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1213 struct inode *parent,
1214 char *start,
1215 unsigned int size)
1217 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1219 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1220 de->name_len = 1;
1221 de->rec_len =
1222 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1223 strcpy(de->name, ".");
1224 ocfs2_set_de_type(de, S_IFDIR);
1226 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1227 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1228 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1229 de->name_len = 2;
1230 strcpy(de->name, "..");
1231 ocfs2_set_de_type(de, S_IFDIR);
1233 return de;
1237 * This works together with code in ocfs2_mknod_locked() which sets
1238 * the inline-data flag and initializes the inline-data section.
1240 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1241 handle_t *handle,
1242 struct inode *parent,
1243 struct inode *inode,
1244 struct buffer_head *di_bh)
1246 int ret;
1247 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1248 struct ocfs2_inline_data *data = &di->id2.i_data;
1249 unsigned int size = le16_to_cpu(data->id_count);
1251 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1252 OCFS2_JOURNAL_ACCESS_WRITE);
1253 if (ret) {
1254 mlog_errno(ret);
1255 goto out;
1258 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1260 ocfs2_journal_dirty(handle, di_bh);
1261 if (ret) {
1262 mlog_errno(ret);
1263 goto out;
1266 i_size_write(inode, size);
1267 inode->i_nlink = 2;
1268 inode->i_blocks = ocfs2_inode_sector_count(inode);
1270 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1271 if (ret < 0)
1272 mlog_errno(ret);
1274 out:
1275 return ret;
1278 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1279 handle_t *handle,
1280 struct inode *parent,
1281 struct inode *inode,
1282 struct buffer_head *fe_bh,
1283 struct ocfs2_alloc_context *data_ac)
1285 int status;
1286 unsigned int size = osb->sb->s_blocksize;
1287 struct buffer_head *new_bh = NULL;
1288 struct ocfs2_dir_entry *de;
1290 mlog_entry_void();
1292 if (ocfs2_supports_dir_trailer(osb))
1293 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1295 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1296 data_ac, NULL, &new_bh);
1297 if (status < 0) {
1298 mlog_errno(status);
1299 goto bail;
1302 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1304 status = ocfs2_journal_access_db(handle, inode, new_bh,
1305 OCFS2_JOURNAL_ACCESS_CREATE);
1306 if (status < 0) {
1307 mlog_errno(status);
1308 goto bail;
1310 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1312 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1313 if (ocfs2_supports_dir_trailer(osb))
1314 ocfs2_init_dir_trailer(inode, new_bh);
1316 status = ocfs2_journal_dirty(handle, new_bh);
1317 if (status < 0) {
1318 mlog_errno(status);
1319 goto bail;
1322 i_size_write(inode, inode->i_sb->s_blocksize);
1323 inode->i_nlink = 2;
1324 inode->i_blocks = ocfs2_inode_sector_count(inode);
1325 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1326 if (status < 0) {
1327 mlog_errno(status);
1328 goto bail;
1331 status = 0;
1332 bail:
1333 brelse(new_bh);
1335 mlog_exit(status);
1336 return status;
1339 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1340 handle_t *handle,
1341 struct inode *parent,
1342 struct inode *inode,
1343 struct buffer_head *fe_bh,
1344 struct ocfs2_alloc_context *data_ac)
1346 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1348 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1349 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1351 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1352 data_ac);
1356 * Expand rec_len of the rightmost dirent in a directory block so that it
1357 * contains the end of our valid space for dirents. We do this during
1358 * expansion from an inline directory to one with extents. The first dir block
1359 * in that case is taken from the inline data portion of the inode block.
1361 * We add the dir trailer if this filesystem wants it.
1363 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1364 struct super_block *sb)
1366 struct ocfs2_dir_entry *de;
1367 struct ocfs2_dir_entry *prev_de;
1368 char *de_buf, *limit;
1369 unsigned int new_size = sb->s_blocksize;
1370 unsigned int bytes;
1372 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1373 new_size = ocfs2_dir_trailer_blk_off(sb);
1375 bytes = new_size - old_size;
1377 limit = start + old_size;
1378 de_buf = start;
1379 de = (struct ocfs2_dir_entry *)de_buf;
1380 do {
1381 prev_de = de;
1382 de_buf += le16_to_cpu(de->rec_len);
1383 de = (struct ocfs2_dir_entry *)de_buf;
1384 } while (de_buf < limit);
1386 le16_add_cpu(&prev_de->rec_len, bytes);
1390 * We allocate enough clusters to fulfill "blocks_wanted", but set
1391 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1392 * rest automatically for us.
1394 * *first_block_bh is a pointer to the 1st data block allocated to the
1395 * directory.
1397 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1398 unsigned int blocks_wanted,
1399 struct buffer_head **first_block_bh)
1401 u32 alloc, bit_off, len;
1402 struct super_block *sb = dir->i_sb;
1403 int ret, credits = ocfs2_inline_to_extents_credits(sb);
1404 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1405 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1406 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1407 struct ocfs2_alloc_context *data_ac;
1408 struct buffer_head *dirdata_bh = NULL;
1409 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1410 handle_t *handle;
1411 struct ocfs2_extent_tree et;
1412 int did_quota = 0;
1414 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1416 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1419 * We should never need more than 2 clusters for this -
1420 * maximum dirent size is far less than one block. In fact,
1421 * the only time we'd need more than one cluster is if
1422 * blocksize == clustersize and the dirent won't fit in the
1423 * extra space that the expansion to a single block gives. As
1424 * of today, that only happens on 4k/4k file systems.
1426 BUG_ON(alloc > 2);
1428 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out;
1434 down_write(&oi->ip_alloc_sem);
1437 * Prepare for worst case allocation scenario of two separate
1438 * extents.
1440 if (alloc == 2)
1441 credits += OCFS2_SUBALLOC_ALLOC;
1443 handle = ocfs2_start_trans(osb, credits);
1444 if (IS_ERR(handle)) {
1445 ret = PTR_ERR(handle);
1446 mlog_errno(ret);
1447 goto out_sem;
1450 if (vfs_dq_alloc_space_nodirty(dir,
1451 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1452 ret = -EDQUOT;
1453 goto out_commit;
1455 did_quota = 1;
1457 * Try to claim as many clusters as the bitmap can give though
1458 * if we only get one now, that's enough to continue. The rest
1459 * will be claimed after the conversion to extents.
1461 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1462 if (ret) {
1463 mlog_errno(ret);
1464 goto out_commit;
1468 * Operations are carefully ordered so that we set up the new
1469 * data block first. The conversion from inline data to
1470 * extents follows.
1472 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1473 dirdata_bh = sb_getblk(sb, blkno);
1474 if (!dirdata_bh) {
1475 ret = -EIO;
1476 mlog_errno(ret);
1477 goto out_commit;
1480 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1482 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1483 OCFS2_JOURNAL_ACCESS_CREATE);
1484 if (ret) {
1485 mlog_errno(ret);
1486 goto out_commit;
1489 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1490 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1491 sb->s_blocksize - i_size_read(dir));
1492 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1493 if (ocfs2_supports_dir_trailer(osb))
1494 ocfs2_init_dir_trailer(dir, dirdata_bh);
1496 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1497 if (ret) {
1498 mlog_errno(ret);
1499 goto out_commit;
1503 * Set extent, i_size, etc on the directory. After this, the
1504 * inode should contain the same exact dirents as before and
1505 * be fully accessible from system calls.
1507 * We let the later dirent insert modify c/mtime - to the user
1508 * the data hasn't changed.
1510 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1511 OCFS2_JOURNAL_ACCESS_CREATE);
1512 if (ret) {
1513 mlog_errno(ret);
1514 goto out_commit;
1517 spin_lock(&oi->ip_lock);
1518 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1519 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1520 spin_unlock(&oi->ip_lock);
1522 ocfs2_dinode_new_extent_list(dir, di);
1524 i_size_write(dir, sb->s_blocksize);
1525 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1527 di->i_size = cpu_to_le64(sb->s_blocksize);
1528 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1529 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1532 * This should never fail as our extent list is empty and all
1533 * related blocks have been journaled already.
1535 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1536 0, NULL);
1537 if (ret) {
1538 mlog_errno(ret);
1539 goto out_commit;
1543 * Set i_blocks after the extent insert for the most up to
1544 * date ip_clusters value.
1546 dir->i_blocks = ocfs2_inode_sector_count(dir);
1548 ret = ocfs2_journal_dirty(handle, di_bh);
1549 if (ret) {
1550 mlog_errno(ret);
1551 goto out_commit;
1555 * We asked for two clusters, but only got one in the 1st
1556 * pass. Claim the 2nd cluster as a separate extent.
1558 if (alloc > len) {
1559 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1560 &len);
1561 if (ret) {
1562 mlog_errno(ret);
1563 goto out_commit;
1565 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1567 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1568 blkno, len, 0, NULL);
1569 if (ret) {
1570 mlog_errno(ret);
1571 goto out_commit;
1575 *first_block_bh = dirdata_bh;
1576 dirdata_bh = NULL;
1578 out_commit:
1579 if (ret < 0 && did_quota)
1580 vfs_dq_free_space_nodirty(dir,
1581 ocfs2_clusters_to_bytes(osb->sb, 2));
1582 ocfs2_commit_trans(osb, handle);
1584 out_sem:
1585 up_write(&oi->ip_alloc_sem);
1587 out:
1588 if (data_ac)
1589 ocfs2_free_alloc_context(data_ac);
1591 brelse(dirdata_bh);
1593 return ret;
1596 /* returns a bh of the 1st new block in the allocation. */
1597 static int ocfs2_do_extend_dir(struct super_block *sb,
1598 handle_t *handle,
1599 struct inode *dir,
1600 struct buffer_head *parent_fe_bh,
1601 struct ocfs2_alloc_context *data_ac,
1602 struct ocfs2_alloc_context *meta_ac,
1603 struct buffer_head **new_bh)
1605 int status;
1606 int extend, did_quota = 0;
1607 u64 p_blkno, v_blkno;
1609 spin_lock(&OCFS2_I(dir)->ip_lock);
1610 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1611 spin_unlock(&OCFS2_I(dir)->ip_lock);
1613 if (extend) {
1614 u32 offset = OCFS2_I(dir)->ip_clusters;
1616 if (vfs_dq_alloc_space_nodirty(dir,
1617 ocfs2_clusters_to_bytes(sb, 1))) {
1618 status = -EDQUOT;
1619 goto bail;
1621 did_quota = 1;
1623 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1624 1, 0, parent_fe_bh, handle,
1625 data_ac, meta_ac, NULL);
1626 BUG_ON(status == -EAGAIN);
1627 if (status < 0) {
1628 mlog_errno(status);
1629 goto bail;
1633 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1634 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1635 if (status < 0) {
1636 mlog_errno(status);
1637 goto bail;
1640 *new_bh = sb_getblk(sb, p_blkno);
1641 if (!*new_bh) {
1642 status = -EIO;
1643 mlog_errno(status);
1644 goto bail;
1646 status = 0;
1647 bail:
1648 if (did_quota && status < 0)
1649 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
1650 mlog_exit(status);
1651 return status;
1655 * Assumes you already have a cluster lock on the directory.
1657 * 'blocks_wanted' is only used if we have an inline directory which
1658 * is to be turned into an extent based one. The size of the dirent to
1659 * insert might be larger than the space gained by growing to just one
1660 * block, so we may have to grow the inode by two blocks in that case.
1662 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1663 struct inode *dir,
1664 struct buffer_head *parent_fe_bh,
1665 unsigned int blocks_wanted,
1666 struct buffer_head **new_de_bh)
1668 int status = 0;
1669 int credits, num_free_extents, drop_alloc_sem = 0;
1670 loff_t dir_i_size;
1671 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1672 struct ocfs2_extent_list *el = &fe->id2.i_list;
1673 struct ocfs2_alloc_context *data_ac = NULL;
1674 struct ocfs2_alloc_context *meta_ac = NULL;
1675 handle_t *handle = NULL;
1676 struct buffer_head *new_bh = NULL;
1677 struct ocfs2_dir_entry * de;
1678 struct super_block *sb = osb->sb;
1679 struct ocfs2_extent_tree et;
1681 mlog_entry_void();
1683 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1684 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1685 blocks_wanted, &new_bh);
1686 if (status) {
1687 mlog_errno(status);
1688 goto bail;
1691 if (blocks_wanted == 1) {
1693 * If the new dirent will fit inside the space
1694 * created by pushing out to one block, then
1695 * we can complete the operation
1696 * here. Otherwise we have to expand i_size
1697 * and format the 2nd block below.
1699 BUG_ON(new_bh == NULL);
1700 goto bail_bh;
1704 * Get rid of 'new_bh' - we want to format the 2nd
1705 * data block and return that instead.
1707 brelse(new_bh);
1708 new_bh = NULL;
1710 dir_i_size = i_size_read(dir);
1711 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1712 goto do_extend;
1715 dir_i_size = i_size_read(dir);
1716 mlog(0, "extending dir %llu (i_size = %lld)\n",
1717 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1719 /* dir->i_size is always block aligned. */
1720 spin_lock(&OCFS2_I(dir)->ip_lock);
1721 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1722 spin_unlock(&OCFS2_I(dir)->ip_lock);
1723 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1724 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1725 if (num_free_extents < 0) {
1726 status = num_free_extents;
1727 mlog_errno(status);
1728 goto bail;
1731 if (!num_free_extents) {
1732 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1733 if (status < 0) {
1734 if (status != -ENOSPC)
1735 mlog_errno(status);
1736 goto bail;
1740 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1741 if (status < 0) {
1742 if (status != -ENOSPC)
1743 mlog_errno(status);
1744 goto bail;
1747 credits = ocfs2_calc_extend_credits(sb, el, 1);
1748 } else {
1749 spin_unlock(&OCFS2_I(dir)->ip_lock);
1750 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1753 do_extend:
1754 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1755 drop_alloc_sem = 1;
1757 handle = ocfs2_start_trans(osb, credits);
1758 if (IS_ERR(handle)) {
1759 status = PTR_ERR(handle);
1760 handle = NULL;
1761 mlog_errno(status);
1762 goto bail;
1765 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1766 data_ac, meta_ac, &new_bh);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto bail;
1772 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1774 status = ocfs2_journal_access_db(handle, dir, new_bh,
1775 OCFS2_JOURNAL_ACCESS_CREATE);
1776 if (status < 0) {
1777 mlog_errno(status);
1778 goto bail;
1780 memset(new_bh->b_data, 0, sb->s_blocksize);
1782 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1783 de->inode = 0;
1784 if (ocfs2_dir_has_trailer(dir)) {
1785 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1786 ocfs2_init_dir_trailer(dir, new_bh);
1787 } else {
1788 de->rec_len = cpu_to_le16(sb->s_blocksize);
1790 status = ocfs2_journal_dirty(handle, new_bh);
1791 if (status < 0) {
1792 mlog_errno(status);
1793 goto bail;
1796 dir_i_size += dir->i_sb->s_blocksize;
1797 i_size_write(dir, dir_i_size);
1798 dir->i_blocks = ocfs2_inode_sector_count(dir);
1799 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1800 if (status < 0) {
1801 mlog_errno(status);
1802 goto bail;
1805 bail_bh:
1806 *new_de_bh = new_bh;
1807 get_bh(*new_de_bh);
1808 bail:
1809 if (drop_alloc_sem)
1810 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1811 if (handle)
1812 ocfs2_commit_trans(osb, handle);
1814 if (data_ac)
1815 ocfs2_free_alloc_context(data_ac);
1816 if (meta_ac)
1817 ocfs2_free_alloc_context(meta_ac);
1819 brelse(new_bh);
1821 mlog_exit(status);
1822 return status;
1825 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1826 const char *name, int namelen,
1827 struct buffer_head **ret_de_bh,
1828 unsigned int *blocks_wanted)
1830 int ret;
1831 struct super_block *sb = dir->i_sb;
1832 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1833 struct ocfs2_dir_entry *de, *last_de = NULL;
1834 char *de_buf, *limit;
1835 unsigned long offset = 0;
1836 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1839 * This calculates how many free bytes we'd have in block zero, should
1840 * this function force expansion to an extent tree.
1842 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1843 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1844 else
1845 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
1847 de_buf = di->id2.i_data.id_data;
1848 limit = de_buf + i_size_read(dir);
1849 rec_len = OCFS2_DIR_REC_LEN(namelen);
1851 while (de_buf < limit) {
1852 de = (struct ocfs2_dir_entry *)de_buf;
1854 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1855 ret = -ENOENT;
1856 goto out;
1858 if (ocfs2_match(namelen, name, de)) {
1859 ret = -EEXIST;
1860 goto out;
1863 * No need to check for a trailing dirent record here as
1864 * they're not used for inline dirs.
1867 if (ocfs2_dirent_would_fit(de, rec_len)) {
1868 /* Ok, we found a spot. Return this bh and let
1869 * the caller actually fill it in. */
1870 *ret_de_bh = di_bh;
1871 get_bh(*ret_de_bh);
1872 ret = 0;
1873 goto out;
1876 last_de = de;
1877 de_buf += le16_to_cpu(de->rec_len);
1878 offset += le16_to_cpu(de->rec_len);
1882 * We're going to require expansion of the directory - figure
1883 * out how many blocks we'll need so that a place for the
1884 * dirent can be found.
1886 *blocks_wanted = 1;
1887 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
1888 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1889 *blocks_wanted = 2;
1891 ret = -ENOSPC;
1892 out:
1893 return ret;
1896 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1897 int namelen, struct buffer_head **ret_de_bh)
1899 unsigned long offset;
1900 struct buffer_head *bh = NULL;
1901 unsigned short rec_len;
1902 struct ocfs2_dir_entry *de;
1903 struct super_block *sb = dir->i_sb;
1904 int status;
1905 int blocksize = dir->i_sb->s_blocksize;
1907 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1908 if (status) {
1909 mlog_errno(status);
1910 goto bail;
1913 rec_len = OCFS2_DIR_REC_LEN(namelen);
1914 offset = 0;
1915 de = (struct ocfs2_dir_entry *) bh->b_data;
1916 while (1) {
1917 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1918 brelse(bh);
1919 bh = NULL;
1921 if (i_size_read(dir) <= offset) {
1923 * Caller will have to expand this
1924 * directory.
1926 status = -ENOSPC;
1927 goto bail;
1929 status = ocfs2_read_dir_block(dir,
1930 offset >> sb->s_blocksize_bits,
1931 &bh, 0);
1932 if (status) {
1933 mlog_errno(status);
1934 goto bail;
1936 /* move to next block */
1937 de = (struct ocfs2_dir_entry *) bh->b_data;
1939 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1940 status = -ENOENT;
1941 goto bail;
1943 if (ocfs2_match(namelen, name, de)) {
1944 status = -EEXIST;
1945 goto bail;
1948 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1949 blocksize))
1950 goto next;
1952 if (ocfs2_dirent_would_fit(de, rec_len)) {
1953 /* Ok, we found a spot. Return this bh and let
1954 * the caller actually fill it in. */
1955 *ret_de_bh = bh;
1956 get_bh(*ret_de_bh);
1957 status = 0;
1958 goto bail;
1960 next:
1961 offset += le16_to_cpu(de->rec_len);
1962 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1965 status = 0;
1966 bail:
1967 brelse(bh);
1969 mlog_exit(status);
1970 return status;
1973 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1974 struct inode *dir,
1975 struct buffer_head *parent_fe_bh,
1976 const char *name,
1977 int namelen,
1978 struct buffer_head **ret_de_bh)
1980 int ret;
1981 unsigned int blocks_wanted = 1;
1982 struct buffer_head *bh = NULL;
1984 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1985 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1987 *ret_de_bh = NULL;
1989 if (!namelen) {
1990 ret = -EINVAL;
1991 mlog_errno(ret);
1992 goto out;
1995 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1996 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1997 namelen, &bh, &blocks_wanted);
1998 } else
1999 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
2001 if (ret && ret != -ENOSPC) {
2002 mlog_errno(ret);
2003 goto out;
2006 if (ret == -ENOSPC) {
2008 * We have to expand the directory to add this name.
2010 BUG_ON(bh);
2012 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
2013 &bh);
2014 if (ret) {
2015 if (ret != -ENOSPC)
2016 mlog_errno(ret);
2017 goto out;
2020 BUG_ON(!bh);
2023 *ret_de_bh = bh;
2024 bh = NULL;
2025 out:
2026 brelse(bh);
2027 return ret;