1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
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)
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.
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>
53 #include "extent_map.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,
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,
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 * bh passed here can be an inode block or a dir data block, depending
88 * on the inode inline data flag.
90 static int ocfs2_check_dir_entry(struct inode * dir,
91 struct ocfs2_dir_entry * de,
92 struct buffer_head * bh,
95 const char *error_msg = NULL;
96 const int rlen = le16_to_cpu(de->rec_len);
98 if (rlen < OCFS2_DIR_REC_LEN(1))
99 error_msg = "rec_len is smaller than minimal";
100 else if (rlen % 4 != 0)
101 error_msg = "rec_len % 4 != 0";
102 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
103 error_msg = "rec_len is too small for name_len";
104 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
105 error_msg = "directory entry across blocks";
107 if (error_msg != NULL)
108 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
109 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
110 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
111 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
113 return error_msg == NULL ? 1 : 0;
116 static inline int ocfs2_match(int len,
117 const char * const name,
118 struct ocfs2_dir_entry *de)
120 if (len != de->name_len)
124 return !memcmp(name, de->name, len);
128 * Returns 0 if not found, -1 on failure, and 1 on success
130 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
132 const char *name, int namelen,
133 unsigned long offset,
136 struct ocfs2_dir_entry **res_dir)
138 struct ocfs2_dir_entry *de;
139 char *dlimit, *de_buf;
146 dlimit = de_buf + bytes;
148 while (de_buf < dlimit) {
149 /* this code is executed quadratically often */
150 /* do minimal checking `by hand' */
152 de = (struct ocfs2_dir_entry *) de_buf;
154 if (de_buf + namelen <= dlimit &&
155 ocfs2_match(namelen, name, de)) {
156 /* found a match - just to be sure, do a full check */
157 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
166 /* prevent looping on a bad block */
167 de_len = le16_to_cpu(de->rec_len);
182 static struct buffer_head *ocfs2_find_entry_id(const char *name,
185 struct ocfs2_dir_entry **res_dir)
188 struct buffer_head *di_bh = NULL;
189 struct ocfs2_dinode *di;
190 struct ocfs2_inline_data *data;
192 ret = ocfs2_read_inode_block(dir, &di_bh);
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);
211 static int ocfs2_validate_dir_block(struct super_block *sb,
212 struct buffer_head *bh)
215 * Nothing yet. We don't validate dirents here, that's handled
216 * in-place when the code walks them.
218 mlog(0, "Validating dirblock %llu\n",
219 (unsigned long long)bh->b_blocknr);
225 * This function forces all errors to -EIO for consistency with its
226 * predecessor, ocfs2_bread(). We haven't audited what returning the
227 * real error codes would do to callers. We log the real codes with
228 * mlog_errno() before we squash them.
230 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
231 struct buffer_head **bh, int flags)
234 struct buffer_head *tmp = *bh;
236 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
237 ocfs2_validate_dir_block);
241 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
245 return rc ? -EIO : 0;
248 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
250 struct ocfs2_dir_entry **res_dir)
252 struct super_block *sb;
253 struct buffer_head *bh_use[NAMEI_RA_SIZE];
254 struct buffer_head *bh, *ret = NULL;
255 unsigned long start, block, b;
256 int ra_max = 0; /* Number of bh's in the readahead
258 int ra_ptr = 0; /* Current index into readahead
267 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
268 start = OCFS2_I(dir)->ip_dir_start_lookup;
269 if (start >= nblocks)
276 * We deal with the read-ahead logic here.
278 if (ra_ptr >= ra_max) {
279 /* Refill the readahead buffer */
282 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
284 * Terminate if we reach the end of the
285 * directory and must wrap, or if our
286 * search has finished at this block.
288 if (b >= nblocks || (num && block == start)) {
289 bh_use[ra_max] = NULL;
295 err = ocfs2_read_dir_block(dir, b++, &bh,
300 if ((bh = bh_use[ra_ptr++]) == NULL)
302 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
303 /* read error, skip block & hope for the best.
304 * ocfs2_read_dir_block() has released the bh. */
305 ocfs2_error(dir->i_sb, "reading directory %llu, "
307 (unsigned long long)OCFS2_I(dir)->ip_blkno,
311 i = ocfs2_search_dirblock(bh, dir, name, namelen,
312 block << sb->s_blocksize_bits,
313 bh->b_data, sb->s_blocksize,
316 OCFS2_I(dir)->ip_dir_start_lookup = block;
318 goto cleanup_and_exit;
322 goto cleanup_and_exit;
325 if (++block >= nblocks)
327 } while (block != start);
330 * If the directory has grown while we were searching, then
331 * search the last part of the directory before giving up.
334 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
335 if (block < nblocks) {
341 /* Clean up the read-ahead blocks */
342 for (; ra_ptr < ra_max; ra_ptr++)
343 brelse(bh_use[ra_ptr]);
350 * Try to find an entry of the provided name within 'dir'.
352 * If nothing was found, NULL is returned. Otherwise, a buffer_head
353 * and pointer to the dir entry are passed back.
355 * Caller can NOT assume anything about the contents of the
356 * buffer_head - it is passed back only so that it can be passed into
357 * any one of the manipulation functions (add entry, delete entry,
358 * etc). As an example, bh in the extent directory case is a data
359 * block, in the inline-data case it actually points to an inode.
361 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
363 struct ocfs2_dir_entry **res_dir)
367 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
368 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
370 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
374 * Update inode number and type of a previously found directory entry.
376 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
377 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
378 struct inode *new_entry_inode)
381 ocfs2_journal_access_func access = ocfs2_journal_access_db;
384 * The same code works fine for both inline-data and extent
385 * based directories, so no need to split this up. The only
386 * difference is the journal_access function.
389 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
390 access = ocfs2_journal_access_di;
392 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
398 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
399 ocfs2_set_de_type(de, new_entry_inode->i_mode);
401 ocfs2_journal_dirty(handle, de_bh);
407 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
408 struct ocfs2_dir_entry *de_del,
409 struct buffer_head *bh, char *first_de,
412 struct ocfs2_dir_entry *de, *pde;
413 int i, status = -ENOENT;
414 ocfs2_journal_access_func access = ocfs2_journal_access_db;
416 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
418 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
419 access = ocfs2_journal_access_di;
423 de = (struct ocfs2_dir_entry *) first_de;
425 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
431 status = access(handle, dir, bh,
432 OCFS2_JOURNAL_ACCESS_WRITE);
439 le16_add_cpu(&pde->rec_len,
440 le16_to_cpu(de->rec_len));
444 status = ocfs2_journal_dirty(handle, bh);
447 i += le16_to_cpu(de->rec_len);
449 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
456 static inline int ocfs2_delete_entry_id(handle_t *handle,
458 struct ocfs2_dir_entry *de_del,
459 struct buffer_head *bh)
462 struct buffer_head *di_bh = NULL;
463 struct ocfs2_dinode *di;
464 struct ocfs2_inline_data *data;
466 ret = ocfs2_read_inode_block(dir, &di_bh);
472 di = (struct ocfs2_dinode *)di_bh->b_data;
473 data = &di->id2.i_data;
475 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
483 static inline int ocfs2_delete_entry_el(handle_t *handle,
485 struct ocfs2_dir_entry *de_del,
486 struct buffer_head *bh)
488 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
493 * ocfs2_delete_entry deletes a directory entry by merging it with the
496 int ocfs2_delete_entry(handle_t *handle,
498 struct ocfs2_dir_entry *de_del,
499 struct buffer_head *bh)
501 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
502 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
504 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
508 * Check whether 'de' has enough room to hold an entry of
509 * 'new_rec_len' bytes.
511 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
512 unsigned int new_rec_len)
514 unsigned int de_really_used;
516 /* Check whether this is an empty record with enough space */
517 if (le64_to_cpu(de->inode) == 0 &&
518 le16_to_cpu(de->rec_len) >= new_rec_len)
522 * Record might have free space at the end which we can
525 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
526 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
532 /* we don't always have a dentry for what we want to add, so people
533 * like orphan dir can call this instead.
535 * If you pass me insert_bh, I'll skip the search of the other dir
536 * blocks and put the record in there.
538 int __ocfs2_add_entry(handle_t *handle,
540 const char *name, int namelen,
541 struct inode *inode, u64 blkno,
542 struct buffer_head *parent_fe_bh,
543 struct buffer_head *insert_bh)
545 unsigned long offset;
546 unsigned short rec_len;
547 struct ocfs2_dir_entry *de, *de1;
548 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
549 struct super_block *sb = dir->i_sb;
551 unsigned int size = sb->s_blocksize;
552 char *data_start = insert_bh->b_data;
559 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
560 data_start = di->id2.i_data.id_data;
561 size = i_size_read(dir);
563 BUG_ON(insert_bh != parent_fe_bh);
566 rec_len = OCFS2_DIR_REC_LEN(namelen);
568 de = (struct ocfs2_dir_entry *) data_start;
570 BUG_ON((char *)de >= (size + data_start));
572 /* These checks should've already been passed by the
573 * prepare function, but I guess we can leave them
575 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
579 if (ocfs2_match(namelen, name, de)) {
584 if (ocfs2_dirent_would_fit(de, rec_len)) {
585 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
586 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
592 if (insert_bh == parent_fe_bh)
593 status = ocfs2_journal_access_di(handle, dir,
595 OCFS2_JOURNAL_ACCESS_WRITE);
597 status = ocfs2_journal_access_db(handle, dir,
599 OCFS2_JOURNAL_ACCESS_WRITE);
600 /* By now the buffer is marked for journaling */
601 offset += le16_to_cpu(de->rec_len);
602 if (le64_to_cpu(de->inode)) {
603 de1 = (struct ocfs2_dir_entry *)((char *) de +
604 OCFS2_DIR_REC_LEN(de->name_len));
606 cpu_to_le16(le16_to_cpu(de->rec_len) -
607 OCFS2_DIR_REC_LEN(de->name_len));
608 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
611 de->file_type = OCFS2_FT_UNKNOWN;
613 de->inode = cpu_to_le64(blkno);
614 ocfs2_set_de_type(de, inode->i_mode);
617 de->name_len = namelen;
618 memcpy(de->name, name, namelen);
621 status = ocfs2_journal_dirty(handle, insert_bh);
625 offset += le16_to_cpu(de->rec_len);
626 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
629 /* when you think about it, the assert above should prevent us
630 * from ever getting here. */
638 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
640 loff_t *f_pos, void *priv,
641 filldir_t filldir, int *filldir_err)
643 int ret, i, filldir_ret;
644 unsigned long offset = *f_pos;
645 struct buffer_head *di_bh = NULL;
646 struct ocfs2_dinode *di;
647 struct ocfs2_inline_data *data;
648 struct ocfs2_dir_entry *de;
650 ret = ocfs2_read_inode_block(inode, &di_bh);
652 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
653 (unsigned long long)OCFS2_I(inode)->ip_blkno);
657 di = (struct ocfs2_dinode *)di_bh->b_data;
658 data = &di->id2.i_data;
660 while (*f_pos < i_size_read(inode)) {
662 /* If the dir block has changed since the last call to
663 * readdir(2), then we might be pointing to an invalid
664 * dirent right now. Scan from the start of the block
666 if (*f_version != inode->i_version) {
667 for (i = 0; i < i_size_read(inode) && i < offset; ) {
668 de = (struct ocfs2_dir_entry *)
670 /* It's too expensive to do a full
671 * dirent test each time round this
672 * loop, but we do have to test at
673 * least that it is non-zero. A
674 * failure will be detected in the
675 * dirent test below. */
676 if (le16_to_cpu(de->rec_len) <
677 OCFS2_DIR_REC_LEN(1))
679 i += le16_to_cpu(de->rec_len);
682 *f_version = inode->i_version;
685 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
686 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
687 /* On error, skip the f_pos to the end. */
688 *f_pos = i_size_read(inode);
691 offset += le16_to_cpu(de->rec_len);
692 if (le64_to_cpu(de->inode)) {
693 /* We might block in the next section
694 * if the data destination is
695 * currently swapped out. So, use a
696 * version stamp to detect whether or
697 * not the directory has been modified
698 * during the copy operation.
700 u64 version = *f_version;
701 unsigned char d_type = DT_UNKNOWN;
703 if (de->file_type < OCFS2_FT_MAX)
704 d_type = ocfs2_filetype_table[de->file_type];
706 filldir_ret = filldir(priv, de->name,
709 le64_to_cpu(de->inode),
713 *filldir_err = filldir_ret;
716 if (version != *f_version)
719 *f_pos += le16_to_cpu(de->rec_len);
728 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
730 loff_t *f_pos, void *priv,
731 filldir_t filldir, int *filldir_err)
734 unsigned long offset, blk, last_ra_blk = 0;
736 struct buffer_head * bh, * tmp;
737 struct ocfs2_dir_entry * de;
738 struct super_block * sb = inode->i_sb;
739 unsigned int ra_sectors = 16;
744 offset = (*f_pos) & (sb->s_blocksize - 1);
746 while (!error && !stored && *f_pos < i_size_read(inode)) {
747 blk = (*f_pos) >> sb->s_blocksize_bits;
748 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
749 /* Skip the corrupt dirblock and keep trying */
750 *f_pos += sb->s_blocksize - offset;
754 /* The idea here is to begin with 8k read-ahead and to stay
755 * 4k ahead of our current position.
757 * TODO: Use the pagecache for this. We just need to
758 * make sure it's cluster-safe... */
760 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
761 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
764 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
773 /* If the dir block has changed since the last call to
774 * readdir(2), then we might be pointing to an invalid
775 * dirent right now. Scan from the start of the block
777 if (*f_version != inode->i_version) {
778 for (i = 0; i < sb->s_blocksize && i < offset; ) {
779 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
780 /* It's too expensive to do a full
781 * dirent test each time round this
782 * loop, but we do have to test at
783 * least that it is non-zero. A
784 * failure will be detected in the
785 * dirent test below. */
786 if (le16_to_cpu(de->rec_len) <
787 OCFS2_DIR_REC_LEN(1))
789 i += le16_to_cpu(de->rec_len);
792 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
794 *f_version = inode->i_version;
797 while (!error && *f_pos < i_size_read(inode)
798 && offset < sb->s_blocksize) {
799 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
800 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
801 /* On error, skip the f_pos to the
803 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
807 offset += le16_to_cpu(de->rec_len);
808 if (le64_to_cpu(de->inode)) {
809 /* We might block in the next section
810 * if the data destination is
811 * currently swapped out. So, use a
812 * version stamp to detect whether or
813 * not the directory has been modified
814 * during the copy operation.
816 unsigned long version = *f_version;
817 unsigned char d_type = DT_UNKNOWN;
819 if (de->file_type < OCFS2_FT_MAX)
820 d_type = ocfs2_filetype_table[de->file_type];
821 error = filldir(priv, de->name,
824 le64_to_cpu(de->inode),
828 *filldir_err = error;
831 if (version != *f_version)
835 *f_pos += le16_to_cpu(de->rec_len);
847 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
848 loff_t *f_pos, void *priv, filldir_t filldir,
851 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
852 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
853 filldir, filldir_err);
855 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
860 * This is intended to be called from inside other kernel functions,
861 * so we fake some arguments.
863 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
866 int ret = 0, filldir_err = 0;
867 u64 version = inode->i_version;
869 while (*f_pos < i_size_read(inode)) {
870 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
871 filldir, &filldir_err);
872 if (ret || filldir_err)
886 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
889 struct inode *inode = filp->f_path.dentry->d_inode;
892 mlog_entry("dirino=%llu\n",
893 (unsigned long long)OCFS2_I(inode)->ip_blkno);
895 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
896 if (lock_level && error >= 0) {
897 /* We release EX lock which used to update atime
898 * and get PR lock again to reduce contention
899 * on commonly accessed directories. */
900 ocfs2_inode_unlock(inode, 1);
902 error = ocfs2_inode_lock(inode, NULL, 0);
905 if (error != -ENOENT)
907 /* we haven't got any yet, so propagate the error. */
911 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
912 dirent, filldir, NULL);
914 ocfs2_inode_unlock(inode, lock_level);
923 * NOTE: this should always be called with parent dir i_mutex taken.
925 int ocfs2_find_files_on_disk(const char *name,
929 struct buffer_head **dirent_bh,
930 struct ocfs2_dir_entry **dirent)
932 int status = -ENOENT;
934 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
935 namelen, name, blkno, inode, dirent_bh, dirent);
937 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
938 if (!*dirent_bh || !*dirent) {
943 *blkno = le64_to_cpu((*dirent)->inode);
958 * Convenience function for callers which just want the block number
959 * mapped to a name and don't require the full dirent info, etc.
961 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
962 int namelen, u64 *blkno)
965 struct buffer_head *bh = NULL;
966 struct ocfs2_dir_entry *dirent = NULL;
968 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
974 /* Check for a name within a directory.
976 * Return 0 if the name does not exist
977 * Return -EEXIST if the directory contains the name
979 * Callers should have i_mutex + a cluster lock on dir
981 int ocfs2_check_dir_for_entry(struct inode *dir,
986 struct buffer_head *dirent_bh = NULL;
987 struct ocfs2_dir_entry *dirent = NULL;
989 mlog_entry("dir %llu, name '%.*s'\n",
990 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
993 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1005 struct ocfs2_empty_dir_priv {
1007 unsigned seen_dot_dot;
1008 unsigned seen_other;
1010 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1011 loff_t pos, u64 ino, unsigned type)
1013 struct ocfs2_empty_dir_priv *p = priv;
1016 * Check the positions of "." and ".." records to be sure
1017 * they're in the correct place.
1019 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1024 if (name_len == 2 && !strncmp("..", name, 2) &&
1025 pos == OCFS2_DIR_REC_LEN(1)) {
1026 p->seen_dot_dot = 1;
1034 * routine to check that the specified directory is empty (for rmdir)
1036 * Returns 1 if dir is empty, zero otherwise.
1038 int ocfs2_empty_dir(struct inode *inode)
1042 struct ocfs2_empty_dir_priv priv;
1044 memset(&priv, 0, sizeof(priv));
1046 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1050 if (!priv.seen_dot || !priv.seen_dot_dot) {
1051 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1052 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1054 * XXX: Is it really safe to allow an unlink to continue?
1059 return !priv.seen_other;
1062 static void ocfs2_fill_initial_dirents(struct inode *inode,
1063 struct inode *parent,
1064 char *start, unsigned int size)
1066 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1068 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1071 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1072 strcpy(de->name, ".");
1073 ocfs2_set_de_type(de, S_IFDIR);
1075 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1076 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1077 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1079 strcpy(de->name, "..");
1080 ocfs2_set_de_type(de, S_IFDIR);
1084 * This works together with code in ocfs2_mknod_locked() which sets
1085 * the inline-data flag and initializes the inline-data section.
1087 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1089 struct inode *parent,
1090 struct inode *inode,
1091 struct buffer_head *di_bh)
1094 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1095 struct ocfs2_inline_data *data = &di->id2.i_data;
1096 unsigned int size = le16_to_cpu(data->id_count);
1098 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1099 OCFS2_JOURNAL_ACCESS_WRITE);
1105 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1107 ocfs2_journal_dirty(handle, di_bh);
1113 i_size_write(inode, size);
1115 inode->i_blocks = ocfs2_inode_sector_count(inode);
1117 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1125 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1127 struct inode *parent,
1128 struct inode *inode,
1129 struct buffer_head *fe_bh,
1130 struct ocfs2_alloc_context *data_ac)
1133 struct buffer_head *new_bh = NULL;
1137 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1138 data_ac, NULL, &new_bh);
1144 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1146 status = ocfs2_journal_access_db(handle, inode, new_bh,
1147 OCFS2_JOURNAL_ACCESS_CREATE);
1152 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1154 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1155 osb->sb->s_blocksize);
1157 status = ocfs2_journal_dirty(handle, new_bh);
1163 i_size_write(inode, inode->i_sb->s_blocksize);
1165 inode->i_blocks = ocfs2_inode_sector_count(inode);
1166 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1180 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1182 struct inode *parent,
1183 struct inode *inode,
1184 struct buffer_head *fe_bh,
1185 struct ocfs2_alloc_context *data_ac)
1187 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1189 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1190 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1192 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1196 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1197 unsigned int new_size)
1199 struct ocfs2_dir_entry *de;
1200 struct ocfs2_dir_entry *prev_de;
1201 char *de_buf, *limit;
1202 unsigned int bytes = new_size - old_size;
1204 limit = start + old_size;
1206 de = (struct ocfs2_dir_entry *)de_buf;
1209 de_buf += le16_to_cpu(de->rec_len);
1210 de = (struct ocfs2_dir_entry *)de_buf;
1211 } while (de_buf < limit);
1213 le16_add_cpu(&prev_de->rec_len, bytes);
1217 * We allocate enough clusters to fulfill "blocks_wanted", but set
1218 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1219 * rest automatically for us.
1221 * *first_block_bh is a pointer to the 1st data block allocated to the
1224 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1225 unsigned int blocks_wanted,
1226 struct buffer_head **first_block_bh)
1228 u32 alloc, bit_off, len;
1229 struct super_block *sb = dir->i_sb;
1230 int ret, credits = ocfs2_inline_to_extents_credits(sb);
1231 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1232 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1233 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1234 struct ocfs2_alloc_context *data_ac;
1235 struct buffer_head *dirdata_bh = NULL;
1236 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1238 struct ocfs2_extent_tree et;
1241 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1243 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1246 * We should never need more than 2 clusters for this -
1247 * maximum dirent size is far less than one block. In fact,
1248 * the only time we'd need more than one cluster is if
1249 * blocksize == clustersize and the dirent won't fit in the
1250 * extra space that the expansion to a single block gives. As
1251 * of today, that only happens on 4k/4k file systems.
1255 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1261 down_write(&oi->ip_alloc_sem);
1264 * Prepare for worst case allocation scenario of two separate
1268 credits += OCFS2_SUBALLOC_ALLOC;
1270 handle = ocfs2_start_trans(osb, credits);
1271 if (IS_ERR(handle)) {
1272 ret = PTR_ERR(handle);
1277 if (vfs_dq_alloc_space_nodirty(dir,
1278 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1284 * Try to claim as many clusters as the bitmap can give though
1285 * if we only get one now, that's enough to continue. The rest
1286 * will be claimed after the conversion to extents.
1288 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1295 * Operations are carefully ordered so that we set up the new
1296 * data block first. The conversion from inline data to
1299 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1300 dirdata_bh = sb_getblk(sb, blkno);
1307 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1309 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1310 OCFS2_JOURNAL_ACCESS_CREATE);
1316 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1317 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1318 sb->s_blocksize - i_size_read(dir));
1319 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1322 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1329 * Set extent, i_size, etc on the directory. After this, the
1330 * inode should contain the same exact dirents as before and
1331 * be fully accessible from system calls.
1333 * We let the later dirent insert modify c/mtime - to the user
1334 * the data hasn't changed.
1336 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1337 OCFS2_JOURNAL_ACCESS_CREATE);
1343 spin_lock(&oi->ip_lock);
1344 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1345 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1346 spin_unlock(&oi->ip_lock);
1348 ocfs2_dinode_new_extent_list(dir, di);
1350 i_size_write(dir, sb->s_blocksize);
1351 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1353 di->i_size = cpu_to_le64(sb->s_blocksize);
1354 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1355 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1358 * This should never fail as our extent list is empty and all
1359 * related blocks have been journaled already.
1361 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1369 * Set i_blocks after the extent insert for the most up to
1370 * date ip_clusters value.
1372 dir->i_blocks = ocfs2_inode_sector_count(dir);
1374 ret = ocfs2_journal_dirty(handle, di_bh);
1381 * We asked for two clusters, but only got one in the 1st
1382 * pass. Claim the 2nd cluster as a separate extent.
1385 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1391 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1393 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1394 blkno, len, 0, NULL);
1401 *first_block_bh = dirdata_bh;
1405 if (ret < 0 && did_quota)
1406 vfs_dq_free_space_nodirty(dir,
1407 ocfs2_clusters_to_bytes(osb->sb, 2));
1408 ocfs2_commit_trans(osb, handle);
1411 up_write(&oi->ip_alloc_sem);
1415 ocfs2_free_alloc_context(data_ac);
1422 /* returns a bh of the 1st new block in the allocation. */
1423 static int ocfs2_do_extend_dir(struct super_block *sb,
1426 struct buffer_head *parent_fe_bh,
1427 struct ocfs2_alloc_context *data_ac,
1428 struct ocfs2_alloc_context *meta_ac,
1429 struct buffer_head **new_bh)
1432 int extend, did_quota = 0;
1433 u64 p_blkno, v_blkno;
1435 spin_lock(&OCFS2_I(dir)->ip_lock);
1436 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1437 spin_unlock(&OCFS2_I(dir)->ip_lock);
1440 u32 offset = OCFS2_I(dir)->ip_clusters;
1442 if (vfs_dq_alloc_space_nodirty(dir,
1443 ocfs2_clusters_to_bytes(sb, 1))) {
1449 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1450 1, 0, parent_fe_bh, handle,
1451 data_ac, meta_ac, NULL);
1452 BUG_ON(status == -EAGAIN);
1459 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1460 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1466 *new_bh = sb_getblk(sb, p_blkno);
1474 if (did_quota && status < 0)
1475 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
1481 * Assumes you already have a cluster lock on the directory.
1483 * 'blocks_wanted' is only used if we have an inline directory which
1484 * is to be turned into an extent based one. The size of the dirent to
1485 * insert might be larger than the space gained by growing to just one
1486 * block, so we may have to grow the inode by two blocks in that case.
1488 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1490 struct buffer_head *parent_fe_bh,
1491 unsigned int blocks_wanted,
1492 struct buffer_head **new_de_bh)
1495 int credits, num_free_extents, drop_alloc_sem = 0;
1497 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1498 struct ocfs2_extent_list *el = &fe->id2.i_list;
1499 struct ocfs2_alloc_context *data_ac = NULL;
1500 struct ocfs2_alloc_context *meta_ac = NULL;
1501 handle_t *handle = NULL;
1502 struct buffer_head *new_bh = NULL;
1503 struct ocfs2_dir_entry * de;
1504 struct super_block *sb = osb->sb;
1505 struct ocfs2_extent_tree et;
1509 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1510 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1511 blocks_wanted, &new_bh);
1517 if (blocks_wanted == 1) {
1519 * If the new dirent will fit inside the space
1520 * created by pushing out to one block, then
1521 * we can complete the operation
1522 * here. Otherwise we have to expand i_size
1523 * and format the 2nd block below.
1525 BUG_ON(new_bh == NULL);
1530 * Get rid of 'new_bh' - we want to format the 2nd
1531 * data block and return that instead.
1536 dir_i_size = i_size_read(dir);
1537 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1541 dir_i_size = i_size_read(dir);
1542 mlog(0, "extending dir %llu (i_size = %lld)\n",
1543 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1545 /* dir->i_size is always block aligned. */
1546 spin_lock(&OCFS2_I(dir)->ip_lock);
1547 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1548 spin_unlock(&OCFS2_I(dir)->ip_lock);
1549 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1550 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1551 if (num_free_extents < 0) {
1552 status = num_free_extents;
1557 if (!num_free_extents) {
1558 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1560 if (status != -ENOSPC)
1566 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1568 if (status != -ENOSPC)
1573 credits = ocfs2_calc_extend_credits(sb, el, 1);
1575 spin_unlock(&OCFS2_I(dir)->ip_lock);
1576 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1580 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1583 handle = ocfs2_start_trans(osb, credits);
1584 if (IS_ERR(handle)) {
1585 status = PTR_ERR(handle);
1591 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1592 data_ac, meta_ac, &new_bh);
1598 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1600 status = ocfs2_journal_access_db(handle, dir, new_bh,
1601 OCFS2_JOURNAL_ACCESS_CREATE);
1606 memset(new_bh->b_data, 0, sb->s_blocksize);
1607 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1609 de->rec_len = cpu_to_le16(sb->s_blocksize);
1610 status = ocfs2_journal_dirty(handle, new_bh);
1616 dir_i_size += dir->i_sb->s_blocksize;
1617 i_size_write(dir, dir_i_size);
1618 dir->i_blocks = ocfs2_inode_sector_count(dir);
1619 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1626 *new_de_bh = new_bh;
1630 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1632 ocfs2_commit_trans(osb, handle);
1635 ocfs2_free_alloc_context(data_ac);
1637 ocfs2_free_alloc_context(meta_ac);
1645 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1646 const char *name, int namelen,
1647 struct buffer_head **ret_de_bh,
1648 unsigned int *blocks_wanted)
1651 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1652 struct ocfs2_dir_entry *de, *last_de = NULL;
1653 char *de_buf, *limit;
1654 unsigned long offset = 0;
1655 unsigned int rec_len, new_rec_len;
1657 de_buf = di->id2.i_data.id_data;
1658 limit = de_buf + i_size_read(dir);
1659 rec_len = OCFS2_DIR_REC_LEN(namelen);
1661 while (de_buf < limit) {
1662 de = (struct ocfs2_dir_entry *)de_buf;
1664 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1668 if (ocfs2_match(namelen, name, de)) {
1672 if (ocfs2_dirent_would_fit(de, rec_len)) {
1673 /* Ok, we found a spot. Return this bh and let
1674 * the caller actually fill it in. */
1682 de_buf += le16_to_cpu(de->rec_len);
1683 offset += le16_to_cpu(de->rec_len);
1687 * We're going to require expansion of the directory - figure
1688 * out how many blocks we'll need so that a place for the
1689 * dirent can be found.
1692 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1693 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1701 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1702 int namelen, struct buffer_head **ret_de_bh)
1704 unsigned long offset;
1705 struct buffer_head *bh = NULL;
1706 unsigned short rec_len;
1707 struct ocfs2_dir_entry *de;
1708 struct super_block *sb = dir->i_sb;
1711 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1717 rec_len = OCFS2_DIR_REC_LEN(namelen);
1719 de = (struct ocfs2_dir_entry *) bh->b_data;
1721 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1725 if (i_size_read(dir) <= offset) {
1727 * Caller will have to expand this
1733 status = ocfs2_read_dir_block(dir,
1734 offset >> sb->s_blocksize_bits,
1740 /* move to next block */
1741 de = (struct ocfs2_dir_entry *) bh->b_data;
1743 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1747 if (ocfs2_match(namelen, name, de)) {
1751 if (ocfs2_dirent_would_fit(de, rec_len)) {
1752 /* Ok, we found a spot. Return this bh and let
1753 * the caller actually fill it in. */
1759 offset += le16_to_cpu(de->rec_len);
1760 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1771 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1773 struct buffer_head *parent_fe_bh,
1776 struct buffer_head **ret_de_bh)
1779 unsigned int blocks_wanted = 1;
1780 struct buffer_head *bh = NULL;
1782 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1783 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1793 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1794 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1795 namelen, &bh, &blocks_wanted);
1797 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1799 if (ret && ret != -ENOSPC) {
1804 if (ret == -ENOSPC) {
1806 * We have to expand the directory to add this name.
1810 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,