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 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]);
275 * ocfs2_delete_entry deletes a directory entry by merging it with the
278 int ocfs2_delete_entry(handle_t *handle,
280 struct ocfs2_dir_entry *de_del,
281 struct buffer_head *bh)
283 struct ocfs2_dir_entry *de, *pde;
284 int i, status = -ENOENT;
286 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
290 de = (struct ocfs2_dir_entry *) bh->b_data;
291 while (i < bh->b_size) {
292 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
298 status = ocfs2_journal_access(handle, dir, bh,
299 OCFS2_JOURNAL_ACCESS_WRITE);
307 cpu_to_le16(le16_to_cpu(pde->rec_len) +
308 le16_to_cpu(de->rec_len));
312 status = ocfs2_journal_dirty(handle, bh);
315 i += le16_to_cpu(de->rec_len);
317 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
324 /* we don't always have a dentry for what we want to add, so people
325 * like orphan dir can call this instead.
327 * If you pass me insert_bh, I'll skip the search of the other dir
328 * blocks and put the record in there.
330 int __ocfs2_add_entry(handle_t *handle,
332 const char *name, int namelen,
333 struct inode *inode, u64 blkno,
334 struct buffer_head *parent_fe_bh,
335 struct buffer_head *insert_bh)
337 unsigned long offset;
338 unsigned short rec_len;
339 struct ocfs2_dir_entry *de, *de1;
340 struct super_block *sb;
350 rec_len = OCFS2_DIR_REC_LEN(namelen);
352 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
354 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
355 /* These checks should've already been passed by the
356 * prepare function, but I guess we can leave them
358 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
362 if (ocfs2_match(namelen, name, de)) {
366 if (((le64_to_cpu(de->inode) == 0) &&
367 (le16_to_cpu(de->rec_len) >= rec_len)) ||
368 (le16_to_cpu(de->rec_len) >=
369 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
370 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
371 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
377 status = ocfs2_journal_access(handle, dir, insert_bh,
378 OCFS2_JOURNAL_ACCESS_WRITE);
379 /* By now the buffer is marked for journaling */
380 offset += le16_to_cpu(de->rec_len);
381 if (le64_to_cpu(de->inode)) {
382 de1 = (struct ocfs2_dir_entry *)((char *) de +
383 OCFS2_DIR_REC_LEN(de->name_len));
385 cpu_to_le16(le16_to_cpu(de->rec_len) -
386 OCFS2_DIR_REC_LEN(de->name_len));
387 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
390 de->file_type = OCFS2_FT_UNKNOWN;
392 de->inode = cpu_to_le64(blkno);
393 ocfs2_set_de_type(de, inode->i_mode);
396 de->name_len = namelen;
397 memcpy(de->name, name, namelen);
400 status = ocfs2_journal_dirty(handle, insert_bh);
404 offset += le16_to_cpu(de->rec_len);
405 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
408 /* when you think about it, the assert above should prevent us
409 * from ever getting here. */
421 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
424 unsigned long offset, blk, last_ra_blk = 0;
426 struct buffer_head * bh, * tmp;
427 struct ocfs2_dir_entry * de;
429 struct inode *inode = filp->f_path.dentry->d_inode;
430 struct super_block * sb = inode->i_sb;
431 unsigned int ra_sectors = 16;
434 mlog_entry("dirino=%llu\n",
435 (unsigned long long)OCFS2_I(inode)->ip_blkno);
440 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
441 if (lock_level && error >= 0) {
442 /* We release EX lock which used to update atime
443 * and get PR lock again to reduce contention
444 * on commonly accessed directories. */
445 ocfs2_meta_unlock(inode, 1);
447 error = ocfs2_meta_lock(inode, NULL, 0);
450 if (error != -ENOENT)
452 /* we haven't got any yet, so propagate the error. */
457 offset = filp->f_pos & (sb->s_blocksize - 1);
459 while (!error && !stored && filp->f_pos < i_size_read(inode)) {
460 blk = (filp->f_pos) >> sb->s_blocksize_bits;
461 bh = ocfs2_bread(inode, blk, &err, 0);
464 "directory #%llu contains a hole at offset %lld\n",
465 (unsigned long long)OCFS2_I(inode)->ip_blkno,
467 filp->f_pos += sb->s_blocksize - offset;
471 /* The idea here is to begin with 8k read-ahead and to stay
472 * 4k ahead of our current position.
474 * TODO: Use the pagecache for this. We just need to
475 * make sure it's cluster-safe... */
477 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
478 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
480 tmp = ocfs2_bread(inode, ++blk, &err, 1);
489 /* If the dir block has changed since the last call to
490 * readdir(2), then we might be pointing to an invalid
491 * dirent right now. Scan from the start of the block
493 if (filp->f_version != inode->i_version) {
494 for (i = 0; i < sb->s_blocksize && i < offset; ) {
495 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
496 /* It's too expensive to do a full
497 * dirent test each time round this
498 * loop, but we do have to test at
499 * least that it is non-zero. A
500 * failure will be detected in the
501 * dirent test below. */
502 if (le16_to_cpu(de->rec_len) <
503 OCFS2_DIR_REC_LEN(1))
505 i += le16_to_cpu(de->rec_len);
508 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
510 filp->f_version = inode->i_version;
513 while (!error && filp->f_pos < i_size_read(inode)
514 && offset < sb->s_blocksize) {
515 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
516 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
517 /* On error, skip the f_pos to the
519 filp->f_pos = (filp->f_pos |
520 (sb->s_blocksize - 1)) + 1;
524 offset += le16_to_cpu(de->rec_len);
525 if (le64_to_cpu(de->inode)) {
526 /* We might block in the next section
527 * if the data destination is
528 * currently swapped out. So, use a
529 * version stamp to detect whether or
530 * not the directory has been modified
531 * during the copy operation.
533 unsigned long version = filp->f_version;
534 unsigned char d_type = DT_UNKNOWN;
536 if (de->file_type < OCFS2_FT_MAX)
537 d_type = ocfs2_filetype_table[de->file_type];
538 error = filldir(dirent, de->name,
541 ino_from_blkno(sb, le64_to_cpu(de->inode)),
545 if (version != filp->f_version)
549 filp->f_pos += le16_to_cpu(de->rec_len);
557 ocfs2_meta_unlock(inode, lock_level);
566 * NOTE: this should always be called with parent dir i_mutex taken.
568 int ocfs2_find_files_on_disk(const char *name,
572 struct buffer_head **dirent_bh,
573 struct ocfs2_dir_entry **dirent)
575 int status = -ENOENT;
577 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
578 namelen, name, blkno, inode, dirent_bh, dirent);
580 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
581 if (!*dirent_bh || !*dirent) {
586 *blkno = le64_to_cpu((*dirent)->inode);
602 /* Check for a name within a directory.
604 * Return 0 if the name does not exist
605 * Return -EEXIST if the directory contains the name
607 * Callers should have i_mutex + a cluster lock on dir
609 int ocfs2_check_dir_for_entry(struct inode *dir,
614 struct buffer_head *dirent_bh = NULL;
615 struct ocfs2_dir_entry *dirent = NULL;
617 mlog_entry("dir %llu, name '%.*s'\n",
618 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
621 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
635 * routine to check that the specified directory is empty (for rmdir)
637 int ocfs2_empty_dir(struct inode *inode)
639 unsigned long offset;
640 struct buffer_head * bh;
641 struct ocfs2_dir_entry * de, * de1;
642 struct super_block * sb;
646 if ((i_size_read(inode) <
647 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
648 !(bh = ocfs2_bread(inode, 0, &err, 0))) {
649 mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
650 (unsigned long long)OCFS2_I(inode)->ip_blkno);
654 de = (struct ocfs2_dir_entry *) bh->b_data;
655 de1 = (struct ocfs2_dir_entry *)
656 ((char *)de + le16_to_cpu(de->rec_len));
657 if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
658 !le64_to_cpu(de1->inode) ||
659 strcmp(".", de->name) ||
660 strcmp("..", de1->name)) {
661 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
662 (unsigned long long)OCFS2_I(inode)->ip_blkno);
666 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
667 de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
668 while (offset < i_size_read(inode) ) {
669 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
671 bh = ocfs2_bread(inode,
672 offset >> sb->s_blocksize_bits, &err, 0);
674 mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
675 (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
676 offset += sb->s_blocksize;
679 de = (struct ocfs2_dir_entry *) bh->b_data;
681 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
685 if (le64_to_cpu(de->inode)) {
689 offset += le16_to_cpu(de->rec_len);
690 de = (struct ocfs2_dir_entry *)
691 ((char *)de + le16_to_cpu(de->rec_len));
697 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
699 struct inode *parent,
701 struct buffer_head *fe_bh,
702 struct ocfs2_alloc_context *data_ac)
705 struct buffer_head *new_bh = NULL;
706 struct ocfs2_dir_entry *de = NULL;
710 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
711 data_ac, NULL, &new_bh);
717 ocfs2_set_new_buffer_uptodate(inode, new_bh);
719 status = ocfs2_journal_access(handle, inode, new_bh,
720 OCFS2_JOURNAL_ACCESS_CREATE);
725 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
727 de = (struct ocfs2_dir_entry *) new_bh->b_data;
728 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
731 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
732 strcpy(de->name, ".");
733 ocfs2_set_de_type(de, S_IFDIR);
734 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
735 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
736 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
737 OCFS2_DIR_REC_LEN(1));
739 strcpy(de->name, "..");
740 ocfs2_set_de_type(de, S_IFDIR);
742 status = ocfs2_journal_dirty(handle, new_bh);
748 i_size_write(inode, inode->i_sb->s_blocksize);
750 inode->i_blocks = ocfs2_inode_sector_count(inode);
751 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
766 /* returns a bh of the 1st new block in the allocation. */
767 static int ocfs2_do_extend_dir(struct super_block *sb,
770 struct buffer_head *parent_fe_bh,
771 struct ocfs2_alloc_context *data_ac,
772 struct ocfs2_alloc_context *meta_ac,
773 struct buffer_head **new_bh)
777 u64 p_blkno, v_blkno;
779 spin_lock(&OCFS2_I(dir)->ip_lock);
780 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
781 spin_unlock(&OCFS2_I(dir)->ip_lock);
784 u32 offset = OCFS2_I(dir)->ip_clusters;
786 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
787 1, 0, parent_fe_bh, handle,
788 data_ac, meta_ac, NULL);
789 BUG_ON(status == -EAGAIN);
796 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
797 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
803 *new_bh = sb_getblk(sb, p_blkno);
815 /* assumes you already have a cluster lock on the directory. */
816 static int ocfs2_extend_dir(struct ocfs2_super *osb,
818 struct buffer_head *parent_fe_bh,
819 struct buffer_head **new_de_bh)
822 int credits, num_free_extents, drop_alloc_sem = 0;
824 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
825 struct ocfs2_alloc_context *data_ac = NULL;
826 struct ocfs2_alloc_context *meta_ac = NULL;
827 handle_t *handle = NULL;
828 struct buffer_head *new_bh = NULL;
829 struct ocfs2_dir_entry * de;
830 struct super_block *sb = osb->sb;
834 dir_i_size = i_size_read(dir);
835 mlog(0, "extending dir %llu (i_size = %lld)\n",
836 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
838 /* dir->i_size is always block aligned. */
839 spin_lock(&OCFS2_I(dir)->ip_lock);
840 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
841 spin_unlock(&OCFS2_I(dir)->ip_lock);
842 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
843 if (num_free_extents < 0) {
844 status = num_free_extents;
849 if (!num_free_extents) {
850 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
852 if (status != -ENOSPC)
858 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
860 if (status != -ENOSPC)
865 credits = ocfs2_calc_extend_credits(sb, fe, 1);
867 spin_unlock(&OCFS2_I(dir)->ip_lock);
868 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
871 down_write(&OCFS2_I(dir)->ip_alloc_sem);
874 handle = ocfs2_start_trans(osb, credits);
875 if (IS_ERR(handle)) {
876 status = PTR_ERR(handle);
882 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
883 data_ac, meta_ac, &new_bh);
889 ocfs2_set_new_buffer_uptodate(dir, new_bh);
891 status = ocfs2_journal_access(handle, dir, new_bh,
892 OCFS2_JOURNAL_ACCESS_CREATE);
897 memset(new_bh->b_data, 0, sb->s_blocksize);
898 de = (struct ocfs2_dir_entry *) new_bh->b_data;
900 de->rec_len = cpu_to_le16(sb->s_blocksize);
901 status = ocfs2_journal_dirty(handle, new_bh);
907 dir_i_size += dir->i_sb->s_blocksize;
908 i_size_write(dir, dir_i_size);
909 dir->i_blocks = ocfs2_inode_sector_count(dir);
910 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
920 up_write(&OCFS2_I(dir)->ip_alloc_sem);
922 ocfs2_commit_trans(osb, handle);
925 ocfs2_free_alloc_context(data_ac);
927 ocfs2_free_alloc_context(meta_ac);
937 * Search the dir for a good spot, extending it if necessary. The
938 * block containing an appropriate record is returned in ret_de_bh.
940 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
942 struct buffer_head *parent_fe_bh,
945 struct buffer_head **ret_de_bh)
947 unsigned long offset;
948 struct buffer_head * bh = NULL;
949 unsigned short rec_len;
950 struct ocfs2_dinode *fe;
951 struct ocfs2_dir_entry *de;
952 struct super_block *sb;
957 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
958 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
960 BUG_ON(!S_ISDIR(dir->i_mode));
961 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
962 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
972 bh = ocfs2_bread(dir, 0, &status, 0);
978 rec_len = OCFS2_DIR_REC_LEN(namelen);
980 de = (struct ocfs2_dir_entry *) bh->b_data;
982 if ((char *)de >= sb->s_blocksize + bh->b_data) {
986 if (i_size_read(dir) <= offset) {
987 status = ocfs2_extend_dir(osb,
1000 bh = ocfs2_bread(dir,
1001 offset >> sb->s_blocksize_bits,
1008 /* move to next block */
1009 de = (struct ocfs2_dir_entry *) bh->b_data;
1011 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1015 if (ocfs2_match(namelen, name, de)) {
1019 if (((le64_to_cpu(de->inode) == 0) &&
1020 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1021 (le16_to_cpu(de->rec_len) >=
1022 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1023 /* Ok, we found a spot. Return this bh and let
1024 * the caller actually fill it in. */
1030 offset += le16_to_cpu(de->rec_len);
1031 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));