blob: f2c4098cf337fd077b1d77835144cc454ce5e8dc [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
155/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700156 * bh passed here can be an inode block or a dir data block, depending
157 * on the inode inline data flag.
158 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700159static int ocfs2_check_dir_entry(struct inode * dir,
160 struct ocfs2_dir_entry * de,
161 struct buffer_head * bh,
162 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700163{
164 const char *error_msg = NULL;
165 const int rlen = le16_to_cpu(de->rec_len);
166
167 if (rlen < OCFS2_DIR_REC_LEN(1))
168 error_msg = "rec_len is smaller than minimal";
169 else if (rlen % 4 != 0)
170 error_msg = "rec_len % 4 != 0";
171 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
172 error_msg = "rec_len is too small for name_len";
173 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
174 error_msg = "directory entry across blocks";
175
176 if (error_msg != NULL)
177 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
178 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
179 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
180 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
181 de->name_len);
182 return error_msg == NULL ? 1 : 0;
183}
184
185static inline int ocfs2_match(int len,
186 const char * const name,
187 struct ocfs2_dir_entry *de)
188{
189 if (len != de->name_len)
190 return 0;
191 if (!de->inode)
192 return 0;
193 return !memcmp(name, de->name, len);
194}
195
196/*
197 * Returns 0 if not found, -1 on failure, and 1 on success
198 */
199static int inline ocfs2_search_dirblock(struct buffer_head *bh,
200 struct inode *dir,
201 const char *name, int namelen,
202 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700203 char *first_de,
204 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700205 struct ocfs2_dir_entry **res_dir)
206{
207 struct ocfs2_dir_entry *de;
208 char *dlimit, *de_buf;
209 int de_len;
210 int ret = 0;
211
212 mlog_entry_void();
213
Mark Fasheh23193e52007-09-12 13:01:18 -0700214 de_buf = first_de;
215 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700216
217 while (de_buf < dlimit) {
218 /* this code is executed quadratically often */
219 /* do minimal checking `by hand' */
220
221 de = (struct ocfs2_dir_entry *) de_buf;
222
223 if (de_buf + namelen <= dlimit &&
224 ocfs2_match(namelen, name, de)) {
225 /* found a match - just to be sure, do a full check */
226 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
227 ret = -1;
228 goto bail;
229 }
230 *res_dir = de;
231 ret = 1;
232 goto bail;
233 }
234
235 /* prevent looping on a bad block */
236 de_len = le16_to_cpu(de->rec_len);
237 if (de_len <= 0) {
238 ret = -1;
239 goto bail;
240 }
241
242 de_buf += de_len;
243 offset += de_len;
244 }
245
246bail:
247 mlog_exit(ret);
248 return ret;
249}
250
Mark Fasheh23193e52007-09-12 13:01:18 -0700251static struct buffer_head *ocfs2_find_entry_id(const char *name,
252 int namelen,
253 struct inode *dir,
254 struct ocfs2_dir_entry **res_dir)
255{
256 int ret, found;
257 struct buffer_head *di_bh = NULL;
258 struct ocfs2_dinode *di;
259 struct ocfs2_inline_data *data;
260
Joel Beckerb657c952008-11-13 14:49:11 -0800261 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700262 if (ret) {
263 mlog_errno(ret);
264 goto out;
265 }
266
267 di = (struct ocfs2_dinode *)di_bh->b_data;
268 data = &di->id2.i_data;
269
270 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
271 data->id_data, i_size_read(dir), res_dir);
272 if (found == 1)
273 return di_bh;
274
275 brelse(di_bh);
276out:
277 return NULL;
278}
279
Joel Beckera22305c2008-11-13 14:49:17 -0800280static int ocfs2_validate_dir_block(struct super_block *sb,
281 struct buffer_head *bh)
282{
Joel Beckerc175a512008-12-10 17:58:22 -0800283 int rc;
284 struct ocfs2_dir_block_trailer *trailer =
285 ocfs2_trailer_from_bh(bh, sb);
286
287
Joel Beckera22305c2008-11-13 14:49:17 -0800288 /*
Joel Beckerc175a512008-12-10 17:58:22 -0800289 * We don't validate dirents here, that's handled
Joel Beckera22305c2008-11-13 14:49:17 -0800290 * in-place when the code walks them.
291 */
Joel Becker970e4932008-11-13 14:49:19 -0800292 mlog(0, "Validating dirblock %llu\n",
293 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800294
Joel Beckerc175a512008-12-10 17:58:22 -0800295 BUG_ON(!buffer_uptodate(bh));
296
297 /*
298 * If the ecc fails, we return the error but otherwise
299 * leave the filesystem running. We know any error is
300 * local to this block.
301 *
302 * Note that we are safe to call this even if the directory
303 * doesn't have a trailer. Filesystems without metaecc will do
304 * nothing, and filesystems with it will have one.
305 */
306 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
307 if (rc)
308 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
309 (unsigned long long)bh->b_blocknr);
310
311 return rc;
Joel Beckera22305c2008-11-13 14:49:17 -0800312}
313
314/*
315 * This function forces all errors to -EIO for consistency with its
316 * predecessor, ocfs2_bread(). We haven't audited what returning the
317 * real error codes would do to callers. We log the real codes with
318 * mlog_errno() before we squash them.
319 */
320static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
321 struct buffer_head **bh, int flags)
322{
323 int rc = 0;
324 struct buffer_head *tmp = *bh;
Mark Fasheh87d35a72008-12-10 17:36:25 -0800325 struct ocfs2_dir_block_trailer *trailer;
Joel Beckera22305c2008-11-13 14:49:17 -0800326
Joel Becker511308d2008-11-13 14:49:21 -0800327 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
328 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800329 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800330 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800331 goto out;
332 }
333
334 /*
335 * We check the trailer here rather than in
336 * ocfs2_validate_dir_block() because that function doesn't have
337 * the inode to test.
338 */
339 if (!(flags & OCFS2_BH_READAHEAD) &&
340 ocfs2_dir_has_trailer(inode)) {
341 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
342 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
343 rc = -EINVAL;
344 ocfs2_error(inode->i_sb,
345 "Invalid dirblock #%llu: "
346 "signature = %.*s\n",
347 (unsigned long long)tmp->b_blocknr, 7,
348 trailer->db_signature);
349 goto out;
350 }
351 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
352 rc = -EINVAL;
353 ocfs2_error(inode->i_sb,
354 "Directory block #%llu has an invalid "
355 "db_blkno of %llu",
356 (unsigned long long)tmp->b_blocknr,
357 (unsigned long long)le64_to_cpu(trailer->db_blkno));
358 goto out;
359 }
360 if (le64_to_cpu(trailer->db_parent_dinode) !=
361 OCFS2_I(inode)->ip_blkno) {
362 rc = -EINVAL;
363 ocfs2_error(inode->i_sb,
364 "Directory block #%llu on dinode "
365 "#%llu has an invalid parent_dinode "
366 "of %llu",
367 (unsigned long long)tmp->b_blocknr,
368 (unsigned long long)OCFS2_I(inode)->ip_blkno,
369 (unsigned long long)le64_to_cpu(trailer->db_blkno));
370 goto out;
371 }
372 }
Joel Beckera22305c2008-11-13 14:49:17 -0800373
Joel Becker511308d2008-11-13 14:49:21 -0800374 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800375 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800376 *bh = tmp;
377
Mark Fasheh87d35a72008-12-10 17:36:25 -0800378out:
Joel Beckera22305c2008-11-13 14:49:17 -0800379 return rc ? -EIO : 0;
380}
381
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200382static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
383 struct inode *dir,
384 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700385{
386 struct super_block *sb;
387 struct buffer_head *bh_use[NAMEI_RA_SIZE];
388 struct buffer_head *bh, *ret = NULL;
389 unsigned long start, block, b;
390 int ra_max = 0; /* Number of bh's in the readahead
391 buffer, bh_use[] */
392 int ra_ptr = 0; /* Current index into readahead
393 buffer */
394 int num = 0;
395 int nblocks, i, err;
396
397 mlog_entry_void();
398
Mark Fasheh316f4b92007-09-07 18:21:26 -0700399 sb = dir->i_sb;
400
401 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
402 start = OCFS2_I(dir)->ip_dir_start_lookup;
403 if (start >= nblocks)
404 start = 0;
405 block = start;
406
407restart:
408 do {
409 /*
410 * We deal with the read-ahead logic here.
411 */
412 if (ra_ptr >= ra_max) {
413 /* Refill the readahead buffer */
414 ra_ptr = 0;
415 b = block;
416 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
417 /*
418 * Terminate if we reach the end of the
419 * directory and must wrap, or if our
420 * search has finished at this block.
421 */
422 if (b >= nblocks || (num && block == start)) {
423 bh_use[ra_max] = NULL;
424 break;
425 }
426 num++;
427
Joel Beckera22305c2008-11-13 14:49:17 -0800428 bh = NULL;
429 err = ocfs2_read_dir_block(dir, b++, &bh,
430 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700431 bh_use[ra_max] = bh;
432 }
433 }
434 if ((bh = bh_use[ra_ptr++]) == NULL)
435 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800436 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700437 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800438 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700439 ocfs2_error(dir->i_sb, "reading directory %llu, "
440 "offset %lu\n",
441 (unsigned long long)OCFS2_I(dir)->ip_blkno,
442 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700443 goto next;
444 }
445 i = ocfs2_search_dirblock(bh, dir, name, namelen,
446 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700447 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700448 res_dir);
449 if (i == 1) {
450 OCFS2_I(dir)->ip_dir_start_lookup = block;
451 ret = bh;
452 goto cleanup_and_exit;
453 } else {
454 brelse(bh);
455 if (i < 0)
456 goto cleanup_and_exit;
457 }
458 next:
459 if (++block >= nblocks)
460 block = 0;
461 } while (block != start);
462
463 /*
464 * If the directory has grown while we were searching, then
465 * search the last part of the directory before giving up.
466 */
467 block = nblocks;
468 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
469 if (block < nblocks) {
470 start = 0;
471 goto restart;
472 }
473
474cleanup_and_exit:
475 /* Clean up the read-ahead blocks */
476 for (; ra_ptr < ra_max; ra_ptr++)
477 brelse(bh_use[ra_ptr]);
478
479 mlog_exit_ptr(ret);
480 return ret;
481}
482
Mark Fasheh23193e52007-09-12 13:01:18 -0700483/*
484 * Try to find an entry of the provided name within 'dir'.
485 *
486 * If nothing was found, NULL is returned. Otherwise, a buffer_head
487 * and pointer to the dir entry are passed back.
488 *
489 * Caller can NOT assume anything about the contents of the
490 * buffer_head - it is passed back only so that it can be passed into
491 * any one of the manipulation functions (add entry, delete entry,
492 * etc). As an example, bh in the extent directory case is a data
493 * block, in the inline-data case it actually points to an inode.
494 */
495struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
496 struct inode *dir,
497 struct ocfs2_dir_entry **res_dir)
498{
499 *res_dir = NULL;
500
501 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
502 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
503
504 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
505}
506
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700507/*
508 * Update inode number and type of a previously found directory entry.
509 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700510int ocfs2_update_entry(struct inode *dir, handle_t *handle,
511 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
512 struct inode *new_entry_inode)
513{
514 int ret;
Joel Becker13723d02008-10-17 19:25:01 -0700515 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh38760e22007-09-11 17:21:56 -0700516
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700517 /*
518 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -0700519 * based directories, so no need to split this up. The only
520 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700521 */
522
Joel Becker13723d02008-10-17 19:25:01 -0700523 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
524 access = ocfs2_journal_access_di;
525
526 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -0700527 if (ret) {
528 mlog_errno(ret);
529 goto out;
530 }
531
532 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
533 ocfs2_set_de_type(de, new_entry_inode->i_mode);
534
535 ocfs2_journal_dirty(handle, de_bh);
536
537out:
538 return ret;
539}
540
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700541static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
542 struct ocfs2_dir_entry *de_del,
543 struct buffer_head *bh, char *first_de,
544 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700545{
546 struct ocfs2_dir_entry *de, *pde;
547 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -0700548 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700549
550 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
551
Joel Becker13723d02008-10-17 19:25:01 -0700552 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
553 access = ocfs2_journal_access_di;
554
Mark Fasheh316f4b92007-09-07 18:21:26 -0700555 i = 0;
556 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700557 de = (struct ocfs2_dir_entry *) first_de;
558 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700559 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
560 status = -EIO;
561 mlog_errno(status);
562 goto bail;
563 }
564 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -0700565 status = access(handle, dir, bh,
566 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700567 if (status < 0) {
568 status = -EIO;
569 mlog_errno(status);
570 goto bail;
571 }
572 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100573 le16_add_cpu(&pde->rec_len,
574 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700575 else
576 de->inode = 0;
577 dir->i_version++;
578 status = ocfs2_journal_dirty(handle, bh);
579 goto bail;
580 }
581 i += le16_to_cpu(de->rec_len);
582 pde = de;
583 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
584 }
585bail:
586 mlog_exit(status);
587 return status;
588}
589
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700590static inline int ocfs2_delete_entry_id(handle_t *handle,
591 struct inode *dir,
592 struct ocfs2_dir_entry *de_del,
593 struct buffer_head *bh)
594{
595 int ret;
596 struct buffer_head *di_bh = NULL;
597 struct ocfs2_dinode *di;
598 struct ocfs2_inline_data *data;
599
Joel Beckerb657c952008-11-13 14:49:11 -0800600 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700601 if (ret) {
602 mlog_errno(ret);
603 goto out;
604 }
605
606 di = (struct ocfs2_dinode *)di_bh->b_data;
607 data = &di->id2.i_data;
608
609 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
610 i_size_read(dir));
611
612 brelse(di_bh);
613out:
614 return ret;
615}
616
617static inline int ocfs2_delete_entry_el(handle_t *handle,
618 struct inode *dir,
619 struct ocfs2_dir_entry *de_del,
620 struct buffer_head *bh)
621{
622 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
623 bh->b_size);
624}
625
626/*
627 * ocfs2_delete_entry deletes a directory entry by merging it with the
628 * previous entry
629 */
630int ocfs2_delete_entry(handle_t *handle,
631 struct inode *dir,
632 struct ocfs2_dir_entry *de_del,
633 struct buffer_head *bh)
634{
635 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
636 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
637
638 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
639}
640
Mark Fasheh8553cf42007-09-13 16:29:01 -0700641/*
642 * Check whether 'de' has enough room to hold an entry of
643 * 'new_rec_len' bytes.
644 */
645static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
646 unsigned int new_rec_len)
647{
648 unsigned int de_really_used;
649
650 /* Check whether this is an empty record with enough space */
651 if (le64_to_cpu(de->inode) == 0 &&
652 le16_to_cpu(de->rec_len) >= new_rec_len)
653 return 1;
654
655 /*
656 * Record might have free space at the end which we can
657 * use.
658 */
659 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
660 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
661 return 1;
662
663 return 0;
664}
665
Mark Fasheh316f4b92007-09-07 18:21:26 -0700666/* we don't always have a dentry for what we want to add, so people
667 * like orphan dir can call this instead.
668 *
669 * If you pass me insert_bh, I'll skip the search of the other dir
670 * blocks and put the record in there.
671 */
672int __ocfs2_add_entry(handle_t *handle,
673 struct inode *dir,
674 const char *name, int namelen,
675 struct inode *inode, u64 blkno,
676 struct buffer_head *parent_fe_bh,
677 struct buffer_head *insert_bh)
678{
679 unsigned long offset;
680 unsigned short rec_len;
681 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700682 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
683 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700684 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700685 unsigned int size = sb->s_blocksize;
686 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700687
688 mlog_entry_void();
689
Mark Fasheh316f4b92007-09-07 18:21:26 -0700690 if (!namelen)
691 return -EINVAL;
692
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700693 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
694 data_start = di->id2.i_data.id_data;
695 size = i_size_read(dir);
696
697 BUG_ON(insert_bh != parent_fe_bh);
698 }
699
Mark Fasheh316f4b92007-09-07 18:21:26 -0700700 rec_len = OCFS2_DIR_REC_LEN(namelen);
701 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700702 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700703 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700704 BUG_ON((char *)de >= (size + data_start));
705
Mark Fasheh316f4b92007-09-07 18:21:26 -0700706 /* These checks should've already been passed by the
707 * prepare function, but I guess we can leave them
708 * here anyway. */
709 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
710 retval = -ENOENT;
711 goto bail;
712 }
713 if (ocfs2_match(namelen, name, de)) {
714 retval = -EEXIST;
715 goto bail;
716 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700717
Mark Fasheh87d35a72008-12-10 17:36:25 -0800718 /* We're guaranteed that we should have space, so we
719 * can't possibly have hit the trailer...right? */
720 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
721 "Hit dir trailer trying to insert %.*s "
722 "(namelen %d) into directory %llu. "
723 "offset is %lu, trailer offset is %d\n",
724 namelen, name, namelen,
725 (unsigned long long)parent_fe_bh->b_blocknr,
726 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
727
Mark Fasheh8553cf42007-09-13 16:29:01 -0700728 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700729 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
730 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
731 if (retval < 0) {
732 mlog_errno(retval);
733 goto bail;
734 }
735
Joel Becker13723d02008-10-17 19:25:01 -0700736 if (insert_bh == parent_fe_bh)
737 status = ocfs2_journal_access_di(handle, dir,
738 insert_bh,
739 OCFS2_JOURNAL_ACCESS_WRITE);
740 else
741 status = ocfs2_journal_access_db(handle, dir,
742 insert_bh,
743 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700744 /* By now the buffer is marked for journaling */
745 offset += le16_to_cpu(de->rec_len);
746 if (le64_to_cpu(de->inode)) {
747 de1 = (struct ocfs2_dir_entry *)((char *) de +
748 OCFS2_DIR_REC_LEN(de->name_len));
749 de1->rec_len =
750 cpu_to_le16(le16_to_cpu(de->rec_len) -
751 OCFS2_DIR_REC_LEN(de->name_len));
752 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
753 de = de1;
754 }
755 de->file_type = OCFS2_FT_UNKNOWN;
756 if (blkno) {
757 de->inode = cpu_to_le64(blkno);
758 ocfs2_set_de_type(de, inode->i_mode);
759 } else
760 de->inode = 0;
761 de->name_len = namelen;
762 memcpy(de->name, name, namelen);
763
764 dir->i_version++;
765 status = ocfs2_journal_dirty(handle, insert_bh);
766 retval = 0;
767 goto bail;
768 }
Mark Fasheh87d35a72008-12-10 17:36:25 -0800769
Mark Fasheh316f4b92007-09-07 18:21:26 -0700770 offset += le16_to_cpu(de->rec_len);
771 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
772 }
773
774 /* when you think about it, the assert above should prevent us
775 * from ever getting here. */
776 retval = -ENOSPC;
777bail:
778
779 mlog_exit(retval);
780 return retval;
781}
782
Mark Fasheh23193e52007-09-12 13:01:18 -0700783static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700784 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700785 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700786 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700787{
788 int ret, i, filldir_ret;
789 unsigned long offset = *f_pos;
790 struct buffer_head *di_bh = NULL;
791 struct ocfs2_dinode *di;
792 struct ocfs2_inline_data *data;
793 struct ocfs2_dir_entry *de;
794
Joel Beckerb657c952008-11-13 14:49:11 -0800795 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700796 if (ret) {
797 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
798 (unsigned long long)OCFS2_I(inode)->ip_blkno);
799 goto out;
800 }
801
802 di = (struct ocfs2_dinode *)di_bh->b_data;
803 data = &di->id2.i_data;
804
805 while (*f_pos < i_size_read(inode)) {
806revalidate:
807 /* If the dir block has changed since the last call to
808 * readdir(2), then we might be pointing to an invalid
809 * dirent right now. Scan from the start of the block
810 * to make sure. */
811 if (*f_version != inode->i_version) {
812 for (i = 0; i < i_size_read(inode) && i < offset; ) {
813 de = (struct ocfs2_dir_entry *)
814 (data->id_data + i);
815 /* It's too expensive to do a full
816 * dirent test each time round this
817 * loop, but we do have to test at
818 * least that it is non-zero. A
819 * failure will be detected in the
820 * dirent test below. */
821 if (le16_to_cpu(de->rec_len) <
822 OCFS2_DIR_REC_LEN(1))
823 break;
824 i += le16_to_cpu(de->rec_len);
825 }
826 *f_pos = offset = i;
827 *f_version = inode->i_version;
828 }
829
830 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
831 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
832 /* On error, skip the f_pos to the end. */
833 *f_pos = i_size_read(inode);
834 goto out;
835 }
836 offset += le16_to_cpu(de->rec_len);
837 if (le64_to_cpu(de->inode)) {
838 /* We might block in the next section
839 * if the data destination is
840 * currently swapped out. So, use a
841 * version stamp to detect whether or
842 * not the directory has been modified
843 * during the copy operation.
844 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700845 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700846 unsigned char d_type = DT_UNKNOWN;
847
848 if (de->file_type < OCFS2_FT_MAX)
849 d_type = ocfs2_filetype_table[de->file_type];
850
851 filldir_ret = filldir(priv, de->name,
852 de->name_len,
853 *f_pos,
854 le64_to_cpu(de->inode),
855 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700856 if (filldir_ret) {
857 if (filldir_err)
858 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700859 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700860 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700861 if (version != *f_version)
862 goto revalidate;
863 }
864 *f_pos += le16_to_cpu(de->rec_len);
865 }
866
867out:
868 brelse(di_bh);
869
870 return 0;
871}
872
873static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700874 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700875 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700876 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800877{
878 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700879 unsigned long offset, blk, last_ra_blk = 0;
880 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800881 struct buffer_head * bh, * tmp;
882 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800883 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700884 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885
886 stored = 0;
887 bh = NULL;
888
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700889 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700891 while (!error && !stored && *f_pos < i_size_read(inode)) {
892 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800893 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
894 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700895 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800896 continue;
897 }
898
Mark Fashehaa958872006-04-21 13:49:02 -0700899 /* The idea here is to begin with 8k read-ahead and to stay
900 * 4k ahead of our current position.
901 *
902 * TODO: Use the pagecache for this. We just need to
903 * make sure it's cluster-safe... */
904 if (!last_ra_blk
905 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
906 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800908 tmp = NULL;
909 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
910 OCFS2_BH_READAHEAD))
911 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 }
Mark Fashehaa958872006-04-21 13:49:02 -0700913 last_ra_blk = blk;
914 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800915 }
916
917revalidate:
918 /* If the dir block has changed since the last call to
919 * readdir(2), then we might be pointing to an invalid
920 * dirent right now. Scan from the start of the block
921 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700922 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800923 for (i = 0; i < sb->s_blocksize && i < offset; ) {
924 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
925 /* It's too expensive to do a full
926 * dirent test each time round this
927 * loop, but we do have to test at
928 * least that it is non-zero. A
929 * failure will be detected in the
930 * dirent test below. */
931 if (le16_to_cpu(de->rec_len) <
932 OCFS2_DIR_REC_LEN(1))
933 break;
934 i += le16_to_cpu(de->rec_len);
935 }
936 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700937 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700939 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 }
941
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700942 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800943 && offset < sb->s_blocksize) {
944 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
945 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
946 /* On error, skip the f_pos to the
947 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700948 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800949 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700950 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 }
952 offset += le16_to_cpu(de->rec_len);
953 if (le64_to_cpu(de->inode)) {
954 /* We might block in the next section
955 * if the data destination is
956 * currently swapped out. So, use a
957 * version stamp to detect whether or
958 * not the directory has been modified
959 * during the copy operation.
960 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700961 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800962 unsigned char d_type = DT_UNKNOWN;
963
964 if (de->file_type < OCFS2_FT_MAX)
965 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700966 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700968 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700969 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700971 if (error) {
972 if (filldir_err)
973 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700975 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700976 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 goto revalidate;
978 stored ++;
979 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700980 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800981 }
982 offset = 0;
983 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -0800984 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 }
986
987 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700988out:
989 return stored;
990}
991
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700992static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -0700993 loff_t *f_pos, void *priv, filldir_t filldir,
994 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700995{
996 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
997 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700998 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700999
Mark Fashehe7b34012007-09-24 14:25:27 -07001000 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1001 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001002}
1003
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001004/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001005 * This is intended to be called from inside other kernel functions,
1006 * so we fake some arguments.
1007 */
1008int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1009 filldir_t filldir)
1010{
Mark Fashehe7b34012007-09-24 14:25:27 -07001011 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001012 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001013
1014 while (*f_pos < i_size_read(inode)) {
1015 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001016 filldir, &filldir_err);
1017 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001018 break;
1019 }
1020
Mark Fashehe7b34012007-09-24 14:25:27 -07001021 if (ret > 0)
1022 ret = -EIO;
1023
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001024 return 0;
1025}
1026
1027/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001028 * ocfs2_readdir()
1029 *
1030 */
1031int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1032{
1033 int error = 0;
1034 struct inode *inode = filp->f_path.dentry->d_inode;
1035 int lock_level = 0;
1036
1037 mlog_entry("dirino=%llu\n",
1038 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1039
Mark Fashehe63aecb62007-10-18 15:30:42 -07001040 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001041 if (lock_level && error >= 0) {
1042 /* We release EX lock which used to update atime
1043 * and get PR lock again to reduce contention
1044 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001045 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001046 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001047 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001048 }
1049 if (error < 0) {
1050 if (error != -ENOENT)
1051 mlog_errno(error);
1052 /* we haven't got any yet, so propagate the error. */
1053 goto bail_nolock;
1054 }
1055
1056 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -07001057 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001058
Mark Fashehe63aecb62007-10-18 15:30:42 -07001059 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001060
Mark Fashehaa958872006-04-21 13:49:02 -07001061bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001062 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08001063
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001064 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001065}
1066
1067/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001068 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 */
1070int ocfs2_find_files_on_disk(const char *name,
1071 int namelen,
1072 u64 *blkno,
1073 struct inode *inode,
1074 struct buffer_head **dirent_bh,
1075 struct ocfs2_dir_entry **dirent)
1076{
1077 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001078
Joel Becker2b388c62006-05-10 18:28:59 -07001079 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1080 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -08001081
1082 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
1083 if (!*dirent_bh || !*dirent) {
1084 status = -ENOENT;
1085 goto leave;
1086 }
1087
1088 *blkno = le64_to_cpu((*dirent)->inode);
1089
1090 status = 0;
1091leave:
1092 if (status < 0) {
1093 *dirent = NULL;
Mark Fasheha81cb882008-10-07 14:25:16 -07001094 brelse(*dirent_bh);
1095 *dirent_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001096 }
1097
1098 mlog_exit(status);
1099 return status;
1100}
1101
Mark Fashehbe94d112007-09-11 15:22:06 -07001102/*
1103 * Convenience function for callers which just want the block number
1104 * mapped to a name and don't require the full dirent info, etc.
1105 */
1106int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1107 int namelen, u64 *blkno)
1108{
1109 int ret;
1110 struct buffer_head *bh = NULL;
1111 struct ocfs2_dir_entry *dirent = NULL;
1112
1113 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
1114 brelse(bh);
1115
1116 return ret;
1117}
1118
Mark Fashehccd979b2005-12-15 14:31:24 -08001119/* Check for a name within a directory.
1120 *
1121 * Return 0 if the name does not exist
1122 * Return -EEXIST if the directory contains the name
1123 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001124 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08001125 */
1126int ocfs2_check_dir_for_entry(struct inode *dir,
1127 const char *name,
1128 int namelen)
1129{
1130 int ret;
1131 struct buffer_head *dirent_bh = NULL;
1132 struct ocfs2_dir_entry *dirent = NULL;
1133
Mark Fashehb0697052006-03-03 10:24:33 -08001134 mlog_entry("dir %llu, name '%.*s'\n",
1135 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001136
1137 ret = -EEXIST;
1138 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1139 if (dirent_bh)
1140 goto bail;
1141
1142 ret = 0;
1143bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001144 brelse(dirent_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001145
1146 mlog_exit(ret);
1147 return ret;
1148}
1149
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001150struct ocfs2_empty_dir_priv {
1151 unsigned seen_dot;
1152 unsigned seen_dot_dot;
1153 unsigned seen_other;
1154};
1155static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1156 loff_t pos, u64 ino, unsigned type)
1157{
1158 struct ocfs2_empty_dir_priv *p = priv;
1159
1160 /*
1161 * Check the positions of "." and ".." records to be sure
1162 * they're in the correct place.
1163 */
1164 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1165 p->seen_dot = 1;
1166 return 0;
1167 }
1168
1169 if (name_len == 2 && !strncmp("..", name, 2) &&
1170 pos == OCFS2_DIR_REC_LEN(1)) {
1171 p->seen_dot_dot = 1;
1172 return 0;
1173 }
1174
1175 p->seen_other = 1;
1176 return 1;
1177}
Mark Fashehccd979b2005-12-15 14:31:24 -08001178/*
1179 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001180 *
1181 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 */
1183int ocfs2_empty_dir(struct inode *inode)
1184{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001185 int ret;
1186 loff_t start = 0;
1187 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001188
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001189 memset(&priv, 0, sizeof(priv));
1190
1191 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1192 if (ret)
1193 mlog_errno(ret);
1194
1195 if (!priv.seen_dot || !priv.seen_dot_dot) {
1196 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001197 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001198 /*
1199 * XXX: Is it really safe to allow an unlink to continue?
1200 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 return 1;
1202 }
1203
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001204 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001205}
1206
Mark Fasheh87d35a72008-12-10 17:36:25 -08001207/*
1208 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1209 * "..", which might be used during creation of a directory with a trailing
1210 * header. It is otherwise safe to ignore the return code.
1211 */
1212static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1213 struct inode *parent,
1214 char *start,
1215 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001216{
1217 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1218
1219 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1220 de->name_len = 1;
1221 de->rec_len =
1222 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1223 strcpy(de->name, ".");
1224 ocfs2_set_de_type(de, S_IFDIR);
1225
1226 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1227 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1228 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1229 de->name_len = 2;
1230 strcpy(de->name, "..");
1231 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001232
1233 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001234}
1235
1236/*
1237 * This works together with code in ocfs2_mknod_locked() which sets
1238 * the inline-data flag and initializes the inline-data section.
1239 */
1240static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1241 handle_t *handle,
1242 struct inode *parent,
1243 struct inode *inode,
1244 struct buffer_head *di_bh)
1245{
1246 int ret;
1247 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1248 struct ocfs2_inline_data *data = &di->id2.i_data;
1249 unsigned int size = le16_to_cpu(data->id_count);
1250
Joel Becker13723d02008-10-17 19:25:01 -07001251 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1252 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001253 if (ret) {
1254 mlog_errno(ret);
1255 goto out;
1256 }
1257
1258 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1259
1260 ocfs2_journal_dirty(handle, di_bh);
1261 if (ret) {
1262 mlog_errno(ret);
1263 goto out;
1264 }
1265
1266 i_size_write(inode, size);
1267 inode->i_nlink = 2;
1268 inode->i_blocks = ocfs2_inode_sector_count(inode);
1269
1270 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1271 if (ret < 0)
1272 mlog_errno(ret);
1273
1274out:
1275 return ret;
1276}
1277
1278static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1279 handle_t *handle,
1280 struct inode *parent,
1281 struct inode *inode,
1282 struct buffer_head *fe_bh,
1283 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001284{
1285 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001286 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001287 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001288 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001289
1290 mlog_entry_void();
1291
Mark Fasheh87d35a72008-12-10 17:36:25 -08001292 if (ocfs2_supports_dir_trailer(osb))
1293 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1294
Mark Fasheh316f4b92007-09-07 18:21:26 -07001295 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1296 data_ac, NULL, &new_bh);
1297 if (status < 0) {
1298 mlog_errno(status);
1299 goto bail;
1300 }
1301
1302 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1303
Joel Becker13723d02008-10-17 19:25:01 -07001304 status = ocfs2_journal_access_db(handle, inode, new_bh,
1305 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001306 if (status < 0) {
1307 mlog_errno(status);
1308 goto bail;
1309 }
1310 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1311
Mark Fasheh87d35a72008-12-10 17:36:25 -08001312 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1313 if (ocfs2_supports_dir_trailer(osb))
1314 ocfs2_init_dir_trailer(inode, new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001315
1316 status = ocfs2_journal_dirty(handle, new_bh);
1317 if (status < 0) {
1318 mlog_errno(status);
1319 goto bail;
1320 }
1321
1322 i_size_write(inode, inode->i_sb->s_blocksize);
1323 inode->i_nlink = 2;
1324 inode->i_blocks = ocfs2_inode_sector_count(inode);
1325 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1326 if (status < 0) {
1327 mlog_errno(status);
1328 goto bail;
1329 }
1330
1331 status = 0;
1332bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001333 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001334
1335 mlog_exit(status);
1336 return status;
1337}
1338
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001339int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1340 handle_t *handle,
1341 struct inode *parent,
1342 struct inode *inode,
1343 struct buffer_head *fe_bh,
1344 struct ocfs2_alloc_context *data_ac)
1345{
1346 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1347
1348 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1349 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1350
1351 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1352 data_ac);
1353}
1354
Mark Fasheh87d35a72008-12-10 17:36:25 -08001355/*
1356 * Expand rec_len of the rightmost dirent in a directory block so that it
1357 * contains the end of our valid space for dirents. We do this during
1358 * expansion from an inline directory to one with extents. The first dir block
1359 * in that case is taken from the inline data portion of the inode block.
1360 *
1361 * We add the dir trailer if this filesystem wants it.
1362 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001363static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
Mark Fasheh87d35a72008-12-10 17:36:25 -08001364 struct super_block *sb)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001365{
1366 struct ocfs2_dir_entry *de;
1367 struct ocfs2_dir_entry *prev_de;
1368 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001369 unsigned int new_size = sb->s_blocksize;
1370 unsigned int bytes;
1371
1372 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1373 new_size = ocfs2_dir_trailer_blk_off(sb);
1374
1375 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001376
1377 limit = start + old_size;
1378 de_buf = start;
1379 de = (struct ocfs2_dir_entry *)de_buf;
1380 do {
1381 prev_de = de;
1382 de_buf += le16_to_cpu(de->rec_len);
1383 de = (struct ocfs2_dir_entry *)de_buf;
1384 } while (de_buf < limit);
1385
1386 le16_add_cpu(&prev_de->rec_len, bytes);
1387}
1388
1389/*
1390 * We allocate enough clusters to fulfill "blocks_wanted", but set
1391 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1392 * rest automatically for us.
1393 *
1394 * *first_block_bh is a pointer to the 1st data block allocated to the
1395 * directory.
1396 */
1397static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1398 unsigned int blocks_wanted,
1399 struct buffer_head **first_block_bh)
1400{
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001401 u32 alloc, bit_off, len;
1402 struct super_block *sb = dir->i_sb;
Jan Karaa90714c2008-10-09 19:38:40 +02001403 int ret, credits = ocfs2_inline_to_extents_credits(sb);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001404 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1405 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1406 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1407 struct ocfs2_alloc_context *data_ac;
1408 struct buffer_head *dirdata_bh = NULL;
1409 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1410 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001411 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02001412 int did_quota = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001413
Joel Becker8d6220d2008-08-22 12:46:09 -07001414 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001415
1416 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1417
1418 /*
1419 * We should never need more than 2 clusters for this -
1420 * maximum dirent size is far less than one block. In fact,
1421 * the only time we'd need more than one cluster is if
1422 * blocksize == clustersize and the dirent won't fit in the
1423 * extra space that the expansion to a single block gives. As
1424 * of today, that only happens on 4k/4k file systems.
1425 */
1426 BUG_ON(alloc > 2);
1427
1428 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out;
1432 }
1433
1434 down_write(&oi->ip_alloc_sem);
1435
1436 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001437 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001438 * extents.
1439 */
1440 if (alloc == 2)
1441 credits += OCFS2_SUBALLOC_ALLOC;
1442
1443 handle = ocfs2_start_trans(osb, credits);
1444 if (IS_ERR(handle)) {
1445 ret = PTR_ERR(handle);
1446 mlog_errno(ret);
1447 goto out_sem;
1448 }
1449
Jan Karaa90714c2008-10-09 19:38:40 +02001450 if (vfs_dq_alloc_space_nodirty(dir,
1451 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1452 ret = -EDQUOT;
1453 goto out_commit;
1454 }
1455 did_quota = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001456 /*
1457 * Try to claim as many clusters as the bitmap can give though
1458 * if we only get one now, that's enough to continue. The rest
1459 * will be claimed after the conversion to extents.
1460 */
1461 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1462 if (ret) {
1463 mlog_errno(ret);
1464 goto out_commit;
1465 }
1466
1467 /*
1468 * Operations are carefully ordered so that we set up the new
1469 * data block first. The conversion from inline data to
1470 * extents follows.
1471 */
1472 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1473 dirdata_bh = sb_getblk(sb, blkno);
1474 if (!dirdata_bh) {
1475 ret = -EIO;
1476 mlog_errno(ret);
1477 goto out_commit;
1478 }
1479
1480 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1481
Joel Becker13723d02008-10-17 19:25:01 -07001482 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1483 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001484 if (ret) {
1485 mlog_errno(ret);
1486 goto out_commit;
1487 }
1488
1489 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1490 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1491 sb->s_blocksize - i_size_read(dir));
Mark Fasheh87d35a72008-12-10 17:36:25 -08001492 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1493 if (ocfs2_supports_dir_trailer(osb))
1494 ocfs2_init_dir_trailer(dir, dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001495
1496 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1497 if (ret) {
1498 mlog_errno(ret);
1499 goto out_commit;
1500 }
1501
1502 /*
1503 * Set extent, i_size, etc on the directory. After this, the
1504 * inode should contain the same exact dirents as before and
1505 * be fully accessible from system calls.
1506 *
1507 * We let the later dirent insert modify c/mtime - to the user
1508 * the data hasn't changed.
1509 */
Joel Becker13723d02008-10-17 19:25:01 -07001510 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1511 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001512 if (ret) {
1513 mlog_errno(ret);
1514 goto out_commit;
1515 }
1516
1517 spin_lock(&oi->ip_lock);
1518 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1519 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1520 spin_unlock(&oi->ip_lock);
1521
1522 ocfs2_dinode_new_extent_list(dir, di);
1523
1524 i_size_write(dir, sb->s_blocksize);
1525 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1526
1527 di->i_size = cpu_to_le64(sb->s_blocksize);
1528 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1529 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001530
1531 /*
1532 * This should never fail as our extent list is empty and all
1533 * related blocks have been journaled already.
1534 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001535 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1536 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001537 if (ret) {
1538 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001539 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001540 }
1541
Mark Fasheh9780eb62008-08-05 11:32:46 -07001542 /*
1543 * Set i_blocks after the extent insert for the most up to
1544 * date ip_clusters value.
1545 */
1546 dir->i_blocks = ocfs2_inode_sector_count(dir);
1547
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001548 ret = ocfs2_journal_dirty(handle, di_bh);
1549 if (ret) {
1550 mlog_errno(ret);
1551 goto out_commit;
1552 }
1553
1554 /*
1555 * We asked for two clusters, but only got one in the 1st
1556 * pass. Claim the 2nd cluster as a separate extent.
1557 */
1558 if (alloc > len) {
1559 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1560 &len);
1561 if (ret) {
1562 mlog_errno(ret);
1563 goto out_commit;
1564 }
1565 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1566
Joel Beckerf99b9b72008-08-20 19:36:33 -07001567 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1568 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001569 if (ret) {
1570 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001571 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001572 }
1573 }
1574
1575 *first_block_bh = dirdata_bh;
1576 dirdata_bh = NULL;
1577
1578out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02001579 if (ret < 0 && did_quota)
1580 vfs_dq_free_space_nodirty(dir,
1581 ocfs2_clusters_to_bytes(osb->sb, 2));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001582 ocfs2_commit_trans(osb, handle);
1583
1584out_sem:
1585 up_write(&oi->ip_alloc_sem);
1586
1587out:
1588 if (data_ac)
1589 ocfs2_free_alloc_context(data_ac);
1590
1591 brelse(dirdata_bh);
1592
1593 return ret;
1594}
1595
Mark Fashehccd979b2005-12-15 14:31:24 -08001596/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001597static int ocfs2_do_extend_dir(struct super_block *sb,
1598 handle_t *handle,
1599 struct inode *dir,
1600 struct buffer_head *parent_fe_bh,
1601 struct ocfs2_alloc_context *data_ac,
1602 struct ocfs2_alloc_context *meta_ac,
1603 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001604{
1605 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02001606 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001607 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001608
1609 spin_lock(&OCFS2_I(dir)->ip_lock);
1610 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1611 spin_unlock(&OCFS2_I(dir)->ip_lock);
1612
1613 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001614 u32 offset = OCFS2_I(dir)->ip_clusters;
1615
Jan Karaa90714c2008-10-09 19:38:40 +02001616 if (vfs_dq_alloc_space_nodirty(dir,
1617 ocfs2_clusters_to_bytes(sb, 1))) {
1618 status = -EDQUOT;
1619 goto bail;
1620 }
1621 did_quota = 1;
1622
Tao Ma0eb8d472008-08-18 17:38:45 +08001623 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1624 1, 0, parent_fe_bh, handle,
1625 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001626 BUG_ON(status == -EAGAIN);
1627 if (status < 0) {
1628 mlog_errno(status);
1629 goto bail;
1630 }
1631 }
1632
Mark Fasheh8110b072007-03-22 16:53:23 -07001633 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1634 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001635 if (status < 0) {
1636 mlog_errno(status);
1637 goto bail;
1638 }
1639
1640 *new_bh = sb_getblk(sb, p_blkno);
1641 if (!*new_bh) {
1642 status = -EIO;
1643 mlog_errno(status);
1644 goto bail;
1645 }
1646 status = 0;
1647bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001648 if (did_quota && status < 0)
1649 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08001650 mlog_exit(status);
1651 return status;
1652}
1653
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001654/*
1655 * Assumes you already have a cluster lock on the directory.
1656 *
1657 * 'blocks_wanted' is only used if we have an inline directory which
1658 * is to be turned into an extent based one. The size of the dirent to
1659 * insert might be larger than the space gained by growing to just one
1660 * block, so we may have to grow the inode by two blocks in that case.
1661 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001662static int ocfs2_extend_dir(struct ocfs2_super *osb,
1663 struct inode *dir,
1664 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001665 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001666 struct buffer_head **new_de_bh)
1667{
1668 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001669 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001670 loff_t dir_i_size;
1671 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001672 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 struct ocfs2_alloc_context *data_ac = NULL;
1674 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001675 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001676 struct buffer_head *new_bh = NULL;
1677 struct ocfs2_dir_entry * de;
1678 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001679 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001680
1681 mlog_entry_void();
1682
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001683 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1684 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1685 blocks_wanted, &new_bh);
1686 if (status) {
1687 mlog_errno(status);
1688 goto bail;
1689 }
1690
1691 if (blocks_wanted == 1) {
1692 /*
1693 * If the new dirent will fit inside the space
1694 * created by pushing out to one block, then
1695 * we can complete the operation
1696 * here. Otherwise we have to expand i_size
1697 * and format the 2nd block below.
1698 */
1699 BUG_ON(new_bh == NULL);
1700 goto bail_bh;
1701 }
1702
1703 /*
1704 * Get rid of 'new_bh' - we want to format the 2nd
1705 * data block and return that instead.
1706 */
1707 brelse(new_bh);
1708 new_bh = NULL;
1709
1710 dir_i_size = i_size_read(dir);
1711 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1712 goto do_extend;
1713 }
1714
Mark Fashehccd979b2005-12-15 14:31:24 -08001715 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -08001716 mlog(0, "extending dir %llu (i_size = %lld)\n",
1717 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001718
Mark Fashehccd979b2005-12-15 14:31:24 -08001719 /* dir->i_size is always block aligned. */
1720 spin_lock(&OCFS2_I(dir)->ip_lock);
1721 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1722 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001723 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001724 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 if (num_free_extents < 0) {
1726 status = num_free_extents;
1727 mlog_errno(status);
1728 goto bail;
1729 }
1730
1731 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001732 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001733 if (status < 0) {
1734 if (status != -ENOSPC)
1735 mlog_errno(status);
1736 goto bail;
1737 }
1738 }
1739
Mark Fashehda5cbf22006-10-06 18:34:35 -07001740 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001741 if (status < 0) {
1742 if (status != -ENOSPC)
1743 mlog_errno(status);
1744 goto bail;
1745 }
1746
Tao Ma811f9332008-08-18 17:38:43 +08001747 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001748 } else {
1749 spin_unlock(&OCFS2_I(dir)->ip_lock);
1750 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1751 }
1752
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001753do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001754 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1755 drop_alloc_sem = 1;
1756
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001757 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001758 if (IS_ERR(handle)) {
1759 status = PTR_ERR(handle);
1760 handle = NULL;
1761 mlog_errno(status);
1762 goto bail;
1763 }
1764
1765 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1766 data_ac, meta_ac, &new_bh);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto bail;
1770 }
1771
1772 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1773
Joel Becker13723d02008-10-17 19:25:01 -07001774 status = ocfs2_journal_access_db(handle, dir, new_bh,
1775 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 if (status < 0) {
1777 mlog_errno(status);
1778 goto bail;
1779 }
1780 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001781
Mark Fashehccd979b2005-12-15 14:31:24 -08001782 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1783 de->inode = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001784 if (ocfs2_dir_has_trailer(dir)) {
1785 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1786 ocfs2_init_dir_trailer(dir, new_bh);
1787 } else {
1788 de->rec_len = cpu_to_le16(sb->s_blocksize);
1789 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 status = ocfs2_journal_dirty(handle, new_bh);
1791 if (status < 0) {
1792 mlog_errno(status);
1793 goto bail;
1794 }
1795
1796 dir_i_size += dir->i_sb->s_blocksize;
1797 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001798 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001799 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1800 if (status < 0) {
1801 mlog_errno(status);
1802 goto bail;
1803 }
1804
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001805bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001806 *new_de_bh = new_bh;
1807 get_bh(*new_de_bh);
1808bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001809 if (drop_alloc_sem)
1810 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001811 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001812 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001813
1814 if (data_ac)
1815 ocfs2_free_alloc_context(data_ac);
1816 if (meta_ac)
1817 ocfs2_free_alloc_context(meta_ac);
1818
Mark Fasheha81cb882008-10-07 14:25:16 -07001819 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001820
1821 mlog_exit(status);
1822 return status;
1823}
1824
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001825static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1826 const char *name, int namelen,
1827 struct buffer_head **ret_de_bh,
1828 unsigned int *blocks_wanted)
1829{
1830 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001831 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001832 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1833 struct ocfs2_dir_entry *de, *last_de = NULL;
1834 char *de_buf, *limit;
1835 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001836 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1837
1838 /*
1839 * This calculates how many free bytes we'd have in block zero, should
1840 * this function force expansion to an extent tree.
1841 */
1842 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1843 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1844 else
1845 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001846
1847 de_buf = di->id2.i_data.id_data;
1848 limit = de_buf + i_size_read(dir);
1849 rec_len = OCFS2_DIR_REC_LEN(namelen);
1850
1851 while (de_buf < limit) {
1852 de = (struct ocfs2_dir_entry *)de_buf;
1853
1854 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1855 ret = -ENOENT;
1856 goto out;
1857 }
1858 if (ocfs2_match(namelen, name, de)) {
1859 ret = -EEXIST;
1860 goto out;
1861 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001862 /*
1863 * No need to check for a trailing dirent record here as
1864 * they're not used for inline dirs.
1865 */
1866
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001867 if (ocfs2_dirent_would_fit(de, rec_len)) {
1868 /* Ok, we found a spot. Return this bh and let
1869 * the caller actually fill it in. */
1870 *ret_de_bh = di_bh;
1871 get_bh(*ret_de_bh);
1872 ret = 0;
1873 goto out;
1874 }
1875
1876 last_de = de;
1877 de_buf += le16_to_cpu(de->rec_len);
1878 offset += le16_to_cpu(de->rec_len);
1879 }
1880
1881 /*
1882 * We're going to require expansion of the directory - figure
1883 * out how many blocks we'll need so that a place for the
1884 * dirent can be found.
1885 */
1886 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001887 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001888 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1889 *blocks_wanted = 2;
1890
1891 ret = -ENOSPC;
1892out:
1893 return ret;
1894}
1895
1896static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1897 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001898{
1899 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001900 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001901 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001902 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001903 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001905 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001906
Joel Beckera22305c2008-11-13 14:49:17 -08001907 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1908 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 mlog_errno(status);
1910 goto bail;
1911 }
1912
1913 rec_len = OCFS2_DIR_REC_LEN(namelen);
1914 offset = 0;
1915 de = (struct ocfs2_dir_entry *) bh->b_data;
1916 while (1) {
1917 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1918 brelse(bh);
1919 bh = NULL;
1920
1921 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001922 /*
1923 * Caller will have to expand this
1924 * directory.
1925 */
1926 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001927 goto bail;
1928 }
Joel Beckera22305c2008-11-13 14:49:17 -08001929 status = ocfs2_read_dir_block(dir,
1930 offset >> sb->s_blocksize_bits,
1931 &bh, 0);
1932 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 mlog_errno(status);
1934 goto bail;
1935 }
1936 /* move to next block */
1937 de = (struct ocfs2_dir_entry *) bh->b_data;
1938 }
1939 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1940 status = -ENOENT;
1941 goto bail;
1942 }
1943 if (ocfs2_match(namelen, name, de)) {
1944 status = -EEXIST;
1945 goto bail;
1946 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001947
1948 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1949 blocksize))
1950 goto next;
1951
Mark Fasheh8553cf42007-09-13 16:29:01 -07001952 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001953 /* Ok, we found a spot. Return this bh and let
1954 * the caller actually fill it in. */
1955 *ret_de_bh = bh;
1956 get_bh(*ret_de_bh);
1957 status = 0;
1958 goto bail;
1959 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001960next:
Mark Fashehccd979b2005-12-15 14:31:24 -08001961 offset += le16_to_cpu(de->rec_len);
1962 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1963 }
1964
1965 status = 0;
1966bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001967 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001968
1969 mlog_exit(status);
1970 return status;
1971}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001972
1973int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1974 struct inode *dir,
1975 struct buffer_head *parent_fe_bh,
1976 const char *name,
1977 int namelen,
1978 struct buffer_head **ret_de_bh)
1979{
1980 int ret;
1981 unsigned int blocks_wanted = 1;
1982 struct buffer_head *bh = NULL;
1983
1984 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1985 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1986
1987 *ret_de_bh = NULL;
1988
1989 if (!namelen) {
1990 ret = -EINVAL;
1991 mlog_errno(ret);
1992 goto out;
1993 }
1994
1995 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1996 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1997 namelen, &bh, &blocks_wanted);
1998 } else
1999 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
2000
2001 if (ret && ret != -ENOSPC) {
2002 mlog_errno(ret);
2003 goto out;
2004 }
2005
2006 if (ret == -ENOSPC) {
2007 /*
2008 * We have to expand the directory to add this name.
2009 */
2010 BUG_ON(bh);
2011
2012 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
2013 &bh);
2014 if (ret) {
2015 if (ret != -ENOSPC)
2016 mlog_errno(ret);
2017 goto out;
2018 }
2019
2020 BUG_ON(!bh);
2021 }
2022
2023 *ret_de_bh = bh;
2024 bh = NULL;
2025out:
Mark Fasheha81cb882008-10-07 14:25:16 -07002026 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002027 return ret;
2028}