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>
43 #include <linux/quotaops.h>
45 #define MLOG_MASK_PREFIX ML_NAMEI
46 #include <cluster/masklog.h>
51 #include "blockcheck.h"
54 #include "extent_map.h"
63 #include "buffer_head_io.h"
65 #define NAMEI_RA_CHUNKS 2
66 #define NAMEI_RA_BLOCKS 4
67 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
68 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
70 static unsigned char ocfs2_filetype_table
[] = {
71 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
74 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
76 struct buffer_head
*parent_fe_bh
,
77 unsigned int blocks_wanted
,
78 struct buffer_head
**new_de_bh
);
79 static int ocfs2_do_extend_dir(struct super_block
*sb
,
82 struct buffer_head
*parent_fe_bh
,
83 struct ocfs2_alloc_context
*data_ac
,
84 struct ocfs2_alloc_context
*meta_ac
,
85 struct buffer_head
**new_bh
);
88 * These are distinct checks because future versions of the file system will
89 * want to have a trailing dirent structure independent of indexing.
91 static int ocfs2_dir_has_trailer(struct inode
*dir
)
93 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
96 return ocfs2_meta_ecc(OCFS2_SB(dir
->i_sb
));
99 static int ocfs2_supports_dir_trailer(struct ocfs2_super
*osb
)
101 return ocfs2_meta_ecc(osb
);
104 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block
*sb
)
106 return sb
->s_blocksize
- sizeof(struct ocfs2_dir_block_trailer
);
109 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
111 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
112 * them more consistent? */
113 struct ocfs2_dir_block_trailer
*ocfs2_dir_trailer_from_size(int blocksize
,
118 p
+= blocksize
- sizeof(struct ocfs2_dir_block_trailer
);
119 return (struct ocfs2_dir_block_trailer
*)p
;
123 * XXX: This is executed once on every dirent. We should consider optimizing
126 static int ocfs2_skip_dir_trailer(struct inode
*dir
,
127 struct ocfs2_dir_entry
*de
,
128 unsigned long offset
,
129 unsigned long blklen
)
131 unsigned long toff
= blklen
- sizeof(struct ocfs2_dir_block_trailer
);
133 if (!ocfs2_dir_has_trailer(dir
))
142 static void ocfs2_init_dir_trailer(struct inode
*inode
,
143 struct buffer_head
*bh
)
145 struct ocfs2_dir_block_trailer
*trailer
;
147 trailer
= ocfs2_trailer_from_bh(bh
, inode
->i_sb
);
148 strcpy(trailer
->db_signature
, OCFS2_DIR_TRAILER_SIGNATURE
);
149 trailer
->db_compat_rec_len
=
150 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer
));
151 trailer
->db_parent_dinode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
152 trailer
->db_blkno
= cpu_to_le64(bh
->b_blocknr
);
156 * bh passed here can be an inode block or a dir data block, depending
157 * on the inode inline data flag.
159 static int ocfs2_check_dir_entry(struct inode
* dir
,
160 struct ocfs2_dir_entry
* de
,
161 struct buffer_head
* bh
,
162 unsigned long offset
)
164 const char *error_msg
= NULL
;
165 const int rlen
= le16_to_cpu(de
->rec_len
);
167 if (rlen
< OCFS2_DIR_REC_LEN(1))
168 error_msg
= "rec_len is smaller than minimal";
169 else if (rlen
% 4 != 0)
170 error_msg
= "rec_len % 4 != 0";
171 else if (rlen
< OCFS2_DIR_REC_LEN(de
->name_len
))
172 error_msg
= "rec_len is too small for name_len";
173 else if (((char *) de
- bh
->b_data
) + rlen
> dir
->i_sb
->s_blocksize
)
174 error_msg
= "directory entry across blocks";
176 if (error_msg
!= NULL
)
177 mlog(ML_ERROR
, "bad entry in directory #%llu: %s - "
178 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
179 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, error_msg
,
180 offset
, (unsigned long long)le64_to_cpu(de
->inode
), rlen
,
182 return error_msg
== NULL
? 1 : 0;
185 static inline int ocfs2_match(int len
,
186 const char * const name
,
187 struct ocfs2_dir_entry
*de
)
189 if (len
!= de
->name_len
)
193 return !memcmp(name
, de
->name
, len
);
197 * Returns 0 if not found, -1 on failure, and 1 on success
199 static int inline ocfs2_search_dirblock(struct buffer_head
*bh
,
201 const char *name
, int namelen
,
202 unsigned long offset
,
205 struct ocfs2_dir_entry
**res_dir
)
207 struct ocfs2_dir_entry
*de
;
208 char *dlimit
, *de_buf
;
215 dlimit
= de_buf
+ bytes
;
217 while (de_buf
< dlimit
) {
218 /* this code is executed quadratically often */
219 /* do minimal checking `by hand' */
221 de
= (struct ocfs2_dir_entry
*) de_buf
;
223 if (de_buf
+ namelen
<= dlimit
&&
224 ocfs2_match(namelen
, name
, de
)) {
225 /* found a match - just to be sure, do a full check */
226 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
235 /* prevent looping on a bad block */
236 de_len
= le16_to_cpu(de
->rec_len
);
251 static struct buffer_head
*ocfs2_find_entry_id(const char *name
,
254 struct ocfs2_dir_entry
**res_dir
)
257 struct buffer_head
*di_bh
= NULL
;
258 struct ocfs2_dinode
*di
;
259 struct ocfs2_inline_data
*data
;
261 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
267 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
268 data
= &di
->id2
.i_data
;
270 found
= ocfs2_search_dirblock(di_bh
, dir
, name
, namelen
, 0,
271 data
->id_data
, i_size_read(dir
), res_dir
);
280 static int ocfs2_validate_dir_block(struct super_block
*sb
,
281 struct buffer_head
*bh
)
284 struct ocfs2_dir_block_trailer
*trailer
=
285 ocfs2_trailer_from_bh(bh
, sb
);
289 * We don't validate dirents here, that's handled
290 * in-place when the code walks them.
292 mlog(0, "Validating dirblock %llu\n",
293 (unsigned long long)bh
->b_blocknr
);
295 BUG_ON(!buffer_uptodate(bh
));
298 * If the ecc fails, we return the error but otherwise
299 * leave the filesystem running. We know any error is
300 * local to this block.
302 * Note that we are safe to call this even if the directory
303 * doesn't have a trailer. Filesystems without metaecc will do
304 * nothing, and filesystems with it will have one.
306 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &trailer
->db_check
);
308 mlog(ML_ERROR
, "Checksum failed for dinode %llu\n",
309 (unsigned long long)bh
->b_blocknr
);
315 * This function forces all errors to -EIO for consistency with its
316 * predecessor, ocfs2_bread(). We haven't audited what returning the
317 * real error codes would do to callers. We log the real codes with
318 * mlog_errno() before we squash them.
320 static int ocfs2_read_dir_block(struct inode
*inode
, u64 v_block
,
321 struct buffer_head
**bh
, int flags
)
324 struct buffer_head
*tmp
= *bh
;
325 struct ocfs2_dir_block_trailer
*trailer
;
327 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, flags
,
328 ocfs2_validate_dir_block
);
335 * We check the trailer here rather than in
336 * ocfs2_validate_dir_block() because that function doesn't have
339 if (!(flags
& OCFS2_BH_READAHEAD
) &&
340 ocfs2_dir_has_trailer(inode
)) {
341 trailer
= ocfs2_trailer_from_bh(tmp
, inode
->i_sb
);
342 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer
)) {
344 ocfs2_error(inode
->i_sb
,
345 "Invalid dirblock #%llu: "
346 "signature = %.*s\n",
347 (unsigned long long)tmp
->b_blocknr
, 7,
348 trailer
->db_signature
);
351 if (le64_to_cpu(trailer
->db_blkno
) != tmp
->b_blocknr
) {
353 ocfs2_error(inode
->i_sb
,
354 "Directory block #%llu has an invalid "
356 (unsigned long long)tmp
->b_blocknr
,
357 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
360 if (le64_to_cpu(trailer
->db_parent_dinode
) !=
361 OCFS2_I(inode
)->ip_blkno
) {
363 ocfs2_error(inode
->i_sb
,
364 "Directory block #%llu on dinode "
365 "#%llu has an invalid parent_dinode "
367 (unsigned long long)tmp
->b_blocknr
,
368 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
369 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
374 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
379 return rc
? -EIO
: 0;
382 static struct buffer_head
*ocfs2_find_entry_el(const char *name
, int namelen
,
384 struct ocfs2_dir_entry
**res_dir
)
386 struct super_block
*sb
;
387 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
388 struct buffer_head
*bh
, *ret
= NULL
;
389 unsigned long start
, block
, b
;
390 int ra_max
= 0; /* Number of bh's in the readahead
392 int ra_ptr
= 0; /* Current index into readahead
401 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
402 start
= OCFS2_I(dir
)->ip_dir_start_lookup
;
403 if (start
>= nblocks
)
410 * We deal with the read-ahead logic here.
412 if (ra_ptr
>= ra_max
) {
413 /* Refill the readahead buffer */
416 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
418 * Terminate if we reach the end of the
419 * directory and must wrap, or if our
420 * search has finished at this block.
422 if (b
>= nblocks
|| (num
&& block
== start
)) {
423 bh_use
[ra_max
] = NULL
;
429 err
= ocfs2_read_dir_block(dir
, b
++, &bh
,
434 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
436 if (ocfs2_read_dir_block(dir
, block
, &bh
, 0)) {
437 /* read error, skip block & hope for the best.
438 * ocfs2_read_dir_block() has released the bh. */
439 ocfs2_error(dir
->i_sb
, "reading directory %llu, "
441 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
445 i
= ocfs2_search_dirblock(bh
, dir
, name
, namelen
,
446 block
<< sb
->s_blocksize_bits
,
447 bh
->b_data
, sb
->s_blocksize
,
450 OCFS2_I(dir
)->ip_dir_start_lookup
= block
;
452 goto cleanup_and_exit
;
456 goto cleanup_and_exit
;
459 if (++block
>= nblocks
)
461 } while (block
!= start
);
464 * If the directory has grown while we were searching, then
465 * search the last part of the directory before giving up.
468 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
469 if (block
< nblocks
) {
475 /* Clean up the read-ahead blocks */
476 for (; ra_ptr
< ra_max
; ra_ptr
++)
477 brelse(bh_use
[ra_ptr
]);
484 * Try to find an entry of the provided name within 'dir'.
486 * If nothing was found, NULL is returned. Otherwise, a buffer_head
487 * and pointer to the dir entry are passed back.
489 * Caller can NOT assume anything about the contents of the
490 * buffer_head - it is passed back only so that it can be passed into
491 * any one of the manipulation functions (add entry, delete entry,
492 * etc). As an example, bh in the extent directory case is a data
493 * block, in the inline-data case it actually points to an inode.
495 struct buffer_head
*ocfs2_find_entry(const char *name
, int namelen
,
497 struct ocfs2_dir_entry
**res_dir
)
501 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
502 return ocfs2_find_entry_id(name
, namelen
, dir
, res_dir
);
504 return ocfs2_find_entry_el(name
, namelen
, dir
, res_dir
);
508 * Update inode number and type of a previously found directory entry.
510 int ocfs2_update_entry(struct inode
*dir
, handle_t
*handle
,
511 struct buffer_head
*de_bh
, struct ocfs2_dir_entry
*de
,
512 struct inode
*new_entry_inode
)
515 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
518 * The same code works fine for both inline-data and extent
519 * based directories, so no need to split this up. The only
520 * difference is the journal_access function.
523 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
524 access
= ocfs2_journal_access_di
;
526 ret
= access(handle
, dir
, de_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
532 de
->inode
= cpu_to_le64(OCFS2_I(new_entry_inode
)->ip_blkno
);
533 ocfs2_set_de_type(de
, new_entry_inode
->i_mode
);
535 ocfs2_journal_dirty(handle
, de_bh
);
541 static int __ocfs2_delete_entry(handle_t
*handle
, struct inode
*dir
,
542 struct ocfs2_dir_entry
*de_del
,
543 struct buffer_head
*bh
, char *first_de
,
546 struct ocfs2_dir_entry
*de
, *pde
;
547 int i
, status
= -ENOENT
;
548 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
550 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle
, dir
, de_del
, bh
);
552 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
553 access
= ocfs2_journal_access_di
;
557 de
= (struct ocfs2_dir_entry
*) first_de
;
559 if (!ocfs2_check_dir_entry(dir
, de
, bh
, i
)) {
565 status
= access(handle
, dir
, bh
,
566 OCFS2_JOURNAL_ACCESS_WRITE
);
573 le16_add_cpu(&pde
->rec_len
,
574 le16_to_cpu(de
->rec_len
));
578 status
= ocfs2_journal_dirty(handle
, bh
);
581 i
+= le16_to_cpu(de
->rec_len
);
583 de
= (struct ocfs2_dir_entry
*)((char *)de
+ le16_to_cpu(de
->rec_len
));
590 static inline int ocfs2_delete_entry_id(handle_t
*handle
,
592 struct ocfs2_dir_entry
*de_del
,
593 struct buffer_head
*bh
)
596 struct buffer_head
*di_bh
= NULL
;
597 struct ocfs2_dinode
*di
;
598 struct ocfs2_inline_data
*data
;
600 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
606 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
607 data
= &di
->id2
.i_data
;
609 ret
= __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, data
->id_data
,
617 static inline int ocfs2_delete_entry_el(handle_t
*handle
,
619 struct ocfs2_dir_entry
*de_del
,
620 struct buffer_head
*bh
)
622 return __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, bh
->b_data
,
627 * ocfs2_delete_entry deletes a directory entry by merging it with the
630 int ocfs2_delete_entry(handle_t
*handle
,
632 struct ocfs2_dir_entry
*de_del
,
633 struct buffer_head
*bh
)
635 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
636 return ocfs2_delete_entry_id(handle
, dir
, de_del
, bh
);
638 return ocfs2_delete_entry_el(handle
, dir
, de_del
, bh
);
642 * Check whether 'de' has enough room to hold an entry of
643 * 'new_rec_len' bytes.
645 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry
*de
,
646 unsigned int new_rec_len
)
648 unsigned int de_really_used
;
650 /* Check whether this is an empty record with enough space */
651 if (le64_to_cpu(de
->inode
) == 0 &&
652 le16_to_cpu(de
->rec_len
) >= new_rec_len
)
656 * Record might have free space at the end which we can
659 de_really_used
= OCFS2_DIR_REC_LEN(de
->name_len
);
660 if (le16_to_cpu(de
->rec_len
) >= (de_really_used
+ new_rec_len
))
666 /* we don't always have a dentry for what we want to add, so people
667 * like orphan dir can call this instead.
669 * If you pass me insert_bh, I'll skip the search of the other dir
670 * blocks and put the record in there.
672 int __ocfs2_add_entry(handle_t
*handle
,
674 const char *name
, int namelen
,
675 struct inode
*inode
, u64 blkno
,
676 struct buffer_head
*parent_fe_bh
,
677 struct buffer_head
*insert_bh
)
679 unsigned long offset
;
680 unsigned short rec_len
;
681 struct ocfs2_dir_entry
*de
, *de1
;
682 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)parent_fe_bh
->b_data
;
683 struct super_block
*sb
= dir
->i_sb
;
685 unsigned int size
= sb
->s_blocksize
;
686 char *data_start
= insert_bh
->b_data
;
693 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
694 data_start
= di
->id2
.i_data
.id_data
;
695 size
= i_size_read(dir
);
697 BUG_ON(insert_bh
!= parent_fe_bh
);
700 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
702 de
= (struct ocfs2_dir_entry
*) data_start
;
704 BUG_ON((char *)de
>= (size
+ data_start
));
706 /* These checks should've already been passed by the
707 * prepare function, but I guess we can leave them
709 if (!ocfs2_check_dir_entry(dir
, de
, insert_bh
, offset
)) {
713 if (ocfs2_match(namelen
, name
, de
)) {
718 /* We're guaranteed that we should have space, so we
719 * can't possibly have hit the trailer...right? */
720 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir
, de
, offset
, size
),
721 "Hit dir trailer trying to insert %.*s "
722 "(namelen %d) into directory %llu. "
723 "offset is %lu, trailer offset is %d\n",
724 namelen
, name
, namelen
,
725 (unsigned long long)parent_fe_bh
->b_blocknr
,
726 offset
, ocfs2_dir_trailer_blk_off(dir
->i_sb
));
728 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
729 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
730 retval
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
736 if (insert_bh
== parent_fe_bh
)
737 status
= ocfs2_journal_access_di(handle
, dir
,
739 OCFS2_JOURNAL_ACCESS_WRITE
);
741 status
= ocfs2_journal_access_db(handle
, dir
,
743 OCFS2_JOURNAL_ACCESS_WRITE
);
744 /* By now the buffer is marked for journaling */
745 offset
+= le16_to_cpu(de
->rec_len
);
746 if (le64_to_cpu(de
->inode
)) {
747 de1
= (struct ocfs2_dir_entry
*)((char *) de
+
748 OCFS2_DIR_REC_LEN(de
->name_len
));
750 cpu_to_le16(le16_to_cpu(de
->rec_len
) -
751 OCFS2_DIR_REC_LEN(de
->name_len
));
752 de
->rec_len
= cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
755 de
->file_type
= OCFS2_FT_UNKNOWN
;
757 de
->inode
= cpu_to_le64(blkno
);
758 ocfs2_set_de_type(de
, inode
->i_mode
);
761 de
->name_len
= namelen
;
762 memcpy(de
->name
, name
, namelen
);
765 status
= ocfs2_journal_dirty(handle
, insert_bh
);
770 offset
+= le16_to_cpu(de
->rec_len
);
771 de
= (struct ocfs2_dir_entry
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
774 /* when you think about it, the assert above should prevent us
775 * from ever getting here. */
783 static int ocfs2_dir_foreach_blk_id(struct inode
*inode
,
785 loff_t
*f_pos
, void *priv
,
786 filldir_t filldir
, int *filldir_err
)
788 int ret
, i
, filldir_ret
;
789 unsigned long offset
= *f_pos
;
790 struct buffer_head
*di_bh
= NULL
;
791 struct ocfs2_dinode
*di
;
792 struct ocfs2_inline_data
*data
;
793 struct ocfs2_dir_entry
*de
;
795 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
797 mlog(ML_ERROR
, "Unable to read inode block for dir %llu\n",
798 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
802 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
803 data
= &di
->id2
.i_data
;
805 while (*f_pos
< i_size_read(inode
)) {
807 /* If the dir block has changed since the last call to
808 * readdir(2), then we might be pointing to an invalid
809 * dirent right now. Scan from the start of the block
811 if (*f_version
!= inode
->i_version
) {
812 for (i
= 0; i
< i_size_read(inode
) && i
< offset
; ) {
813 de
= (struct ocfs2_dir_entry
*)
815 /* It's too expensive to do a full
816 * dirent test each time round this
817 * loop, but we do have to test at
818 * least that it is non-zero. A
819 * failure will be detected in the
820 * dirent test below. */
821 if (le16_to_cpu(de
->rec_len
) <
822 OCFS2_DIR_REC_LEN(1))
824 i
+= le16_to_cpu(de
->rec_len
);
827 *f_version
= inode
->i_version
;
830 de
= (struct ocfs2_dir_entry
*) (data
->id_data
+ *f_pos
);
831 if (!ocfs2_check_dir_entry(inode
, de
, di_bh
, *f_pos
)) {
832 /* On error, skip the f_pos to the end. */
833 *f_pos
= i_size_read(inode
);
836 offset
+= le16_to_cpu(de
->rec_len
);
837 if (le64_to_cpu(de
->inode
)) {
838 /* We might block in the next section
839 * if the data destination is
840 * currently swapped out. So, use a
841 * version stamp to detect whether or
842 * not the directory has been modified
843 * during the copy operation.
845 u64 version
= *f_version
;
846 unsigned char d_type
= DT_UNKNOWN
;
848 if (de
->file_type
< OCFS2_FT_MAX
)
849 d_type
= ocfs2_filetype_table
[de
->file_type
];
851 filldir_ret
= filldir(priv
, de
->name
,
854 le64_to_cpu(de
->inode
),
858 *filldir_err
= filldir_ret
;
861 if (version
!= *f_version
)
864 *f_pos
+= le16_to_cpu(de
->rec_len
);
873 static int ocfs2_dir_foreach_blk_el(struct inode
*inode
,
875 loff_t
*f_pos
, void *priv
,
876 filldir_t filldir
, int *filldir_err
)
879 unsigned long offset
, blk
, last_ra_blk
= 0;
881 struct buffer_head
* bh
, * tmp
;
882 struct ocfs2_dir_entry
* de
;
883 struct super_block
* sb
= inode
->i_sb
;
884 unsigned int ra_sectors
= 16;
889 offset
= (*f_pos
) & (sb
->s_blocksize
- 1);
891 while (!error
&& !stored
&& *f_pos
< i_size_read(inode
)) {
892 blk
= (*f_pos
) >> sb
->s_blocksize_bits
;
893 if (ocfs2_read_dir_block(inode
, blk
, &bh
, 0)) {
894 /* Skip the corrupt dirblock and keep trying */
895 *f_pos
+= sb
->s_blocksize
- offset
;
899 /* The idea here is to begin with 8k read-ahead and to stay
900 * 4k ahead of our current position.
902 * TODO: Use the pagecache for this. We just need to
903 * make sure it's cluster-safe... */
905 || (((last_ra_blk
- blk
) << 9) <= (ra_sectors
/ 2))) {
906 for (i
= ra_sectors
>> (sb
->s_blocksize_bits
- 9);
909 if (!ocfs2_read_dir_block(inode
, ++blk
, &tmp
,
918 /* If the dir block has changed since the last call to
919 * readdir(2), then we might be pointing to an invalid
920 * dirent right now. Scan from the start of the block
922 if (*f_version
!= inode
->i_version
) {
923 for (i
= 0; i
< sb
->s_blocksize
&& i
< offset
; ) {
924 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ i
);
925 /* It's too expensive to do a full
926 * dirent test each time round this
927 * loop, but we do have to test at
928 * least that it is non-zero. A
929 * failure will be detected in the
930 * dirent test below. */
931 if (le16_to_cpu(de
->rec_len
) <
932 OCFS2_DIR_REC_LEN(1))
934 i
+= le16_to_cpu(de
->rec_len
);
937 *f_pos
= ((*f_pos
) & ~(sb
->s_blocksize
- 1))
939 *f_version
= inode
->i_version
;
942 while (!error
&& *f_pos
< i_size_read(inode
)
943 && offset
< sb
->s_blocksize
) {
944 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ offset
);
945 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
946 /* On error, skip the f_pos to the
948 *f_pos
= ((*f_pos
) | (sb
->s_blocksize
- 1)) + 1;
952 offset
+= le16_to_cpu(de
->rec_len
);
953 if (le64_to_cpu(de
->inode
)) {
954 /* We might block in the next section
955 * if the data destination is
956 * currently swapped out. So, use a
957 * version stamp to detect whether or
958 * not the directory has been modified
959 * during the copy operation.
961 unsigned long version
= *f_version
;
962 unsigned char d_type
= DT_UNKNOWN
;
964 if (de
->file_type
< OCFS2_FT_MAX
)
965 d_type
= ocfs2_filetype_table
[de
->file_type
];
966 error
= filldir(priv
, de
->name
,
969 le64_to_cpu(de
->inode
),
973 *filldir_err
= error
;
976 if (version
!= *f_version
)
980 *f_pos
+= le16_to_cpu(de
->rec_len
);
992 static int ocfs2_dir_foreach_blk(struct inode
*inode
, u64
*f_version
,
993 loff_t
*f_pos
, void *priv
, filldir_t filldir
,
996 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
997 return ocfs2_dir_foreach_blk_id(inode
, f_version
, f_pos
, priv
,
998 filldir
, filldir_err
);
1000 return ocfs2_dir_foreach_blk_el(inode
, f_version
, f_pos
, priv
, filldir
,
1005 * This is intended to be called from inside other kernel functions,
1006 * so we fake some arguments.
1008 int ocfs2_dir_foreach(struct inode
*inode
, loff_t
*f_pos
, void *priv
,
1011 int ret
= 0, filldir_err
= 0;
1012 u64 version
= inode
->i_version
;
1014 while (*f_pos
< i_size_read(inode
)) {
1015 ret
= ocfs2_dir_foreach_blk(inode
, &version
, f_pos
, priv
,
1016 filldir
, &filldir_err
);
1017 if (ret
|| filldir_err
)
1031 int ocfs2_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1034 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1037 mlog_entry("dirino=%llu\n",
1038 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1040 error
= ocfs2_inode_lock_atime(inode
, filp
->f_vfsmnt
, &lock_level
);
1041 if (lock_level
&& error
>= 0) {
1042 /* We release EX lock which used to update atime
1043 * and get PR lock again to reduce contention
1044 * on commonly accessed directories. */
1045 ocfs2_inode_unlock(inode
, 1);
1047 error
= ocfs2_inode_lock(inode
, NULL
, 0);
1050 if (error
!= -ENOENT
)
1052 /* we haven't got any yet, so propagate the error. */
1056 error
= ocfs2_dir_foreach_blk(inode
, &filp
->f_version
, &filp
->f_pos
,
1057 dirent
, filldir
, NULL
);
1059 ocfs2_inode_unlock(inode
, lock_level
);
1068 * NOTE: this should always be called with parent dir i_mutex taken.
1070 int ocfs2_find_files_on_disk(const char *name
,
1073 struct inode
*inode
,
1074 struct buffer_head
**dirent_bh
,
1075 struct ocfs2_dir_entry
**dirent
)
1077 int status
= -ENOENT
;
1079 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1080 namelen
, name
, blkno
, inode
, dirent_bh
, dirent
);
1082 *dirent_bh
= ocfs2_find_entry(name
, namelen
, inode
, dirent
);
1083 if (!*dirent_bh
|| !*dirent
) {
1088 *blkno
= le64_to_cpu((*dirent
)->inode
);
1103 * Convenience function for callers which just want the block number
1104 * mapped to a name and don't require the full dirent info, etc.
1106 int ocfs2_lookup_ino_from_name(struct inode
*dir
, const char *name
,
1107 int namelen
, u64
*blkno
)
1110 struct buffer_head
*bh
= NULL
;
1111 struct ocfs2_dir_entry
*dirent
= NULL
;
1113 ret
= ocfs2_find_files_on_disk(name
, namelen
, blkno
, dir
, &bh
, &dirent
);
1119 /* Check for a name within a directory.
1121 * Return 0 if the name does not exist
1122 * Return -EEXIST if the directory contains the name
1124 * Callers should have i_mutex + a cluster lock on dir
1126 int ocfs2_check_dir_for_entry(struct inode
*dir
,
1131 struct buffer_head
*dirent_bh
= NULL
;
1132 struct ocfs2_dir_entry
*dirent
= NULL
;
1134 mlog_entry("dir %llu, name '%.*s'\n",
1135 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, namelen
, name
);
1138 dirent_bh
= ocfs2_find_entry(name
, namelen
, dir
, &dirent
);
1150 struct ocfs2_empty_dir_priv
{
1152 unsigned seen_dot_dot
;
1153 unsigned seen_other
;
1155 static int ocfs2_empty_dir_filldir(void *priv
, const char *name
, int name_len
,
1156 loff_t pos
, u64 ino
, unsigned type
)
1158 struct ocfs2_empty_dir_priv
*p
= priv
;
1161 * Check the positions of "." and ".." records to be sure
1162 * they're in the correct place.
1164 if (name_len
== 1 && !strncmp(".", name
, 1) && pos
== 0) {
1169 if (name_len
== 2 && !strncmp("..", name
, 2) &&
1170 pos
== OCFS2_DIR_REC_LEN(1)) {
1171 p
->seen_dot_dot
= 1;
1179 * routine to check that the specified directory is empty (for rmdir)
1181 * Returns 1 if dir is empty, zero otherwise.
1183 int ocfs2_empty_dir(struct inode
*inode
)
1187 struct ocfs2_empty_dir_priv priv
;
1189 memset(&priv
, 0, sizeof(priv
));
1191 ret
= ocfs2_dir_foreach(inode
, &start
, &priv
, ocfs2_empty_dir_filldir
);
1195 if (!priv
.seen_dot
|| !priv
.seen_dot_dot
) {
1196 mlog(ML_ERROR
, "bad directory (dir #%llu) - no `.' or `..'\n",
1197 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1199 * XXX: Is it really safe to allow an unlink to continue?
1204 return !priv
.seen_other
;
1208 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1209 * "..", which might be used during creation of a directory with a trailing
1210 * header. It is otherwise safe to ignore the return code.
1212 static struct ocfs2_dir_entry
*ocfs2_fill_initial_dirents(struct inode
*inode
,
1213 struct inode
*parent
,
1217 struct ocfs2_dir_entry
*de
= (struct ocfs2_dir_entry
*)start
;
1219 de
->inode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
1222 cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
1223 strcpy(de
->name
, ".");
1224 ocfs2_set_de_type(de
, S_IFDIR
);
1226 de
= (struct ocfs2_dir_entry
*) ((char *)de
+ le16_to_cpu(de
->rec_len
));
1227 de
->inode
= cpu_to_le64(OCFS2_I(parent
)->ip_blkno
);
1228 de
->rec_len
= cpu_to_le16(size
- OCFS2_DIR_REC_LEN(1));
1230 strcpy(de
->name
, "..");
1231 ocfs2_set_de_type(de
, S_IFDIR
);
1237 * This works together with code in ocfs2_mknod_locked() which sets
1238 * the inline-data flag and initializes the inline-data section.
1240 static int ocfs2_fill_new_dir_id(struct ocfs2_super
*osb
,
1242 struct inode
*parent
,
1243 struct inode
*inode
,
1244 struct buffer_head
*di_bh
)
1247 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1248 struct ocfs2_inline_data
*data
= &di
->id2
.i_data
;
1249 unsigned int size
= le16_to_cpu(data
->id_count
);
1251 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
1252 OCFS2_JOURNAL_ACCESS_WRITE
);
1258 ocfs2_fill_initial_dirents(inode
, parent
, data
->id_data
, size
);
1260 ocfs2_journal_dirty(handle
, di_bh
);
1266 i_size_write(inode
, size
);
1268 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1270 ret
= ocfs2_mark_inode_dirty(handle
, inode
, di_bh
);
1278 static int ocfs2_fill_new_dir_el(struct ocfs2_super
*osb
,
1280 struct inode
*parent
,
1281 struct inode
*inode
,
1282 struct buffer_head
*fe_bh
,
1283 struct ocfs2_alloc_context
*data_ac
)
1286 unsigned int size
= osb
->sb
->s_blocksize
;
1287 struct buffer_head
*new_bh
= NULL
;
1288 struct ocfs2_dir_entry
*de
;
1292 if (ocfs2_supports_dir_trailer(osb
))
1293 size
= ocfs2_dir_trailer_blk_off(parent
->i_sb
);
1295 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, inode
, fe_bh
,
1296 data_ac
, NULL
, &new_bh
);
1302 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1304 status
= ocfs2_journal_access_db(handle
, inode
, new_bh
,
1305 OCFS2_JOURNAL_ACCESS_CREATE
);
1310 memset(new_bh
->b_data
, 0, osb
->sb
->s_blocksize
);
1312 de
= ocfs2_fill_initial_dirents(inode
, parent
, new_bh
->b_data
, size
);
1313 if (ocfs2_supports_dir_trailer(osb
))
1314 ocfs2_init_dir_trailer(inode
, new_bh
);
1316 status
= ocfs2_journal_dirty(handle
, new_bh
);
1322 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
1324 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1325 status
= ocfs2_mark_inode_dirty(handle
, inode
, fe_bh
);
1339 int ocfs2_fill_new_dir(struct ocfs2_super
*osb
,
1341 struct inode
*parent
,
1342 struct inode
*inode
,
1343 struct buffer_head
*fe_bh
,
1344 struct ocfs2_alloc_context
*data_ac
)
1346 BUG_ON(!ocfs2_supports_inline_data(osb
) && data_ac
== NULL
);
1348 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1349 return ocfs2_fill_new_dir_id(osb
, handle
, parent
, inode
, fe_bh
);
1351 return ocfs2_fill_new_dir_el(osb
, handle
, parent
, inode
, fe_bh
,
1356 * Expand rec_len of the rightmost dirent in a directory block so that it
1357 * contains the end of our valid space for dirents. We do this during
1358 * expansion from an inline directory to one with extents. The first dir block
1359 * in that case is taken from the inline data portion of the inode block.
1361 * We add the dir trailer if this filesystem wants it.
1363 static void ocfs2_expand_last_dirent(char *start
, unsigned int old_size
,
1364 struct super_block
*sb
)
1366 struct ocfs2_dir_entry
*de
;
1367 struct ocfs2_dir_entry
*prev_de
;
1368 char *de_buf
, *limit
;
1369 unsigned int new_size
= sb
->s_blocksize
;
1372 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb
)))
1373 new_size
= ocfs2_dir_trailer_blk_off(sb
);
1375 bytes
= new_size
- old_size
;
1377 limit
= start
+ old_size
;
1379 de
= (struct ocfs2_dir_entry
*)de_buf
;
1382 de_buf
+= le16_to_cpu(de
->rec_len
);
1383 de
= (struct ocfs2_dir_entry
*)de_buf
;
1384 } while (de_buf
< limit
);
1386 le16_add_cpu(&prev_de
->rec_len
, bytes
);
1390 * We allocate enough clusters to fulfill "blocks_wanted", but set
1391 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1392 * rest automatically for us.
1394 * *first_block_bh is a pointer to the 1st data block allocated to the
1397 static int ocfs2_expand_inline_dir(struct inode
*dir
, struct buffer_head
*di_bh
,
1398 unsigned int blocks_wanted
,
1399 struct buffer_head
**first_block_bh
)
1401 u32 alloc
, bit_off
, len
;
1402 struct super_block
*sb
= dir
->i_sb
;
1403 int ret
, credits
= ocfs2_inline_to_extents_credits(sb
);
1404 u64 blkno
, bytes
= blocks_wanted
<< sb
->s_blocksize_bits
;
1405 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
1406 struct ocfs2_inode_info
*oi
= OCFS2_I(dir
);
1407 struct ocfs2_alloc_context
*data_ac
;
1408 struct buffer_head
*dirdata_bh
= NULL
;
1409 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1411 struct ocfs2_extent_tree et
;
1414 ocfs2_init_dinode_extent_tree(&et
, dir
, di_bh
);
1416 alloc
= ocfs2_clusters_for_bytes(sb
, bytes
);
1419 * We should never need more than 2 clusters for this -
1420 * maximum dirent size is far less than one block. In fact,
1421 * the only time we'd need more than one cluster is if
1422 * blocksize == clustersize and the dirent won't fit in the
1423 * extra space that the expansion to a single block gives. As
1424 * of today, that only happens on 4k/4k file systems.
1428 ret
= ocfs2_reserve_clusters(osb
, alloc
, &data_ac
);
1434 down_write(&oi
->ip_alloc_sem
);
1437 * Prepare for worst case allocation scenario of two separate
1441 credits
+= OCFS2_SUBALLOC_ALLOC
;
1443 handle
= ocfs2_start_trans(osb
, credits
);
1444 if (IS_ERR(handle
)) {
1445 ret
= PTR_ERR(handle
);
1450 if (vfs_dq_alloc_space_nodirty(dir
,
1451 ocfs2_clusters_to_bytes(osb
->sb
, alloc
))) {
1457 * Try to claim as many clusters as the bitmap can give though
1458 * if we only get one now, that's enough to continue. The rest
1459 * will be claimed after the conversion to extents.
1461 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
1468 * Operations are carefully ordered so that we set up the new
1469 * data block first. The conversion from inline data to
1472 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
1473 dirdata_bh
= sb_getblk(sb
, blkno
);
1480 ocfs2_set_new_buffer_uptodate(dir
, dirdata_bh
);
1482 ret
= ocfs2_journal_access_db(handle
, dir
, dirdata_bh
,
1483 OCFS2_JOURNAL_ACCESS_CREATE
);
1489 memcpy(dirdata_bh
->b_data
, di
->id2
.i_data
.id_data
, i_size_read(dir
));
1490 memset(dirdata_bh
->b_data
+ i_size_read(dir
), 0,
1491 sb
->s_blocksize
- i_size_read(dir
));
1492 ocfs2_expand_last_dirent(dirdata_bh
->b_data
, i_size_read(dir
), sb
);
1493 if (ocfs2_supports_dir_trailer(osb
))
1494 ocfs2_init_dir_trailer(dir
, dirdata_bh
);
1496 ret
= ocfs2_journal_dirty(handle
, dirdata_bh
);
1503 * Set extent, i_size, etc on the directory. After this, the
1504 * inode should contain the same exact dirents as before and
1505 * be fully accessible from system calls.
1507 * We let the later dirent insert modify c/mtime - to the user
1508 * the data hasn't changed.
1510 ret
= ocfs2_journal_access_di(handle
, dir
, di_bh
,
1511 OCFS2_JOURNAL_ACCESS_CREATE
);
1517 spin_lock(&oi
->ip_lock
);
1518 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
1519 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1520 spin_unlock(&oi
->ip_lock
);
1522 ocfs2_dinode_new_extent_list(dir
, di
);
1524 i_size_write(dir
, sb
->s_blocksize
);
1525 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1527 di
->i_size
= cpu_to_le64(sb
->s_blocksize
);
1528 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(dir
->i_ctime
.tv_sec
);
1529 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(dir
->i_ctime
.tv_nsec
);
1532 * This should never fail as our extent list is empty and all
1533 * related blocks have been journaled already.
1535 ret
= ocfs2_insert_extent(osb
, handle
, dir
, &et
, 0, blkno
, len
,
1543 * Set i_blocks after the extent insert for the most up to
1544 * date ip_clusters value.
1546 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
1548 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1555 * We asked for two clusters, but only got one in the 1st
1556 * pass. Claim the 2nd cluster as a separate extent.
1559 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
1565 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
1567 ret
= ocfs2_insert_extent(osb
, handle
, dir
, &et
, 1,
1568 blkno
, len
, 0, NULL
);
1575 *first_block_bh
= dirdata_bh
;
1579 if (ret
< 0 && did_quota
)
1580 vfs_dq_free_space_nodirty(dir
,
1581 ocfs2_clusters_to_bytes(osb
->sb
, 2));
1582 ocfs2_commit_trans(osb
, handle
);
1585 up_write(&oi
->ip_alloc_sem
);
1589 ocfs2_free_alloc_context(data_ac
);
1596 /* returns a bh of the 1st new block in the allocation. */
1597 static int ocfs2_do_extend_dir(struct super_block
*sb
,
1600 struct buffer_head
*parent_fe_bh
,
1601 struct ocfs2_alloc_context
*data_ac
,
1602 struct ocfs2_alloc_context
*meta_ac
,
1603 struct buffer_head
**new_bh
)
1606 int extend
, did_quota
= 0;
1607 u64 p_blkno
, v_blkno
;
1609 spin_lock(&OCFS2_I(dir
)->ip_lock
);
1610 extend
= (i_size_read(dir
) == ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
));
1611 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1614 u32 offset
= OCFS2_I(dir
)->ip_clusters
;
1616 if (vfs_dq_alloc_space_nodirty(dir
,
1617 ocfs2_clusters_to_bytes(sb
, 1))) {
1623 status
= ocfs2_add_inode_data(OCFS2_SB(sb
), dir
, &offset
,
1624 1, 0, parent_fe_bh
, handle
,
1625 data_ac
, meta_ac
, NULL
);
1626 BUG_ON(status
== -EAGAIN
);
1633 v_blkno
= ocfs2_blocks_for_bytes(sb
, i_size_read(dir
));
1634 status
= ocfs2_extent_map_get_blocks(dir
, v_blkno
, &p_blkno
, NULL
, NULL
);
1640 *new_bh
= sb_getblk(sb
, p_blkno
);
1648 if (did_quota
&& status
< 0)
1649 vfs_dq_free_space_nodirty(dir
, ocfs2_clusters_to_bytes(sb
, 1));
1655 * Assumes you already have a cluster lock on the directory.
1657 * 'blocks_wanted' is only used if we have an inline directory which
1658 * is to be turned into an extent based one. The size of the dirent to
1659 * insert might be larger than the space gained by growing to just one
1660 * block, so we may have to grow the inode by two blocks in that case.
1662 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
1664 struct buffer_head
*parent_fe_bh
,
1665 unsigned int blocks_wanted
,
1666 struct buffer_head
**new_de_bh
)
1669 int credits
, num_free_extents
, drop_alloc_sem
= 0;
1671 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
1672 struct ocfs2_extent_list
*el
= &fe
->id2
.i_list
;
1673 struct ocfs2_alloc_context
*data_ac
= NULL
;
1674 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1675 handle_t
*handle
= NULL
;
1676 struct buffer_head
*new_bh
= NULL
;
1677 struct ocfs2_dir_entry
* de
;
1678 struct super_block
*sb
= osb
->sb
;
1679 struct ocfs2_extent_tree et
;
1683 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1684 status
= ocfs2_expand_inline_dir(dir
, parent_fe_bh
,
1685 blocks_wanted
, &new_bh
);
1691 if (blocks_wanted
== 1) {
1693 * If the new dirent will fit inside the space
1694 * created by pushing out to one block, then
1695 * we can complete the operation
1696 * here. Otherwise we have to expand i_size
1697 * and format the 2nd block below.
1699 BUG_ON(new_bh
== NULL
);
1704 * Get rid of 'new_bh' - we want to format the 2nd
1705 * data block and return that instead.
1710 dir_i_size
= i_size_read(dir
);
1711 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
1715 dir_i_size
= i_size_read(dir
);
1716 mlog(0, "extending dir %llu (i_size = %lld)\n",
1717 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, dir_i_size
);
1719 /* dir->i_size is always block aligned. */
1720 spin_lock(&OCFS2_I(dir
)->ip_lock
);
1721 if (dir_i_size
== ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
)) {
1722 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1723 ocfs2_init_dinode_extent_tree(&et
, dir
, parent_fe_bh
);
1724 num_free_extents
= ocfs2_num_free_extents(osb
, dir
, &et
);
1725 if (num_free_extents
< 0) {
1726 status
= num_free_extents
;
1731 if (!num_free_extents
) {
1732 status
= ocfs2_reserve_new_metadata(osb
, el
, &meta_ac
);
1734 if (status
!= -ENOSPC
)
1740 status
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
1742 if (status
!= -ENOSPC
)
1747 credits
= ocfs2_calc_extend_credits(sb
, el
, 1);
1749 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1750 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
1754 down_write(&OCFS2_I(dir
)->ip_alloc_sem
);
1757 handle
= ocfs2_start_trans(osb
, credits
);
1758 if (IS_ERR(handle
)) {
1759 status
= PTR_ERR(handle
);
1765 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, dir
, parent_fe_bh
,
1766 data_ac
, meta_ac
, &new_bh
);
1772 ocfs2_set_new_buffer_uptodate(dir
, new_bh
);
1774 status
= ocfs2_journal_access_db(handle
, dir
, new_bh
,
1775 OCFS2_JOURNAL_ACCESS_CREATE
);
1780 memset(new_bh
->b_data
, 0, sb
->s_blocksize
);
1782 de
= (struct ocfs2_dir_entry
*) new_bh
->b_data
;
1784 if (ocfs2_dir_has_trailer(dir
)) {
1785 de
->rec_len
= cpu_to_le16(ocfs2_dir_trailer_blk_off(sb
));
1786 ocfs2_init_dir_trailer(dir
, new_bh
);
1788 de
->rec_len
= cpu_to_le16(sb
->s_blocksize
);
1790 status
= ocfs2_journal_dirty(handle
, new_bh
);
1796 dir_i_size
+= dir
->i_sb
->s_blocksize
;
1797 i_size_write(dir
, dir_i_size
);
1798 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
1799 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
1806 *new_de_bh
= new_bh
;
1810 up_write(&OCFS2_I(dir
)->ip_alloc_sem
);
1812 ocfs2_commit_trans(osb
, handle
);
1815 ocfs2_free_alloc_context(data_ac
);
1817 ocfs2_free_alloc_context(meta_ac
);
1825 static int ocfs2_find_dir_space_id(struct inode
*dir
, struct buffer_head
*di_bh
,
1826 const char *name
, int namelen
,
1827 struct buffer_head
**ret_de_bh
,
1828 unsigned int *blocks_wanted
)
1831 struct super_block
*sb
= dir
->i_sb
;
1832 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1833 struct ocfs2_dir_entry
*de
, *last_de
= NULL
;
1834 char *de_buf
, *limit
;
1835 unsigned long offset
= 0;
1836 unsigned int rec_len
, new_rec_len
, free_space
= dir
->i_sb
->s_blocksize
;
1839 * This calculates how many free bytes we'd have in block zero, should
1840 * this function force expansion to an extent tree.
1842 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb
)))
1843 free_space
= ocfs2_dir_trailer_blk_off(sb
) - i_size_read(dir
);
1845 free_space
= dir
->i_sb
->s_blocksize
- i_size_read(dir
);
1847 de_buf
= di
->id2
.i_data
.id_data
;
1848 limit
= de_buf
+ i_size_read(dir
);
1849 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
1851 while (de_buf
< limit
) {
1852 de
= (struct ocfs2_dir_entry
*)de_buf
;
1854 if (!ocfs2_check_dir_entry(dir
, de
, di_bh
, offset
)) {
1858 if (ocfs2_match(namelen
, name
, de
)) {
1863 * No need to check for a trailing dirent record here as
1864 * they're not used for inline dirs.
1867 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
1868 /* Ok, we found a spot. Return this bh and let
1869 * the caller actually fill it in. */
1877 de_buf
+= le16_to_cpu(de
->rec_len
);
1878 offset
+= le16_to_cpu(de
->rec_len
);
1882 * We're going to require expansion of the directory - figure
1883 * out how many blocks we'll need so that a place for the
1884 * dirent can be found.
1887 new_rec_len
= le16_to_cpu(last_de
->rec_len
) + free_space
;
1888 if (new_rec_len
< (rec_len
+ OCFS2_DIR_REC_LEN(last_de
->name_len
)))
1896 static int ocfs2_find_dir_space_el(struct inode
*dir
, const char *name
,
1897 int namelen
, struct buffer_head
**ret_de_bh
)
1899 unsigned long offset
;
1900 struct buffer_head
*bh
= NULL
;
1901 unsigned short rec_len
;
1902 struct ocfs2_dir_entry
*de
;
1903 struct super_block
*sb
= dir
->i_sb
;
1905 int blocksize
= dir
->i_sb
->s_blocksize
;
1907 status
= ocfs2_read_dir_block(dir
, 0, &bh
, 0);
1913 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
1915 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
1917 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
1921 if (i_size_read(dir
) <= offset
) {
1923 * Caller will have to expand this
1929 status
= ocfs2_read_dir_block(dir
,
1930 offset
>> sb
->s_blocksize_bits
,
1936 /* move to next block */
1937 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
1939 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
1943 if (ocfs2_match(namelen
, name
, de
)) {
1948 if (ocfs2_skip_dir_trailer(dir
, de
, offset
% blocksize
,
1952 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
1953 /* Ok, we found a spot. Return this bh and let
1954 * the caller actually fill it in. */
1961 offset
+= le16_to_cpu(de
->rec_len
);
1962 de
= (struct ocfs2_dir_entry
*)((char *) de
+ le16_to_cpu(de
->rec_len
));
1973 int ocfs2_prepare_dir_for_insert(struct ocfs2_super
*osb
,
1975 struct buffer_head
*parent_fe_bh
,
1978 struct buffer_head
**ret_de_bh
)
1981 unsigned int blocks_wanted
= 1;
1982 struct buffer_head
*bh
= NULL
;
1984 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1985 namelen
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
1995 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1996 ret
= ocfs2_find_dir_space_id(dir
, parent_fe_bh
, name
,
1997 namelen
, &bh
, &blocks_wanted
);
1999 ret
= ocfs2_find_dir_space_el(dir
, name
, namelen
, &bh
);
2001 if (ret
&& ret
!= -ENOSPC
) {
2006 if (ret
== -ENOSPC
) {
2008 * We have to expand the directory to add this name.
2012 ret
= ocfs2_extend_dir(osb
, dir
, parent_fe_bh
, blocks_wanted
,