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>
53 #include "extent_map.h"
62 #include "buffer_head_io.h"
64 #define NAMEI_RA_CHUNKS 2
65 #define NAMEI_RA_BLOCKS 4
66 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
67 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
69 static unsigned char ocfs2_filetype_table
[] = {
70 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
73 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
75 struct buffer_head
*parent_fe_bh
,
76 unsigned int blocks_wanted
,
77 struct buffer_head
**new_de_bh
);
78 static int ocfs2_do_extend_dir(struct super_block
*sb
,
81 struct buffer_head
*parent_fe_bh
,
82 struct ocfs2_alloc_context
*data_ac
,
83 struct ocfs2_alloc_context
*meta_ac
,
84 struct buffer_head
**new_bh
);
87 * These are distinct checks because future versions of the file system will
88 * want to have a trailing dirent structure independent of indexing.
90 static int ocfs2_dir_has_trailer(struct inode
*dir
)
92 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
95 return ocfs2_meta_ecc(OCFS2_SB(dir
->i_sb
));
98 static int ocfs2_supports_dir_trailer(struct ocfs2_super
*osb
)
100 return ocfs2_meta_ecc(osb
);
103 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block
*sb
)
105 return sb
->s_blocksize
- sizeof(struct ocfs2_dir_block_trailer
);
108 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
111 * XXX: This is executed once on every dirent. We should consider optimizing
114 static int ocfs2_skip_dir_trailer(struct inode
*dir
,
115 struct ocfs2_dir_entry
*de
,
116 unsigned long offset
,
117 unsigned long blklen
)
119 unsigned long toff
= blklen
- sizeof(struct ocfs2_dir_block_trailer
);
121 if (!ocfs2_dir_has_trailer(dir
))
130 static void ocfs2_init_dir_trailer(struct inode
*inode
,
131 struct buffer_head
*bh
)
133 struct ocfs2_dir_block_trailer
*trailer
;
135 trailer
= ocfs2_trailer_from_bh(bh
, inode
->i_sb
);
136 strcpy(trailer
->db_signature
, OCFS2_DIR_TRAILER_SIGNATURE
);
137 trailer
->db_compat_rec_len
=
138 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer
));
139 trailer
->db_parent_dinode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
140 trailer
->db_blkno
= cpu_to_le64(bh
->b_blocknr
);
144 * bh passed here can be an inode block or a dir data block, depending
145 * on the inode inline data flag.
147 static int ocfs2_check_dir_entry(struct inode
* dir
,
148 struct ocfs2_dir_entry
* de
,
149 struct buffer_head
* bh
,
150 unsigned long offset
)
152 const char *error_msg
= NULL
;
153 const int rlen
= le16_to_cpu(de
->rec_len
);
155 if (rlen
< OCFS2_DIR_REC_LEN(1))
156 error_msg
= "rec_len is smaller than minimal";
157 else if (rlen
% 4 != 0)
158 error_msg
= "rec_len % 4 != 0";
159 else if (rlen
< OCFS2_DIR_REC_LEN(de
->name_len
))
160 error_msg
= "rec_len is too small for name_len";
161 else if (((char *) de
- bh
->b_data
) + rlen
> dir
->i_sb
->s_blocksize
)
162 error_msg
= "directory entry across blocks";
164 if (error_msg
!= NULL
)
165 mlog(ML_ERROR
, "bad entry in directory #%llu: %s - "
166 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
167 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, error_msg
,
168 offset
, (unsigned long long)le64_to_cpu(de
->inode
), rlen
,
170 return error_msg
== NULL
? 1 : 0;
173 static inline int ocfs2_match(int len
,
174 const char * const name
,
175 struct ocfs2_dir_entry
*de
)
177 if (len
!= de
->name_len
)
181 return !memcmp(name
, de
->name
, len
);
185 * Returns 0 if not found, -1 on failure, and 1 on success
187 static int inline ocfs2_search_dirblock(struct buffer_head
*bh
,
189 const char *name
, int namelen
,
190 unsigned long offset
,
193 struct ocfs2_dir_entry
**res_dir
)
195 struct ocfs2_dir_entry
*de
;
196 char *dlimit
, *de_buf
;
203 dlimit
= de_buf
+ bytes
;
205 while (de_buf
< dlimit
) {
206 /* this code is executed quadratically often */
207 /* do minimal checking `by hand' */
209 de
= (struct ocfs2_dir_entry
*) de_buf
;
211 if (de_buf
+ namelen
<= dlimit
&&
212 ocfs2_match(namelen
, name
, de
)) {
213 /* found a match - just to be sure, do a full check */
214 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
223 /* prevent looping on a bad block */
224 de_len
= le16_to_cpu(de
->rec_len
);
239 static struct buffer_head
*ocfs2_find_entry_id(const char *name
,
242 struct ocfs2_dir_entry
**res_dir
)
245 struct buffer_head
*di_bh
= NULL
;
246 struct ocfs2_dinode
*di
;
247 struct ocfs2_inline_data
*data
;
249 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
255 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
256 data
= &di
->id2
.i_data
;
258 found
= ocfs2_search_dirblock(di_bh
, dir
, name
, namelen
, 0,
259 data
->id_data
, i_size_read(dir
), res_dir
);
268 static int ocfs2_validate_dir_block(struct super_block
*sb
,
269 struct buffer_head
*bh
)
272 * Nothing yet. We don't validate dirents here, that's handled
273 * in-place when the code walks them.
275 mlog(0, "Validating dirblock %llu\n",
276 (unsigned long long)bh
->b_blocknr
);
282 * This function forces all errors to -EIO for consistency with its
283 * predecessor, ocfs2_bread(). We haven't audited what returning the
284 * real error codes would do to callers. We log the real codes with
285 * mlog_errno() before we squash them.
287 static int ocfs2_read_dir_block(struct inode
*inode
, u64 v_block
,
288 struct buffer_head
**bh
, int flags
)
291 struct buffer_head
*tmp
= *bh
;
292 struct ocfs2_dir_block_trailer
*trailer
;
294 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, flags
,
295 ocfs2_validate_dir_block
);
302 * We check the trailer here rather than in
303 * ocfs2_validate_dir_block() because that function doesn't have
306 if (!(flags
& OCFS2_BH_READAHEAD
) &&
307 ocfs2_dir_has_trailer(inode
)) {
308 trailer
= ocfs2_trailer_from_bh(tmp
, inode
->i_sb
);
309 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer
)) {
311 ocfs2_error(inode
->i_sb
,
312 "Invalid dirblock #%llu: "
313 "signature = %.*s\n",
314 (unsigned long long)tmp
->b_blocknr
, 7,
315 trailer
->db_signature
);
318 if (le64_to_cpu(trailer
->db_blkno
) != tmp
->b_blocknr
) {
320 ocfs2_error(inode
->i_sb
,
321 "Directory block #%llu has an invalid "
323 (unsigned long long)tmp
->b_blocknr
,
324 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
327 if (le64_to_cpu(trailer
->db_parent_dinode
) !=
328 OCFS2_I(inode
)->ip_blkno
) {
330 ocfs2_error(inode
->i_sb
,
331 "Directory block #%llu on dinode "
332 "#%llu has an invalid parent_dinode "
334 (unsigned long long)tmp
->b_blocknr
,
335 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
336 (unsigned long long)le64_to_cpu(trailer
->db_blkno
));
341 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
346 return rc
? -EIO
: 0;
349 static struct buffer_head
*ocfs2_find_entry_el(const char *name
, int namelen
,
351 struct ocfs2_dir_entry
**res_dir
)
353 struct super_block
*sb
;
354 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
355 struct buffer_head
*bh
, *ret
= NULL
;
356 unsigned long start
, block
, b
;
357 int ra_max
= 0; /* Number of bh's in the readahead
359 int ra_ptr
= 0; /* Current index into readahead
368 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
369 start
= OCFS2_I(dir
)->ip_dir_start_lookup
;
370 if (start
>= nblocks
)
377 * We deal with the read-ahead logic here.
379 if (ra_ptr
>= ra_max
) {
380 /* Refill the readahead buffer */
383 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
385 * Terminate if we reach the end of the
386 * directory and must wrap, or if our
387 * search has finished at this block.
389 if (b
>= nblocks
|| (num
&& block
== start
)) {
390 bh_use
[ra_max
] = NULL
;
396 err
= ocfs2_read_dir_block(dir
, b
++, &bh
,
401 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
403 if (ocfs2_read_dir_block(dir
, block
, &bh
, 0)) {
404 /* read error, skip block & hope for the best.
405 * ocfs2_read_dir_block() has released the bh. */
406 ocfs2_error(dir
->i_sb
, "reading directory %llu, "
408 (unsigned long long)OCFS2_I(dir
)->ip_blkno
,
412 i
= ocfs2_search_dirblock(bh
, dir
, name
, namelen
,
413 block
<< sb
->s_blocksize_bits
,
414 bh
->b_data
, sb
->s_blocksize
,
417 OCFS2_I(dir
)->ip_dir_start_lookup
= block
;
419 goto cleanup_and_exit
;
423 goto cleanup_and_exit
;
426 if (++block
>= nblocks
)
428 } while (block
!= start
);
431 * If the directory has grown while we were searching, then
432 * search the last part of the directory before giving up.
435 nblocks
= i_size_read(dir
) >> sb
->s_blocksize_bits
;
436 if (block
< nblocks
) {
442 /* Clean up the read-ahead blocks */
443 for (; ra_ptr
< ra_max
; ra_ptr
++)
444 brelse(bh_use
[ra_ptr
]);
451 * Try to find an entry of the provided name within 'dir'.
453 * If nothing was found, NULL is returned. Otherwise, a buffer_head
454 * and pointer to the dir entry are passed back.
456 * Caller can NOT assume anything about the contents of the
457 * buffer_head - it is passed back only so that it can be passed into
458 * any one of the manipulation functions (add entry, delete entry,
459 * etc). As an example, bh in the extent directory case is a data
460 * block, in the inline-data case it actually points to an inode.
462 struct buffer_head
*ocfs2_find_entry(const char *name
, int namelen
,
464 struct ocfs2_dir_entry
**res_dir
)
468 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
469 return ocfs2_find_entry_id(name
, namelen
, dir
, res_dir
);
471 return ocfs2_find_entry_el(name
, namelen
, dir
, res_dir
);
475 * Update inode number and type of a previously found directory entry.
477 int ocfs2_update_entry(struct inode
*dir
, handle_t
*handle
,
478 struct buffer_head
*de_bh
, struct ocfs2_dir_entry
*de
,
479 struct inode
*new_entry_inode
)
482 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
485 * The same code works fine for both inline-data and extent
486 * based directories, so no need to split this up. The only
487 * difference is the journal_access function.
490 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
491 access
= ocfs2_journal_access_di
;
493 ret
= access(handle
, dir
, de_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
499 de
->inode
= cpu_to_le64(OCFS2_I(new_entry_inode
)->ip_blkno
);
500 ocfs2_set_de_type(de
, new_entry_inode
->i_mode
);
502 ocfs2_journal_dirty(handle
, de_bh
);
508 static int __ocfs2_delete_entry(handle_t
*handle
, struct inode
*dir
,
509 struct ocfs2_dir_entry
*de_del
,
510 struct buffer_head
*bh
, char *first_de
,
513 struct ocfs2_dir_entry
*de
, *pde
;
514 int i
, status
= -ENOENT
;
515 ocfs2_journal_access_func access
= ocfs2_journal_access_db
;
517 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle
, dir
, de_del
, bh
);
519 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
520 access
= ocfs2_journal_access_di
;
524 de
= (struct ocfs2_dir_entry
*) first_de
;
526 if (!ocfs2_check_dir_entry(dir
, de
, bh
, i
)) {
532 status
= access(handle
, dir
, bh
,
533 OCFS2_JOURNAL_ACCESS_WRITE
);
540 le16_add_cpu(&pde
->rec_len
,
541 le16_to_cpu(de
->rec_len
));
545 status
= ocfs2_journal_dirty(handle
, bh
);
548 i
+= le16_to_cpu(de
->rec_len
);
550 de
= (struct ocfs2_dir_entry
*)((char *)de
+ le16_to_cpu(de
->rec_len
));
557 static inline int ocfs2_delete_entry_id(handle_t
*handle
,
559 struct ocfs2_dir_entry
*de_del
,
560 struct buffer_head
*bh
)
563 struct buffer_head
*di_bh
= NULL
;
564 struct ocfs2_dinode
*di
;
565 struct ocfs2_inline_data
*data
;
567 ret
= ocfs2_read_inode_block(dir
, &di_bh
);
573 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
574 data
= &di
->id2
.i_data
;
576 ret
= __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, data
->id_data
,
584 static inline int ocfs2_delete_entry_el(handle_t
*handle
,
586 struct ocfs2_dir_entry
*de_del
,
587 struct buffer_head
*bh
)
589 return __ocfs2_delete_entry(handle
, dir
, de_del
, bh
, bh
->b_data
,
594 * ocfs2_delete_entry deletes a directory entry by merging it with the
597 int ocfs2_delete_entry(handle_t
*handle
,
599 struct ocfs2_dir_entry
*de_del
,
600 struct buffer_head
*bh
)
602 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
603 return ocfs2_delete_entry_id(handle
, dir
, de_del
, bh
);
605 return ocfs2_delete_entry_el(handle
, dir
, de_del
, bh
);
609 * Check whether 'de' has enough room to hold an entry of
610 * 'new_rec_len' bytes.
612 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry
*de
,
613 unsigned int new_rec_len
)
615 unsigned int de_really_used
;
617 /* Check whether this is an empty record with enough space */
618 if (le64_to_cpu(de
->inode
) == 0 &&
619 le16_to_cpu(de
->rec_len
) >= new_rec_len
)
623 * Record might have free space at the end which we can
626 de_really_used
= OCFS2_DIR_REC_LEN(de
->name_len
);
627 if (le16_to_cpu(de
->rec_len
) >= (de_really_used
+ new_rec_len
))
633 /* we don't always have a dentry for what we want to add, so people
634 * like orphan dir can call this instead.
636 * If you pass me insert_bh, I'll skip the search of the other dir
637 * blocks and put the record in there.
639 int __ocfs2_add_entry(handle_t
*handle
,
641 const char *name
, int namelen
,
642 struct inode
*inode
, u64 blkno
,
643 struct buffer_head
*parent_fe_bh
,
644 struct buffer_head
*insert_bh
)
646 unsigned long offset
;
647 unsigned short rec_len
;
648 struct ocfs2_dir_entry
*de
, *de1
;
649 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)parent_fe_bh
->b_data
;
650 struct super_block
*sb
= dir
->i_sb
;
652 unsigned int size
= sb
->s_blocksize
;
653 char *data_start
= insert_bh
->b_data
;
660 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
661 data_start
= di
->id2
.i_data
.id_data
;
662 size
= i_size_read(dir
);
664 BUG_ON(insert_bh
!= parent_fe_bh
);
667 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
669 de
= (struct ocfs2_dir_entry
*) data_start
;
671 BUG_ON((char *)de
>= (size
+ data_start
));
673 /* These checks should've already been passed by the
674 * prepare function, but I guess we can leave them
676 if (!ocfs2_check_dir_entry(dir
, de
, insert_bh
, offset
)) {
680 if (ocfs2_match(namelen
, name
, de
)) {
685 /* We're guaranteed that we should have space, so we
686 * can't possibly have hit the trailer...right? */
687 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir
, de
, offset
, size
),
688 "Hit dir trailer trying to insert %.*s "
689 "(namelen %d) into directory %llu. "
690 "offset is %lu, trailer offset is %d\n",
691 namelen
, name
, namelen
,
692 (unsigned long long)parent_fe_bh
->b_blocknr
,
693 offset
, ocfs2_dir_trailer_blk_off(dir
->i_sb
));
695 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
696 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
697 retval
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
703 if (insert_bh
== parent_fe_bh
)
704 status
= ocfs2_journal_access_di(handle
, dir
,
706 OCFS2_JOURNAL_ACCESS_WRITE
);
708 status
= ocfs2_journal_access_db(handle
, dir
,
710 OCFS2_JOURNAL_ACCESS_WRITE
);
711 /* By now the buffer is marked for journaling */
712 offset
+= le16_to_cpu(de
->rec_len
);
713 if (le64_to_cpu(de
->inode
)) {
714 de1
= (struct ocfs2_dir_entry
*)((char *) de
+
715 OCFS2_DIR_REC_LEN(de
->name_len
));
717 cpu_to_le16(le16_to_cpu(de
->rec_len
) -
718 OCFS2_DIR_REC_LEN(de
->name_len
));
719 de
->rec_len
= cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
722 de
->file_type
= OCFS2_FT_UNKNOWN
;
724 de
->inode
= cpu_to_le64(blkno
);
725 ocfs2_set_de_type(de
, inode
->i_mode
);
728 de
->name_len
= namelen
;
729 memcpy(de
->name
, name
, namelen
);
732 status
= ocfs2_journal_dirty(handle
, insert_bh
);
737 offset
+= le16_to_cpu(de
->rec_len
);
738 de
= (struct ocfs2_dir_entry
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
741 /* when you think about it, the assert above should prevent us
742 * from ever getting here. */
750 static int ocfs2_dir_foreach_blk_id(struct inode
*inode
,
752 loff_t
*f_pos
, void *priv
,
753 filldir_t filldir
, int *filldir_err
)
755 int ret
, i
, filldir_ret
;
756 unsigned long offset
= *f_pos
;
757 struct buffer_head
*di_bh
= NULL
;
758 struct ocfs2_dinode
*di
;
759 struct ocfs2_inline_data
*data
;
760 struct ocfs2_dir_entry
*de
;
762 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
764 mlog(ML_ERROR
, "Unable to read inode block for dir %llu\n",
765 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
769 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
770 data
= &di
->id2
.i_data
;
772 while (*f_pos
< i_size_read(inode
)) {
774 /* If the dir block has changed since the last call to
775 * readdir(2), then we might be pointing to an invalid
776 * dirent right now. Scan from the start of the block
778 if (*f_version
!= inode
->i_version
) {
779 for (i
= 0; i
< i_size_read(inode
) && i
< offset
; ) {
780 de
= (struct ocfs2_dir_entry
*)
782 /* It's too expensive to do a full
783 * dirent test each time round this
784 * loop, but we do have to test at
785 * least that it is non-zero. A
786 * failure will be detected in the
787 * dirent test below. */
788 if (le16_to_cpu(de
->rec_len
) <
789 OCFS2_DIR_REC_LEN(1))
791 i
+= le16_to_cpu(de
->rec_len
);
794 *f_version
= inode
->i_version
;
797 de
= (struct ocfs2_dir_entry
*) (data
->id_data
+ *f_pos
);
798 if (!ocfs2_check_dir_entry(inode
, de
, di_bh
, *f_pos
)) {
799 /* On error, skip the f_pos to the end. */
800 *f_pos
= i_size_read(inode
);
803 offset
+= le16_to_cpu(de
->rec_len
);
804 if (le64_to_cpu(de
->inode
)) {
805 /* We might block in the next section
806 * if the data destination is
807 * currently swapped out. So, use a
808 * version stamp to detect whether or
809 * not the directory has been modified
810 * during the copy operation.
812 u64 version
= *f_version
;
813 unsigned char d_type
= DT_UNKNOWN
;
815 if (de
->file_type
< OCFS2_FT_MAX
)
816 d_type
= ocfs2_filetype_table
[de
->file_type
];
818 filldir_ret
= filldir(priv
, de
->name
,
821 le64_to_cpu(de
->inode
),
825 *filldir_err
= filldir_ret
;
828 if (version
!= *f_version
)
831 *f_pos
+= le16_to_cpu(de
->rec_len
);
840 static int ocfs2_dir_foreach_blk_el(struct inode
*inode
,
842 loff_t
*f_pos
, void *priv
,
843 filldir_t filldir
, int *filldir_err
)
846 unsigned long offset
, blk
, last_ra_blk
= 0;
848 struct buffer_head
* bh
, * tmp
;
849 struct ocfs2_dir_entry
* de
;
850 struct super_block
* sb
= inode
->i_sb
;
851 unsigned int ra_sectors
= 16;
856 offset
= (*f_pos
) & (sb
->s_blocksize
- 1);
858 while (!error
&& !stored
&& *f_pos
< i_size_read(inode
)) {
859 blk
= (*f_pos
) >> sb
->s_blocksize_bits
;
860 if (ocfs2_read_dir_block(inode
, blk
, &bh
, 0)) {
861 /* Skip the corrupt dirblock and keep trying */
862 *f_pos
+= sb
->s_blocksize
- offset
;
866 /* The idea here is to begin with 8k read-ahead and to stay
867 * 4k ahead of our current position.
869 * TODO: Use the pagecache for this. We just need to
870 * make sure it's cluster-safe... */
872 || (((last_ra_blk
- blk
) << 9) <= (ra_sectors
/ 2))) {
873 for (i
= ra_sectors
>> (sb
->s_blocksize_bits
- 9);
876 if (!ocfs2_read_dir_block(inode
, ++blk
, &tmp
,
885 /* If the dir block has changed since the last call to
886 * readdir(2), then we might be pointing to an invalid
887 * dirent right now. Scan from the start of the block
889 if (*f_version
!= inode
->i_version
) {
890 for (i
= 0; i
< sb
->s_blocksize
&& i
< offset
; ) {
891 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ i
);
892 /* It's too expensive to do a full
893 * dirent test each time round this
894 * loop, but we do have to test at
895 * least that it is non-zero. A
896 * failure will be detected in the
897 * dirent test below. */
898 if (le16_to_cpu(de
->rec_len
) <
899 OCFS2_DIR_REC_LEN(1))
901 i
+= le16_to_cpu(de
->rec_len
);
904 *f_pos
= ((*f_pos
) & ~(sb
->s_blocksize
- 1))
906 *f_version
= inode
->i_version
;
909 while (!error
&& *f_pos
< i_size_read(inode
)
910 && offset
< sb
->s_blocksize
) {
911 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ offset
);
912 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
913 /* On error, skip the f_pos to the
915 *f_pos
= ((*f_pos
) | (sb
->s_blocksize
- 1)) + 1;
919 offset
+= le16_to_cpu(de
->rec_len
);
920 if (le64_to_cpu(de
->inode
)) {
921 /* We might block in the next section
922 * if the data destination is
923 * currently swapped out. So, use a
924 * version stamp to detect whether or
925 * not the directory has been modified
926 * during the copy operation.
928 unsigned long version
= *f_version
;
929 unsigned char d_type
= DT_UNKNOWN
;
931 if (de
->file_type
< OCFS2_FT_MAX
)
932 d_type
= ocfs2_filetype_table
[de
->file_type
];
933 error
= filldir(priv
, de
->name
,
936 le64_to_cpu(de
->inode
),
940 *filldir_err
= error
;
943 if (version
!= *f_version
)
947 *f_pos
+= le16_to_cpu(de
->rec_len
);
959 static int ocfs2_dir_foreach_blk(struct inode
*inode
, u64
*f_version
,
960 loff_t
*f_pos
, void *priv
, filldir_t filldir
,
963 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
964 return ocfs2_dir_foreach_blk_id(inode
, f_version
, f_pos
, priv
,
965 filldir
, filldir_err
);
967 return ocfs2_dir_foreach_blk_el(inode
, f_version
, f_pos
, priv
, filldir
,
972 * This is intended to be called from inside other kernel functions,
973 * so we fake some arguments.
975 int ocfs2_dir_foreach(struct inode
*inode
, loff_t
*f_pos
, void *priv
,
978 int ret
= 0, filldir_err
= 0;
979 u64 version
= inode
->i_version
;
981 while (*f_pos
< i_size_read(inode
)) {
982 ret
= ocfs2_dir_foreach_blk(inode
, &version
, f_pos
, priv
,
983 filldir
, &filldir_err
);
984 if (ret
|| filldir_err
)
998 int ocfs2_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1001 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1004 mlog_entry("dirino=%llu\n",
1005 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1007 error
= ocfs2_inode_lock_atime(inode
, filp
->f_vfsmnt
, &lock_level
);
1008 if (lock_level
&& error
>= 0) {
1009 /* We release EX lock which used to update atime
1010 * and get PR lock again to reduce contention
1011 * on commonly accessed directories. */
1012 ocfs2_inode_unlock(inode
, 1);
1014 error
= ocfs2_inode_lock(inode
, NULL
, 0);
1017 if (error
!= -ENOENT
)
1019 /* we haven't got any yet, so propagate the error. */
1023 error
= ocfs2_dir_foreach_blk(inode
, &filp
->f_version
, &filp
->f_pos
,
1024 dirent
, filldir
, NULL
);
1026 ocfs2_inode_unlock(inode
, lock_level
);
1035 * NOTE: this should always be called with parent dir i_mutex taken.
1037 int ocfs2_find_files_on_disk(const char *name
,
1040 struct inode
*inode
,
1041 struct buffer_head
**dirent_bh
,
1042 struct ocfs2_dir_entry
**dirent
)
1044 int status
= -ENOENT
;
1046 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1047 namelen
, name
, blkno
, inode
, dirent_bh
, dirent
);
1049 *dirent_bh
= ocfs2_find_entry(name
, namelen
, inode
, dirent
);
1050 if (!*dirent_bh
|| !*dirent
) {
1055 *blkno
= le64_to_cpu((*dirent
)->inode
);
1070 * Convenience function for callers which just want the block number
1071 * mapped to a name and don't require the full dirent info, etc.
1073 int ocfs2_lookup_ino_from_name(struct inode
*dir
, const char *name
,
1074 int namelen
, u64
*blkno
)
1077 struct buffer_head
*bh
= NULL
;
1078 struct ocfs2_dir_entry
*dirent
= NULL
;
1080 ret
= ocfs2_find_files_on_disk(name
, namelen
, blkno
, dir
, &bh
, &dirent
);
1086 /* Check for a name within a directory.
1088 * Return 0 if the name does not exist
1089 * Return -EEXIST if the directory contains the name
1091 * Callers should have i_mutex + a cluster lock on dir
1093 int ocfs2_check_dir_for_entry(struct inode
*dir
,
1098 struct buffer_head
*dirent_bh
= NULL
;
1099 struct ocfs2_dir_entry
*dirent
= NULL
;
1101 mlog_entry("dir %llu, name '%.*s'\n",
1102 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, namelen
, name
);
1105 dirent_bh
= ocfs2_find_entry(name
, namelen
, dir
, &dirent
);
1117 struct ocfs2_empty_dir_priv
{
1119 unsigned seen_dot_dot
;
1120 unsigned seen_other
;
1122 static int ocfs2_empty_dir_filldir(void *priv
, const char *name
, int name_len
,
1123 loff_t pos
, u64 ino
, unsigned type
)
1125 struct ocfs2_empty_dir_priv
*p
= priv
;
1128 * Check the positions of "." and ".." records to be sure
1129 * they're in the correct place.
1131 if (name_len
== 1 && !strncmp(".", name
, 1) && pos
== 0) {
1136 if (name_len
== 2 && !strncmp("..", name
, 2) &&
1137 pos
== OCFS2_DIR_REC_LEN(1)) {
1138 p
->seen_dot_dot
= 1;
1146 * routine to check that the specified directory is empty (for rmdir)
1148 * Returns 1 if dir is empty, zero otherwise.
1150 int ocfs2_empty_dir(struct inode
*inode
)
1154 struct ocfs2_empty_dir_priv priv
;
1156 memset(&priv
, 0, sizeof(priv
));
1158 ret
= ocfs2_dir_foreach(inode
, &start
, &priv
, ocfs2_empty_dir_filldir
);
1162 if (!priv
.seen_dot
|| !priv
.seen_dot_dot
) {
1163 mlog(ML_ERROR
, "bad directory (dir #%llu) - no `.' or `..'\n",
1164 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1166 * XXX: Is it really safe to allow an unlink to continue?
1171 return !priv
.seen_other
;
1175 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1176 * "..", which might be used during creation of a directory with a trailing
1177 * header. It is otherwise safe to ignore the return code.
1179 static struct ocfs2_dir_entry
*ocfs2_fill_initial_dirents(struct inode
*inode
,
1180 struct inode
*parent
,
1184 struct ocfs2_dir_entry
*de
= (struct ocfs2_dir_entry
*)start
;
1186 de
->inode
= cpu_to_le64(OCFS2_I(inode
)->ip_blkno
);
1189 cpu_to_le16(OCFS2_DIR_REC_LEN(de
->name_len
));
1190 strcpy(de
->name
, ".");
1191 ocfs2_set_de_type(de
, S_IFDIR
);
1193 de
= (struct ocfs2_dir_entry
*) ((char *)de
+ le16_to_cpu(de
->rec_len
));
1194 de
->inode
= cpu_to_le64(OCFS2_I(parent
)->ip_blkno
);
1195 de
->rec_len
= cpu_to_le16(size
- OCFS2_DIR_REC_LEN(1));
1197 strcpy(de
->name
, "..");
1198 ocfs2_set_de_type(de
, S_IFDIR
);
1204 * This works together with code in ocfs2_mknod_locked() which sets
1205 * the inline-data flag and initializes the inline-data section.
1207 static int ocfs2_fill_new_dir_id(struct ocfs2_super
*osb
,
1209 struct inode
*parent
,
1210 struct inode
*inode
,
1211 struct buffer_head
*di_bh
)
1214 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1215 struct ocfs2_inline_data
*data
= &di
->id2
.i_data
;
1216 unsigned int size
= le16_to_cpu(data
->id_count
);
1218 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
1219 OCFS2_JOURNAL_ACCESS_WRITE
);
1225 ocfs2_fill_initial_dirents(inode
, parent
, data
->id_data
, size
);
1227 ocfs2_journal_dirty(handle
, di_bh
);
1233 i_size_write(inode
, size
);
1235 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1237 ret
= ocfs2_mark_inode_dirty(handle
, inode
, di_bh
);
1245 static int ocfs2_fill_new_dir_el(struct ocfs2_super
*osb
,
1247 struct inode
*parent
,
1248 struct inode
*inode
,
1249 struct buffer_head
*fe_bh
,
1250 struct ocfs2_alloc_context
*data_ac
)
1253 unsigned int size
= osb
->sb
->s_blocksize
;
1254 struct buffer_head
*new_bh
= NULL
;
1255 struct ocfs2_dir_entry
*de
;
1259 if (ocfs2_supports_dir_trailer(osb
))
1260 size
= ocfs2_dir_trailer_blk_off(parent
->i_sb
);
1262 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, inode
, fe_bh
,
1263 data_ac
, NULL
, &new_bh
);
1269 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1271 status
= ocfs2_journal_access_db(handle
, inode
, new_bh
,
1272 OCFS2_JOURNAL_ACCESS_CREATE
);
1277 memset(new_bh
->b_data
, 0, osb
->sb
->s_blocksize
);
1279 de
= ocfs2_fill_initial_dirents(inode
, parent
, new_bh
->b_data
, size
);
1280 if (ocfs2_supports_dir_trailer(osb
))
1281 ocfs2_init_dir_trailer(inode
, new_bh
);
1283 status
= ocfs2_journal_dirty(handle
, new_bh
);
1289 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
1291 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1292 status
= ocfs2_mark_inode_dirty(handle
, inode
, fe_bh
);
1306 int ocfs2_fill_new_dir(struct ocfs2_super
*osb
,
1308 struct inode
*parent
,
1309 struct inode
*inode
,
1310 struct buffer_head
*fe_bh
,
1311 struct ocfs2_alloc_context
*data_ac
)
1313 BUG_ON(!ocfs2_supports_inline_data(osb
) && data_ac
== NULL
);
1315 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
1316 return ocfs2_fill_new_dir_id(osb
, handle
, parent
, inode
, fe_bh
);
1318 return ocfs2_fill_new_dir_el(osb
, handle
, parent
, inode
, fe_bh
,
1323 * Expand rec_len of the rightmost dirent in a directory block so that it
1324 * contains the end of our valid space for dirents. We do this during
1325 * expansion from an inline directory to one with extents. The first dir block
1326 * in that case is taken from the inline data portion of the inode block.
1328 * We add the dir trailer if this filesystem wants it.
1330 static void ocfs2_expand_last_dirent(char *start
, unsigned int old_size
,
1331 struct super_block
*sb
)
1333 struct ocfs2_dir_entry
*de
;
1334 struct ocfs2_dir_entry
*prev_de
;
1335 char *de_buf
, *limit
;
1336 unsigned int new_size
= sb
->s_blocksize
;
1339 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb
)))
1340 new_size
= ocfs2_dir_trailer_blk_off(sb
);
1342 bytes
= new_size
- old_size
;
1344 limit
= start
+ old_size
;
1346 de
= (struct ocfs2_dir_entry
*)de_buf
;
1349 de_buf
+= le16_to_cpu(de
->rec_len
);
1350 de
= (struct ocfs2_dir_entry
*)de_buf
;
1351 } while (de_buf
< limit
);
1353 le16_add_cpu(&prev_de
->rec_len
, bytes
);
1357 * We allocate enough clusters to fulfill "blocks_wanted", but set
1358 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1359 * rest automatically for us.
1361 * *first_block_bh is a pointer to the 1st data block allocated to the
1364 static int ocfs2_expand_inline_dir(struct inode
*dir
, struct buffer_head
*di_bh
,
1365 unsigned int blocks_wanted
,
1366 struct buffer_head
**first_block_bh
)
1368 u32 alloc
, bit_off
, len
;
1369 struct super_block
*sb
= dir
->i_sb
;
1370 int ret
, credits
= ocfs2_inline_to_extents_credits(sb
);
1371 u64 blkno
, bytes
= blocks_wanted
<< sb
->s_blocksize_bits
;
1372 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
1373 struct ocfs2_inode_info
*oi
= OCFS2_I(dir
);
1374 struct ocfs2_alloc_context
*data_ac
;
1375 struct buffer_head
*dirdata_bh
= NULL
;
1376 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1378 struct ocfs2_extent_tree et
;
1381 ocfs2_init_dinode_extent_tree(&et
, dir
, di_bh
);
1383 alloc
= ocfs2_clusters_for_bytes(sb
, bytes
);
1386 * We should never need more than 2 clusters for this -
1387 * maximum dirent size is far less than one block. In fact,
1388 * the only time we'd need more than one cluster is if
1389 * blocksize == clustersize and the dirent won't fit in the
1390 * extra space that the expansion to a single block gives. As
1391 * of today, that only happens on 4k/4k file systems.
1395 ret
= ocfs2_reserve_clusters(osb
, alloc
, &data_ac
);
1401 down_write(&oi
->ip_alloc_sem
);
1404 * Prepare for worst case allocation scenario of two separate
1408 credits
+= OCFS2_SUBALLOC_ALLOC
;
1410 handle
= ocfs2_start_trans(osb
, credits
);
1411 if (IS_ERR(handle
)) {
1412 ret
= PTR_ERR(handle
);
1417 if (vfs_dq_alloc_space_nodirty(dir
,
1418 ocfs2_clusters_to_bytes(osb
->sb
, alloc
))) {
1424 * Try to claim as many clusters as the bitmap can give though
1425 * if we only get one now, that's enough to continue. The rest
1426 * will be claimed after the conversion to extents.
1428 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
1435 * Operations are carefully ordered so that we set up the new
1436 * data block first. The conversion from inline data to
1439 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
1440 dirdata_bh
= sb_getblk(sb
, blkno
);
1447 ocfs2_set_new_buffer_uptodate(dir
, dirdata_bh
);
1449 ret
= ocfs2_journal_access_db(handle
, dir
, dirdata_bh
,
1450 OCFS2_JOURNAL_ACCESS_CREATE
);
1456 memcpy(dirdata_bh
->b_data
, di
->id2
.i_data
.id_data
, i_size_read(dir
));
1457 memset(dirdata_bh
->b_data
+ i_size_read(dir
), 0,
1458 sb
->s_blocksize
- i_size_read(dir
));
1459 ocfs2_expand_last_dirent(dirdata_bh
->b_data
, i_size_read(dir
), sb
);
1460 if (ocfs2_supports_dir_trailer(osb
))
1461 ocfs2_init_dir_trailer(dir
, dirdata_bh
);
1463 ret
= ocfs2_journal_dirty(handle
, dirdata_bh
);
1470 * Set extent, i_size, etc on the directory. After this, the
1471 * inode should contain the same exact dirents as before and
1472 * be fully accessible from system calls.
1474 * We let the later dirent insert modify c/mtime - to the user
1475 * the data hasn't changed.
1477 ret
= ocfs2_journal_access_di(handle
, dir
, di_bh
,
1478 OCFS2_JOURNAL_ACCESS_CREATE
);
1484 spin_lock(&oi
->ip_lock
);
1485 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
1486 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1487 spin_unlock(&oi
->ip_lock
);
1489 ocfs2_dinode_new_extent_list(dir
, di
);
1491 i_size_write(dir
, sb
->s_blocksize
);
1492 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1494 di
->i_size
= cpu_to_le64(sb
->s_blocksize
);
1495 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(dir
->i_ctime
.tv_sec
);
1496 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(dir
->i_ctime
.tv_nsec
);
1499 * This should never fail as our extent list is empty and all
1500 * related blocks have been journaled already.
1502 ret
= ocfs2_insert_extent(osb
, handle
, dir
, &et
, 0, blkno
, len
,
1510 * Set i_blocks after the extent insert for the most up to
1511 * date ip_clusters value.
1513 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
1515 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1522 * We asked for two clusters, but only got one in the 1st
1523 * pass. Claim the 2nd cluster as a separate extent.
1526 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
1532 blkno
= ocfs2_clusters_to_blocks(dir
->i_sb
, bit_off
);
1534 ret
= ocfs2_insert_extent(osb
, handle
, dir
, &et
, 1,
1535 blkno
, len
, 0, NULL
);
1542 *first_block_bh
= dirdata_bh
;
1546 if (ret
< 0 && did_quota
)
1547 vfs_dq_free_space_nodirty(dir
,
1548 ocfs2_clusters_to_bytes(osb
->sb
, 2));
1549 ocfs2_commit_trans(osb
, handle
);
1552 up_write(&oi
->ip_alloc_sem
);
1556 ocfs2_free_alloc_context(data_ac
);
1563 /* returns a bh of the 1st new block in the allocation. */
1564 static int ocfs2_do_extend_dir(struct super_block
*sb
,
1567 struct buffer_head
*parent_fe_bh
,
1568 struct ocfs2_alloc_context
*data_ac
,
1569 struct ocfs2_alloc_context
*meta_ac
,
1570 struct buffer_head
**new_bh
)
1573 int extend
, did_quota
= 0;
1574 u64 p_blkno
, v_blkno
;
1576 spin_lock(&OCFS2_I(dir
)->ip_lock
);
1577 extend
= (i_size_read(dir
) == ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
));
1578 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1581 u32 offset
= OCFS2_I(dir
)->ip_clusters
;
1583 if (vfs_dq_alloc_space_nodirty(dir
,
1584 ocfs2_clusters_to_bytes(sb
, 1))) {
1590 status
= ocfs2_add_inode_data(OCFS2_SB(sb
), dir
, &offset
,
1591 1, 0, parent_fe_bh
, handle
,
1592 data_ac
, meta_ac
, NULL
);
1593 BUG_ON(status
== -EAGAIN
);
1600 v_blkno
= ocfs2_blocks_for_bytes(sb
, i_size_read(dir
));
1601 status
= ocfs2_extent_map_get_blocks(dir
, v_blkno
, &p_blkno
, NULL
, NULL
);
1607 *new_bh
= sb_getblk(sb
, p_blkno
);
1615 if (did_quota
&& status
< 0)
1616 vfs_dq_free_space_nodirty(dir
, ocfs2_clusters_to_bytes(sb
, 1));
1622 * Assumes you already have a cluster lock on the directory.
1624 * 'blocks_wanted' is only used if we have an inline directory which
1625 * is to be turned into an extent based one. The size of the dirent to
1626 * insert might be larger than the space gained by growing to just one
1627 * block, so we may have to grow the inode by two blocks in that case.
1629 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
1631 struct buffer_head
*parent_fe_bh
,
1632 unsigned int blocks_wanted
,
1633 struct buffer_head
**new_de_bh
)
1636 int credits
, num_free_extents
, drop_alloc_sem
= 0;
1638 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
1639 struct ocfs2_extent_list
*el
= &fe
->id2
.i_list
;
1640 struct ocfs2_alloc_context
*data_ac
= NULL
;
1641 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1642 handle_t
*handle
= NULL
;
1643 struct buffer_head
*new_bh
= NULL
;
1644 struct ocfs2_dir_entry
* de
;
1645 struct super_block
*sb
= osb
->sb
;
1646 struct ocfs2_extent_tree et
;
1650 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1651 status
= ocfs2_expand_inline_dir(dir
, parent_fe_bh
,
1652 blocks_wanted
, &new_bh
);
1658 if (blocks_wanted
== 1) {
1660 * If the new dirent will fit inside the space
1661 * created by pushing out to one block, then
1662 * we can complete the operation
1663 * here. Otherwise we have to expand i_size
1664 * and format the 2nd block below.
1666 BUG_ON(new_bh
== NULL
);
1671 * Get rid of 'new_bh' - we want to format the 2nd
1672 * data block and return that instead.
1677 dir_i_size
= i_size_read(dir
);
1678 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
1682 dir_i_size
= i_size_read(dir
);
1683 mlog(0, "extending dir %llu (i_size = %lld)\n",
1684 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, dir_i_size
);
1686 /* dir->i_size is always block aligned. */
1687 spin_lock(&OCFS2_I(dir
)->ip_lock
);
1688 if (dir_i_size
== ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
)) {
1689 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1690 ocfs2_init_dinode_extent_tree(&et
, dir
, parent_fe_bh
);
1691 num_free_extents
= ocfs2_num_free_extents(osb
, dir
, &et
);
1692 if (num_free_extents
< 0) {
1693 status
= num_free_extents
;
1698 if (!num_free_extents
) {
1699 status
= ocfs2_reserve_new_metadata(osb
, el
, &meta_ac
);
1701 if (status
!= -ENOSPC
)
1707 status
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
1709 if (status
!= -ENOSPC
)
1714 credits
= ocfs2_calc_extend_credits(sb
, el
, 1);
1716 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
1717 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
1721 down_write(&OCFS2_I(dir
)->ip_alloc_sem
);
1724 handle
= ocfs2_start_trans(osb
, credits
);
1725 if (IS_ERR(handle
)) {
1726 status
= PTR_ERR(handle
);
1732 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, dir
, parent_fe_bh
,
1733 data_ac
, meta_ac
, &new_bh
);
1739 ocfs2_set_new_buffer_uptodate(dir
, new_bh
);
1741 status
= ocfs2_journal_access_db(handle
, dir
, new_bh
,
1742 OCFS2_JOURNAL_ACCESS_CREATE
);
1747 memset(new_bh
->b_data
, 0, sb
->s_blocksize
);
1749 de
= (struct ocfs2_dir_entry
*) new_bh
->b_data
;
1751 if (ocfs2_dir_has_trailer(dir
)) {
1752 de
->rec_len
= cpu_to_le16(ocfs2_dir_trailer_blk_off(sb
));
1753 ocfs2_init_dir_trailer(dir
, new_bh
);
1755 de
->rec_len
= cpu_to_le16(sb
->s_blocksize
);
1757 status
= ocfs2_journal_dirty(handle
, new_bh
);
1763 dir_i_size
+= dir
->i_sb
->s_blocksize
;
1764 i_size_write(dir
, dir_i_size
);
1765 dir
->i_blocks
= ocfs2_inode_sector_count(dir
);
1766 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
1773 *new_de_bh
= new_bh
;
1777 up_write(&OCFS2_I(dir
)->ip_alloc_sem
);
1779 ocfs2_commit_trans(osb
, handle
);
1782 ocfs2_free_alloc_context(data_ac
);
1784 ocfs2_free_alloc_context(meta_ac
);
1792 static int ocfs2_find_dir_space_id(struct inode
*dir
, struct buffer_head
*di_bh
,
1793 const char *name
, int namelen
,
1794 struct buffer_head
**ret_de_bh
,
1795 unsigned int *blocks_wanted
)
1798 struct super_block
*sb
= dir
->i_sb
;
1799 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1800 struct ocfs2_dir_entry
*de
, *last_de
= NULL
;
1801 char *de_buf
, *limit
;
1802 unsigned long offset
= 0;
1803 unsigned int rec_len
, new_rec_len
, free_space
= dir
->i_sb
->s_blocksize
;
1806 * This calculates how many free bytes we'd have in block zero, should
1807 * this function force expansion to an extent tree.
1809 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb
)))
1810 free_space
= ocfs2_dir_trailer_blk_off(sb
) - i_size_read(dir
);
1812 free_space
= dir
->i_sb
->s_blocksize
- i_size_read(dir
);
1814 de_buf
= di
->id2
.i_data
.id_data
;
1815 limit
= de_buf
+ i_size_read(dir
);
1816 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
1818 while (de_buf
< limit
) {
1819 de
= (struct ocfs2_dir_entry
*)de_buf
;
1821 if (!ocfs2_check_dir_entry(dir
, de
, di_bh
, offset
)) {
1825 if (ocfs2_match(namelen
, name
, de
)) {
1830 * No need to check for a trailing dirent record here as
1831 * they're not used for inline dirs.
1834 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
1835 /* Ok, we found a spot. Return this bh and let
1836 * the caller actually fill it in. */
1844 de_buf
+= le16_to_cpu(de
->rec_len
);
1845 offset
+= le16_to_cpu(de
->rec_len
);
1849 * We're going to require expansion of the directory - figure
1850 * out how many blocks we'll need so that a place for the
1851 * dirent can be found.
1854 new_rec_len
= le16_to_cpu(last_de
->rec_len
) + free_space
;
1855 if (new_rec_len
< (rec_len
+ OCFS2_DIR_REC_LEN(last_de
->name_len
)))
1863 static int ocfs2_find_dir_space_el(struct inode
*dir
, const char *name
,
1864 int namelen
, struct buffer_head
**ret_de_bh
)
1866 unsigned long offset
;
1867 struct buffer_head
*bh
= NULL
;
1868 unsigned short rec_len
;
1869 struct ocfs2_dir_entry
*de
;
1870 struct super_block
*sb
= dir
->i_sb
;
1872 int blocksize
= dir
->i_sb
->s_blocksize
;
1874 status
= ocfs2_read_dir_block(dir
, 0, &bh
, 0);
1880 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
1882 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
1884 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
1888 if (i_size_read(dir
) <= offset
) {
1890 * Caller will have to expand this
1896 status
= ocfs2_read_dir_block(dir
,
1897 offset
>> sb
->s_blocksize_bits
,
1903 /* move to next block */
1904 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
1906 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
1910 if (ocfs2_match(namelen
, name
, de
)) {
1915 if (ocfs2_skip_dir_trailer(dir
, de
, offset
% blocksize
,
1919 if (ocfs2_dirent_would_fit(de
, rec_len
)) {
1920 /* Ok, we found a spot. Return this bh and let
1921 * the caller actually fill it in. */
1928 offset
+= le16_to_cpu(de
->rec_len
);
1929 de
= (struct ocfs2_dir_entry
*)((char *) de
+ le16_to_cpu(de
->rec_len
));
1940 int ocfs2_prepare_dir_for_insert(struct ocfs2_super
*osb
,
1942 struct buffer_head
*parent_fe_bh
,
1945 struct buffer_head
**ret_de_bh
)
1948 unsigned int blocks_wanted
= 1;
1949 struct buffer_head
*bh
= NULL
;
1951 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1952 namelen
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
1962 if (OCFS2_I(dir
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1963 ret
= ocfs2_find_dir_space_id(dir
, parent_fe_bh
, name
,
1964 namelen
, &bh
, &blocks_wanted
);
1966 ret
= ocfs2_find_dir_space_el(dir
, name
, namelen
, &bh
);
1968 if (ret
&& ret
!= -ENOSPC
) {
1973 if (ret
== -ENOSPC
) {
1975 * We have to expand the directory to add this name.
1979 ret
= ocfs2_extend_dir(osb
, dir
, parent_fe_bh
, blocks_wanted
,