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 struct buffer_head **new_de_bh);
76 static int ocfs2_do_extend_dir(struct super_block *sb,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
84 static int ocfs2_check_dir_entry(struct inode * dir,
85 struct ocfs2_dir_entry * de,
86 struct buffer_head * bh,
89 const char *error_msg = NULL;
90 const int rlen = le16_to_cpu(de->rec_len);
92 if (rlen < OCFS2_DIR_REC_LEN(1))
93 error_msg = "rec_len is smaller than minimal";
94 else if (rlen % 4 != 0)
95 error_msg = "rec_len % 4 != 0";
96 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
97 error_msg = "rec_len is too small for name_len";
98 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
99 error_msg = "directory entry across blocks";
101 if (error_msg != NULL)
102 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
103 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
104 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
105 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
107 return error_msg == NULL ? 1 : 0;
110 static inline int ocfs2_match(int len,
111 const char * const name,
112 struct ocfs2_dir_entry *de)
114 if (len != de->name_len)
118 return !memcmp(name, de->name, len);
122 * Returns 0 if not found, -1 on failure, and 1 on success
124 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
126 const char *name, int namelen,
127 unsigned long offset,
128 struct ocfs2_dir_entry **res_dir)
130 struct ocfs2_dir_entry *de;
131 char *dlimit, *de_buf;
138 dlimit = de_buf + dir->i_sb->s_blocksize;
140 while (de_buf < dlimit) {
141 /* this code is executed quadratically often */
142 /* do minimal checking `by hand' */
144 de = (struct ocfs2_dir_entry *) de_buf;
146 if (de_buf + namelen <= dlimit &&
147 ocfs2_match(namelen, name, de)) {
148 /* found a match - just to be sure, do a full check */
149 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
158 /* prevent looping on a bad block */
159 de_len = le16_to_cpu(de->rec_len);
174 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
176 struct ocfs2_dir_entry **res_dir)
178 struct super_block *sb;
179 struct buffer_head *bh_use[NAMEI_RA_SIZE];
180 struct buffer_head *bh, *ret = NULL;
181 unsigned long start, block, b;
182 int ra_max = 0; /* Number of bh's in the readahead
184 int ra_ptr = 0; /* Current index into readahead
194 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
195 start = OCFS2_I(dir)->ip_dir_start_lookup;
196 if (start >= nblocks)
203 * We deal with the read-ahead logic here.
205 if (ra_ptr >= ra_max) {
206 /* Refill the readahead buffer */
209 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
211 * Terminate if we reach the end of the
212 * directory and must wrap, or if our
213 * search has finished at this block.
215 if (b >= nblocks || (num && block == start)) {
216 bh_use[ra_max] = NULL;
221 bh = ocfs2_bread(dir, b++, &err, 1);
225 if ((bh = bh_use[ra_ptr++]) == NULL)
228 if (!buffer_uptodate(bh)) {
229 /* read error, skip block & hope for the best */
230 ocfs2_error(dir->i_sb, "reading directory %llu, "
232 (unsigned long long)OCFS2_I(dir)->ip_blkno,
237 i = ocfs2_search_dirblock(bh, dir, name, namelen,
238 block << sb->s_blocksize_bits,
241 OCFS2_I(dir)->ip_dir_start_lookup = block;
243 goto cleanup_and_exit;
247 goto cleanup_and_exit;
250 if (++block >= nblocks)
252 } while (block != start);
255 * If the directory has grown while we were searching, then
256 * search the last part of the directory before giving up.
259 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
260 if (block < nblocks) {
266 /* Clean up the read-ahead blocks */
267 for (; ra_ptr < ra_max; ra_ptr++)
268 brelse(bh_use[ra_ptr]);
274 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
275 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
276 struct inode *new_entry_inode)
280 ret = ocfs2_journal_access(handle, dir, de_bh,
281 OCFS2_JOURNAL_ACCESS_WRITE);
287 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
288 ocfs2_set_de_type(de, new_entry_inode->i_mode);
290 ocfs2_journal_dirty(handle, de_bh);
297 * ocfs2_delete_entry deletes a directory entry by merging it with the
300 int ocfs2_delete_entry(handle_t *handle,
302 struct ocfs2_dir_entry *de_del,
303 struct buffer_head *bh)
305 struct ocfs2_dir_entry *de, *pde;
306 int i, status = -ENOENT;
308 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
312 de = (struct ocfs2_dir_entry *) bh->b_data;
313 while (i < bh->b_size) {
314 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
320 status = ocfs2_journal_access(handle, dir, bh,
321 OCFS2_JOURNAL_ACCESS_WRITE);
329 cpu_to_le16(le16_to_cpu(pde->rec_len) +
330 le16_to_cpu(de->rec_len));
334 status = ocfs2_journal_dirty(handle, bh);
337 i += le16_to_cpu(de->rec_len);
339 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
347 * Check whether 'de' has enough room to hold an entry of
348 * 'new_rec_len' bytes.
350 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
351 unsigned int new_rec_len)
353 unsigned int de_really_used;
355 /* Check whether this is an empty record with enough space */
356 if (le64_to_cpu(de->inode) == 0 &&
357 le16_to_cpu(de->rec_len) >= new_rec_len)
361 * Record might have free space at the end which we can
364 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
365 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
371 /* we don't always have a dentry for what we want to add, so people
372 * like orphan dir can call this instead.
374 * If you pass me insert_bh, I'll skip the search of the other dir
375 * blocks and put the record in there.
377 int __ocfs2_add_entry(handle_t *handle,
379 const char *name, int namelen,
380 struct inode *inode, u64 blkno,
381 struct buffer_head *parent_fe_bh,
382 struct buffer_head *insert_bh)
384 unsigned long offset;
385 unsigned short rec_len;
386 struct ocfs2_dir_entry *de, *de1;
387 struct super_block *sb;
397 rec_len = OCFS2_DIR_REC_LEN(namelen);
399 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
401 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
402 /* These checks should've already been passed by the
403 * prepare function, but I guess we can leave them
405 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
409 if (ocfs2_match(namelen, name, de)) {
414 if (ocfs2_dirent_would_fit(de, rec_len)) {
415 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
416 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
422 status = ocfs2_journal_access(handle, dir, insert_bh,
423 OCFS2_JOURNAL_ACCESS_WRITE);
424 /* By now the buffer is marked for journaling */
425 offset += le16_to_cpu(de->rec_len);
426 if (le64_to_cpu(de->inode)) {
427 de1 = (struct ocfs2_dir_entry *)((char *) de +
428 OCFS2_DIR_REC_LEN(de->name_len));
430 cpu_to_le16(le16_to_cpu(de->rec_len) -
431 OCFS2_DIR_REC_LEN(de->name_len));
432 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
435 de->file_type = OCFS2_FT_UNKNOWN;
437 de->inode = cpu_to_le64(blkno);
438 ocfs2_set_de_type(de, inode->i_mode);
441 de->name_len = namelen;
442 memcpy(de->name, name, namelen);
445 status = ocfs2_journal_dirty(handle, insert_bh);
449 offset += le16_to_cpu(de->rec_len);
450 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
453 /* when you think about it, the assert above should prevent us
454 * from ever getting here. */
462 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
463 loff_t *f_pos, void *priv, filldir_t filldir)
466 unsigned long offset, blk, last_ra_blk = 0;
468 struct buffer_head * bh, * tmp;
469 struct ocfs2_dir_entry * de;
471 struct super_block * sb = inode->i_sb;
472 unsigned int ra_sectors = 16;
477 offset = (*f_pos) & (sb->s_blocksize - 1);
479 while (!error && !stored && *f_pos < i_size_read(inode)) {
480 blk = (*f_pos) >> sb->s_blocksize_bits;
481 bh = ocfs2_bread(inode, blk, &err, 0);
484 "directory #%llu contains a hole at offset %lld\n",
485 (unsigned long long)OCFS2_I(inode)->ip_blkno,
487 *f_pos += sb->s_blocksize - offset;
491 /* The idea here is to begin with 8k read-ahead and to stay
492 * 4k ahead of our current position.
494 * TODO: Use the pagecache for this. We just need to
495 * make sure it's cluster-safe... */
497 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
498 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
500 tmp = ocfs2_bread(inode, ++blk, &err, 1);
509 /* If the dir block has changed since the last call to
510 * readdir(2), then we might be pointing to an invalid
511 * dirent right now. Scan from the start of the block
513 if (*f_version != inode->i_version) {
514 for (i = 0; i < sb->s_blocksize && i < offset; ) {
515 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
516 /* It's too expensive to do a full
517 * dirent test each time round this
518 * loop, but we do have to test at
519 * least that it is non-zero. A
520 * failure will be detected in the
521 * dirent test below. */
522 if (le16_to_cpu(de->rec_len) <
523 OCFS2_DIR_REC_LEN(1))
525 i += le16_to_cpu(de->rec_len);
528 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
530 *f_version = inode->i_version;
533 while (!error && *f_pos < i_size_read(inode)
534 && offset < sb->s_blocksize) {
535 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
536 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
537 /* On error, skip the f_pos to the
539 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
543 offset += le16_to_cpu(de->rec_len);
544 if (le64_to_cpu(de->inode)) {
545 /* We might block in the next section
546 * if the data destination is
547 * currently swapped out. So, use a
548 * version stamp to detect whether or
549 * not the directory has been modified
550 * during the copy operation.
552 unsigned long version = *f_version;
553 unsigned char d_type = DT_UNKNOWN;
555 if (de->file_type < OCFS2_FT_MAX)
556 d_type = ocfs2_filetype_table[de->file_type];
557 error = filldir(priv, de->name,
560 le64_to_cpu(de->inode),
564 if (version != *f_version)
568 *f_pos += le16_to_cpu(de->rec_len);
580 * This is intended to be called from inside other kernel functions,
581 * so we fake some arguments.
583 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
587 unsigned long version = inode->i_version;
589 while (*f_pos < i_size_read(inode)) {
590 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
603 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
606 struct inode *inode = filp->f_path.dentry->d_inode;
609 mlog_entry("dirino=%llu\n",
610 (unsigned long long)OCFS2_I(inode)->ip_blkno);
612 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
613 if (lock_level && error >= 0) {
614 /* We release EX lock which used to update atime
615 * and get PR lock again to reduce contention
616 * on commonly accessed directories. */
617 ocfs2_meta_unlock(inode, 1);
619 error = ocfs2_meta_lock(inode, NULL, 0);
622 if (error != -ENOENT)
624 /* we haven't got any yet, so propagate the error. */
628 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
631 ocfs2_meta_unlock(inode, lock_level);
640 * NOTE: this should always be called with parent dir i_mutex taken.
642 int ocfs2_find_files_on_disk(const char *name,
646 struct buffer_head **dirent_bh,
647 struct ocfs2_dir_entry **dirent)
649 int status = -ENOENT;
651 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
652 namelen, name, blkno, inode, dirent_bh, dirent);
654 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
655 if (!*dirent_bh || !*dirent) {
660 *blkno = le64_to_cpu((*dirent)->inode);
677 * Convenience function for callers which just want the block number
678 * mapped to a name and don't require the full dirent info, etc.
680 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
681 int namelen, u64 *blkno)
684 struct buffer_head *bh = NULL;
685 struct ocfs2_dir_entry *dirent = NULL;
687 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
693 /* Check for a name within a directory.
695 * Return 0 if the name does not exist
696 * Return -EEXIST if the directory contains the name
698 * Callers should have i_mutex + a cluster lock on dir
700 int ocfs2_check_dir_for_entry(struct inode *dir,
705 struct buffer_head *dirent_bh = NULL;
706 struct ocfs2_dir_entry *dirent = NULL;
708 mlog_entry("dir %llu, name '%.*s'\n",
709 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
712 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
725 struct ocfs2_empty_dir_priv {
727 unsigned seen_dot_dot;
730 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
731 loff_t pos, u64 ino, unsigned type)
733 struct ocfs2_empty_dir_priv *p = priv;
736 * Check the positions of "." and ".." records to be sure
737 * they're in the correct place.
739 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
744 if (name_len == 2 && !strncmp("..", name, 2) &&
745 pos == OCFS2_DIR_REC_LEN(1)) {
754 * routine to check that the specified directory is empty (for rmdir)
756 * Returns 1 if dir is empty, zero otherwise.
758 int ocfs2_empty_dir(struct inode *inode)
762 struct ocfs2_empty_dir_priv priv;
764 memset(&priv, 0, sizeof(priv));
766 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
770 if (!priv.seen_dot || !priv.seen_dot_dot) {
771 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
772 (unsigned long long)OCFS2_I(inode)->ip_blkno);
774 * XXX: Is it really safe to allow an unlink to continue?
779 return !priv.seen_other;
782 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
784 struct inode *parent,
786 struct buffer_head *fe_bh,
787 struct ocfs2_alloc_context *data_ac)
790 struct buffer_head *new_bh = NULL;
791 struct ocfs2_dir_entry *de = NULL;
795 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
796 data_ac, NULL, &new_bh);
802 ocfs2_set_new_buffer_uptodate(inode, new_bh);
804 status = ocfs2_journal_access(handle, inode, new_bh,
805 OCFS2_JOURNAL_ACCESS_CREATE);
810 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
812 de = (struct ocfs2_dir_entry *) new_bh->b_data;
813 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
816 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
817 strcpy(de->name, ".");
818 ocfs2_set_de_type(de, S_IFDIR);
819 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
820 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
821 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
822 OCFS2_DIR_REC_LEN(1));
824 strcpy(de->name, "..");
825 ocfs2_set_de_type(de, S_IFDIR);
827 status = ocfs2_journal_dirty(handle, new_bh);
833 i_size_write(inode, inode->i_sb->s_blocksize);
835 inode->i_blocks = ocfs2_inode_sector_count(inode);
836 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
851 /* returns a bh of the 1st new block in the allocation. */
852 static int ocfs2_do_extend_dir(struct super_block *sb,
855 struct buffer_head *parent_fe_bh,
856 struct ocfs2_alloc_context *data_ac,
857 struct ocfs2_alloc_context *meta_ac,
858 struct buffer_head **new_bh)
862 u64 p_blkno, v_blkno;
864 spin_lock(&OCFS2_I(dir)->ip_lock);
865 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
866 spin_unlock(&OCFS2_I(dir)->ip_lock);
869 u32 offset = OCFS2_I(dir)->ip_clusters;
871 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
872 1, 0, parent_fe_bh, handle,
873 data_ac, meta_ac, NULL);
874 BUG_ON(status == -EAGAIN);
881 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
882 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
888 *new_bh = sb_getblk(sb, p_blkno);
900 /* assumes you already have a cluster lock on the directory. */
901 static int ocfs2_extend_dir(struct ocfs2_super *osb,
903 struct buffer_head *parent_fe_bh,
904 struct buffer_head **new_de_bh)
907 int credits, num_free_extents, drop_alloc_sem = 0;
909 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
910 struct ocfs2_alloc_context *data_ac = NULL;
911 struct ocfs2_alloc_context *meta_ac = NULL;
912 handle_t *handle = NULL;
913 struct buffer_head *new_bh = NULL;
914 struct ocfs2_dir_entry * de;
915 struct super_block *sb = osb->sb;
919 dir_i_size = i_size_read(dir);
920 mlog(0, "extending dir %llu (i_size = %lld)\n",
921 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
923 /* dir->i_size is always block aligned. */
924 spin_lock(&OCFS2_I(dir)->ip_lock);
925 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
926 spin_unlock(&OCFS2_I(dir)->ip_lock);
927 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
928 if (num_free_extents < 0) {
929 status = num_free_extents;
934 if (!num_free_extents) {
935 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
937 if (status != -ENOSPC)
943 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
945 if (status != -ENOSPC)
950 credits = ocfs2_calc_extend_credits(sb, fe, 1);
952 spin_unlock(&OCFS2_I(dir)->ip_lock);
953 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
956 down_write(&OCFS2_I(dir)->ip_alloc_sem);
959 handle = ocfs2_start_trans(osb, credits);
960 if (IS_ERR(handle)) {
961 status = PTR_ERR(handle);
967 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
968 data_ac, meta_ac, &new_bh);
974 ocfs2_set_new_buffer_uptodate(dir, new_bh);
976 status = ocfs2_journal_access(handle, dir, new_bh,
977 OCFS2_JOURNAL_ACCESS_CREATE);
982 memset(new_bh->b_data, 0, sb->s_blocksize);
983 de = (struct ocfs2_dir_entry *) new_bh->b_data;
985 de->rec_len = cpu_to_le16(sb->s_blocksize);
986 status = ocfs2_journal_dirty(handle, new_bh);
992 dir_i_size += dir->i_sb->s_blocksize;
993 i_size_write(dir, dir_i_size);
994 dir->i_blocks = ocfs2_inode_sector_count(dir);
995 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1001 *new_de_bh = new_bh;
1005 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1007 ocfs2_commit_trans(osb, handle);
1010 ocfs2_free_alloc_context(data_ac);
1012 ocfs2_free_alloc_context(meta_ac);
1022 * Search the dir for a good spot, extending it if necessary. The
1023 * block containing an appropriate record is returned in ret_de_bh.
1025 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1027 struct buffer_head *parent_fe_bh,
1030 struct buffer_head **ret_de_bh)
1032 unsigned long offset;
1033 struct buffer_head * bh = NULL;
1034 unsigned short rec_len;
1035 struct ocfs2_dinode *fe;
1036 struct ocfs2_dir_entry *de;
1037 struct super_block *sb;
1042 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1043 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1045 BUG_ON(!S_ISDIR(dir->i_mode));
1046 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1047 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1057 bh = ocfs2_bread(dir, 0, &status, 0);
1063 rec_len = OCFS2_DIR_REC_LEN(namelen);
1065 de = (struct ocfs2_dir_entry *) bh->b_data;
1067 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1071 if (i_size_read(dir) <= offset) {
1072 status = ocfs2_extend_dir(osb,
1085 bh = ocfs2_bread(dir,
1086 offset >> sb->s_blocksize_bits,
1093 /* move to next block */
1094 de = (struct ocfs2_dir_entry *) bh->b_data;
1096 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1100 if (ocfs2_match(namelen, name, de)) {
1104 if (ocfs2_dirent_would_fit(de, rec_len)) {
1105 /* Ok, we found a spot. Return this bh and let
1106 * the caller actually fill it in. */
1112 offset += le16_to_cpu(de->rec_len);
1113 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));