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));
346 /* we don't always have a dentry for what we want to add, so people
347 * like orphan dir can call this instead.
349 * If you pass me insert_bh, I'll skip the search of the other dir
350 * blocks and put the record in there.
352 int __ocfs2_add_entry(handle_t *handle,
354 const char *name, int namelen,
355 struct inode *inode, u64 blkno,
356 struct buffer_head *parent_fe_bh,
357 struct buffer_head *insert_bh)
359 unsigned long offset;
360 unsigned short rec_len;
361 struct ocfs2_dir_entry *de, *de1;
362 struct super_block *sb;
372 rec_len = OCFS2_DIR_REC_LEN(namelen);
374 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
376 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
377 /* These checks should've already been passed by the
378 * prepare function, but I guess we can leave them
380 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
384 if (ocfs2_match(namelen, name, de)) {
388 if (((le64_to_cpu(de->inode) == 0) &&
389 (le16_to_cpu(de->rec_len) >= rec_len)) ||
390 (le16_to_cpu(de->rec_len) >=
391 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
392 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
393 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
399 status = ocfs2_journal_access(handle, dir, insert_bh,
400 OCFS2_JOURNAL_ACCESS_WRITE);
401 /* By now the buffer is marked for journaling */
402 offset += le16_to_cpu(de->rec_len);
403 if (le64_to_cpu(de->inode)) {
404 de1 = (struct ocfs2_dir_entry *)((char *) de +
405 OCFS2_DIR_REC_LEN(de->name_len));
407 cpu_to_le16(le16_to_cpu(de->rec_len) -
408 OCFS2_DIR_REC_LEN(de->name_len));
409 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
412 de->file_type = OCFS2_FT_UNKNOWN;
414 de->inode = cpu_to_le64(blkno);
415 ocfs2_set_de_type(de, inode->i_mode);
418 de->name_len = namelen;
419 memcpy(de->name, name, namelen);
422 status = ocfs2_journal_dirty(handle, insert_bh);
426 offset += le16_to_cpu(de->rec_len);
427 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
430 /* when you think about it, the assert above should prevent us
431 * from ever getting here. */
439 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
440 loff_t *f_pos, void *priv, filldir_t filldir)
443 unsigned long offset, blk, last_ra_blk = 0;
445 struct buffer_head * bh, * tmp;
446 struct ocfs2_dir_entry * de;
448 struct super_block * sb = inode->i_sb;
449 unsigned int ra_sectors = 16;
454 offset = (*f_pos) & (sb->s_blocksize - 1);
456 while (!error && !stored && *f_pos < i_size_read(inode)) {
457 blk = (*f_pos) >> sb->s_blocksize_bits;
458 bh = ocfs2_bread(inode, blk, &err, 0);
461 "directory #%llu contains a hole at offset %lld\n",
462 (unsigned long long)OCFS2_I(inode)->ip_blkno,
464 *f_pos += sb->s_blocksize - offset;
468 /* The idea here is to begin with 8k read-ahead and to stay
469 * 4k ahead of our current position.
471 * TODO: Use the pagecache for this. We just need to
472 * make sure it's cluster-safe... */
474 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
475 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
477 tmp = ocfs2_bread(inode, ++blk, &err, 1);
486 /* If the dir block has changed since the last call to
487 * readdir(2), then we might be pointing to an invalid
488 * dirent right now. Scan from the start of the block
490 if (*f_version != inode->i_version) {
491 for (i = 0; i < sb->s_blocksize && i < offset; ) {
492 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
493 /* It's too expensive to do a full
494 * dirent test each time round this
495 * loop, but we do have to test at
496 * least that it is non-zero. A
497 * failure will be detected in the
498 * dirent test below. */
499 if (le16_to_cpu(de->rec_len) <
500 OCFS2_DIR_REC_LEN(1))
502 i += le16_to_cpu(de->rec_len);
505 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
507 *f_version = inode->i_version;
510 while (!error && *f_pos < i_size_read(inode)
511 && offset < sb->s_blocksize) {
512 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
513 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
514 /* On error, skip the f_pos to the
516 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
520 offset += le16_to_cpu(de->rec_len);
521 if (le64_to_cpu(de->inode)) {
522 /* We might block in the next section
523 * if the data destination is
524 * currently swapped out. So, use a
525 * version stamp to detect whether or
526 * not the directory has been modified
527 * during the copy operation.
529 unsigned long version = *f_version;
530 unsigned char d_type = DT_UNKNOWN;
532 if (de->file_type < OCFS2_FT_MAX)
533 d_type = ocfs2_filetype_table[de->file_type];
534 error = filldir(priv, de->name,
537 le64_to_cpu(de->inode),
541 if (version != *f_version)
545 *f_pos += le16_to_cpu(de->rec_len);
557 * This is intended to be called from inside other kernel functions,
558 * so we fake some arguments.
560 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
564 unsigned long version = inode->i_version;
566 while (*f_pos < i_size_read(inode)) {
567 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
580 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
583 struct inode *inode = filp->f_path.dentry->d_inode;
586 mlog_entry("dirino=%llu\n",
587 (unsigned long long)OCFS2_I(inode)->ip_blkno);
589 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
590 if (lock_level && error >= 0) {
591 /* We release EX lock which used to update atime
592 * and get PR lock again to reduce contention
593 * on commonly accessed directories. */
594 ocfs2_meta_unlock(inode, 1);
596 error = ocfs2_meta_lock(inode, NULL, 0);
599 if (error != -ENOENT)
601 /* we haven't got any yet, so propagate the error. */
605 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
608 ocfs2_meta_unlock(inode, lock_level);
617 * NOTE: this should always be called with parent dir i_mutex taken.
619 int ocfs2_find_files_on_disk(const char *name,
623 struct buffer_head **dirent_bh,
624 struct ocfs2_dir_entry **dirent)
626 int status = -ENOENT;
628 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
629 namelen, name, blkno, inode, dirent_bh, dirent);
631 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
632 if (!*dirent_bh || !*dirent) {
637 *blkno = le64_to_cpu((*dirent)->inode);
654 * Convenience function for callers which just want the block number
655 * mapped to a name and don't require the full dirent info, etc.
657 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
658 int namelen, u64 *blkno)
661 struct buffer_head *bh = NULL;
662 struct ocfs2_dir_entry *dirent = NULL;
664 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
670 /* Check for a name within a directory.
672 * Return 0 if the name does not exist
673 * Return -EEXIST if the directory contains the name
675 * Callers should have i_mutex + a cluster lock on dir
677 int ocfs2_check_dir_for_entry(struct inode *dir,
682 struct buffer_head *dirent_bh = NULL;
683 struct ocfs2_dir_entry *dirent = NULL;
685 mlog_entry("dir %llu, name '%.*s'\n",
686 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
689 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
702 struct ocfs2_empty_dir_priv {
704 unsigned seen_dot_dot;
707 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
708 loff_t pos, u64 ino, unsigned type)
710 struct ocfs2_empty_dir_priv *p = priv;
713 * Check the positions of "." and ".." records to be sure
714 * they're in the correct place.
716 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
721 if (name_len == 2 && !strncmp("..", name, 2) &&
722 pos == OCFS2_DIR_REC_LEN(1)) {
731 * routine to check that the specified directory is empty (for rmdir)
733 * Returns 1 if dir is empty, zero otherwise.
735 int ocfs2_empty_dir(struct inode *inode)
739 struct ocfs2_empty_dir_priv priv;
741 memset(&priv, 0, sizeof(priv));
743 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
747 if (!priv.seen_dot || !priv.seen_dot_dot) {
748 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
749 (unsigned long long)OCFS2_I(inode)->ip_blkno);
751 * XXX: Is it really safe to allow an unlink to continue?
756 return !priv.seen_other;
759 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
761 struct inode *parent,
763 struct buffer_head *fe_bh,
764 struct ocfs2_alloc_context *data_ac)
767 struct buffer_head *new_bh = NULL;
768 struct ocfs2_dir_entry *de = NULL;
772 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
773 data_ac, NULL, &new_bh);
779 ocfs2_set_new_buffer_uptodate(inode, new_bh);
781 status = ocfs2_journal_access(handle, inode, new_bh,
782 OCFS2_JOURNAL_ACCESS_CREATE);
787 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
789 de = (struct ocfs2_dir_entry *) new_bh->b_data;
790 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
793 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
794 strcpy(de->name, ".");
795 ocfs2_set_de_type(de, S_IFDIR);
796 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
797 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
798 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
799 OCFS2_DIR_REC_LEN(1));
801 strcpy(de->name, "..");
802 ocfs2_set_de_type(de, S_IFDIR);
804 status = ocfs2_journal_dirty(handle, new_bh);
810 i_size_write(inode, inode->i_sb->s_blocksize);
812 inode->i_blocks = ocfs2_inode_sector_count(inode);
813 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
828 /* returns a bh of the 1st new block in the allocation. */
829 static int ocfs2_do_extend_dir(struct super_block *sb,
832 struct buffer_head *parent_fe_bh,
833 struct ocfs2_alloc_context *data_ac,
834 struct ocfs2_alloc_context *meta_ac,
835 struct buffer_head **new_bh)
839 u64 p_blkno, v_blkno;
841 spin_lock(&OCFS2_I(dir)->ip_lock);
842 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
843 spin_unlock(&OCFS2_I(dir)->ip_lock);
846 u32 offset = OCFS2_I(dir)->ip_clusters;
848 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
849 1, 0, parent_fe_bh, handle,
850 data_ac, meta_ac, NULL);
851 BUG_ON(status == -EAGAIN);
858 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
859 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
865 *new_bh = sb_getblk(sb, p_blkno);
877 /* assumes you already have a cluster lock on the directory. */
878 static int ocfs2_extend_dir(struct ocfs2_super *osb,
880 struct buffer_head *parent_fe_bh,
881 struct buffer_head **new_de_bh)
884 int credits, num_free_extents, drop_alloc_sem = 0;
886 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
887 struct ocfs2_alloc_context *data_ac = NULL;
888 struct ocfs2_alloc_context *meta_ac = NULL;
889 handle_t *handle = NULL;
890 struct buffer_head *new_bh = NULL;
891 struct ocfs2_dir_entry * de;
892 struct super_block *sb = osb->sb;
896 dir_i_size = i_size_read(dir);
897 mlog(0, "extending dir %llu (i_size = %lld)\n",
898 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
900 /* dir->i_size is always block aligned. */
901 spin_lock(&OCFS2_I(dir)->ip_lock);
902 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
903 spin_unlock(&OCFS2_I(dir)->ip_lock);
904 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
905 if (num_free_extents < 0) {
906 status = num_free_extents;
911 if (!num_free_extents) {
912 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
914 if (status != -ENOSPC)
920 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
922 if (status != -ENOSPC)
927 credits = ocfs2_calc_extend_credits(sb, fe, 1);
929 spin_unlock(&OCFS2_I(dir)->ip_lock);
930 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
933 down_write(&OCFS2_I(dir)->ip_alloc_sem);
936 handle = ocfs2_start_trans(osb, credits);
937 if (IS_ERR(handle)) {
938 status = PTR_ERR(handle);
944 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
945 data_ac, meta_ac, &new_bh);
951 ocfs2_set_new_buffer_uptodate(dir, new_bh);
953 status = ocfs2_journal_access(handle, dir, new_bh,
954 OCFS2_JOURNAL_ACCESS_CREATE);
959 memset(new_bh->b_data, 0, sb->s_blocksize);
960 de = (struct ocfs2_dir_entry *) new_bh->b_data;
962 de->rec_len = cpu_to_le16(sb->s_blocksize);
963 status = ocfs2_journal_dirty(handle, new_bh);
969 dir_i_size += dir->i_sb->s_blocksize;
970 i_size_write(dir, dir_i_size);
971 dir->i_blocks = ocfs2_inode_sector_count(dir);
972 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
982 up_write(&OCFS2_I(dir)->ip_alloc_sem);
984 ocfs2_commit_trans(osb, handle);
987 ocfs2_free_alloc_context(data_ac);
989 ocfs2_free_alloc_context(meta_ac);
999 * Search the dir for a good spot, extending it if necessary. The
1000 * block containing an appropriate record is returned in ret_de_bh.
1002 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1004 struct buffer_head *parent_fe_bh,
1007 struct buffer_head **ret_de_bh)
1009 unsigned long offset;
1010 struct buffer_head * bh = NULL;
1011 unsigned short rec_len;
1012 struct ocfs2_dinode *fe;
1013 struct ocfs2_dir_entry *de;
1014 struct super_block *sb;
1019 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1020 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1022 BUG_ON(!S_ISDIR(dir->i_mode));
1023 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1024 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1034 bh = ocfs2_bread(dir, 0, &status, 0);
1040 rec_len = OCFS2_DIR_REC_LEN(namelen);
1042 de = (struct ocfs2_dir_entry *) bh->b_data;
1044 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1048 if (i_size_read(dir) <= offset) {
1049 status = ocfs2_extend_dir(osb,
1062 bh = ocfs2_bread(dir,
1063 offset >> sb->s_blocksize_bits,
1070 /* move to next block */
1071 de = (struct ocfs2_dir_entry *) bh->b_data;
1073 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1077 if (ocfs2_match(namelen, name, de)) {
1081 if (((le64_to_cpu(de->inode) == 0) &&
1082 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1083 (le16_to_cpu(de->rec_len) >=
1084 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1085 /* Ok, we found a spot. Return this bh and let
1086 * the caller actually fill it in. */
1092 offset += le16_to_cpu(de->rec_len);
1093 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));