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;
236 if (((u64)v_block << inode->i_sb->s_blocksize_bits) >=
237 i_size_read(inode)) {
238 BUG_ON(!(flags & OCFS2_BH_READAHEAD));
242 down_read(&OCFS2_I(inode)->ip_alloc_sem);
243 rc = ocfs2_extent_map_get_blocks(inode, v_block, &p_blkno, NULL,
245 up_read(&OCFS2_I(inode)->ip_alloc_sem);
254 "Directory #%llu contains a hole at offset %llu\n",
255 (unsigned long long)OCFS2_I(inode)->ip_blkno,
256 (unsigned long long)v_block << inode->i_sb->s_blocksize_bits);
260 rc = ocfs2_read_blocks(inode, p_blkno, 1, &tmp, flags,
261 ocfs2_validate_dir_block);
267 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
272 return rc ? -EIO : 0;
275 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
277 struct ocfs2_dir_entry **res_dir)
279 struct super_block *sb;
280 struct buffer_head *bh_use[NAMEI_RA_SIZE];
281 struct buffer_head *bh, *ret = NULL;
282 unsigned long start, block, b;
283 int ra_max = 0; /* Number of bh's in the readahead
285 int ra_ptr = 0; /* Current index into readahead
294 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
295 start = OCFS2_I(dir)->ip_dir_start_lookup;
296 if (start >= nblocks)
303 * We deal with the read-ahead logic here.
305 if (ra_ptr >= ra_max) {
306 /* Refill the readahead buffer */
309 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
311 * Terminate if we reach the end of the
312 * directory and must wrap, or if our
313 * search has finished at this block.
315 if (b >= nblocks || (num && block == start)) {
316 bh_use[ra_max] = NULL;
322 err = ocfs2_read_dir_block(dir, b++, &bh,
327 if ((bh = bh_use[ra_ptr++]) == NULL)
329 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
330 /* read error, skip block & hope for the best.
331 * ocfs2_read_dir_block() has released the bh. */
332 ocfs2_error(dir->i_sb, "reading directory %llu, "
334 (unsigned long long)OCFS2_I(dir)->ip_blkno,
338 i = ocfs2_search_dirblock(bh, dir, name, namelen,
339 block << sb->s_blocksize_bits,
340 bh->b_data, sb->s_blocksize,
343 OCFS2_I(dir)->ip_dir_start_lookup = block;
345 goto cleanup_and_exit;
349 goto cleanup_and_exit;
352 if (++block >= nblocks)
354 } while (block != start);
357 * If the directory has grown while we were searching, then
358 * search the last part of the directory before giving up.
361 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
362 if (block < nblocks) {
368 /* Clean up the read-ahead blocks */
369 for (; ra_ptr < ra_max; ra_ptr++)
370 brelse(bh_use[ra_ptr]);
377 * Try to find an entry of the provided name within 'dir'.
379 * If nothing was found, NULL is returned. Otherwise, a buffer_head
380 * and pointer to the dir entry are passed back.
382 * Caller can NOT assume anything about the contents of the
383 * buffer_head - it is passed back only so that it can be passed into
384 * any one of the manipulation functions (add entry, delete entry,
385 * etc). As an example, bh in the extent directory case is a data
386 * block, in the inline-data case it actually points to an inode.
388 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
390 struct ocfs2_dir_entry **res_dir)
394 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
395 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
397 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
401 * Update inode number and type of a previously found directory entry.
403 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
404 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
405 struct inode *new_entry_inode)
410 * The same code works fine for both inline-data and extent
411 * based directories, so no need to split this up.
414 ret = ocfs2_journal_access(handle, dir, de_bh,
415 OCFS2_JOURNAL_ACCESS_WRITE);
421 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
422 ocfs2_set_de_type(de, new_entry_inode->i_mode);
424 ocfs2_journal_dirty(handle, de_bh);
430 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
431 struct ocfs2_dir_entry *de_del,
432 struct buffer_head *bh, char *first_de,
435 struct ocfs2_dir_entry *de, *pde;
436 int i, status = -ENOENT;
438 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
442 de = (struct ocfs2_dir_entry *) first_de;
444 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
450 status = ocfs2_journal_access(handle, dir, bh,
451 OCFS2_JOURNAL_ACCESS_WRITE);
458 le16_add_cpu(&pde->rec_len,
459 le16_to_cpu(de->rec_len));
463 status = ocfs2_journal_dirty(handle, bh);
466 i += le16_to_cpu(de->rec_len);
468 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
475 static inline int ocfs2_delete_entry_id(handle_t *handle,
477 struct ocfs2_dir_entry *de_del,
478 struct buffer_head *bh)
481 struct buffer_head *di_bh = NULL;
482 struct ocfs2_dinode *di;
483 struct ocfs2_inline_data *data;
485 ret = ocfs2_read_inode_block(dir, &di_bh);
491 di = (struct ocfs2_dinode *)di_bh->b_data;
492 data = &di->id2.i_data;
494 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
502 static inline int ocfs2_delete_entry_el(handle_t *handle,
504 struct ocfs2_dir_entry *de_del,
505 struct buffer_head *bh)
507 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
512 * ocfs2_delete_entry deletes a directory entry by merging it with the
515 int ocfs2_delete_entry(handle_t *handle,
517 struct ocfs2_dir_entry *de_del,
518 struct buffer_head *bh)
520 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
521 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
523 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
527 * Check whether 'de' has enough room to hold an entry of
528 * 'new_rec_len' bytes.
530 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
531 unsigned int new_rec_len)
533 unsigned int de_really_used;
535 /* Check whether this is an empty record with enough space */
536 if (le64_to_cpu(de->inode) == 0 &&
537 le16_to_cpu(de->rec_len) >= new_rec_len)
541 * Record might have free space at the end which we can
544 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
545 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
551 /* we don't always have a dentry for what we want to add, so people
552 * like orphan dir can call this instead.
554 * If you pass me insert_bh, I'll skip the search of the other dir
555 * blocks and put the record in there.
557 int __ocfs2_add_entry(handle_t *handle,
559 const char *name, int namelen,
560 struct inode *inode, u64 blkno,
561 struct buffer_head *parent_fe_bh,
562 struct buffer_head *insert_bh)
564 unsigned long offset;
565 unsigned short rec_len;
566 struct ocfs2_dir_entry *de, *de1;
567 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
568 struct super_block *sb = dir->i_sb;
570 unsigned int size = sb->s_blocksize;
571 char *data_start = insert_bh->b_data;
578 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
579 data_start = di->id2.i_data.id_data;
580 size = i_size_read(dir);
582 BUG_ON(insert_bh != parent_fe_bh);
585 rec_len = OCFS2_DIR_REC_LEN(namelen);
587 de = (struct ocfs2_dir_entry *) data_start;
589 BUG_ON((char *)de >= (size + data_start));
591 /* These checks should've already been passed by the
592 * prepare function, but I guess we can leave them
594 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
598 if (ocfs2_match(namelen, name, de)) {
603 if (ocfs2_dirent_would_fit(de, rec_len)) {
604 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
605 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
611 status = ocfs2_journal_access(handle, dir, insert_bh,
612 OCFS2_JOURNAL_ACCESS_WRITE);
613 /* By now the buffer is marked for journaling */
614 offset += le16_to_cpu(de->rec_len);
615 if (le64_to_cpu(de->inode)) {
616 de1 = (struct ocfs2_dir_entry *)((char *) de +
617 OCFS2_DIR_REC_LEN(de->name_len));
619 cpu_to_le16(le16_to_cpu(de->rec_len) -
620 OCFS2_DIR_REC_LEN(de->name_len));
621 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
624 de->file_type = OCFS2_FT_UNKNOWN;
626 de->inode = cpu_to_le64(blkno);
627 ocfs2_set_de_type(de, inode->i_mode);
630 de->name_len = namelen;
631 memcpy(de->name, name, namelen);
634 status = ocfs2_journal_dirty(handle, insert_bh);
638 offset += le16_to_cpu(de->rec_len);
639 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
642 /* when you think about it, the assert above should prevent us
643 * from ever getting here. */
651 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
653 loff_t *f_pos, void *priv,
654 filldir_t filldir, int *filldir_err)
656 int ret, i, filldir_ret;
657 unsigned long offset = *f_pos;
658 struct buffer_head *di_bh = NULL;
659 struct ocfs2_dinode *di;
660 struct ocfs2_inline_data *data;
661 struct ocfs2_dir_entry *de;
663 ret = ocfs2_read_inode_block(inode, &di_bh);
665 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
666 (unsigned long long)OCFS2_I(inode)->ip_blkno);
670 di = (struct ocfs2_dinode *)di_bh->b_data;
671 data = &di->id2.i_data;
673 while (*f_pos < i_size_read(inode)) {
675 /* If the dir block has changed since the last call to
676 * readdir(2), then we might be pointing to an invalid
677 * dirent right now. Scan from the start of the block
679 if (*f_version != inode->i_version) {
680 for (i = 0; i < i_size_read(inode) && i < offset; ) {
681 de = (struct ocfs2_dir_entry *)
683 /* It's too expensive to do a full
684 * dirent test each time round this
685 * loop, but we do have to test at
686 * least that it is non-zero. A
687 * failure will be detected in the
688 * dirent test below. */
689 if (le16_to_cpu(de->rec_len) <
690 OCFS2_DIR_REC_LEN(1))
692 i += le16_to_cpu(de->rec_len);
695 *f_version = inode->i_version;
698 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
699 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
700 /* On error, skip the f_pos to the end. */
701 *f_pos = i_size_read(inode);
704 offset += le16_to_cpu(de->rec_len);
705 if (le64_to_cpu(de->inode)) {
706 /* We might block in the next section
707 * if the data destination is
708 * currently swapped out. So, use a
709 * version stamp to detect whether or
710 * not the directory has been modified
711 * during the copy operation.
713 u64 version = *f_version;
714 unsigned char d_type = DT_UNKNOWN;
716 if (de->file_type < OCFS2_FT_MAX)
717 d_type = ocfs2_filetype_table[de->file_type];
719 filldir_ret = filldir(priv, de->name,
722 le64_to_cpu(de->inode),
726 *filldir_err = filldir_ret;
729 if (version != *f_version)
732 *f_pos += le16_to_cpu(de->rec_len);
741 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
743 loff_t *f_pos, void *priv,
744 filldir_t filldir, int *filldir_err)
747 unsigned long offset, blk, last_ra_blk = 0;
749 struct buffer_head * bh, * tmp;
750 struct ocfs2_dir_entry * de;
751 struct super_block * sb = inode->i_sb;
752 unsigned int ra_sectors = 16;
757 offset = (*f_pos) & (sb->s_blocksize - 1);
759 while (!error && !stored && *f_pos < i_size_read(inode)) {
760 blk = (*f_pos) >> sb->s_blocksize_bits;
761 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
762 /* Skip the corrupt dirblock and keep trying */
763 *f_pos += sb->s_blocksize - offset;
767 /* The idea here is to begin with 8k read-ahead and to stay
768 * 4k ahead of our current position.
770 * TODO: Use the pagecache for this. We just need to
771 * make sure it's cluster-safe... */
773 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
774 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
777 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
786 /* If the dir block has changed since the last call to
787 * readdir(2), then we might be pointing to an invalid
788 * dirent right now. Scan from the start of the block
790 if (*f_version != inode->i_version) {
791 for (i = 0; i < sb->s_blocksize && i < offset; ) {
792 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
793 /* It's too expensive to do a full
794 * dirent test each time round this
795 * loop, but we do have to test at
796 * least that it is non-zero. A
797 * failure will be detected in the
798 * dirent test below. */
799 if (le16_to_cpu(de->rec_len) <
800 OCFS2_DIR_REC_LEN(1))
802 i += le16_to_cpu(de->rec_len);
805 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
807 *f_version = inode->i_version;
810 while (!error && *f_pos < i_size_read(inode)
811 && offset < sb->s_blocksize) {
812 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
813 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
814 /* On error, skip the f_pos to the
816 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
820 offset += le16_to_cpu(de->rec_len);
821 if (le64_to_cpu(de->inode)) {
822 /* We might block in the next section
823 * if the data destination is
824 * currently swapped out. So, use a
825 * version stamp to detect whether or
826 * not the directory has been modified
827 * during the copy operation.
829 unsigned long version = *f_version;
830 unsigned char d_type = DT_UNKNOWN;
832 if (de->file_type < OCFS2_FT_MAX)
833 d_type = ocfs2_filetype_table[de->file_type];
834 error = filldir(priv, de->name,
837 le64_to_cpu(de->inode),
841 *filldir_err = error;
844 if (version != *f_version)
848 *f_pos += le16_to_cpu(de->rec_len);
860 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
861 loff_t *f_pos, void *priv, filldir_t filldir,
864 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
865 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
866 filldir, filldir_err);
868 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
873 * This is intended to be called from inside other kernel functions,
874 * so we fake some arguments.
876 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
879 int ret = 0, filldir_err = 0;
880 u64 version = inode->i_version;
882 while (*f_pos < i_size_read(inode)) {
883 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
884 filldir, &filldir_err);
885 if (ret || filldir_err)
899 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
902 struct inode *inode = filp->f_path.dentry->d_inode;
905 mlog_entry("dirino=%llu\n",
906 (unsigned long long)OCFS2_I(inode)->ip_blkno);
908 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
909 if (lock_level && error >= 0) {
910 /* We release EX lock which used to update atime
911 * and get PR lock again to reduce contention
912 * on commonly accessed directories. */
913 ocfs2_inode_unlock(inode, 1);
915 error = ocfs2_inode_lock(inode, NULL, 0);
918 if (error != -ENOENT)
920 /* we haven't got any yet, so propagate the error. */
924 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
925 dirent, filldir, NULL);
927 ocfs2_inode_unlock(inode, lock_level);
936 * NOTE: this should always be called with parent dir i_mutex taken.
938 int ocfs2_find_files_on_disk(const char *name,
942 struct buffer_head **dirent_bh,
943 struct ocfs2_dir_entry **dirent)
945 int status = -ENOENT;
947 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
948 namelen, name, blkno, inode, dirent_bh, dirent);
950 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
951 if (!*dirent_bh || !*dirent) {
956 *blkno = le64_to_cpu((*dirent)->inode);
971 * Convenience function for callers which just want the block number
972 * mapped to a name and don't require the full dirent info, etc.
974 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
975 int namelen, u64 *blkno)
978 struct buffer_head *bh = NULL;
979 struct ocfs2_dir_entry *dirent = NULL;
981 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
987 /* Check for a name within a directory.
989 * Return 0 if the name does not exist
990 * Return -EEXIST if the directory contains the name
992 * Callers should have i_mutex + a cluster lock on dir
994 int ocfs2_check_dir_for_entry(struct inode *dir,
999 struct buffer_head *dirent_bh = NULL;
1000 struct ocfs2_dir_entry *dirent = NULL;
1002 mlog_entry("dir %llu, name '%.*s'\n",
1003 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
1006 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1018 struct ocfs2_empty_dir_priv {
1020 unsigned seen_dot_dot;
1021 unsigned seen_other;
1023 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1024 loff_t pos, u64 ino, unsigned type)
1026 struct ocfs2_empty_dir_priv *p = priv;
1029 * Check the positions of "." and ".." records to be sure
1030 * they're in the correct place.
1032 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1037 if (name_len == 2 && !strncmp("..", name, 2) &&
1038 pos == OCFS2_DIR_REC_LEN(1)) {
1039 p->seen_dot_dot = 1;
1047 * routine to check that the specified directory is empty (for rmdir)
1049 * Returns 1 if dir is empty, zero otherwise.
1051 int ocfs2_empty_dir(struct inode *inode)
1055 struct ocfs2_empty_dir_priv priv;
1057 memset(&priv, 0, sizeof(priv));
1059 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1063 if (!priv.seen_dot || !priv.seen_dot_dot) {
1064 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1065 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1067 * XXX: Is it really safe to allow an unlink to continue?
1072 return !priv.seen_other;
1075 static void ocfs2_fill_initial_dirents(struct inode *inode,
1076 struct inode *parent,
1077 char *start, unsigned int size)
1079 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1081 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1084 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1085 strcpy(de->name, ".");
1086 ocfs2_set_de_type(de, S_IFDIR);
1088 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1089 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1090 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1092 strcpy(de->name, "..");
1093 ocfs2_set_de_type(de, S_IFDIR);
1097 * This works together with code in ocfs2_mknod_locked() which sets
1098 * the inline-data flag and initializes the inline-data section.
1100 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1102 struct inode *parent,
1103 struct inode *inode,
1104 struct buffer_head *di_bh)
1107 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1108 struct ocfs2_inline_data *data = &di->id2.i_data;
1109 unsigned int size = le16_to_cpu(data->id_count);
1111 ret = ocfs2_journal_access(handle, inode, di_bh,
1112 OCFS2_JOURNAL_ACCESS_WRITE);
1118 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1120 ocfs2_journal_dirty(handle, di_bh);
1126 i_size_write(inode, size);
1128 inode->i_blocks = ocfs2_inode_sector_count(inode);
1130 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1138 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1140 struct inode *parent,
1141 struct inode *inode,
1142 struct buffer_head *fe_bh,
1143 struct ocfs2_alloc_context *data_ac)
1146 struct buffer_head *new_bh = NULL;
1150 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1151 data_ac, NULL, &new_bh);
1157 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1159 status = ocfs2_journal_access(handle, inode, new_bh,
1160 OCFS2_JOURNAL_ACCESS_CREATE);
1165 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1167 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1168 osb->sb->s_blocksize);
1170 status = ocfs2_journal_dirty(handle, new_bh);
1176 i_size_write(inode, inode->i_sb->s_blocksize);
1178 inode->i_blocks = ocfs2_inode_sector_count(inode);
1179 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1193 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1195 struct inode *parent,
1196 struct inode *inode,
1197 struct buffer_head *fe_bh,
1198 struct ocfs2_alloc_context *data_ac)
1200 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1202 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1203 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1205 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1209 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1210 unsigned int new_size)
1212 struct ocfs2_dir_entry *de;
1213 struct ocfs2_dir_entry *prev_de;
1214 char *de_buf, *limit;
1215 unsigned int bytes = new_size - old_size;
1217 limit = start + old_size;
1219 de = (struct ocfs2_dir_entry *)de_buf;
1222 de_buf += le16_to_cpu(de->rec_len);
1223 de = (struct ocfs2_dir_entry *)de_buf;
1224 } while (de_buf < limit);
1226 le16_add_cpu(&prev_de->rec_len, bytes);
1230 * We allocate enough clusters to fulfill "blocks_wanted", but set
1231 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1232 * rest automatically for us.
1234 * *first_block_bh is a pointer to the 1st data block allocated to the
1237 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1238 unsigned int blocks_wanted,
1239 struct buffer_head **first_block_bh)
1241 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1242 u32 alloc, bit_off, len;
1243 struct super_block *sb = dir->i_sb;
1244 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1245 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1246 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1247 struct ocfs2_alloc_context *data_ac;
1248 struct buffer_head *dirdata_bh = NULL;
1249 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1251 struct ocfs2_extent_tree et;
1253 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1255 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1258 * We should never need more than 2 clusters for this -
1259 * maximum dirent size is far less than one block. In fact,
1260 * the only time we'd need more than one cluster is if
1261 * blocksize == clustersize and the dirent won't fit in the
1262 * extra space that the expansion to a single block gives. As
1263 * of today, that only happens on 4k/4k file systems.
1267 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1273 down_write(&oi->ip_alloc_sem);
1276 * Prepare for worst case allocation scenario of two separate
1280 credits += OCFS2_SUBALLOC_ALLOC;
1282 handle = ocfs2_start_trans(osb, credits);
1283 if (IS_ERR(handle)) {
1284 ret = PTR_ERR(handle);
1290 * Try to claim as many clusters as the bitmap can give though
1291 * if we only get one now, that's enough to continue. The rest
1292 * will be claimed after the conversion to extents.
1294 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1301 * Operations are carefully ordered so that we set up the new
1302 * data block first. The conversion from inline data to
1305 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1306 dirdata_bh = sb_getblk(sb, blkno);
1313 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1315 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1316 OCFS2_JOURNAL_ACCESS_CREATE);
1322 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1323 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1324 sb->s_blocksize - i_size_read(dir));
1325 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1328 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1335 * Set extent, i_size, etc on the directory. After this, the
1336 * inode should contain the same exact dirents as before and
1337 * be fully accessible from system calls.
1339 * We let the later dirent insert modify c/mtime - to the user
1340 * the data hasn't changed.
1342 ret = ocfs2_journal_access(handle, dir, di_bh,
1343 OCFS2_JOURNAL_ACCESS_CREATE);
1349 spin_lock(&oi->ip_lock);
1350 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1351 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1352 spin_unlock(&oi->ip_lock);
1354 ocfs2_dinode_new_extent_list(dir, di);
1356 i_size_write(dir, sb->s_blocksize);
1357 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1359 di->i_size = cpu_to_le64(sb->s_blocksize);
1360 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1361 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1364 * This should never fail as our extent list is empty and all
1365 * related blocks have been journaled already.
1367 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1375 * Set i_blocks after the extent insert for the most up to
1376 * date ip_clusters value.
1378 dir->i_blocks = ocfs2_inode_sector_count(dir);
1380 ret = ocfs2_journal_dirty(handle, di_bh);
1387 * We asked for two clusters, but only got one in the 1st
1388 * pass. Claim the 2nd cluster as a separate extent.
1391 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1397 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1399 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1400 blkno, len, 0, NULL);
1407 *first_block_bh = dirdata_bh;
1411 ocfs2_commit_trans(osb, handle);
1414 up_write(&oi->ip_alloc_sem);
1418 ocfs2_free_alloc_context(data_ac);
1425 /* returns a bh of the 1st new block in the allocation. */
1426 static int ocfs2_do_extend_dir(struct super_block *sb,
1429 struct buffer_head *parent_fe_bh,
1430 struct ocfs2_alloc_context *data_ac,
1431 struct ocfs2_alloc_context *meta_ac,
1432 struct buffer_head **new_bh)
1436 u64 p_blkno, v_blkno;
1438 spin_lock(&OCFS2_I(dir)->ip_lock);
1439 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1440 spin_unlock(&OCFS2_I(dir)->ip_lock);
1443 u32 offset = OCFS2_I(dir)->ip_clusters;
1445 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1446 1, 0, parent_fe_bh, handle,
1447 data_ac, meta_ac, NULL);
1448 BUG_ON(status == -EAGAIN);
1455 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1456 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1462 *new_bh = sb_getblk(sb, p_blkno);
1475 * Assumes you already have a cluster lock on the directory.
1477 * 'blocks_wanted' is only used if we have an inline directory which
1478 * is to be turned into an extent based one. The size of the dirent to
1479 * insert might be larger than the space gained by growing to just one
1480 * block, so we may have to grow the inode by two blocks in that case.
1482 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1484 struct buffer_head *parent_fe_bh,
1485 unsigned int blocks_wanted,
1486 struct buffer_head **new_de_bh)
1489 int credits, num_free_extents, drop_alloc_sem = 0;
1491 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1492 struct ocfs2_extent_list *el = &fe->id2.i_list;
1493 struct ocfs2_alloc_context *data_ac = NULL;
1494 struct ocfs2_alloc_context *meta_ac = NULL;
1495 handle_t *handle = NULL;
1496 struct buffer_head *new_bh = NULL;
1497 struct ocfs2_dir_entry * de;
1498 struct super_block *sb = osb->sb;
1499 struct ocfs2_extent_tree et;
1503 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1504 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1505 blocks_wanted, &new_bh);
1511 if (blocks_wanted == 1) {
1513 * If the new dirent will fit inside the space
1514 * created by pushing out to one block, then
1515 * we can complete the operation
1516 * here. Otherwise we have to expand i_size
1517 * and format the 2nd block below.
1519 BUG_ON(new_bh == NULL);
1524 * Get rid of 'new_bh' - we want to format the 2nd
1525 * data block and return that instead.
1530 dir_i_size = i_size_read(dir);
1531 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1535 dir_i_size = i_size_read(dir);
1536 mlog(0, "extending dir %llu (i_size = %lld)\n",
1537 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1539 /* dir->i_size is always block aligned. */
1540 spin_lock(&OCFS2_I(dir)->ip_lock);
1541 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1542 spin_unlock(&OCFS2_I(dir)->ip_lock);
1543 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1544 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1545 if (num_free_extents < 0) {
1546 status = num_free_extents;
1551 if (!num_free_extents) {
1552 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1554 if (status != -ENOSPC)
1560 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1562 if (status != -ENOSPC)
1567 credits = ocfs2_calc_extend_credits(sb, el, 1);
1569 spin_unlock(&OCFS2_I(dir)->ip_lock);
1570 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1574 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1577 handle = ocfs2_start_trans(osb, credits);
1578 if (IS_ERR(handle)) {
1579 status = PTR_ERR(handle);
1585 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1586 data_ac, meta_ac, &new_bh);
1592 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1594 status = ocfs2_journal_access(handle, dir, new_bh,
1595 OCFS2_JOURNAL_ACCESS_CREATE);
1600 memset(new_bh->b_data, 0, sb->s_blocksize);
1601 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1603 de->rec_len = cpu_to_le16(sb->s_blocksize);
1604 status = ocfs2_journal_dirty(handle, new_bh);
1610 dir_i_size += dir->i_sb->s_blocksize;
1611 i_size_write(dir, dir_i_size);
1612 dir->i_blocks = ocfs2_inode_sector_count(dir);
1613 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1620 *new_de_bh = new_bh;
1624 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1626 ocfs2_commit_trans(osb, handle);
1629 ocfs2_free_alloc_context(data_ac);
1631 ocfs2_free_alloc_context(meta_ac);
1639 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1640 const char *name, int namelen,
1641 struct buffer_head **ret_de_bh,
1642 unsigned int *blocks_wanted)
1645 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1646 struct ocfs2_dir_entry *de, *last_de = NULL;
1647 char *de_buf, *limit;
1648 unsigned long offset = 0;
1649 unsigned int rec_len, new_rec_len;
1651 de_buf = di->id2.i_data.id_data;
1652 limit = de_buf + i_size_read(dir);
1653 rec_len = OCFS2_DIR_REC_LEN(namelen);
1655 while (de_buf < limit) {
1656 de = (struct ocfs2_dir_entry *)de_buf;
1658 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1662 if (ocfs2_match(namelen, name, de)) {
1666 if (ocfs2_dirent_would_fit(de, rec_len)) {
1667 /* Ok, we found a spot. Return this bh and let
1668 * the caller actually fill it in. */
1676 de_buf += le16_to_cpu(de->rec_len);
1677 offset += le16_to_cpu(de->rec_len);
1681 * We're going to require expansion of the directory - figure
1682 * out how many blocks we'll need so that a place for the
1683 * dirent can be found.
1686 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1687 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1695 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1696 int namelen, struct buffer_head **ret_de_bh)
1698 unsigned long offset;
1699 struct buffer_head *bh = NULL;
1700 unsigned short rec_len;
1701 struct ocfs2_dir_entry *de;
1702 struct super_block *sb = dir->i_sb;
1705 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1711 rec_len = OCFS2_DIR_REC_LEN(namelen);
1713 de = (struct ocfs2_dir_entry *) bh->b_data;
1715 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1719 if (i_size_read(dir) <= offset) {
1721 * Caller will have to expand this
1727 status = ocfs2_read_dir_block(dir,
1728 offset >> sb->s_blocksize_bits,
1734 /* move to next block */
1735 de = (struct ocfs2_dir_entry *) bh->b_data;
1737 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1741 if (ocfs2_match(namelen, name, de)) {
1745 if (ocfs2_dirent_would_fit(de, rec_len)) {
1746 /* Ok, we found a spot. Return this bh and let
1747 * the caller actually fill it in. */
1753 offset += le16_to_cpu(de->rec_len);
1754 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1765 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1767 struct buffer_head *parent_fe_bh,
1770 struct buffer_head **ret_de_bh)
1773 unsigned int blocks_wanted = 1;
1774 struct buffer_head *bh = NULL;
1776 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1777 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1787 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1788 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1789 namelen, &bh, &blocks_wanted);
1791 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1793 if (ret && ret != -ENOSPC) {
1798 if (ret == -ENOSPC) {
1800 * We have to expand the directory to add this name.
1804 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,