blob: 4bb54406354b6bf3e3d0edcbf3c2a892986a9603 [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
274/*
275 * ocfs2_delete_entry deletes a directory entry by merging it with the
276 * previous entry
277 */
278int ocfs2_delete_entry(handle_t *handle,
279 struct inode *dir,
280 struct ocfs2_dir_entry *de_del,
281 struct buffer_head *bh)
282{
283 struct ocfs2_dir_entry *de, *pde;
284 int i, status = -ENOENT;
285
286 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
287
288 i = 0;
289 pde = NULL;
290 de = (struct ocfs2_dir_entry *) bh->b_data;
291 while (i < bh->b_size) {
292 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
293 status = -EIO;
294 mlog_errno(status);
295 goto bail;
296 }
297 if (de == de_del) {
298 status = ocfs2_journal_access(handle, dir, bh,
299 OCFS2_JOURNAL_ACCESS_WRITE);
300 if (status < 0) {
301 status = -EIO;
302 mlog_errno(status);
303 goto bail;
304 }
305 if (pde)
306 pde->rec_len =
307 cpu_to_le16(le16_to_cpu(pde->rec_len) +
308 le16_to_cpu(de->rec_len));
309 else
310 de->inode = 0;
311 dir->i_version++;
312 status = ocfs2_journal_dirty(handle, bh);
313 goto bail;
314 }
315 i += le16_to_cpu(de->rec_len);
316 pde = de;
317 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
318 }
319bail:
320 mlog_exit(status);
321 return status;
322}
323
324/* we don't always have a dentry for what we want to add, so people
325 * like orphan dir can call this instead.
326 *
327 * If you pass me insert_bh, I'll skip the search of the other dir
328 * blocks and put the record in there.
329 */
330int __ocfs2_add_entry(handle_t *handle,
331 struct inode *dir,
332 const char *name, int namelen,
333 struct inode *inode, u64 blkno,
334 struct buffer_head *parent_fe_bh,
335 struct buffer_head *insert_bh)
336{
337 unsigned long offset;
338 unsigned short rec_len;
339 struct ocfs2_dir_entry *de, *de1;
340 struct super_block *sb;
341 int retval, status;
342
343 mlog_entry_void();
344
345 sb = dir->i_sb;
346
347 if (!namelen)
348 return -EINVAL;
349
350 rec_len = OCFS2_DIR_REC_LEN(namelen);
351 offset = 0;
352 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
353 while (1) {
354 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
355 /* These checks should've already been passed by the
356 * prepare function, but I guess we can leave them
357 * here anyway. */
358 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
359 retval = -ENOENT;
360 goto bail;
361 }
362 if (ocfs2_match(namelen, name, de)) {
363 retval = -EEXIST;
364 goto bail;
365 }
366 if (((le64_to_cpu(de->inode) == 0) &&
367 (le16_to_cpu(de->rec_len) >= rec_len)) ||
368 (le16_to_cpu(de->rec_len) >=
369 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
370 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
371 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
372 if (retval < 0) {
373 mlog_errno(retval);
374 goto bail;
375 }
376
377 status = ocfs2_journal_access(handle, dir, insert_bh,
378 OCFS2_JOURNAL_ACCESS_WRITE);
379 /* By now the buffer is marked for journaling */
380 offset += le16_to_cpu(de->rec_len);
381 if (le64_to_cpu(de->inode)) {
382 de1 = (struct ocfs2_dir_entry *)((char *) de +
383 OCFS2_DIR_REC_LEN(de->name_len));
384 de1->rec_len =
385 cpu_to_le16(le16_to_cpu(de->rec_len) -
386 OCFS2_DIR_REC_LEN(de->name_len));
387 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
388 de = de1;
389 }
390 de->file_type = OCFS2_FT_UNKNOWN;
391 if (blkno) {
392 de->inode = cpu_to_le64(blkno);
393 ocfs2_set_de_type(de, inode->i_mode);
394 } else
395 de->inode = 0;
396 de->name_len = namelen;
397 memcpy(de->name, name, namelen);
398
399 dir->i_version++;
400 status = ocfs2_journal_dirty(handle, insert_bh);
401 retval = 0;
402 goto bail;
403 }
404 offset += le16_to_cpu(de->rec_len);
405 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
406 }
407
408 /* when you think about it, the assert above should prevent us
409 * from ever getting here. */
410 retval = -ENOSPC;
411bail:
412
413 mlog_exit(retval);
414 return retval;
415}
416
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700417static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
418 loff_t *f_pos, void *priv, filldir_t filldir)
Mark Fashehccd979b2005-12-15 14:31:24 -0800419{
420 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700421 unsigned long offset, blk, last_ra_blk = 0;
422 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 struct buffer_head * bh, * tmp;
424 struct ocfs2_dir_entry * de;
425 int err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700427 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800428
429 stored = 0;
430 bh = NULL;
431
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700432 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800433
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700434 while (!error && !stored && *f_pos < i_size_read(inode)) {
435 blk = (*f_pos) >> sb->s_blocksize_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 bh = ocfs2_bread(inode, blk, &err, 0);
437 if (!bh) {
Mark Fashehb06970532006-03-03 10:24:33 -0800438 mlog(ML_ERROR,
439 "directory #%llu contains a hole at offset %lld\n",
440 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700441 *f_pos);
442 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800443 continue;
444 }
445
Mark Fashehaa958872006-04-21 13:49:02 -0700446 /* The idea here is to begin with 8k read-ahead and to stay
447 * 4k ahead of our current position.
448 *
449 * TODO: Use the pagecache for this. We just need to
450 * make sure it's cluster-safe... */
451 if (!last_ra_blk
452 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
453 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 i > 0; i--) {
455 tmp = ocfs2_bread(inode, ++blk, &err, 1);
456 if (tmp)
457 brelse(tmp);
458 }
Mark Fashehaa958872006-04-21 13:49:02 -0700459 last_ra_blk = blk;
460 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800461 }
462
463revalidate:
464 /* If the dir block has changed since the last call to
465 * readdir(2), then we might be pointing to an invalid
466 * dirent right now. Scan from the start of the block
467 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700468 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800469 for (i = 0; i < sb->s_blocksize && i < offset; ) {
470 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
471 /* It's too expensive to do a full
472 * dirent test each time round this
473 * loop, but we do have to test at
474 * least that it is non-zero. A
475 * failure will be detected in the
476 * dirent test below. */
477 if (le16_to_cpu(de->rec_len) <
478 OCFS2_DIR_REC_LEN(1))
479 break;
480 i += le16_to_cpu(de->rec_len);
481 }
482 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700483 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800484 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700485 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800486 }
487
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700488 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800489 && offset < sb->s_blocksize) {
490 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
491 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
492 /* On error, skip the f_pos to the
493 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700494 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800495 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700496 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 }
498 offset += le16_to_cpu(de->rec_len);
499 if (le64_to_cpu(de->inode)) {
500 /* We might block in the next section
501 * if the data destination is
502 * currently swapped out. So, use a
503 * version stamp to detect whether or
504 * not the directory has been modified
505 * during the copy operation.
506 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700507 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800508 unsigned char d_type = DT_UNKNOWN;
509
510 if (de->file_type < OCFS2_FT_MAX)
511 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700512 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700514 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700515 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 d_type);
517 if (error)
518 break;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700519 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800520 goto revalidate;
521 stored ++;
522 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700523 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 }
525 offset = 0;
526 brelse(bh);
527 }
528
529 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700530out:
531 return stored;
532}
533
534/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700535 * This is intended to be called from inside other kernel functions,
536 * so we fake some arguments.
537 */
538int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
539 filldir_t filldir)
540{
541 int ret = 0;
542 unsigned long version = inode->i_version;
543
544 while (*f_pos < i_size_read(inode)) {
545 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
546 filldir);
547 if (ret)
548 break;
549 }
550
551 return 0;
552}
553
554/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700555 * ocfs2_readdir()
556 *
557 */
558int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
559{
560 int error = 0;
561 struct inode *inode = filp->f_path.dentry->d_inode;
562 int lock_level = 0;
563
564 mlog_entry("dirino=%llu\n",
565 (unsigned long long)OCFS2_I(inode)->ip_blkno);
566
567 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
568 if (lock_level && error >= 0) {
569 /* We release EX lock which used to update atime
570 * and get PR lock again to reduce contention
571 * on commonly accessed directories. */
572 ocfs2_meta_unlock(inode, 1);
573 lock_level = 0;
574 error = ocfs2_meta_lock(inode, NULL, 0);
575 }
576 if (error < 0) {
577 if (error != -ENOENT)
578 mlog_errno(error);
579 /* we haven't got any yet, so propagate the error. */
580 goto bail_nolock;
581 }
582
583 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
584 dirent, filldir);
585
Tiger Yang25899de2006-11-15 15:49:02 +0800586 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800587
Mark Fashehaa958872006-04-21 13:49:02 -0700588bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700589 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -0800590
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700591 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800592}
593
594/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800595 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 */
597int ocfs2_find_files_on_disk(const char *name,
598 int namelen,
599 u64 *blkno,
600 struct inode *inode,
601 struct buffer_head **dirent_bh,
602 struct ocfs2_dir_entry **dirent)
603{
604 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800605
Joel Becker2b388c62006-05-10 18:28:59 -0700606 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
607 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800608
609 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
610 if (!*dirent_bh || !*dirent) {
611 status = -ENOENT;
612 goto leave;
613 }
614
615 *blkno = le64_to_cpu((*dirent)->inode);
616
617 status = 0;
618leave:
619 if (status < 0) {
620 *dirent = NULL;
621 if (*dirent_bh) {
622 brelse(*dirent_bh);
623 *dirent_bh = NULL;
624 }
625 }
626
627 mlog_exit(status);
628 return status;
629}
630
631/* Check for a name within a directory.
632 *
633 * Return 0 if the name does not exist
634 * Return -EEXIST if the directory contains the name
635 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800636 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800637 */
638int ocfs2_check_dir_for_entry(struct inode *dir,
639 const char *name,
640 int namelen)
641{
642 int ret;
643 struct buffer_head *dirent_bh = NULL;
644 struct ocfs2_dir_entry *dirent = NULL;
645
Mark Fashehb06970532006-03-03 10:24:33 -0800646 mlog_entry("dir %llu, name '%.*s'\n",
647 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800648
649 ret = -EEXIST;
650 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
651 if (dirent_bh)
652 goto bail;
653
654 ret = 0;
655bail:
656 if (dirent_bh)
657 brelse(dirent_bh);
658
659 mlog_exit(ret);
660 return ret;
661}
662
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700663struct ocfs2_empty_dir_priv {
664 unsigned seen_dot;
665 unsigned seen_dot_dot;
666 unsigned seen_other;
667};
668static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
669 loff_t pos, u64 ino, unsigned type)
670{
671 struct ocfs2_empty_dir_priv *p = priv;
672
673 /*
674 * Check the positions of "." and ".." records to be sure
675 * they're in the correct place.
676 */
677 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
678 p->seen_dot = 1;
679 return 0;
680 }
681
682 if (name_len == 2 && !strncmp("..", name, 2) &&
683 pos == OCFS2_DIR_REC_LEN(1)) {
684 p->seen_dot_dot = 1;
685 return 0;
686 }
687
688 p->seen_other = 1;
689 return 1;
690}
Mark Fashehccd979b2005-12-15 14:31:24 -0800691/*
692 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700693 *
694 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -0800695 */
696int ocfs2_empty_dir(struct inode *inode)
697{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700698 int ret;
699 loff_t start = 0;
700 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -0800701
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700702 memset(&priv, 0, sizeof(priv));
703
704 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
705 if (ret)
706 mlog_errno(ret);
707
708 if (!priv.seen_dot || !priv.seen_dot_dot) {
709 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -0800710 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700711 /*
712 * XXX: Is it really safe to allow an unlink to continue?
713 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800714 return 1;
715 }
716
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700717 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -0800718}
719
Mark Fasheh316f4b92007-09-07 18:21:26 -0700720int ocfs2_fill_new_dir(struct ocfs2_super *osb,
721 handle_t *handle,
722 struct inode *parent,
723 struct inode *inode,
724 struct buffer_head *fe_bh,
725 struct ocfs2_alloc_context *data_ac)
726{
727 int status;
728 struct buffer_head *new_bh = NULL;
729 struct ocfs2_dir_entry *de = NULL;
730
731 mlog_entry_void();
732
733 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
734 data_ac, NULL, &new_bh);
735 if (status < 0) {
736 mlog_errno(status);
737 goto bail;
738 }
739
740 ocfs2_set_new_buffer_uptodate(inode, new_bh);
741
742 status = ocfs2_journal_access(handle, inode, new_bh,
743 OCFS2_JOURNAL_ACCESS_CREATE);
744 if (status < 0) {
745 mlog_errno(status);
746 goto bail;
747 }
748 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
749
750 de = (struct ocfs2_dir_entry *) new_bh->b_data;
751 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
752 de->name_len = 1;
753 de->rec_len =
754 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
755 strcpy(de->name, ".");
756 ocfs2_set_de_type(de, S_IFDIR);
757 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
758 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
759 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
760 OCFS2_DIR_REC_LEN(1));
761 de->name_len = 2;
762 strcpy(de->name, "..");
763 ocfs2_set_de_type(de, S_IFDIR);
764
765 status = ocfs2_journal_dirty(handle, new_bh);
766 if (status < 0) {
767 mlog_errno(status);
768 goto bail;
769 }
770
771 i_size_write(inode, inode->i_sb->s_blocksize);
772 inode->i_nlink = 2;
773 inode->i_blocks = ocfs2_inode_sector_count(inode);
774 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
775 if (status < 0) {
776 mlog_errno(status);
777 goto bail;
778 }
779
780 status = 0;
781bail:
782 if (new_bh)
783 brelse(new_bh);
784
785 mlog_exit(status);
786 return status;
787}
788
Mark Fashehccd979b2005-12-15 14:31:24 -0800789/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700790static int ocfs2_do_extend_dir(struct super_block *sb,
791 handle_t *handle,
792 struct inode *dir,
793 struct buffer_head *parent_fe_bh,
794 struct ocfs2_alloc_context *data_ac,
795 struct ocfs2_alloc_context *meta_ac,
796 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -0800797{
798 int status;
799 int extend;
Mark Fasheh8110b072007-03-22 16:53:23 -0700800 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800801
802 spin_lock(&OCFS2_I(dir)->ip_lock);
803 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
804 spin_unlock(&OCFS2_I(dir)->ip_lock);
805
806 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -0800807 u32 offset = OCFS2_I(dir)->ip_clusters;
808
809 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800810 1, 0, parent_fe_bh, handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800811 data_ac, meta_ac, NULL);
812 BUG_ON(status == -EAGAIN);
813 if (status < 0) {
814 mlog_errno(status);
815 goto bail;
816 }
817 }
818
Mark Fasheh8110b072007-03-22 16:53:23 -0700819 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
820 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800821 if (status < 0) {
822 mlog_errno(status);
823 goto bail;
824 }
825
826 *new_bh = sb_getblk(sb, p_blkno);
827 if (!*new_bh) {
828 status = -EIO;
829 mlog_errno(status);
830 goto bail;
831 }
832 status = 0;
833bail:
834 mlog_exit(status);
835 return status;
836}
837
838/* assumes you already have a cluster lock on the directory. */
839static int ocfs2_extend_dir(struct ocfs2_super *osb,
840 struct inode *dir,
841 struct buffer_head *parent_fe_bh,
842 struct buffer_head **new_de_bh)
843{
844 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -0700845 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800846 loff_t dir_i_size;
847 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
848 struct ocfs2_alloc_context *data_ac = NULL;
849 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700850 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 struct buffer_head *new_bh = NULL;
852 struct ocfs2_dir_entry * de;
853 struct super_block *sb = osb->sb;
854
855 mlog_entry_void();
856
857 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -0800858 mlog(0, "extending dir %llu (i_size = %lld)\n",
859 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800860
Mark Fashehccd979b2005-12-15 14:31:24 -0800861 /* dir->i_size is always block aligned. */
862 spin_lock(&OCFS2_I(dir)->ip_lock);
863 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
864 spin_unlock(&OCFS2_I(dir)->ip_lock);
865 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
866 if (num_free_extents < 0) {
867 status = num_free_extents;
868 mlog_errno(status);
869 goto bail;
870 }
871
872 if (!num_free_extents) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700873 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800874 if (status < 0) {
875 if (status != -ENOSPC)
876 mlog_errno(status);
877 goto bail;
878 }
879 }
880
Mark Fashehda5cbf22006-10-06 18:34:35 -0700881 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 if (status < 0) {
883 if (status != -ENOSPC)
884 mlog_errno(status);
885 goto bail;
886 }
887
888 credits = ocfs2_calc_extend_credits(sb, fe, 1);
889 } else {
890 spin_unlock(&OCFS2_I(dir)->ip_lock);
891 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
892 }
893
Joel Beckeree19a772007-03-28 18:27:07 -0700894 down_write(&OCFS2_I(dir)->ip_alloc_sem);
895 drop_alloc_sem = 1;
896
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700897 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 if (IS_ERR(handle)) {
899 status = PTR_ERR(handle);
900 handle = NULL;
901 mlog_errno(status);
902 goto bail;
903 }
904
905 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
906 data_ac, meta_ac, &new_bh);
907 if (status < 0) {
908 mlog_errno(status);
909 goto bail;
910 }
911
912 ocfs2_set_new_buffer_uptodate(dir, new_bh);
913
914 status = ocfs2_journal_access(handle, dir, new_bh,
915 OCFS2_JOURNAL_ACCESS_CREATE);
916 if (status < 0) {
917 mlog_errno(status);
918 goto bail;
919 }
920 memset(new_bh->b_data, 0, sb->s_blocksize);
921 de = (struct ocfs2_dir_entry *) new_bh->b_data;
922 de->inode = 0;
923 de->rec_len = cpu_to_le16(sb->s_blocksize);
924 status = ocfs2_journal_dirty(handle, new_bh);
925 if (status < 0) {
926 mlog_errno(status);
927 goto bail;
928 }
929
930 dir_i_size += dir->i_sb->s_blocksize;
931 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700932 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800933 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
934 if (status < 0) {
935 mlog_errno(status);
936 goto bail;
937 }
938
939 *new_de_bh = new_bh;
940 get_bh(*new_de_bh);
941bail:
Joel Beckeree19a772007-03-28 18:27:07 -0700942 if (drop_alloc_sem)
943 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -0800944 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700945 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800946
947 if (data_ac)
948 ocfs2_free_alloc_context(data_ac);
949 if (meta_ac)
950 ocfs2_free_alloc_context(meta_ac);
951
952 if (new_bh)
953 brelse(new_bh);
954
955 mlog_exit(status);
956 return status;
957}
958
959/*
960 * Search the dir for a good spot, extending it if necessary. The
961 * block containing an appropriate record is returned in ret_de_bh.
962 */
963int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
964 struct inode *dir,
965 struct buffer_head *parent_fe_bh,
966 const char *name,
967 int namelen,
968 struct buffer_head **ret_de_bh)
969{
970 unsigned long offset;
971 struct buffer_head * bh = NULL;
972 unsigned short rec_len;
973 struct ocfs2_dinode *fe;
974 struct ocfs2_dir_entry *de;
975 struct super_block *sb;
976 int status;
977
978 mlog_entry_void();
979
Mark Fashehb06970532006-03-03 10:24:33 -0800980 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
981 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800982
983 BUG_ON(!S_ISDIR(dir->i_mode));
984 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
985 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
986
987 sb = dir->i_sb;
988
989 if (!namelen) {
990 status = -EINVAL;
991 mlog_errno(status);
992 goto bail;
993 }
994
995 bh = ocfs2_bread(dir, 0, &status, 0);
996 if (!bh) {
997 mlog_errno(status);
998 goto bail;
999 }
1000
1001 rec_len = OCFS2_DIR_REC_LEN(namelen);
1002 offset = 0;
1003 de = (struct ocfs2_dir_entry *) bh->b_data;
1004 while (1) {
1005 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1006 brelse(bh);
1007 bh = NULL;
1008
1009 if (i_size_read(dir) <= offset) {
1010 status = ocfs2_extend_dir(osb,
1011 dir,
1012 parent_fe_bh,
1013 &bh);
1014 if (status < 0) {
1015 mlog_errno(status);
1016 goto bail;
1017 }
1018 BUG_ON(!bh);
1019 *ret_de_bh = bh;
1020 get_bh(*ret_de_bh);
1021 goto bail;
1022 }
1023 bh = ocfs2_bread(dir,
1024 offset >> sb->s_blocksize_bits,
1025 &status,
1026 0);
1027 if (!bh) {
1028 mlog_errno(status);
1029 goto bail;
1030 }
1031 /* move to next block */
1032 de = (struct ocfs2_dir_entry *) bh->b_data;
1033 }
1034 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1035 status = -ENOENT;
1036 goto bail;
1037 }
1038 if (ocfs2_match(namelen, name, de)) {
1039 status = -EEXIST;
1040 goto bail;
1041 }
1042 if (((le64_to_cpu(de->inode) == 0) &&
1043 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1044 (le16_to_cpu(de->rec_len) >=
1045 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1046 /* Ok, we found a spot. Return this bh and let
1047 * the caller actually fill it in. */
1048 *ret_de_bh = bh;
1049 get_bh(*ret_de_bh);
1050 status = 0;
1051 goto bail;
1052 }
1053 offset += le16_to_cpu(de->rec_len);
1054 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1055 }
1056
1057 status = 0;
1058bail:
1059 if (bh)
1060 brelse(bh);
1061
1062 mlog_exit(status);
1063 return status;
1064}