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);
85 static struct buffer_head *ocfs2_bread(struct inode *inode,
86 int block, int *err, int reada)
88 struct buffer_head *bh = NULL;
91 int readflags = OCFS2_BH_CACHED;
94 readflags |= OCFS2_BH_READAHEAD;
96 if (((u64)block << inode->i_sb->s_blocksize_bits) >=
102 down_read(&OCFS2_I(inode)->ip_alloc_sem);
103 tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
105 up_read(&OCFS2_I(inode)->ip_alloc_sem);
111 tmperr = ocfs2_read_blocks(inode, p_blkno, 1, &bh, readflags);
129 * bh passed here can be an inode block or a dir data block, depending
130 * on the inode inline data flag.
132 static int ocfs2_check_dir_entry(struct inode * dir,
133 struct ocfs2_dir_entry * de,
134 struct buffer_head * bh,
135 unsigned long offset)
137 const char *error_msg = NULL;
138 const int rlen = le16_to_cpu(de->rec_len);
140 if (rlen < OCFS2_DIR_REC_LEN(1))
141 error_msg = "rec_len is smaller than minimal";
142 else if (rlen % 4 != 0)
143 error_msg = "rec_len % 4 != 0";
144 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
145 error_msg = "rec_len is too small for name_len";
146 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
147 error_msg = "directory entry across blocks";
149 if (error_msg != NULL)
150 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
151 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
152 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
153 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
155 return error_msg == NULL ? 1 : 0;
158 static inline int ocfs2_match(int len,
159 const char * const name,
160 struct ocfs2_dir_entry *de)
162 if (len != de->name_len)
166 return !memcmp(name, de->name, len);
170 * Returns 0 if not found, -1 on failure, and 1 on success
172 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
174 const char *name, int namelen,
175 unsigned long offset,
178 struct ocfs2_dir_entry **res_dir)
180 struct ocfs2_dir_entry *de;
181 char *dlimit, *de_buf;
188 dlimit = de_buf + bytes;
190 while (de_buf < dlimit) {
191 /* this code is executed quadratically often */
192 /* do minimal checking `by hand' */
194 de = (struct ocfs2_dir_entry *) de_buf;
196 if (de_buf + namelen <= dlimit &&
197 ocfs2_match(namelen, name, de)) {
198 /* found a match - just to be sure, do a full check */
199 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
208 /* prevent looping on a bad block */
209 de_len = le16_to_cpu(de->rec_len);
224 static struct buffer_head *ocfs2_find_entry_id(const char *name,
227 struct ocfs2_dir_entry **res_dir)
230 struct buffer_head *di_bh = NULL;
231 struct ocfs2_dinode *di;
232 struct ocfs2_inline_data *data;
234 ret = ocfs2_read_block(dir, OCFS2_I(dir)->ip_blkno, &di_bh);
240 di = (struct ocfs2_dinode *)di_bh->b_data;
241 data = &di->id2.i_data;
243 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
244 data->id_data, i_size_read(dir), res_dir);
253 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
255 struct ocfs2_dir_entry **res_dir)
257 struct super_block *sb;
258 struct buffer_head *bh_use[NAMEI_RA_SIZE];
259 struct buffer_head *bh, *ret = NULL;
260 unsigned long start, block, b;
261 int ra_max = 0; /* Number of bh's in the readahead
263 int ra_ptr = 0; /* Current index into readahead
272 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
273 start = OCFS2_I(dir)->ip_dir_start_lookup;
274 if (start >= nblocks)
281 * We deal with the read-ahead logic here.
283 if (ra_ptr >= ra_max) {
284 /* Refill the readahead buffer */
287 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
289 * Terminate if we reach the end of the
290 * directory and must wrap, or if our
291 * search has finished at this block.
293 if (b >= nblocks || (num && block == start)) {
294 bh_use[ra_max] = NULL;
299 bh = ocfs2_bread(dir, b++, &err, 1);
303 if ((bh = bh_use[ra_ptr++]) == NULL)
306 if (!buffer_uptodate(bh)) {
307 /* read error, skip block & hope for the best */
308 ocfs2_error(dir->i_sb, "reading directory %llu, "
310 (unsigned long long)OCFS2_I(dir)->ip_blkno,
315 i = ocfs2_search_dirblock(bh, dir, name, namelen,
316 block << sb->s_blocksize_bits,
317 bh->b_data, sb->s_blocksize,
320 OCFS2_I(dir)->ip_dir_start_lookup = block;
322 goto cleanup_and_exit;
326 goto cleanup_and_exit;
329 if (++block >= nblocks)
331 } while (block != start);
334 * If the directory has grown while we were searching, then
335 * search the last part of the directory before giving up.
338 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
339 if (block < nblocks) {
345 /* Clean up the read-ahead blocks */
346 for (; ra_ptr < ra_max; ra_ptr++)
347 brelse(bh_use[ra_ptr]);
354 * Try to find an entry of the provided name within 'dir'.
356 * If nothing was found, NULL is returned. Otherwise, a buffer_head
357 * and pointer to the dir entry are passed back.
359 * Caller can NOT assume anything about the contents of the
360 * buffer_head - it is passed back only so that it can be passed into
361 * any one of the manipulation functions (add entry, delete entry,
362 * etc). As an example, bh in the extent directory case is a data
363 * block, in the inline-data case it actually points to an inode.
365 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
367 struct ocfs2_dir_entry **res_dir)
371 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
372 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
374 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
378 * Update inode number and type of a previously found directory entry.
380 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
381 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
382 struct inode *new_entry_inode)
387 * The same code works fine for both inline-data and extent
388 * based directories, so no need to split this up.
391 ret = ocfs2_journal_access(handle, dir, de_bh,
392 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;
415 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
419 de = (struct ocfs2_dir_entry *) first_de;
421 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
427 status = ocfs2_journal_access(handle, dir, bh,
428 OCFS2_JOURNAL_ACCESS_WRITE);
435 le16_add_cpu(&pde->rec_len,
436 le16_to_cpu(de->rec_len));
440 status = ocfs2_journal_dirty(handle, bh);
443 i += le16_to_cpu(de->rec_len);
445 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
452 static inline int ocfs2_delete_entry_id(handle_t *handle,
454 struct ocfs2_dir_entry *de_del,
455 struct buffer_head *bh)
458 struct buffer_head *di_bh = NULL;
459 struct ocfs2_dinode *di;
460 struct ocfs2_inline_data *data;
462 ret = ocfs2_read_block(dir, OCFS2_I(dir)->ip_blkno, &di_bh);
468 di = (struct ocfs2_dinode *)di_bh->b_data;
469 data = &di->id2.i_data;
471 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
479 static inline int ocfs2_delete_entry_el(handle_t *handle,
481 struct ocfs2_dir_entry *de_del,
482 struct buffer_head *bh)
484 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
489 * ocfs2_delete_entry deletes a directory entry by merging it with the
492 int ocfs2_delete_entry(handle_t *handle,
494 struct ocfs2_dir_entry *de_del,
495 struct buffer_head *bh)
497 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
498 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
500 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
504 * Check whether 'de' has enough room to hold an entry of
505 * 'new_rec_len' bytes.
507 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
508 unsigned int new_rec_len)
510 unsigned int de_really_used;
512 /* Check whether this is an empty record with enough space */
513 if (le64_to_cpu(de->inode) == 0 &&
514 le16_to_cpu(de->rec_len) >= new_rec_len)
518 * Record might have free space at the end which we can
521 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
522 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
528 /* we don't always have a dentry for what we want to add, so people
529 * like orphan dir can call this instead.
531 * If you pass me insert_bh, I'll skip the search of the other dir
532 * blocks and put the record in there.
534 int __ocfs2_add_entry(handle_t *handle,
536 const char *name, int namelen,
537 struct inode *inode, u64 blkno,
538 struct buffer_head *parent_fe_bh,
539 struct buffer_head *insert_bh)
541 unsigned long offset;
542 unsigned short rec_len;
543 struct ocfs2_dir_entry *de, *de1;
544 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
545 struct super_block *sb = dir->i_sb;
547 unsigned int size = sb->s_blocksize;
548 char *data_start = insert_bh->b_data;
555 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
556 data_start = di->id2.i_data.id_data;
557 size = i_size_read(dir);
559 BUG_ON(insert_bh != parent_fe_bh);
562 rec_len = OCFS2_DIR_REC_LEN(namelen);
564 de = (struct ocfs2_dir_entry *) data_start;
566 BUG_ON((char *)de >= (size + data_start));
568 /* These checks should've already been passed by the
569 * prepare function, but I guess we can leave them
571 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
575 if (ocfs2_match(namelen, name, de)) {
580 if (ocfs2_dirent_would_fit(de, rec_len)) {
581 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
582 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
588 status = ocfs2_journal_access(handle, dir, insert_bh,
589 OCFS2_JOURNAL_ACCESS_WRITE);
590 /* By now the buffer is marked for journaling */
591 offset += le16_to_cpu(de->rec_len);
592 if (le64_to_cpu(de->inode)) {
593 de1 = (struct ocfs2_dir_entry *)((char *) de +
594 OCFS2_DIR_REC_LEN(de->name_len));
596 cpu_to_le16(le16_to_cpu(de->rec_len) -
597 OCFS2_DIR_REC_LEN(de->name_len));
598 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
601 de->file_type = OCFS2_FT_UNKNOWN;
603 de->inode = cpu_to_le64(blkno);
604 ocfs2_set_de_type(de, inode->i_mode);
607 de->name_len = namelen;
608 memcpy(de->name, name, namelen);
611 status = ocfs2_journal_dirty(handle, insert_bh);
615 offset += le16_to_cpu(de->rec_len);
616 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
619 /* when you think about it, the assert above should prevent us
620 * from ever getting here. */
628 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
630 loff_t *f_pos, void *priv,
631 filldir_t filldir, int *filldir_err)
633 int ret, i, filldir_ret;
634 unsigned long offset = *f_pos;
635 struct buffer_head *di_bh = NULL;
636 struct ocfs2_dinode *di;
637 struct ocfs2_inline_data *data;
638 struct ocfs2_dir_entry *de;
640 ret = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &di_bh);
642 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
643 (unsigned long long)OCFS2_I(inode)->ip_blkno);
647 di = (struct ocfs2_dinode *)di_bh->b_data;
648 data = &di->id2.i_data;
650 while (*f_pos < i_size_read(inode)) {
652 /* If the dir block has changed since the last call to
653 * readdir(2), then we might be pointing to an invalid
654 * dirent right now. Scan from the start of the block
656 if (*f_version != inode->i_version) {
657 for (i = 0; i < i_size_read(inode) && i < offset; ) {
658 de = (struct ocfs2_dir_entry *)
660 /* It's too expensive to do a full
661 * dirent test each time round this
662 * loop, but we do have to test at
663 * least that it is non-zero. A
664 * failure will be detected in the
665 * dirent test below. */
666 if (le16_to_cpu(de->rec_len) <
667 OCFS2_DIR_REC_LEN(1))
669 i += le16_to_cpu(de->rec_len);
672 *f_version = inode->i_version;
675 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
676 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
677 /* On error, skip the f_pos to the end. */
678 *f_pos = i_size_read(inode);
681 offset += le16_to_cpu(de->rec_len);
682 if (le64_to_cpu(de->inode)) {
683 /* We might block in the next section
684 * if the data destination is
685 * currently swapped out. So, use a
686 * version stamp to detect whether or
687 * not the directory has been modified
688 * during the copy operation.
690 u64 version = *f_version;
691 unsigned char d_type = DT_UNKNOWN;
693 if (de->file_type < OCFS2_FT_MAX)
694 d_type = ocfs2_filetype_table[de->file_type];
696 filldir_ret = filldir(priv, de->name,
699 le64_to_cpu(de->inode),
703 *filldir_err = filldir_ret;
706 if (version != *f_version)
709 *f_pos += le16_to_cpu(de->rec_len);
718 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
720 loff_t *f_pos, void *priv,
721 filldir_t filldir, int *filldir_err)
724 unsigned long offset, blk, last_ra_blk = 0;
726 struct buffer_head * bh, * tmp;
727 struct ocfs2_dir_entry * de;
729 struct super_block * sb = inode->i_sb;
730 unsigned int ra_sectors = 16;
735 offset = (*f_pos) & (sb->s_blocksize - 1);
737 while (!error && !stored && *f_pos < i_size_read(inode)) {
738 blk = (*f_pos) >> sb->s_blocksize_bits;
739 bh = ocfs2_bread(inode, blk, &err, 0);
742 "directory #%llu contains a hole at offset %lld\n",
743 (unsigned long long)OCFS2_I(inode)->ip_blkno,
745 *f_pos += sb->s_blocksize - offset;
749 /* The idea here is to begin with 8k read-ahead and to stay
750 * 4k ahead of our current position.
752 * TODO: Use the pagecache for this. We just need to
753 * make sure it's cluster-safe... */
755 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
756 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
758 tmp = ocfs2_bread(inode, ++blk, &err, 1);
766 /* If the dir block has changed since the last call to
767 * readdir(2), then we might be pointing to an invalid
768 * dirent right now. Scan from the start of the block
770 if (*f_version != inode->i_version) {
771 for (i = 0; i < sb->s_blocksize && i < offset; ) {
772 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
773 /* It's too expensive to do a full
774 * dirent test each time round this
775 * loop, but we do have to test at
776 * least that it is non-zero. A
777 * failure will be detected in the
778 * dirent test below. */
779 if (le16_to_cpu(de->rec_len) <
780 OCFS2_DIR_REC_LEN(1))
782 i += le16_to_cpu(de->rec_len);
785 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
787 *f_version = inode->i_version;
790 while (!error && *f_pos < i_size_read(inode)
791 && offset < sb->s_blocksize) {
792 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
793 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
794 /* On error, skip the f_pos to the
796 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
800 offset += le16_to_cpu(de->rec_len);
801 if (le64_to_cpu(de->inode)) {
802 /* We might block in the next section
803 * if the data destination is
804 * currently swapped out. So, use a
805 * version stamp to detect whether or
806 * not the directory has been modified
807 * during the copy operation.
809 unsigned long version = *f_version;
810 unsigned char d_type = DT_UNKNOWN;
812 if (de->file_type < OCFS2_FT_MAX)
813 d_type = ocfs2_filetype_table[de->file_type];
814 error = filldir(priv, de->name,
817 le64_to_cpu(de->inode),
821 *filldir_err = error;
824 if (version != *f_version)
828 *f_pos += le16_to_cpu(de->rec_len);
839 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
840 loff_t *f_pos, void *priv, filldir_t filldir,
843 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
844 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
845 filldir, filldir_err);
847 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
852 * This is intended to be called from inside other kernel functions,
853 * so we fake some arguments.
855 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
858 int ret = 0, filldir_err = 0;
859 u64 version = inode->i_version;
861 while (*f_pos < i_size_read(inode)) {
862 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
863 filldir, &filldir_err);
864 if (ret || filldir_err)
878 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
881 struct inode *inode = filp->f_path.dentry->d_inode;
884 mlog_entry("dirino=%llu\n",
885 (unsigned long long)OCFS2_I(inode)->ip_blkno);
887 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
888 if (lock_level && error >= 0) {
889 /* We release EX lock which used to update atime
890 * and get PR lock again to reduce contention
891 * on commonly accessed directories. */
892 ocfs2_inode_unlock(inode, 1);
894 error = ocfs2_inode_lock(inode, NULL, 0);
897 if (error != -ENOENT)
899 /* we haven't got any yet, so propagate the error. */
903 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
904 dirent, filldir, NULL);
906 ocfs2_inode_unlock(inode, lock_level);
915 * NOTE: this should always be called with parent dir i_mutex taken.
917 int ocfs2_find_files_on_disk(const char *name,
921 struct buffer_head **dirent_bh,
922 struct ocfs2_dir_entry **dirent)
924 int status = -ENOENT;
926 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
927 namelen, name, blkno, inode, dirent_bh, dirent);
929 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
930 if (!*dirent_bh || !*dirent) {
935 *blkno = le64_to_cpu((*dirent)->inode);
950 * Convenience function for callers which just want the block number
951 * mapped to a name and don't require the full dirent info, etc.
953 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
954 int namelen, u64 *blkno)
957 struct buffer_head *bh = NULL;
958 struct ocfs2_dir_entry *dirent = NULL;
960 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
966 /* Check for a name within a directory.
968 * Return 0 if the name does not exist
969 * Return -EEXIST if the directory contains the name
971 * Callers should have i_mutex + a cluster lock on dir
973 int ocfs2_check_dir_for_entry(struct inode *dir,
978 struct buffer_head *dirent_bh = NULL;
979 struct ocfs2_dir_entry *dirent = NULL;
981 mlog_entry("dir %llu, name '%.*s'\n",
982 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
985 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
997 struct ocfs2_empty_dir_priv {
999 unsigned seen_dot_dot;
1000 unsigned seen_other;
1002 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1003 loff_t pos, u64 ino, unsigned type)
1005 struct ocfs2_empty_dir_priv *p = priv;
1008 * Check the positions of "." and ".." records to be sure
1009 * they're in the correct place.
1011 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1016 if (name_len == 2 && !strncmp("..", name, 2) &&
1017 pos == OCFS2_DIR_REC_LEN(1)) {
1018 p->seen_dot_dot = 1;
1026 * routine to check that the specified directory is empty (for rmdir)
1028 * Returns 1 if dir is empty, zero otherwise.
1030 int ocfs2_empty_dir(struct inode *inode)
1034 struct ocfs2_empty_dir_priv priv;
1036 memset(&priv, 0, sizeof(priv));
1038 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1042 if (!priv.seen_dot || !priv.seen_dot_dot) {
1043 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1044 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1046 * XXX: Is it really safe to allow an unlink to continue?
1051 return !priv.seen_other;
1054 static void ocfs2_fill_initial_dirents(struct inode *inode,
1055 struct inode *parent,
1056 char *start, unsigned int size)
1058 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1060 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1063 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1064 strcpy(de->name, ".");
1065 ocfs2_set_de_type(de, S_IFDIR);
1067 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1068 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1069 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1071 strcpy(de->name, "..");
1072 ocfs2_set_de_type(de, S_IFDIR);
1076 * This works together with code in ocfs2_mknod_locked() which sets
1077 * the inline-data flag and initializes the inline-data section.
1079 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1081 struct inode *parent,
1082 struct inode *inode,
1083 struct buffer_head *di_bh)
1086 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1087 struct ocfs2_inline_data *data = &di->id2.i_data;
1088 unsigned int size = le16_to_cpu(data->id_count);
1090 ret = ocfs2_journal_access(handle, inode, di_bh,
1091 OCFS2_JOURNAL_ACCESS_WRITE);
1097 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1099 ocfs2_journal_dirty(handle, di_bh);
1105 i_size_write(inode, size);
1107 inode->i_blocks = ocfs2_inode_sector_count(inode);
1109 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1117 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1119 struct inode *parent,
1120 struct inode *inode,
1121 struct buffer_head *fe_bh,
1122 struct ocfs2_alloc_context *data_ac)
1125 struct buffer_head *new_bh = NULL;
1129 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1130 data_ac, NULL, &new_bh);
1136 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1138 status = ocfs2_journal_access(handle, inode, new_bh,
1139 OCFS2_JOURNAL_ACCESS_CREATE);
1144 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1146 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1147 osb->sb->s_blocksize);
1149 status = ocfs2_journal_dirty(handle, new_bh);
1155 i_size_write(inode, inode->i_sb->s_blocksize);
1157 inode->i_blocks = ocfs2_inode_sector_count(inode);
1158 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1172 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1174 struct inode *parent,
1175 struct inode *inode,
1176 struct buffer_head *fe_bh,
1177 struct ocfs2_alloc_context *data_ac)
1179 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1181 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1182 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1184 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1188 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1189 unsigned int new_size)
1191 struct ocfs2_dir_entry *de;
1192 struct ocfs2_dir_entry *prev_de;
1193 char *de_buf, *limit;
1194 unsigned int bytes = new_size - old_size;
1196 limit = start + old_size;
1198 de = (struct ocfs2_dir_entry *)de_buf;
1201 de_buf += le16_to_cpu(de->rec_len);
1202 de = (struct ocfs2_dir_entry *)de_buf;
1203 } while (de_buf < limit);
1205 le16_add_cpu(&prev_de->rec_len, bytes);
1209 * We allocate enough clusters to fulfill "blocks_wanted", but set
1210 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1211 * rest automatically for us.
1213 * *first_block_bh is a pointer to the 1st data block allocated to the
1216 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1217 unsigned int blocks_wanted,
1218 struct buffer_head **first_block_bh)
1220 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1221 u32 alloc, bit_off, len;
1222 struct super_block *sb = dir->i_sb;
1223 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1224 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1225 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1226 struct ocfs2_alloc_context *data_ac;
1227 struct buffer_head *dirdata_bh = NULL;
1228 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1230 struct ocfs2_extent_tree et;
1232 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1234 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1237 * We should never need more than 2 clusters for this -
1238 * maximum dirent size is far less than one block. In fact,
1239 * the only time we'd need more than one cluster is if
1240 * blocksize == clustersize and the dirent won't fit in the
1241 * extra space that the expansion to a single block gives. As
1242 * of today, that only happens on 4k/4k file systems.
1246 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1252 down_write(&oi->ip_alloc_sem);
1255 * Prepare for worst case allocation scenario of two separate
1259 credits += OCFS2_SUBALLOC_ALLOC;
1261 handle = ocfs2_start_trans(osb, credits);
1262 if (IS_ERR(handle)) {
1263 ret = PTR_ERR(handle);
1269 * Try to claim as many clusters as the bitmap can give though
1270 * if we only get one now, that's enough to continue. The rest
1271 * will be claimed after the conversion to extents.
1273 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1280 * Operations are carefully ordered so that we set up the new
1281 * data block first. The conversion from inline data to
1284 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1285 dirdata_bh = sb_getblk(sb, blkno);
1292 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1294 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1295 OCFS2_JOURNAL_ACCESS_CREATE);
1301 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1302 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1303 sb->s_blocksize - i_size_read(dir));
1304 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1307 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1314 * Set extent, i_size, etc on the directory. After this, the
1315 * inode should contain the same exact dirents as before and
1316 * be fully accessible from system calls.
1318 * We let the later dirent insert modify c/mtime - to the user
1319 * the data hasn't changed.
1321 ret = ocfs2_journal_access(handle, dir, di_bh,
1322 OCFS2_JOURNAL_ACCESS_CREATE);
1328 spin_lock(&oi->ip_lock);
1329 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1330 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1331 spin_unlock(&oi->ip_lock);
1333 ocfs2_dinode_new_extent_list(dir, di);
1335 i_size_write(dir, sb->s_blocksize);
1336 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1338 di->i_size = cpu_to_le64(sb->s_blocksize);
1339 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1340 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1343 * This should never fail as our extent list is empty and all
1344 * related blocks have been journaled already.
1346 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1354 * Set i_blocks after the extent insert for the most up to
1355 * date ip_clusters value.
1357 dir->i_blocks = ocfs2_inode_sector_count(dir);
1359 ret = ocfs2_journal_dirty(handle, di_bh);
1366 * We asked for two clusters, but only got one in the 1st
1367 * pass. Claim the 2nd cluster as a separate extent.
1370 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1376 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1378 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1379 blkno, len, 0, NULL);
1386 *first_block_bh = dirdata_bh;
1390 ocfs2_commit_trans(osb, handle);
1393 up_write(&oi->ip_alloc_sem);
1397 ocfs2_free_alloc_context(data_ac);
1404 /* returns a bh of the 1st new block in the allocation. */
1405 static int ocfs2_do_extend_dir(struct super_block *sb,
1408 struct buffer_head *parent_fe_bh,
1409 struct ocfs2_alloc_context *data_ac,
1410 struct ocfs2_alloc_context *meta_ac,
1411 struct buffer_head **new_bh)
1415 u64 p_blkno, v_blkno;
1417 spin_lock(&OCFS2_I(dir)->ip_lock);
1418 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1419 spin_unlock(&OCFS2_I(dir)->ip_lock);
1422 u32 offset = OCFS2_I(dir)->ip_clusters;
1424 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1425 1, 0, parent_fe_bh, handle,
1426 data_ac, meta_ac, NULL);
1427 BUG_ON(status == -EAGAIN);
1434 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1435 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1441 *new_bh = sb_getblk(sb, p_blkno);
1454 * Assumes you already have a cluster lock on the directory.
1456 * 'blocks_wanted' is only used if we have an inline directory which
1457 * is to be turned into an extent based one. The size of the dirent to
1458 * insert might be larger than the space gained by growing to just one
1459 * block, so we may have to grow the inode by two blocks in that case.
1461 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1463 struct buffer_head *parent_fe_bh,
1464 unsigned int blocks_wanted,
1465 struct buffer_head **new_de_bh)
1468 int credits, num_free_extents, drop_alloc_sem = 0;
1470 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1471 struct ocfs2_extent_list *el = &fe->id2.i_list;
1472 struct ocfs2_alloc_context *data_ac = NULL;
1473 struct ocfs2_alloc_context *meta_ac = NULL;
1474 handle_t *handle = NULL;
1475 struct buffer_head *new_bh = NULL;
1476 struct ocfs2_dir_entry * de;
1477 struct super_block *sb = osb->sb;
1478 struct ocfs2_extent_tree et;
1482 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1483 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1484 blocks_wanted, &new_bh);
1490 if (blocks_wanted == 1) {
1492 * If the new dirent will fit inside the space
1493 * created by pushing out to one block, then
1494 * we can complete the operation
1495 * here. Otherwise we have to expand i_size
1496 * and format the 2nd block below.
1498 BUG_ON(new_bh == NULL);
1503 * Get rid of 'new_bh' - we want to format the 2nd
1504 * data block and return that instead.
1509 dir_i_size = i_size_read(dir);
1510 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1514 dir_i_size = i_size_read(dir);
1515 mlog(0, "extending dir %llu (i_size = %lld)\n",
1516 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1518 /* dir->i_size is always block aligned. */
1519 spin_lock(&OCFS2_I(dir)->ip_lock);
1520 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1521 spin_unlock(&OCFS2_I(dir)->ip_lock);
1522 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1523 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1524 if (num_free_extents < 0) {
1525 status = num_free_extents;
1530 if (!num_free_extents) {
1531 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1533 if (status != -ENOSPC)
1539 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1541 if (status != -ENOSPC)
1546 credits = ocfs2_calc_extend_credits(sb, el, 1);
1548 spin_unlock(&OCFS2_I(dir)->ip_lock);
1549 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1553 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1556 handle = ocfs2_start_trans(osb, credits);
1557 if (IS_ERR(handle)) {
1558 status = PTR_ERR(handle);
1564 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1565 data_ac, meta_ac, &new_bh);
1571 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1573 status = ocfs2_journal_access(handle, dir, new_bh,
1574 OCFS2_JOURNAL_ACCESS_CREATE);
1579 memset(new_bh->b_data, 0, sb->s_blocksize);
1580 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1582 de->rec_len = cpu_to_le16(sb->s_blocksize);
1583 status = ocfs2_journal_dirty(handle, new_bh);
1589 dir_i_size += dir->i_sb->s_blocksize;
1590 i_size_write(dir, dir_i_size);
1591 dir->i_blocks = ocfs2_inode_sector_count(dir);
1592 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1599 *new_de_bh = new_bh;
1603 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1605 ocfs2_commit_trans(osb, handle);
1608 ocfs2_free_alloc_context(data_ac);
1610 ocfs2_free_alloc_context(meta_ac);
1618 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1619 const char *name, int namelen,
1620 struct buffer_head **ret_de_bh,
1621 unsigned int *blocks_wanted)
1624 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1625 struct ocfs2_dir_entry *de, *last_de = NULL;
1626 char *de_buf, *limit;
1627 unsigned long offset = 0;
1628 unsigned int rec_len, new_rec_len;
1630 de_buf = di->id2.i_data.id_data;
1631 limit = de_buf + i_size_read(dir);
1632 rec_len = OCFS2_DIR_REC_LEN(namelen);
1634 while (de_buf < limit) {
1635 de = (struct ocfs2_dir_entry *)de_buf;
1637 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1641 if (ocfs2_match(namelen, name, de)) {
1645 if (ocfs2_dirent_would_fit(de, rec_len)) {
1646 /* Ok, we found a spot. Return this bh and let
1647 * the caller actually fill it in. */
1655 de_buf += le16_to_cpu(de->rec_len);
1656 offset += le16_to_cpu(de->rec_len);
1660 * We're going to require expansion of the directory - figure
1661 * out how many blocks we'll need so that a place for the
1662 * dirent can be found.
1665 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1666 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1674 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1675 int namelen, struct buffer_head **ret_de_bh)
1677 unsigned long offset;
1678 struct buffer_head *bh = NULL;
1679 unsigned short rec_len;
1680 struct ocfs2_dir_entry *de;
1681 struct super_block *sb = dir->i_sb;
1684 bh = ocfs2_bread(dir, 0, &status, 0);
1690 rec_len = OCFS2_DIR_REC_LEN(namelen);
1692 de = (struct ocfs2_dir_entry *) bh->b_data;
1694 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1698 if (i_size_read(dir) <= offset) {
1700 * Caller will have to expand this
1706 bh = ocfs2_bread(dir,
1707 offset >> sb->s_blocksize_bits,
1714 /* move to next block */
1715 de = (struct ocfs2_dir_entry *) bh->b_data;
1717 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1721 if (ocfs2_match(namelen, name, de)) {
1725 if (ocfs2_dirent_would_fit(de, rec_len)) {
1726 /* Ok, we found a spot. Return this bh and let
1727 * the caller actually fill it in. */
1733 offset += le16_to_cpu(de->rec_len);
1734 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1745 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1747 struct buffer_head *parent_fe_bh,
1750 struct buffer_head **ret_de_bh)
1753 unsigned int blocks_wanted = 1;
1754 struct buffer_head *bh = NULL;
1756 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1757 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1767 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1768 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1769 namelen, &bh, &blocks_wanted);
1771 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1773 if (ret && ret != -ENOSPC) {
1778 if (ret == -ENOSPC) {
1780 * We have to expand the directory to add this name.
1784 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,