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_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
192 &di_bh, OCFS2_BH_CACHED, dir);
198 di = (struct ocfs2_dinode *)di_bh->b_data;
199 data = &di->id2.i_data;
201 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
202 data->id_data, i_size_read(dir), res_dir);
211 struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
213 struct ocfs2_dir_entry **res_dir)
215 struct super_block *sb;
216 struct buffer_head *bh_use[NAMEI_RA_SIZE];
217 struct buffer_head *bh, *ret = NULL;
218 unsigned long start, block, b;
219 int ra_max = 0; /* Number of bh's in the readahead
221 int ra_ptr = 0; /* Current index into readahead
230 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
231 start = OCFS2_I(dir)->ip_dir_start_lookup;
232 if (start >= nblocks)
239 * We deal with the read-ahead logic here.
241 if (ra_ptr >= ra_max) {
242 /* Refill the readahead buffer */
245 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
247 * Terminate if we reach the end of the
248 * directory and must wrap, or if our
249 * search has finished at this block.
251 if (b >= nblocks || (num && block == start)) {
252 bh_use[ra_max] = NULL;
257 bh = ocfs2_bread(dir, b++, &err, 1);
261 if ((bh = bh_use[ra_ptr++]) == NULL)
264 if (!buffer_uptodate(bh)) {
265 /* read error, skip block & hope for the best */
266 ocfs2_error(dir->i_sb, "reading directory %llu, "
268 (unsigned long long)OCFS2_I(dir)->ip_blkno,
273 i = ocfs2_search_dirblock(bh, dir, name, namelen,
274 block << sb->s_blocksize_bits,
275 bh->b_data, sb->s_blocksize,
278 OCFS2_I(dir)->ip_dir_start_lookup = block;
280 goto cleanup_and_exit;
284 goto cleanup_and_exit;
287 if (++block >= nblocks)
289 } while (block != start);
292 * If the directory has grown while we were searching, then
293 * search the last part of the directory before giving up.
296 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
297 if (block < nblocks) {
303 /* Clean up the read-ahead blocks */
304 for (; ra_ptr < ra_max; ra_ptr++)
305 brelse(bh_use[ra_ptr]);
312 * Try to find an entry of the provided name within 'dir'.
314 * If nothing was found, NULL is returned. Otherwise, a buffer_head
315 * and pointer to the dir entry are passed back.
317 * Caller can NOT assume anything about the contents of the
318 * buffer_head - it is passed back only so that it can be passed into
319 * any one of the manipulation functions (add entry, delete entry,
320 * etc). As an example, bh in the extent directory case is a data
321 * block, in the inline-data case it actually points to an inode.
323 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
325 struct ocfs2_dir_entry **res_dir)
329 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
330 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
332 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
336 * Update inode number and type of a previously found directory entry.
338 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
339 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
340 struct inode *new_entry_inode)
345 * The same code works fine for both inline-data and extent
346 * based directories, so no need to split this up.
349 ret = ocfs2_journal_access(handle, dir, de_bh,
350 OCFS2_JOURNAL_ACCESS_WRITE);
356 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
357 ocfs2_set_de_type(de, new_entry_inode->i_mode);
359 ocfs2_journal_dirty(handle, de_bh);
365 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
366 struct ocfs2_dir_entry *de_del,
367 struct buffer_head *bh, char *first_de,
370 struct ocfs2_dir_entry *de, *pde;
371 int i, status = -ENOENT;
373 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
377 de = (struct ocfs2_dir_entry *) first_de;
379 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
385 status = ocfs2_journal_access(handle, dir, bh,
386 OCFS2_JOURNAL_ACCESS_WRITE);
394 cpu_to_le16(le16_to_cpu(pde->rec_len) +
395 le16_to_cpu(de->rec_len));
399 status = ocfs2_journal_dirty(handle, bh);
402 i += le16_to_cpu(de->rec_len);
404 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
411 static inline int ocfs2_delete_entry_id(handle_t *handle,
413 struct ocfs2_dir_entry *de_del,
414 struct buffer_head *bh)
417 struct buffer_head *di_bh = NULL;
418 struct ocfs2_dinode *di;
419 struct ocfs2_inline_data *data;
421 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
422 &di_bh, OCFS2_BH_CACHED, dir);
428 di = (struct ocfs2_dinode *)di_bh->b_data;
429 data = &di->id2.i_data;
431 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
439 static inline int ocfs2_delete_entry_el(handle_t *handle,
441 struct ocfs2_dir_entry *de_del,
442 struct buffer_head *bh)
444 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
449 * ocfs2_delete_entry deletes a directory entry by merging it with the
452 int ocfs2_delete_entry(handle_t *handle,
454 struct ocfs2_dir_entry *de_del,
455 struct buffer_head *bh)
457 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
458 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
460 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
464 * Check whether 'de' has enough room to hold an entry of
465 * 'new_rec_len' bytes.
467 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
468 unsigned int new_rec_len)
470 unsigned int de_really_used;
472 /* Check whether this is an empty record with enough space */
473 if (le64_to_cpu(de->inode) == 0 &&
474 le16_to_cpu(de->rec_len) >= new_rec_len)
478 * Record might have free space at the end which we can
481 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
482 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
488 /* we don't always have a dentry for what we want to add, so people
489 * like orphan dir can call this instead.
491 * If you pass me insert_bh, I'll skip the search of the other dir
492 * blocks and put the record in there.
494 int __ocfs2_add_entry(handle_t *handle,
496 const char *name, int namelen,
497 struct inode *inode, u64 blkno,
498 struct buffer_head *parent_fe_bh,
499 struct buffer_head *insert_bh)
501 unsigned long offset;
502 unsigned short rec_len;
503 struct ocfs2_dir_entry *de, *de1;
504 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
505 struct super_block *sb = dir->i_sb;
507 unsigned int size = sb->s_blocksize;
508 char *data_start = insert_bh->b_data;
515 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
516 data_start = di->id2.i_data.id_data;
517 size = i_size_read(dir);
519 BUG_ON(insert_bh != parent_fe_bh);
522 rec_len = OCFS2_DIR_REC_LEN(namelen);
524 de = (struct ocfs2_dir_entry *) data_start;
526 BUG_ON((char *)de >= (size + data_start));
528 /* These checks should've already been passed by the
529 * prepare function, but I guess we can leave them
531 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
535 if (ocfs2_match(namelen, name, de)) {
540 if (ocfs2_dirent_would_fit(de, rec_len)) {
541 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
542 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
548 status = ocfs2_journal_access(handle, dir, insert_bh,
549 OCFS2_JOURNAL_ACCESS_WRITE);
550 /* By now the buffer is marked for journaling */
551 offset += le16_to_cpu(de->rec_len);
552 if (le64_to_cpu(de->inode)) {
553 de1 = (struct ocfs2_dir_entry *)((char *) de +
554 OCFS2_DIR_REC_LEN(de->name_len));
556 cpu_to_le16(le16_to_cpu(de->rec_len) -
557 OCFS2_DIR_REC_LEN(de->name_len));
558 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
561 de->file_type = OCFS2_FT_UNKNOWN;
563 de->inode = cpu_to_le64(blkno);
564 ocfs2_set_de_type(de, inode->i_mode);
567 de->name_len = namelen;
568 memcpy(de->name, name, namelen);
571 status = ocfs2_journal_dirty(handle, insert_bh);
575 offset += le16_to_cpu(de->rec_len);
576 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
579 /* when you think about it, the assert above should prevent us
580 * from ever getting here. */
588 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
589 unsigned long *f_version,
590 loff_t *f_pos, void *priv,
591 filldir_t filldir, int *filldir_err)
593 int ret, i, filldir_ret;
594 unsigned long offset = *f_pos;
595 struct buffer_head *di_bh = NULL;
596 struct ocfs2_dinode *di;
597 struct ocfs2_inline_data *data;
598 struct ocfs2_dir_entry *de;
600 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
601 &di_bh, OCFS2_BH_CACHED, inode);
603 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
604 (unsigned long long)OCFS2_I(inode)->ip_blkno);
608 di = (struct ocfs2_dinode *)di_bh->b_data;
609 data = &di->id2.i_data;
611 while (*f_pos < i_size_read(inode)) {
613 /* If the dir block has changed since the last call to
614 * readdir(2), then we might be pointing to an invalid
615 * dirent right now. Scan from the start of the block
617 if (*f_version != inode->i_version) {
618 for (i = 0; i < i_size_read(inode) && i < offset; ) {
619 de = (struct ocfs2_dir_entry *)
621 /* It's too expensive to do a full
622 * dirent test each time round this
623 * loop, but we do have to test at
624 * least that it is non-zero. A
625 * failure will be detected in the
626 * dirent test below. */
627 if (le16_to_cpu(de->rec_len) <
628 OCFS2_DIR_REC_LEN(1))
630 i += le16_to_cpu(de->rec_len);
633 *f_version = inode->i_version;
636 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
637 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
638 /* On error, skip the f_pos to the end. */
639 *f_pos = i_size_read(inode);
642 offset += le16_to_cpu(de->rec_len);
643 if (le64_to_cpu(de->inode)) {
644 /* We might block in the next section
645 * if the data destination is
646 * currently swapped out. So, use a
647 * version stamp to detect whether or
648 * not the directory has been modified
649 * during the copy operation.
651 unsigned long version = *f_version;
652 unsigned char d_type = DT_UNKNOWN;
654 if (de->file_type < OCFS2_FT_MAX)
655 d_type = ocfs2_filetype_table[de->file_type];
657 filldir_ret = filldir(priv, de->name,
660 le64_to_cpu(de->inode),
664 *filldir_err = filldir_ret;
667 if (version != *f_version)
670 *f_pos += le16_to_cpu(de->rec_len);
679 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
680 unsigned long *f_version,
681 loff_t *f_pos, void *priv,
682 filldir_t filldir, int *filldir_err)
685 unsigned long offset, blk, last_ra_blk = 0;
687 struct buffer_head * bh, * tmp;
688 struct ocfs2_dir_entry * de;
690 struct super_block * sb = inode->i_sb;
691 unsigned int ra_sectors = 16;
696 offset = (*f_pos) & (sb->s_blocksize - 1);
698 while (!error && !stored && *f_pos < i_size_read(inode)) {
699 blk = (*f_pos) >> sb->s_blocksize_bits;
700 bh = ocfs2_bread(inode, blk, &err, 0);
703 "directory #%llu contains a hole at offset %lld\n",
704 (unsigned long long)OCFS2_I(inode)->ip_blkno,
706 *f_pos += sb->s_blocksize - offset;
710 /* The idea here is to begin with 8k read-ahead and to stay
711 * 4k ahead of our current position.
713 * TODO: Use the pagecache for this. We just need to
714 * make sure it's cluster-safe... */
716 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
717 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
719 tmp = ocfs2_bread(inode, ++blk, &err, 1);
728 /* If the dir block has changed since the last call to
729 * readdir(2), then we might be pointing to an invalid
730 * dirent right now. Scan from the start of the block
732 if (*f_version != inode->i_version) {
733 for (i = 0; i < sb->s_blocksize && i < offset; ) {
734 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
735 /* It's too expensive to do a full
736 * dirent test each time round this
737 * loop, but we do have to test at
738 * least that it is non-zero. A
739 * failure will be detected in the
740 * dirent test below. */
741 if (le16_to_cpu(de->rec_len) <
742 OCFS2_DIR_REC_LEN(1))
744 i += le16_to_cpu(de->rec_len);
747 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
749 *f_version = inode->i_version;
752 while (!error && *f_pos < i_size_read(inode)
753 && offset < sb->s_blocksize) {
754 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
755 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
756 /* On error, skip the f_pos to the
758 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
762 offset += le16_to_cpu(de->rec_len);
763 if (le64_to_cpu(de->inode)) {
764 /* We might block in the next section
765 * if the data destination is
766 * currently swapped out. So, use a
767 * version stamp to detect whether or
768 * not the directory has been modified
769 * during the copy operation.
771 unsigned long version = *f_version;
772 unsigned char d_type = DT_UNKNOWN;
774 if (de->file_type < OCFS2_FT_MAX)
775 d_type = ocfs2_filetype_table[de->file_type];
776 error = filldir(priv, de->name,
779 le64_to_cpu(de->inode),
783 *filldir_err = error;
786 if (version != *f_version)
790 *f_pos += le16_to_cpu(de->rec_len);
801 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
802 loff_t *f_pos, void *priv, filldir_t filldir,
805 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
806 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
807 filldir, filldir_err);
809 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
814 * This is intended to be called from inside other kernel functions,
815 * so we fake some arguments.
817 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
820 int ret = 0, filldir_err = 0;
821 unsigned long version = inode->i_version;
823 while (*f_pos < i_size_read(inode)) {
824 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
825 filldir, &filldir_err);
826 if (ret || filldir_err)
840 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
843 struct inode *inode = filp->f_path.dentry->d_inode;
846 mlog_entry("dirino=%llu\n",
847 (unsigned long long)OCFS2_I(inode)->ip_blkno);
849 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
850 if (lock_level && error >= 0) {
851 /* We release EX lock which used to update atime
852 * and get PR lock again to reduce contention
853 * on commonly accessed directories. */
854 ocfs2_meta_unlock(inode, 1);
856 error = ocfs2_meta_lock(inode, NULL, 0);
859 if (error != -ENOENT)
861 /* we haven't got any yet, so propagate the error. */
865 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
866 dirent, filldir, NULL);
868 ocfs2_meta_unlock(inode, lock_level);
877 * NOTE: this should always be called with parent dir i_mutex taken.
879 int ocfs2_find_files_on_disk(const char *name,
883 struct buffer_head **dirent_bh,
884 struct ocfs2_dir_entry **dirent)
886 int status = -ENOENT;
888 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
889 namelen, name, blkno, inode, dirent_bh, dirent);
891 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
892 if (!*dirent_bh || !*dirent) {
897 *blkno = le64_to_cpu((*dirent)->inode);
914 * Convenience function for callers which just want the block number
915 * mapped to a name and don't require the full dirent info, etc.
917 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
918 int namelen, u64 *blkno)
921 struct buffer_head *bh = NULL;
922 struct ocfs2_dir_entry *dirent = NULL;
924 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
930 /* Check for a name within a directory.
932 * Return 0 if the name does not exist
933 * Return -EEXIST if the directory contains the name
935 * Callers should have i_mutex + a cluster lock on dir
937 int ocfs2_check_dir_for_entry(struct inode *dir,
942 struct buffer_head *dirent_bh = NULL;
943 struct ocfs2_dir_entry *dirent = NULL;
945 mlog_entry("dir %llu, name '%.*s'\n",
946 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
949 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
962 struct ocfs2_empty_dir_priv {
964 unsigned seen_dot_dot;
967 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
968 loff_t pos, u64 ino, unsigned type)
970 struct ocfs2_empty_dir_priv *p = priv;
973 * Check the positions of "." and ".." records to be sure
974 * they're in the correct place.
976 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
981 if (name_len == 2 && !strncmp("..", name, 2) &&
982 pos == OCFS2_DIR_REC_LEN(1)) {
991 * routine to check that the specified directory is empty (for rmdir)
993 * Returns 1 if dir is empty, zero otherwise.
995 int ocfs2_empty_dir(struct inode *inode)
999 struct ocfs2_empty_dir_priv priv;
1001 memset(&priv, 0, sizeof(priv));
1003 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1007 if (!priv.seen_dot || !priv.seen_dot_dot) {
1008 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1009 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1011 * XXX: Is it really safe to allow an unlink to continue?
1016 return !priv.seen_other;
1019 static void ocfs2_fill_initial_dirents(struct inode *inode,
1020 struct inode *parent,
1021 char *start, unsigned int size)
1023 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1025 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1028 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1029 strcpy(de->name, ".");
1030 ocfs2_set_de_type(de, S_IFDIR);
1032 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1033 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1034 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1036 strcpy(de->name, "..");
1037 ocfs2_set_de_type(de, S_IFDIR);
1041 * This works together with code in ocfs2_mknod_locked() which sets
1042 * the inline-data flag and initializes the inline-data section.
1044 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1046 struct inode *parent,
1047 struct inode *inode,
1048 struct buffer_head *di_bh)
1051 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1052 struct ocfs2_inline_data *data = &di->id2.i_data;
1053 unsigned int size = le16_to_cpu(data->id_count);
1055 ret = ocfs2_journal_access(handle, inode, di_bh,
1056 OCFS2_JOURNAL_ACCESS_WRITE);
1062 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1064 ocfs2_journal_dirty(handle, di_bh);
1070 i_size_write(inode, size);
1072 inode->i_blocks = ocfs2_inode_sector_count(inode);
1074 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1082 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1084 struct inode *parent,
1085 struct inode *inode,
1086 struct buffer_head *fe_bh,
1087 struct ocfs2_alloc_context *data_ac)
1090 struct buffer_head *new_bh = NULL;
1094 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1095 data_ac, NULL, &new_bh);
1101 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1103 status = ocfs2_journal_access(handle, inode, new_bh,
1104 OCFS2_JOURNAL_ACCESS_CREATE);
1109 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1111 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1112 osb->sb->s_blocksize);
1114 status = ocfs2_journal_dirty(handle, new_bh);
1120 i_size_write(inode, inode->i_sb->s_blocksize);
1122 inode->i_blocks = ocfs2_inode_sector_count(inode);
1123 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1138 int ocfs2_fill_new_dir(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)
1145 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1147 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1148 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1150 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1154 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1155 unsigned int new_size)
1157 struct ocfs2_dir_entry *de;
1158 struct ocfs2_dir_entry *prev_de;
1159 char *de_buf, *limit;
1160 unsigned int bytes = new_size - old_size;
1162 limit = start + old_size;
1164 de = (struct ocfs2_dir_entry *)de_buf;
1167 de_buf += le16_to_cpu(de->rec_len);
1168 de = (struct ocfs2_dir_entry *)de_buf;
1169 } while (de_buf < limit);
1171 le16_add_cpu(&prev_de->rec_len, bytes);
1175 * We allocate enough clusters to fulfill "blocks_wanted", but set
1176 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1177 * rest automatically for us.
1179 * *first_block_bh is a pointer to the 1st data block allocated to the
1182 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1183 unsigned int blocks_wanted,
1184 struct buffer_head **first_block_bh)
1186 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1187 u32 alloc, bit_off, len;
1188 struct super_block *sb = dir->i_sb;
1189 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1190 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1191 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1192 struct ocfs2_alloc_context *data_ac;
1193 struct buffer_head *dirdata_bh = NULL;
1194 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1197 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1200 * We should never need more than 2 clusters for this -
1201 * maximum dirent size is far less than one block. In fact,
1202 * the only time we'd need more than one cluster is if
1203 * blocksize == clustersize and the dirent won't fit in the
1204 * extra space that the expansion to a single block gives. As
1205 * of today, that only happens on 4k/4k file systems.
1209 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1215 down_write(&oi->ip_alloc_sem);
1218 * Prepare for worst case allocation scenario of two seperate
1222 credits += OCFS2_SUBALLOC_ALLOC;
1224 handle = ocfs2_start_trans(osb, credits);
1225 if (IS_ERR(handle)) {
1226 ret = PTR_ERR(handle);
1232 * Try to claim as many clusters as the bitmap can give though
1233 * if we only get one now, that's enough to continue. The rest
1234 * will be claimed after the conversion to extents.
1236 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1243 * Operations are carefully ordered so that we set up the new
1244 * data block first. The conversion from inline data to
1247 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1248 dirdata_bh = sb_getblk(sb, blkno);
1255 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1257 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1258 OCFS2_JOURNAL_ACCESS_CREATE);
1264 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1265 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1266 sb->s_blocksize - i_size_read(dir));
1267 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1270 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1277 * Set extent, i_size, etc on the directory. After this, the
1278 * inode should contain the same exact dirents as before and
1279 * be fully accessible from system calls.
1281 * We let the later dirent insert modify c/mtime - to the user
1282 * the data hasn't changed.
1284 ret = ocfs2_journal_access(handle, dir, di_bh,
1285 OCFS2_JOURNAL_ACCESS_CREATE);
1291 spin_lock(&oi->ip_lock);
1292 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1293 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1294 spin_unlock(&oi->ip_lock);
1296 ocfs2_dinode_new_extent_list(dir, di);
1298 i_size_write(dir, sb->s_blocksize);
1299 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1301 di->i_size = cpu_to_le64(sb->s_blocksize);
1302 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1303 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1304 dir->i_blocks = ocfs2_inode_sector_count(dir);
1307 * This should never fail as our extent list is empty and all
1308 * related blocks have been journaled already.
1310 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0,
1317 ret = ocfs2_journal_dirty(handle, di_bh);
1324 * We asked for two clusters, but only got one in the 1st
1325 * pass. Claim the 2nd cluster as a separate extent.
1328 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1334 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1336 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 1, blkno,
1344 *first_block_bh = dirdata_bh;
1348 ocfs2_commit_trans(osb, handle);
1351 up_write(&oi->ip_alloc_sem);
1355 ocfs2_free_alloc_context(data_ac);
1362 /* returns a bh of the 1st new block in the allocation. */
1363 static int ocfs2_do_extend_dir(struct super_block *sb,
1366 struct buffer_head *parent_fe_bh,
1367 struct ocfs2_alloc_context *data_ac,
1368 struct ocfs2_alloc_context *meta_ac,
1369 struct buffer_head **new_bh)
1373 u64 p_blkno, v_blkno;
1375 spin_lock(&OCFS2_I(dir)->ip_lock);
1376 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1377 spin_unlock(&OCFS2_I(dir)->ip_lock);
1380 u32 offset = OCFS2_I(dir)->ip_clusters;
1382 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
1383 1, 0, parent_fe_bh, handle,
1384 data_ac, meta_ac, NULL);
1385 BUG_ON(status == -EAGAIN);
1392 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1393 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1399 *new_bh = sb_getblk(sb, p_blkno);
1412 * Assumes you already have a cluster lock on the directory.
1414 * 'blocks_wanted' is only used if we have an inline directory which
1415 * is to be turned into an extent based one. The size of the dirent to
1416 * insert might be larger than the space gained by growing to just one
1417 * block, so we may have to grow the inode by two blocks in that case.
1419 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1421 struct buffer_head *parent_fe_bh,
1422 unsigned int blocks_wanted,
1423 struct buffer_head **new_de_bh)
1426 int credits, num_free_extents, drop_alloc_sem = 0;
1428 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1429 struct ocfs2_alloc_context *data_ac = NULL;
1430 struct ocfs2_alloc_context *meta_ac = NULL;
1431 handle_t *handle = NULL;
1432 struct buffer_head *new_bh = NULL;
1433 struct ocfs2_dir_entry * de;
1434 struct super_block *sb = osb->sb;
1438 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1439 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1440 blocks_wanted, &new_bh);
1446 if (blocks_wanted == 1) {
1448 * If the new dirent will fit inside the space
1449 * created by pushing out to one block, then
1450 * we can complete the operation
1451 * here. Otherwise we have to expand i_size
1452 * and format the 2nd block below.
1454 BUG_ON(new_bh == NULL);
1459 * Get rid of 'new_bh' - we want to format the 2nd
1460 * data block and return that instead.
1465 dir_i_size = i_size_read(dir);
1466 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1470 dir_i_size = i_size_read(dir);
1471 mlog(0, "extending dir %llu (i_size = %lld)\n",
1472 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1474 /* dir->i_size is always block aligned. */
1475 spin_lock(&OCFS2_I(dir)->ip_lock);
1476 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1477 spin_unlock(&OCFS2_I(dir)->ip_lock);
1478 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
1479 if (num_free_extents < 0) {
1480 status = num_free_extents;
1485 if (!num_free_extents) {
1486 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
1488 if (status != -ENOSPC)
1494 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1496 if (status != -ENOSPC)
1501 credits = ocfs2_calc_extend_credits(sb, fe, 1);
1503 spin_unlock(&OCFS2_I(dir)->ip_lock);
1504 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1508 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1511 handle = ocfs2_start_trans(osb, credits);
1512 if (IS_ERR(handle)) {
1513 status = PTR_ERR(handle);
1519 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1520 data_ac, meta_ac, &new_bh);
1526 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1528 status = ocfs2_journal_access(handle, dir, new_bh,
1529 OCFS2_JOURNAL_ACCESS_CREATE);
1534 memset(new_bh->b_data, 0, sb->s_blocksize);
1535 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1537 de->rec_len = cpu_to_le16(sb->s_blocksize);
1538 status = ocfs2_journal_dirty(handle, new_bh);
1544 dir_i_size += dir->i_sb->s_blocksize;
1545 i_size_write(dir, dir_i_size);
1546 dir->i_blocks = ocfs2_inode_sector_count(dir);
1547 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1554 *new_de_bh = new_bh;
1558 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1560 ocfs2_commit_trans(osb, handle);
1563 ocfs2_free_alloc_context(data_ac);
1565 ocfs2_free_alloc_context(meta_ac);
1574 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1575 const char *name, int namelen,
1576 struct buffer_head **ret_de_bh,
1577 unsigned int *blocks_wanted)
1580 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1581 struct ocfs2_dir_entry *de, *last_de = NULL;
1582 char *de_buf, *limit;
1583 unsigned long offset = 0;
1584 unsigned int rec_len, new_rec_len;
1586 de_buf = di->id2.i_data.id_data;
1587 limit = de_buf + i_size_read(dir);
1588 rec_len = OCFS2_DIR_REC_LEN(namelen);
1590 while (de_buf < limit) {
1591 de = (struct ocfs2_dir_entry *)de_buf;
1593 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1597 if (ocfs2_match(namelen, name, de)) {
1601 if (ocfs2_dirent_would_fit(de, rec_len)) {
1602 /* Ok, we found a spot. Return this bh and let
1603 * the caller actually fill it in. */
1611 de_buf += le16_to_cpu(de->rec_len);
1612 offset += le16_to_cpu(de->rec_len);
1616 * We're going to require expansion of the directory - figure
1617 * out how many blocks we'll need so that a place for the
1618 * dirent can be found.
1621 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1622 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1630 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1631 int namelen, struct buffer_head **ret_de_bh)
1633 unsigned long offset;
1634 struct buffer_head *bh = NULL;
1635 unsigned short rec_len;
1636 struct ocfs2_dir_entry *de;
1637 struct super_block *sb = dir->i_sb;
1640 bh = ocfs2_bread(dir, 0, &status, 0);
1646 rec_len = OCFS2_DIR_REC_LEN(namelen);
1648 de = (struct ocfs2_dir_entry *) bh->b_data;
1650 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1654 if (i_size_read(dir) <= offset) {
1656 * Caller will have to expand this
1662 bh = ocfs2_bread(dir,
1663 offset >> sb->s_blocksize_bits,
1670 /* move to next block */
1671 de = (struct ocfs2_dir_entry *) bh->b_data;
1673 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1677 if (ocfs2_match(namelen, name, de)) {
1681 if (ocfs2_dirent_would_fit(de, rec_len)) {
1682 /* Ok, we found a spot. Return this bh and let
1683 * the caller actually fill it in. */
1689 offset += le16_to_cpu(de->rec_len);
1690 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1702 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1704 struct buffer_head *parent_fe_bh,
1707 struct buffer_head **ret_de_bh)
1710 unsigned int blocks_wanted = 1;
1711 struct buffer_head *bh = NULL;
1713 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1714 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1724 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1725 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1726 namelen, &bh, &blocks_wanted);
1728 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1730 if (ret && ret != -ENOSPC) {
1735 if (ret == -ENOSPC) {
1737 * We have to expand the directory to add this name.
1741 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,