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>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
52 #include "extent_map.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,
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,
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,
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,
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)
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,
131 const char *name, int namelen,
132 unsigned long offset,
135 struct ocfs2_dir_entry **res_dir)
137 struct ocfs2_dir_entry *de;
138 char *dlimit, *de_buf;
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)) {
165 /* prevent looping on a bad block */
166 de_len = le16_to_cpu(de->rec_len);
181 static struct buffer_head *ocfs2_find_entry_id(const char *name,
184 struct ocfs2_dir_entry **res_dir)
187 struct buffer_head *di_bh = NULL;
188 struct ocfs2_dinode *di;
189 struct ocfs2_inline_data *data;
191 ret = ocfs2_read_inode_block(dir, &di_bh);
197 di = (struct ocfs2_dinode *)di_bh->b_data;
198 data = &di->id2.i_data;
200 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
201 data->id_data, i_size_read(dir), res_dir);
210 static int ocfs2_validate_dir_block(struct super_block *sb,
211 struct buffer_head *bh)
214 * Nothing yet. We don't validate dirents here, that's handled
215 * in-place when the code walks them.
217 mlog(0, "Validating dirblock %llu\n",
218 (unsigned long long)bh->b_blocknr);
224 * This function forces all errors to -EIO for consistency with its
225 * predecessor, ocfs2_bread(). We haven't audited what returning the
226 * real error codes would do to callers. We log the real codes with
227 * mlog_errno() before we squash them.
229 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
230 struct buffer_head **bh, int flags)
233 struct buffer_head *tmp = *bh;
235 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
236 ocfs2_validate_dir_block);
240 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
244 return rc ? -EIO : 0;
247 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
249 struct ocfs2_dir_entry **res_dir)
251 struct super_block *sb;
252 struct buffer_head *bh_use[NAMEI_RA_SIZE];
253 struct buffer_head *bh, *ret = NULL;
254 unsigned long start, block, b;
255 int ra_max = 0; /* Number of bh's in the readahead
257 int ra_ptr = 0; /* Current index into readahead
266 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
267 start = OCFS2_I(dir)->ip_dir_start_lookup;
268 if (start >= nblocks)
275 * We deal with the read-ahead logic here.
277 if (ra_ptr >= ra_max) {
278 /* Refill the readahead buffer */
281 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
283 * Terminate if we reach the end of the
284 * directory and must wrap, or if our
285 * search has finished at this block.
287 if (b >= nblocks || (num && block == start)) {
288 bh_use[ra_max] = NULL;
294 err = ocfs2_read_dir_block(dir, b++, &bh,
299 if ((bh = bh_use[ra_ptr++]) == NULL)
301 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
302 /* read error, skip block & hope for the best.
303 * ocfs2_read_dir_block() has released the bh. */
304 ocfs2_error(dir->i_sb, "reading directory %llu, "
306 (unsigned long long)OCFS2_I(dir)->ip_blkno,
310 i = ocfs2_search_dirblock(bh, dir, name, namelen,
311 block << sb->s_blocksize_bits,
312 bh->b_data, sb->s_blocksize,
315 OCFS2_I(dir)->ip_dir_start_lookup = block;
317 goto cleanup_and_exit;
321 goto cleanup_and_exit;
324 if (++block >= nblocks)
326 } while (block != start);
329 * If the directory has grown while we were searching, then
330 * search the last part of the directory before giving up.
333 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
334 if (block < nblocks) {
340 /* Clean up the read-ahead blocks */
341 for (; ra_ptr < ra_max; ra_ptr++)
342 brelse(bh_use[ra_ptr]);
349 * Try to find an entry of the provided name within 'dir'.
351 * If nothing was found, NULL is returned. Otherwise, a buffer_head
352 * and pointer to the dir entry are passed back.
354 * Caller can NOT assume anything about the contents of the
355 * buffer_head - it is passed back only so that it can be passed into
356 * any one of the manipulation functions (add entry, delete entry,
357 * etc). As an example, bh in the extent directory case is a data
358 * block, in the inline-data case it actually points to an inode.
360 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
362 struct ocfs2_dir_entry **res_dir)
366 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
367 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
369 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
373 * Update inode number and type of a previously found directory entry.
375 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
376 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
377 struct inode *new_entry_inode)
382 * The same code works fine for both inline-data and extent
383 * based directories, so no need to split this up.
386 ret = ocfs2_journal_access(handle, dir, de_bh,
387 OCFS2_JOURNAL_ACCESS_WRITE);
393 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
394 ocfs2_set_de_type(de, new_entry_inode->i_mode);
396 ocfs2_journal_dirty(handle, de_bh);
402 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
403 struct ocfs2_dir_entry *de_del,
404 struct buffer_head *bh, char *first_de,
407 struct ocfs2_dir_entry *de, *pde;
408 int i, status = -ENOENT;
410 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
414 de = (struct ocfs2_dir_entry *) first_de;
416 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
422 status = ocfs2_journal_access(handle, dir, bh,
423 OCFS2_JOURNAL_ACCESS_WRITE);
430 le16_add_cpu(&pde->rec_len,
431 le16_to_cpu(de->rec_len));
435 status = ocfs2_journal_dirty(handle, bh);
438 i += le16_to_cpu(de->rec_len);
440 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
447 static inline int ocfs2_delete_entry_id(handle_t *handle,
449 struct ocfs2_dir_entry *de_del,
450 struct buffer_head *bh)
453 struct buffer_head *di_bh = NULL;
454 struct ocfs2_dinode *di;
455 struct ocfs2_inline_data *data;
457 ret = ocfs2_read_inode_block(dir, &di_bh);
463 di = (struct ocfs2_dinode *)di_bh->b_data;
464 data = &di->id2.i_data;
466 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
474 static inline int ocfs2_delete_entry_el(handle_t *handle,
476 struct ocfs2_dir_entry *de_del,
477 struct buffer_head *bh)
479 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
484 * ocfs2_delete_entry deletes a directory entry by merging it with the
487 int ocfs2_delete_entry(handle_t *handle,
489 struct ocfs2_dir_entry *de_del,
490 struct buffer_head *bh)
492 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
493 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
495 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
499 * Check whether 'de' has enough room to hold an entry of
500 * 'new_rec_len' bytes.
502 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
503 unsigned int new_rec_len)
505 unsigned int de_really_used;
507 /* Check whether this is an empty record with enough space */
508 if (le64_to_cpu(de->inode) == 0 &&
509 le16_to_cpu(de->rec_len) >= new_rec_len)
513 * Record might have free space at the end which we can
516 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
517 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
523 /* we don't always have a dentry for what we want to add, so people
524 * like orphan dir can call this instead.
526 * If you pass me insert_bh, I'll skip the search of the other dir
527 * blocks and put the record in there.
529 int __ocfs2_add_entry(handle_t *handle,
531 const char *name, int namelen,
532 struct inode *inode, u64 blkno,
533 struct buffer_head *parent_fe_bh,
534 struct buffer_head *insert_bh)
536 unsigned long offset;
537 unsigned short rec_len;
538 struct ocfs2_dir_entry *de, *de1;
539 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
540 struct super_block *sb = dir->i_sb;
542 unsigned int size = sb->s_blocksize;
543 char *data_start = insert_bh->b_data;
550 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
551 data_start = di->id2.i_data.id_data;
552 size = i_size_read(dir);
554 BUG_ON(insert_bh != parent_fe_bh);
557 rec_len = OCFS2_DIR_REC_LEN(namelen);
559 de = (struct ocfs2_dir_entry *) data_start;
561 BUG_ON((char *)de >= (size + data_start));
563 /* These checks should've already been passed by the
564 * prepare function, but I guess we can leave them
566 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
570 if (ocfs2_match(namelen, name, de)) {
575 if (ocfs2_dirent_would_fit(de, rec_len)) {
576 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
577 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
583 status = ocfs2_journal_access(handle, dir, insert_bh,
584 OCFS2_JOURNAL_ACCESS_WRITE);
585 /* By now the buffer is marked for journaling */
586 offset += le16_to_cpu(de->rec_len);
587 if (le64_to_cpu(de->inode)) {
588 de1 = (struct ocfs2_dir_entry *)((char *) de +
589 OCFS2_DIR_REC_LEN(de->name_len));
591 cpu_to_le16(le16_to_cpu(de->rec_len) -
592 OCFS2_DIR_REC_LEN(de->name_len));
593 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
596 de->file_type = OCFS2_FT_UNKNOWN;
598 de->inode = cpu_to_le64(blkno);
599 ocfs2_set_de_type(de, inode->i_mode);
602 de->name_len = namelen;
603 memcpy(de->name, name, namelen);
606 status = ocfs2_journal_dirty(handle, insert_bh);
610 offset += le16_to_cpu(de->rec_len);
611 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
614 /* when you think about it, the assert above should prevent us
615 * from ever getting here. */
623 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
625 loff_t *f_pos, void *priv,
626 filldir_t filldir, int *filldir_err)
628 int ret, i, filldir_ret;
629 unsigned long offset = *f_pos;
630 struct buffer_head *di_bh = NULL;
631 struct ocfs2_dinode *di;
632 struct ocfs2_inline_data *data;
633 struct ocfs2_dir_entry *de;
635 ret = ocfs2_read_inode_block(inode, &di_bh);
637 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
638 (unsigned long long)OCFS2_I(inode)->ip_blkno);
642 di = (struct ocfs2_dinode *)di_bh->b_data;
643 data = &di->id2.i_data;
645 while (*f_pos < i_size_read(inode)) {
647 /* If the dir block has changed since the last call to
648 * readdir(2), then we might be pointing to an invalid
649 * dirent right now. Scan from the start of the block
651 if (*f_version != inode->i_version) {
652 for (i = 0; i < i_size_read(inode) && i < offset; ) {
653 de = (struct ocfs2_dir_entry *)
655 /* It's too expensive to do a full
656 * dirent test each time round this
657 * loop, but we do have to test at
658 * least that it is non-zero. A
659 * failure will be detected in the
660 * dirent test below. */
661 if (le16_to_cpu(de->rec_len) <
662 OCFS2_DIR_REC_LEN(1))
664 i += le16_to_cpu(de->rec_len);
667 *f_version = inode->i_version;
670 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
671 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
672 /* On error, skip the f_pos to the end. */
673 *f_pos = i_size_read(inode);
676 offset += le16_to_cpu(de->rec_len);
677 if (le64_to_cpu(de->inode)) {
678 /* We might block in the next section
679 * if the data destination is
680 * currently swapped out. So, use a
681 * version stamp to detect whether or
682 * not the directory has been modified
683 * during the copy operation.
685 u64 version = *f_version;
686 unsigned char d_type = DT_UNKNOWN;
688 if (de->file_type < OCFS2_FT_MAX)
689 d_type = ocfs2_filetype_table[de->file_type];
691 filldir_ret = filldir(priv, de->name,
694 le64_to_cpu(de->inode),
698 *filldir_err = filldir_ret;
701 if (version != *f_version)
704 *f_pos += le16_to_cpu(de->rec_len);
713 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
715 loff_t *f_pos, void *priv,
716 filldir_t filldir, int *filldir_err)
719 unsigned long offset, blk, last_ra_blk = 0;
721 struct buffer_head * bh, * tmp;
722 struct ocfs2_dir_entry * de;
723 struct super_block * sb = inode->i_sb;
724 unsigned int ra_sectors = 16;
729 offset = (*f_pos) & (sb->s_blocksize - 1);
731 while (!error && !stored && *f_pos < i_size_read(inode)) {
732 blk = (*f_pos) >> sb->s_blocksize_bits;
733 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
734 /* Skip the corrupt dirblock and keep trying */
735 *f_pos += sb->s_blocksize - offset;
739 /* The idea here is to begin with 8k read-ahead and to stay
740 * 4k ahead of our current position.
742 * TODO: Use the pagecache for this. We just need to
743 * make sure it's cluster-safe... */
745 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
746 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
749 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
758 /* If the dir block has changed since the last call to
759 * readdir(2), then we might be pointing to an invalid
760 * dirent right now. Scan from the start of the block
762 if (*f_version != inode->i_version) {
763 for (i = 0; i < sb->s_blocksize && i < offset; ) {
764 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
765 /* It's too expensive to do a full
766 * dirent test each time round this
767 * loop, but we do have to test at
768 * least that it is non-zero. A
769 * failure will be detected in the
770 * dirent test below. */
771 if (le16_to_cpu(de->rec_len) <
772 OCFS2_DIR_REC_LEN(1))
774 i += le16_to_cpu(de->rec_len);
777 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
779 *f_version = inode->i_version;
782 while (!error && *f_pos < i_size_read(inode)
783 && offset < sb->s_blocksize) {
784 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
785 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
786 /* On error, skip the f_pos to the
788 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
792 offset += le16_to_cpu(de->rec_len);
793 if (le64_to_cpu(de->inode)) {
794 /* We might block in the next section
795 * if the data destination is
796 * currently swapped out. So, use a
797 * version stamp to detect whether or
798 * not the directory has been modified
799 * during the copy operation.
801 unsigned long version = *f_version;
802 unsigned char d_type = DT_UNKNOWN;
804 if (de->file_type < OCFS2_FT_MAX)
805 d_type = ocfs2_filetype_table[de->file_type];
806 error = filldir(priv, de->name,
809 le64_to_cpu(de->inode),
813 *filldir_err = error;
816 if (version != *f_version)
820 *f_pos += le16_to_cpu(de->rec_len);
832 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
833 loff_t *f_pos, void *priv, filldir_t filldir,
836 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
837 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
838 filldir, filldir_err);
840 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
845 * This is intended to be called from inside other kernel functions,
846 * so we fake some arguments.
848 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
851 int ret = 0, filldir_err = 0;
852 u64 version = inode->i_version;
854 while (*f_pos < i_size_read(inode)) {
855 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
856 filldir, &filldir_err);
857 if (ret || filldir_err)
871 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
874 struct inode *inode = filp->f_path.dentry->d_inode;
877 mlog_entry("dirino=%llu\n",
878 (unsigned long long)OCFS2_I(inode)->ip_blkno);
880 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
881 if (lock_level && error >= 0) {
882 /* We release EX lock which used to update atime
883 * and get PR lock again to reduce contention
884 * on commonly accessed directories. */
885 ocfs2_inode_unlock(inode, 1);
887 error = ocfs2_inode_lock(inode, NULL, 0);
890 if (error != -ENOENT)
892 /* we haven't got any yet, so propagate the error. */
896 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
897 dirent, filldir, NULL);
899 ocfs2_inode_unlock(inode, lock_level);
908 * NOTE: this should always be called with parent dir i_mutex taken.
910 int ocfs2_find_files_on_disk(const char *name,
914 struct buffer_head **dirent_bh,
915 struct ocfs2_dir_entry **dirent)
917 int status = -ENOENT;
919 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
920 namelen, name, blkno, inode, dirent_bh, dirent);
922 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
923 if (!*dirent_bh || !*dirent) {
928 *blkno = le64_to_cpu((*dirent)->inode);
943 * Convenience function for callers which just want the block number
944 * mapped to a name and don't require the full dirent info, etc.
946 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
947 int namelen, u64 *blkno)
950 struct buffer_head *bh = NULL;
951 struct ocfs2_dir_entry *dirent = NULL;
953 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
959 /* Check for a name within a directory.
961 * Return 0 if the name does not exist
962 * Return -EEXIST if the directory contains the name
964 * Callers should have i_mutex + a cluster lock on dir
966 int ocfs2_check_dir_for_entry(struct inode *dir,
971 struct buffer_head *dirent_bh = NULL;
972 struct ocfs2_dir_entry *dirent = NULL;
974 mlog_entry("dir %llu, name '%.*s'\n",
975 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
978 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
990 struct ocfs2_empty_dir_priv {
992 unsigned seen_dot_dot;
995 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
996 loff_t pos, u64 ino, unsigned type)
998 struct ocfs2_empty_dir_priv *p = priv;
1001 * Check the positions of "." and ".." records to be sure
1002 * they're in the correct place.
1004 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1009 if (name_len == 2 && !strncmp("..", name, 2) &&
1010 pos == OCFS2_DIR_REC_LEN(1)) {
1011 p->seen_dot_dot = 1;
1019 * routine to check that the specified directory is empty (for rmdir)
1021 * Returns 1 if dir is empty, zero otherwise.
1023 int ocfs2_empty_dir(struct inode *inode)
1027 struct ocfs2_empty_dir_priv priv;
1029 memset(&priv, 0, sizeof(priv));
1031 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1035 if (!priv.seen_dot || !priv.seen_dot_dot) {
1036 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1037 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1039 * XXX: Is it really safe to allow an unlink to continue?
1044 return !priv.seen_other;
1047 static void ocfs2_fill_initial_dirents(struct inode *inode,
1048 struct inode *parent,
1049 char *start, unsigned int size)
1051 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1053 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1056 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1057 strcpy(de->name, ".");
1058 ocfs2_set_de_type(de, S_IFDIR);
1060 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1061 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1062 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1064 strcpy(de->name, "..");
1065 ocfs2_set_de_type(de, S_IFDIR);
1069 * This works together with code in ocfs2_mknod_locked() which sets
1070 * the inline-data flag and initializes the inline-data section.
1072 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1074 struct inode *parent,
1075 struct inode *inode,
1076 struct buffer_head *di_bh)
1079 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1080 struct ocfs2_inline_data *data = &di->id2.i_data;
1081 unsigned int size = le16_to_cpu(data->id_count);
1083 ret = ocfs2_journal_access(handle, inode, di_bh,
1084 OCFS2_JOURNAL_ACCESS_WRITE);
1090 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1092 ocfs2_journal_dirty(handle, di_bh);
1098 i_size_write(inode, size);
1100 inode->i_blocks = ocfs2_inode_sector_count(inode);
1102 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1110 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1112 struct inode *parent,
1113 struct inode *inode,
1114 struct buffer_head *fe_bh,
1115 struct ocfs2_alloc_context *data_ac)
1118 struct buffer_head *new_bh = NULL;
1122 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1123 data_ac, NULL, &new_bh);
1129 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1131 status = ocfs2_journal_access(handle, inode, new_bh,
1132 OCFS2_JOURNAL_ACCESS_CREATE);
1137 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1139 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1140 osb->sb->s_blocksize);
1142 status = ocfs2_journal_dirty(handle, new_bh);
1148 i_size_write(inode, inode->i_sb->s_blocksize);
1150 inode->i_blocks = ocfs2_inode_sector_count(inode);
1151 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1165 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1167 struct inode *parent,
1168 struct inode *inode,
1169 struct buffer_head *fe_bh,
1170 struct ocfs2_alloc_context *data_ac)
1172 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1174 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1175 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1177 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1181 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1182 unsigned int new_size)
1184 struct ocfs2_dir_entry *de;
1185 struct ocfs2_dir_entry *prev_de;
1186 char *de_buf, *limit;
1187 unsigned int bytes = new_size - old_size;
1189 limit = start + old_size;
1191 de = (struct ocfs2_dir_entry *)de_buf;
1194 de_buf += le16_to_cpu(de->rec_len);
1195 de = (struct ocfs2_dir_entry *)de_buf;
1196 } while (de_buf < limit);
1198 le16_add_cpu(&prev_de->rec_len, bytes);
1202 * We allocate enough clusters to fulfill "blocks_wanted", but set
1203 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1204 * rest automatically for us.
1206 * *first_block_bh is a pointer to the 1st data block allocated to the
1209 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1210 unsigned int blocks_wanted,
1211 struct buffer_head **first_block_bh)
1213 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1214 u32 alloc, bit_off, len;
1215 struct super_block *sb = dir->i_sb;
1216 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1217 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1218 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1219 struct ocfs2_alloc_context *data_ac;
1220 struct buffer_head *dirdata_bh = NULL;
1221 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1223 struct ocfs2_extent_tree et;
1225 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1227 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1230 * We should never need more than 2 clusters for this -
1231 * maximum dirent size is far less than one block. In fact,
1232 * the only time we'd need more than one cluster is if
1233 * blocksize == clustersize and the dirent won't fit in the
1234 * extra space that the expansion to a single block gives. As
1235 * of today, that only happens on 4k/4k file systems.
1239 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1245 down_write(&oi->ip_alloc_sem);
1248 * Prepare for worst case allocation scenario of two separate
1252 credits += OCFS2_SUBALLOC_ALLOC;
1254 handle = ocfs2_start_trans(osb, credits);
1255 if (IS_ERR(handle)) {
1256 ret = PTR_ERR(handle);
1262 * Try to claim as many clusters as the bitmap can give though
1263 * if we only get one now, that's enough to continue. The rest
1264 * will be claimed after the conversion to extents.
1266 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1273 * Operations are carefully ordered so that we set up the new
1274 * data block first. The conversion from inline data to
1277 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1278 dirdata_bh = sb_getblk(sb, blkno);
1285 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1287 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1288 OCFS2_JOURNAL_ACCESS_CREATE);
1294 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1295 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1296 sb->s_blocksize - i_size_read(dir));
1297 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1300 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1307 * Set extent, i_size, etc on the directory. After this, the
1308 * inode should contain the same exact dirents as before and
1309 * be fully accessible from system calls.
1311 * We let the later dirent insert modify c/mtime - to the user
1312 * the data hasn't changed.
1314 ret = ocfs2_journal_access(handle, dir, di_bh,
1315 OCFS2_JOURNAL_ACCESS_CREATE);
1321 spin_lock(&oi->ip_lock);
1322 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1323 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1324 spin_unlock(&oi->ip_lock);
1326 ocfs2_dinode_new_extent_list(dir, di);
1328 i_size_write(dir, sb->s_blocksize);
1329 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1331 di->i_size = cpu_to_le64(sb->s_blocksize);
1332 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1333 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1336 * This should never fail as our extent list is empty and all
1337 * related blocks have been journaled already.
1339 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1347 * Set i_blocks after the extent insert for the most up to
1348 * date ip_clusters value.
1350 dir->i_blocks = ocfs2_inode_sector_count(dir);
1352 ret = ocfs2_journal_dirty(handle, di_bh);
1359 * We asked for two clusters, but only got one in the 1st
1360 * pass. Claim the 2nd cluster as a separate extent.
1363 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1369 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1371 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1372 blkno, len, 0, NULL);
1379 *first_block_bh = dirdata_bh;
1383 ocfs2_commit_trans(osb, handle);
1386 up_write(&oi->ip_alloc_sem);
1390 ocfs2_free_alloc_context(data_ac);
1397 /* returns a bh of the 1st new block in the allocation. */
1398 static int ocfs2_do_extend_dir(struct super_block *sb,
1401 struct buffer_head *parent_fe_bh,
1402 struct ocfs2_alloc_context *data_ac,
1403 struct ocfs2_alloc_context *meta_ac,
1404 struct buffer_head **new_bh)
1408 u64 p_blkno, v_blkno;
1410 spin_lock(&OCFS2_I(dir)->ip_lock);
1411 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1412 spin_unlock(&OCFS2_I(dir)->ip_lock);
1415 u32 offset = OCFS2_I(dir)->ip_clusters;
1417 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1418 1, 0, parent_fe_bh, handle,
1419 data_ac, meta_ac, NULL);
1420 BUG_ON(status == -EAGAIN);
1427 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1428 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1434 *new_bh = sb_getblk(sb, p_blkno);
1447 * Assumes you already have a cluster lock on the directory.
1449 * 'blocks_wanted' is only used if we have an inline directory which
1450 * is to be turned into an extent based one. The size of the dirent to
1451 * insert might be larger than the space gained by growing to just one
1452 * block, so we may have to grow the inode by two blocks in that case.
1454 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1456 struct buffer_head *parent_fe_bh,
1457 unsigned int blocks_wanted,
1458 struct buffer_head **new_de_bh)
1461 int credits, num_free_extents, drop_alloc_sem = 0;
1463 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1464 struct ocfs2_extent_list *el = &fe->id2.i_list;
1465 struct ocfs2_alloc_context *data_ac = NULL;
1466 struct ocfs2_alloc_context *meta_ac = NULL;
1467 handle_t *handle = NULL;
1468 struct buffer_head *new_bh = NULL;
1469 struct ocfs2_dir_entry * de;
1470 struct super_block *sb = osb->sb;
1471 struct ocfs2_extent_tree et;
1475 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1476 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1477 blocks_wanted, &new_bh);
1483 if (blocks_wanted == 1) {
1485 * If the new dirent will fit inside the space
1486 * created by pushing out to one block, then
1487 * we can complete the operation
1488 * here. Otherwise we have to expand i_size
1489 * and format the 2nd block below.
1491 BUG_ON(new_bh == NULL);
1496 * Get rid of 'new_bh' - we want to format the 2nd
1497 * data block and return that instead.
1502 dir_i_size = i_size_read(dir);
1503 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1507 dir_i_size = i_size_read(dir);
1508 mlog(0, "extending dir %llu (i_size = %lld)\n",
1509 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1511 /* dir->i_size is always block aligned. */
1512 spin_lock(&OCFS2_I(dir)->ip_lock);
1513 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1514 spin_unlock(&OCFS2_I(dir)->ip_lock);
1515 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1516 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1517 if (num_free_extents < 0) {
1518 status = num_free_extents;
1523 if (!num_free_extents) {
1524 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1526 if (status != -ENOSPC)
1532 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1534 if (status != -ENOSPC)
1539 credits = ocfs2_calc_extend_credits(sb, el, 1);
1541 spin_unlock(&OCFS2_I(dir)->ip_lock);
1542 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1546 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1549 handle = ocfs2_start_trans(osb, credits);
1550 if (IS_ERR(handle)) {
1551 status = PTR_ERR(handle);
1557 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1558 data_ac, meta_ac, &new_bh);
1564 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1566 status = ocfs2_journal_access(handle, dir, new_bh,
1567 OCFS2_JOURNAL_ACCESS_CREATE);
1572 memset(new_bh->b_data, 0, sb->s_blocksize);
1573 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1575 de->rec_len = cpu_to_le16(sb->s_blocksize);
1576 status = ocfs2_journal_dirty(handle, new_bh);
1582 dir_i_size += dir->i_sb->s_blocksize;
1583 i_size_write(dir, dir_i_size);
1584 dir->i_blocks = ocfs2_inode_sector_count(dir);
1585 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1592 *new_de_bh = new_bh;
1596 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1598 ocfs2_commit_trans(osb, handle);
1601 ocfs2_free_alloc_context(data_ac);
1603 ocfs2_free_alloc_context(meta_ac);
1611 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1612 const char *name, int namelen,
1613 struct buffer_head **ret_de_bh,
1614 unsigned int *blocks_wanted)
1617 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1618 struct ocfs2_dir_entry *de, *last_de = NULL;
1619 char *de_buf, *limit;
1620 unsigned long offset = 0;
1621 unsigned int rec_len, new_rec_len;
1623 de_buf = di->id2.i_data.id_data;
1624 limit = de_buf + i_size_read(dir);
1625 rec_len = OCFS2_DIR_REC_LEN(namelen);
1627 while (de_buf < limit) {
1628 de = (struct ocfs2_dir_entry *)de_buf;
1630 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1634 if (ocfs2_match(namelen, name, de)) {
1638 if (ocfs2_dirent_would_fit(de, rec_len)) {
1639 /* Ok, we found a spot. Return this bh and let
1640 * the caller actually fill it in. */
1648 de_buf += le16_to_cpu(de->rec_len);
1649 offset += le16_to_cpu(de->rec_len);
1653 * We're going to require expansion of the directory - figure
1654 * out how many blocks we'll need so that a place for the
1655 * dirent can be found.
1658 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1659 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1667 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1668 int namelen, struct buffer_head **ret_de_bh)
1670 unsigned long offset;
1671 struct buffer_head *bh = NULL;
1672 unsigned short rec_len;
1673 struct ocfs2_dir_entry *de;
1674 struct super_block *sb = dir->i_sb;
1677 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1683 rec_len = OCFS2_DIR_REC_LEN(namelen);
1685 de = (struct ocfs2_dir_entry *) bh->b_data;
1687 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1691 if (i_size_read(dir) <= offset) {
1693 * Caller will have to expand this
1699 status = ocfs2_read_dir_block(dir,
1700 offset >> sb->s_blocksize_bits,
1706 /* move to next block */
1707 de = (struct ocfs2_dir_entry *) bh->b_data;
1709 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1713 if (ocfs2_match(namelen, name, de)) {
1717 if (ocfs2_dirent_would_fit(de, rec_len)) {
1718 /* Ok, we found a spot. Return this bh and let
1719 * the caller actually fill it in. */
1725 offset += le16_to_cpu(de->rec_len);
1726 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1737 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1739 struct buffer_head *parent_fe_bh,
1742 struct buffer_head **ret_de_bh)
1745 unsigned int blocks_wanted = 1;
1746 struct buffer_head *bh = NULL;
1748 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1749 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1759 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1760 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1761 namelen, &bh, &blocks_wanted);
1763 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1765 if (ret && ret != -ENOSPC) {
1770 if (ret == -ENOSPC) {
1772 * We have to expand the directory to add this name.
1776 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,