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"
60 #include "buffer_head_io.h"
62 static unsigned char ocfs2_filetype_table
[] = {
63 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
66 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
68 struct buffer_head
*parent_fe_bh
,
69 struct buffer_head
**new_de_bh
);
74 int ocfs2_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
77 unsigned long offset
, blk
, last_ra_blk
= 0;
79 struct buffer_head
* bh
, * tmp
;
80 struct ocfs2_dir_entry
* de
;
82 struct inode
*inode
= filp
->f_dentry
->d_inode
;
83 struct super_block
* sb
= inode
->i_sb
;
84 unsigned int ra_sectors
= 16;
87 mlog_entry("dirino=%llu\n",
88 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
93 error
= ocfs2_meta_lock_atime(inode
, filp
->f_vfsmnt
, &lock_level
);
94 if (lock_level
&& error
>= 0) {
95 /* We release EX lock which used to update atime
96 * and get PR lock again to reduce contention
97 * on commonly accessed directories. */
98 ocfs2_meta_unlock(inode
, 1);
100 error
= ocfs2_meta_lock(inode
, NULL
, 0);
103 if (error
!= -ENOENT
)
105 /* we haven't got any yet, so propagate the error. */
110 offset
= filp
->f_pos
& (sb
->s_blocksize
- 1);
112 while (!error
&& !stored
&& filp
->f_pos
< i_size_read(inode
)) {
113 blk
= (filp
->f_pos
) >> sb
->s_blocksize_bits
;
114 bh
= ocfs2_bread(inode
, blk
, &err
, 0);
117 "directory #%llu contains a hole at offset %lld\n",
118 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
120 filp
->f_pos
+= sb
->s_blocksize
- offset
;
124 /* The idea here is to begin with 8k read-ahead and to stay
125 * 4k ahead of our current position.
127 * TODO: Use the pagecache for this. We just need to
128 * make sure it's cluster-safe... */
130 || (((last_ra_blk
- blk
) << 9) <= (ra_sectors
/ 2))) {
131 for (i
= ra_sectors
>> (sb
->s_blocksize_bits
- 9);
133 tmp
= ocfs2_bread(inode
, ++blk
, &err
, 1);
142 /* If the dir block has changed since the last call to
143 * readdir(2), then we might be pointing to an invalid
144 * dirent right now. Scan from the start of the block
146 if (filp
->f_version
!= inode
->i_version
) {
147 for (i
= 0; i
< sb
->s_blocksize
&& i
< offset
; ) {
148 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ i
);
149 /* It's too expensive to do a full
150 * dirent test each time round this
151 * loop, but we do have to test at
152 * least that it is non-zero. A
153 * failure will be detected in the
154 * dirent test below. */
155 if (le16_to_cpu(de
->rec_len
) <
156 OCFS2_DIR_REC_LEN(1))
158 i
+= le16_to_cpu(de
->rec_len
);
161 filp
->f_pos
= (filp
->f_pos
& ~(sb
->s_blocksize
- 1))
163 filp
->f_version
= inode
->i_version
;
166 while (!error
&& filp
->f_pos
< i_size_read(inode
)
167 && offset
< sb
->s_blocksize
) {
168 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ offset
);
169 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
170 /* On error, skip the f_pos to the
172 filp
->f_pos
= (filp
->f_pos
|
173 (sb
->s_blocksize
- 1)) + 1;
177 offset
+= le16_to_cpu(de
->rec_len
);
178 if (le64_to_cpu(de
->inode
)) {
179 /* We might block in the next section
180 * if the data destination is
181 * currently swapped out. So, use a
182 * version stamp to detect whether or
183 * not the directory has been modified
184 * during the copy operation.
186 unsigned long version
= filp
->f_version
;
187 unsigned char d_type
= DT_UNKNOWN
;
189 if (de
->file_type
< OCFS2_FT_MAX
)
190 d_type
= ocfs2_filetype_table
[de
->file_type
];
191 error
= filldir(dirent
, de
->name
,
194 ino_from_blkno(sb
, le64_to_cpu(de
->inode
)),
198 if (version
!= filp
->f_version
)
202 filp
->f_pos
+= le16_to_cpu(de
->rec_len
);
210 ocfs2_meta_unlock(inode
, lock_level
);
219 * NOTE: this should always be called with parent dir i_mutex taken.
221 int ocfs2_find_files_on_disk(const char *name
,
225 struct buffer_head
**dirent_bh
,
226 struct ocfs2_dir_entry
**dirent
)
228 int status
= -ENOENT
;
230 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
231 namelen
, name
, blkno
, inode
, dirent_bh
, dirent
);
233 *dirent_bh
= ocfs2_find_entry(name
, namelen
, inode
, dirent
);
234 if (!*dirent_bh
|| !*dirent
) {
239 *blkno
= le64_to_cpu((*dirent
)->inode
);
255 /* Check for a name within a directory.
257 * Return 0 if the name does not exist
258 * Return -EEXIST if the directory contains the name
260 * Callers should have i_mutex + a cluster lock on dir
262 int ocfs2_check_dir_for_entry(struct inode
*dir
,
267 struct buffer_head
*dirent_bh
= NULL
;
268 struct ocfs2_dir_entry
*dirent
= NULL
;
270 mlog_entry("dir %llu, name '%.*s'\n",
271 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, namelen
, name
);
274 dirent_bh
= ocfs2_find_entry(name
, namelen
, dir
, &dirent
);
288 * routine to check that the specified directory is empty (for rmdir)
290 int ocfs2_empty_dir(struct inode
*inode
)
292 unsigned long offset
;
293 struct buffer_head
* bh
;
294 struct ocfs2_dir_entry
* de
, * de1
;
295 struct super_block
* sb
;
299 if ((i_size_read(inode
) <
300 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
301 !(bh
= ocfs2_bread(inode
, 0, &err
, 0))) {
302 mlog(ML_ERROR
, "bad directory (dir #%llu) - no data block\n",
303 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
307 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
308 de1
= (struct ocfs2_dir_entry
*)
309 ((char *)de
+ le16_to_cpu(de
->rec_len
));
310 if ((le64_to_cpu(de
->inode
) != OCFS2_I(inode
)->ip_blkno
) ||
311 !le64_to_cpu(de1
->inode
) ||
312 strcmp(".", de
->name
) ||
313 strcmp("..", de1
->name
)) {
314 mlog(ML_ERROR
, "bad directory (dir #%llu) - no `.' or `..'\n",
315 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
319 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
320 de
= (struct ocfs2_dir_entry
*)((char *)de1
+ le16_to_cpu(de1
->rec_len
));
321 while (offset
< i_size_read(inode
) ) {
322 if (!bh
|| (void *)de
>= (void *)(bh
->b_data
+ sb
->s_blocksize
)) {
324 bh
= ocfs2_bread(inode
,
325 offset
>> sb
->s_blocksize_bits
, &err
, 0);
327 mlog(ML_ERROR
, "dir %llu has a hole at %lu\n",
328 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, offset
);
329 offset
+= sb
->s_blocksize
;
332 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
334 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
338 if (le64_to_cpu(de
->inode
)) {
342 offset
+= le16_to_cpu(de
->rec_len
);
343 de
= (struct ocfs2_dir_entry
*)
344 ((char *)de
+ le16_to_cpu(de
->rec_len
));
350 /* returns a bh of the 1st new block in the allocation. */
351 int ocfs2_do_extend_dir(struct super_block
*sb
,
354 struct buffer_head
*parent_fe_bh
,
355 struct ocfs2_alloc_context
*data_ac
,
356 struct ocfs2_alloc_context
*meta_ac
,
357 struct buffer_head
**new_bh
)
363 spin_lock(&OCFS2_I(dir
)->ip_lock
);
364 extend
= (i_size_read(dir
) == ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
));
365 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
368 status
= ocfs2_do_extend_allocation(OCFS2_SB(sb
), dir
, 1,
369 parent_fe_bh
, handle
,
370 data_ac
, meta_ac
, NULL
);
371 BUG_ON(status
== -EAGAIN
);
378 status
= ocfs2_extent_map_get_blocks(dir
, (dir
->i_blocks
>>
379 (sb
->s_blocksize_bits
- 9)),
386 *new_bh
= sb_getblk(sb
, p_blkno
);
398 /* assumes you already have a cluster lock on the directory. */
399 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
401 struct buffer_head
*parent_fe_bh
,
402 struct buffer_head
**new_de_bh
)
405 int credits
, num_free_extents
;
407 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
408 struct ocfs2_alloc_context
*data_ac
= NULL
;
409 struct ocfs2_alloc_context
*meta_ac
= NULL
;
410 handle_t
*handle
= NULL
;
411 struct buffer_head
*new_bh
= NULL
;
412 struct ocfs2_dir_entry
* de
;
413 struct super_block
*sb
= osb
->sb
;
417 dir_i_size
= i_size_read(dir
);
418 mlog(0, "extending dir %llu (i_size = %lld)\n",
419 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, dir_i_size
);
421 /* dir->i_size is always block aligned. */
422 spin_lock(&OCFS2_I(dir
)->ip_lock
);
423 if (dir_i_size
== ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
)) {
424 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
425 num_free_extents
= ocfs2_num_free_extents(osb
, dir
, fe
);
426 if (num_free_extents
< 0) {
427 status
= num_free_extents
;
432 if (!num_free_extents
) {
433 status
= ocfs2_reserve_new_metadata(osb
, fe
, &meta_ac
);
435 if (status
!= -ENOSPC
)
441 status
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
443 if (status
!= -ENOSPC
)
448 credits
= ocfs2_calc_extend_credits(sb
, fe
, 1);
450 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
451 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
454 handle
= ocfs2_start_trans(osb
, credits
);
455 if (IS_ERR(handle
)) {
456 status
= PTR_ERR(handle
);
462 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, dir
, parent_fe_bh
,
463 data_ac
, meta_ac
, &new_bh
);
469 ocfs2_set_new_buffer_uptodate(dir
, new_bh
);
471 status
= ocfs2_journal_access(handle
, dir
, new_bh
,
472 OCFS2_JOURNAL_ACCESS_CREATE
);
477 memset(new_bh
->b_data
, 0, sb
->s_blocksize
);
478 de
= (struct ocfs2_dir_entry
*) new_bh
->b_data
;
480 de
->rec_len
= cpu_to_le16(sb
->s_blocksize
);
481 status
= ocfs2_journal_dirty(handle
, new_bh
);
487 dir_i_size
+= dir
->i_sb
->s_blocksize
;
488 i_size_write(dir
, dir_i_size
);
489 dir
->i_blocks
= ocfs2_align_bytes_to_sectors(dir_i_size
);
490 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
500 ocfs2_commit_trans(osb
, handle
);
503 ocfs2_free_alloc_context(data_ac
);
505 ocfs2_free_alloc_context(meta_ac
);
515 * Search the dir for a good spot, extending it if necessary. The
516 * block containing an appropriate record is returned in ret_de_bh.
518 int ocfs2_prepare_dir_for_insert(struct ocfs2_super
*osb
,
520 struct buffer_head
*parent_fe_bh
,
523 struct buffer_head
**ret_de_bh
)
525 unsigned long offset
;
526 struct buffer_head
* bh
= NULL
;
527 unsigned short rec_len
;
528 struct ocfs2_dinode
*fe
;
529 struct ocfs2_dir_entry
*de
;
530 struct super_block
*sb
;
535 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
536 namelen
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
538 BUG_ON(!S_ISDIR(dir
->i_mode
));
539 fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
540 BUG_ON(le64_to_cpu(fe
->i_size
) != i_size_read(dir
));
550 bh
= ocfs2_bread(dir
, 0, &status
, 0);
556 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
558 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
560 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
564 if (i_size_read(dir
) <= offset
) {
565 status
= ocfs2_extend_dir(osb
,
578 bh
= ocfs2_bread(dir
,
579 offset
>> sb
->s_blocksize_bits
,
586 /* move to next block */
587 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
589 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
593 if (ocfs2_match(namelen
, name
, de
)) {
597 if (((le64_to_cpu(de
->inode
) == 0) &&
598 (le16_to_cpu(de
->rec_len
) >= rec_len
)) ||
599 (le16_to_cpu(de
->rec_len
) >=
600 (OCFS2_DIR_REC_LEN(de
->name_len
) + rec_len
))) {
601 /* Ok, we found a spot. Return this bh and let
602 * the caller actually fill it in. */
608 offset
+= le16_to_cpu(de
->rec_len
);
609 de
= (struct ocfs2_dir_entry
*)((char *) de
+ le16_to_cpu(de
->rec_len
));