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;
86 mlog_entry("dirino=%llu\n",
87 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
92 error
= ocfs2_meta_lock(inode
, NULL
, NULL
, 0);
96 /* we haven't got any yet, so propagate the error. */
101 offset
= filp
->f_pos
& (sb
->s_blocksize
- 1);
103 while (!error
&& !stored
&& filp
->f_pos
< i_size_read(inode
)) {
104 blk
= (filp
->f_pos
) >> sb
->s_blocksize_bits
;
105 bh
= ocfs2_bread(inode
, blk
, &err
, 0);
108 "directory #%llu contains a hole at offset %lld\n",
109 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
111 filp
->f_pos
+= sb
->s_blocksize
- offset
;
115 /* The idea here is to begin with 8k read-ahead and to stay
116 * 4k ahead of our current position.
118 * TODO: Use the pagecache for this. We just need to
119 * make sure it's cluster-safe... */
121 || (((last_ra_blk
- blk
) << 9) <= (ra_sectors
/ 2))) {
122 for (i
= ra_sectors
>> (sb
->s_blocksize_bits
- 9);
124 tmp
= ocfs2_bread(inode
, ++blk
, &err
, 1);
133 /* If the dir block has changed since the last call to
134 * readdir(2), then we might be pointing to an invalid
135 * dirent right now. Scan from the start of the block
137 if (filp
->f_version
!= inode
->i_version
) {
138 for (i
= 0; i
< sb
->s_blocksize
&& i
< offset
; ) {
139 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ i
);
140 /* It's too expensive to do a full
141 * dirent test each time round this
142 * loop, but we do have to test at
143 * least that it is non-zero. A
144 * failure will be detected in the
145 * dirent test below. */
146 if (le16_to_cpu(de
->rec_len
) <
147 OCFS2_DIR_REC_LEN(1))
149 i
+= le16_to_cpu(de
->rec_len
);
152 filp
->f_pos
= (filp
->f_pos
& ~(sb
->s_blocksize
- 1))
154 filp
->f_version
= inode
->i_version
;
157 while (!error
&& filp
->f_pos
< i_size_read(inode
)
158 && offset
< sb
->s_blocksize
) {
159 de
= (struct ocfs2_dir_entry
*) (bh
->b_data
+ offset
);
160 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
161 /* On error, skip the f_pos to the
163 filp
->f_pos
= (filp
->f_pos
|
164 (sb
->s_blocksize
- 1)) + 1;
168 offset
+= le16_to_cpu(de
->rec_len
);
169 if (le64_to_cpu(de
->inode
)) {
170 /* We might block in the next section
171 * if the data destination is
172 * currently swapped out. So, use a
173 * version stamp to detect whether or
174 * not the directory has been modified
175 * during the copy operation.
177 unsigned long version
= filp
->f_version
;
178 unsigned char d_type
= DT_UNKNOWN
;
180 if (de
->file_type
< OCFS2_FT_MAX
)
181 d_type
= ocfs2_filetype_table
[de
->file_type
];
182 error
= filldir(dirent
, de
->name
,
185 ino_from_blkno(sb
, le64_to_cpu(de
->inode
)),
189 if (version
!= filp
->f_version
)
193 filp
->f_pos
+= le16_to_cpu(de
->rec_len
);
201 ocfs2_meta_unlock(inode
, 0);
210 * NOTE: this should always be called with parent dir i_mutex taken.
212 int ocfs2_find_files_on_disk(const char *name
,
216 struct buffer_head
**dirent_bh
,
217 struct ocfs2_dir_entry
**dirent
)
219 int status
= -ENOENT
;
221 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
222 namelen
, name
, blkno
, inode
, dirent_bh
, dirent
);
224 *dirent_bh
= ocfs2_find_entry(name
, namelen
, inode
, dirent
);
225 if (!*dirent_bh
|| !*dirent
) {
230 *blkno
= le64_to_cpu((*dirent
)->inode
);
246 /* Check for a name within a directory.
248 * Return 0 if the name does not exist
249 * Return -EEXIST if the directory contains the name
251 * Callers should have i_mutex + a cluster lock on dir
253 int ocfs2_check_dir_for_entry(struct inode
*dir
,
258 struct buffer_head
*dirent_bh
= NULL
;
259 struct ocfs2_dir_entry
*dirent
= NULL
;
261 mlog_entry("dir %llu, name '%.*s'\n",
262 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, namelen
, name
);
265 dirent_bh
= ocfs2_find_entry(name
, namelen
, dir
, &dirent
);
279 * routine to check that the specified directory is empty (for rmdir)
281 int ocfs2_empty_dir(struct inode
*inode
)
283 unsigned long offset
;
284 struct buffer_head
* bh
;
285 struct ocfs2_dir_entry
* de
, * de1
;
286 struct super_block
* sb
;
290 if ((i_size_read(inode
) <
291 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
292 !(bh
= ocfs2_bread(inode
, 0, &err
, 0))) {
293 mlog(ML_ERROR
, "bad directory (dir #%llu) - no data block\n",
294 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
298 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
299 de1
= (struct ocfs2_dir_entry
*)
300 ((char *)de
+ le16_to_cpu(de
->rec_len
));
301 if ((le64_to_cpu(de
->inode
) != OCFS2_I(inode
)->ip_blkno
) ||
302 !le64_to_cpu(de1
->inode
) ||
303 strcmp(".", de
->name
) ||
304 strcmp("..", de1
->name
)) {
305 mlog(ML_ERROR
, "bad directory (dir #%llu) - no `.' or `..'\n",
306 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
310 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
311 de
= (struct ocfs2_dir_entry
*)((char *)de1
+ le16_to_cpu(de1
->rec_len
));
312 while (offset
< i_size_read(inode
) ) {
313 if (!bh
|| (void *)de
>= (void *)(bh
->b_data
+ sb
->s_blocksize
)) {
315 bh
= ocfs2_bread(inode
,
316 offset
>> sb
->s_blocksize_bits
, &err
, 0);
318 mlog(ML_ERROR
, "dir %llu has a hole at %lu\n",
319 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, offset
);
320 offset
+= sb
->s_blocksize
;
323 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
325 if (!ocfs2_check_dir_entry(inode
, de
, bh
, offset
)) {
329 if (le64_to_cpu(de
->inode
)) {
333 offset
+= le16_to_cpu(de
->rec_len
);
334 de
= (struct ocfs2_dir_entry
*)
335 ((char *)de
+ le16_to_cpu(de
->rec_len
));
341 /* returns a bh of the 1st new block in the allocation. */
342 int ocfs2_do_extend_dir(struct super_block
*sb
,
343 struct ocfs2_journal_handle
*handle
,
345 struct buffer_head
*parent_fe_bh
,
346 struct ocfs2_alloc_context
*data_ac
,
347 struct ocfs2_alloc_context
*meta_ac
,
348 struct buffer_head
**new_bh
)
354 spin_lock(&OCFS2_I(dir
)->ip_lock
);
355 extend
= (i_size_read(dir
) == ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
));
356 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
359 status
= ocfs2_do_extend_allocation(OCFS2_SB(sb
), dir
, 1,
360 parent_fe_bh
, handle
,
361 data_ac
, meta_ac
, NULL
);
362 BUG_ON(status
== -EAGAIN
);
369 status
= ocfs2_extent_map_get_blocks(dir
, (dir
->i_blocks
>>
370 (sb
->s_blocksize_bits
- 9)),
377 *new_bh
= sb_getblk(sb
, p_blkno
);
389 /* assumes you already have a cluster lock on the directory. */
390 static int ocfs2_extend_dir(struct ocfs2_super
*osb
,
392 struct buffer_head
*parent_fe_bh
,
393 struct buffer_head
**new_de_bh
)
396 int credits
, num_free_extents
;
398 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
399 struct ocfs2_alloc_context
*data_ac
= NULL
;
400 struct ocfs2_alloc_context
*meta_ac
= NULL
;
401 struct ocfs2_journal_handle
*handle
= NULL
;
402 struct buffer_head
*new_bh
= NULL
;
403 struct ocfs2_dir_entry
* de
;
404 struct super_block
*sb
= osb
->sb
;
408 dir_i_size
= i_size_read(dir
);
409 mlog(0, "extending dir %llu (i_size = %lld)\n",
410 (unsigned long long)OCFS2_I(dir
)->ip_blkno
, dir_i_size
);
412 handle
= ocfs2_alloc_handle(osb
);
413 if (handle
== NULL
) {
419 /* dir->i_size is always block aligned. */
420 spin_lock(&OCFS2_I(dir
)->ip_lock
);
421 if (dir_i_size
== ocfs2_clusters_to_bytes(sb
, OCFS2_I(dir
)->ip_clusters
)) {
422 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
423 num_free_extents
= ocfs2_num_free_extents(osb
, dir
, fe
);
424 if (num_free_extents
< 0) {
425 status
= num_free_extents
;
430 if (!num_free_extents
) {
431 status
= ocfs2_reserve_new_metadata(osb
, handle
,
434 if (status
!= -ENOSPC
)
440 status
= ocfs2_reserve_clusters(osb
, handle
, 1, &data_ac
);
442 if (status
!= -ENOSPC
)
447 credits
= ocfs2_calc_extend_credits(sb
, fe
, 1);
449 spin_unlock(&OCFS2_I(dir
)->ip_lock
);
450 credits
= OCFS2_SIMPLE_DIR_EXTEND_CREDITS
;
453 handle
= ocfs2_start_trans(osb
, handle
, credits
);
454 if (IS_ERR(handle
)) {
455 status
= PTR_ERR(handle
);
461 status
= ocfs2_do_extend_dir(osb
->sb
, handle
, dir
, parent_fe_bh
,
462 data_ac
, meta_ac
, &new_bh
);
468 ocfs2_set_new_buffer_uptodate(dir
, new_bh
);
470 status
= ocfs2_journal_access(handle
, dir
, new_bh
,
471 OCFS2_JOURNAL_ACCESS_CREATE
);
476 memset(new_bh
->b_data
, 0, sb
->s_blocksize
);
477 de
= (struct ocfs2_dir_entry
*) new_bh
->b_data
;
479 de
->rec_len
= cpu_to_le16(sb
->s_blocksize
);
480 status
= ocfs2_journal_dirty(handle
, new_bh
);
486 dir_i_size
+= dir
->i_sb
->s_blocksize
;
487 i_size_write(dir
, dir_i_size
);
488 dir
->i_blocks
= ocfs2_align_bytes_to_sectors(dir_i_size
);
489 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_fe_bh
);
499 ocfs2_commit_trans(handle
);
502 ocfs2_free_alloc_context(data_ac
);
504 ocfs2_free_alloc_context(meta_ac
);
514 * Search the dir for a good spot, extending it if necessary. The
515 * block containing an appropriate record is returned in ret_de_bh.
517 int ocfs2_prepare_dir_for_insert(struct ocfs2_super
*osb
,
519 struct buffer_head
*parent_fe_bh
,
522 struct buffer_head
**ret_de_bh
)
524 unsigned long offset
;
525 struct buffer_head
* bh
= NULL
;
526 unsigned short rec_len
;
527 struct ocfs2_dinode
*fe
;
528 struct ocfs2_dir_entry
*de
;
529 struct super_block
*sb
;
534 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
535 namelen
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
537 BUG_ON(!S_ISDIR(dir
->i_mode
));
538 fe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
539 BUG_ON(le64_to_cpu(fe
->i_size
) != i_size_read(dir
));
549 bh
= ocfs2_bread(dir
, 0, &status
, 0);
555 rec_len
= OCFS2_DIR_REC_LEN(namelen
);
557 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
559 if ((char *)de
>= sb
->s_blocksize
+ bh
->b_data
) {
563 if (i_size_read(dir
) <= offset
) {
564 status
= ocfs2_extend_dir(osb
,
577 bh
= ocfs2_bread(dir
,
578 offset
>> sb
->s_blocksize_bits
,
585 /* move to next block */
586 de
= (struct ocfs2_dir_entry
*) bh
->b_data
;
588 if (!ocfs2_check_dir_entry(dir
, de
, bh
, offset
)) {
592 if (ocfs2_match(namelen
, name
, de
)) {
596 if (((le64_to_cpu(de
->inode
) == 0) &&
597 (le16_to_cpu(de
->rec_len
) >= rec_len
)) ||
598 (le16_to_cpu(de
->rec_len
) >=
599 (OCFS2_DIR_REC_LEN(de
->name_len
) + rec_len
))) {
600 /* Ok, we found a spot. Return this bh and let
601 * the caller actually fill it in. */
607 offset
+= le16_to_cpu(de
->rec_len
);
608 de
= (struct ocfs2_dir_entry
*)((char *) de
+ le16_to_cpu(de
->rec_len
));