blob: 76ffb5c10b3e0a0a8c21ddbf22d774b5cc55cf5a [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>
Jan Karaa90714c2008-10-09 19:38:40 +020043#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080044
45#define MLOG_MASK_PREFIX ML_NAMEI
46#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50#include "alloc.h"
Joel Beckerc175a512008-12-10 17:58:22 -080051#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#include "dir.h"
53#include "dlmglue.h"
54#include "extent_map.h"
55#include "file.h"
56#include "inode.h"
57#include "journal.h"
58#include "namei.h"
59#include "suballoc.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070060#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080061#include "uptodate.h"
62
63#include "buffer_head_io.h"
64
Mark Fasheh316f4b92007-09-07 18:21:26 -070065#define NAMEI_RA_CHUNKS 2
66#define NAMEI_RA_BLOCKS 4
67#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
68#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
69
Mark Fashehccd979b2005-12-15 14:31:24 -080070static unsigned char ocfs2_filetype_table[] = {
71 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
72};
73
74static int ocfs2_extend_dir(struct ocfs2_super *osb,
75 struct inode *dir,
76 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -070077 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -080078 struct buffer_head **new_de_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -070079static int ocfs2_do_extend_dir(struct super_block *sb,
80 handle_t *handle,
81 struct inode *dir,
82 struct buffer_head *parent_fe_bh,
83 struct ocfs2_alloc_context *data_ac,
84 struct ocfs2_alloc_context *meta_ac,
85 struct buffer_head **new_bh);
86
Mark Fasheh23193e52007-09-12 13:01:18 -070087/*
Mark Fasheh87d35a72008-12-10 17:36:25 -080088 * These are distinct checks because future versions of the file system will
89 * want to have a trailing dirent structure independent of indexing.
90 */
91static int ocfs2_dir_has_trailer(struct inode *dir)
92{
93 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
95
96 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
97}
98
99static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
100{
101 return ocfs2_meta_ecc(osb);
102}
103
104static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
105{
106 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
107}
108
109#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
110
Joel Beckerc175a512008-12-10 17:58:22 -0800111/* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
112 * them more consistent? */
113struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
114 void *data)
115{
116 char *p = data;
117
118 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
119 return (struct ocfs2_dir_block_trailer *)p;
120}
121
Mark Fasheh87d35a72008-12-10 17:36:25 -0800122/*
123 * XXX: This is executed once on every dirent. We should consider optimizing
124 * it.
125 */
126static int ocfs2_skip_dir_trailer(struct inode *dir,
127 struct ocfs2_dir_entry *de,
128 unsigned long offset,
129 unsigned long blklen)
130{
131 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
132
133 if (!ocfs2_dir_has_trailer(dir))
134 return 0;
135
136 if (offset != toff)
137 return 0;
138
139 return 1;
140}
141
142static void ocfs2_init_dir_trailer(struct inode *inode,
143 struct buffer_head *bh)
144{
145 struct ocfs2_dir_block_trailer *trailer;
146
147 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
148 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
149 trailer->db_compat_rec_len =
150 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
151 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
152 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
153}
154
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800155void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
156{
157 brelse(res->dl_leaf_bh);
158}
159
Mark Fasheh87d35a72008-12-10 17:36:25 -0800160/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700161 * bh passed here can be an inode block or a dir data block, depending
162 * on the inode inline data flag.
163 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700164static int ocfs2_check_dir_entry(struct inode * dir,
165 struct ocfs2_dir_entry * de,
166 struct buffer_head * bh,
167 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700168{
169 const char *error_msg = NULL;
170 const int rlen = le16_to_cpu(de->rec_len);
171
172 if (rlen < OCFS2_DIR_REC_LEN(1))
173 error_msg = "rec_len is smaller than minimal";
174 else if (rlen % 4 != 0)
175 error_msg = "rec_len % 4 != 0";
176 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
177 error_msg = "rec_len is too small for name_len";
178 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
179 error_msg = "directory entry across blocks";
180
181 if (error_msg != NULL)
182 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
183 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
184 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
185 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
186 de->name_len);
187 return error_msg == NULL ? 1 : 0;
188}
189
190static inline int ocfs2_match(int len,
191 const char * const name,
192 struct ocfs2_dir_entry *de)
193{
194 if (len != de->name_len)
195 return 0;
196 if (!de->inode)
197 return 0;
198 return !memcmp(name, de->name, len);
199}
200
201/*
202 * Returns 0 if not found, -1 on failure, and 1 on success
203 */
204static int inline ocfs2_search_dirblock(struct buffer_head *bh,
205 struct inode *dir,
206 const char *name, int namelen,
207 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700208 char *first_de,
209 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700210 struct ocfs2_dir_entry **res_dir)
211{
212 struct ocfs2_dir_entry *de;
213 char *dlimit, *de_buf;
214 int de_len;
215 int ret = 0;
216
217 mlog_entry_void();
218
Mark Fasheh23193e52007-09-12 13:01:18 -0700219 de_buf = first_de;
220 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700221
222 while (de_buf < dlimit) {
223 /* this code is executed quadratically often */
224 /* do minimal checking `by hand' */
225
226 de = (struct ocfs2_dir_entry *) de_buf;
227
228 if (de_buf + namelen <= dlimit &&
229 ocfs2_match(namelen, name, de)) {
230 /* found a match - just to be sure, do a full check */
231 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
232 ret = -1;
233 goto bail;
234 }
235 *res_dir = de;
236 ret = 1;
237 goto bail;
238 }
239
240 /* prevent looping on a bad block */
241 de_len = le16_to_cpu(de->rec_len);
242 if (de_len <= 0) {
243 ret = -1;
244 goto bail;
245 }
246
247 de_buf += de_len;
248 offset += de_len;
249 }
250
251bail:
252 mlog_exit(ret);
253 return ret;
254}
255
Mark Fasheh23193e52007-09-12 13:01:18 -0700256static struct buffer_head *ocfs2_find_entry_id(const char *name,
257 int namelen,
258 struct inode *dir,
259 struct ocfs2_dir_entry **res_dir)
260{
261 int ret, found;
262 struct buffer_head *di_bh = NULL;
263 struct ocfs2_dinode *di;
264 struct ocfs2_inline_data *data;
265
Joel Beckerb657c952008-11-13 14:49:11 -0800266 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700267 if (ret) {
268 mlog_errno(ret);
269 goto out;
270 }
271
272 di = (struct ocfs2_dinode *)di_bh->b_data;
273 data = &di->id2.i_data;
274
275 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
276 data->id_data, i_size_read(dir), res_dir);
277 if (found == 1)
278 return di_bh;
279
280 brelse(di_bh);
281out:
282 return NULL;
283}
284
Joel Beckera22305c2008-11-13 14:49:17 -0800285static int ocfs2_validate_dir_block(struct super_block *sb,
286 struct buffer_head *bh)
287{
Joel Beckerc175a512008-12-10 17:58:22 -0800288 int rc;
289 struct ocfs2_dir_block_trailer *trailer =
290 ocfs2_trailer_from_bh(bh, sb);
291
292
Joel Beckera22305c2008-11-13 14:49:17 -0800293 /*
Joel Beckerc175a512008-12-10 17:58:22 -0800294 * We don't validate dirents here, that's handled
Joel Beckera22305c2008-11-13 14:49:17 -0800295 * in-place when the code walks them.
296 */
Joel Becker970e4932008-11-13 14:49:19 -0800297 mlog(0, "Validating dirblock %llu\n",
298 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800299
Joel Beckerc175a512008-12-10 17:58:22 -0800300 BUG_ON(!buffer_uptodate(bh));
301
302 /*
303 * If the ecc fails, we return the error but otherwise
304 * leave the filesystem running. We know any error is
305 * local to this block.
306 *
307 * Note that we are safe to call this even if the directory
308 * doesn't have a trailer. Filesystems without metaecc will do
309 * nothing, and filesystems with it will have one.
310 */
311 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
312 if (rc)
313 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
314 (unsigned long long)bh->b_blocknr);
315
316 return rc;
Joel Beckera22305c2008-11-13 14:49:17 -0800317}
318
319/*
320 * This function forces all errors to -EIO for consistency with its
321 * predecessor, ocfs2_bread(). We haven't audited what returning the
322 * real error codes would do to callers. We log the real codes with
323 * mlog_errno() before we squash them.
324 */
325static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
326 struct buffer_head **bh, int flags)
327{
328 int rc = 0;
329 struct buffer_head *tmp = *bh;
Mark Fasheh87d35a72008-12-10 17:36:25 -0800330 struct ocfs2_dir_block_trailer *trailer;
Joel Beckera22305c2008-11-13 14:49:17 -0800331
Joel Becker511308d2008-11-13 14:49:21 -0800332 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
333 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800334 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800335 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800336 goto out;
337 }
338
339 /*
340 * We check the trailer here rather than in
341 * ocfs2_validate_dir_block() because that function doesn't have
342 * the inode to test.
343 */
344 if (!(flags & OCFS2_BH_READAHEAD) &&
345 ocfs2_dir_has_trailer(inode)) {
346 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
347 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
348 rc = -EINVAL;
349 ocfs2_error(inode->i_sb,
350 "Invalid dirblock #%llu: "
351 "signature = %.*s\n",
352 (unsigned long long)tmp->b_blocknr, 7,
353 trailer->db_signature);
354 goto out;
355 }
356 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
357 rc = -EINVAL;
358 ocfs2_error(inode->i_sb,
359 "Directory block #%llu has an invalid "
360 "db_blkno of %llu",
361 (unsigned long long)tmp->b_blocknr,
362 (unsigned long long)le64_to_cpu(trailer->db_blkno));
363 goto out;
364 }
365 if (le64_to_cpu(trailer->db_parent_dinode) !=
366 OCFS2_I(inode)->ip_blkno) {
367 rc = -EINVAL;
368 ocfs2_error(inode->i_sb,
369 "Directory block #%llu on dinode "
370 "#%llu has an invalid parent_dinode "
371 "of %llu",
372 (unsigned long long)tmp->b_blocknr,
373 (unsigned long long)OCFS2_I(inode)->ip_blkno,
374 (unsigned long long)le64_to_cpu(trailer->db_blkno));
375 goto out;
376 }
377 }
Joel Beckera22305c2008-11-13 14:49:17 -0800378
Joel Becker511308d2008-11-13 14:49:21 -0800379 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800380 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800381 *bh = tmp;
382
Mark Fasheh87d35a72008-12-10 17:36:25 -0800383out:
Joel Beckera22305c2008-11-13 14:49:17 -0800384 return rc ? -EIO : 0;
385}
386
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200387static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
388 struct inode *dir,
389 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700390{
391 struct super_block *sb;
392 struct buffer_head *bh_use[NAMEI_RA_SIZE];
393 struct buffer_head *bh, *ret = NULL;
394 unsigned long start, block, b;
395 int ra_max = 0; /* Number of bh's in the readahead
396 buffer, bh_use[] */
397 int ra_ptr = 0; /* Current index into readahead
398 buffer */
399 int num = 0;
400 int nblocks, i, err;
401
402 mlog_entry_void();
403
Mark Fasheh316f4b92007-09-07 18:21:26 -0700404 sb = dir->i_sb;
405
406 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
407 start = OCFS2_I(dir)->ip_dir_start_lookup;
408 if (start >= nblocks)
409 start = 0;
410 block = start;
411
412restart:
413 do {
414 /*
415 * We deal with the read-ahead logic here.
416 */
417 if (ra_ptr >= ra_max) {
418 /* Refill the readahead buffer */
419 ra_ptr = 0;
420 b = block;
421 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
422 /*
423 * Terminate if we reach the end of the
424 * directory and must wrap, or if our
425 * search has finished at this block.
426 */
427 if (b >= nblocks || (num && block == start)) {
428 bh_use[ra_max] = NULL;
429 break;
430 }
431 num++;
432
Joel Beckera22305c2008-11-13 14:49:17 -0800433 bh = NULL;
434 err = ocfs2_read_dir_block(dir, b++, &bh,
435 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700436 bh_use[ra_max] = bh;
437 }
438 }
439 if ((bh = bh_use[ra_ptr++]) == NULL)
440 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800441 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700442 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800443 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700444 ocfs2_error(dir->i_sb, "reading directory %llu, "
445 "offset %lu\n",
446 (unsigned long long)OCFS2_I(dir)->ip_blkno,
447 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700448 goto next;
449 }
450 i = ocfs2_search_dirblock(bh, dir, name, namelen,
451 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700452 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700453 res_dir);
454 if (i == 1) {
455 OCFS2_I(dir)->ip_dir_start_lookup = block;
456 ret = bh;
457 goto cleanup_and_exit;
458 } else {
459 brelse(bh);
460 if (i < 0)
461 goto cleanup_and_exit;
462 }
463 next:
464 if (++block >= nblocks)
465 block = 0;
466 } while (block != start);
467
468 /*
469 * If the directory has grown while we were searching, then
470 * search the last part of the directory before giving up.
471 */
472 block = nblocks;
473 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
474 if (block < nblocks) {
475 start = 0;
476 goto restart;
477 }
478
479cleanup_and_exit:
480 /* Clean up the read-ahead blocks */
481 for (; ra_ptr < ra_max; ra_ptr++)
482 brelse(bh_use[ra_ptr]);
483
484 mlog_exit_ptr(ret);
485 return ret;
486}
487
Mark Fasheh23193e52007-09-12 13:01:18 -0700488/*
489 * Try to find an entry of the provided name within 'dir'.
490 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800491 * If nothing was found, -ENOENT is returned. Otherwise, zero is
492 * returned and the struct 'res' will contain information useful to
493 * other directory manipulation functions.
Mark Fasheh23193e52007-09-12 13:01:18 -0700494 *
495 * Caller can NOT assume anything about the contents of the
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800496 * buffer_heads - they are passed back only so that it can be passed into
Mark Fasheh23193e52007-09-12 13:01:18 -0700497 * any one of the manipulation functions (add entry, delete entry,
498 * etc). As an example, bh in the extent directory case is a data
499 * block, in the inline-data case it actually points to an inode.
500 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800501int ocfs2_find_entry(const char *name, int namelen,
502 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh23193e52007-09-12 13:01:18 -0700503{
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800504 struct buffer_head *bh;
505 struct ocfs2_dir_entry *res_dir = NULL;
Mark Fasheh23193e52007-09-12 13:01:18 -0700506
507 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800508 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
509 else
510 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
Mark Fasheh23193e52007-09-12 13:01:18 -0700511
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800512 if (bh == NULL)
513 return -ENOENT;
514
515 lookup->dl_leaf_bh = bh;
516 lookup->dl_entry = res_dir;
517 return 0;
Mark Fasheh23193e52007-09-12 13:01:18 -0700518}
519
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700520/*
521 * Update inode number and type of a previously found directory entry.
522 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700523int ocfs2_update_entry(struct inode *dir, handle_t *handle,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800524 struct ocfs2_dir_lookup_result *res,
Mark Fasheh38760e22007-09-11 17:21:56 -0700525 struct inode *new_entry_inode)
526{
527 int ret;
Joel Becker13723d02008-10-17 19:25:01 -0700528 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800529 struct ocfs2_dir_entry *de = res->dl_entry;
530 struct buffer_head *de_bh = res->dl_leaf_bh;
Mark Fasheh38760e22007-09-11 17:21:56 -0700531
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700532 /*
533 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -0700534 * based directories, so no need to split this up. The only
535 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700536 */
537
Joel Becker13723d02008-10-17 19:25:01 -0700538 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
539 access = ocfs2_journal_access_di;
540
541 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -0700542 if (ret) {
543 mlog_errno(ret);
544 goto out;
545 }
546
547 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
548 ocfs2_set_de_type(de, new_entry_inode->i_mode);
549
550 ocfs2_journal_dirty(handle, de_bh);
551
552out:
553 return ret;
554}
555
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700556static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
557 struct ocfs2_dir_entry *de_del,
558 struct buffer_head *bh, char *first_de,
559 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700560{
561 struct ocfs2_dir_entry *de, *pde;
562 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -0700563 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700564
565 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
566
Joel Becker13723d02008-10-17 19:25:01 -0700567 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
568 access = ocfs2_journal_access_di;
569
Mark Fasheh316f4b92007-09-07 18:21:26 -0700570 i = 0;
571 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700572 de = (struct ocfs2_dir_entry *) first_de;
573 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700574 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
575 status = -EIO;
576 mlog_errno(status);
577 goto bail;
578 }
579 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -0700580 status = access(handle, dir, bh,
581 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700582 if (status < 0) {
583 status = -EIO;
584 mlog_errno(status);
585 goto bail;
586 }
587 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100588 le16_add_cpu(&pde->rec_len,
589 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700590 else
591 de->inode = 0;
592 dir->i_version++;
593 status = ocfs2_journal_dirty(handle, bh);
594 goto bail;
595 }
596 i += le16_to_cpu(de->rec_len);
597 pde = de;
598 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
599 }
600bail:
601 mlog_exit(status);
602 return status;
603}
604
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700605static inline int ocfs2_delete_entry_id(handle_t *handle,
606 struct inode *dir,
607 struct ocfs2_dir_entry *de_del,
608 struct buffer_head *bh)
609{
610 int ret;
611 struct buffer_head *di_bh = NULL;
612 struct ocfs2_dinode *di;
613 struct ocfs2_inline_data *data;
614
Joel Beckerb657c952008-11-13 14:49:11 -0800615 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700616 if (ret) {
617 mlog_errno(ret);
618 goto out;
619 }
620
621 di = (struct ocfs2_dinode *)di_bh->b_data;
622 data = &di->id2.i_data;
623
624 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
625 i_size_read(dir));
626
627 brelse(di_bh);
628out:
629 return ret;
630}
631
632static inline int ocfs2_delete_entry_el(handle_t *handle,
633 struct inode *dir,
634 struct ocfs2_dir_entry *de_del,
635 struct buffer_head *bh)
636{
637 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
638 bh->b_size);
639}
640
641/*
642 * ocfs2_delete_entry deletes a directory entry by merging it with the
643 * previous entry
644 */
645int ocfs2_delete_entry(handle_t *handle,
646 struct inode *dir,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800647 struct ocfs2_dir_lookup_result *res)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700648{
649 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800650 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
651 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700652
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800653 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
654 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700655}
656
Mark Fasheh8553cf42007-09-13 16:29:01 -0700657/*
658 * Check whether 'de' has enough room to hold an entry of
659 * 'new_rec_len' bytes.
660 */
661static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
662 unsigned int new_rec_len)
663{
664 unsigned int de_really_used;
665
666 /* Check whether this is an empty record with enough space */
667 if (le64_to_cpu(de->inode) == 0 &&
668 le16_to_cpu(de->rec_len) >= new_rec_len)
669 return 1;
670
671 /*
672 * Record might have free space at the end which we can
673 * use.
674 */
675 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
676 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
677 return 1;
678
679 return 0;
680}
681
Mark Fasheh316f4b92007-09-07 18:21:26 -0700682/* we don't always have a dentry for what we want to add, so people
683 * like orphan dir can call this instead.
684 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800685 * The lookup context must have been filled from
686 * ocfs2_prepare_dir_for_insert.
Mark Fasheh316f4b92007-09-07 18:21:26 -0700687 */
688int __ocfs2_add_entry(handle_t *handle,
689 struct inode *dir,
690 const char *name, int namelen,
691 struct inode *inode, u64 blkno,
692 struct buffer_head *parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800693 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700694{
695 unsigned long offset;
696 unsigned short rec_len;
697 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700698 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
699 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700700 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700701 unsigned int size = sb->s_blocksize;
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800702 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700703 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700704
705 mlog_entry_void();
706
Mark Fasheh316f4b92007-09-07 18:21:26 -0700707 if (!namelen)
708 return -EINVAL;
709
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700710 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
711 data_start = di->id2.i_data.id_data;
712 size = i_size_read(dir);
713
714 BUG_ON(insert_bh != parent_fe_bh);
715 }
716
Mark Fasheh316f4b92007-09-07 18:21:26 -0700717 rec_len = OCFS2_DIR_REC_LEN(namelen);
718 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700719 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700720 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700721 BUG_ON((char *)de >= (size + data_start));
722
Mark Fasheh316f4b92007-09-07 18:21:26 -0700723 /* These checks should've already been passed by the
724 * prepare function, but I guess we can leave them
725 * here anyway. */
726 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
727 retval = -ENOENT;
728 goto bail;
729 }
730 if (ocfs2_match(namelen, name, de)) {
731 retval = -EEXIST;
732 goto bail;
733 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700734
Mark Fasheh87d35a72008-12-10 17:36:25 -0800735 /* We're guaranteed that we should have space, so we
736 * can't possibly have hit the trailer...right? */
737 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
738 "Hit dir trailer trying to insert %.*s "
739 "(namelen %d) into directory %llu. "
740 "offset is %lu, trailer offset is %d\n",
741 namelen, name, namelen,
742 (unsigned long long)parent_fe_bh->b_blocknr,
743 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
744
Mark Fasheh8553cf42007-09-13 16:29:01 -0700745 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700746 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
747 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
748 if (retval < 0) {
749 mlog_errno(retval);
750 goto bail;
751 }
752
Joel Becker13723d02008-10-17 19:25:01 -0700753 if (insert_bh == parent_fe_bh)
754 status = ocfs2_journal_access_di(handle, dir,
755 insert_bh,
756 OCFS2_JOURNAL_ACCESS_WRITE);
757 else
758 status = ocfs2_journal_access_db(handle, dir,
759 insert_bh,
760 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700761 /* By now the buffer is marked for journaling */
762 offset += le16_to_cpu(de->rec_len);
763 if (le64_to_cpu(de->inode)) {
764 de1 = (struct ocfs2_dir_entry *)((char *) de +
765 OCFS2_DIR_REC_LEN(de->name_len));
766 de1->rec_len =
767 cpu_to_le16(le16_to_cpu(de->rec_len) -
768 OCFS2_DIR_REC_LEN(de->name_len));
769 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
770 de = de1;
771 }
772 de->file_type = OCFS2_FT_UNKNOWN;
773 if (blkno) {
774 de->inode = cpu_to_le64(blkno);
775 ocfs2_set_de_type(de, inode->i_mode);
776 } else
777 de->inode = 0;
778 de->name_len = namelen;
779 memcpy(de->name, name, namelen);
780
781 dir->i_version++;
782 status = ocfs2_journal_dirty(handle, insert_bh);
783 retval = 0;
784 goto bail;
785 }
Mark Fasheh87d35a72008-12-10 17:36:25 -0800786
Mark Fasheh316f4b92007-09-07 18:21:26 -0700787 offset += le16_to_cpu(de->rec_len);
788 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
789 }
790
791 /* when you think about it, the assert above should prevent us
792 * from ever getting here. */
793 retval = -ENOSPC;
794bail:
795
796 mlog_exit(retval);
797 return retval;
798}
799
Mark Fasheh23193e52007-09-12 13:01:18 -0700800static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700801 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700802 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700803 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700804{
805 int ret, i, filldir_ret;
806 unsigned long offset = *f_pos;
807 struct buffer_head *di_bh = NULL;
808 struct ocfs2_dinode *di;
809 struct ocfs2_inline_data *data;
810 struct ocfs2_dir_entry *de;
811
Joel Beckerb657c952008-11-13 14:49:11 -0800812 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700813 if (ret) {
814 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
815 (unsigned long long)OCFS2_I(inode)->ip_blkno);
816 goto out;
817 }
818
819 di = (struct ocfs2_dinode *)di_bh->b_data;
820 data = &di->id2.i_data;
821
822 while (*f_pos < i_size_read(inode)) {
823revalidate:
824 /* If the dir block has changed since the last call to
825 * readdir(2), then we might be pointing to an invalid
826 * dirent right now. Scan from the start of the block
827 * to make sure. */
828 if (*f_version != inode->i_version) {
829 for (i = 0; i < i_size_read(inode) && i < offset; ) {
830 de = (struct ocfs2_dir_entry *)
831 (data->id_data + i);
832 /* It's too expensive to do a full
833 * dirent test each time round this
834 * loop, but we do have to test at
835 * least that it is non-zero. A
836 * failure will be detected in the
837 * dirent test below. */
838 if (le16_to_cpu(de->rec_len) <
839 OCFS2_DIR_REC_LEN(1))
840 break;
841 i += le16_to_cpu(de->rec_len);
842 }
843 *f_pos = offset = i;
844 *f_version = inode->i_version;
845 }
846
847 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
848 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
849 /* On error, skip the f_pos to the end. */
850 *f_pos = i_size_read(inode);
851 goto out;
852 }
853 offset += le16_to_cpu(de->rec_len);
854 if (le64_to_cpu(de->inode)) {
855 /* We might block in the next section
856 * if the data destination is
857 * currently swapped out. So, use a
858 * version stamp to detect whether or
859 * not the directory has been modified
860 * during the copy operation.
861 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700862 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700863 unsigned char d_type = DT_UNKNOWN;
864
865 if (de->file_type < OCFS2_FT_MAX)
866 d_type = ocfs2_filetype_table[de->file_type];
867
868 filldir_ret = filldir(priv, de->name,
869 de->name_len,
870 *f_pos,
871 le64_to_cpu(de->inode),
872 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700873 if (filldir_ret) {
874 if (filldir_err)
875 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700876 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700877 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700878 if (version != *f_version)
879 goto revalidate;
880 }
881 *f_pos += le16_to_cpu(de->rec_len);
882 }
883
884out:
885 brelse(di_bh);
886
887 return 0;
888}
889
890static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700891 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700892 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700893 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800894{
895 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700896 unsigned long offset, blk, last_ra_blk = 0;
897 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 struct buffer_head * bh, * tmp;
899 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700901 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800902
903 stored = 0;
904 bh = NULL;
905
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700906 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700908 while (!error && !stored && *f_pos < i_size_read(inode)) {
909 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800910 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
911 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700912 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 continue;
914 }
915
Mark Fashehaa958872006-04-21 13:49:02 -0700916 /* The idea here is to begin with 8k read-ahead and to stay
917 * 4k ahead of our current position.
918 *
919 * TODO: Use the pagecache for this. We just need to
920 * make sure it's cluster-safe... */
921 if (!last_ra_blk
922 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
923 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800924 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800925 tmp = NULL;
926 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
927 OCFS2_BH_READAHEAD))
928 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 }
Mark Fashehaa958872006-04-21 13:49:02 -0700930 last_ra_blk = blk;
931 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800932 }
933
934revalidate:
935 /* If the dir block has changed since the last call to
936 * readdir(2), then we might be pointing to an invalid
937 * dirent right now. Scan from the start of the block
938 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700939 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 for (i = 0; i < sb->s_blocksize && i < offset; ) {
941 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
942 /* It's too expensive to do a full
943 * dirent test each time round this
944 * loop, but we do have to test at
945 * least that it is non-zero. A
946 * failure will be detected in the
947 * dirent test below. */
948 if (le16_to_cpu(de->rec_len) <
949 OCFS2_DIR_REC_LEN(1))
950 break;
951 i += le16_to_cpu(de->rec_len);
952 }
953 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700954 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800955 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700956 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800957 }
958
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700959 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 && offset < sb->s_blocksize) {
961 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
962 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
963 /* On error, skip the f_pos to the
964 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700965 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800966 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700967 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800968 }
969 offset += le16_to_cpu(de->rec_len);
970 if (le64_to_cpu(de->inode)) {
971 /* We might block in the next section
972 * if the data destination is
973 * currently swapped out. So, use a
974 * version stamp to detect whether or
975 * not the directory has been modified
976 * during the copy operation.
977 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700978 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 unsigned char d_type = DT_UNKNOWN;
980
981 if (de->file_type < OCFS2_FT_MAX)
982 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700983 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800984 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700985 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700986 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700988 if (error) {
989 if (filldir_err)
990 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800991 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700992 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700993 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 goto revalidate;
995 stored ++;
996 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700997 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800998 }
999 offset = 0;
1000 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -08001001 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 }
1003
1004 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001005out:
1006 return stored;
1007}
1008
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001009static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -07001010 loff_t *f_pos, void *priv, filldir_t filldir,
1011 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -07001012{
1013 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1014 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001015 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001016
Mark Fashehe7b34012007-09-24 14:25:27 -07001017 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1018 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001019}
1020
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001021/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001022 * This is intended to be called from inside other kernel functions,
1023 * so we fake some arguments.
1024 */
1025int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1026 filldir_t filldir)
1027{
Mark Fashehe7b34012007-09-24 14:25:27 -07001028 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001029 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001030
1031 while (*f_pos < i_size_read(inode)) {
1032 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001033 filldir, &filldir_err);
1034 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001035 break;
1036 }
1037
Mark Fashehe7b34012007-09-24 14:25:27 -07001038 if (ret > 0)
1039 ret = -EIO;
1040
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001041 return 0;
1042}
1043
1044/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001045 * ocfs2_readdir()
1046 *
1047 */
1048int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1049{
1050 int error = 0;
1051 struct inode *inode = filp->f_path.dentry->d_inode;
1052 int lock_level = 0;
1053
1054 mlog_entry("dirino=%llu\n",
1055 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1056
Mark Fashehe63aecb62007-10-18 15:30:42 -07001057 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001058 if (lock_level && error >= 0) {
1059 /* We release EX lock which used to update atime
1060 * and get PR lock again to reduce contention
1061 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001062 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001063 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001064 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001065 }
1066 if (error < 0) {
1067 if (error != -ENOENT)
1068 mlog_errno(error);
1069 /* we haven't got any yet, so propagate the error. */
1070 goto bail_nolock;
1071 }
1072
1073 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -07001074 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001075
Mark Fashehe63aecb62007-10-18 15:30:42 -07001076 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001077
Mark Fashehaa958872006-04-21 13:49:02 -07001078bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001079 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08001080
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001081 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001082}
1083
1084/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001085 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 */
1087int ocfs2_find_files_on_disk(const char *name,
1088 int namelen,
1089 u64 *blkno,
1090 struct inode *inode,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001091 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08001092{
1093 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001094
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001095 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
1096 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001097
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001098 status = ocfs2_find_entry(name, namelen, inode, lookup);
1099 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08001101
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001102 *blkno = le64_to_cpu(lookup->dl_entry->inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103
1104 status = 0;
1105leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08001106
Mark Fashehccd979b2005-12-15 14:31:24 -08001107 return status;
1108}
1109
Mark Fashehbe94d112007-09-11 15:22:06 -07001110/*
1111 * Convenience function for callers which just want the block number
1112 * mapped to a name and don't require the full dirent info, etc.
1113 */
1114int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1115 int namelen, u64 *blkno)
1116{
1117 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001118 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehbe94d112007-09-11 15:22:06 -07001119
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001120 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
1121 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehbe94d112007-09-11 15:22:06 -07001122
1123 return ret;
1124}
1125
Mark Fashehccd979b2005-12-15 14:31:24 -08001126/* Check for a name within a directory.
1127 *
1128 * Return 0 if the name does not exist
1129 * Return -EEXIST if the directory contains the name
1130 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001131 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 */
1133int ocfs2_check_dir_for_entry(struct inode *dir,
1134 const char *name,
1135 int namelen)
1136{
1137 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001138 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001139
Mark Fashehb06970532006-03-03 10:24:33 -08001140 mlog_entry("dir %llu, name '%.*s'\n",
1141 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001142
1143 ret = -EEXIST;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001144 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 goto bail;
1146
1147 ret = 0;
1148bail:
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001149 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001150
1151 mlog_exit(ret);
1152 return ret;
1153}
1154
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001155struct ocfs2_empty_dir_priv {
1156 unsigned seen_dot;
1157 unsigned seen_dot_dot;
1158 unsigned seen_other;
1159};
1160static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1161 loff_t pos, u64 ino, unsigned type)
1162{
1163 struct ocfs2_empty_dir_priv *p = priv;
1164
1165 /*
1166 * Check the positions of "." and ".." records to be sure
1167 * they're in the correct place.
1168 */
1169 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1170 p->seen_dot = 1;
1171 return 0;
1172 }
1173
1174 if (name_len == 2 && !strncmp("..", name, 2) &&
1175 pos == OCFS2_DIR_REC_LEN(1)) {
1176 p->seen_dot_dot = 1;
1177 return 0;
1178 }
1179
1180 p->seen_other = 1;
1181 return 1;
1182}
Mark Fashehccd979b2005-12-15 14:31:24 -08001183/*
1184 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001185 *
1186 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 */
1188int ocfs2_empty_dir(struct inode *inode)
1189{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001190 int ret;
1191 loff_t start = 0;
1192 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001193
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001194 memset(&priv, 0, sizeof(priv));
1195
1196 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1197 if (ret)
1198 mlog_errno(ret);
1199
1200 if (!priv.seen_dot || !priv.seen_dot_dot) {
1201 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001202 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001203 /*
1204 * XXX: Is it really safe to allow an unlink to continue?
1205 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 return 1;
1207 }
1208
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001209 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001210}
1211
Mark Fasheh87d35a72008-12-10 17:36:25 -08001212/*
1213 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1214 * "..", which might be used during creation of a directory with a trailing
1215 * header. It is otherwise safe to ignore the return code.
1216 */
1217static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1218 struct inode *parent,
1219 char *start,
1220 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001221{
1222 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1223
1224 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1225 de->name_len = 1;
1226 de->rec_len =
1227 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1228 strcpy(de->name, ".");
1229 ocfs2_set_de_type(de, S_IFDIR);
1230
1231 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1232 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1233 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1234 de->name_len = 2;
1235 strcpy(de->name, "..");
1236 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001237
1238 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001239}
1240
1241/*
1242 * This works together with code in ocfs2_mknod_locked() which sets
1243 * the inline-data flag and initializes the inline-data section.
1244 */
1245static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1246 handle_t *handle,
1247 struct inode *parent,
1248 struct inode *inode,
1249 struct buffer_head *di_bh)
1250{
1251 int ret;
1252 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1253 struct ocfs2_inline_data *data = &di->id2.i_data;
1254 unsigned int size = le16_to_cpu(data->id_count);
1255
Joel Becker13723d02008-10-17 19:25:01 -07001256 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1257 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001258 if (ret) {
1259 mlog_errno(ret);
1260 goto out;
1261 }
1262
1263 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1264
1265 ocfs2_journal_dirty(handle, di_bh);
1266 if (ret) {
1267 mlog_errno(ret);
1268 goto out;
1269 }
1270
1271 i_size_write(inode, size);
1272 inode->i_nlink = 2;
1273 inode->i_blocks = ocfs2_inode_sector_count(inode);
1274
1275 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1276 if (ret < 0)
1277 mlog_errno(ret);
1278
1279out:
1280 return ret;
1281}
1282
1283static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1284 handle_t *handle,
1285 struct inode *parent,
1286 struct inode *inode,
1287 struct buffer_head *fe_bh,
1288 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001289{
1290 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001291 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001292 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001293 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001294
1295 mlog_entry_void();
1296
Mark Fasheh87d35a72008-12-10 17:36:25 -08001297 if (ocfs2_supports_dir_trailer(osb))
1298 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1299
Mark Fasheh316f4b92007-09-07 18:21:26 -07001300 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1301 data_ac, NULL, &new_bh);
1302 if (status < 0) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306
1307 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1308
Joel Becker13723d02008-10-17 19:25:01 -07001309 status = ocfs2_journal_access_db(handle, inode, new_bh,
1310 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001311 if (status < 0) {
1312 mlog_errno(status);
1313 goto bail;
1314 }
1315 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1316
Mark Fasheh87d35a72008-12-10 17:36:25 -08001317 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1318 if (ocfs2_supports_dir_trailer(osb))
1319 ocfs2_init_dir_trailer(inode, new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001320
1321 status = ocfs2_journal_dirty(handle, new_bh);
1322 if (status < 0) {
1323 mlog_errno(status);
1324 goto bail;
1325 }
1326
1327 i_size_write(inode, inode->i_sb->s_blocksize);
1328 inode->i_nlink = 2;
1329 inode->i_blocks = ocfs2_inode_sector_count(inode);
1330 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335
1336 status = 0;
1337bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001338 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001339
1340 mlog_exit(status);
1341 return status;
1342}
1343
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001344int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1345 handle_t *handle,
1346 struct inode *parent,
1347 struct inode *inode,
1348 struct buffer_head *fe_bh,
1349 struct ocfs2_alloc_context *data_ac)
1350{
1351 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1352
1353 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1354 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1355
1356 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1357 data_ac);
1358}
1359
Mark Fasheh87d35a72008-12-10 17:36:25 -08001360/*
1361 * Expand rec_len of the rightmost dirent in a directory block so that it
1362 * contains the end of our valid space for dirents. We do this during
1363 * expansion from an inline directory to one with extents. The first dir block
1364 * in that case is taken from the inline data portion of the inode block.
1365 *
1366 * We add the dir trailer if this filesystem wants it.
1367 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001368static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
Mark Fasheh87d35a72008-12-10 17:36:25 -08001369 struct super_block *sb)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001370{
1371 struct ocfs2_dir_entry *de;
1372 struct ocfs2_dir_entry *prev_de;
1373 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001374 unsigned int new_size = sb->s_blocksize;
1375 unsigned int bytes;
1376
1377 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1378 new_size = ocfs2_dir_trailer_blk_off(sb);
1379
1380 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001381
1382 limit = start + old_size;
1383 de_buf = start;
1384 de = (struct ocfs2_dir_entry *)de_buf;
1385 do {
1386 prev_de = de;
1387 de_buf += le16_to_cpu(de->rec_len);
1388 de = (struct ocfs2_dir_entry *)de_buf;
1389 } while (de_buf < limit);
1390
1391 le16_add_cpu(&prev_de->rec_len, bytes);
1392}
1393
1394/*
1395 * We allocate enough clusters to fulfill "blocks_wanted", but set
1396 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1397 * rest automatically for us.
1398 *
1399 * *first_block_bh is a pointer to the 1st data block allocated to the
1400 * directory.
1401 */
1402static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1403 unsigned int blocks_wanted,
1404 struct buffer_head **first_block_bh)
1405{
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001406 u32 alloc, bit_off, len;
1407 struct super_block *sb = dir->i_sb;
Jan Karaa90714c2008-10-09 19:38:40 +02001408 int ret, credits = ocfs2_inline_to_extents_credits(sb);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001409 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1410 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1411 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1412 struct ocfs2_alloc_context *data_ac;
1413 struct buffer_head *dirdata_bh = NULL;
1414 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1415 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001416 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02001417 int did_quota = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001418
Joel Becker8d6220d2008-08-22 12:46:09 -07001419 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001420
1421 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1422
1423 /*
1424 * We should never need more than 2 clusters for this -
1425 * maximum dirent size is far less than one block. In fact,
1426 * the only time we'd need more than one cluster is if
1427 * blocksize == clustersize and the dirent won't fit in the
1428 * extra space that the expansion to a single block gives. As
1429 * of today, that only happens on 4k/4k file systems.
1430 */
1431 BUG_ON(alloc > 2);
1432
1433 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1434 if (ret) {
1435 mlog_errno(ret);
1436 goto out;
1437 }
1438
1439 down_write(&oi->ip_alloc_sem);
1440
1441 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001442 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001443 * extents.
1444 */
1445 if (alloc == 2)
1446 credits += OCFS2_SUBALLOC_ALLOC;
1447
1448 handle = ocfs2_start_trans(osb, credits);
1449 if (IS_ERR(handle)) {
1450 ret = PTR_ERR(handle);
1451 mlog_errno(ret);
1452 goto out_sem;
1453 }
1454
Jan Karaa90714c2008-10-09 19:38:40 +02001455 if (vfs_dq_alloc_space_nodirty(dir,
1456 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1457 ret = -EDQUOT;
1458 goto out_commit;
1459 }
1460 did_quota = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001461 /*
1462 * Try to claim as many clusters as the bitmap can give though
1463 * if we only get one now, that's enough to continue. The rest
1464 * will be claimed after the conversion to extents.
1465 */
1466 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1467 if (ret) {
1468 mlog_errno(ret);
1469 goto out_commit;
1470 }
1471
1472 /*
1473 * Operations are carefully ordered so that we set up the new
1474 * data block first. The conversion from inline data to
1475 * extents follows.
1476 */
1477 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1478 dirdata_bh = sb_getblk(sb, blkno);
1479 if (!dirdata_bh) {
1480 ret = -EIO;
1481 mlog_errno(ret);
1482 goto out_commit;
1483 }
1484
1485 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1486
Joel Becker13723d02008-10-17 19:25:01 -07001487 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1488 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001489 if (ret) {
1490 mlog_errno(ret);
1491 goto out_commit;
1492 }
1493
1494 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1495 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1496 sb->s_blocksize - i_size_read(dir));
Mark Fasheh87d35a72008-12-10 17:36:25 -08001497 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1498 if (ocfs2_supports_dir_trailer(osb))
1499 ocfs2_init_dir_trailer(dir, dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001500
1501 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1502 if (ret) {
1503 mlog_errno(ret);
1504 goto out_commit;
1505 }
1506
1507 /*
1508 * Set extent, i_size, etc on the directory. After this, the
1509 * inode should contain the same exact dirents as before and
1510 * be fully accessible from system calls.
1511 *
1512 * We let the later dirent insert modify c/mtime - to the user
1513 * the data hasn't changed.
1514 */
Joel Becker13723d02008-10-17 19:25:01 -07001515 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1516 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001517 if (ret) {
1518 mlog_errno(ret);
1519 goto out_commit;
1520 }
1521
1522 spin_lock(&oi->ip_lock);
1523 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1524 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1525 spin_unlock(&oi->ip_lock);
1526
1527 ocfs2_dinode_new_extent_list(dir, di);
1528
1529 i_size_write(dir, sb->s_blocksize);
1530 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1531
1532 di->i_size = cpu_to_le64(sb->s_blocksize);
1533 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1534 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001535
1536 /*
1537 * This should never fail as our extent list is empty and all
1538 * related blocks have been journaled already.
1539 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001540 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1541 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001542 if (ret) {
1543 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001544 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001545 }
1546
Mark Fasheh9780eb62008-08-05 11:32:46 -07001547 /*
1548 * Set i_blocks after the extent insert for the most up to
1549 * date ip_clusters value.
1550 */
1551 dir->i_blocks = ocfs2_inode_sector_count(dir);
1552
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001553 ret = ocfs2_journal_dirty(handle, di_bh);
1554 if (ret) {
1555 mlog_errno(ret);
1556 goto out_commit;
1557 }
1558
1559 /*
1560 * We asked for two clusters, but only got one in the 1st
1561 * pass. Claim the 2nd cluster as a separate extent.
1562 */
1563 if (alloc > len) {
1564 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1565 &len);
1566 if (ret) {
1567 mlog_errno(ret);
1568 goto out_commit;
1569 }
1570 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1571
Joel Beckerf99b9b72008-08-20 19:36:33 -07001572 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1573 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001574 if (ret) {
1575 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001576 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001577 }
1578 }
1579
1580 *first_block_bh = dirdata_bh;
1581 dirdata_bh = NULL;
1582
1583out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02001584 if (ret < 0 && did_quota)
1585 vfs_dq_free_space_nodirty(dir,
1586 ocfs2_clusters_to_bytes(osb->sb, 2));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001587 ocfs2_commit_trans(osb, handle);
1588
1589out_sem:
1590 up_write(&oi->ip_alloc_sem);
1591
1592out:
1593 if (data_ac)
1594 ocfs2_free_alloc_context(data_ac);
1595
1596 brelse(dirdata_bh);
1597
1598 return ret;
1599}
1600
Mark Fashehccd979b2005-12-15 14:31:24 -08001601/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001602static int ocfs2_do_extend_dir(struct super_block *sb,
1603 handle_t *handle,
1604 struct inode *dir,
1605 struct buffer_head *parent_fe_bh,
1606 struct ocfs2_alloc_context *data_ac,
1607 struct ocfs2_alloc_context *meta_ac,
1608 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001609{
1610 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02001611 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001612 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001613
1614 spin_lock(&OCFS2_I(dir)->ip_lock);
1615 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1616 spin_unlock(&OCFS2_I(dir)->ip_lock);
1617
1618 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001619 u32 offset = OCFS2_I(dir)->ip_clusters;
1620
Jan Karaa90714c2008-10-09 19:38:40 +02001621 if (vfs_dq_alloc_space_nodirty(dir,
1622 ocfs2_clusters_to_bytes(sb, 1))) {
1623 status = -EDQUOT;
1624 goto bail;
1625 }
1626 did_quota = 1;
1627
Tao Ma0eb8d472008-08-18 17:38:45 +08001628 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1629 1, 0, parent_fe_bh, handle,
1630 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001631 BUG_ON(status == -EAGAIN);
1632 if (status < 0) {
1633 mlog_errno(status);
1634 goto bail;
1635 }
1636 }
1637
Mark Fasheh8110b072007-03-22 16:53:23 -07001638 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1639 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001640 if (status < 0) {
1641 mlog_errno(status);
1642 goto bail;
1643 }
1644
1645 *new_bh = sb_getblk(sb, p_blkno);
1646 if (!*new_bh) {
1647 status = -EIO;
1648 mlog_errno(status);
1649 goto bail;
1650 }
1651 status = 0;
1652bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001653 if (did_quota && status < 0)
1654 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08001655 mlog_exit(status);
1656 return status;
1657}
1658
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001659/*
1660 * Assumes you already have a cluster lock on the directory.
1661 *
1662 * 'blocks_wanted' is only used if we have an inline directory which
1663 * is to be turned into an extent based one. The size of the dirent to
1664 * insert might be larger than the space gained by growing to just one
1665 * block, so we may have to grow the inode by two blocks in that case.
1666 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001667static int ocfs2_extend_dir(struct ocfs2_super *osb,
1668 struct inode *dir,
1669 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001670 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 struct buffer_head **new_de_bh)
1672{
1673 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001674 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001675 loff_t dir_i_size;
1676 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001677 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001678 struct ocfs2_alloc_context *data_ac = NULL;
1679 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001680 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001681 struct buffer_head *new_bh = NULL;
1682 struct ocfs2_dir_entry * de;
1683 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001684 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001685
1686 mlog_entry_void();
1687
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001688 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1689 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1690 blocks_wanted, &new_bh);
1691 if (status) {
1692 mlog_errno(status);
1693 goto bail;
1694 }
1695
1696 if (blocks_wanted == 1) {
1697 /*
1698 * If the new dirent will fit inside the space
1699 * created by pushing out to one block, then
1700 * we can complete the operation
1701 * here. Otherwise we have to expand i_size
1702 * and format the 2nd block below.
1703 */
1704 BUG_ON(new_bh == NULL);
1705 goto bail_bh;
1706 }
1707
1708 /*
1709 * Get rid of 'new_bh' - we want to format the 2nd
1710 * data block and return that instead.
1711 */
1712 brelse(new_bh);
1713 new_bh = NULL;
1714
1715 dir_i_size = i_size_read(dir);
1716 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1717 goto do_extend;
1718 }
1719
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -08001721 mlog(0, "extending dir %llu (i_size = %lld)\n",
1722 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001723
Mark Fashehccd979b2005-12-15 14:31:24 -08001724 /* dir->i_size is always block aligned. */
1725 spin_lock(&OCFS2_I(dir)->ip_lock);
1726 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1727 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001728 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001729 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001730 if (num_free_extents < 0) {
1731 status = num_free_extents;
1732 mlog_errno(status);
1733 goto bail;
1734 }
1735
1736 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001737 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 if (status < 0) {
1739 if (status != -ENOSPC)
1740 mlog_errno(status);
1741 goto bail;
1742 }
1743 }
1744
Mark Fashehda5cbf22006-10-06 18:34:35 -07001745 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 if (status < 0) {
1747 if (status != -ENOSPC)
1748 mlog_errno(status);
1749 goto bail;
1750 }
1751
Tao Ma811f9332008-08-18 17:38:43 +08001752 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001753 } else {
1754 spin_unlock(&OCFS2_I(dir)->ip_lock);
1755 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1756 }
1757
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001758do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001759 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1760 drop_alloc_sem = 1;
1761
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001762 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001763 if (IS_ERR(handle)) {
1764 status = PTR_ERR(handle);
1765 handle = NULL;
1766 mlog_errno(status);
1767 goto bail;
1768 }
1769
1770 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1771 data_ac, meta_ac, &new_bh);
1772 if (status < 0) {
1773 mlog_errno(status);
1774 goto bail;
1775 }
1776
1777 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1778
Joel Becker13723d02008-10-17 19:25:01 -07001779 status = ocfs2_journal_access_db(handle, dir, new_bh,
1780 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001781 if (status < 0) {
1782 mlog_errno(status);
1783 goto bail;
1784 }
1785 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001786
Mark Fashehccd979b2005-12-15 14:31:24 -08001787 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1788 de->inode = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001789 if (ocfs2_dir_has_trailer(dir)) {
1790 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1791 ocfs2_init_dir_trailer(dir, new_bh);
1792 } else {
1793 de->rec_len = cpu_to_le16(sb->s_blocksize);
1794 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 status = ocfs2_journal_dirty(handle, new_bh);
1796 if (status < 0) {
1797 mlog_errno(status);
1798 goto bail;
1799 }
1800
1801 dir_i_size += dir->i_sb->s_blocksize;
1802 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001803 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1805 if (status < 0) {
1806 mlog_errno(status);
1807 goto bail;
1808 }
1809
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001810bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001811 *new_de_bh = new_bh;
1812 get_bh(*new_de_bh);
1813bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001814 if (drop_alloc_sem)
1815 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001816 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001817 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001818
1819 if (data_ac)
1820 ocfs2_free_alloc_context(data_ac);
1821 if (meta_ac)
1822 ocfs2_free_alloc_context(meta_ac);
1823
Mark Fasheha81cb882008-10-07 14:25:16 -07001824 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001825
1826 mlog_exit(status);
1827 return status;
1828}
1829
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001830static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1831 const char *name, int namelen,
1832 struct buffer_head **ret_de_bh,
1833 unsigned int *blocks_wanted)
1834{
1835 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001836 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001837 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1838 struct ocfs2_dir_entry *de, *last_de = NULL;
1839 char *de_buf, *limit;
1840 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001841 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1842
1843 /*
1844 * This calculates how many free bytes we'd have in block zero, should
1845 * this function force expansion to an extent tree.
1846 */
1847 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1848 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1849 else
1850 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001851
1852 de_buf = di->id2.i_data.id_data;
1853 limit = de_buf + i_size_read(dir);
1854 rec_len = OCFS2_DIR_REC_LEN(namelen);
1855
1856 while (de_buf < limit) {
1857 de = (struct ocfs2_dir_entry *)de_buf;
1858
1859 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1860 ret = -ENOENT;
1861 goto out;
1862 }
1863 if (ocfs2_match(namelen, name, de)) {
1864 ret = -EEXIST;
1865 goto out;
1866 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001867 /*
1868 * No need to check for a trailing dirent record here as
1869 * they're not used for inline dirs.
1870 */
1871
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001872 if (ocfs2_dirent_would_fit(de, rec_len)) {
1873 /* Ok, we found a spot. Return this bh and let
1874 * the caller actually fill it in. */
1875 *ret_de_bh = di_bh;
1876 get_bh(*ret_de_bh);
1877 ret = 0;
1878 goto out;
1879 }
1880
1881 last_de = de;
1882 de_buf += le16_to_cpu(de->rec_len);
1883 offset += le16_to_cpu(de->rec_len);
1884 }
1885
1886 /*
1887 * We're going to require expansion of the directory - figure
1888 * out how many blocks we'll need so that a place for the
1889 * dirent can be found.
1890 */
1891 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001892 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001893 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1894 *blocks_wanted = 2;
1895
1896 ret = -ENOSPC;
1897out:
1898 return ret;
1899}
1900
1901static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1902 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001903{
1904 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001905 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001906 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001907 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001908 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001910 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001911
Joel Beckera22305c2008-11-13 14:49:17 -08001912 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1913 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001914 mlog_errno(status);
1915 goto bail;
1916 }
1917
1918 rec_len = OCFS2_DIR_REC_LEN(namelen);
1919 offset = 0;
1920 de = (struct ocfs2_dir_entry *) bh->b_data;
1921 while (1) {
1922 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1923 brelse(bh);
1924 bh = NULL;
1925
1926 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001927 /*
1928 * Caller will have to expand this
1929 * directory.
1930 */
1931 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 goto bail;
1933 }
Joel Beckera22305c2008-11-13 14:49:17 -08001934 status = ocfs2_read_dir_block(dir,
1935 offset >> sb->s_blocksize_bits,
1936 &bh, 0);
1937 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001938 mlog_errno(status);
1939 goto bail;
1940 }
1941 /* move to next block */
1942 de = (struct ocfs2_dir_entry *) bh->b_data;
1943 }
1944 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1945 status = -ENOENT;
1946 goto bail;
1947 }
1948 if (ocfs2_match(namelen, name, de)) {
1949 status = -EEXIST;
1950 goto bail;
1951 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001952
1953 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1954 blocksize))
1955 goto next;
1956
Mark Fasheh8553cf42007-09-13 16:29:01 -07001957 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001958 /* Ok, we found a spot. Return this bh and let
1959 * the caller actually fill it in. */
1960 *ret_de_bh = bh;
1961 get_bh(*ret_de_bh);
1962 status = 0;
1963 goto bail;
1964 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001965next:
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 offset += le16_to_cpu(de->rec_len);
1967 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1968 }
1969
1970 status = 0;
1971bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001972 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001973
1974 mlog_exit(status);
1975 return status;
1976}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001977
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001978/*
1979 * Get a directory ready for insert. Any directory allocation required
1980 * happens here. Success returns zero, and enough context in the dir
1981 * lookup result that ocfs2_add_entry() will be able complete the task
1982 * with minimal performance impact.
1983 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001984int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1985 struct inode *dir,
1986 struct buffer_head *parent_fe_bh,
1987 const char *name,
1988 int namelen,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001989 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001990{
1991 int ret;
1992 unsigned int blocks_wanted = 1;
1993 struct buffer_head *bh = NULL;
1994
1995 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1996 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1997
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001998 if (!namelen) {
1999 ret = -EINVAL;
2000 mlog_errno(ret);
2001 goto out;
2002 }
2003
2004 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2005 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
2006 namelen, &bh, &blocks_wanted);
2007 } else
2008 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
2009
2010 if (ret && ret != -ENOSPC) {
2011 mlog_errno(ret);
2012 goto out;
2013 }
2014
2015 if (ret == -ENOSPC) {
2016 /*
2017 * We have to expand the directory to add this name.
2018 */
2019 BUG_ON(bh);
2020
2021 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
2022 &bh);
2023 if (ret) {
2024 if (ret != -ENOSPC)
2025 mlog_errno(ret);
2026 goto out;
2027 }
2028
2029 BUG_ON(!bh);
2030 }
2031
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002032 lookup->dl_leaf_bh = bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002033 bh = NULL;
2034out:
Mark Fasheha81cb882008-10-07 14:25:16 -07002035 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002036 return ret;
2037}