ocfs2: Add directory block trailers.
[linux-2.6/mini2440.git] / fs / ocfs2 / dir.c
blob1efd0ab680cffe8974ed892b810419393fe09f5b
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 "dir.h"
52 #include "dlmglue.h"
53 #include "extent_map.h"
54 #include "file.h"
55 #include "inode.h"
56 #include "journal.h"
57 #include "namei.h"
58 #include "suballoc.h"
59 #include "super.h"
60 #include "uptodate.h"
62 #include "buffer_head_io.h"
64 #define NAMEI_RA_CHUNKS 2
65 #define NAMEI_RA_BLOCKS 4
66 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
67 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
69 static unsigned char ocfs2_filetype_table[] = {
70 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
73 static int ocfs2_extend_dir(struct ocfs2_super *osb,
74 struct inode *dir,
75 struct buffer_head *parent_fe_bh,
76 unsigned int blocks_wanted,
77 struct buffer_head **new_de_bh);
78 static int ocfs2_do_extend_dir(struct super_block *sb,
79 handle_t *handle,
80 struct inode *dir,
81 struct buffer_head *parent_fe_bh,
82 struct ocfs2_alloc_context *data_ac,
83 struct ocfs2_alloc_context *meta_ac,
84 struct buffer_head **new_bh);
87 * These are distinct checks because future versions of the file system will
88 * want to have a trailing dirent structure independent of indexing.
90 static int ocfs2_dir_has_trailer(struct inode *dir)
92 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
93 return 0;
95 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
98 static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
100 return ocfs2_meta_ecc(osb);
103 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
105 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
108 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
111 * XXX: This is executed once on every dirent. We should consider optimizing
112 * it.
114 static int ocfs2_skip_dir_trailer(struct inode *dir,
115 struct ocfs2_dir_entry *de,
116 unsigned long offset,
117 unsigned long blklen)
119 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
121 if (!ocfs2_dir_has_trailer(dir))
122 return 0;
124 if (offset != toff)
125 return 0;
127 return 1;
130 static void ocfs2_init_dir_trailer(struct inode *inode,
131 struct buffer_head *bh)
133 struct ocfs2_dir_block_trailer *trailer;
135 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
136 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
137 trailer->db_compat_rec_len =
138 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
139 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
140 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
144 * bh passed here can be an inode block or a dir data block, depending
145 * on the inode inline data flag.
147 static int ocfs2_check_dir_entry(struct inode * dir,
148 struct ocfs2_dir_entry * de,
149 struct buffer_head * bh,
150 unsigned long offset)
152 const char *error_msg = NULL;
153 const int rlen = le16_to_cpu(de->rec_len);
155 if (rlen < OCFS2_DIR_REC_LEN(1))
156 error_msg = "rec_len is smaller than minimal";
157 else if (rlen % 4 != 0)
158 error_msg = "rec_len % 4 != 0";
159 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
160 error_msg = "rec_len is too small for name_len";
161 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
162 error_msg = "directory entry across blocks";
164 if (error_msg != NULL)
165 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
166 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
167 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
168 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
169 de->name_len);
170 return error_msg == NULL ? 1 : 0;
173 static inline int ocfs2_match(int len,
174 const char * const name,
175 struct ocfs2_dir_entry *de)
177 if (len != de->name_len)
178 return 0;
179 if (!de->inode)
180 return 0;
181 return !memcmp(name, de->name, len);
185 * Returns 0 if not found, -1 on failure, and 1 on success
187 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
188 struct inode *dir,
189 const char *name, int namelen,
190 unsigned long offset,
191 char *first_de,
192 unsigned int bytes,
193 struct ocfs2_dir_entry **res_dir)
195 struct ocfs2_dir_entry *de;
196 char *dlimit, *de_buf;
197 int de_len;
198 int ret = 0;
200 mlog_entry_void();
202 de_buf = first_de;
203 dlimit = de_buf + bytes;
205 while (de_buf < dlimit) {
206 /* this code is executed quadratically often */
207 /* do minimal checking `by hand' */
209 de = (struct ocfs2_dir_entry *) de_buf;
211 if (de_buf + namelen <= dlimit &&
212 ocfs2_match(namelen, name, de)) {
213 /* found a match - just to be sure, do a full check */
214 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
215 ret = -1;
216 goto bail;
218 *res_dir = de;
219 ret = 1;
220 goto bail;
223 /* prevent looping on a bad block */
224 de_len = le16_to_cpu(de->rec_len);
225 if (de_len <= 0) {
226 ret = -1;
227 goto bail;
230 de_buf += de_len;
231 offset += de_len;
234 bail:
235 mlog_exit(ret);
236 return ret;
239 static struct buffer_head *ocfs2_find_entry_id(const char *name,
240 int namelen,
241 struct inode *dir,
242 struct ocfs2_dir_entry **res_dir)
244 int ret, found;
245 struct buffer_head *di_bh = NULL;
246 struct ocfs2_dinode *di;
247 struct ocfs2_inline_data *data;
249 ret = ocfs2_read_inode_block(dir, &di_bh);
250 if (ret) {
251 mlog_errno(ret);
252 goto out;
255 di = (struct ocfs2_dinode *)di_bh->b_data;
256 data = &di->id2.i_data;
258 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
259 data->id_data, i_size_read(dir), res_dir);
260 if (found == 1)
261 return di_bh;
263 brelse(di_bh);
264 out:
265 return NULL;
268 static int ocfs2_validate_dir_block(struct super_block *sb,
269 struct buffer_head *bh)
272 * Nothing yet. We don't validate dirents here, that's handled
273 * in-place when the code walks them.
275 mlog(0, "Validating dirblock %llu\n",
276 (unsigned long long)bh->b_blocknr);
278 return 0;
282 * This function forces all errors to -EIO for consistency with its
283 * predecessor, ocfs2_bread(). We haven't audited what returning the
284 * real error codes would do to callers. We log the real codes with
285 * mlog_errno() before we squash them.
287 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
288 struct buffer_head **bh, int flags)
290 int rc = 0;
291 struct buffer_head *tmp = *bh;
292 struct ocfs2_dir_block_trailer *trailer;
294 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
295 ocfs2_validate_dir_block);
296 if (rc) {
297 mlog_errno(rc);
298 goto out;
302 * We check the trailer here rather than in
303 * ocfs2_validate_dir_block() because that function doesn't have
304 * the inode to test.
306 if (!(flags & OCFS2_BH_READAHEAD) &&
307 ocfs2_dir_has_trailer(inode)) {
308 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
309 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
310 rc = -EINVAL;
311 ocfs2_error(inode->i_sb,
312 "Invalid dirblock #%llu: "
313 "signature = %.*s\n",
314 (unsigned long long)tmp->b_blocknr, 7,
315 trailer->db_signature);
316 goto out;
318 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
319 rc = -EINVAL;
320 ocfs2_error(inode->i_sb,
321 "Directory block #%llu has an invalid "
322 "db_blkno of %llu",
323 (unsigned long long)tmp->b_blocknr,
324 (unsigned long long)le64_to_cpu(trailer->db_blkno));
325 goto out;
327 if (le64_to_cpu(trailer->db_parent_dinode) !=
328 OCFS2_I(inode)->ip_blkno) {
329 rc = -EINVAL;
330 ocfs2_error(inode->i_sb,
331 "Directory block #%llu on dinode "
332 "#%llu has an invalid parent_dinode "
333 "of %llu",
334 (unsigned long long)tmp->b_blocknr,
335 (unsigned long long)OCFS2_I(inode)->ip_blkno,
336 (unsigned long long)le64_to_cpu(trailer->db_blkno));
337 goto out;
341 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
342 if (!*bh)
343 *bh = tmp;
345 out:
346 return rc ? -EIO : 0;
349 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
350 struct inode *dir,
351 struct ocfs2_dir_entry **res_dir)
353 struct super_block *sb;
354 struct buffer_head *bh_use[NAMEI_RA_SIZE];
355 struct buffer_head *bh, *ret = NULL;
356 unsigned long start, block, b;
357 int ra_max = 0; /* Number of bh's in the readahead
358 buffer, bh_use[] */
359 int ra_ptr = 0; /* Current index into readahead
360 buffer */
361 int num = 0;
362 int nblocks, i, err;
364 mlog_entry_void();
366 sb = dir->i_sb;
368 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
369 start = OCFS2_I(dir)->ip_dir_start_lookup;
370 if (start >= nblocks)
371 start = 0;
372 block = start;
374 restart:
375 do {
377 * We deal with the read-ahead logic here.
379 if (ra_ptr >= ra_max) {
380 /* Refill the readahead buffer */
381 ra_ptr = 0;
382 b = block;
383 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
385 * Terminate if we reach the end of the
386 * directory and must wrap, or if our
387 * search has finished at this block.
389 if (b >= nblocks || (num && block == start)) {
390 bh_use[ra_max] = NULL;
391 break;
393 num++;
395 bh = NULL;
396 err = ocfs2_read_dir_block(dir, b++, &bh,
397 OCFS2_BH_READAHEAD);
398 bh_use[ra_max] = bh;
401 if ((bh = bh_use[ra_ptr++]) == NULL)
402 goto next;
403 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
404 /* read error, skip block & hope for the best.
405 * ocfs2_read_dir_block() has released the bh. */
406 ocfs2_error(dir->i_sb, "reading directory %llu, "
407 "offset %lu\n",
408 (unsigned long long)OCFS2_I(dir)->ip_blkno,
409 block);
410 goto next;
412 i = ocfs2_search_dirblock(bh, dir, name, namelen,
413 block << sb->s_blocksize_bits,
414 bh->b_data, sb->s_blocksize,
415 res_dir);
416 if (i == 1) {
417 OCFS2_I(dir)->ip_dir_start_lookup = block;
418 ret = bh;
419 goto cleanup_and_exit;
420 } else {
421 brelse(bh);
422 if (i < 0)
423 goto cleanup_and_exit;
425 next:
426 if (++block >= nblocks)
427 block = 0;
428 } while (block != start);
431 * If the directory has grown while we were searching, then
432 * search the last part of the directory before giving up.
434 block = nblocks;
435 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
436 if (block < nblocks) {
437 start = 0;
438 goto restart;
441 cleanup_and_exit:
442 /* Clean up the read-ahead blocks */
443 for (; ra_ptr < ra_max; ra_ptr++)
444 brelse(bh_use[ra_ptr]);
446 mlog_exit_ptr(ret);
447 return ret;
451 * Try to find an entry of the provided name within 'dir'.
453 * If nothing was found, NULL is returned. Otherwise, a buffer_head
454 * and pointer to the dir entry are passed back.
456 * Caller can NOT assume anything about the contents of the
457 * buffer_head - it is passed back only so that it can be passed into
458 * any one of the manipulation functions (add entry, delete entry,
459 * etc). As an example, bh in the extent directory case is a data
460 * block, in the inline-data case it actually points to an inode.
462 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
463 struct inode *dir,
464 struct ocfs2_dir_entry **res_dir)
466 *res_dir = NULL;
468 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
469 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
471 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
475 * Update inode number and type of a previously found directory entry.
477 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
478 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
479 struct inode *new_entry_inode)
481 int ret;
482 ocfs2_journal_access_func access = ocfs2_journal_access_db;
485 * The same code works fine for both inline-data and extent
486 * based directories, so no need to split this up. The only
487 * difference is the journal_access function.
490 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
491 access = ocfs2_journal_access_di;
493 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
494 if (ret) {
495 mlog_errno(ret);
496 goto out;
499 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
500 ocfs2_set_de_type(de, new_entry_inode->i_mode);
502 ocfs2_journal_dirty(handle, de_bh);
504 out:
505 return ret;
508 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
509 struct ocfs2_dir_entry *de_del,
510 struct buffer_head *bh, char *first_de,
511 unsigned int bytes)
513 struct ocfs2_dir_entry *de, *pde;
514 int i, status = -ENOENT;
515 ocfs2_journal_access_func access = ocfs2_journal_access_db;
517 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
519 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
520 access = ocfs2_journal_access_di;
522 i = 0;
523 pde = NULL;
524 de = (struct ocfs2_dir_entry *) first_de;
525 while (i < bytes) {
526 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
527 status = -EIO;
528 mlog_errno(status);
529 goto bail;
531 if (de == de_del) {
532 status = access(handle, dir, bh,
533 OCFS2_JOURNAL_ACCESS_WRITE);
534 if (status < 0) {
535 status = -EIO;
536 mlog_errno(status);
537 goto bail;
539 if (pde)
540 le16_add_cpu(&pde->rec_len,
541 le16_to_cpu(de->rec_len));
542 else
543 de->inode = 0;
544 dir->i_version++;
545 status = ocfs2_journal_dirty(handle, bh);
546 goto bail;
548 i += le16_to_cpu(de->rec_len);
549 pde = de;
550 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
552 bail:
553 mlog_exit(status);
554 return status;
557 static inline int ocfs2_delete_entry_id(handle_t *handle,
558 struct inode *dir,
559 struct ocfs2_dir_entry *de_del,
560 struct buffer_head *bh)
562 int ret;
563 struct buffer_head *di_bh = NULL;
564 struct ocfs2_dinode *di;
565 struct ocfs2_inline_data *data;
567 ret = ocfs2_read_inode_block(dir, &di_bh);
568 if (ret) {
569 mlog_errno(ret);
570 goto out;
573 di = (struct ocfs2_dinode *)di_bh->b_data;
574 data = &di->id2.i_data;
576 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
577 i_size_read(dir));
579 brelse(di_bh);
580 out:
581 return ret;
584 static inline int ocfs2_delete_entry_el(handle_t *handle,
585 struct inode *dir,
586 struct ocfs2_dir_entry *de_del,
587 struct buffer_head *bh)
589 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
590 bh->b_size);
594 * ocfs2_delete_entry deletes a directory entry by merging it with the
595 * previous entry
597 int ocfs2_delete_entry(handle_t *handle,
598 struct inode *dir,
599 struct ocfs2_dir_entry *de_del,
600 struct buffer_head *bh)
602 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
603 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
605 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
609 * Check whether 'de' has enough room to hold an entry of
610 * 'new_rec_len' bytes.
612 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
613 unsigned int new_rec_len)
615 unsigned int de_really_used;
617 /* Check whether this is an empty record with enough space */
618 if (le64_to_cpu(de->inode) == 0 &&
619 le16_to_cpu(de->rec_len) >= new_rec_len)
620 return 1;
623 * Record might have free space at the end which we can
624 * use.
626 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
627 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
628 return 1;
630 return 0;
633 /* we don't always have a dentry for what we want to add, so people
634 * like orphan dir can call this instead.
636 * If you pass me insert_bh, I'll skip the search of the other dir
637 * blocks and put the record in there.
639 int __ocfs2_add_entry(handle_t *handle,
640 struct inode *dir,
641 const char *name, int namelen,
642 struct inode *inode, u64 blkno,
643 struct buffer_head *parent_fe_bh,
644 struct buffer_head *insert_bh)
646 unsigned long offset;
647 unsigned short rec_len;
648 struct ocfs2_dir_entry *de, *de1;
649 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
650 struct super_block *sb = dir->i_sb;
651 int retval, status;
652 unsigned int size = sb->s_blocksize;
653 char *data_start = insert_bh->b_data;
655 mlog_entry_void();
657 if (!namelen)
658 return -EINVAL;
660 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
661 data_start = di->id2.i_data.id_data;
662 size = i_size_read(dir);
664 BUG_ON(insert_bh != parent_fe_bh);
667 rec_len = OCFS2_DIR_REC_LEN(namelen);
668 offset = 0;
669 de = (struct ocfs2_dir_entry *) data_start;
670 while (1) {
671 BUG_ON((char *)de >= (size + data_start));
673 /* These checks should've already been passed by the
674 * prepare function, but I guess we can leave them
675 * here anyway. */
676 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
677 retval = -ENOENT;
678 goto bail;
680 if (ocfs2_match(namelen, name, de)) {
681 retval = -EEXIST;
682 goto bail;
685 /* We're guaranteed that we should have space, so we
686 * can't possibly have hit the trailer...right? */
687 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
688 "Hit dir trailer trying to insert %.*s "
689 "(namelen %d) into directory %llu. "
690 "offset is %lu, trailer offset is %d\n",
691 namelen, name, namelen,
692 (unsigned long long)parent_fe_bh->b_blocknr,
693 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
695 if (ocfs2_dirent_would_fit(de, rec_len)) {
696 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
697 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
698 if (retval < 0) {
699 mlog_errno(retval);
700 goto bail;
703 if (insert_bh == parent_fe_bh)
704 status = ocfs2_journal_access_di(handle, dir,
705 insert_bh,
706 OCFS2_JOURNAL_ACCESS_WRITE);
707 else
708 status = ocfs2_journal_access_db(handle, dir,
709 insert_bh,
710 OCFS2_JOURNAL_ACCESS_WRITE);
711 /* By now the buffer is marked for journaling */
712 offset += le16_to_cpu(de->rec_len);
713 if (le64_to_cpu(de->inode)) {
714 de1 = (struct ocfs2_dir_entry *)((char *) de +
715 OCFS2_DIR_REC_LEN(de->name_len));
716 de1->rec_len =
717 cpu_to_le16(le16_to_cpu(de->rec_len) -
718 OCFS2_DIR_REC_LEN(de->name_len));
719 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
720 de = de1;
722 de->file_type = OCFS2_FT_UNKNOWN;
723 if (blkno) {
724 de->inode = cpu_to_le64(blkno);
725 ocfs2_set_de_type(de, inode->i_mode);
726 } else
727 de->inode = 0;
728 de->name_len = namelen;
729 memcpy(de->name, name, namelen);
731 dir->i_version++;
732 status = ocfs2_journal_dirty(handle, insert_bh);
733 retval = 0;
734 goto bail;
737 offset += le16_to_cpu(de->rec_len);
738 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
741 /* when you think about it, the assert above should prevent us
742 * from ever getting here. */
743 retval = -ENOSPC;
744 bail:
746 mlog_exit(retval);
747 return retval;
750 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
751 u64 *f_version,
752 loff_t *f_pos, void *priv,
753 filldir_t filldir, int *filldir_err)
755 int ret, i, filldir_ret;
756 unsigned long offset = *f_pos;
757 struct buffer_head *di_bh = NULL;
758 struct ocfs2_dinode *di;
759 struct ocfs2_inline_data *data;
760 struct ocfs2_dir_entry *de;
762 ret = ocfs2_read_inode_block(inode, &di_bh);
763 if (ret) {
764 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
765 (unsigned long long)OCFS2_I(inode)->ip_blkno);
766 goto out;
769 di = (struct ocfs2_dinode *)di_bh->b_data;
770 data = &di->id2.i_data;
772 while (*f_pos < i_size_read(inode)) {
773 revalidate:
774 /* If the dir block has changed since the last call to
775 * readdir(2), then we might be pointing to an invalid
776 * dirent right now. Scan from the start of the block
777 * to make sure. */
778 if (*f_version != inode->i_version) {
779 for (i = 0; i < i_size_read(inode) && i < offset; ) {
780 de = (struct ocfs2_dir_entry *)
781 (data->id_data + i);
782 /* It's too expensive to do a full
783 * dirent test each time round this
784 * loop, but we do have to test at
785 * least that it is non-zero. A
786 * failure will be detected in the
787 * dirent test below. */
788 if (le16_to_cpu(de->rec_len) <
789 OCFS2_DIR_REC_LEN(1))
790 break;
791 i += le16_to_cpu(de->rec_len);
793 *f_pos = offset = i;
794 *f_version = inode->i_version;
797 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
798 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
799 /* On error, skip the f_pos to the end. */
800 *f_pos = i_size_read(inode);
801 goto out;
803 offset += le16_to_cpu(de->rec_len);
804 if (le64_to_cpu(de->inode)) {
805 /* We might block in the next section
806 * if the data destination is
807 * currently swapped out. So, use a
808 * version stamp to detect whether or
809 * not the directory has been modified
810 * during the copy operation.
812 u64 version = *f_version;
813 unsigned char d_type = DT_UNKNOWN;
815 if (de->file_type < OCFS2_FT_MAX)
816 d_type = ocfs2_filetype_table[de->file_type];
818 filldir_ret = filldir(priv, de->name,
819 de->name_len,
820 *f_pos,
821 le64_to_cpu(de->inode),
822 d_type);
823 if (filldir_ret) {
824 if (filldir_err)
825 *filldir_err = filldir_ret;
826 break;
828 if (version != *f_version)
829 goto revalidate;
831 *f_pos += le16_to_cpu(de->rec_len);
834 out:
835 brelse(di_bh);
837 return 0;
840 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
841 u64 *f_version,
842 loff_t *f_pos, void *priv,
843 filldir_t filldir, int *filldir_err)
845 int error = 0;
846 unsigned long offset, blk, last_ra_blk = 0;
847 int i, stored;
848 struct buffer_head * bh, * tmp;
849 struct ocfs2_dir_entry * de;
850 struct super_block * sb = inode->i_sb;
851 unsigned int ra_sectors = 16;
853 stored = 0;
854 bh = NULL;
856 offset = (*f_pos) & (sb->s_blocksize - 1);
858 while (!error && !stored && *f_pos < i_size_read(inode)) {
859 blk = (*f_pos) >> sb->s_blocksize_bits;
860 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
861 /* Skip the corrupt dirblock and keep trying */
862 *f_pos += sb->s_blocksize - offset;
863 continue;
866 /* The idea here is to begin with 8k read-ahead and to stay
867 * 4k ahead of our current position.
869 * TODO: Use the pagecache for this. We just need to
870 * make sure it's cluster-safe... */
871 if (!last_ra_blk
872 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
873 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
874 i > 0; i--) {
875 tmp = NULL;
876 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
877 OCFS2_BH_READAHEAD))
878 brelse(tmp);
880 last_ra_blk = blk;
881 ra_sectors = 8;
884 revalidate:
885 /* If the dir block has changed since the last call to
886 * readdir(2), then we might be pointing to an invalid
887 * dirent right now. Scan from the start of the block
888 * to make sure. */
889 if (*f_version != inode->i_version) {
890 for (i = 0; i < sb->s_blocksize && i < offset; ) {
891 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
892 /* It's too expensive to do a full
893 * dirent test each time round this
894 * loop, but we do have to test at
895 * least that it is non-zero. A
896 * failure will be detected in the
897 * dirent test below. */
898 if (le16_to_cpu(de->rec_len) <
899 OCFS2_DIR_REC_LEN(1))
900 break;
901 i += le16_to_cpu(de->rec_len);
903 offset = i;
904 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
905 | offset;
906 *f_version = inode->i_version;
909 while (!error && *f_pos < i_size_read(inode)
910 && offset < sb->s_blocksize) {
911 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
912 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
913 /* On error, skip the f_pos to the
914 next block. */
915 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
916 brelse(bh);
917 goto out;
919 offset += le16_to_cpu(de->rec_len);
920 if (le64_to_cpu(de->inode)) {
921 /* We might block in the next section
922 * if the data destination is
923 * currently swapped out. So, use a
924 * version stamp to detect whether or
925 * not the directory has been modified
926 * during the copy operation.
928 unsigned long version = *f_version;
929 unsigned char d_type = DT_UNKNOWN;
931 if (de->file_type < OCFS2_FT_MAX)
932 d_type = ocfs2_filetype_table[de->file_type];
933 error = filldir(priv, de->name,
934 de->name_len,
935 *f_pos,
936 le64_to_cpu(de->inode),
937 d_type);
938 if (error) {
939 if (filldir_err)
940 *filldir_err = error;
941 break;
943 if (version != *f_version)
944 goto revalidate;
945 stored ++;
947 *f_pos += le16_to_cpu(de->rec_len);
949 offset = 0;
950 brelse(bh);
951 bh = NULL;
954 stored = 0;
955 out:
956 return stored;
959 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
960 loff_t *f_pos, void *priv, filldir_t filldir,
961 int *filldir_err)
963 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
964 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
965 filldir, filldir_err);
967 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
968 filldir_err);
972 * This is intended to be called from inside other kernel functions,
973 * so we fake some arguments.
975 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
976 filldir_t filldir)
978 int ret = 0, filldir_err = 0;
979 u64 version = inode->i_version;
981 while (*f_pos < i_size_read(inode)) {
982 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
983 filldir, &filldir_err);
984 if (ret || filldir_err)
985 break;
988 if (ret > 0)
989 ret = -EIO;
991 return 0;
995 * ocfs2_readdir()
998 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1000 int error = 0;
1001 struct inode *inode = filp->f_path.dentry->d_inode;
1002 int lock_level = 0;
1004 mlog_entry("dirino=%llu\n",
1005 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1007 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1008 if (lock_level && error >= 0) {
1009 /* We release EX lock which used to update atime
1010 * and get PR lock again to reduce contention
1011 * on commonly accessed directories. */
1012 ocfs2_inode_unlock(inode, 1);
1013 lock_level = 0;
1014 error = ocfs2_inode_lock(inode, NULL, 0);
1016 if (error < 0) {
1017 if (error != -ENOENT)
1018 mlog_errno(error);
1019 /* we haven't got any yet, so propagate the error. */
1020 goto bail_nolock;
1023 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
1024 dirent, filldir, NULL);
1026 ocfs2_inode_unlock(inode, lock_level);
1028 bail_nolock:
1029 mlog_exit(error);
1031 return error;
1035 * NOTE: this should always be called with parent dir i_mutex taken.
1037 int ocfs2_find_files_on_disk(const char *name,
1038 int namelen,
1039 u64 *blkno,
1040 struct inode *inode,
1041 struct buffer_head **dirent_bh,
1042 struct ocfs2_dir_entry **dirent)
1044 int status = -ENOENT;
1046 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1047 namelen, name, blkno, inode, dirent_bh, dirent);
1049 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
1050 if (!*dirent_bh || !*dirent) {
1051 status = -ENOENT;
1052 goto leave;
1055 *blkno = le64_to_cpu((*dirent)->inode);
1057 status = 0;
1058 leave:
1059 if (status < 0) {
1060 *dirent = NULL;
1061 brelse(*dirent_bh);
1062 *dirent_bh = NULL;
1065 mlog_exit(status);
1066 return status;
1070 * Convenience function for callers which just want the block number
1071 * mapped to a name and don't require the full dirent info, etc.
1073 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1074 int namelen, u64 *blkno)
1076 int ret;
1077 struct buffer_head *bh = NULL;
1078 struct ocfs2_dir_entry *dirent = NULL;
1080 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
1081 brelse(bh);
1083 return ret;
1086 /* Check for a name within a directory.
1088 * Return 0 if the name does not exist
1089 * Return -EEXIST if the directory contains the name
1091 * Callers should have i_mutex + a cluster lock on dir
1093 int ocfs2_check_dir_for_entry(struct inode *dir,
1094 const char *name,
1095 int namelen)
1097 int ret;
1098 struct buffer_head *dirent_bh = NULL;
1099 struct ocfs2_dir_entry *dirent = NULL;
1101 mlog_entry("dir %llu, name '%.*s'\n",
1102 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
1104 ret = -EEXIST;
1105 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1106 if (dirent_bh)
1107 goto bail;
1109 ret = 0;
1110 bail:
1111 brelse(dirent_bh);
1113 mlog_exit(ret);
1114 return ret;
1117 struct ocfs2_empty_dir_priv {
1118 unsigned seen_dot;
1119 unsigned seen_dot_dot;
1120 unsigned seen_other;
1122 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1123 loff_t pos, u64 ino, unsigned type)
1125 struct ocfs2_empty_dir_priv *p = priv;
1128 * Check the positions of "." and ".." records to be sure
1129 * they're in the correct place.
1131 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1132 p->seen_dot = 1;
1133 return 0;
1136 if (name_len == 2 && !strncmp("..", name, 2) &&
1137 pos == OCFS2_DIR_REC_LEN(1)) {
1138 p->seen_dot_dot = 1;
1139 return 0;
1142 p->seen_other = 1;
1143 return 1;
1146 * routine to check that the specified directory is empty (for rmdir)
1148 * Returns 1 if dir is empty, zero otherwise.
1150 int ocfs2_empty_dir(struct inode *inode)
1152 int ret;
1153 loff_t start = 0;
1154 struct ocfs2_empty_dir_priv priv;
1156 memset(&priv, 0, sizeof(priv));
1158 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1159 if (ret)
1160 mlog_errno(ret);
1162 if (!priv.seen_dot || !priv.seen_dot_dot) {
1163 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1164 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1166 * XXX: Is it really safe to allow an unlink to continue?
1168 return 1;
1171 return !priv.seen_other;
1175 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1176 * "..", which might be used during creation of a directory with a trailing
1177 * header. It is otherwise safe to ignore the return code.
1179 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1180 struct inode *parent,
1181 char *start,
1182 unsigned int size)
1184 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1186 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1187 de->name_len = 1;
1188 de->rec_len =
1189 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1190 strcpy(de->name, ".");
1191 ocfs2_set_de_type(de, S_IFDIR);
1193 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1194 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1195 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1196 de->name_len = 2;
1197 strcpy(de->name, "..");
1198 ocfs2_set_de_type(de, S_IFDIR);
1200 return de;
1204 * This works together with code in ocfs2_mknod_locked() which sets
1205 * the inline-data flag and initializes the inline-data section.
1207 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1208 handle_t *handle,
1209 struct inode *parent,
1210 struct inode *inode,
1211 struct buffer_head *di_bh)
1213 int ret;
1214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1215 struct ocfs2_inline_data *data = &di->id2.i_data;
1216 unsigned int size = le16_to_cpu(data->id_count);
1218 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1219 OCFS2_JOURNAL_ACCESS_WRITE);
1220 if (ret) {
1221 mlog_errno(ret);
1222 goto out;
1225 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1227 ocfs2_journal_dirty(handle, di_bh);
1228 if (ret) {
1229 mlog_errno(ret);
1230 goto out;
1233 i_size_write(inode, size);
1234 inode->i_nlink = 2;
1235 inode->i_blocks = ocfs2_inode_sector_count(inode);
1237 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1238 if (ret < 0)
1239 mlog_errno(ret);
1241 out:
1242 return ret;
1245 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1246 handle_t *handle,
1247 struct inode *parent,
1248 struct inode *inode,
1249 struct buffer_head *fe_bh,
1250 struct ocfs2_alloc_context *data_ac)
1252 int status;
1253 unsigned int size = osb->sb->s_blocksize;
1254 struct buffer_head *new_bh = NULL;
1255 struct ocfs2_dir_entry *de;
1257 mlog_entry_void();
1259 if (ocfs2_supports_dir_trailer(osb))
1260 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1262 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1263 data_ac, NULL, &new_bh);
1264 if (status < 0) {
1265 mlog_errno(status);
1266 goto bail;
1269 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1271 status = ocfs2_journal_access_db(handle, inode, new_bh,
1272 OCFS2_JOURNAL_ACCESS_CREATE);
1273 if (status < 0) {
1274 mlog_errno(status);
1275 goto bail;
1277 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1279 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1280 if (ocfs2_supports_dir_trailer(osb))
1281 ocfs2_init_dir_trailer(inode, new_bh);
1283 status = ocfs2_journal_dirty(handle, new_bh);
1284 if (status < 0) {
1285 mlog_errno(status);
1286 goto bail;
1289 i_size_write(inode, inode->i_sb->s_blocksize);
1290 inode->i_nlink = 2;
1291 inode->i_blocks = ocfs2_inode_sector_count(inode);
1292 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1293 if (status < 0) {
1294 mlog_errno(status);
1295 goto bail;
1298 status = 0;
1299 bail:
1300 brelse(new_bh);
1302 mlog_exit(status);
1303 return status;
1306 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1307 handle_t *handle,
1308 struct inode *parent,
1309 struct inode *inode,
1310 struct buffer_head *fe_bh,
1311 struct ocfs2_alloc_context *data_ac)
1313 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1315 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1316 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1318 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1319 data_ac);
1323 * Expand rec_len of the rightmost dirent in a directory block so that it
1324 * contains the end of our valid space for dirents. We do this during
1325 * expansion from an inline directory to one with extents. The first dir block
1326 * in that case is taken from the inline data portion of the inode block.
1328 * We add the dir trailer if this filesystem wants it.
1330 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1331 struct super_block *sb)
1333 struct ocfs2_dir_entry *de;
1334 struct ocfs2_dir_entry *prev_de;
1335 char *de_buf, *limit;
1336 unsigned int new_size = sb->s_blocksize;
1337 unsigned int bytes;
1339 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1340 new_size = ocfs2_dir_trailer_blk_off(sb);
1342 bytes = new_size - old_size;
1344 limit = start + old_size;
1345 de_buf = start;
1346 de = (struct ocfs2_dir_entry *)de_buf;
1347 do {
1348 prev_de = de;
1349 de_buf += le16_to_cpu(de->rec_len);
1350 de = (struct ocfs2_dir_entry *)de_buf;
1351 } while (de_buf < limit);
1353 le16_add_cpu(&prev_de->rec_len, bytes);
1357 * We allocate enough clusters to fulfill "blocks_wanted", but set
1358 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1359 * rest automatically for us.
1361 * *first_block_bh is a pointer to the 1st data block allocated to the
1362 * directory.
1364 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1365 unsigned int blocks_wanted,
1366 struct buffer_head **first_block_bh)
1368 u32 alloc, bit_off, len;
1369 struct super_block *sb = dir->i_sb;
1370 int ret, credits = ocfs2_inline_to_extents_credits(sb);
1371 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1372 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1373 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1374 struct ocfs2_alloc_context *data_ac;
1375 struct buffer_head *dirdata_bh = NULL;
1376 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1377 handle_t *handle;
1378 struct ocfs2_extent_tree et;
1379 int did_quota = 0;
1381 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1383 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1386 * We should never need more than 2 clusters for this -
1387 * maximum dirent size is far less than one block. In fact,
1388 * the only time we'd need more than one cluster is if
1389 * blocksize == clustersize and the dirent won't fit in the
1390 * extra space that the expansion to a single block gives. As
1391 * of today, that only happens on 4k/4k file systems.
1393 BUG_ON(alloc > 2);
1395 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1396 if (ret) {
1397 mlog_errno(ret);
1398 goto out;
1401 down_write(&oi->ip_alloc_sem);
1404 * Prepare for worst case allocation scenario of two separate
1405 * extents.
1407 if (alloc == 2)
1408 credits += OCFS2_SUBALLOC_ALLOC;
1410 handle = ocfs2_start_trans(osb, credits);
1411 if (IS_ERR(handle)) {
1412 ret = PTR_ERR(handle);
1413 mlog_errno(ret);
1414 goto out_sem;
1417 if (vfs_dq_alloc_space_nodirty(dir,
1418 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1419 ret = -EDQUOT;
1420 goto out_commit;
1422 did_quota = 1;
1424 * Try to claim as many clusters as the bitmap can give though
1425 * if we only get one now, that's enough to continue. The rest
1426 * will be claimed after the conversion to extents.
1428 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out_commit;
1435 * Operations are carefully ordered so that we set up the new
1436 * data block first. The conversion from inline data to
1437 * extents follows.
1439 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1440 dirdata_bh = sb_getblk(sb, blkno);
1441 if (!dirdata_bh) {
1442 ret = -EIO;
1443 mlog_errno(ret);
1444 goto out_commit;
1447 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1449 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1450 OCFS2_JOURNAL_ACCESS_CREATE);
1451 if (ret) {
1452 mlog_errno(ret);
1453 goto out_commit;
1456 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1457 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1458 sb->s_blocksize - i_size_read(dir));
1459 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1460 if (ocfs2_supports_dir_trailer(osb))
1461 ocfs2_init_dir_trailer(dir, dirdata_bh);
1463 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1464 if (ret) {
1465 mlog_errno(ret);
1466 goto out_commit;
1470 * Set extent, i_size, etc on the directory. After this, the
1471 * inode should contain the same exact dirents as before and
1472 * be fully accessible from system calls.
1474 * We let the later dirent insert modify c/mtime - to the user
1475 * the data hasn't changed.
1477 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1478 OCFS2_JOURNAL_ACCESS_CREATE);
1479 if (ret) {
1480 mlog_errno(ret);
1481 goto out_commit;
1484 spin_lock(&oi->ip_lock);
1485 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1486 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1487 spin_unlock(&oi->ip_lock);
1489 ocfs2_dinode_new_extent_list(dir, di);
1491 i_size_write(dir, sb->s_blocksize);
1492 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1494 di->i_size = cpu_to_le64(sb->s_blocksize);
1495 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1496 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1499 * This should never fail as our extent list is empty and all
1500 * related blocks have been journaled already.
1502 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1503 0, NULL);
1504 if (ret) {
1505 mlog_errno(ret);
1506 goto out_commit;
1510 * Set i_blocks after the extent insert for the most up to
1511 * date ip_clusters value.
1513 dir->i_blocks = ocfs2_inode_sector_count(dir);
1515 ret = ocfs2_journal_dirty(handle, di_bh);
1516 if (ret) {
1517 mlog_errno(ret);
1518 goto out_commit;
1522 * We asked for two clusters, but only got one in the 1st
1523 * pass. Claim the 2nd cluster as a separate extent.
1525 if (alloc > len) {
1526 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1527 &len);
1528 if (ret) {
1529 mlog_errno(ret);
1530 goto out_commit;
1532 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1534 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1535 blkno, len, 0, NULL);
1536 if (ret) {
1537 mlog_errno(ret);
1538 goto out_commit;
1542 *first_block_bh = dirdata_bh;
1543 dirdata_bh = NULL;
1545 out_commit:
1546 if (ret < 0 && did_quota)
1547 vfs_dq_free_space_nodirty(dir,
1548 ocfs2_clusters_to_bytes(osb->sb, 2));
1549 ocfs2_commit_trans(osb, handle);
1551 out_sem:
1552 up_write(&oi->ip_alloc_sem);
1554 out:
1555 if (data_ac)
1556 ocfs2_free_alloc_context(data_ac);
1558 brelse(dirdata_bh);
1560 return ret;
1563 /* returns a bh of the 1st new block in the allocation. */
1564 static int ocfs2_do_extend_dir(struct super_block *sb,
1565 handle_t *handle,
1566 struct inode *dir,
1567 struct buffer_head *parent_fe_bh,
1568 struct ocfs2_alloc_context *data_ac,
1569 struct ocfs2_alloc_context *meta_ac,
1570 struct buffer_head **new_bh)
1572 int status;
1573 int extend, did_quota = 0;
1574 u64 p_blkno, v_blkno;
1576 spin_lock(&OCFS2_I(dir)->ip_lock);
1577 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1578 spin_unlock(&OCFS2_I(dir)->ip_lock);
1580 if (extend) {
1581 u32 offset = OCFS2_I(dir)->ip_clusters;
1583 if (vfs_dq_alloc_space_nodirty(dir,
1584 ocfs2_clusters_to_bytes(sb, 1))) {
1585 status = -EDQUOT;
1586 goto bail;
1588 did_quota = 1;
1590 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1591 1, 0, parent_fe_bh, handle,
1592 data_ac, meta_ac, NULL);
1593 BUG_ON(status == -EAGAIN);
1594 if (status < 0) {
1595 mlog_errno(status);
1596 goto bail;
1600 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1601 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1602 if (status < 0) {
1603 mlog_errno(status);
1604 goto bail;
1607 *new_bh = sb_getblk(sb, p_blkno);
1608 if (!*new_bh) {
1609 status = -EIO;
1610 mlog_errno(status);
1611 goto bail;
1613 status = 0;
1614 bail:
1615 if (did_quota && status < 0)
1616 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
1617 mlog_exit(status);
1618 return status;
1622 * Assumes you already have a cluster lock on the directory.
1624 * 'blocks_wanted' is only used if we have an inline directory which
1625 * is to be turned into an extent based one. The size of the dirent to
1626 * insert might be larger than the space gained by growing to just one
1627 * block, so we may have to grow the inode by two blocks in that case.
1629 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1630 struct inode *dir,
1631 struct buffer_head *parent_fe_bh,
1632 unsigned int blocks_wanted,
1633 struct buffer_head **new_de_bh)
1635 int status = 0;
1636 int credits, num_free_extents, drop_alloc_sem = 0;
1637 loff_t dir_i_size;
1638 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1639 struct ocfs2_extent_list *el = &fe->id2.i_list;
1640 struct ocfs2_alloc_context *data_ac = NULL;
1641 struct ocfs2_alloc_context *meta_ac = NULL;
1642 handle_t *handle = NULL;
1643 struct buffer_head *new_bh = NULL;
1644 struct ocfs2_dir_entry * de;
1645 struct super_block *sb = osb->sb;
1646 struct ocfs2_extent_tree et;
1648 mlog_entry_void();
1650 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1651 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1652 blocks_wanted, &new_bh);
1653 if (status) {
1654 mlog_errno(status);
1655 goto bail;
1658 if (blocks_wanted == 1) {
1660 * If the new dirent will fit inside the space
1661 * created by pushing out to one block, then
1662 * we can complete the operation
1663 * here. Otherwise we have to expand i_size
1664 * and format the 2nd block below.
1666 BUG_ON(new_bh == NULL);
1667 goto bail_bh;
1671 * Get rid of 'new_bh' - we want to format the 2nd
1672 * data block and return that instead.
1674 brelse(new_bh);
1675 new_bh = NULL;
1677 dir_i_size = i_size_read(dir);
1678 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1679 goto do_extend;
1682 dir_i_size = i_size_read(dir);
1683 mlog(0, "extending dir %llu (i_size = %lld)\n",
1684 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1686 /* dir->i_size is always block aligned. */
1687 spin_lock(&OCFS2_I(dir)->ip_lock);
1688 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1689 spin_unlock(&OCFS2_I(dir)->ip_lock);
1690 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1691 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1692 if (num_free_extents < 0) {
1693 status = num_free_extents;
1694 mlog_errno(status);
1695 goto bail;
1698 if (!num_free_extents) {
1699 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1700 if (status < 0) {
1701 if (status != -ENOSPC)
1702 mlog_errno(status);
1703 goto bail;
1707 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1708 if (status < 0) {
1709 if (status != -ENOSPC)
1710 mlog_errno(status);
1711 goto bail;
1714 credits = ocfs2_calc_extend_credits(sb, el, 1);
1715 } else {
1716 spin_unlock(&OCFS2_I(dir)->ip_lock);
1717 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1720 do_extend:
1721 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1722 drop_alloc_sem = 1;
1724 handle = ocfs2_start_trans(osb, credits);
1725 if (IS_ERR(handle)) {
1726 status = PTR_ERR(handle);
1727 handle = NULL;
1728 mlog_errno(status);
1729 goto bail;
1732 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1733 data_ac, meta_ac, &new_bh);
1734 if (status < 0) {
1735 mlog_errno(status);
1736 goto bail;
1739 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1741 status = ocfs2_journal_access_db(handle, dir, new_bh,
1742 OCFS2_JOURNAL_ACCESS_CREATE);
1743 if (status < 0) {
1744 mlog_errno(status);
1745 goto bail;
1747 memset(new_bh->b_data, 0, sb->s_blocksize);
1749 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1750 de->inode = 0;
1751 if (ocfs2_dir_has_trailer(dir)) {
1752 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1753 ocfs2_init_dir_trailer(dir, new_bh);
1754 } else {
1755 de->rec_len = cpu_to_le16(sb->s_blocksize);
1757 status = ocfs2_journal_dirty(handle, new_bh);
1758 if (status < 0) {
1759 mlog_errno(status);
1760 goto bail;
1763 dir_i_size += dir->i_sb->s_blocksize;
1764 i_size_write(dir, dir_i_size);
1765 dir->i_blocks = ocfs2_inode_sector_count(dir);
1766 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto bail;
1772 bail_bh:
1773 *new_de_bh = new_bh;
1774 get_bh(*new_de_bh);
1775 bail:
1776 if (drop_alloc_sem)
1777 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1778 if (handle)
1779 ocfs2_commit_trans(osb, handle);
1781 if (data_ac)
1782 ocfs2_free_alloc_context(data_ac);
1783 if (meta_ac)
1784 ocfs2_free_alloc_context(meta_ac);
1786 brelse(new_bh);
1788 mlog_exit(status);
1789 return status;
1792 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1793 const char *name, int namelen,
1794 struct buffer_head **ret_de_bh,
1795 unsigned int *blocks_wanted)
1797 int ret;
1798 struct super_block *sb = dir->i_sb;
1799 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1800 struct ocfs2_dir_entry *de, *last_de = NULL;
1801 char *de_buf, *limit;
1802 unsigned long offset = 0;
1803 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1806 * This calculates how many free bytes we'd have in block zero, should
1807 * this function force expansion to an extent tree.
1809 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1810 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1811 else
1812 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
1814 de_buf = di->id2.i_data.id_data;
1815 limit = de_buf + i_size_read(dir);
1816 rec_len = OCFS2_DIR_REC_LEN(namelen);
1818 while (de_buf < limit) {
1819 de = (struct ocfs2_dir_entry *)de_buf;
1821 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1822 ret = -ENOENT;
1823 goto out;
1825 if (ocfs2_match(namelen, name, de)) {
1826 ret = -EEXIST;
1827 goto out;
1830 * No need to check for a trailing dirent record here as
1831 * they're not used for inline dirs.
1834 if (ocfs2_dirent_would_fit(de, rec_len)) {
1835 /* Ok, we found a spot. Return this bh and let
1836 * the caller actually fill it in. */
1837 *ret_de_bh = di_bh;
1838 get_bh(*ret_de_bh);
1839 ret = 0;
1840 goto out;
1843 last_de = de;
1844 de_buf += le16_to_cpu(de->rec_len);
1845 offset += le16_to_cpu(de->rec_len);
1849 * We're going to require expansion of the directory - figure
1850 * out how many blocks we'll need so that a place for the
1851 * dirent can be found.
1853 *blocks_wanted = 1;
1854 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
1855 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1856 *blocks_wanted = 2;
1858 ret = -ENOSPC;
1859 out:
1860 return ret;
1863 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1864 int namelen, struct buffer_head **ret_de_bh)
1866 unsigned long offset;
1867 struct buffer_head *bh = NULL;
1868 unsigned short rec_len;
1869 struct ocfs2_dir_entry *de;
1870 struct super_block *sb = dir->i_sb;
1871 int status;
1872 int blocksize = dir->i_sb->s_blocksize;
1874 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1875 if (status) {
1876 mlog_errno(status);
1877 goto bail;
1880 rec_len = OCFS2_DIR_REC_LEN(namelen);
1881 offset = 0;
1882 de = (struct ocfs2_dir_entry *) bh->b_data;
1883 while (1) {
1884 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1885 brelse(bh);
1886 bh = NULL;
1888 if (i_size_read(dir) <= offset) {
1890 * Caller will have to expand this
1891 * directory.
1893 status = -ENOSPC;
1894 goto bail;
1896 status = ocfs2_read_dir_block(dir,
1897 offset >> sb->s_blocksize_bits,
1898 &bh, 0);
1899 if (status) {
1900 mlog_errno(status);
1901 goto bail;
1903 /* move to next block */
1904 de = (struct ocfs2_dir_entry *) bh->b_data;
1906 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1907 status = -ENOENT;
1908 goto bail;
1910 if (ocfs2_match(namelen, name, de)) {
1911 status = -EEXIST;
1912 goto bail;
1915 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1916 blocksize))
1917 goto next;
1919 if (ocfs2_dirent_would_fit(de, rec_len)) {
1920 /* Ok, we found a spot. Return this bh and let
1921 * the caller actually fill it in. */
1922 *ret_de_bh = bh;
1923 get_bh(*ret_de_bh);
1924 status = 0;
1925 goto bail;
1927 next:
1928 offset += le16_to_cpu(de->rec_len);
1929 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1932 status = 0;
1933 bail:
1934 brelse(bh);
1936 mlog_exit(status);
1937 return status;
1940 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1941 struct inode *dir,
1942 struct buffer_head *parent_fe_bh,
1943 const char *name,
1944 int namelen,
1945 struct buffer_head **ret_de_bh)
1947 int ret;
1948 unsigned int blocks_wanted = 1;
1949 struct buffer_head *bh = NULL;
1951 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1952 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1954 *ret_de_bh = NULL;
1956 if (!namelen) {
1957 ret = -EINVAL;
1958 mlog_errno(ret);
1959 goto out;
1962 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1963 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1964 namelen, &bh, &blocks_wanted);
1965 } else
1966 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1968 if (ret && ret != -ENOSPC) {
1969 mlog_errno(ret);
1970 goto out;
1973 if (ret == -ENOSPC) {
1975 * We have to expand the directory to add this name.
1977 BUG_ON(bh);
1979 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1980 &bh);
1981 if (ret) {
1982 if (ret != -ENOSPC)
1983 mlog_errno(ret);
1984 goto out;
1987 BUG_ON(!bh);
1990 *ret_de_bh = bh;
1991 bh = NULL;
1992 out:
1993 brelse(bh);
1994 return ret;