blob: 8e0ae022b2e9d2b4b9485750f9d0ffadc74a1157 [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
84int ocfs2_check_dir_entry(struct inode * dir,
85 struct ocfs2_dir_entry * de,
86 struct buffer_head * bh,
87 unsigned long offset)
88{
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 Fashehccd979b2005-12-15 14:31:24 -0800417/*
418 * ocfs2_readdir()
419 *
420 */
421int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
422{
423 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700424 unsigned long offset, blk, last_ra_blk = 0;
425 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 struct buffer_head * bh, * tmp;
427 struct ocfs2_dir_entry * de;
428 int err;
Josef Sipekd28c9172006-12-08 02:37:25 -0800429 struct inode *inode = filp->f_path.dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800430 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700431 unsigned int ra_sectors = 16;
Tiger Yang25899de2006-11-15 15:49:02 +0800432 int lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800433
Mark Fashehb06970532006-03-03 10:24:33 -0800434 mlog_entry("dirino=%llu\n",
435 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800436
437 stored = 0;
438 bh = NULL;
439
Tiger Yang25899de2006-11-15 15:49:02 +0800440 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
441 if (lock_level && error >= 0) {
442 /* We release EX lock which used to update atime
443 * and get PR lock again to reduce contention
444 * on commonly accessed directories. */
445 ocfs2_meta_unlock(inode, 1);
446 lock_level = 0;
447 error = ocfs2_meta_lock(inode, NULL, 0);
448 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800449 if (error < 0) {
450 if (error != -ENOENT)
451 mlog_errno(error);
452 /* we haven't got any yet, so propagate the error. */
453 stored = error;
Mark Fashehaa958872006-04-21 13:49:02 -0700454 goto bail_nolock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800455 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800456
457 offset = filp->f_pos & (sb->s_blocksize - 1);
458
459 while (!error && !stored && filp->f_pos < i_size_read(inode)) {
460 blk = (filp->f_pos) >> sb->s_blocksize_bits;
461 bh = ocfs2_bread(inode, blk, &err, 0);
462 if (!bh) {
Mark Fashehb06970532006-03-03 10:24:33 -0800463 mlog(ML_ERROR,
464 "directory #%llu contains a hole at offset %lld\n",
465 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 filp->f_pos);
467 filp->f_pos += sb->s_blocksize - offset;
468 continue;
469 }
470
Mark Fashehaa958872006-04-21 13:49:02 -0700471 /* The idea here is to begin with 8k read-ahead and to stay
472 * 4k ahead of our current position.
473 *
474 * TODO: Use the pagecache for this. We just need to
475 * make sure it's cluster-safe... */
476 if (!last_ra_blk
477 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
478 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800479 i > 0; i--) {
480 tmp = ocfs2_bread(inode, ++blk, &err, 1);
481 if (tmp)
482 brelse(tmp);
483 }
Mark Fashehaa958872006-04-21 13:49:02 -0700484 last_ra_blk = blk;
485 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800486 }
487
488revalidate:
489 /* If the dir block has changed since the last call to
490 * readdir(2), then we might be pointing to an invalid
491 * dirent right now. Scan from the start of the block
492 * to make sure. */
493 if (filp->f_version != inode->i_version) {
494 for (i = 0; i < sb->s_blocksize && i < offset; ) {
495 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
496 /* It's too expensive to do a full
497 * dirent test each time round this
498 * loop, but we do have to test at
499 * least that it is non-zero. A
500 * failure will be detected in the
501 * dirent test below. */
502 if (le16_to_cpu(de->rec_len) <
503 OCFS2_DIR_REC_LEN(1))
504 break;
505 i += le16_to_cpu(de->rec_len);
506 }
507 offset = i;
508 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
509 | offset;
510 filp->f_version = inode->i_version;
511 }
512
513 while (!error && filp->f_pos < i_size_read(inode)
514 && offset < sb->s_blocksize) {
515 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
516 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
517 /* On error, skip the f_pos to the
518 next block. */
519 filp->f_pos = (filp->f_pos |
520 (sb->s_blocksize - 1)) + 1;
521 brelse(bh);
522 goto bail;
523 }
524 offset += le16_to_cpu(de->rec_len);
525 if (le64_to_cpu(de->inode)) {
526 /* We might block in the next section
527 * if the data destination is
528 * currently swapped out. So, use a
529 * version stamp to detect whether or
530 * not the directory has been modified
531 * during the copy operation.
532 */
533 unsigned long version = filp->f_version;
534 unsigned char d_type = DT_UNKNOWN;
535
536 if (de->file_type < OCFS2_FT_MAX)
537 d_type = ocfs2_filetype_table[de->file_type];
538 error = filldir(dirent, de->name,
539 de->name_len,
540 filp->f_pos,
541 ino_from_blkno(sb, le64_to_cpu(de->inode)),
542 d_type);
543 if (error)
544 break;
545 if (version != filp->f_version)
546 goto revalidate;
547 stored ++;
548 }
549 filp->f_pos += le16_to_cpu(de->rec_len);
550 }
551 offset = 0;
552 brelse(bh);
553 }
554
555 stored = 0;
556bail:
Tiger Yang25899de2006-11-15 15:49:02 +0800557 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800558
Mark Fashehaa958872006-04-21 13:49:02 -0700559bail_nolock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800560 mlog_exit(stored);
561
562 return stored;
563}
564
565/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800566 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800567 */
568int ocfs2_find_files_on_disk(const char *name,
569 int namelen,
570 u64 *blkno,
571 struct inode *inode,
572 struct buffer_head **dirent_bh,
573 struct ocfs2_dir_entry **dirent)
574{
575 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800576
Joel Becker2b388c62006-05-10 18:28:59 -0700577 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
578 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800579
580 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
581 if (!*dirent_bh || !*dirent) {
582 status = -ENOENT;
583 goto leave;
584 }
585
586 *blkno = le64_to_cpu((*dirent)->inode);
587
588 status = 0;
589leave:
590 if (status < 0) {
591 *dirent = NULL;
592 if (*dirent_bh) {
593 brelse(*dirent_bh);
594 *dirent_bh = NULL;
595 }
596 }
597
598 mlog_exit(status);
599 return status;
600}
601
602/* Check for a name within a directory.
603 *
604 * Return 0 if the name does not exist
605 * Return -EEXIST if the directory contains the name
606 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800607 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800608 */
609int ocfs2_check_dir_for_entry(struct inode *dir,
610 const char *name,
611 int namelen)
612{
613 int ret;
614 struct buffer_head *dirent_bh = NULL;
615 struct ocfs2_dir_entry *dirent = NULL;
616
Mark Fashehb06970532006-03-03 10:24:33 -0800617 mlog_entry("dir %llu, name '%.*s'\n",
618 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800619
620 ret = -EEXIST;
621 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
622 if (dirent_bh)
623 goto bail;
624
625 ret = 0;
626bail:
627 if (dirent_bh)
628 brelse(dirent_bh);
629
630 mlog_exit(ret);
631 return ret;
632}
633
634/*
635 * routine to check that the specified directory is empty (for rmdir)
636 */
637int ocfs2_empty_dir(struct inode *inode)
638{
639 unsigned long offset;
640 struct buffer_head * bh;
641 struct ocfs2_dir_entry * de, * de1;
642 struct super_block * sb;
643 int err;
644
645 sb = inode->i_sb;
646 if ((i_size_read(inode) <
647 (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
648 !(bh = ocfs2_bread(inode, 0, &err, 0))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800649 mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
650 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 return 1;
652 }
653
654 de = (struct ocfs2_dir_entry *) bh->b_data;
655 de1 = (struct ocfs2_dir_entry *)
656 ((char *)de + le16_to_cpu(de->rec_len));
657 if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
658 !le64_to_cpu(de1->inode) ||
659 strcmp(".", de->name) ||
660 strcmp("..", de1->name)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800661 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
662 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800663 brelse(bh);
664 return 1;
665 }
666 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
667 de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
668 while (offset < i_size_read(inode) ) {
669 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
670 brelse(bh);
671 bh = ocfs2_bread(inode,
672 offset >> sb->s_blocksize_bits, &err, 0);
673 if (!bh) {
Mark Fashehb06970532006-03-03 10:24:33 -0800674 mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
675 (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 offset += sb->s_blocksize;
677 continue;
678 }
679 de = (struct ocfs2_dir_entry *) bh->b_data;
680 }
681 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
682 brelse(bh);
683 return 1;
684 }
685 if (le64_to_cpu(de->inode)) {
686 brelse(bh);
687 return 0;
688 }
689 offset += le16_to_cpu(de->rec_len);
690 de = (struct ocfs2_dir_entry *)
691 ((char *)de + le16_to_cpu(de->rec_len));
692 }
693 brelse(bh);
694 return 1;
695}
696
Mark Fasheh316f4b92007-09-07 18:21:26 -0700697int ocfs2_fill_new_dir(struct ocfs2_super *osb,
698 handle_t *handle,
699 struct inode *parent,
700 struct inode *inode,
701 struct buffer_head *fe_bh,
702 struct ocfs2_alloc_context *data_ac)
703{
704 int status;
705 struct buffer_head *new_bh = NULL;
706 struct ocfs2_dir_entry *de = NULL;
707
708 mlog_entry_void();
709
710 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
711 data_ac, NULL, &new_bh);
712 if (status < 0) {
713 mlog_errno(status);
714 goto bail;
715 }
716
717 ocfs2_set_new_buffer_uptodate(inode, new_bh);
718
719 status = ocfs2_journal_access(handle, inode, new_bh,
720 OCFS2_JOURNAL_ACCESS_CREATE);
721 if (status < 0) {
722 mlog_errno(status);
723 goto bail;
724 }
725 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
726
727 de = (struct ocfs2_dir_entry *) new_bh->b_data;
728 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
729 de->name_len = 1;
730 de->rec_len =
731 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
732 strcpy(de->name, ".");
733 ocfs2_set_de_type(de, S_IFDIR);
734 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
735 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
736 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
737 OCFS2_DIR_REC_LEN(1));
738 de->name_len = 2;
739 strcpy(de->name, "..");
740 ocfs2_set_de_type(de, S_IFDIR);
741
742 status = ocfs2_journal_dirty(handle, new_bh);
743 if (status < 0) {
744 mlog_errno(status);
745 goto bail;
746 }
747
748 i_size_write(inode, inode->i_sb->s_blocksize);
749 inode->i_nlink = 2;
750 inode->i_blocks = ocfs2_inode_sector_count(inode);
751 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
752 if (status < 0) {
753 mlog_errno(status);
754 goto bail;
755 }
756
757 status = 0;
758bail:
759 if (new_bh)
760 brelse(new_bh);
761
762 mlog_exit(status);
763 return status;
764}
765
Mark Fashehccd979b2005-12-15 14:31:24 -0800766/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700767static int ocfs2_do_extend_dir(struct super_block *sb,
768 handle_t *handle,
769 struct inode *dir,
770 struct buffer_head *parent_fe_bh,
771 struct ocfs2_alloc_context *data_ac,
772 struct ocfs2_alloc_context *meta_ac,
773 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -0800774{
775 int status;
776 int extend;
Mark Fasheh8110b072007-03-22 16:53:23 -0700777 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800778
779 spin_lock(&OCFS2_I(dir)->ip_lock);
780 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
781 spin_unlock(&OCFS2_I(dir)->ip_lock);
782
783 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -0800784 u32 offset = OCFS2_I(dir)->ip_clusters;
785
786 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800787 1, 0, parent_fe_bh, handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 data_ac, meta_ac, NULL);
789 BUG_ON(status == -EAGAIN);
790 if (status < 0) {
791 mlog_errno(status);
792 goto bail;
793 }
794 }
795
Mark Fasheh8110b072007-03-22 16:53:23 -0700796 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
797 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 if (status < 0) {
799 mlog_errno(status);
800 goto bail;
801 }
802
803 *new_bh = sb_getblk(sb, p_blkno);
804 if (!*new_bh) {
805 status = -EIO;
806 mlog_errno(status);
807 goto bail;
808 }
809 status = 0;
810bail:
811 mlog_exit(status);
812 return status;
813}
814
815/* assumes you already have a cluster lock on the directory. */
816static int ocfs2_extend_dir(struct ocfs2_super *osb,
817 struct inode *dir,
818 struct buffer_head *parent_fe_bh,
819 struct buffer_head **new_de_bh)
820{
821 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -0700822 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800823 loff_t dir_i_size;
824 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
825 struct ocfs2_alloc_context *data_ac = NULL;
826 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700827 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800828 struct buffer_head *new_bh = NULL;
829 struct ocfs2_dir_entry * de;
830 struct super_block *sb = osb->sb;
831
832 mlog_entry_void();
833
834 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -0800835 mlog(0, "extending dir %llu (i_size = %lld)\n",
836 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800837
Mark Fashehccd979b2005-12-15 14:31:24 -0800838 /* dir->i_size is always block aligned. */
839 spin_lock(&OCFS2_I(dir)->ip_lock);
840 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
841 spin_unlock(&OCFS2_I(dir)->ip_lock);
842 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
843 if (num_free_extents < 0) {
844 status = num_free_extents;
845 mlog_errno(status);
846 goto bail;
847 }
848
849 if (!num_free_extents) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700850 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 if (status < 0) {
852 if (status != -ENOSPC)
853 mlog_errno(status);
854 goto bail;
855 }
856 }
857
Mark Fashehda5cbf22006-10-06 18:34:35 -0700858 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800859 if (status < 0) {
860 if (status != -ENOSPC)
861 mlog_errno(status);
862 goto bail;
863 }
864
865 credits = ocfs2_calc_extend_credits(sb, fe, 1);
866 } else {
867 spin_unlock(&OCFS2_I(dir)->ip_lock);
868 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
869 }
870
Joel Beckeree19a772007-03-28 18:27:07 -0700871 down_write(&OCFS2_I(dir)->ip_alloc_sem);
872 drop_alloc_sem = 1;
873
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700874 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800875 if (IS_ERR(handle)) {
876 status = PTR_ERR(handle);
877 handle = NULL;
878 mlog_errno(status);
879 goto bail;
880 }
881
882 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
883 data_ac, meta_ac, &new_bh);
884 if (status < 0) {
885 mlog_errno(status);
886 goto bail;
887 }
888
889 ocfs2_set_new_buffer_uptodate(dir, new_bh);
890
891 status = ocfs2_journal_access(handle, dir, new_bh,
892 OCFS2_JOURNAL_ACCESS_CREATE);
893 if (status < 0) {
894 mlog_errno(status);
895 goto bail;
896 }
897 memset(new_bh->b_data, 0, sb->s_blocksize);
898 de = (struct ocfs2_dir_entry *) new_bh->b_data;
899 de->inode = 0;
900 de->rec_len = cpu_to_le16(sb->s_blocksize);
901 status = ocfs2_journal_dirty(handle, new_bh);
902 if (status < 0) {
903 mlog_errno(status);
904 goto bail;
905 }
906
907 dir_i_size += dir->i_sb->s_blocksize;
908 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700909 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
911 if (status < 0) {
912 mlog_errno(status);
913 goto bail;
914 }
915
916 *new_de_bh = new_bh;
917 get_bh(*new_de_bh);
918bail:
Joel Beckeree19a772007-03-28 18:27:07 -0700919 if (drop_alloc_sem)
920 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -0800921 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700922 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800923
924 if (data_ac)
925 ocfs2_free_alloc_context(data_ac);
926 if (meta_ac)
927 ocfs2_free_alloc_context(meta_ac);
928
929 if (new_bh)
930 brelse(new_bh);
931
932 mlog_exit(status);
933 return status;
934}
935
936/*
937 * Search the dir for a good spot, extending it if necessary. The
938 * block containing an appropriate record is returned in ret_de_bh.
939 */
940int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
941 struct inode *dir,
942 struct buffer_head *parent_fe_bh,
943 const char *name,
944 int namelen,
945 struct buffer_head **ret_de_bh)
946{
947 unsigned long offset;
948 struct buffer_head * bh = NULL;
949 unsigned short rec_len;
950 struct ocfs2_dinode *fe;
951 struct ocfs2_dir_entry *de;
952 struct super_block *sb;
953 int status;
954
955 mlog_entry_void();
956
Mark Fashehb06970532006-03-03 10:24:33 -0800957 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
958 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800959
960 BUG_ON(!S_ISDIR(dir->i_mode));
961 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
962 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
963
964 sb = dir->i_sb;
965
966 if (!namelen) {
967 status = -EINVAL;
968 mlog_errno(status);
969 goto bail;
970 }
971
972 bh = ocfs2_bread(dir, 0, &status, 0);
973 if (!bh) {
974 mlog_errno(status);
975 goto bail;
976 }
977
978 rec_len = OCFS2_DIR_REC_LEN(namelen);
979 offset = 0;
980 de = (struct ocfs2_dir_entry *) bh->b_data;
981 while (1) {
982 if ((char *)de >= sb->s_blocksize + bh->b_data) {
983 brelse(bh);
984 bh = NULL;
985
986 if (i_size_read(dir) <= offset) {
987 status = ocfs2_extend_dir(osb,
988 dir,
989 parent_fe_bh,
990 &bh);
991 if (status < 0) {
992 mlog_errno(status);
993 goto bail;
994 }
995 BUG_ON(!bh);
996 *ret_de_bh = bh;
997 get_bh(*ret_de_bh);
998 goto bail;
999 }
1000 bh = ocfs2_bread(dir,
1001 offset >> sb->s_blocksize_bits,
1002 &status,
1003 0);
1004 if (!bh) {
1005 mlog_errno(status);
1006 goto bail;
1007 }
1008 /* move to next block */
1009 de = (struct ocfs2_dir_entry *) bh->b_data;
1010 }
1011 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1012 status = -ENOENT;
1013 goto bail;
1014 }
1015 if (ocfs2_match(namelen, name, de)) {
1016 status = -EEXIST;
1017 goto bail;
1018 }
1019 if (((le64_to_cpu(de->inode) == 0) &&
1020 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1021 (le16_to_cpu(de->rec_len) >=
1022 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1023 /* Ok, we found a spot. Return this bh and let
1024 * the caller actually fill it in. */
1025 *ret_de_bh = bh;
1026 get_bh(*ret_de_bh);
1027 status = 0;
1028 goto bail;
1029 }
1030 offset += le16_to_cpu(de->rec_len);
1031 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1032 }
1033
1034 status = 0;
1035bail:
1036 if (bh)
1037 brelse(bh);
1038
1039 mlog_exit(status);
1040 return status;
1041}