blob: d83cff95759e31ffdde42439212d42f047f40920 [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,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -070075 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -080076 struct buffer_head **new_de_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -070077static int ocfs2_do_extend_dir(struct super_block *sb,
78 handle_t *handle,
79 struct inode *dir,
80 struct buffer_head *parent_fe_bh,
81 struct ocfs2_alloc_context *data_ac,
82 struct ocfs2_alloc_context *meta_ac,
83 struct buffer_head **new_bh);
84
Mark Fasheh23193e52007-09-12 13:01:18 -070085/*
86 * bh passed here can be an inode block or a dir data block, depending
87 * on the inode inline data flag.
88 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -070089static int ocfs2_check_dir_entry(struct inode * dir,
90 struct ocfs2_dir_entry * de,
91 struct buffer_head * bh,
92 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -070093{
94 const char *error_msg = NULL;
95 const int rlen = le16_to_cpu(de->rec_len);
96
97 if (rlen < OCFS2_DIR_REC_LEN(1))
98 error_msg = "rec_len is smaller than minimal";
99 else if (rlen % 4 != 0)
100 error_msg = "rec_len % 4 != 0";
101 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
102 error_msg = "rec_len is too small for name_len";
103 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
104 error_msg = "directory entry across blocks";
105
106 if (error_msg != NULL)
107 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
108 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
109 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
110 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
111 de->name_len);
112 return error_msg == NULL ? 1 : 0;
113}
114
115static inline int ocfs2_match(int len,
116 const char * const name,
117 struct ocfs2_dir_entry *de)
118{
119 if (len != de->name_len)
120 return 0;
121 if (!de->inode)
122 return 0;
123 return !memcmp(name, de->name, len);
124}
125
126/*
127 * Returns 0 if not found, -1 on failure, and 1 on success
128 */
129static int inline ocfs2_search_dirblock(struct buffer_head *bh,
130 struct inode *dir,
131 const char *name, int namelen,
132 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700133 char *first_de,
134 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700135 struct ocfs2_dir_entry **res_dir)
136{
137 struct ocfs2_dir_entry *de;
138 char *dlimit, *de_buf;
139 int de_len;
140 int ret = 0;
141
142 mlog_entry_void();
143
Mark Fasheh23193e52007-09-12 13:01:18 -0700144 de_buf = first_de;
145 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700146
147 while (de_buf < dlimit) {
148 /* this code is executed quadratically often */
149 /* do minimal checking `by hand' */
150
151 de = (struct ocfs2_dir_entry *) de_buf;
152
153 if (de_buf + namelen <= dlimit &&
154 ocfs2_match(namelen, name, de)) {
155 /* found a match - just to be sure, do a full check */
156 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
157 ret = -1;
158 goto bail;
159 }
160 *res_dir = de;
161 ret = 1;
162 goto bail;
163 }
164
165 /* prevent looping on a bad block */
166 de_len = le16_to_cpu(de->rec_len);
167 if (de_len <= 0) {
168 ret = -1;
169 goto bail;
170 }
171
172 de_buf += de_len;
173 offset += de_len;
174 }
175
176bail:
177 mlog_exit(ret);
178 return ret;
179}
180
Mark Fasheh23193e52007-09-12 13:01:18 -0700181static struct buffer_head *ocfs2_find_entry_id(const char *name,
182 int namelen,
183 struct inode *dir,
184 struct ocfs2_dir_entry **res_dir)
185{
186 int ret, found;
187 struct buffer_head *di_bh = NULL;
188 struct ocfs2_dinode *di;
189 struct ocfs2_inline_data *data;
190
Joel Beckerb657c952008-11-13 14:49:11 -0800191 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700192 if (ret) {
193 mlog_errno(ret);
194 goto out;
195 }
196
197 di = (struct ocfs2_dinode *)di_bh->b_data;
198 data = &di->id2.i_data;
199
200 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
201 data->id_data, i_size_read(dir), res_dir);
202 if (found == 1)
203 return di_bh;
204
205 brelse(di_bh);
206out:
207 return NULL;
208}
209
Joel Beckera22305c2008-11-13 14:49:17 -0800210static int ocfs2_validate_dir_block(struct super_block *sb,
211 struct buffer_head *bh)
212{
213 /*
214 * Nothing yet. We don't validate dirents here, that's handled
215 * in-place when the code walks them.
216 */
Joel Becker970e4932008-11-13 14:49:19 -0800217 mlog(0, "Validating dirblock %llu\n",
218 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800219
220 return 0;
221}
222
223/*
224 * This function forces all errors to -EIO for consistency with its
225 * predecessor, ocfs2_bread(). We haven't audited what returning the
226 * real error codes would do to callers. We log the real codes with
227 * mlog_errno() before we squash them.
228 */
229static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
230 struct buffer_head **bh, int flags)
231{
232 int rc = 0;
233 struct buffer_head *tmp = *bh;
Joel Beckera22305c2008-11-13 14:49:17 -0800234
Joel Becker511308d2008-11-13 14:49:21 -0800235 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
236 ocfs2_validate_dir_block);
237 if (rc)
Joel Beckera22305c2008-11-13 14:49:17 -0800238 mlog_errno(rc);
Joel Beckera22305c2008-11-13 14:49:17 -0800239
Joel Becker511308d2008-11-13 14:49:21 -0800240 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
241 if (!rc && !*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800242 *bh = tmp;
243
Joel Beckera22305c2008-11-13 14:49:17 -0800244 return rc ? -EIO : 0;
245}
246
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200247static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
248 struct inode *dir,
249 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700250{
251 struct super_block *sb;
252 struct buffer_head *bh_use[NAMEI_RA_SIZE];
253 struct buffer_head *bh, *ret = NULL;
254 unsigned long start, block, b;
255 int ra_max = 0; /* Number of bh's in the readahead
256 buffer, bh_use[] */
257 int ra_ptr = 0; /* Current index into readahead
258 buffer */
259 int num = 0;
260 int nblocks, i, err;
261
262 mlog_entry_void();
263
Mark Fasheh316f4b92007-09-07 18:21:26 -0700264 sb = dir->i_sb;
265
266 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
267 start = OCFS2_I(dir)->ip_dir_start_lookup;
268 if (start >= nblocks)
269 start = 0;
270 block = start;
271
272restart:
273 do {
274 /*
275 * We deal with the read-ahead logic here.
276 */
277 if (ra_ptr >= ra_max) {
278 /* Refill the readahead buffer */
279 ra_ptr = 0;
280 b = block;
281 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
282 /*
283 * Terminate if we reach the end of the
284 * directory and must wrap, or if our
285 * search has finished at this block.
286 */
287 if (b >= nblocks || (num && block == start)) {
288 bh_use[ra_max] = NULL;
289 break;
290 }
291 num++;
292
Joel Beckera22305c2008-11-13 14:49:17 -0800293 bh = NULL;
294 err = ocfs2_read_dir_block(dir, b++, &bh,
295 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700296 bh_use[ra_max] = bh;
297 }
298 }
299 if ((bh = bh_use[ra_ptr++]) == NULL)
300 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800301 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700302 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800303 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700304 ocfs2_error(dir->i_sb, "reading directory %llu, "
305 "offset %lu\n",
306 (unsigned long long)OCFS2_I(dir)->ip_blkno,
307 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700308 goto next;
309 }
310 i = ocfs2_search_dirblock(bh, dir, name, namelen,
311 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700312 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700313 res_dir);
314 if (i == 1) {
315 OCFS2_I(dir)->ip_dir_start_lookup = block;
316 ret = bh;
317 goto cleanup_and_exit;
318 } else {
319 brelse(bh);
320 if (i < 0)
321 goto cleanup_and_exit;
322 }
323 next:
324 if (++block >= nblocks)
325 block = 0;
326 } while (block != start);
327
328 /*
329 * If the directory has grown while we were searching, then
330 * search the last part of the directory before giving up.
331 */
332 block = nblocks;
333 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
334 if (block < nblocks) {
335 start = 0;
336 goto restart;
337 }
338
339cleanup_and_exit:
340 /* Clean up the read-ahead blocks */
341 for (; ra_ptr < ra_max; ra_ptr++)
342 brelse(bh_use[ra_ptr]);
343
344 mlog_exit_ptr(ret);
345 return ret;
346}
347
Mark Fasheh23193e52007-09-12 13:01:18 -0700348/*
349 * Try to find an entry of the provided name within 'dir'.
350 *
351 * If nothing was found, NULL is returned. Otherwise, a buffer_head
352 * and pointer to the dir entry are passed back.
353 *
354 * Caller can NOT assume anything about the contents of the
355 * buffer_head - it is passed back only so that it can be passed into
356 * any one of the manipulation functions (add entry, delete entry,
357 * etc). As an example, bh in the extent directory case is a data
358 * block, in the inline-data case it actually points to an inode.
359 */
360struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
361 struct inode *dir,
362 struct ocfs2_dir_entry **res_dir)
363{
364 *res_dir = NULL;
365
366 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
367 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
368
369 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
370}
371
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700372/*
373 * Update inode number and type of a previously found directory entry.
374 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700375int ocfs2_update_entry(struct inode *dir, handle_t *handle,
376 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
377 struct inode *new_entry_inode)
378{
379 int ret;
380
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700381 /*
382 * The same code works fine for both inline-data and extent
383 * based directories, so no need to split this up.
384 */
385
Mark Fasheh38760e22007-09-11 17:21:56 -0700386 ret = ocfs2_journal_access(handle, dir, de_bh,
387 OCFS2_JOURNAL_ACCESS_WRITE);
388 if (ret) {
389 mlog_errno(ret);
390 goto out;
391 }
392
393 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
394 ocfs2_set_de_type(de, new_entry_inode->i_mode);
395
396 ocfs2_journal_dirty(handle, de_bh);
397
398out:
399 return ret;
400}
401
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700402static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
403 struct ocfs2_dir_entry *de_del,
404 struct buffer_head *bh, char *first_de,
405 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700406{
407 struct ocfs2_dir_entry *de, *pde;
408 int i, status = -ENOENT;
409
410 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
411
412 i = 0;
413 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700414 de = (struct ocfs2_dir_entry *) first_de;
415 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700416 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
417 status = -EIO;
418 mlog_errno(status);
419 goto bail;
420 }
421 if (de == de_del) {
422 status = ocfs2_journal_access(handle, dir, bh,
423 OCFS2_JOURNAL_ACCESS_WRITE);
424 if (status < 0) {
425 status = -EIO;
426 mlog_errno(status);
427 goto bail;
428 }
429 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100430 le16_add_cpu(&pde->rec_len,
431 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700432 else
433 de->inode = 0;
434 dir->i_version++;
435 status = ocfs2_journal_dirty(handle, bh);
436 goto bail;
437 }
438 i += le16_to_cpu(de->rec_len);
439 pde = de;
440 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
441 }
442bail:
443 mlog_exit(status);
444 return status;
445}
446
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700447static inline int ocfs2_delete_entry_id(handle_t *handle,
448 struct inode *dir,
449 struct ocfs2_dir_entry *de_del,
450 struct buffer_head *bh)
451{
452 int ret;
453 struct buffer_head *di_bh = NULL;
454 struct ocfs2_dinode *di;
455 struct ocfs2_inline_data *data;
456
Joel Beckerb657c952008-11-13 14:49:11 -0800457 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700458 if (ret) {
459 mlog_errno(ret);
460 goto out;
461 }
462
463 di = (struct ocfs2_dinode *)di_bh->b_data;
464 data = &di->id2.i_data;
465
466 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
467 i_size_read(dir));
468
469 brelse(di_bh);
470out:
471 return ret;
472}
473
474static inline int ocfs2_delete_entry_el(handle_t *handle,
475 struct inode *dir,
476 struct ocfs2_dir_entry *de_del,
477 struct buffer_head *bh)
478{
479 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
480 bh->b_size);
481}
482
483/*
484 * ocfs2_delete_entry deletes a directory entry by merging it with the
485 * previous entry
486 */
487int ocfs2_delete_entry(handle_t *handle,
488 struct inode *dir,
489 struct ocfs2_dir_entry *de_del,
490 struct buffer_head *bh)
491{
492 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
493 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
494
495 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
496}
497
Mark Fasheh8553cf42007-09-13 16:29:01 -0700498/*
499 * Check whether 'de' has enough room to hold an entry of
500 * 'new_rec_len' bytes.
501 */
502static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
503 unsigned int new_rec_len)
504{
505 unsigned int de_really_used;
506
507 /* Check whether this is an empty record with enough space */
508 if (le64_to_cpu(de->inode) == 0 &&
509 le16_to_cpu(de->rec_len) >= new_rec_len)
510 return 1;
511
512 /*
513 * Record might have free space at the end which we can
514 * use.
515 */
516 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
517 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
518 return 1;
519
520 return 0;
521}
522
Mark Fasheh316f4b92007-09-07 18:21:26 -0700523/* we don't always have a dentry for what we want to add, so people
524 * like orphan dir can call this instead.
525 *
526 * If you pass me insert_bh, I'll skip the search of the other dir
527 * blocks and put the record in there.
528 */
529int __ocfs2_add_entry(handle_t *handle,
530 struct inode *dir,
531 const char *name, int namelen,
532 struct inode *inode, u64 blkno,
533 struct buffer_head *parent_fe_bh,
534 struct buffer_head *insert_bh)
535{
536 unsigned long offset;
537 unsigned short rec_len;
538 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700539 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
540 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700541 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700542 unsigned int size = sb->s_blocksize;
543 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700544
545 mlog_entry_void();
546
Mark Fasheh316f4b92007-09-07 18:21:26 -0700547 if (!namelen)
548 return -EINVAL;
549
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700550 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
551 data_start = di->id2.i_data.id_data;
552 size = i_size_read(dir);
553
554 BUG_ON(insert_bh != parent_fe_bh);
555 }
556
Mark Fasheh316f4b92007-09-07 18:21:26 -0700557 rec_len = OCFS2_DIR_REC_LEN(namelen);
558 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700559 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700560 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700561 BUG_ON((char *)de >= (size + data_start));
562
Mark Fasheh316f4b92007-09-07 18:21:26 -0700563 /* These checks should've already been passed by the
564 * prepare function, but I guess we can leave them
565 * here anyway. */
566 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
567 retval = -ENOENT;
568 goto bail;
569 }
570 if (ocfs2_match(namelen, name, de)) {
571 retval = -EEXIST;
572 goto bail;
573 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700574
575 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700576 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
577 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
578 if (retval < 0) {
579 mlog_errno(retval);
580 goto bail;
581 }
582
583 status = ocfs2_journal_access(handle, dir, insert_bh,
584 OCFS2_JOURNAL_ACCESS_WRITE);
585 /* By now the buffer is marked for journaling */
586 offset += le16_to_cpu(de->rec_len);
587 if (le64_to_cpu(de->inode)) {
588 de1 = (struct ocfs2_dir_entry *)((char *) de +
589 OCFS2_DIR_REC_LEN(de->name_len));
590 de1->rec_len =
591 cpu_to_le16(le16_to_cpu(de->rec_len) -
592 OCFS2_DIR_REC_LEN(de->name_len));
593 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
594 de = de1;
595 }
596 de->file_type = OCFS2_FT_UNKNOWN;
597 if (blkno) {
598 de->inode = cpu_to_le64(blkno);
599 ocfs2_set_de_type(de, inode->i_mode);
600 } else
601 de->inode = 0;
602 de->name_len = namelen;
603 memcpy(de->name, name, namelen);
604
605 dir->i_version++;
606 status = ocfs2_journal_dirty(handle, insert_bh);
607 retval = 0;
608 goto bail;
609 }
610 offset += le16_to_cpu(de->rec_len);
611 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
612 }
613
614 /* when you think about it, the assert above should prevent us
615 * from ever getting here. */
616 retval = -ENOSPC;
617bail:
618
619 mlog_exit(retval);
620 return retval;
621}
622
Mark Fasheh23193e52007-09-12 13:01:18 -0700623static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700624 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700625 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700626 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700627{
628 int ret, i, filldir_ret;
629 unsigned long offset = *f_pos;
630 struct buffer_head *di_bh = NULL;
631 struct ocfs2_dinode *di;
632 struct ocfs2_inline_data *data;
633 struct ocfs2_dir_entry *de;
634
Joel Beckerb657c952008-11-13 14:49:11 -0800635 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700636 if (ret) {
637 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
638 (unsigned long long)OCFS2_I(inode)->ip_blkno);
639 goto out;
640 }
641
642 di = (struct ocfs2_dinode *)di_bh->b_data;
643 data = &di->id2.i_data;
644
645 while (*f_pos < i_size_read(inode)) {
646revalidate:
647 /* If the dir block has changed since the last call to
648 * readdir(2), then we might be pointing to an invalid
649 * dirent right now. Scan from the start of the block
650 * to make sure. */
651 if (*f_version != inode->i_version) {
652 for (i = 0; i < i_size_read(inode) && i < offset; ) {
653 de = (struct ocfs2_dir_entry *)
654 (data->id_data + i);
655 /* It's too expensive to do a full
656 * dirent test each time round this
657 * loop, but we do have to test at
658 * least that it is non-zero. A
659 * failure will be detected in the
660 * dirent test below. */
661 if (le16_to_cpu(de->rec_len) <
662 OCFS2_DIR_REC_LEN(1))
663 break;
664 i += le16_to_cpu(de->rec_len);
665 }
666 *f_pos = offset = i;
667 *f_version = inode->i_version;
668 }
669
670 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
671 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
672 /* On error, skip the f_pos to the end. */
673 *f_pos = i_size_read(inode);
674 goto out;
675 }
676 offset += le16_to_cpu(de->rec_len);
677 if (le64_to_cpu(de->inode)) {
678 /* We might block in the next section
679 * if the data destination is
680 * currently swapped out. So, use a
681 * version stamp to detect whether or
682 * not the directory has been modified
683 * during the copy operation.
684 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700685 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700686 unsigned char d_type = DT_UNKNOWN;
687
688 if (de->file_type < OCFS2_FT_MAX)
689 d_type = ocfs2_filetype_table[de->file_type];
690
691 filldir_ret = filldir(priv, de->name,
692 de->name_len,
693 *f_pos,
694 le64_to_cpu(de->inode),
695 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700696 if (filldir_ret) {
697 if (filldir_err)
698 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700699 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700700 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700701 if (version != *f_version)
702 goto revalidate;
703 }
704 *f_pos += le16_to_cpu(de->rec_len);
705 }
706
707out:
708 brelse(di_bh);
709
710 return 0;
711}
712
713static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700714 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700715 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700716 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800717{
718 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700719 unsigned long offset, blk, last_ra_blk = 0;
720 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 struct buffer_head * bh, * tmp;
722 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800723 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700724 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800725
726 stored = 0;
727 bh = NULL;
728
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700729 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800730
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700731 while (!error && !stored && *f_pos < i_size_read(inode)) {
732 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800733 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
734 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700735 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 continue;
737 }
738
Mark Fashehaa958872006-04-21 13:49:02 -0700739 /* The idea here is to begin with 8k read-ahead and to stay
740 * 4k ahead of our current position.
741 *
742 * TODO: Use the pagecache for this. We just need to
743 * make sure it's cluster-safe... */
744 if (!last_ra_blk
745 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
746 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800747 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800748 tmp = NULL;
749 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
750 OCFS2_BH_READAHEAD))
751 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 }
Mark Fashehaa958872006-04-21 13:49:02 -0700753 last_ra_blk = blk;
754 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 }
756
757revalidate:
758 /* If the dir block has changed since the last call to
759 * readdir(2), then we might be pointing to an invalid
760 * dirent right now. Scan from the start of the block
761 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700762 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 for (i = 0; i < sb->s_blocksize && i < offset; ) {
764 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
765 /* It's too expensive to do a full
766 * dirent test each time round this
767 * loop, but we do have to test at
768 * least that it is non-zero. A
769 * failure will be detected in the
770 * dirent test below. */
771 if (le16_to_cpu(de->rec_len) <
772 OCFS2_DIR_REC_LEN(1))
773 break;
774 i += le16_to_cpu(de->rec_len);
775 }
776 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700777 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800778 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700779 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800780 }
781
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700782 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 && offset < sb->s_blocksize) {
784 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
785 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
786 /* On error, skip the f_pos to the
787 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700788 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800789 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700790 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 }
792 offset += le16_to_cpu(de->rec_len);
793 if (le64_to_cpu(de->inode)) {
794 /* We might block in the next section
795 * if the data destination is
796 * currently swapped out. So, use a
797 * version stamp to detect whether or
798 * not the directory has been modified
799 * during the copy operation.
800 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700801 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 unsigned char d_type = DT_UNKNOWN;
803
804 if (de->file_type < OCFS2_FT_MAX)
805 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700806 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800807 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700808 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700809 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800810 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700811 if (error) {
812 if (filldir_err)
813 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700815 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700816 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 goto revalidate;
818 stored ++;
819 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700820 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800821 }
822 offset = 0;
823 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -0800824 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 }
826
827 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700828out:
829 return stored;
830}
831
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700832static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -0700833 loff_t *f_pos, void *priv, filldir_t filldir,
834 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700835{
836 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
837 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700838 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700839
Mark Fashehe7b34012007-09-24 14:25:27 -0700840 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
841 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700842}
843
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700844/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700845 * This is intended to be called from inside other kernel functions,
846 * so we fake some arguments.
847 */
848int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
849 filldir_t filldir)
850{
Mark Fashehe7b34012007-09-24 14:25:27 -0700851 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700852 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700853
854 while (*f_pos < i_size_read(inode)) {
855 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700856 filldir, &filldir_err);
857 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700858 break;
859 }
860
Mark Fashehe7b34012007-09-24 14:25:27 -0700861 if (ret > 0)
862 ret = -EIO;
863
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700864 return 0;
865}
866
867/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700868 * ocfs2_readdir()
869 *
870 */
871int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
872{
873 int error = 0;
874 struct inode *inode = filp->f_path.dentry->d_inode;
875 int lock_level = 0;
876
877 mlog_entry("dirino=%llu\n",
878 (unsigned long long)OCFS2_I(inode)->ip_blkno);
879
Mark Fashehe63aecb62007-10-18 15:30:42 -0700880 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700881 if (lock_level && error >= 0) {
882 /* We release EX lock which used to update atime
883 * and get PR lock again to reduce contention
884 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700885 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700886 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700887 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700888 }
889 if (error < 0) {
890 if (error != -ENOENT)
891 mlog_errno(error);
892 /* we haven't got any yet, so propagate the error. */
893 goto bail_nolock;
894 }
895
896 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -0700897 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700898
Mark Fashehe63aecb62007-10-18 15:30:42 -0700899 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800900
Mark Fashehaa958872006-04-21 13:49:02 -0700901bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700902 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -0800903
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700904 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800905}
906
907/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800908 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800909 */
910int ocfs2_find_files_on_disk(const char *name,
911 int namelen,
912 u64 *blkno,
913 struct inode *inode,
914 struct buffer_head **dirent_bh,
915 struct ocfs2_dir_entry **dirent)
916{
917 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800918
Joel Becker2b388c62006-05-10 18:28:59 -0700919 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
920 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800921
922 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
923 if (!*dirent_bh || !*dirent) {
924 status = -ENOENT;
925 goto leave;
926 }
927
928 *blkno = le64_to_cpu((*dirent)->inode);
929
930 status = 0;
931leave:
932 if (status < 0) {
933 *dirent = NULL;
Mark Fasheha81cb882008-10-07 14:25:16 -0700934 brelse(*dirent_bh);
935 *dirent_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800936 }
937
938 mlog_exit(status);
939 return status;
940}
941
Mark Fashehbe94d112007-09-11 15:22:06 -0700942/*
943 * Convenience function for callers which just want the block number
944 * mapped to a name and don't require the full dirent info, etc.
945 */
946int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
947 int namelen, u64 *blkno)
948{
949 int ret;
950 struct buffer_head *bh = NULL;
951 struct ocfs2_dir_entry *dirent = NULL;
952
953 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
954 brelse(bh);
955
956 return ret;
957}
958
Mark Fashehccd979b2005-12-15 14:31:24 -0800959/* Check for a name within a directory.
960 *
961 * Return 0 if the name does not exist
962 * Return -EEXIST if the directory contains the name
963 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800964 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800965 */
966int ocfs2_check_dir_for_entry(struct inode *dir,
967 const char *name,
968 int namelen)
969{
970 int ret;
971 struct buffer_head *dirent_bh = NULL;
972 struct ocfs2_dir_entry *dirent = NULL;
973
Mark Fashehb06970532006-03-03 10:24:33 -0800974 mlog_entry("dir %llu, name '%.*s'\n",
975 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800976
977 ret = -EEXIST;
978 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
979 if (dirent_bh)
980 goto bail;
981
982 ret = 0;
983bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700984 brelse(dirent_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800985
986 mlog_exit(ret);
987 return ret;
988}
989
Mark Fasheh0bfbbf62007-09-12 11:19:00 -0700990struct ocfs2_empty_dir_priv {
991 unsigned seen_dot;
992 unsigned seen_dot_dot;
993 unsigned seen_other;
994};
995static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
996 loff_t pos, u64 ino, unsigned type)
997{
998 struct ocfs2_empty_dir_priv *p = priv;
999
1000 /*
1001 * Check the positions of "." and ".." records to be sure
1002 * they're in the correct place.
1003 */
1004 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1005 p->seen_dot = 1;
1006 return 0;
1007 }
1008
1009 if (name_len == 2 && !strncmp("..", name, 2) &&
1010 pos == OCFS2_DIR_REC_LEN(1)) {
1011 p->seen_dot_dot = 1;
1012 return 0;
1013 }
1014
1015 p->seen_other = 1;
1016 return 1;
1017}
Mark Fashehccd979b2005-12-15 14:31:24 -08001018/*
1019 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001020 *
1021 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001022 */
1023int ocfs2_empty_dir(struct inode *inode)
1024{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001025 int ret;
1026 loff_t start = 0;
1027 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001028
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001029 memset(&priv, 0, sizeof(priv));
1030
1031 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1032 if (ret)
1033 mlog_errno(ret);
1034
1035 if (!priv.seen_dot || !priv.seen_dot_dot) {
1036 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001037 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001038 /*
1039 * XXX: Is it really safe to allow an unlink to continue?
1040 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 return 1;
1042 }
1043
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001044 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001045}
1046
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001047static void ocfs2_fill_initial_dirents(struct inode *inode,
1048 struct inode *parent,
1049 char *start, unsigned int size)
1050{
1051 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1052
1053 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1054 de->name_len = 1;
1055 de->rec_len =
1056 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1057 strcpy(de->name, ".");
1058 ocfs2_set_de_type(de, S_IFDIR);
1059
1060 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1061 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1062 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1063 de->name_len = 2;
1064 strcpy(de->name, "..");
1065 ocfs2_set_de_type(de, S_IFDIR);
1066}
1067
1068/*
1069 * This works together with code in ocfs2_mknod_locked() which sets
1070 * the inline-data flag and initializes the inline-data section.
1071 */
1072static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1073 handle_t *handle,
1074 struct inode *parent,
1075 struct inode *inode,
1076 struct buffer_head *di_bh)
1077{
1078 int ret;
1079 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1080 struct ocfs2_inline_data *data = &di->id2.i_data;
1081 unsigned int size = le16_to_cpu(data->id_count);
1082
1083 ret = ocfs2_journal_access(handle, inode, di_bh,
1084 OCFS2_JOURNAL_ACCESS_WRITE);
1085 if (ret) {
1086 mlog_errno(ret);
1087 goto out;
1088 }
1089
1090 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1091
1092 ocfs2_journal_dirty(handle, di_bh);
1093 if (ret) {
1094 mlog_errno(ret);
1095 goto out;
1096 }
1097
1098 i_size_write(inode, size);
1099 inode->i_nlink = 2;
1100 inode->i_blocks = ocfs2_inode_sector_count(inode);
1101
1102 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1103 if (ret < 0)
1104 mlog_errno(ret);
1105
1106out:
1107 return ret;
1108}
1109
1110static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1111 handle_t *handle,
1112 struct inode *parent,
1113 struct inode *inode,
1114 struct buffer_head *fe_bh,
1115 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001116{
1117 int status;
1118 struct buffer_head *new_bh = NULL;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001119
1120 mlog_entry_void();
1121
1122 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1123 data_ac, NULL, &new_bh);
1124 if (status < 0) {
1125 mlog_errno(status);
1126 goto bail;
1127 }
1128
1129 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1130
1131 status = ocfs2_journal_access(handle, inode, new_bh,
1132 OCFS2_JOURNAL_ACCESS_CREATE);
1133 if (status < 0) {
1134 mlog_errno(status);
1135 goto bail;
1136 }
1137 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1138
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001139 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1140 osb->sb->s_blocksize);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001141
1142 status = ocfs2_journal_dirty(handle, new_bh);
1143 if (status < 0) {
1144 mlog_errno(status);
1145 goto bail;
1146 }
1147
1148 i_size_write(inode, inode->i_sb->s_blocksize);
1149 inode->i_nlink = 2;
1150 inode->i_blocks = ocfs2_inode_sector_count(inode);
1151 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1152 if (status < 0) {
1153 mlog_errno(status);
1154 goto bail;
1155 }
1156
1157 status = 0;
1158bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001159 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001160
1161 mlog_exit(status);
1162 return status;
1163}
1164
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001165int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1166 handle_t *handle,
1167 struct inode *parent,
1168 struct inode *inode,
1169 struct buffer_head *fe_bh,
1170 struct ocfs2_alloc_context *data_ac)
1171{
1172 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1173
1174 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1175 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1176
1177 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1178 data_ac);
1179}
1180
1181static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1182 unsigned int new_size)
1183{
1184 struct ocfs2_dir_entry *de;
1185 struct ocfs2_dir_entry *prev_de;
1186 char *de_buf, *limit;
1187 unsigned int bytes = new_size - old_size;
1188
1189 limit = start + old_size;
1190 de_buf = start;
1191 de = (struct ocfs2_dir_entry *)de_buf;
1192 do {
1193 prev_de = de;
1194 de_buf += le16_to_cpu(de->rec_len);
1195 de = (struct ocfs2_dir_entry *)de_buf;
1196 } while (de_buf < limit);
1197
1198 le16_add_cpu(&prev_de->rec_len, bytes);
1199}
1200
1201/*
1202 * We allocate enough clusters to fulfill "blocks_wanted", but set
1203 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1204 * rest automatically for us.
1205 *
1206 * *first_block_bh is a pointer to the 1st data block allocated to the
1207 * directory.
1208 */
1209static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1210 unsigned int blocks_wanted,
1211 struct buffer_head **first_block_bh)
1212{
1213 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1214 u32 alloc, bit_off, len;
1215 struct super_block *sb = dir->i_sb;
1216 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1217 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1218 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1219 struct ocfs2_alloc_context *data_ac;
1220 struct buffer_head *dirdata_bh = NULL;
1221 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1222 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001223 struct ocfs2_extent_tree et;
1224
Joel Becker8d6220d2008-08-22 12:46:09 -07001225 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001226
1227 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1228
1229 /*
1230 * We should never need more than 2 clusters for this -
1231 * maximum dirent size is far less than one block. In fact,
1232 * the only time we'd need more than one cluster is if
1233 * blocksize == clustersize and the dirent won't fit in the
1234 * extra space that the expansion to a single block gives. As
1235 * of today, that only happens on 4k/4k file systems.
1236 */
1237 BUG_ON(alloc > 2);
1238
1239 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1240 if (ret) {
1241 mlog_errno(ret);
1242 goto out;
1243 }
1244
1245 down_write(&oi->ip_alloc_sem);
1246
1247 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001248 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001249 * extents.
1250 */
1251 if (alloc == 2)
1252 credits += OCFS2_SUBALLOC_ALLOC;
1253
1254 handle = ocfs2_start_trans(osb, credits);
1255 if (IS_ERR(handle)) {
1256 ret = PTR_ERR(handle);
1257 mlog_errno(ret);
1258 goto out_sem;
1259 }
1260
1261 /*
1262 * Try to claim as many clusters as the bitmap can give though
1263 * if we only get one now, that's enough to continue. The rest
1264 * will be claimed after the conversion to extents.
1265 */
1266 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1267 if (ret) {
1268 mlog_errno(ret);
1269 goto out_commit;
1270 }
1271
1272 /*
1273 * Operations are carefully ordered so that we set up the new
1274 * data block first. The conversion from inline data to
1275 * extents follows.
1276 */
1277 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1278 dirdata_bh = sb_getblk(sb, blkno);
1279 if (!dirdata_bh) {
1280 ret = -EIO;
1281 mlog_errno(ret);
1282 goto out_commit;
1283 }
1284
1285 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1286
1287 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1288 OCFS2_JOURNAL_ACCESS_CREATE);
1289 if (ret) {
1290 mlog_errno(ret);
1291 goto out_commit;
1292 }
1293
1294 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1295 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1296 sb->s_blocksize - i_size_read(dir));
1297 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1298 sb->s_blocksize);
1299
1300 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1301 if (ret) {
1302 mlog_errno(ret);
1303 goto out_commit;
1304 }
1305
1306 /*
1307 * Set extent, i_size, etc on the directory. After this, the
1308 * inode should contain the same exact dirents as before and
1309 * be fully accessible from system calls.
1310 *
1311 * We let the later dirent insert modify c/mtime - to the user
1312 * the data hasn't changed.
1313 */
1314 ret = ocfs2_journal_access(handle, dir, di_bh,
1315 OCFS2_JOURNAL_ACCESS_CREATE);
1316 if (ret) {
1317 mlog_errno(ret);
1318 goto out_commit;
1319 }
1320
1321 spin_lock(&oi->ip_lock);
1322 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1323 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1324 spin_unlock(&oi->ip_lock);
1325
1326 ocfs2_dinode_new_extent_list(dir, di);
1327
1328 i_size_write(dir, sb->s_blocksize);
1329 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1330
1331 di->i_size = cpu_to_le64(sb->s_blocksize);
1332 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1333 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001334
1335 /*
1336 * This should never fail as our extent list is empty and all
1337 * related blocks have been journaled already.
1338 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001339 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1340 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001341 if (ret) {
1342 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001343 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001344 }
1345
Mark Fasheh9780eb62008-08-05 11:32:46 -07001346 /*
1347 * Set i_blocks after the extent insert for the most up to
1348 * date ip_clusters value.
1349 */
1350 dir->i_blocks = ocfs2_inode_sector_count(dir);
1351
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001352 ret = ocfs2_journal_dirty(handle, di_bh);
1353 if (ret) {
1354 mlog_errno(ret);
1355 goto out_commit;
1356 }
1357
1358 /*
1359 * We asked for two clusters, but only got one in the 1st
1360 * pass. Claim the 2nd cluster as a separate extent.
1361 */
1362 if (alloc > len) {
1363 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1364 &len);
1365 if (ret) {
1366 mlog_errno(ret);
1367 goto out_commit;
1368 }
1369 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1370
Joel Beckerf99b9b72008-08-20 19:36:33 -07001371 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1372 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001373 if (ret) {
1374 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001375 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001376 }
1377 }
1378
1379 *first_block_bh = dirdata_bh;
1380 dirdata_bh = NULL;
1381
1382out_commit:
1383 ocfs2_commit_trans(osb, handle);
1384
1385out_sem:
1386 up_write(&oi->ip_alloc_sem);
1387
1388out:
1389 if (data_ac)
1390 ocfs2_free_alloc_context(data_ac);
1391
1392 brelse(dirdata_bh);
1393
1394 return ret;
1395}
1396
Mark Fashehccd979b2005-12-15 14:31:24 -08001397/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001398static int ocfs2_do_extend_dir(struct super_block *sb,
1399 handle_t *handle,
1400 struct inode *dir,
1401 struct buffer_head *parent_fe_bh,
1402 struct ocfs2_alloc_context *data_ac,
1403 struct ocfs2_alloc_context *meta_ac,
1404 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001405{
1406 int status;
1407 int extend;
Mark Fasheh8110b072007-03-22 16:53:23 -07001408 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001409
1410 spin_lock(&OCFS2_I(dir)->ip_lock);
1411 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1412 spin_unlock(&OCFS2_I(dir)->ip_lock);
1413
1414 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001415 u32 offset = OCFS2_I(dir)->ip_clusters;
1416
Tao Ma0eb8d47e2008-08-18 17:38:45 +08001417 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1418 1, 0, parent_fe_bh, handle,
1419 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001420 BUG_ON(status == -EAGAIN);
1421 if (status < 0) {
1422 mlog_errno(status);
1423 goto bail;
1424 }
1425 }
1426
Mark Fasheh8110b072007-03-22 16:53:23 -07001427 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1428 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001429 if (status < 0) {
1430 mlog_errno(status);
1431 goto bail;
1432 }
1433
1434 *new_bh = sb_getblk(sb, p_blkno);
1435 if (!*new_bh) {
1436 status = -EIO;
1437 mlog_errno(status);
1438 goto bail;
1439 }
1440 status = 0;
1441bail:
1442 mlog_exit(status);
1443 return status;
1444}
1445
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001446/*
1447 * Assumes you already have a cluster lock on the directory.
1448 *
1449 * 'blocks_wanted' is only used if we have an inline directory which
1450 * is to be turned into an extent based one. The size of the dirent to
1451 * insert might be larger than the space gained by growing to just one
1452 * block, so we may have to grow the inode by two blocks in that case.
1453 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001454static int ocfs2_extend_dir(struct ocfs2_super *osb,
1455 struct inode *dir,
1456 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001457 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 struct buffer_head **new_de_bh)
1459{
1460 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001461 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001462 loff_t dir_i_size;
1463 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001464 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001465 struct ocfs2_alloc_context *data_ac = NULL;
1466 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001467 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 struct buffer_head *new_bh = NULL;
1469 struct ocfs2_dir_entry * de;
1470 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001471 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001472
1473 mlog_entry_void();
1474
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001475 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1476 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1477 blocks_wanted, &new_bh);
1478 if (status) {
1479 mlog_errno(status);
1480 goto bail;
1481 }
1482
1483 if (blocks_wanted == 1) {
1484 /*
1485 * If the new dirent will fit inside the space
1486 * created by pushing out to one block, then
1487 * we can complete the operation
1488 * here. Otherwise we have to expand i_size
1489 * and format the 2nd block below.
1490 */
1491 BUG_ON(new_bh == NULL);
1492 goto bail_bh;
1493 }
1494
1495 /*
1496 * Get rid of 'new_bh' - we want to format the 2nd
1497 * data block and return that instead.
1498 */
1499 brelse(new_bh);
1500 new_bh = NULL;
1501
1502 dir_i_size = i_size_read(dir);
1503 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1504 goto do_extend;
1505 }
1506
Mark Fashehccd979b2005-12-15 14:31:24 -08001507 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -08001508 mlog(0, "extending dir %llu (i_size = %lld)\n",
1509 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001510
Mark Fashehccd979b2005-12-15 14:31:24 -08001511 /* dir->i_size is always block aligned. */
1512 spin_lock(&OCFS2_I(dir)->ip_lock);
1513 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1514 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001515 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001516 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001517 if (num_free_extents < 0) {
1518 status = num_free_extents;
1519 mlog_errno(status);
1520 goto bail;
1521 }
1522
1523 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001524 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001525 if (status < 0) {
1526 if (status != -ENOSPC)
1527 mlog_errno(status);
1528 goto bail;
1529 }
1530 }
1531
Mark Fashehda5cbf22006-10-06 18:34:35 -07001532 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 if (status < 0) {
1534 if (status != -ENOSPC)
1535 mlog_errno(status);
1536 goto bail;
1537 }
1538
Tao Ma811f9332008-08-18 17:38:43 +08001539 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 } else {
1541 spin_unlock(&OCFS2_I(dir)->ip_lock);
1542 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1543 }
1544
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001545do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001546 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1547 drop_alloc_sem = 1;
1548
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001549 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001550 if (IS_ERR(handle)) {
1551 status = PTR_ERR(handle);
1552 handle = NULL;
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
1557 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1558 data_ac, meta_ac, &new_bh);
1559 if (status < 0) {
1560 mlog_errno(status);
1561 goto bail;
1562 }
1563
1564 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1565
1566 status = ocfs2_journal_access(handle, dir, new_bh,
1567 OCFS2_JOURNAL_ACCESS_CREATE);
1568 if (status < 0) {
1569 mlog_errno(status);
1570 goto bail;
1571 }
1572 memset(new_bh->b_data, 0, sb->s_blocksize);
1573 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1574 de->inode = 0;
1575 de->rec_len = cpu_to_le16(sb->s_blocksize);
1576 status = ocfs2_journal_dirty(handle, new_bh);
1577 if (status < 0) {
1578 mlog_errno(status);
1579 goto bail;
1580 }
1581
1582 dir_i_size += dir->i_sb->s_blocksize;
1583 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001584 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001585 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1586 if (status < 0) {
1587 mlog_errno(status);
1588 goto bail;
1589 }
1590
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001591bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001592 *new_de_bh = new_bh;
1593 get_bh(*new_de_bh);
1594bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001595 if (drop_alloc_sem)
1596 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001597 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001598 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001599
1600 if (data_ac)
1601 ocfs2_free_alloc_context(data_ac);
1602 if (meta_ac)
1603 ocfs2_free_alloc_context(meta_ac);
1604
Mark Fasheha81cb882008-10-07 14:25:16 -07001605 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001606
1607 mlog_exit(status);
1608 return status;
1609}
1610
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001611static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1612 const char *name, int namelen,
1613 struct buffer_head **ret_de_bh,
1614 unsigned int *blocks_wanted)
1615{
1616 int ret;
1617 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1618 struct ocfs2_dir_entry *de, *last_de = NULL;
1619 char *de_buf, *limit;
1620 unsigned long offset = 0;
1621 unsigned int rec_len, new_rec_len;
1622
1623 de_buf = di->id2.i_data.id_data;
1624 limit = de_buf + i_size_read(dir);
1625 rec_len = OCFS2_DIR_REC_LEN(namelen);
1626
1627 while (de_buf < limit) {
1628 de = (struct ocfs2_dir_entry *)de_buf;
1629
1630 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1631 ret = -ENOENT;
1632 goto out;
1633 }
1634 if (ocfs2_match(namelen, name, de)) {
1635 ret = -EEXIST;
1636 goto out;
1637 }
1638 if (ocfs2_dirent_would_fit(de, rec_len)) {
1639 /* Ok, we found a spot. Return this bh and let
1640 * the caller actually fill it in. */
1641 *ret_de_bh = di_bh;
1642 get_bh(*ret_de_bh);
1643 ret = 0;
1644 goto out;
1645 }
1646
1647 last_de = de;
1648 de_buf += le16_to_cpu(de->rec_len);
1649 offset += le16_to_cpu(de->rec_len);
1650 }
1651
1652 /*
1653 * We're going to require expansion of the directory - figure
1654 * out how many blocks we'll need so that a place for the
1655 * dirent can be found.
1656 */
1657 *blocks_wanted = 1;
1658 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1659 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1660 *blocks_wanted = 2;
1661
1662 ret = -ENOSPC;
1663out:
1664 return ret;
1665}
1666
1667static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1668 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001669{
1670 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001671 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001672 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001674 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001675 int status;
1676
Joel Beckera22305c2008-11-13 14:49:17 -08001677 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1678 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001679 mlog_errno(status);
1680 goto bail;
1681 }
1682
1683 rec_len = OCFS2_DIR_REC_LEN(namelen);
1684 offset = 0;
1685 de = (struct ocfs2_dir_entry *) bh->b_data;
1686 while (1) {
1687 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1688 brelse(bh);
1689 bh = NULL;
1690
1691 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001692 /*
1693 * Caller will have to expand this
1694 * directory.
1695 */
1696 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001697 goto bail;
1698 }
Joel Beckera22305c2008-11-13 14:49:17 -08001699 status = ocfs2_read_dir_block(dir,
1700 offset >> sb->s_blocksize_bits,
1701 &bh, 0);
1702 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001703 mlog_errno(status);
1704 goto bail;
1705 }
1706 /* move to next block */
1707 de = (struct ocfs2_dir_entry *) bh->b_data;
1708 }
1709 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1710 status = -ENOENT;
1711 goto bail;
1712 }
1713 if (ocfs2_match(namelen, name, de)) {
1714 status = -EEXIST;
1715 goto bail;
1716 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001717 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001718 /* Ok, we found a spot. Return this bh and let
1719 * the caller actually fill it in. */
1720 *ret_de_bh = bh;
1721 get_bh(*ret_de_bh);
1722 status = 0;
1723 goto bail;
1724 }
1725 offset += le16_to_cpu(de->rec_len);
1726 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1727 }
1728
1729 status = 0;
1730bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001731 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001732
1733 mlog_exit(status);
1734 return status;
1735}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001736
1737int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1738 struct inode *dir,
1739 struct buffer_head *parent_fe_bh,
1740 const char *name,
1741 int namelen,
1742 struct buffer_head **ret_de_bh)
1743{
1744 int ret;
1745 unsigned int blocks_wanted = 1;
1746 struct buffer_head *bh = NULL;
1747
1748 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1749 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1750
1751 *ret_de_bh = NULL;
1752
1753 if (!namelen) {
1754 ret = -EINVAL;
1755 mlog_errno(ret);
1756 goto out;
1757 }
1758
1759 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1760 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1761 namelen, &bh, &blocks_wanted);
1762 } else
1763 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1764
1765 if (ret && ret != -ENOSPC) {
1766 mlog_errno(ret);
1767 goto out;
1768 }
1769
1770 if (ret == -ENOSPC) {
1771 /*
1772 * We have to expand the directory to add this name.
1773 */
1774 BUG_ON(bh);
1775
1776 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1777 &bh);
1778 if (ret) {
1779 if (ret != -ENOSPC)
1780 mlog_errno(ret);
1781 goto out;
1782 }
1783
1784 BUG_ON(!bh);
1785 }
1786
1787 *ret_de_bh = bh;
1788 bh = NULL;
1789out:
Mark Fasheha81cb882008-10-07 14:25:16 -07001790 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001791 return ret;
1792}