blob: 31db7e3881b19b372cc748bbfa13c5440aa3c893 [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"
Mark Fasheh316f4b92007-09-07 18:21:26 -070058#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080059#include "uptodate.h"
60
61#include "buffer_head_io.h"
62
Mark Fasheh316f4b92007-09-07 18:21:26 -070063#define NAMEI_RA_CHUNKS 2
64#define NAMEI_RA_BLOCKS 4
65#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
66#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
67
Mark Fashehccd979b2005-12-15 14:31:24 -080068static unsigned char ocfs2_filetype_table[] = {
69 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
70};
71
72static int ocfs2_extend_dir(struct ocfs2_super *osb,
73 struct inode *dir,
74 struct buffer_head *parent_fe_bh,
75 struct buffer_head **new_de_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -070076static int ocfs2_do_extend_dir(struct super_block *sb,
77 handle_t *handle,
78 struct inode *dir,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
83
Mark Fasheh5eae5b92007-09-10 17:50:51 -070084static int ocfs2_check_dir_entry(struct inode * dir,
85 struct ocfs2_dir_entry * de,
86 struct buffer_head * bh,
87 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -070088{
89 const char *error_msg = NULL;
90 const int rlen = le16_to_cpu(de->rec_len);
91
92 if (rlen < OCFS2_DIR_REC_LEN(1))
93 error_msg = "rec_len is smaller than minimal";
94 else if (rlen % 4 != 0)
95 error_msg = "rec_len % 4 != 0";
96 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
97 error_msg = "rec_len is too small for name_len";
98 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
99 error_msg = "directory entry across blocks";
100
101 if (error_msg != NULL)
102 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
103 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
104 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
105 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
106 de->name_len);
107 return error_msg == NULL ? 1 : 0;
108}
109
110static inline int ocfs2_match(int len,
111 const char * const name,
112 struct ocfs2_dir_entry *de)
113{
114 if (len != de->name_len)
115 return 0;
116 if (!de->inode)
117 return 0;
118 return !memcmp(name, de->name, len);
119}
120
121/*
122 * Returns 0 if not found, -1 on failure, and 1 on success
123 */
124static int inline ocfs2_search_dirblock(struct buffer_head *bh,
125 struct inode *dir,
126 const char *name, int namelen,
127 unsigned long offset,
128 struct ocfs2_dir_entry **res_dir)
129{
130 struct ocfs2_dir_entry *de;
131 char *dlimit, *de_buf;
132 int de_len;
133 int ret = 0;
134
135 mlog_entry_void();
136
137 de_buf = bh->b_data;
138 dlimit = de_buf + dir->i_sb->s_blocksize;
139
140 while (de_buf < dlimit) {
141 /* this code is executed quadratically often */
142 /* do minimal checking `by hand' */
143
144 de = (struct ocfs2_dir_entry *) de_buf;
145
146 if (de_buf + namelen <= dlimit &&
147 ocfs2_match(namelen, name, de)) {
148 /* found a match - just to be sure, do a full check */
149 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
150 ret = -1;
151 goto bail;
152 }
153 *res_dir = de;
154 ret = 1;
155 goto bail;
156 }
157
158 /* prevent looping on a bad block */
159 de_len = le16_to_cpu(de->rec_len);
160 if (de_len <= 0) {
161 ret = -1;
162 goto bail;
163 }
164
165 de_buf += de_len;
166 offset += de_len;
167 }
168
169bail:
170 mlog_exit(ret);
171 return ret;
172}
173
174struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
175 struct inode *dir,
176 struct ocfs2_dir_entry **res_dir)
177{
178 struct super_block *sb;
179 struct buffer_head *bh_use[NAMEI_RA_SIZE];
180 struct buffer_head *bh, *ret = NULL;
181 unsigned long start, block, b;
182 int ra_max = 0; /* Number of bh's in the readahead
183 buffer, bh_use[] */
184 int ra_ptr = 0; /* Current index into readahead
185 buffer */
186 int num = 0;
187 int nblocks, i, err;
188
189 mlog_entry_void();
190
191 *res_dir = NULL;
192 sb = dir->i_sb;
193
194 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
195 start = OCFS2_I(dir)->ip_dir_start_lookup;
196 if (start >= nblocks)
197 start = 0;
198 block = start;
199
200restart:
201 do {
202 /*
203 * We deal with the read-ahead logic here.
204 */
205 if (ra_ptr >= ra_max) {
206 /* Refill the readahead buffer */
207 ra_ptr = 0;
208 b = block;
209 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
210 /*
211 * Terminate if we reach the end of the
212 * directory and must wrap, or if our
213 * search has finished at this block.
214 */
215 if (b >= nblocks || (num && block == start)) {
216 bh_use[ra_max] = NULL;
217 break;
218 }
219 num++;
220
221 bh = ocfs2_bread(dir, b++, &err, 1);
222 bh_use[ra_max] = bh;
223 }
224 }
225 if ((bh = bh_use[ra_ptr++]) == NULL)
226 goto next;
227 wait_on_buffer(bh);
228 if (!buffer_uptodate(bh)) {
229 /* read error, skip block & hope for the best */
230 ocfs2_error(dir->i_sb, "reading directory %llu, "
231 "offset %lu\n",
232 (unsigned long long)OCFS2_I(dir)->ip_blkno,
233 block);
234 brelse(bh);
235 goto next;
236 }
237 i = ocfs2_search_dirblock(bh, dir, name, namelen,
238 block << sb->s_blocksize_bits,
239 res_dir);
240 if (i == 1) {
241 OCFS2_I(dir)->ip_dir_start_lookup = block;
242 ret = bh;
243 goto cleanup_and_exit;
244 } else {
245 brelse(bh);
246 if (i < 0)
247 goto cleanup_and_exit;
248 }
249 next:
250 if (++block >= nblocks)
251 block = 0;
252 } while (block != start);
253
254 /*
255 * If the directory has grown while we were searching, then
256 * search the last part of the directory before giving up.
257 */
258 block = nblocks;
259 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
260 if (block < nblocks) {
261 start = 0;
262 goto restart;
263 }
264
265cleanup_and_exit:
266 /* Clean up the read-ahead blocks */
267 for (; ra_ptr < ra_max; ra_ptr++)
268 brelse(bh_use[ra_ptr]);
269
270 mlog_exit_ptr(ret);
271 return ret;
272}
273
Mark Fasheh38760e22007-09-11 17:21:56 -0700274int ocfs2_update_entry(struct inode *dir, handle_t *handle,
275 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
276 struct inode *new_entry_inode)
277{
278 int ret;
279
280 ret = ocfs2_journal_access(handle, dir, de_bh,
281 OCFS2_JOURNAL_ACCESS_WRITE);
282 if (ret) {
283 mlog_errno(ret);
284 goto out;
285 }
286
287 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
288 ocfs2_set_de_type(de, new_entry_inode->i_mode);
289
290 ocfs2_journal_dirty(handle, de_bh);
291
292out:
293 return ret;
294}
295
Mark Fasheh316f4b92007-09-07 18:21:26 -0700296/*
297 * ocfs2_delete_entry deletes a directory entry by merging it with the
298 * previous entry
299 */
300int ocfs2_delete_entry(handle_t *handle,
301 struct inode *dir,
302 struct ocfs2_dir_entry *de_del,
303 struct buffer_head *bh)
304{
305 struct ocfs2_dir_entry *de, *pde;
306 int i, status = -ENOENT;
307
308 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
309
310 i = 0;
311 pde = NULL;
312 de = (struct ocfs2_dir_entry *) bh->b_data;
313 while (i < bh->b_size) {
314 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
315 status = -EIO;
316 mlog_errno(status);
317 goto bail;
318 }
319 if (de == de_del) {
320 status = ocfs2_journal_access(handle, dir, bh,
321 OCFS2_JOURNAL_ACCESS_WRITE);
322 if (status < 0) {
323 status = -EIO;
324 mlog_errno(status);
325 goto bail;
326 }
327 if (pde)
328 pde->rec_len =
329 cpu_to_le16(le16_to_cpu(pde->rec_len) +
330 le16_to_cpu(de->rec_len));
331 else
332 de->inode = 0;
333 dir->i_version++;
334 status = ocfs2_journal_dirty(handle, bh);
335 goto bail;
336 }
337 i += le16_to_cpu(de->rec_len);
338 pde = de;
339 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
340 }
341bail:
342 mlog_exit(status);
343 return status;
344}
345
346/* we don't always have a dentry for what we want to add, so people
347 * like orphan dir can call this instead.
348 *
349 * If you pass me insert_bh, I'll skip the search of the other dir
350 * blocks and put the record in there.
351 */
352int __ocfs2_add_entry(handle_t *handle,
353 struct inode *dir,
354 const char *name, int namelen,
355 struct inode *inode, u64 blkno,
356 struct buffer_head *parent_fe_bh,
357 struct buffer_head *insert_bh)
358{
359 unsigned long offset;
360 unsigned short rec_len;
361 struct ocfs2_dir_entry *de, *de1;
362 struct super_block *sb;
363 int retval, status;
364
365 mlog_entry_void();
366
367 sb = dir->i_sb;
368
369 if (!namelen)
370 return -EINVAL;
371
372 rec_len = OCFS2_DIR_REC_LEN(namelen);
373 offset = 0;
374 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
375 while (1) {
376 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
377 /* These checks should've already been passed by the
378 * prepare function, but I guess we can leave them
379 * here anyway. */
380 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
381 retval = -ENOENT;
382 goto bail;
383 }
384 if (ocfs2_match(namelen, name, de)) {
385 retval = -EEXIST;
386 goto bail;
387 }
388 if (((le64_to_cpu(de->inode) == 0) &&
389 (le16_to_cpu(de->rec_len) >= rec_len)) ||
390 (le16_to_cpu(de->rec_len) >=
391 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
392 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
393 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
394 if (retval < 0) {
395 mlog_errno(retval);
396 goto bail;
397 }
398
399 status = ocfs2_journal_access(handle, dir, insert_bh,
400 OCFS2_JOURNAL_ACCESS_WRITE);
401 /* By now the buffer is marked for journaling */
402 offset += le16_to_cpu(de->rec_len);
403 if (le64_to_cpu(de->inode)) {
404 de1 = (struct ocfs2_dir_entry *)((char *) de +
405 OCFS2_DIR_REC_LEN(de->name_len));
406 de1->rec_len =
407 cpu_to_le16(le16_to_cpu(de->rec_len) -
408 OCFS2_DIR_REC_LEN(de->name_len));
409 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
410 de = de1;
411 }
412 de->file_type = OCFS2_FT_UNKNOWN;
413 if (blkno) {
414 de->inode = cpu_to_le64(blkno);
415 ocfs2_set_de_type(de, inode->i_mode);
416 } else
417 de->inode = 0;
418 de->name_len = namelen;
419 memcpy(de->name, name, namelen);
420
421 dir->i_version++;
422 status = ocfs2_journal_dirty(handle, insert_bh);
423 retval = 0;
424 goto bail;
425 }
426 offset += le16_to_cpu(de->rec_len);
427 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
428 }
429
430 /* when you think about it, the assert above should prevent us
431 * from ever getting here. */
432 retval = -ENOSPC;
433bail:
434
435 mlog_exit(retval);
436 return retval;
437}
438
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700439static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
440 loff_t *f_pos, void *priv, filldir_t filldir)
Mark Fashehccd979b2005-12-15 14:31:24 -0800441{
442 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700443 unsigned long offset, blk, last_ra_blk = 0;
444 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 struct buffer_head * bh, * tmp;
446 struct ocfs2_dir_entry * de;
447 int err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700449 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
451 stored = 0;
452 bh = NULL;
453
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700454 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800455
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700456 while (!error && !stored && *f_pos < i_size_read(inode)) {
457 blk = (*f_pos) >> sb->s_blocksize_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800458 bh = ocfs2_bread(inode, blk, &err, 0);
459 if (!bh) {
Mark Fashehb0697052006-03-03 10:24:33 -0800460 mlog(ML_ERROR,
461 "directory #%llu contains a hole at offset %lld\n",
462 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700463 *f_pos);
464 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 continue;
466 }
467
Mark Fashehaa958872006-04-21 13:49:02 -0700468 /* The idea here is to begin with 8k read-ahead and to stay
469 * 4k ahead of our current position.
470 *
471 * TODO: Use the pagecache for this. We just need to
472 * make sure it's cluster-safe... */
473 if (!last_ra_blk
474 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
475 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800476 i > 0; i--) {
477 tmp = ocfs2_bread(inode, ++blk, &err, 1);
478 if (tmp)
479 brelse(tmp);
480 }
Mark Fashehaa958872006-04-21 13:49:02 -0700481 last_ra_blk = blk;
482 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800483 }
484
485revalidate:
486 /* If the dir block has changed since the last call to
487 * readdir(2), then we might be pointing to an invalid
488 * dirent right now. Scan from the start of the block
489 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700490 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800491 for (i = 0; i < sb->s_blocksize && i < offset; ) {
492 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
493 /* It's too expensive to do a full
494 * dirent test each time round this
495 * loop, but we do have to test at
496 * least that it is non-zero. A
497 * failure will be detected in the
498 * dirent test below. */
499 if (le16_to_cpu(de->rec_len) <
500 OCFS2_DIR_REC_LEN(1))
501 break;
502 i += le16_to_cpu(de->rec_len);
503 }
504 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700505 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800506 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700507 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800508 }
509
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700510 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800511 && offset < sb->s_blocksize) {
512 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
513 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
514 /* On error, skip the f_pos to the
515 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700516 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700518 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 }
520 offset += le16_to_cpu(de->rec_len);
521 if (le64_to_cpu(de->inode)) {
522 /* We might block in the next section
523 * if the data destination is
524 * currently swapped out. So, use a
525 * version stamp to detect whether or
526 * not the directory has been modified
527 * during the copy operation.
528 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700529 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800530 unsigned char d_type = DT_UNKNOWN;
531
532 if (de->file_type < OCFS2_FT_MAX)
533 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700534 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700536 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700537 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800538 d_type);
539 if (error)
540 break;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700541 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 goto revalidate;
543 stored ++;
544 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700545 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800546 }
547 offset = 0;
548 brelse(bh);
549 }
550
551 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700552out:
553 return stored;
554}
555
556/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700557 * This is intended to be called from inside other kernel functions,
558 * so we fake some arguments.
559 */
560int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
561 filldir_t filldir)
562{
563 int ret = 0;
564 unsigned long version = inode->i_version;
565
566 while (*f_pos < i_size_read(inode)) {
567 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
568 filldir);
569 if (ret)
570 break;
571 }
572
573 return 0;
574}
575
576/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700577 * ocfs2_readdir()
578 *
579 */
580int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
581{
582 int error = 0;
583 struct inode *inode = filp->f_path.dentry->d_inode;
584 int lock_level = 0;
585
586 mlog_entry("dirino=%llu\n",
587 (unsigned long long)OCFS2_I(inode)->ip_blkno);
588
589 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
590 if (lock_level && error >= 0) {
591 /* We release EX lock which used to update atime
592 * and get PR lock again to reduce contention
593 * on commonly accessed directories. */
594 ocfs2_meta_unlock(inode, 1);
595 lock_level = 0;
596 error = ocfs2_meta_lock(inode, NULL, 0);
597 }
598 if (error < 0) {
599 if (error != -ENOENT)
600 mlog_errno(error);
601 /* we haven't got any yet, so propagate the error. */
602 goto bail_nolock;
603 }
604
605 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
606 dirent, filldir);
607
Tiger Yang25899de2006-11-15 15:49:02 +0800608 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800609
Mark Fashehaa958872006-04-21 13:49:02 -0700610bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700611 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -0800612
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700613 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800614}
615
616/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800617 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 */
619int ocfs2_find_files_on_disk(const char *name,
620 int namelen,
621 u64 *blkno,
622 struct inode *inode,
623 struct buffer_head **dirent_bh,
624 struct ocfs2_dir_entry **dirent)
625{
626 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800627
Joel Becker2b388c62006-05-10 18:28:59 -0700628 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
629 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800630
631 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
632 if (!*dirent_bh || !*dirent) {
633 status = -ENOENT;
634 goto leave;
635 }
636
637 *blkno = le64_to_cpu((*dirent)->inode);
638
639 status = 0;
640leave:
641 if (status < 0) {
642 *dirent = NULL;
643 if (*dirent_bh) {
644 brelse(*dirent_bh);
645 *dirent_bh = NULL;
646 }
647 }
648
649 mlog_exit(status);
650 return status;
651}
652
Mark Fashehbe94d112007-09-11 15:22:06 -0700653/*
654 * Convenience function for callers which just want the block number
655 * mapped to a name and don't require the full dirent info, etc.
656 */
657int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
658 int namelen, u64 *blkno)
659{
660 int ret;
661 struct buffer_head *bh = NULL;
662 struct ocfs2_dir_entry *dirent = NULL;
663
664 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
665 brelse(bh);
666
667 return ret;
668}
669
Mark Fashehccd979b2005-12-15 14:31:24 -0800670/* Check for a name within a directory.
671 *
672 * Return 0 if the name does not exist
673 * Return -EEXIST if the directory contains the name
674 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800675 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 */
677int ocfs2_check_dir_for_entry(struct inode *dir,
678 const char *name,
679 int namelen)
680{
681 int ret;
682 struct buffer_head *dirent_bh = NULL;
683 struct ocfs2_dir_entry *dirent = NULL;
684
Mark Fashehb0697052006-03-03 10:24:33 -0800685 mlog_entry("dir %llu, name '%.*s'\n",
686 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800687
688 ret = -EEXIST;
689 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
690 if (dirent_bh)
691 goto bail;
692
693 ret = 0;
694bail:
695 if (dirent_bh)
696 brelse(dirent_bh);
697
698 mlog_exit(ret);
699 return ret;
700}
701
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700702struct ocfs2_empty_dir_priv {
703 unsigned seen_dot;
704 unsigned seen_dot_dot;
705 unsigned seen_other;
706};
707static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
708 loff_t pos, u64 ino, unsigned type)
709{
710 struct ocfs2_empty_dir_priv *p = priv;
711
712 /*
713 * Check the positions of "." and ".." records to be sure
714 * they're in the correct place.
715 */
716 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
717 p->seen_dot = 1;
718 return 0;
719 }
720
721 if (name_len == 2 && !strncmp("..", name, 2) &&
722 pos == OCFS2_DIR_REC_LEN(1)) {
723 p->seen_dot_dot = 1;
724 return 0;
725 }
726
727 p->seen_other = 1;
728 return 1;
729}
Mark Fashehccd979b2005-12-15 14:31:24 -0800730/*
731 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700732 *
733 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 */
735int ocfs2_empty_dir(struct inode *inode)
736{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700737 int ret;
738 loff_t start = 0;
739 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -0800740
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700741 memset(&priv, 0, sizeof(priv));
742
743 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
744 if (ret)
745 mlog_errno(ret);
746
747 if (!priv.seen_dot || !priv.seen_dot_dot) {
748 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb0697052006-03-03 10:24:33 -0800749 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700750 /*
751 * XXX: Is it really safe to allow an unlink to continue?
752 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800753 return 1;
754 }
755
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700756 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -0800757}
758
Mark Fasheh316f4b92007-09-07 18:21:26 -0700759int ocfs2_fill_new_dir(struct ocfs2_super *osb,
760 handle_t *handle,
761 struct inode *parent,
762 struct inode *inode,
763 struct buffer_head *fe_bh,
764 struct ocfs2_alloc_context *data_ac)
765{
766 int status;
767 struct buffer_head *new_bh = NULL;
768 struct ocfs2_dir_entry *de = NULL;
769
770 mlog_entry_void();
771
772 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
773 data_ac, NULL, &new_bh);
774 if (status < 0) {
775 mlog_errno(status);
776 goto bail;
777 }
778
779 ocfs2_set_new_buffer_uptodate(inode, new_bh);
780
781 status = ocfs2_journal_access(handle, inode, new_bh,
782 OCFS2_JOURNAL_ACCESS_CREATE);
783 if (status < 0) {
784 mlog_errno(status);
785 goto bail;
786 }
787 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
788
789 de = (struct ocfs2_dir_entry *) new_bh->b_data;
790 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
791 de->name_len = 1;
792 de->rec_len =
793 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
794 strcpy(de->name, ".");
795 ocfs2_set_de_type(de, S_IFDIR);
796 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
797 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
798 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
799 OCFS2_DIR_REC_LEN(1));
800 de->name_len = 2;
801 strcpy(de->name, "..");
802 ocfs2_set_de_type(de, S_IFDIR);
803
804 status = ocfs2_journal_dirty(handle, new_bh);
805 if (status < 0) {
806 mlog_errno(status);
807 goto bail;
808 }
809
810 i_size_write(inode, inode->i_sb->s_blocksize);
811 inode->i_nlink = 2;
812 inode->i_blocks = ocfs2_inode_sector_count(inode);
813 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
814 if (status < 0) {
815 mlog_errno(status);
816 goto bail;
817 }
818
819 status = 0;
820bail:
821 if (new_bh)
822 brelse(new_bh);
823
824 mlog_exit(status);
825 return status;
826}
827
Mark Fashehccd979b2005-12-15 14:31:24 -0800828/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700829static int ocfs2_do_extend_dir(struct super_block *sb,
830 handle_t *handle,
831 struct inode *dir,
832 struct buffer_head *parent_fe_bh,
833 struct ocfs2_alloc_context *data_ac,
834 struct ocfs2_alloc_context *meta_ac,
835 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -0800836{
837 int status;
838 int extend;
Mark Fasheh8110b072007-03-22 16:53:23 -0700839 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800840
841 spin_lock(&OCFS2_I(dir)->ip_lock);
842 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
843 spin_unlock(&OCFS2_I(dir)->ip_lock);
844
845 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -0800846 u32 offset = OCFS2_I(dir)->ip_clusters;
847
848 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800849 1, 0, parent_fe_bh, handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800850 data_ac, meta_ac, NULL);
851 BUG_ON(status == -EAGAIN);
852 if (status < 0) {
853 mlog_errno(status);
854 goto bail;
855 }
856 }
857
Mark Fasheh8110b072007-03-22 16:53:23 -0700858 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
859 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800860 if (status < 0) {
861 mlog_errno(status);
862 goto bail;
863 }
864
865 *new_bh = sb_getblk(sb, p_blkno);
866 if (!*new_bh) {
867 status = -EIO;
868 mlog_errno(status);
869 goto bail;
870 }
871 status = 0;
872bail:
873 mlog_exit(status);
874 return status;
875}
876
877/* assumes you already have a cluster lock on the directory. */
878static int ocfs2_extend_dir(struct ocfs2_super *osb,
879 struct inode *dir,
880 struct buffer_head *parent_fe_bh,
881 struct buffer_head **new_de_bh)
882{
883 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -0700884 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885 loff_t dir_i_size;
886 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
887 struct ocfs2_alloc_context *data_ac = NULL;
888 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700889 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800890 struct buffer_head *new_bh = NULL;
891 struct ocfs2_dir_entry * de;
892 struct super_block *sb = osb->sb;
893
894 mlog_entry_void();
895
896 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -0800897 mlog(0, "extending dir %llu (i_size = %lld)\n",
898 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800899
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 /* dir->i_size is always block aligned. */
901 spin_lock(&OCFS2_I(dir)->ip_lock);
902 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
903 spin_unlock(&OCFS2_I(dir)->ip_lock);
904 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
905 if (num_free_extents < 0) {
906 status = num_free_extents;
907 mlog_errno(status);
908 goto bail;
909 }
910
911 if (!num_free_extents) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700912 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 if (status < 0) {
914 if (status != -ENOSPC)
915 mlog_errno(status);
916 goto bail;
917 }
918 }
919
Mark Fashehda5cbf22006-10-06 18:34:35 -0700920 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800921 if (status < 0) {
922 if (status != -ENOSPC)
923 mlog_errno(status);
924 goto bail;
925 }
926
927 credits = ocfs2_calc_extend_credits(sb, fe, 1);
928 } else {
929 spin_unlock(&OCFS2_I(dir)->ip_lock);
930 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
931 }
932
Joel Beckeree19a772007-03-28 18:27:07 -0700933 down_write(&OCFS2_I(dir)->ip_alloc_sem);
934 drop_alloc_sem = 1;
935
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700936 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800937 if (IS_ERR(handle)) {
938 status = PTR_ERR(handle);
939 handle = NULL;
940 mlog_errno(status);
941 goto bail;
942 }
943
944 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
945 data_ac, meta_ac, &new_bh);
946 if (status < 0) {
947 mlog_errno(status);
948 goto bail;
949 }
950
951 ocfs2_set_new_buffer_uptodate(dir, new_bh);
952
953 status = ocfs2_journal_access(handle, dir, new_bh,
954 OCFS2_JOURNAL_ACCESS_CREATE);
955 if (status < 0) {
956 mlog_errno(status);
957 goto bail;
958 }
959 memset(new_bh->b_data, 0, sb->s_blocksize);
960 de = (struct ocfs2_dir_entry *) new_bh->b_data;
961 de->inode = 0;
962 de->rec_len = cpu_to_le16(sb->s_blocksize);
963 status = ocfs2_journal_dirty(handle, new_bh);
964 if (status < 0) {
965 mlog_errno(status);
966 goto bail;
967 }
968
969 dir_i_size += dir->i_sb->s_blocksize;
970 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700971 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
973 if (status < 0) {
974 mlog_errno(status);
975 goto bail;
976 }
977
978 *new_de_bh = new_bh;
979 get_bh(*new_de_bh);
980bail:
Joel Beckeree19a772007-03-28 18:27:07 -0700981 if (drop_alloc_sem)
982 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700984 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800985
986 if (data_ac)
987 ocfs2_free_alloc_context(data_ac);
988 if (meta_ac)
989 ocfs2_free_alloc_context(meta_ac);
990
991 if (new_bh)
992 brelse(new_bh);
993
994 mlog_exit(status);
995 return status;
996}
997
998/*
999 * Search the dir for a good spot, extending it if necessary. The
1000 * block containing an appropriate record is returned in ret_de_bh.
1001 */
1002int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1003 struct inode *dir,
1004 struct buffer_head *parent_fe_bh,
1005 const char *name,
1006 int namelen,
1007 struct buffer_head **ret_de_bh)
1008{
1009 unsigned long offset;
1010 struct buffer_head * bh = NULL;
1011 unsigned short rec_len;
1012 struct ocfs2_dinode *fe;
1013 struct ocfs2_dir_entry *de;
1014 struct super_block *sb;
1015 int status;
1016
1017 mlog_entry_void();
1018
Mark Fashehb0697052006-03-03 10:24:33 -08001019 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1020 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001021
1022 BUG_ON(!S_ISDIR(dir->i_mode));
1023 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1024 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1025
1026 sb = dir->i_sb;
1027
1028 if (!namelen) {
1029 status = -EINVAL;
1030 mlog_errno(status);
1031 goto bail;
1032 }
1033
1034 bh = ocfs2_bread(dir, 0, &status, 0);
1035 if (!bh) {
1036 mlog_errno(status);
1037 goto bail;
1038 }
1039
1040 rec_len = OCFS2_DIR_REC_LEN(namelen);
1041 offset = 0;
1042 de = (struct ocfs2_dir_entry *) bh->b_data;
1043 while (1) {
1044 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1045 brelse(bh);
1046 bh = NULL;
1047
1048 if (i_size_read(dir) <= offset) {
1049 status = ocfs2_extend_dir(osb,
1050 dir,
1051 parent_fe_bh,
1052 &bh);
1053 if (status < 0) {
1054 mlog_errno(status);
1055 goto bail;
1056 }
1057 BUG_ON(!bh);
1058 *ret_de_bh = bh;
1059 get_bh(*ret_de_bh);
1060 goto bail;
1061 }
1062 bh = ocfs2_bread(dir,
1063 offset >> sb->s_blocksize_bits,
1064 &status,
1065 0);
1066 if (!bh) {
1067 mlog_errno(status);
1068 goto bail;
1069 }
1070 /* move to next block */
1071 de = (struct ocfs2_dir_entry *) bh->b_data;
1072 }
1073 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1074 status = -ENOENT;
1075 goto bail;
1076 }
1077 if (ocfs2_match(namelen, name, de)) {
1078 status = -EEXIST;
1079 goto bail;
1080 }
1081 if (((le64_to_cpu(de->inode) == 0) &&
1082 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1083 (le16_to_cpu(de->rec_len) >=
1084 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1085 /* Ok, we found a spot. Return this bh and let
1086 * the caller actually fill it in. */
1087 *ret_de_bh = bh;
1088 get_bh(*ret_de_bh);
1089 status = 0;
1090 goto bail;
1091 }
1092 offset += le16_to_cpu(de->rec_len);
1093 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1094 }
1095
1096 status = 0;
1097bail:
1098 if (bh)
1099 brelse(bh);
1100
1101 mlog_exit(status);
1102 return status;
1103}