blob: 7e863d40380d5ff1b30c273e830fe36b27e15be8 [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;
234 u64 p_blkno;
235
236 if (((u64)v_block << inode->i_sb->s_blocksize_bits) >=
237 i_size_read(inode)) {
238 BUG_ON(!(flags & OCFS2_BH_READAHEAD));
239 goto out;
240 }
241
242 down_read(&OCFS2_I(inode)->ip_alloc_sem);
243 rc = ocfs2_extent_map_get_blocks(inode, v_block, &p_blkno, NULL,
244 NULL);
245 up_read(&OCFS2_I(inode)->ip_alloc_sem);
246 if (rc) {
247 mlog_errno(rc);
248 goto out;
249 }
250
251 if (!p_blkno) {
252 rc = -EIO;
253 mlog(ML_ERROR,
254 "Directory #%llu contains a hole at offset %llu\n",
255 (unsigned long long)OCFS2_I(inode)->ip_blkno,
256 (unsigned long long)v_block << inode->i_sb->s_blocksize_bits);
257 goto out;
258 }
259
Joel Becker970e4932008-11-13 14:49:19 -0800260 rc = ocfs2_read_blocks(inode, p_blkno, 1, &tmp, flags,
261 ocfs2_validate_dir_block);
Joel Beckera22305c2008-11-13 14:49:17 -0800262 if (rc) {
263 mlog_errno(rc);
264 goto out;
265 }
266
Joel Beckera22305c2008-11-13 14:49:17 -0800267 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
268 if (!*bh)
269 *bh = tmp;
270
271out:
272 return rc ? -EIO : 0;
273}
274
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200275static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
276 struct inode *dir,
277 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700278{
279 struct super_block *sb;
280 struct buffer_head *bh_use[NAMEI_RA_SIZE];
281 struct buffer_head *bh, *ret = NULL;
282 unsigned long start, block, b;
283 int ra_max = 0; /* Number of bh's in the readahead
284 buffer, bh_use[] */
285 int ra_ptr = 0; /* Current index into readahead
286 buffer */
287 int num = 0;
288 int nblocks, i, err;
289
290 mlog_entry_void();
291
Mark Fasheh316f4b92007-09-07 18:21:26 -0700292 sb = dir->i_sb;
293
294 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
295 start = OCFS2_I(dir)->ip_dir_start_lookup;
296 if (start >= nblocks)
297 start = 0;
298 block = start;
299
300restart:
301 do {
302 /*
303 * We deal with the read-ahead logic here.
304 */
305 if (ra_ptr >= ra_max) {
306 /* Refill the readahead buffer */
307 ra_ptr = 0;
308 b = block;
309 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
310 /*
311 * Terminate if we reach the end of the
312 * directory and must wrap, or if our
313 * search has finished at this block.
314 */
315 if (b >= nblocks || (num && block == start)) {
316 bh_use[ra_max] = NULL;
317 break;
318 }
319 num++;
320
Joel Beckera22305c2008-11-13 14:49:17 -0800321 bh = NULL;
322 err = ocfs2_read_dir_block(dir, b++, &bh,
323 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700324 bh_use[ra_max] = bh;
325 }
326 }
327 if ((bh = bh_use[ra_ptr++]) == NULL)
328 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800329 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700330 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800331 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700332 ocfs2_error(dir->i_sb, "reading directory %llu, "
333 "offset %lu\n",
334 (unsigned long long)OCFS2_I(dir)->ip_blkno,
335 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700336 goto next;
337 }
338 i = ocfs2_search_dirblock(bh, dir, name, namelen,
339 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700340 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700341 res_dir);
342 if (i == 1) {
343 OCFS2_I(dir)->ip_dir_start_lookup = block;
344 ret = bh;
345 goto cleanup_and_exit;
346 } else {
347 brelse(bh);
348 if (i < 0)
349 goto cleanup_and_exit;
350 }
351 next:
352 if (++block >= nblocks)
353 block = 0;
354 } while (block != start);
355
356 /*
357 * If the directory has grown while we were searching, then
358 * search the last part of the directory before giving up.
359 */
360 block = nblocks;
361 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
362 if (block < nblocks) {
363 start = 0;
364 goto restart;
365 }
366
367cleanup_and_exit:
368 /* Clean up the read-ahead blocks */
369 for (; ra_ptr < ra_max; ra_ptr++)
370 brelse(bh_use[ra_ptr]);
371
372 mlog_exit_ptr(ret);
373 return ret;
374}
375
Mark Fasheh23193e52007-09-12 13:01:18 -0700376/*
377 * Try to find an entry of the provided name within 'dir'.
378 *
379 * If nothing was found, NULL is returned. Otherwise, a buffer_head
380 * and pointer to the dir entry are passed back.
381 *
382 * Caller can NOT assume anything about the contents of the
383 * buffer_head - it is passed back only so that it can be passed into
384 * any one of the manipulation functions (add entry, delete entry,
385 * etc). As an example, bh in the extent directory case is a data
386 * block, in the inline-data case it actually points to an inode.
387 */
388struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
389 struct inode *dir,
390 struct ocfs2_dir_entry **res_dir)
391{
392 *res_dir = NULL;
393
394 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
395 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
396
397 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
398}
399
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700400/*
401 * Update inode number and type of a previously found directory entry.
402 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700403int ocfs2_update_entry(struct inode *dir, handle_t *handle,
404 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
405 struct inode *new_entry_inode)
406{
407 int ret;
408
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700409 /*
410 * The same code works fine for both inline-data and extent
411 * based directories, so no need to split this up.
412 */
413
Mark Fasheh38760e22007-09-11 17:21:56 -0700414 ret = ocfs2_journal_access(handle, dir, de_bh,
415 OCFS2_JOURNAL_ACCESS_WRITE);
416 if (ret) {
417 mlog_errno(ret);
418 goto out;
419 }
420
421 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
422 ocfs2_set_de_type(de, new_entry_inode->i_mode);
423
424 ocfs2_journal_dirty(handle, de_bh);
425
426out:
427 return ret;
428}
429
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700430static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
431 struct ocfs2_dir_entry *de_del,
432 struct buffer_head *bh, char *first_de,
433 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700434{
435 struct ocfs2_dir_entry *de, *pde;
436 int i, status = -ENOENT;
437
438 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
439
440 i = 0;
441 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700442 de = (struct ocfs2_dir_entry *) first_de;
443 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700444 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
445 status = -EIO;
446 mlog_errno(status);
447 goto bail;
448 }
449 if (de == de_del) {
450 status = ocfs2_journal_access(handle, dir, bh,
451 OCFS2_JOURNAL_ACCESS_WRITE);
452 if (status < 0) {
453 status = -EIO;
454 mlog_errno(status);
455 goto bail;
456 }
457 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100458 le16_add_cpu(&pde->rec_len,
459 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700460 else
461 de->inode = 0;
462 dir->i_version++;
463 status = ocfs2_journal_dirty(handle, bh);
464 goto bail;
465 }
466 i += le16_to_cpu(de->rec_len);
467 pde = de;
468 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
469 }
470bail:
471 mlog_exit(status);
472 return status;
473}
474
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700475static inline int ocfs2_delete_entry_id(handle_t *handle,
476 struct inode *dir,
477 struct ocfs2_dir_entry *de_del,
478 struct buffer_head *bh)
479{
480 int ret;
481 struct buffer_head *di_bh = NULL;
482 struct ocfs2_dinode *di;
483 struct ocfs2_inline_data *data;
484
Joel Beckerb657c952008-11-13 14:49:11 -0800485 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700486 if (ret) {
487 mlog_errno(ret);
488 goto out;
489 }
490
491 di = (struct ocfs2_dinode *)di_bh->b_data;
492 data = &di->id2.i_data;
493
494 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
495 i_size_read(dir));
496
497 brelse(di_bh);
498out:
499 return ret;
500}
501
502static inline int ocfs2_delete_entry_el(handle_t *handle,
503 struct inode *dir,
504 struct ocfs2_dir_entry *de_del,
505 struct buffer_head *bh)
506{
507 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
508 bh->b_size);
509}
510
511/*
512 * ocfs2_delete_entry deletes a directory entry by merging it with the
513 * previous entry
514 */
515int ocfs2_delete_entry(handle_t *handle,
516 struct inode *dir,
517 struct ocfs2_dir_entry *de_del,
518 struct buffer_head *bh)
519{
520 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
521 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
522
523 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
524}
525
Mark Fasheh8553cf42007-09-13 16:29:01 -0700526/*
527 * Check whether 'de' has enough room to hold an entry of
528 * 'new_rec_len' bytes.
529 */
530static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
531 unsigned int new_rec_len)
532{
533 unsigned int de_really_used;
534
535 /* Check whether this is an empty record with enough space */
536 if (le64_to_cpu(de->inode) == 0 &&
537 le16_to_cpu(de->rec_len) >= new_rec_len)
538 return 1;
539
540 /*
541 * Record might have free space at the end which we can
542 * use.
543 */
544 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
545 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
546 return 1;
547
548 return 0;
549}
550
Mark Fasheh316f4b92007-09-07 18:21:26 -0700551/* we don't always have a dentry for what we want to add, so people
552 * like orphan dir can call this instead.
553 *
554 * If you pass me insert_bh, I'll skip the search of the other dir
555 * blocks and put the record in there.
556 */
557int __ocfs2_add_entry(handle_t *handle,
558 struct inode *dir,
559 const char *name, int namelen,
560 struct inode *inode, u64 blkno,
561 struct buffer_head *parent_fe_bh,
562 struct buffer_head *insert_bh)
563{
564 unsigned long offset;
565 unsigned short rec_len;
566 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700567 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
568 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700569 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700570 unsigned int size = sb->s_blocksize;
571 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700572
573 mlog_entry_void();
574
Mark Fasheh316f4b92007-09-07 18:21:26 -0700575 if (!namelen)
576 return -EINVAL;
577
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700578 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
579 data_start = di->id2.i_data.id_data;
580 size = i_size_read(dir);
581
582 BUG_ON(insert_bh != parent_fe_bh);
583 }
584
Mark Fasheh316f4b92007-09-07 18:21:26 -0700585 rec_len = OCFS2_DIR_REC_LEN(namelen);
586 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700587 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700588 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700589 BUG_ON((char *)de >= (size + data_start));
590
Mark Fasheh316f4b92007-09-07 18:21:26 -0700591 /* These checks should've already been passed by the
592 * prepare function, but I guess we can leave them
593 * here anyway. */
594 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
595 retval = -ENOENT;
596 goto bail;
597 }
598 if (ocfs2_match(namelen, name, de)) {
599 retval = -EEXIST;
600 goto bail;
601 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700602
603 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700604 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
605 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
606 if (retval < 0) {
607 mlog_errno(retval);
608 goto bail;
609 }
610
611 status = ocfs2_journal_access(handle, dir, insert_bh,
612 OCFS2_JOURNAL_ACCESS_WRITE);
613 /* By now the buffer is marked for journaling */
614 offset += le16_to_cpu(de->rec_len);
615 if (le64_to_cpu(de->inode)) {
616 de1 = (struct ocfs2_dir_entry *)((char *) de +
617 OCFS2_DIR_REC_LEN(de->name_len));
618 de1->rec_len =
619 cpu_to_le16(le16_to_cpu(de->rec_len) -
620 OCFS2_DIR_REC_LEN(de->name_len));
621 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
622 de = de1;
623 }
624 de->file_type = OCFS2_FT_UNKNOWN;
625 if (blkno) {
626 de->inode = cpu_to_le64(blkno);
627 ocfs2_set_de_type(de, inode->i_mode);
628 } else
629 de->inode = 0;
630 de->name_len = namelen;
631 memcpy(de->name, name, namelen);
632
633 dir->i_version++;
634 status = ocfs2_journal_dirty(handle, insert_bh);
635 retval = 0;
636 goto bail;
637 }
638 offset += le16_to_cpu(de->rec_len);
639 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
640 }
641
642 /* when you think about it, the assert above should prevent us
643 * from ever getting here. */
644 retval = -ENOSPC;
645bail:
646
647 mlog_exit(retval);
648 return retval;
649}
650
Mark Fasheh23193e52007-09-12 13:01:18 -0700651static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700652 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700653 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700654 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700655{
656 int ret, i, filldir_ret;
657 unsigned long offset = *f_pos;
658 struct buffer_head *di_bh = NULL;
659 struct ocfs2_dinode *di;
660 struct ocfs2_inline_data *data;
661 struct ocfs2_dir_entry *de;
662
Joel Beckerb657c952008-11-13 14:49:11 -0800663 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700664 if (ret) {
665 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
666 (unsigned long long)OCFS2_I(inode)->ip_blkno);
667 goto out;
668 }
669
670 di = (struct ocfs2_dinode *)di_bh->b_data;
671 data = &di->id2.i_data;
672
673 while (*f_pos < i_size_read(inode)) {
674revalidate:
675 /* If the dir block has changed since the last call to
676 * readdir(2), then we might be pointing to an invalid
677 * dirent right now. Scan from the start of the block
678 * to make sure. */
679 if (*f_version != inode->i_version) {
680 for (i = 0; i < i_size_read(inode) && i < offset; ) {
681 de = (struct ocfs2_dir_entry *)
682 (data->id_data + i);
683 /* It's too expensive to do a full
684 * dirent test each time round this
685 * loop, but we do have to test at
686 * least that it is non-zero. A
687 * failure will be detected in the
688 * dirent test below. */
689 if (le16_to_cpu(de->rec_len) <
690 OCFS2_DIR_REC_LEN(1))
691 break;
692 i += le16_to_cpu(de->rec_len);
693 }
694 *f_pos = offset = i;
695 *f_version = inode->i_version;
696 }
697
698 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
699 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
700 /* On error, skip the f_pos to the end. */
701 *f_pos = i_size_read(inode);
702 goto out;
703 }
704 offset += le16_to_cpu(de->rec_len);
705 if (le64_to_cpu(de->inode)) {
706 /* We might block in the next section
707 * if the data destination is
708 * currently swapped out. So, use a
709 * version stamp to detect whether or
710 * not the directory has been modified
711 * during the copy operation.
712 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700713 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700714 unsigned char d_type = DT_UNKNOWN;
715
716 if (de->file_type < OCFS2_FT_MAX)
717 d_type = ocfs2_filetype_table[de->file_type];
718
719 filldir_ret = filldir(priv, de->name,
720 de->name_len,
721 *f_pos,
722 le64_to_cpu(de->inode),
723 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700724 if (filldir_ret) {
725 if (filldir_err)
726 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700727 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700728 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700729 if (version != *f_version)
730 goto revalidate;
731 }
732 *f_pos += le16_to_cpu(de->rec_len);
733 }
734
735out:
736 brelse(di_bh);
737
738 return 0;
739}
740
741static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700742 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700743 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700744 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800745{
746 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700747 unsigned long offset, blk, last_ra_blk = 0;
748 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 struct buffer_head * bh, * tmp;
750 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800751 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700752 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800753
754 stored = 0;
755 bh = NULL;
756
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700757 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800758
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700759 while (!error && !stored && *f_pos < i_size_read(inode)) {
760 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800761 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
762 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700763 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800764 continue;
765 }
766
Mark Fashehaa958872006-04-21 13:49:02 -0700767 /* The idea here is to begin with 8k read-ahead and to stay
768 * 4k ahead of our current position.
769 *
770 * TODO: Use the pagecache for this. We just need to
771 * make sure it's cluster-safe... */
772 if (!last_ra_blk
773 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
774 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800775 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800776 tmp = NULL;
777 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
778 OCFS2_BH_READAHEAD))
779 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800780 }
Mark Fashehaa958872006-04-21 13:49:02 -0700781 last_ra_blk = blk;
782 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 }
784
785revalidate:
786 /* If the dir block has changed since the last call to
787 * readdir(2), then we might be pointing to an invalid
788 * dirent right now. Scan from the start of the block
789 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700790 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 for (i = 0; i < sb->s_blocksize && i < offset; ) {
792 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
793 /* It's too expensive to do a full
794 * dirent test each time round this
795 * loop, but we do have to test at
796 * least that it is non-zero. A
797 * failure will be detected in the
798 * dirent test below. */
799 if (le16_to_cpu(de->rec_len) <
800 OCFS2_DIR_REC_LEN(1))
801 break;
802 i += le16_to_cpu(de->rec_len);
803 }
804 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700805 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700807 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800808 }
809
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700810 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800811 && offset < sb->s_blocksize) {
812 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
813 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
814 /* On error, skip the f_pos to the
815 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700816 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700818 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800819 }
820 offset += le16_to_cpu(de->rec_len);
821 if (le64_to_cpu(de->inode)) {
822 /* We might block in the next section
823 * if the data destination is
824 * currently swapped out. So, use a
825 * version stamp to detect whether or
826 * not the directory has been modified
827 * during the copy operation.
828 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700829 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800830 unsigned char d_type = DT_UNKNOWN;
831
832 if (de->file_type < OCFS2_FT_MAX)
833 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700834 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700836 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700837 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800838 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700839 if (error) {
840 if (filldir_err)
841 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800842 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700843 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700844 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800845 goto revalidate;
846 stored ++;
847 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700848 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 }
850 offset = 0;
851 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -0800852 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 }
854
855 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700856out:
857 return stored;
858}
859
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700860static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -0700861 loff_t *f_pos, void *priv, filldir_t filldir,
862 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700863{
864 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
865 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700866 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700867
Mark Fashehe7b34012007-09-24 14:25:27 -0700868 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
869 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700870}
871
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700872/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700873 * This is intended to be called from inside other kernel functions,
874 * so we fake some arguments.
875 */
876int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
877 filldir_t filldir)
878{
Mark Fashehe7b34012007-09-24 14:25:27 -0700879 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700880 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700881
882 while (*f_pos < i_size_read(inode)) {
883 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700884 filldir, &filldir_err);
885 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700886 break;
887 }
888
Mark Fashehe7b34012007-09-24 14:25:27 -0700889 if (ret > 0)
890 ret = -EIO;
891
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700892 return 0;
893}
894
895/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700896 * ocfs2_readdir()
897 *
898 */
899int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
900{
901 int error = 0;
902 struct inode *inode = filp->f_path.dentry->d_inode;
903 int lock_level = 0;
904
905 mlog_entry("dirino=%llu\n",
906 (unsigned long long)OCFS2_I(inode)->ip_blkno);
907
Mark Fashehe63aecb62007-10-18 15:30:42 -0700908 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700909 if (lock_level && error >= 0) {
910 /* We release EX lock which used to update atime
911 * and get PR lock again to reduce contention
912 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700913 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700914 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700915 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700916 }
917 if (error < 0) {
918 if (error != -ENOENT)
919 mlog_errno(error);
920 /* we haven't got any yet, so propagate the error. */
921 goto bail_nolock;
922 }
923
924 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -0700925 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700926
Mark Fashehe63aecb62007-10-18 15:30:42 -0700927 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800928
Mark Fashehaa958872006-04-21 13:49:02 -0700929bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700930 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -0800931
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700932 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800933}
934
935/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800936 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800937 */
938int ocfs2_find_files_on_disk(const char *name,
939 int namelen,
940 u64 *blkno,
941 struct inode *inode,
942 struct buffer_head **dirent_bh,
943 struct ocfs2_dir_entry **dirent)
944{
945 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800946
Joel Becker2b388c62006-05-10 18:28:59 -0700947 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
948 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800949
950 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
951 if (!*dirent_bh || !*dirent) {
952 status = -ENOENT;
953 goto leave;
954 }
955
956 *blkno = le64_to_cpu((*dirent)->inode);
957
958 status = 0;
959leave:
960 if (status < 0) {
961 *dirent = NULL;
Mark Fasheha81cb882008-10-07 14:25:16 -0700962 brelse(*dirent_bh);
963 *dirent_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 }
965
966 mlog_exit(status);
967 return status;
968}
969
Mark Fashehbe94d112007-09-11 15:22:06 -0700970/*
971 * Convenience function for callers which just want the block number
972 * mapped to a name and don't require the full dirent info, etc.
973 */
974int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
975 int namelen, u64 *blkno)
976{
977 int ret;
978 struct buffer_head *bh = NULL;
979 struct ocfs2_dir_entry *dirent = NULL;
980
981 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
982 brelse(bh);
983
984 return ret;
985}
986
Mark Fashehccd979b2005-12-15 14:31:24 -0800987/* Check for a name within a directory.
988 *
989 * Return 0 if the name does not exist
990 * Return -EEXIST if the directory contains the name
991 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800992 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 */
994int ocfs2_check_dir_for_entry(struct inode *dir,
995 const char *name,
996 int namelen)
997{
998 int ret;
999 struct buffer_head *dirent_bh = NULL;
1000 struct ocfs2_dir_entry *dirent = NULL;
1001
Mark Fashehb0697052006-03-03 10:24:33 -08001002 mlog_entry("dir %llu, name '%.*s'\n",
1003 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001004
1005 ret = -EEXIST;
1006 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1007 if (dirent_bh)
1008 goto bail;
1009
1010 ret = 0;
1011bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001012 brelse(dirent_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001013
1014 mlog_exit(ret);
1015 return ret;
1016}
1017
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001018struct ocfs2_empty_dir_priv {
1019 unsigned seen_dot;
1020 unsigned seen_dot_dot;
1021 unsigned seen_other;
1022};
1023static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1024 loff_t pos, u64 ino, unsigned type)
1025{
1026 struct ocfs2_empty_dir_priv *p = priv;
1027
1028 /*
1029 * Check the positions of "." and ".." records to be sure
1030 * they're in the correct place.
1031 */
1032 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1033 p->seen_dot = 1;
1034 return 0;
1035 }
1036
1037 if (name_len == 2 && !strncmp("..", name, 2) &&
1038 pos == OCFS2_DIR_REC_LEN(1)) {
1039 p->seen_dot_dot = 1;
1040 return 0;
1041 }
1042
1043 p->seen_other = 1;
1044 return 1;
1045}
Mark Fashehccd979b2005-12-15 14:31:24 -08001046/*
1047 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001048 *
1049 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001050 */
1051int ocfs2_empty_dir(struct inode *inode)
1052{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001053 int ret;
1054 loff_t start = 0;
1055 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001056
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001057 memset(&priv, 0, sizeof(priv));
1058
1059 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1060 if (ret)
1061 mlog_errno(ret);
1062
1063 if (!priv.seen_dot || !priv.seen_dot_dot) {
1064 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001065 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001066 /*
1067 * XXX: Is it really safe to allow an unlink to continue?
1068 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 return 1;
1070 }
1071
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001072 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001073}
1074
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001075static void ocfs2_fill_initial_dirents(struct inode *inode,
1076 struct inode *parent,
1077 char *start, unsigned int size)
1078{
1079 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1080
1081 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1082 de->name_len = 1;
1083 de->rec_len =
1084 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1085 strcpy(de->name, ".");
1086 ocfs2_set_de_type(de, S_IFDIR);
1087
1088 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1089 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1090 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1091 de->name_len = 2;
1092 strcpy(de->name, "..");
1093 ocfs2_set_de_type(de, S_IFDIR);
1094}
1095
1096/*
1097 * This works together with code in ocfs2_mknod_locked() which sets
1098 * the inline-data flag and initializes the inline-data section.
1099 */
1100static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1101 handle_t *handle,
1102 struct inode *parent,
1103 struct inode *inode,
1104 struct buffer_head *di_bh)
1105{
1106 int ret;
1107 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1108 struct ocfs2_inline_data *data = &di->id2.i_data;
1109 unsigned int size = le16_to_cpu(data->id_count);
1110
1111 ret = ocfs2_journal_access(handle, inode, di_bh,
1112 OCFS2_JOURNAL_ACCESS_WRITE);
1113 if (ret) {
1114 mlog_errno(ret);
1115 goto out;
1116 }
1117
1118 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1119
1120 ocfs2_journal_dirty(handle, di_bh);
1121 if (ret) {
1122 mlog_errno(ret);
1123 goto out;
1124 }
1125
1126 i_size_write(inode, size);
1127 inode->i_nlink = 2;
1128 inode->i_blocks = ocfs2_inode_sector_count(inode);
1129
1130 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1131 if (ret < 0)
1132 mlog_errno(ret);
1133
1134out:
1135 return ret;
1136}
1137
1138static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1139 handle_t *handle,
1140 struct inode *parent,
1141 struct inode *inode,
1142 struct buffer_head *fe_bh,
1143 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001144{
1145 int status;
1146 struct buffer_head *new_bh = NULL;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001147
1148 mlog_entry_void();
1149
1150 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1151 data_ac, NULL, &new_bh);
1152 if (status < 0) {
1153 mlog_errno(status);
1154 goto bail;
1155 }
1156
1157 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1158
1159 status = ocfs2_journal_access(handle, inode, new_bh,
1160 OCFS2_JOURNAL_ACCESS_CREATE);
1161 if (status < 0) {
1162 mlog_errno(status);
1163 goto bail;
1164 }
1165 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1166
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001167 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1168 osb->sb->s_blocksize);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001169
1170 status = ocfs2_journal_dirty(handle, new_bh);
1171 if (status < 0) {
1172 mlog_errno(status);
1173 goto bail;
1174 }
1175
1176 i_size_write(inode, inode->i_sb->s_blocksize);
1177 inode->i_nlink = 2;
1178 inode->i_blocks = ocfs2_inode_sector_count(inode);
1179 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1180 if (status < 0) {
1181 mlog_errno(status);
1182 goto bail;
1183 }
1184
1185 status = 0;
1186bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001187 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001188
1189 mlog_exit(status);
1190 return status;
1191}
1192
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001193int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1194 handle_t *handle,
1195 struct inode *parent,
1196 struct inode *inode,
1197 struct buffer_head *fe_bh,
1198 struct ocfs2_alloc_context *data_ac)
1199{
1200 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1201
1202 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1203 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1204
1205 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1206 data_ac);
1207}
1208
1209static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1210 unsigned int new_size)
1211{
1212 struct ocfs2_dir_entry *de;
1213 struct ocfs2_dir_entry *prev_de;
1214 char *de_buf, *limit;
1215 unsigned int bytes = new_size - old_size;
1216
1217 limit = start + old_size;
1218 de_buf = start;
1219 de = (struct ocfs2_dir_entry *)de_buf;
1220 do {
1221 prev_de = de;
1222 de_buf += le16_to_cpu(de->rec_len);
1223 de = (struct ocfs2_dir_entry *)de_buf;
1224 } while (de_buf < limit);
1225
1226 le16_add_cpu(&prev_de->rec_len, bytes);
1227}
1228
1229/*
1230 * We allocate enough clusters to fulfill "blocks_wanted", but set
1231 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1232 * rest automatically for us.
1233 *
1234 * *first_block_bh is a pointer to the 1st data block allocated to the
1235 * directory.
1236 */
1237static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1238 unsigned int blocks_wanted,
1239 struct buffer_head **first_block_bh)
1240{
1241 int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
1242 u32 alloc, bit_off, len;
1243 struct super_block *sb = dir->i_sb;
1244 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1245 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1246 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1247 struct ocfs2_alloc_context *data_ac;
1248 struct buffer_head *dirdata_bh = NULL;
1249 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1250 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001251 struct ocfs2_extent_tree et;
1252
Joel Becker8d6220d2008-08-22 12:46:09 -07001253 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001254
1255 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1256
1257 /*
1258 * We should never need more than 2 clusters for this -
1259 * maximum dirent size is far less than one block. In fact,
1260 * the only time we'd need more than one cluster is if
1261 * blocksize == clustersize and the dirent won't fit in the
1262 * extra space that the expansion to a single block gives. As
1263 * of today, that only happens on 4k/4k file systems.
1264 */
1265 BUG_ON(alloc > 2);
1266
1267 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1268 if (ret) {
1269 mlog_errno(ret);
1270 goto out;
1271 }
1272
1273 down_write(&oi->ip_alloc_sem);
1274
1275 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001276 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001277 * extents.
1278 */
1279 if (alloc == 2)
1280 credits += OCFS2_SUBALLOC_ALLOC;
1281
1282 handle = ocfs2_start_trans(osb, credits);
1283 if (IS_ERR(handle)) {
1284 ret = PTR_ERR(handle);
1285 mlog_errno(ret);
1286 goto out_sem;
1287 }
1288
1289 /*
1290 * Try to claim as many clusters as the bitmap can give though
1291 * if we only get one now, that's enough to continue. The rest
1292 * will be claimed after the conversion to extents.
1293 */
1294 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1295 if (ret) {
1296 mlog_errno(ret);
1297 goto out_commit;
1298 }
1299
1300 /*
1301 * Operations are carefully ordered so that we set up the new
1302 * data block first. The conversion from inline data to
1303 * extents follows.
1304 */
1305 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1306 dirdata_bh = sb_getblk(sb, blkno);
1307 if (!dirdata_bh) {
1308 ret = -EIO;
1309 mlog_errno(ret);
1310 goto out_commit;
1311 }
1312
1313 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1314
1315 ret = ocfs2_journal_access(handle, dir, dirdata_bh,
1316 OCFS2_JOURNAL_ACCESS_CREATE);
1317 if (ret) {
1318 mlog_errno(ret);
1319 goto out_commit;
1320 }
1321
1322 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1323 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1324 sb->s_blocksize - i_size_read(dir));
1325 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1326 sb->s_blocksize);
1327
1328 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1329 if (ret) {
1330 mlog_errno(ret);
1331 goto out_commit;
1332 }
1333
1334 /*
1335 * Set extent, i_size, etc on the directory. After this, the
1336 * inode should contain the same exact dirents as before and
1337 * be fully accessible from system calls.
1338 *
1339 * We let the later dirent insert modify c/mtime - to the user
1340 * the data hasn't changed.
1341 */
1342 ret = ocfs2_journal_access(handle, dir, di_bh,
1343 OCFS2_JOURNAL_ACCESS_CREATE);
1344 if (ret) {
1345 mlog_errno(ret);
1346 goto out_commit;
1347 }
1348
1349 spin_lock(&oi->ip_lock);
1350 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1351 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1352 spin_unlock(&oi->ip_lock);
1353
1354 ocfs2_dinode_new_extent_list(dir, di);
1355
1356 i_size_write(dir, sb->s_blocksize);
1357 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1358
1359 di->i_size = cpu_to_le64(sb->s_blocksize);
1360 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1361 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001362
1363 /*
1364 * This should never fail as our extent list is empty and all
1365 * related blocks have been journaled already.
1366 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001367 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1368 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001369 if (ret) {
1370 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001371 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001372 }
1373
Mark Fasheh9780eb62008-08-05 11:32:46 -07001374 /*
1375 * Set i_blocks after the extent insert for the most up to
1376 * date ip_clusters value.
1377 */
1378 dir->i_blocks = ocfs2_inode_sector_count(dir);
1379
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001380 ret = ocfs2_journal_dirty(handle, di_bh);
1381 if (ret) {
1382 mlog_errno(ret);
1383 goto out_commit;
1384 }
1385
1386 /*
1387 * We asked for two clusters, but only got one in the 1st
1388 * pass. Claim the 2nd cluster as a separate extent.
1389 */
1390 if (alloc > len) {
1391 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1392 &len);
1393 if (ret) {
1394 mlog_errno(ret);
1395 goto out_commit;
1396 }
1397 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1398
Joel Beckerf99b9b72008-08-20 19:36:33 -07001399 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1400 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001401 if (ret) {
1402 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001403 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001404 }
1405 }
1406
1407 *first_block_bh = dirdata_bh;
1408 dirdata_bh = NULL;
1409
1410out_commit:
1411 ocfs2_commit_trans(osb, handle);
1412
1413out_sem:
1414 up_write(&oi->ip_alloc_sem);
1415
1416out:
1417 if (data_ac)
1418 ocfs2_free_alloc_context(data_ac);
1419
1420 brelse(dirdata_bh);
1421
1422 return ret;
1423}
1424
Mark Fashehccd979b2005-12-15 14:31:24 -08001425/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001426static int ocfs2_do_extend_dir(struct super_block *sb,
1427 handle_t *handle,
1428 struct inode *dir,
1429 struct buffer_head *parent_fe_bh,
1430 struct ocfs2_alloc_context *data_ac,
1431 struct ocfs2_alloc_context *meta_ac,
1432 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001433{
1434 int status;
1435 int extend;
Mark Fasheh8110b072007-03-22 16:53:23 -07001436 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001437
1438 spin_lock(&OCFS2_I(dir)->ip_lock);
1439 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1440 spin_unlock(&OCFS2_I(dir)->ip_lock);
1441
1442 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001443 u32 offset = OCFS2_I(dir)->ip_clusters;
1444
Tao Ma0eb8d472008-08-18 17:38:45 +08001445 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1446 1, 0, parent_fe_bh, handle,
1447 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 BUG_ON(status == -EAGAIN);
1449 if (status < 0) {
1450 mlog_errno(status);
1451 goto bail;
1452 }
1453 }
1454
Mark Fasheh8110b072007-03-22 16:53:23 -07001455 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1456 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001457 if (status < 0) {
1458 mlog_errno(status);
1459 goto bail;
1460 }
1461
1462 *new_bh = sb_getblk(sb, p_blkno);
1463 if (!*new_bh) {
1464 status = -EIO;
1465 mlog_errno(status);
1466 goto bail;
1467 }
1468 status = 0;
1469bail:
1470 mlog_exit(status);
1471 return status;
1472}
1473
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001474/*
1475 * Assumes you already have a cluster lock on the directory.
1476 *
1477 * 'blocks_wanted' is only used if we have an inline directory which
1478 * is to be turned into an extent based one. The size of the dirent to
1479 * insert might be larger than the space gained by growing to just one
1480 * block, so we may have to grow the inode by two blocks in that case.
1481 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001482static int ocfs2_extend_dir(struct ocfs2_super *osb,
1483 struct inode *dir,
1484 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001485 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001486 struct buffer_head **new_de_bh)
1487{
1488 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001489 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 loff_t dir_i_size;
1491 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001492 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001493 struct ocfs2_alloc_context *data_ac = NULL;
1494 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001495 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001496 struct buffer_head *new_bh = NULL;
1497 struct ocfs2_dir_entry * de;
1498 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001499 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001500
1501 mlog_entry_void();
1502
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001503 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1504 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1505 blocks_wanted, &new_bh);
1506 if (status) {
1507 mlog_errno(status);
1508 goto bail;
1509 }
1510
1511 if (blocks_wanted == 1) {
1512 /*
1513 * If the new dirent will fit inside the space
1514 * created by pushing out to one block, then
1515 * we can complete the operation
1516 * here. Otherwise we have to expand i_size
1517 * and format the 2nd block below.
1518 */
1519 BUG_ON(new_bh == NULL);
1520 goto bail_bh;
1521 }
1522
1523 /*
1524 * Get rid of 'new_bh' - we want to format the 2nd
1525 * data block and return that instead.
1526 */
1527 brelse(new_bh);
1528 new_bh = NULL;
1529
1530 dir_i_size = i_size_read(dir);
1531 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1532 goto do_extend;
1533 }
1534
Mark Fashehccd979b2005-12-15 14:31:24 -08001535 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -08001536 mlog(0, "extending dir %llu (i_size = %lld)\n",
1537 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001538
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 /* dir->i_size is always block aligned. */
1540 spin_lock(&OCFS2_I(dir)->ip_lock);
1541 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1542 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001543 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001544 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001545 if (num_free_extents < 0) {
1546 status = num_free_extents;
1547 mlog_errno(status);
1548 goto bail;
1549 }
1550
1551 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001552 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001553 if (status < 0) {
1554 if (status != -ENOSPC)
1555 mlog_errno(status);
1556 goto bail;
1557 }
1558 }
1559
Mark Fashehda5cbf22006-10-06 18:34:35 -07001560 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001561 if (status < 0) {
1562 if (status != -ENOSPC)
1563 mlog_errno(status);
1564 goto bail;
1565 }
1566
Tao Ma811f9332008-08-18 17:38:43 +08001567 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001568 } else {
1569 spin_unlock(&OCFS2_I(dir)->ip_lock);
1570 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1571 }
1572
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001573do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001574 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1575 drop_alloc_sem = 1;
1576
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001577 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001578 if (IS_ERR(handle)) {
1579 status = PTR_ERR(handle);
1580 handle = NULL;
1581 mlog_errno(status);
1582 goto bail;
1583 }
1584
1585 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1586 data_ac, meta_ac, &new_bh);
1587 if (status < 0) {
1588 mlog_errno(status);
1589 goto bail;
1590 }
1591
1592 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1593
1594 status = ocfs2_journal_access(handle, dir, new_bh,
1595 OCFS2_JOURNAL_ACCESS_CREATE);
1596 if (status < 0) {
1597 mlog_errno(status);
1598 goto bail;
1599 }
1600 memset(new_bh->b_data, 0, sb->s_blocksize);
1601 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1602 de->inode = 0;
1603 de->rec_len = cpu_to_le16(sb->s_blocksize);
1604 status = ocfs2_journal_dirty(handle, new_bh);
1605 if (status < 0) {
1606 mlog_errno(status);
1607 goto bail;
1608 }
1609
1610 dir_i_size += dir->i_sb->s_blocksize;
1611 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001612 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1614 if (status < 0) {
1615 mlog_errno(status);
1616 goto bail;
1617 }
1618
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001619bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001620 *new_de_bh = new_bh;
1621 get_bh(*new_de_bh);
1622bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001623 if (drop_alloc_sem)
1624 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001625 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001626 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001627
1628 if (data_ac)
1629 ocfs2_free_alloc_context(data_ac);
1630 if (meta_ac)
1631 ocfs2_free_alloc_context(meta_ac);
1632
Mark Fasheha81cb882008-10-07 14:25:16 -07001633 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001634
1635 mlog_exit(status);
1636 return status;
1637}
1638
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001639static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1640 const char *name, int namelen,
1641 struct buffer_head **ret_de_bh,
1642 unsigned int *blocks_wanted)
1643{
1644 int ret;
1645 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1646 struct ocfs2_dir_entry *de, *last_de = NULL;
1647 char *de_buf, *limit;
1648 unsigned long offset = 0;
1649 unsigned int rec_len, new_rec_len;
1650
1651 de_buf = di->id2.i_data.id_data;
1652 limit = de_buf + i_size_read(dir);
1653 rec_len = OCFS2_DIR_REC_LEN(namelen);
1654
1655 while (de_buf < limit) {
1656 de = (struct ocfs2_dir_entry *)de_buf;
1657
1658 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1659 ret = -ENOENT;
1660 goto out;
1661 }
1662 if (ocfs2_match(namelen, name, de)) {
1663 ret = -EEXIST;
1664 goto out;
1665 }
1666 if (ocfs2_dirent_would_fit(de, rec_len)) {
1667 /* Ok, we found a spot. Return this bh and let
1668 * the caller actually fill it in. */
1669 *ret_de_bh = di_bh;
1670 get_bh(*ret_de_bh);
1671 ret = 0;
1672 goto out;
1673 }
1674
1675 last_de = de;
1676 de_buf += le16_to_cpu(de->rec_len);
1677 offset += le16_to_cpu(de->rec_len);
1678 }
1679
1680 /*
1681 * We're going to require expansion of the directory - figure
1682 * out how many blocks we'll need so that a place for the
1683 * dirent can be found.
1684 */
1685 *blocks_wanted = 1;
1686 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1687 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1688 *blocks_wanted = 2;
1689
1690 ret = -ENOSPC;
1691out:
1692 return ret;
1693}
1694
1695static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1696 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001697{
1698 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001699 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001701 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001702 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001703 int status;
1704
Joel Beckera22305c2008-11-13 14:49:17 -08001705 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1706 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001707 mlog_errno(status);
1708 goto bail;
1709 }
1710
1711 rec_len = OCFS2_DIR_REC_LEN(namelen);
1712 offset = 0;
1713 de = (struct ocfs2_dir_entry *) bh->b_data;
1714 while (1) {
1715 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1716 brelse(bh);
1717 bh = NULL;
1718
1719 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001720 /*
1721 * Caller will have to expand this
1722 * directory.
1723 */
1724 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 goto bail;
1726 }
Joel Beckera22305c2008-11-13 14:49:17 -08001727 status = ocfs2_read_dir_block(dir,
1728 offset >> sb->s_blocksize_bits,
1729 &bh, 0);
1730 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 mlog_errno(status);
1732 goto bail;
1733 }
1734 /* move to next block */
1735 de = (struct ocfs2_dir_entry *) bh->b_data;
1736 }
1737 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1738 status = -ENOENT;
1739 goto bail;
1740 }
1741 if (ocfs2_match(namelen, name, de)) {
1742 status = -EEXIST;
1743 goto bail;
1744 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001745 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 /* Ok, we found a spot. Return this bh and let
1747 * the caller actually fill it in. */
1748 *ret_de_bh = bh;
1749 get_bh(*ret_de_bh);
1750 status = 0;
1751 goto bail;
1752 }
1753 offset += le16_to_cpu(de->rec_len);
1754 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1755 }
1756
1757 status = 0;
1758bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001759 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001760
1761 mlog_exit(status);
1762 return status;
1763}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001764
1765int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1766 struct inode *dir,
1767 struct buffer_head *parent_fe_bh,
1768 const char *name,
1769 int namelen,
1770 struct buffer_head **ret_de_bh)
1771{
1772 int ret;
1773 unsigned int blocks_wanted = 1;
1774 struct buffer_head *bh = NULL;
1775
1776 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1777 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1778
1779 *ret_de_bh = NULL;
1780
1781 if (!namelen) {
1782 ret = -EINVAL;
1783 mlog_errno(ret);
1784 goto out;
1785 }
1786
1787 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1788 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1789 namelen, &bh, &blocks_wanted);
1790 } else
1791 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1792
1793 if (ret && ret != -ENOSPC) {
1794 mlog_errno(ret);
1795 goto out;
1796 }
1797
1798 if (ret == -ENOSPC) {
1799 /*
1800 * We have to expand the directory to add this name.
1801 */
1802 BUG_ON(bh);
1803
1804 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1805 &bh);
1806 if (ret) {
1807 if (ret != -ENOSPC)
1808 mlog_errno(ret);
1809 goto out;
1810 }
1811
1812 BUG_ON(!bh);
1813 }
1814
1815 *ret_de_bh = bh;
1816 bh = NULL;
1817out:
Mark Fasheha81cb882008-10-07 14:25:16 -07001818 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001819 return ret;
1820}