Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / ocfs2 / dir.c
blob96d0d14ca5788e6955c95de7f75cd4fd5a175eb1
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>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
47 #include "ocfs2.h"
49 #include "alloc.h"
50 #include "dir.h"
51 #include "dlmglue.h"
52 #include "extent_map.h"
53 #include "file.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "namei.h"
57 #include "suballoc.h"
58 #include "super.h"
59 #include "uptodate.h"
61 #include "buffer_head_io.h"
63 #define NAMEI_RA_CHUNKS 2
64 #define NAMEI_RA_BLOCKS 4
65 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
66 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
68 static unsigned char ocfs2_filetype_table[] = {
69 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
72 static int ocfs2_extend_dir(struct ocfs2_super *osb,
73 struct inode *dir,
74 struct buffer_head *parent_fe_bh,
75 unsigned int blocks_wanted,
76 struct buffer_head **new_de_bh);
77 static int ocfs2_do_extend_dir(struct super_block *sb,
78 handle_t *handle,
79 struct inode *dir,
80 struct buffer_head *parent_fe_bh,
81 struct ocfs2_alloc_context *data_ac,
82 struct ocfs2_alloc_context *meta_ac,
83 struct buffer_head **new_bh);
86 * bh passed here can be an inode block or a dir data block, depending
87 * on the inode inline data flag.
89 static int ocfs2_check_dir_entry(struct inode * dir,
90 struct ocfs2_dir_entry * de,
91 struct buffer_head * bh,
92 unsigned long offset)
94 const char *error_msg = NULL;
95 const int rlen = le16_to_cpu(de->rec_len);
97 if (rlen < OCFS2_DIR_REC_LEN(1))
98 error_msg = "rec_len is smaller than minimal";
99 else if (rlen % 4 != 0)
100 error_msg = "rec_len % 4 != 0";
101 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
102 error_msg = "rec_len is too small for name_len";
103 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
104 error_msg = "directory entry across blocks";
106 if (error_msg != NULL)
107 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
108 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
109 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
110 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
111 de->name_len);
112 return error_msg == NULL ? 1 : 0;
115 static inline int ocfs2_match(int len,
116 const char * const name,
117 struct ocfs2_dir_entry *de)
119 if (len != de->name_len)
120 return 0;
121 if (!de->inode)
122 return 0;
123 return !memcmp(name, de->name, len);
127 * Returns 0 if not found, -1 on failure, and 1 on success
129 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
130 struct inode *dir,
131 const char *name, int namelen,
132 unsigned long offset,
133 char *first_de,
134 unsigned int bytes,
135 struct ocfs2_dir_entry **res_dir)
137 struct ocfs2_dir_entry *de;
138 char *dlimit, *de_buf;
139 int de_len;
140 int ret = 0;
142 mlog_entry_void();
144 de_buf = first_de;
145 dlimit = de_buf + bytes;
147 while (de_buf < dlimit) {
148 /* this code is executed quadratically often */
149 /* do minimal checking `by hand' */
151 de = (struct ocfs2_dir_entry *) de_buf;
153 if (de_buf + namelen <= dlimit &&
154 ocfs2_match(namelen, name, de)) {
155 /* found a match - just to be sure, do a full check */
156 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
157 ret = -1;
158 goto bail;
160 *res_dir = de;
161 ret = 1;
162 goto bail;
165 /* prevent looping on a bad block */
166 de_len = le16_to_cpu(de->rec_len);
167 if (de_len <= 0) {
168 ret = -1;
169 goto bail;
172 de_buf += de_len;
173 offset += de_len;
176 bail:
177 mlog_exit(ret);
178 return ret;
181 static struct buffer_head *ocfs2_find_entry_id(const char *name,
182 int namelen,
183 struct inode *dir,
184 struct ocfs2_dir_entry **res_dir)
186 int ret, found;
187 struct buffer_head *di_bh = NULL;
188 struct ocfs2_dinode *di;
189 struct ocfs2_inline_data *data;
191 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
192 &di_bh, OCFS2_BH_CACHED, dir);
193 if (ret) {
194 mlog_errno(ret);
195 goto out;
198 di = (struct ocfs2_dinode *)di_bh->b_data;
199 data = &di->id2.i_data;
201 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
202 data->id_data, i_size_read(dir), res_dir);
203 if (found == 1)
204 return di_bh;
206 brelse(di_bh);
207 out:
208 return NULL;
211 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
212 struct inode *dir,
213 struct ocfs2_dir_entry **res_dir)
215 struct super_block *sb;
216 struct buffer_head *bh_use[NAMEI_RA_SIZE];
217 struct buffer_head *bh, *ret = NULL;
218 unsigned long start, block, b;
219 int ra_max = 0; /* Number of bh's in the readahead
220 buffer, bh_use[] */
221 int ra_ptr = 0; /* Current index into readahead
222 buffer */
223 int num = 0;
224 int nblocks, i, err;
226 mlog_entry_void();
228 sb = dir->i_sb;
230 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
231 start = OCFS2_I(dir)->ip_dir_start_lookup;
232 if (start >= nblocks)
233 start = 0;
234 block = start;
236 restart:
237 do {
239 * We deal with the read-ahead logic here.
241 if (ra_ptr >= ra_max) {
242 /* Refill the readahead buffer */
243 ra_ptr = 0;
244 b = block;
245 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
247 * Terminate if we reach the end of the
248 * directory and must wrap, or if our
249 * search has finished at this block.
251 if (b >= nblocks || (num && block == start)) {
252 bh_use[ra_max] = NULL;
253 break;
255 num++;
257 bh = ocfs2_bread(dir, b++, &err, 1);
258 bh_use[ra_max] = bh;
261 if ((bh = bh_use[ra_ptr++]) == NULL)
262 goto next;
263 wait_on_buffer(bh);
264 if (!buffer_uptodate(bh)) {
265 /* read error, skip block & hope for the best */
266 ocfs2_error(dir->i_sb, "reading directory %llu, "
267 "offset %lu\n",
268 (unsigned long long)OCFS2_I(dir)->ip_blkno,
269 block);
270 brelse(bh);
271 goto next;
273 i = ocfs2_search_dirblock(bh, dir, name, namelen,
274 block << sb->s_blocksize_bits,
275 bh->b_data, sb->s_blocksize,
276 res_dir);
277 if (i == 1) {
278 OCFS2_I(dir)->ip_dir_start_lookup = block;
279 ret = bh;
280 goto cleanup_and_exit;
281 } else {
282 brelse(bh);
283 if (i < 0)
284 goto cleanup_and_exit;
286 next:
287 if (++block >= nblocks)
288 block = 0;
289 } while (block != start);
292 * If the directory has grown while we were searching, then
293 * search the last part of the directory before giving up.
295 block = nblocks;
296 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
297 if (block < nblocks) {
298 start = 0;
299 goto restart;
302 cleanup_and_exit:
303 /* Clean up the read-ahead blocks */
304 for (; ra_ptr < ra_max; ra_ptr++)
305 brelse(bh_use[ra_ptr]);
307 mlog_exit_ptr(ret);
308 return ret;
312 * Try to find an entry of the provided name within 'dir'.
314 * If nothing was found, NULL is returned. Otherwise, a buffer_head
315 * and pointer to the dir entry are passed back.
317 * Caller can NOT assume anything about the contents of the
318 * buffer_head - it is passed back only so that it can be passed into
319 * any one of the manipulation functions (add entry, delete entry,
320 * etc). As an example, bh in the extent directory case is a data
321 * block, in the inline-data case it actually points to an inode.
323 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
324 struct inode *dir,
325 struct ocfs2_dir_entry **res_dir)
327 *res_dir = NULL;
329 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
330 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
332 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
336 * Update inode number and type of a previously found directory entry.
338 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
339 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
340 struct inode *new_entry_inode)
342 int ret;
345 * The same code works fine for both inline-data and extent
346 * based directories, so no need to split this up.
349 ret = ocfs2_journal_access(handle, dir, de_bh,
350 OCFS2_JOURNAL_ACCESS_WRITE);
351 if (ret) {
352 mlog_errno(ret);
353 goto out;
356 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
357 ocfs2_set_de_type(de, new_entry_inode->i_mode);
359 ocfs2_journal_dirty(handle, de_bh);
361 out:
362 return ret;
365 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
366 struct ocfs2_dir_entry *de_del,
367 struct buffer_head *bh, char *first_de,
368 unsigned int bytes)
370 struct ocfs2_dir_entry *de, *pde;
371 int i, status = -ENOENT;
373 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
375 i = 0;
376 pde = NULL;
377 de = (struct ocfs2_dir_entry *) first_de;
378 while (i < bytes) {
379 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
380 status = -EIO;
381 mlog_errno(status);
382 goto bail;
384 if (de == de_del) {
385 status = ocfs2_journal_access(handle, dir, bh,
386 OCFS2_JOURNAL_ACCESS_WRITE);
387 if (status < 0) {
388 status = -EIO;
389 mlog_errno(status);
390 goto bail;
392 if (pde)
393 <<<<<<< HEAD:fs/ocfs2/dir.c
394 pde->rec_len =
395 cpu_to_le16(le16_to_cpu(pde->rec_len) +
396 le16_to_cpu(de->rec_len));
397 =======
398 le16_add_cpu(&pde->rec_len,
399 le16_to_cpu(de->rec_len));
400 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dir.c
401 else
402 de->inode = 0;
403 dir->i_version++;
404 status = ocfs2_journal_dirty(handle, bh);
405 goto bail;
407 i += le16_to_cpu(de->rec_len);
408 pde = de;
409 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
411 bail:
412 mlog_exit(status);
413 return status;
416 static inline int ocfs2_delete_entry_id(handle_t *handle,
417 struct inode *dir,
418 struct ocfs2_dir_entry *de_del,
419 struct buffer_head *bh)
421 int ret;
422 struct buffer_head *di_bh = NULL;
423 struct ocfs2_dinode *di;
424 struct ocfs2_inline_data *data;
426 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
427 &di_bh, OCFS2_BH_CACHED, dir);
428 if (ret) {
429 mlog_errno(ret);
430 goto out;
433 di = (struct ocfs2_dinode *)di_bh->b_data;
434 data = &di->id2.i_data;
436 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
437 i_size_read(dir));
439 brelse(di_bh);
440 out:
441 return ret;
444 static inline int ocfs2_delete_entry_el(handle_t *handle,
445 struct inode *dir,
446 struct ocfs2_dir_entry *de_del,
447 struct buffer_head *bh)
449 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
450 bh->b_size);
454 * ocfs2_delete_entry deletes a directory entry by merging it with the
455 * previous entry
457 int ocfs2_delete_entry(handle_t *handle,
458 struct inode *dir,
459 struct ocfs2_dir_entry *de_del,
460 struct buffer_head *bh)
462 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
463 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
465 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
469 * Check whether 'de' has enough room to hold an entry of
470 * 'new_rec_len' bytes.
472 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
473 unsigned int new_rec_len)
475 unsigned int de_really_used;
477 /* Check whether this is an empty record with enough space */
478 if (le64_to_cpu(de->inode) == 0 &&
479 le16_to_cpu(de->rec_len) >= new_rec_len)
480 return 1;
483 * Record might have free space at the end which we can
484 * use.
486 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
487 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
488 return 1;
490 return 0;
493 /* we don't always have a dentry for what we want to add, so people
494 * like orphan dir can call this instead.
496 * If you pass me insert_bh, I'll skip the search of the other dir
497 * blocks and put the record in there.
499 int __ocfs2_add_entry(handle_t *handle,
500 struct inode *dir,
501 const char *name, int namelen,
502 struct inode *inode, u64 blkno,
503 struct buffer_head *parent_fe_bh,
504 struct buffer_head *insert_bh)
506 unsigned long offset;
507 unsigned short rec_len;
508 struct ocfs2_dir_entry *de, *de1;
509 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
510 struct super_block *sb = dir->i_sb;
511 int retval, status;
512 unsigned int size = sb->s_blocksize;
513 char *data_start = insert_bh->b_data;
515 mlog_entry_void();
517 if (!namelen)
518 return -EINVAL;
520 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
521 data_start = di->id2.i_data.id_data;
522 size = i_size_read(dir);
524 BUG_ON(insert_bh != parent_fe_bh);
527 rec_len = OCFS2_DIR_REC_LEN(namelen);
528 offset = 0;
529 de = (struct ocfs2_dir_entry *) data_start;
530 while (1) {
531 BUG_ON((char *)de >= (size + data_start));
533 /* These checks should've already been passed by the
534 * prepare function, but I guess we can leave them
535 * here anyway. */
536 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
537 retval = -ENOENT;
538 goto bail;
540 if (ocfs2_match(namelen, name, de)) {
541 retval = -EEXIST;
542 goto bail;
545 if (ocfs2_dirent_would_fit(de, rec_len)) {
546 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
547 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
548 if (retval < 0) {
549 mlog_errno(retval);
550 goto bail;
553 status = ocfs2_journal_access(handle, dir, insert_bh,
554 OCFS2_JOURNAL_ACCESS_WRITE);
555 /* By now the buffer is marked for journaling */
556 offset += le16_to_cpu(de->rec_len);
557 if (le64_to_cpu(de->inode)) {
558 de1 = (struct ocfs2_dir_entry *)((char *) de +
559 OCFS2_DIR_REC_LEN(de->name_len));
560 de1->rec_len =
561 cpu_to_le16(le16_to_cpu(de->rec_len) -
562 OCFS2_DIR_REC_LEN(de->name_len));
563 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
564 de = de1;
566 de->file_type = OCFS2_FT_UNKNOWN;
567 if (blkno) {
568 de->inode = cpu_to_le64(blkno);
569 ocfs2_set_de_type(de, inode->i_mode);
570 } else
571 de->inode = 0;
572 de->name_len = namelen;
573 memcpy(de->name, name, namelen);
575 dir->i_version++;
576 status = ocfs2_journal_dirty(handle, insert_bh);
577 retval = 0;
578 goto bail;
580 offset += le16_to_cpu(de->rec_len);
581 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
584 /* when you think about it, the assert above should prevent us
585 * from ever getting here. */
586 retval = -ENOSPC;
587 bail:
589 mlog_exit(retval);
590 return retval;
593 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
594 u64 *f_version,
595 loff_t *f_pos, void *priv,
596 filldir_t filldir, int *filldir_err)
598 int ret, i, filldir_ret;
599 unsigned long offset = *f_pos;
600 struct buffer_head *di_bh = NULL;
601 struct ocfs2_dinode *di;
602 struct ocfs2_inline_data *data;
603 struct ocfs2_dir_entry *de;
605 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
606 &di_bh, OCFS2_BH_CACHED, inode);
607 if (ret) {
608 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
609 (unsigned long long)OCFS2_I(inode)->ip_blkno);
610 goto out;
613 di = (struct ocfs2_dinode *)di_bh->b_data;
614 data = &di->id2.i_data;
616 while (*f_pos < i_size_read(inode)) {
617 revalidate:
618 /* If the dir block has changed since the last call to
619 * readdir(2), then we might be pointing to an invalid
620 * dirent right now. Scan from the start of the block
621 * to make sure. */
622 if (*f_version != inode->i_version) {
623 for (i = 0; i < i_size_read(inode) && i < offset; ) {
624 de = (struct ocfs2_dir_entry *)
625 (data->id_data + i);
626 /* It's too expensive to do a full
627 * dirent test each time round this
628 * loop, but we do have to test at
629 * least that it is non-zero. A
630 * failure will be detected in the
631 * dirent test below. */
632 if (le16_to_cpu(de->rec_len) <
633 OCFS2_DIR_REC_LEN(1))
634 break;
635 i += le16_to_cpu(de->rec_len);
637 *f_pos = offset = i;
638 *f_version = inode->i_version;
641 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
642 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
643 /* On error, skip the f_pos to the end. */
644 *f_pos = i_size_read(inode);
645 goto out;
647 offset += le16_to_cpu(de->rec_len);
648 if (le64_to_cpu(de->inode)) {
649 /* We might block in the next section
650 * if the data destination is
651 * currently swapped out. So, use a
652 * version stamp to detect whether or
653 * not the directory has been modified
654 * during the copy operation.
656 u64 version = *f_version;
657 unsigned char d_type = DT_UNKNOWN;
659 if (de->file_type < OCFS2_FT_MAX)
660 d_type = ocfs2_filetype_table[de->file_type];
662 filldir_ret = filldir(priv, de->name,
663 de->name_len,
664 *f_pos,
665 le64_to_cpu(de->inode),
666 d_type);
667 if (filldir_ret) {
668 if (filldir_err)
669 *filldir_err = filldir_ret;
670 break;
672 if (version != *f_version)
673 goto revalidate;
675 *f_pos += le16_to_cpu(de->rec_len);
678 out:
679 brelse(di_bh);
681 return 0;
684 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
685 u64 *f_version,
686 loff_t *f_pos, void *priv,
687 filldir_t filldir, int *filldir_err)
689 int error = 0;
690 unsigned long offset, blk, last_ra_blk = 0;
691 int i, stored;
692 struct buffer_head * bh, * tmp;
693 struct ocfs2_dir_entry * de;
694 int err;
695 struct super_block * sb = inode->i_sb;
696 unsigned int ra_sectors = 16;
698 stored = 0;
699 bh = NULL;
701 offset = (*f_pos) & (sb->s_blocksize - 1);
703 while (!error && !stored && *f_pos < i_size_read(inode)) {
704 blk = (*f_pos) >> sb->s_blocksize_bits;
705 bh = ocfs2_bread(inode, blk, &err, 0);
706 if (!bh) {
707 mlog(ML_ERROR,
708 "directory #%llu contains a hole at offset %lld\n",
709 (unsigned long long)OCFS2_I(inode)->ip_blkno,
710 *f_pos);
711 *f_pos += sb->s_blocksize - offset;
712 continue;
715 /* The idea here is to begin with 8k read-ahead and to stay
716 * 4k ahead of our current position.
718 * TODO: Use the pagecache for this. We just need to
719 * make sure it's cluster-safe... */
720 if (!last_ra_blk
721 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
722 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
723 i > 0; i--) {
724 tmp = ocfs2_bread(inode, ++blk, &err, 1);
725 if (tmp)
726 brelse(tmp);
728 last_ra_blk = blk;
729 ra_sectors = 8;
732 revalidate:
733 /* If the dir block has changed since the last call to
734 * readdir(2), then we might be pointing to an invalid
735 * dirent right now. Scan from the start of the block
736 * to make sure. */
737 if (*f_version != inode->i_version) {
738 for (i = 0; i < sb->s_blocksize && i < offset; ) {
739 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
740 /* It's too expensive to do a full
741 * dirent test each time round this
742 * loop, but we do have to test at
743 * least that it is non-zero. A
744 * failure will be detected in the
745 * dirent test below. */
746 if (le16_to_cpu(de->rec_len) <
747 OCFS2_DIR_REC_LEN(1))
748 break;
749 i += le16_to_cpu(de->rec_len);
751 offset = i;
752 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
753 | offset;
754 *f_version = inode->i_version;
757 while (!error && *f_pos < i_size_read(inode)
758 && offset < sb->s_blocksize) {
759 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
760 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
761 /* On error, skip the f_pos to the
762 next block. */
763 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
764 brelse(bh);
765 goto out;
767 offset += le16_to_cpu(de->rec_len);
768 if (le64_to_cpu(de->inode)) {
769 /* We might block in the next section
770 * if the data destination is
771 * currently swapped out. So, use a
772 * version stamp to detect whether or
773 * not the directory has been modified
774 * during the copy operation.
776 unsigned long version = *f_version;
777 unsigned char d_type = DT_UNKNOWN;
779 if (de->file_type < OCFS2_FT_MAX)
780 d_type = ocfs2_filetype_table[de->file_type];
781 error = filldir(priv, de->name,
782 de->name_len,
783 *f_pos,
784 le64_to_cpu(de->inode),
785 d_type);
786 if (error) {
787 if (filldir_err)
788 *filldir_err = error;
789 break;
791 if (version != *f_version)
792 goto revalidate;
793 stored ++;
795 *f_pos += le16_to_cpu(de->rec_len);
797 offset = 0;
798 brelse(bh);
801 stored = 0;
802 out:
803 return stored;
806 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
807 loff_t *f_pos, void *priv, filldir_t filldir,
808 int *filldir_err)
810 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
811 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
812 filldir, filldir_err);
814 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
815 filldir_err);
819 * This is intended to be called from inside other kernel functions,
820 * so we fake some arguments.
822 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
823 filldir_t filldir)
825 int ret = 0, filldir_err = 0;
826 u64 version = inode->i_version;
828 while (*f_pos < i_size_read(inode)) {
829 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
830 filldir, &filldir_err);
831 if (ret || filldir_err)
832 break;
835 if (ret > 0)
836 ret = -EIO;
838 return 0;
842 * ocfs2_readdir()
845 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
847 int error = 0;
848 struct inode *inode = filp->f_path.dentry->d_inode;
849 int lock_level = 0;
851 mlog_entry("dirino=%llu\n",
852 (unsigned long long)OCFS2_I(inode)->ip_blkno);
854 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
855 if (lock_level && error >= 0) {
856 /* We release EX lock which used to update atime
857 * and get PR lock again to reduce contention
858 * on commonly accessed directories. */
859 ocfs2_inode_unlock(inode, 1);
860 lock_level = 0;
861 error = ocfs2_inode_lock(inode, NULL, 0);
863 if (error < 0) {
864 if (error != -ENOENT)
865 mlog_errno(error);
866 /* we haven't got any yet, so propagate the error. */
867 goto bail_nolock;
870 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
871 dirent, filldir, NULL);
873 ocfs2_inode_unlock(inode, lock_level);
875 bail_nolock:
876 mlog_exit(error);
878 return error;
882 * NOTE: this should always be called with parent dir i_mutex taken.
884 int ocfs2_find_files_on_disk(const char *name,
885 int namelen,
886 u64 *blkno,
887 struct inode *inode,
888 struct buffer_head **dirent_bh,
889 struct ocfs2_dir_entry **dirent)
891 int status = -ENOENT;
893 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
894 namelen, name, blkno, inode, dirent_bh, dirent);
896 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
897 if (!*dirent_bh || !*dirent) {
898 status = -ENOENT;
899 goto leave;
902 *blkno = le64_to_cpu((*dirent)->inode);
904 status = 0;
905 leave:
906 if (status < 0) {
907 *dirent = NULL;
908 if (*dirent_bh) {
909 brelse(*dirent_bh);
910 *dirent_bh = NULL;
914 mlog_exit(status);
915 return status;
919 * Convenience function for callers which just want the block number
920 * mapped to a name and don't require the full dirent info, etc.
922 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
923 int namelen, u64 *blkno)
925 int ret;
926 struct buffer_head *bh = NULL;
927 struct ocfs2_dir_entry *dirent = NULL;
929 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
930 brelse(bh);
932 return ret;
935 /* Check for a name within a directory.
937 * Return 0 if the name does not exist
938 * Return -EEXIST if the directory contains the name
940 * Callers should have i_mutex + a cluster lock on dir
942 int ocfs2_check_dir_for_entry(struct inode *dir,
943 const char *name,
944 int namelen)
946 int ret;
947 struct buffer_head *dirent_bh = NULL;
948 struct ocfs2_dir_entry *dirent = NULL;
950 mlog_entry("dir %llu, name '%.*s'\n",
951 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
953 ret = -EEXIST;
954 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
955 if (dirent_bh)
956 goto bail;
958 ret = 0;
959 bail:
960 if (dirent_bh)
961 brelse(dirent_bh);
963 mlog_exit(ret);
964 return ret;
967 struct ocfs2_empty_dir_priv {
968 unsigned seen_dot;
969 unsigned seen_dot_dot;
970 unsigned seen_other;
972 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
973 loff_t pos, u64 ino, unsigned type)
975 struct ocfs2_empty_dir_priv *p = priv;
978 * Check the positions of "." and ".." records to be sure
979 * they're in the correct place.
981 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
982 p->seen_dot = 1;
983 return 0;
986 if (name_len == 2 && !strncmp("..", name, 2) &&
987 pos == OCFS2_DIR_REC_LEN(1)) {
988 p->seen_dot_dot = 1;
989 return 0;
992 p->seen_other = 1;
993 return 1;
996 * routine to check that the specified directory is empty (for rmdir)
998 * Returns 1 if dir is empty, zero otherwise.
1000 int ocfs2_empty_dir(struct inode *inode)
1002 int ret;
1003 loff_t start = 0;
1004 struct ocfs2_empty_dir_priv priv;
1006 memset(&priv, 0, sizeof(priv));
1008 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1009 if (ret)
1010 mlog_errno(ret);
1012 if (!priv.seen_dot || !priv.seen_dot_dot) {
1013 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1014 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1016 * XXX: Is it really safe to allow an unlink to continue?
1018 return 1;
1021 return !priv.seen_other;
1024 static void ocfs2_fill_initial_dirents(struct inode *inode,
1025 struct inode *parent,
1026 char *start, unsigned int size)
1028 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1030 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1031 de->name_len = 1;
1032 de->rec_len =
1033 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1034 strcpy(de->name, ".");
1035 ocfs2_set_de_type(de, S_IFDIR);
1037 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1038 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1039 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1040 de->name_len = 2;
1041 strcpy(de->name, "..");
1042 ocfs2_set_de_type(de, S_IFDIR);
1046 * This works together with code in ocfs2_mknod_locked() which sets
1047 * the inline-data flag and initializes the inline-data section.
1049 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1050 handle_t *handle,
1051 struct inode *parent,
1052 struct inode *inode,
1053 struct buffer_head *di_bh)
1055 int ret;
1056 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1057 struct ocfs2_inline_data *data = &di->id2.i_data;
1058 unsigned int size = le16_to_cpu(data->id_count);
1060 ret = ocfs2_journal_access(handle, inode, di_bh,
1061 OCFS2_JOURNAL_ACCESS_WRITE);
1062 if (ret) {
1063 mlog_errno(ret);
1064 goto out;
1067 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1069 ocfs2_journal_dirty(handle, di_bh);
1070 if (ret) {
1071 mlog_errno(ret);
1072 goto out;
1075 i_size_write(inode, size);
1076 inode->i_nlink = 2;
1077 inode->i_blocks = ocfs2_inode_sector_count(inode);
1079 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1080 if (ret < 0)
1081 mlog_errno(ret);
1083 out:
1084 return ret;
1087 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1088 handle_t *handle,
1089 struct inode *parent,
1090 struct inode *inode,
1091 struct buffer_head *fe_bh,
1092 struct ocfs2_alloc_context *data_ac)
1094 int status;
1095 struct buffer_head *new_bh = NULL;
1097 mlog_entry_void();
1099 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1100 data_ac, NULL, &new_bh);
1101 if (status < 0) {
1102 mlog_errno(status);
1103 goto bail;
1106 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1108 status = ocfs2_journal_access(handle, inode, new_bh,
1109 OCFS2_JOURNAL_ACCESS_CREATE);
1110 if (status < 0) {
1111 mlog_errno(status);
1112 goto bail;
1114 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1116 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1117 osb->sb->s_blocksize);
1119 status = ocfs2_journal_dirty(handle, new_bh);
1120 if (status < 0) {
1121 mlog_errno(status);
1122 goto bail;
1125 i_size_write(inode, inode->i_sb->s_blocksize);
1126 inode->i_nlink = 2;
1127 inode->i_blocks = ocfs2_inode_sector_count(inode);
1128 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1129 if (status < 0) {
1130 mlog_errno(status);
1131 goto bail;
1134 status = 0;
1135 bail:
1136 if (new_bh)
1137 brelse(new_bh);
1139 mlog_exit(status);
1140 return status;
1143 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1144 handle_t *handle,
1145 struct inode *parent,
1146 struct inode *inode,
1147 struct buffer_head *fe_bh,
1148 struct ocfs2_alloc_context *data_ac)
1150 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1152 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1153 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1155 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1156 data_ac);
1159 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1160 unsigned int new_size)
1162 struct ocfs2_dir_entry *de;
1163 struct ocfs2_dir_entry *prev_de;
1164 char *de_buf, *limit;
1165 unsigned int bytes = new_size - old_size;
1167 limit = start + old_size;
1168 de_buf = start;
1169 de = (struct ocfs2_dir_entry *)de_buf;
1170 do {
1171 prev_de = de;
1172 de_buf += le16_to_cpu(de->rec_len);
1173 de = (struct ocfs2_dir_entry *)de_buf;
1174 } while (de_buf < limit);
1176 le16_add_cpu(&prev_de->rec_len, bytes);
1180 * We allocate enough clusters to fulfill "blocks_wanted", but set
1181 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1182 * rest automatically for us.
1184 * *first_block_bh is a pointer to the 1st data block allocated to the
1185 * directory.
1187 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1188 unsigned int blocks_wanted,
1189 struct buffer_head **first_block_bh)
1191 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1192 u32 alloc, bit_off, len;
1193 struct super_block *sb = dir->i_sb;
1194 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1195 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1196 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1197 struct ocfs2_alloc_context *data_ac;
1198 struct buffer_head *dirdata_bh = NULL;
1199 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1200 handle_t *handle;
1202 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1205 * We should never need more than 2 clusters for this -
1206 * maximum dirent size is far less than one block. In fact,
1207 * the only time we'd need more than one cluster is if
1208 * blocksize == clustersize and the dirent won't fit in the
1209 * extra space that the expansion to a single block gives. As
1210 * of today, that only happens on 4k/4k file systems.
1212 BUG_ON(alloc > 2);
1214 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1215 if (ret) {
1216 mlog_errno(ret);
1217 goto out;
1220 down_write(&oi->ip_alloc_sem);
1223 * Prepare for worst case allocation scenario of two separate
1224 * extents.
1226 if (alloc == 2)
1227 credits += OCFS2_SUBALLOC_ALLOC;
1229 handle = ocfs2_start_trans(osb, credits);
1230 if (IS_ERR(handle)) {
1231 ret = PTR_ERR(handle);
1232 mlog_errno(ret);
1233 goto out_sem;
1237 * Try to claim as many clusters as the bitmap can give though
1238 * if we only get one now, that's enough to continue. The rest
1239 * will be claimed after the conversion to extents.
1241 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1242 if (ret) {
1243 mlog_errno(ret);
1244 goto out_commit;
1248 * Operations are carefully ordered so that we set up the new
1249 * data block first. The conversion from inline data to
1250 * extents follows.
1252 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1253 dirdata_bh = sb_getblk(sb, blkno);
1254 if (!dirdata_bh) {
1255 ret = -EIO;
1256 mlog_errno(ret);
1257 goto out_commit;
1260 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1262 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1263 OCFS2_JOURNAL_ACCESS_CREATE);
1264 if (ret) {
1265 mlog_errno(ret);
1266 goto out_commit;
1269 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1270 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1271 sb->s_blocksize - i_size_read(dir));
1272 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1273 sb->s_blocksize);
1275 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1276 if (ret) {
1277 mlog_errno(ret);
1278 goto out_commit;
1282 * Set extent, i_size, etc on the directory. After this, the
1283 * inode should contain the same exact dirents as before and
1284 * be fully accessible from system calls.
1286 * We let the later dirent insert modify c/mtime - to the user
1287 * the data hasn't changed.
1289 ret = ocfs2_journal_access(handle, dir, di_bh,
1290 OCFS2_JOURNAL_ACCESS_CREATE);
1291 if (ret) {
1292 mlog_errno(ret);
1293 goto out_commit;
1296 spin_lock(&oi->ip_lock);
1297 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1298 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1299 spin_unlock(&oi->ip_lock);
1301 ocfs2_dinode_new_extent_list(dir, di);
1303 i_size_write(dir, sb->s_blocksize);
1304 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1306 di->i_size = cpu_to_le64(sb->s_blocksize);
1307 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1308 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1309 dir->i_blocks = ocfs2_inode_sector_count(dir);
1312 * This should never fail as our extent list is empty and all
1313 * related blocks have been journaled already.
1315 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0,
1316 NULL);
1317 if (ret) {
1318 mlog_errno(ret);
1319 goto out;
1322 ret = ocfs2_journal_dirty(handle, di_bh);
1323 if (ret) {
1324 mlog_errno(ret);
1325 goto out_commit;
1329 * We asked for two clusters, but only got one in the 1st
1330 * pass. Claim the 2nd cluster as a separate extent.
1332 if (alloc > len) {
1333 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1334 &len);
1335 if (ret) {
1336 mlog_errno(ret);
1337 goto out_commit;
1339 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1341 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 1, blkno,
1342 len, 0, NULL);
1343 if (ret) {
1344 mlog_errno(ret);
1345 goto out;
1349 *first_block_bh = dirdata_bh;
1350 dirdata_bh = NULL;
1352 out_commit:
1353 ocfs2_commit_trans(osb, handle);
1355 out_sem:
1356 up_write(&oi->ip_alloc_sem);
1358 out:
1359 if (data_ac)
1360 ocfs2_free_alloc_context(data_ac);
1362 brelse(dirdata_bh);
1364 return ret;
1367 /* returns a bh of the 1st new block in the allocation. */
1368 static int ocfs2_do_extend_dir(struct super_block *sb,
1369 handle_t *handle,
1370 struct inode *dir,
1371 struct buffer_head *parent_fe_bh,
1372 struct ocfs2_alloc_context *data_ac,
1373 struct ocfs2_alloc_context *meta_ac,
1374 struct buffer_head **new_bh)
1376 int status;
1377 int extend;
1378 u64 p_blkno, v_blkno;
1380 spin_lock(&OCFS2_I(dir)->ip_lock);
1381 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1382 spin_unlock(&OCFS2_I(dir)->ip_lock);
1384 if (extend) {
1385 u32 offset = OCFS2_I(dir)->ip_clusters;
1387 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
1388 1, 0, parent_fe_bh, handle,
1389 data_ac, meta_ac, NULL);
1390 BUG_ON(status == -EAGAIN);
1391 if (status < 0) {
1392 mlog_errno(status);
1393 goto bail;
1397 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1398 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1399 if (status < 0) {
1400 mlog_errno(status);
1401 goto bail;
1404 *new_bh = sb_getblk(sb, p_blkno);
1405 if (!*new_bh) {
1406 status = -EIO;
1407 mlog_errno(status);
1408 goto bail;
1410 status = 0;
1411 bail:
1412 mlog_exit(status);
1413 return status;
1417 * Assumes you already have a cluster lock on the directory.
1419 * 'blocks_wanted' is only used if we have an inline directory which
1420 * is to be turned into an extent based one. The size of the dirent to
1421 * insert might be larger than the space gained by growing to just one
1422 * block, so we may have to grow the inode by two blocks in that case.
1424 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1425 struct inode *dir,
1426 struct buffer_head *parent_fe_bh,
1427 unsigned int blocks_wanted,
1428 struct buffer_head **new_de_bh)
1430 int status = 0;
1431 int credits, num_free_extents, drop_alloc_sem = 0;
1432 loff_t dir_i_size;
1433 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1434 struct ocfs2_alloc_context *data_ac = NULL;
1435 struct ocfs2_alloc_context *meta_ac = NULL;
1436 handle_t *handle = NULL;
1437 struct buffer_head *new_bh = NULL;
1438 struct ocfs2_dir_entry * de;
1439 struct super_block *sb = osb->sb;
1441 mlog_entry_void();
1443 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1444 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1445 blocks_wanted, &new_bh);
1446 if (status) {
1447 mlog_errno(status);
1448 goto bail;
1451 if (blocks_wanted == 1) {
1453 * If the new dirent will fit inside the space
1454 * created by pushing out to one block, then
1455 * we can complete the operation
1456 * here. Otherwise we have to expand i_size
1457 * and format the 2nd block below.
1459 BUG_ON(new_bh == NULL);
1460 goto bail_bh;
1464 * Get rid of 'new_bh' - we want to format the 2nd
1465 * data block and return that instead.
1467 brelse(new_bh);
1468 new_bh = NULL;
1470 dir_i_size = i_size_read(dir);
1471 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1472 goto do_extend;
1475 dir_i_size = i_size_read(dir);
1476 mlog(0, "extending dir %llu (i_size = %lld)\n",
1477 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1479 /* dir->i_size is always block aligned. */
1480 spin_lock(&OCFS2_I(dir)->ip_lock);
1481 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1482 spin_unlock(&OCFS2_I(dir)->ip_lock);
1483 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
1484 if (num_free_extents < 0) {
1485 status = num_free_extents;
1486 mlog_errno(status);
1487 goto bail;
1490 if (!num_free_extents) {
1491 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
1492 if (status < 0) {
1493 if (status != -ENOSPC)
1494 mlog_errno(status);
1495 goto bail;
1499 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1500 if (status < 0) {
1501 if (status != -ENOSPC)
1502 mlog_errno(status);
1503 goto bail;
1506 credits = ocfs2_calc_extend_credits(sb, fe, 1);
1507 } else {
1508 spin_unlock(&OCFS2_I(dir)->ip_lock);
1509 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1512 do_extend:
1513 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1514 drop_alloc_sem = 1;
1516 handle = ocfs2_start_trans(osb, credits);
1517 if (IS_ERR(handle)) {
1518 status = PTR_ERR(handle);
1519 handle = NULL;
1520 mlog_errno(status);
1521 goto bail;
1524 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1525 data_ac, meta_ac, &new_bh);
1526 if (status < 0) {
1527 mlog_errno(status);
1528 goto bail;
1531 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1533 status = ocfs2_journal_access(handle, dir, new_bh,
1534 OCFS2_JOURNAL_ACCESS_CREATE);
1535 if (status < 0) {
1536 mlog_errno(status);
1537 goto bail;
1539 memset(new_bh->b_data, 0, sb->s_blocksize);
1540 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1541 de->inode = 0;
1542 de->rec_len = cpu_to_le16(sb->s_blocksize);
1543 status = ocfs2_journal_dirty(handle, new_bh);
1544 if (status < 0) {
1545 mlog_errno(status);
1546 goto bail;
1549 dir_i_size += dir->i_sb->s_blocksize;
1550 i_size_write(dir, dir_i_size);
1551 dir->i_blocks = ocfs2_inode_sector_count(dir);
1552 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1553 if (status < 0) {
1554 mlog_errno(status);
1555 goto bail;
1558 bail_bh:
1559 *new_de_bh = new_bh;
1560 get_bh(*new_de_bh);
1561 bail:
1562 if (drop_alloc_sem)
1563 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1564 if (handle)
1565 ocfs2_commit_trans(osb, handle);
1567 if (data_ac)
1568 ocfs2_free_alloc_context(data_ac);
1569 if (meta_ac)
1570 ocfs2_free_alloc_context(meta_ac);
1572 if (new_bh)
1573 brelse(new_bh);
1575 mlog_exit(status);
1576 return status;
1579 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1580 const char *name, int namelen,
1581 struct buffer_head **ret_de_bh,
1582 unsigned int *blocks_wanted)
1584 int ret;
1585 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1586 struct ocfs2_dir_entry *de, *last_de = NULL;
1587 char *de_buf, *limit;
1588 unsigned long offset = 0;
1589 unsigned int rec_len, new_rec_len;
1591 de_buf = di->id2.i_data.id_data;
1592 limit = de_buf + i_size_read(dir);
1593 rec_len = OCFS2_DIR_REC_LEN(namelen);
1595 while (de_buf < limit) {
1596 de = (struct ocfs2_dir_entry *)de_buf;
1598 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1599 ret = -ENOENT;
1600 goto out;
1602 if (ocfs2_match(namelen, name, de)) {
1603 ret = -EEXIST;
1604 goto out;
1606 if (ocfs2_dirent_would_fit(de, rec_len)) {
1607 /* Ok, we found a spot. Return this bh and let
1608 * the caller actually fill it in. */
1609 *ret_de_bh = di_bh;
1610 get_bh(*ret_de_bh);
1611 ret = 0;
1612 goto out;
1615 last_de = de;
1616 de_buf += le16_to_cpu(de->rec_len);
1617 offset += le16_to_cpu(de->rec_len);
1621 * We're going to require expansion of the directory - figure
1622 * out how many blocks we'll need so that a place for the
1623 * dirent can be found.
1625 *blocks_wanted = 1;
1626 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1627 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1628 *blocks_wanted = 2;
1630 ret = -ENOSPC;
1631 out:
1632 return ret;
1635 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1636 int namelen, struct buffer_head **ret_de_bh)
1638 unsigned long offset;
1639 struct buffer_head *bh = NULL;
1640 unsigned short rec_len;
1641 struct ocfs2_dir_entry *de;
1642 struct super_block *sb = dir->i_sb;
1643 int status;
1645 bh = ocfs2_bread(dir, 0, &status, 0);
1646 if (!bh) {
1647 mlog_errno(status);
1648 goto bail;
1651 rec_len = OCFS2_DIR_REC_LEN(namelen);
1652 offset = 0;
1653 de = (struct ocfs2_dir_entry *) bh->b_data;
1654 while (1) {
1655 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1656 brelse(bh);
1657 bh = NULL;
1659 if (i_size_read(dir) <= offset) {
1661 * Caller will have to expand this
1662 * directory.
1664 status = -ENOSPC;
1665 goto bail;
1667 bh = ocfs2_bread(dir,
1668 offset >> sb->s_blocksize_bits,
1669 &status,
1671 if (!bh) {
1672 mlog_errno(status);
1673 goto bail;
1675 /* move to next block */
1676 de = (struct ocfs2_dir_entry *) bh->b_data;
1678 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1679 status = -ENOENT;
1680 goto bail;
1682 if (ocfs2_match(namelen, name, de)) {
1683 status = -EEXIST;
1684 goto bail;
1686 if (ocfs2_dirent_would_fit(de, rec_len)) {
1687 /* Ok, we found a spot. Return this bh and let
1688 * the caller actually fill it in. */
1689 *ret_de_bh = bh;
1690 get_bh(*ret_de_bh);
1691 status = 0;
1692 goto bail;
1694 offset += le16_to_cpu(de->rec_len);
1695 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1698 status = 0;
1699 bail:
1700 if (bh)
1701 brelse(bh);
1703 mlog_exit(status);
1704 return status;
1707 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1708 struct inode *dir,
1709 struct buffer_head *parent_fe_bh,
1710 const char *name,
1711 int namelen,
1712 struct buffer_head **ret_de_bh)
1714 int ret;
1715 unsigned int blocks_wanted = 1;
1716 struct buffer_head *bh = NULL;
1718 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1719 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1721 *ret_de_bh = NULL;
1723 if (!namelen) {
1724 ret = -EINVAL;
1725 mlog_errno(ret);
1726 goto out;
1729 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1730 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1731 namelen, &bh, &blocks_wanted);
1732 } else
1733 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1735 if (ret && ret != -ENOSPC) {
1736 mlog_errno(ret);
1737 goto out;
1740 if (ret == -ENOSPC) {
1742 * We have to expand the directory to add this name.
1744 BUG_ON(bh);
1746 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1747 &bh);
1748 if (ret) {
1749 if (ret != -ENOSPC)
1750 mlog_errno(ret);
1751 goto out;
1754 BUG_ON(!bh);
1757 *ret_de_bh = bh;
1758 bh = NULL;
1759 out:
1760 if (bh)
1761 brelse(bh);
1762 return ret;