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;
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 int have_disk_lock = 0;
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. */
102 offset = filp->f_pos & (sb->s_blocksize - 1);
104 while (!error && !stored && filp->f_pos < i_size_read(inode)) {
105 blk = (filp->f_pos) >> sb->s_blocksize_bits;
106 bh = ocfs2_bread(inode, blk, &err, 0);
109 "directory #%llu contains a hole at offset %lld\n",
110 (unsigned long long)OCFS2_I(inode)->ip_blkno,
112 filp->f_pos += sb->s_blocksize - offset;
117 * Do the readahead (8k)
120 for (i = 16 >> (sb->s_blocksize_bits - 9), num = 0;
122 tmp = ocfs2_bread(inode, ++blk, &err, 1);
129 /* If the dir block has changed since the last call to
130 * readdir(2), then we might be pointing to an invalid
131 * dirent right now. Scan from the start of the block
133 if (filp->f_version != inode->i_version) {
134 for (i = 0; i < sb->s_blocksize && i < offset; ) {
135 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
136 /* It's too expensive to do a full
137 * dirent test each time round this
138 * loop, but we do have to test at
139 * least that it is non-zero. A
140 * failure will be detected in the
141 * dirent test below. */
142 if (le16_to_cpu(de->rec_len) <
143 OCFS2_DIR_REC_LEN(1))
145 i += le16_to_cpu(de->rec_len);
148 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
150 filp->f_version = inode->i_version;
153 while (!error && filp->f_pos < i_size_read(inode)
154 && offset < sb->s_blocksize) {
155 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
156 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
157 /* On error, skip the f_pos to the
159 filp->f_pos = (filp->f_pos |
160 (sb->s_blocksize - 1)) + 1;
164 offset += le16_to_cpu(de->rec_len);
165 if (le64_to_cpu(de->inode)) {
166 /* We might block in the next section
167 * if the data destination is
168 * currently swapped out. So, use a
169 * version stamp to detect whether or
170 * not the directory has been modified
171 * during the copy operation.
173 unsigned long version = filp->f_version;
174 unsigned char d_type = DT_UNKNOWN;
176 if (de->file_type < OCFS2_FT_MAX)
177 d_type = ocfs2_filetype_table[de->file_type];
178 error = filldir(dirent, de->name,
181 ino_from_blkno(sb, le64_to_cpu(de->inode)),
185 if (version != filp->f_version)
189 filp->f_pos += le16_to_cpu(de->rec_len);
198 ocfs2_meta_unlock(inode, 0);
206 * NOTE: this should always be called with parent dir i_mutex taken.
208 int ocfs2_find_files_on_disk(const char *name,
212 struct buffer_head **dirent_bh,
213 struct ocfs2_dir_entry **dirent)
215 int status = -ENOENT;
216 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
218 mlog_entry("(osb=%p, parent=%llu, name='%.*s', blkno=%p, inode=%p)\n",
219 osb, (unsigned long long)OCFS2_I(inode)->ip_blkno,
220 namelen, name, blkno, inode);
222 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
223 if (!*dirent_bh || !*dirent) {
228 *blkno = le64_to_cpu((*dirent)->inode);
244 /* Check for a name within a directory.
246 * Return 0 if the name does not exist
247 * Return -EEXIST if the directory contains the name
249 * Callers should have i_mutex + a cluster lock on dir
251 int ocfs2_check_dir_for_entry(struct inode *dir,
256 struct buffer_head *dirent_bh = NULL;
257 struct ocfs2_dir_entry *dirent = NULL;
259 mlog_entry("dir %llu, name '%.*s'\n",
260 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
263 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
277 * routine to check that the specified directory is empty (for rmdir)
279 int ocfs2_empty_dir(struct inode *inode)
281 unsigned long offset;
282 struct buffer_head * bh;
283 struct ocfs2_dir_entry * de, * de1;
284 struct super_block * sb;
288 if ((i_size_read(inode) <
289 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
290 !(bh = ocfs2_bread(inode, 0, &err, 0))) {
291 mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
292 (unsigned long long)OCFS2_I(inode)->ip_blkno);
296 de = (struct ocfs2_dir_entry *) bh->b_data;
297 de1 = (struct ocfs2_dir_entry *)
298 ((char *)de + le16_to_cpu(de->rec_len));
299 if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
300 !le64_to_cpu(de1->inode) ||
301 strcmp(".", de->name) ||
302 strcmp("..", de1->name)) {
303 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
304 (unsigned long long)OCFS2_I(inode)->ip_blkno);
308 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
309 de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
310 while (offset < i_size_read(inode) ) {
311 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
313 bh = ocfs2_bread(inode,
314 offset >> sb->s_blocksize_bits, &err, 0);
316 mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
317 (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
318 offset += sb->s_blocksize;
321 de = (struct ocfs2_dir_entry *) bh->b_data;
323 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
327 if (le64_to_cpu(de->inode)) {
331 offset += le16_to_cpu(de->rec_len);
332 de = (struct ocfs2_dir_entry *)
333 ((char *)de + le16_to_cpu(de->rec_len));
339 /* returns a bh of the 1st new block in the allocation. */
340 int ocfs2_do_extend_dir(struct super_block *sb,
341 struct ocfs2_journal_handle *handle,
343 struct buffer_head *parent_fe_bh,
344 struct ocfs2_alloc_context *data_ac,
345 struct ocfs2_alloc_context *meta_ac,
346 struct buffer_head **new_bh)
352 spin_lock(&OCFS2_I(dir)->ip_lock);
353 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
354 spin_unlock(&OCFS2_I(dir)->ip_lock);
357 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
358 parent_fe_bh, handle,
359 data_ac, meta_ac, NULL);
360 BUG_ON(status == -EAGAIN);
367 status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
368 (sb->s_blocksize_bits - 9)),
375 *new_bh = sb_getblk(sb, p_blkno);
387 /* assumes you already have a cluster lock on the directory. */
388 static int ocfs2_extend_dir(struct ocfs2_super *osb,
390 struct buffer_head *parent_fe_bh,
391 struct buffer_head **new_de_bh)
394 int credits, num_free_extents;
396 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
397 struct ocfs2_alloc_context *data_ac = NULL;
398 struct ocfs2_alloc_context *meta_ac = NULL;
399 struct ocfs2_journal_handle *handle = NULL;
400 struct buffer_head *new_bh = NULL;
401 struct ocfs2_dir_entry * de;
402 struct super_block *sb = osb->sb;
406 dir_i_size = i_size_read(dir);
407 mlog(0, "extending dir %llu (i_size = %lld)\n",
408 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
410 handle = ocfs2_alloc_handle(osb);
411 if (handle == NULL) {
417 /* dir->i_size is always block aligned. */
418 spin_lock(&OCFS2_I(dir)->ip_lock);
419 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
420 spin_unlock(&OCFS2_I(dir)->ip_lock);
421 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
422 if (num_free_extents < 0) {
423 status = num_free_extents;
428 if (!num_free_extents) {
429 status = ocfs2_reserve_new_metadata(osb, handle,
432 if (status != -ENOSPC)
438 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
440 if (status != -ENOSPC)
445 credits = ocfs2_calc_extend_credits(sb, fe, 1);
447 spin_unlock(&OCFS2_I(dir)->ip_lock);
448 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
451 handle = ocfs2_start_trans(osb, handle, credits);
452 if (IS_ERR(handle)) {
453 status = PTR_ERR(handle);
459 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
460 data_ac, meta_ac, &new_bh);
466 ocfs2_set_new_buffer_uptodate(dir, new_bh);
468 status = ocfs2_journal_access(handle, dir, new_bh,
469 OCFS2_JOURNAL_ACCESS_CREATE);
474 memset(new_bh->b_data, 0, sb->s_blocksize);
475 de = (struct ocfs2_dir_entry *) new_bh->b_data;
477 de->rec_len = cpu_to_le16(sb->s_blocksize);
478 status = ocfs2_journal_dirty(handle, new_bh);
484 dir_i_size += dir->i_sb->s_blocksize;
485 i_size_write(dir, dir_i_size);
486 dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
487 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
497 ocfs2_commit_trans(handle);
500 ocfs2_free_alloc_context(data_ac);
502 ocfs2_free_alloc_context(meta_ac);
512 * Search the dir for a good spot, extending it if necessary. The
513 * block containing an appropriate record is returned in ret_de_bh.
515 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
517 struct buffer_head *parent_fe_bh,
520 struct buffer_head **ret_de_bh)
522 unsigned long offset;
523 struct buffer_head * bh = NULL;
524 unsigned short rec_len;
525 struct ocfs2_dinode *fe;
526 struct ocfs2_dir_entry *de;
527 struct super_block *sb;
532 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
533 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
535 BUG_ON(!S_ISDIR(dir->i_mode));
536 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
537 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
547 bh = ocfs2_bread(dir, 0, &status, 0);
553 rec_len = OCFS2_DIR_REC_LEN(namelen);
555 de = (struct ocfs2_dir_entry *) bh->b_data;
557 if ((char *)de >= sb->s_blocksize + bh->b_data) {
561 if (i_size_read(dir) <= offset) {
562 status = ocfs2_extend_dir(osb,
575 bh = ocfs2_bread(dir,
576 offset >> sb->s_blocksize_bits,
583 /* move to next block */
584 de = (struct ocfs2_dir_entry *) bh->b_data;
586 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
590 if (ocfs2_match(namelen, name, de)) {
594 if (((le64_to_cpu(de->inode) == 0) &&
595 (le16_to_cpu(de->rec_len) >= rec_len)) ||
596 (le16_to_cpu(de->rec_len) >=
597 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
598 /* Ok, we found a spot. Return this bh and let
599 * the caller actually fill it in. */
605 offset += le16_to_cpu(de->rec_len);
606 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));