blob: 3d494d1a5f36360bd2f90983c05fff5b9b2f88a5 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dir.c
5 *
6 * Creates, reads, walks and deletes directory-nodes
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * Portions of this code from linux/fs/ext3/dir.c
11 *
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)
16 *
17 * from
18 *
19 * linux/fs/minix/dir.c
20 *
21 * Copyright (C) 1991, 1992 Linux Torvalds
22 *
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.
27 *
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.
32 *
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.
37 */
38
39#include <linux/fs.h>
40#include <linux/types.h>
41#include <linux/slab.h>
42#include <linux/highmem.h>
43
44#define MLOG_MASK_PREFIX ML_NAMEI
45#include <cluster/masklog.h>
46
47#include "ocfs2.h"
48
49#include "alloc.h"
50#include "dir.h"
51#include "dlmglue.h"
52#include "extent_map.h"
53#include "file.h"
54#include "inode.h"
55#include "journal.h"
56#include "namei.h"
57#include "suballoc.h"
58#include "uptodate.h"
59
60#include "buffer_head_io.h"
61
62static unsigned char ocfs2_filetype_table[] = {
63 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
64};
65
66static int ocfs2_extend_dir(struct ocfs2_super *osb,
67 struct inode *dir,
68 struct buffer_head *parent_fe_bh,
69 struct buffer_head **new_de_bh);
70/*
71 * ocfs2_readdir()
72 *
73 */
74int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
75{
76 int error = 0;
77 unsigned long offset, blk;
78 int i, num, stored;
79 struct buffer_head * bh, * tmp;
80 struct ocfs2_dir_entry * de;
81 int err;
82 struct inode *inode = filp->f_dentry->d_inode;
83 struct super_block * sb = inode->i_sb;
84 int have_disk_lock = 0;
85
Mark Fashehb0697052006-03-03 10:24:33 -080086 mlog_entry("dirino=%llu\n",
87 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -080088
89 stored = 0;
90 bh = NULL;
91
92 error = ocfs2_meta_lock(inode, NULL, NULL, 0);
93 if (error < 0) {
94 if (error != -ENOENT)
95 mlog_errno(error);
96 /* we haven't got any yet, so propagate the error. */
97 stored = error;
98 goto bail;
99 }
100 have_disk_lock = 1;
101
102 offset = filp->f_pos & (sb->s_blocksize - 1);
103
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);
107 if (!bh) {
Mark Fashehb0697052006-03-03 10:24:33 -0800108 mlog(ML_ERROR,
109 "directory #%llu contains a hole at offset %lld\n",
110 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800111 filp->f_pos);
112 filp->f_pos += sb->s_blocksize - offset;
113 continue;
114 }
115
116 /*
117 * Do the readahead (8k)
118 */
119 if (!offset) {
120 for (i = 16 >> (sb->s_blocksize_bits - 9), num = 0;
121 i > 0; i--) {
122 tmp = ocfs2_bread(inode, ++blk, &err, 1);
123 if (tmp)
124 brelse(tmp);
125 }
126 }
127
128revalidate:
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
132 * to make sure. */
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))
144 break;
145 i += le16_to_cpu(de->rec_len);
146 }
147 offset = i;
148 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
149 | offset;
150 filp->f_version = inode->i_version;
151 }
152
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
158 next block. */
159 filp->f_pos = (filp->f_pos |
160 (sb->s_blocksize - 1)) + 1;
161 brelse(bh);
162 goto bail;
163 }
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.
172 */
173 unsigned long version = filp->f_version;
174 unsigned char d_type = DT_UNKNOWN;
175
176 if (de->file_type < OCFS2_FT_MAX)
177 d_type = ocfs2_filetype_table[de->file_type];
178 error = filldir(dirent, de->name,
179 de->name_len,
180 filp->f_pos,
181 ino_from_blkno(sb, le64_to_cpu(de->inode)),
182 d_type);
183 if (error)
184 break;
185 if (version != filp->f_version)
186 goto revalidate;
187 stored ++;
188 }
189 filp->f_pos += le16_to_cpu(de->rec_len);
190 }
191 offset = 0;
192 brelse(bh);
193 }
194
195 stored = 0;
196bail:
197 if (have_disk_lock)
198 ocfs2_meta_unlock(inode, 0);
199
200 mlog_exit(stored);
201
202 return stored;
203}
204
205/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800206 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800207 */
208int ocfs2_find_files_on_disk(const char *name,
209 int namelen,
210 u64 *blkno,
211 struct inode *inode,
212 struct buffer_head **dirent_bh,
213 struct ocfs2_dir_entry **dirent)
214{
215 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800216
Joel Becker2b388c62006-05-10 18:28:59 -0700217 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
218 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800219
220 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
221 if (!*dirent_bh || !*dirent) {
222 status = -ENOENT;
223 goto leave;
224 }
225
226 *blkno = le64_to_cpu((*dirent)->inode);
227
228 status = 0;
229leave:
230 if (status < 0) {
231 *dirent = NULL;
232 if (*dirent_bh) {
233 brelse(*dirent_bh);
234 *dirent_bh = NULL;
235 }
236 }
237
238 mlog_exit(status);
239 return status;
240}
241
242/* Check for a name within a directory.
243 *
244 * Return 0 if the name does not exist
245 * Return -EEXIST if the directory contains the name
246 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800247 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800248 */
249int ocfs2_check_dir_for_entry(struct inode *dir,
250 const char *name,
251 int namelen)
252{
253 int ret;
254 struct buffer_head *dirent_bh = NULL;
255 struct ocfs2_dir_entry *dirent = NULL;
256
Mark Fashehb0697052006-03-03 10:24:33 -0800257 mlog_entry("dir %llu, name '%.*s'\n",
258 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800259
260 ret = -EEXIST;
261 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
262 if (dirent_bh)
263 goto bail;
264
265 ret = 0;
266bail:
267 if (dirent_bh)
268 brelse(dirent_bh);
269
270 mlog_exit(ret);
271 return ret;
272}
273
274/*
275 * routine to check that the specified directory is empty (for rmdir)
276 */
277int ocfs2_empty_dir(struct inode *inode)
278{
279 unsigned long offset;
280 struct buffer_head * bh;
281 struct ocfs2_dir_entry * de, * de1;
282 struct super_block * sb;
283 int err;
284
285 sb = inode->i_sb;
286 if ((i_size_read(inode) <
287 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
288 !(bh = ocfs2_bread(inode, 0, &err, 0))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800289 mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
290 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800291 return 1;
292 }
293
294 de = (struct ocfs2_dir_entry *) bh->b_data;
295 de1 = (struct ocfs2_dir_entry *)
296 ((char *)de + le16_to_cpu(de->rec_len));
297 if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
298 !le64_to_cpu(de1->inode) ||
299 strcmp(".", de->name) ||
300 strcmp("..", de1->name)) {
Mark Fashehb0697052006-03-03 10:24:33 -0800301 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
302 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800303 brelse(bh);
304 return 1;
305 }
306 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
307 de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
308 while (offset < i_size_read(inode) ) {
309 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
310 brelse(bh);
311 bh = ocfs2_bread(inode,
312 offset >> sb->s_blocksize_bits, &err, 0);
313 if (!bh) {
Mark Fashehb0697052006-03-03 10:24:33 -0800314 mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
315 (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800316 offset += sb->s_blocksize;
317 continue;
318 }
319 de = (struct ocfs2_dir_entry *) bh->b_data;
320 }
321 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
322 brelse(bh);
323 return 1;
324 }
325 if (le64_to_cpu(de->inode)) {
326 brelse(bh);
327 return 0;
328 }
329 offset += le16_to_cpu(de->rec_len);
330 de = (struct ocfs2_dir_entry *)
331 ((char *)de + le16_to_cpu(de->rec_len));
332 }
333 brelse(bh);
334 return 1;
335}
336
337/* returns a bh of the 1st new block in the allocation. */
338int ocfs2_do_extend_dir(struct super_block *sb,
339 struct ocfs2_journal_handle *handle,
340 struct inode *dir,
341 struct buffer_head *parent_fe_bh,
342 struct ocfs2_alloc_context *data_ac,
343 struct ocfs2_alloc_context *meta_ac,
344 struct buffer_head **new_bh)
345{
346 int status;
347 int extend;
348 u64 p_blkno;
349
350 spin_lock(&OCFS2_I(dir)->ip_lock);
351 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
352 spin_unlock(&OCFS2_I(dir)->ip_lock);
353
354 if (extend) {
355 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
356 parent_fe_bh, handle,
357 data_ac, meta_ac, NULL);
358 BUG_ON(status == -EAGAIN);
359 if (status < 0) {
360 mlog_errno(status);
361 goto bail;
362 }
363 }
364
365 status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
366 (sb->s_blocksize_bits - 9)),
367 1, &p_blkno, NULL);
368 if (status < 0) {
369 mlog_errno(status);
370 goto bail;
371 }
372
373 *new_bh = sb_getblk(sb, p_blkno);
374 if (!*new_bh) {
375 status = -EIO;
376 mlog_errno(status);
377 goto bail;
378 }
379 status = 0;
380bail:
381 mlog_exit(status);
382 return status;
383}
384
385/* assumes you already have a cluster lock on the directory. */
386static int ocfs2_extend_dir(struct ocfs2_super *osb,
387 struct inode *dir,
388 struct buffer_head *parent_fe_bh,
389 struct buffer_head **new_de_bh)
390{
391 int status = 0;
392 int credits, num_free_extents;
393 loff_t dir_i_size;
394 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
395 struct ocfs2_alloc_context *data_ac = NULL;
396 struct ocfs2_alloc_context *meta_ac = NULL;
397 struct ocfs2_journal_handle *handle = NULL;
398 struct buffer_head *new_bh = NULL;
399 struct ocfs2_dir_entry * de;
400 struct super_block *sb = osb->sb;
401
402 mlog_entry_void();
403
404 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -0800405 mlog(0, "extending dir %llu (i_size = %lld)\n",
406 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800407
408 handle = ocfs2_alloc_handle(osb);
409 if (handle == NULL) {
410 status = -ENOMEM;
411 mlog_errno(status);
412 goto bail;
413 }
414
415 /* dir->i_size is always block aligned. */
416 spin_lock(&OCFS2_I(dir)->ip_lock);
417 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
418 spin_unlock(&OCFS2_I(dir)->ip_lock);
419 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
420 if (num_free_extents < 0) {
421 status = num_free_extents;
422 mlog_errno(status);
423 goto bail;
424 }
425
426 if (!num_free_extents) {
427 status = ocfs2_reserve_new_metadata(osb, handle,
428 fe, &meta_ac);
429 if (status < 0) {
430 if (status != -ENOSPC)
431 mlog_errno(status);
432 goto bail;
433 }
434 }
435
436 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
437 if (status < 0) {
438 if (status != -ENOSPC)
439 mlog_errno(status);
440 goto bail;
441 }
442
443 credits = ocfs2_calc_extend_credits(sb, fe, 1);
444 } else {
445 spin_unlock(&OCFS2_I(dir)->ip_lock);
446 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
447 }
448
449 handle = ocfs2_start_trans(osb, handle, credits);
450 if (IS_ERR(handle)) {
451 status = PTR_ERR(handle);
452 handle = NULL;
453 mlog_errno(status);
454 goto bail;
455 }
456
457 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
458 data_ac, meta_ac, &new_bh);
459 if (status < 0) {
460 mlog_errno(status);
461 goto bail;
462 }
463
464 ocfs2_set_new_buffer_uptodate(dir, new_bh);
465
466 status = ocfs2_journal_access(handle, dir, new_bh,
467 OCFS2_JOURNAL_ACCESS_CREATE);
468 if (status < 0) {
469 mlog_errno(status);
470 goto bail;
471 }
472 memset(new_bh->b_data, 0, sb->s_blocksize);
473 de = (struct ocfs2_dir_entry *) new_bh->b_data;
474 de->inode = 0;
475 de->rec_len = cpu_to_le16(sb->s_blocksize);
476 status = ocfs2_journal_dirty(handle, new_bh);
477 if (status < 0) {
478 mlog_errno(status);
479 goto bail;
480 }
481
482 dir_i_size += dir->i_sb->s_blocksize;
483 i_size_write(dir, dir_i_size);
484 dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
485 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
486 if (status < 0) {
487 mlog_errno(status);
488 goto bail;
489 }
490
491 *new_de_bh = new_bh;
492 get_bh(*new_de_bh);
493bail:
494 if (handle)
495 ocfs2_commit_trans(handle);
496
497 if (data_ac)
498 ocfs2_free_alloc_context(data_ac);
499 if (meta_ac)
500 ocfs2_free_alloc_context(meta_ac);
501
502 if (new_bh)
503 brelse(new_bh);
504
505 mlog_exit(status);
506 return status;
507}
508
509/*
510 * Search the dir for a good spot, extending it if necessary. The
511 * block containing an appropriate record is returned in ret_de_bh.
512 */
513int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
514 struct inode *dir,
515 struct buffer_head *parent_fe_bh,
516 const char *name,
517 int namelen,
518 struct buffer_head **ret_de_bh)
519{
520 unsigned long offset;
521 struct buffer_head * bh = NULL;
522 unsigned short rec_len;
523 struct ocfs2_dinode *fe;
524 struct ocfs2_dir_entry *de;
525 struct super_block *sb;
526 int status;
527
528 mlog_entry_void();
529
Mark Fashehb0697052006-03-03 10:24:33 -0800530 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
531 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532
533 BUG_ON(!S_ISDIR(dir->i_mode));
534 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
535 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
536
537 sb = dir->i_sb;
538
539 if (!namelen) {
540 status = -EINVAL;
541 mlog_errno(status);
542 goto bail;
543 }
544
545 bh = ocfs2_bread(dir, 0, &status, 0);
546 if (!bh) {
547 mlog_errno(status);
548 goto bail;
549 }
550
551 rec_len = OCFS2_DIR_REC_LEN(namelen);
552 offset = 0;
553 de = (struct ocfs2_dir_entry *) bh->b_data;
554 while (1) {
555 if ((char *)de >= sb->s_blocksize + bh->b_data) {
556 brelse(bh);
557 bh = NULL;
558
559 if (i_size_read(dir) <= offset) {
560 status = ocfs2_extend_dir(osb,
561 dir,
562 parent_fe_bh,
563 &bh);
564 if (status < 0) {
565 mlog_errno(status);
566 goto bail;
567 }
568 BUG_ON(!bh);
569 *ret_de_bh = bh;
570 get_bh(*ret_de_bh);
571 goto bail;
572 }
573 bh = ocfs2_bread(dir,
574 offset >> sb->s_blocksize_bits,
575 &status,
576 0);
577 if (!bh) {
578 mlog_errno(status);
579 goto bail;
580 }
581 /* move to next block */
582 de = (struct ocfs2_dir_entry *) bh->b_data;
583 }
584 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
585 status = -ENOENT;
586 goto bail;
587 }
588 if (ocfs2_match(namelen, name, de)) {
589 status = -EEXIST;
590 goto bail;
591 }
592 if (((le64_to_cpu(de->inode) == 0) &&
593 (le16_to_cpu(de->rec_len) >= rec_len)) ||
594 (le16_to_cpu(de->rec_len) >=
595 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
596 /* Ok, we found a spot. Return this bh and let
597 * the caller actually fill it in. */
598 *ret_de_bh = bh;
599 get_bh(*ret_de_bh);
600 status = 0;
601 goto bail;
602 }
603 offset += le16_to_cpu(de->rec_len);
604 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
605 }
606
607 status = 0;
608bail:
609 if (bh)
610 brelse(bh);
611
612 mlog_exit(status);
613 return status;
614}