blob: 45e4e03d8f71ce1f61a1c7d1d509d1bc29493177 [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"
51#include "dir.h"
52#include "dlmglue.h"
53#include "extent_map.h"
54#include "file.h"
55#include "inode.h"
56#include "journal.h"
57#include "namei.h"
58#include "suballoc.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070059#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080060#include "uptodate.h"
61
62#include "buffer_head_io.h"
63
Mark Fasheh316f4b92007-09-07 18:21:26 -070064#define NAMEI_RA_CHUNKS 2
65#define NAMEI_RA_BLOCKS 4
66#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
67#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
68
Mark Fashehccd979b2005-12-15 14:31:24 -080069static unsigned char ocfs2_filetype_table[] = {
70 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
71};
72
73static int ocfs2_extend_dir(struct ocfs2_super *osb,
74 struct inode *dir,
75 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -070076 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -080077 struct buffer_head **new_de_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -070078static int ocfs2_do_extend_dir(struct super_block *sb,
79 handle_t *handle,
80 struct inode *dir,
81 struct buffer_head *parent_fe_bh,
82 struct ocfs2_alloc_context *data_ac,
83 struct ocfs2_alloc_context *meta_ac,
84 struct buffer_head **new_bh);
85
Mark Fasheh23193e52007-09-12 13:01:18 -070086/*
87 * bh passed here can be an inode block or a dir data block, depending
88 * on the inode inline data flag.
89 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -070090static int ocfs2_check_dir_entry(struct inode * dir,
91 struct ocfs2_dir_entry * de,
92 struct buffer_head * bh,
93 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -070094{
95 const char *error_msg = NULL;
96 const int rlen = le16_to_cpu(de->rec_len);
97
98 if (rlen < OCFS2_DIR_REC_LEN(1))
99 error_msg = "rec_len is smaller than minimal";
100 else if (rlen % 4 != 0)
101 error_msg = "rec_len % 4 != 0";
102 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
103 error_msg = "rec_len is too small for name_len";
104 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
105 error_msg = "directory entry across blocks";
106
107 if (error_msg != NULL)
108 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
109 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
110 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
111 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
112 de->name_len);
113 return error_msg == NULL ? 1 : 0;
114}
115
116static inline int ocfs2_match(int len,
117 const char * const name,
118 struct ocfs2_dir_entry *de)
119{
120 if (len != de->name_len)
121 return 0;
122 if (!de->inode)
123 return 0;
124 return !memcmp(name, de->name, len);
125}
126
127/*
128 * Returns 0 if not found, -1 on failure, and 1 on success
129 */
130static int inline ocfs2_search_dirblock(struct buffer_head *bh,
131 struct inode *dir,
132 const char *name, int namelen,
133 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700134 char *first_de,
135 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700136 struct ocfs2_dir_entry **res_dir)
137{
138 struct ocfs2_dir_entry *de;
139 char *dlimit, *de_buf;
140 int de_len;
141 int ret = 0;
142
143 mlog_entry_void();
144
Mark Fasheh23193e52007-09-12 13:01:18 -0700145 de_buf = first_de;
146 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700147
148 while (de_buf < dlimit) {
149 /* this code is executed quadratically often */
150 /* do minimal checking `by hand' */
151
152 de = (struct ocfs2_dir_entry *) de_buf;
153
154 if (de_buf + namelen <= dlimit &&
155 ocfs2_match(namelen, name, de)) {
156 /* found a match - just to be sure, do a full check */
157 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
158 ret = -1;
159 goto bail;
160 }
161 *res_dir = de;
162 ret = 1;
163 goto bail;
164 }
165
166 /* prevent looping on a bad block */
167 de_len = le16_to_cpu(de->rec_len);
168 if (de_len <= 0) {
169 ret = -1;
170 goto bail;
171 }
172
173 de_buf += de_len;
174 offset += de_len;
175 }
176
177bail:
178 mlog_exit(ret);
179 return ret;
180}
181
Mark Fasheh23193e52007-09-12 13:01:18 -0700182static struct buffer_head *ocfs2_find_entry_id(const char *name,
183 int namelen,
184 struct inode *dir,
185 struct ocfs2_dir_entry **res_dir)
186{
187 int ret, found;
188 struct buffer_head *di_bh = NULL;
189 struct ocfs2_dinode *di;
190 struct ocfs2_inline_data *data;
191
Joel Beckerb657c952008-11-13 14:49:11 -0800192 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700193 if (ret) {
194 mlog_errno(ret);
195 goto out;
196 }
197
198 di = (struct ocfs2_dinode *)di_bh->b_data;
199 data = &di->id2.i_data;
200
201 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
202 data->id_data, i_size_read(dir), res_dir);
203 if (found == 1)
204 return di_bh;
205
206 brelse(di_bh);
207out:
208 return NULL;
209}
210
Joel Beckera22305c2008-11-13 14:49:17 -0800211static int ocfs2_validate_dir_block(struct super_block *sb,
212 struct buffer_head *bh)
213{
214 /*
215 * Nothing yet. We don't validate dirents here, that's handled
216 * in-place when the code walks them.
217 */
Joel Becker970e4932008-11-13 14:49:19 -0800218 mlog(0, "Validating dirblock %llu\n",
219 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800220
221 return 0;
222}
223
224/*
225 * This function forces all errors to -EIO for consistency with its
226 * predecessor, ocfs2_bread(). We haven't audited what returning the
227 * real error codes would do to callers. We log the real codes with
228 * mlog_errno() before we squash them.
229 */
230static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
231 struct buffer_head **bh, int flags)
232{
233 int rc = 0;
234 struct buffer_head *tmp = *bh;
Joel Beckera22305c2008-11-13 14:49:17 -0800235
Joel Becker511308d2008-11-13 14:49:21 -0800236 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
237 ocfs2_validate_dir_block);
238 if (rc)
Joel Beckera22305c2008-11-13 14:49:17 -0800239 mlog_errno(rc);
Joel Beckera22305c2008-11-13 14:49:17 -0800240
Joel Becker511308d2008-11-13 14:49:21 -0800241 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
242 if (!rc && !*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800243 *bh = tmp;
244
Joel Beckera22305c2008-11-13 14:49:17 -0800245 return rc ? -EIO : 0;
246}
247
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200248static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
249 struct inode *dir,
250 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700251{
252 struct super_block *sb;
253 struct buffer_head *bh_use[NAMEI_RA_SIZE];
254 struct buffer_head *bh, *ret = NULL;
255 unsigned long start, block, b;
256 int ra_max = 0; /* Number of bh's in the readahead
257 buffer, bh_use[] */
258 int ra_ptr = 0; /* Current index into readahead
259 buffer */
260 int num = 0;
261 int nblocks, i, err;
262
263 mlog_entry_void();
264
Mark Fasheh316f4b92007-09-07 18:21:26 -0700265 sb = dir->i_sb;
266
267 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
268 start = OCFS2_I(dir)->ip_dir_start_lookup;
269 if (start >= nblocks)
270 start = 0;
271 block = start;
272
273restart:
274 do {
275 /*
276 * We deal with the read-ahead logic here.
277 */
278 if (ra_ptr >= ra_max) {
279 /* Refill the readahead buffer */
280 ra_ptr = 0;
281 b = block;
282 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
283 /*
284 * Terminate if we reach the end of the
285 * directory and must wrap, or if our
286 * search has finished at this block.
287 */
288 if (b >= nblocks || (num && block == start)) {
289 bh_use[ra_max] = NULL;
290 break;
291 }
292 num++;
293
Joel Beckera22305c2008-11-13 14:49:17 -0800294 bh = NULL;
295 err = ocfs2_read_dir_block(dir, b++, &bh,
296 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700297 bh_use[ra_max] = bh;
298 }
299 }
300 if ((bh = bh_use[ra_ptr++]) == NULL)
301 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800302 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700303 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800304 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700305 ocfs2_error(dir->i_sb, "reading directory %llu, "
306 "offset %lu\n",
307 (unsigned long long)OCFS2_I(dir)->ip_blkno,
308 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700309 goto next;
310 }
311 i = ocfs2_search_dirblock(bh, dir, name, namelen,
312 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700313 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700314 res_dir);
315 if (i == 1) {
316 OCFS2_I(dir)->ip_dir_start_lookup = block;
317 ret = bh;
318 goto cleanup_and_exit;
319 } else {
320 brelse(bh);
321 if (i < 0)
322 goto cleanup_and_exit;
323 }
324 next:
325 if (++block >= nblocks)
326 block = 0;
327 } while (block != start);
328
329 /*
330 * If the directory has grown while we were searching, then
331 * search the last part of the directory before giving up.
332 */
333 block = nblocks;
334 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
335 if (block < nblocks) {
336 start = 0;
337 goto restart;
338 }
339
340cleanup_and_exit:
341 /* Clean up the read-ahead blocks */
342 for (; ra_ptr < ra_max; ra_ptr++)
343 brelse(bh_use[ra_ptr]);
344
345 mlog_exit_ptr(ret);
346 return ret;
347}
348
Mark Fasheh23193e52007-09-12 13:01:18 -0700349/*
350 * Try to find an entry of the provided name within 'dir'.
351 *
352 * If nothing was found, NULL is returned. Otherwise, a buffer_head
353 * and pointer to the dir entry are passed back.
354 *
355 * Caller can NOT assume anything about the contents of the
356 * buffer_head - it is passed back only so that it can be passed into
357 * any one of the manipulation functions (add entry, delete entry,
358 * etc). As an example, bh in the extent directory case is a data
359 * block, in the inline-data case it actually points to an inode.
360 */
361struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
362 struct inode *dir,
363 struct ocfs2_dir_entry **res_dir)
364{
365 *res_dir = NULL;
366
367 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
368 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
369
370 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
371}
372
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700373/*
374 * Update inode number and type of a previously found directory entry.
375 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700376int ocfs2_update_entry(struct inode *dir, handle_t *handle,
377 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
378 struct inode *new_entry_inode)
379{
380 int ret;
Joel Becker13723d02008-10-17 19:25:01 -0700381 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh38760e22007-09-11 17:21:56 -0700382
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700383 /*
384 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -0700385 * based directories, so no need to split this up. The only
386 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700387 */
388
Joel Becker13723d02008-10-17 19:25:01 -0700389 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
390 access = ocfs2_journal_access_di;
391
392 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -0700393 if (ret) {
394 mlog_errno(ret);
395 goto out;
396 }
397
398 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
399 ocfs2_set_de_type(de, new_entry_inode->i_mode);
400
401 ocfs2_journal_dirty(handle, de_bh);
402
403out:
404 return ret;
405}
406
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700407static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
408 struct ocfs2_dir_entry *de_del,
409 struct buffer_head *bh, char *first_de,
410 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700411{
412 struct ocfs2_dir_entry *de, *pde;
413 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -0700414 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700415
416 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
417
Joel Becker13723d02008-10-17 19:25:01 -0700418 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
419 access = ocfs2_journal_access_di;
420
Mark Fasheh316f4b92007-09-07 18:21:26 -0700421 i = 0;
422 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700423 de = (struct ocfs2_dir_entry *) first_de;
424 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700425 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
426 status = -EIO;
427 mlog_errno(status);
428 goto bail;
429 }
430 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -0700431 status = access(handle, dir, bh,
432 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700433 if (status < 0) {
434 status = -EIO;
435 mlog_errno(status);
436 goto bail;
437 }
438 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100439 le16_add_cpu(&pde->rec_len,
440 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700441 else
442 de->inode = 0;
443 dir->i_version++;
444 status = ocfs2_journal_dirty(handle, bh);
445 goto bail;
446 }
447 i += le16_to_cpu(de->rec_len);
448 pde = de;
449 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
450 }
451bail:
452 mlog_exit(status);
453 return status;
454}
455
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700456static inline int ocfs2_delete_entry_id(handle_t *handle,
457 struct inode *dir,
458 struct ocfs2_dir_entry *de_del,
459 struct buffer_head *bh)
460{
461 int ret;
462 struct buffer_head *di_bh = NULL;
463 struct ocfs2_dinode *di;
464 struct ocfs2_inline_data *data;
465
Joel Beckerb657c952008-11-13 14:49:11 -0800466 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700467 if (ret) {
468 mlog_errno(ret);
469 goto out;
470 }
471
472 di = (struct ocfs2_dinode *)di_bh->b_data;
473 data = &di->id2.i_data;
474
475 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
476 i_size_read(dir));
477
478 brelse(di_bh);
479out:
480 return ret;
481}
482
483static inline int ocfs2_delete_entry_el(handle_t *handle,
484 struct inode *dir,
485 struct ocfs2_dir_entry *de_del,
486 struct buffer_head *bh)
487{
488 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
489 bh->b_size);
490}
491
492/*
493 * ocfs2_delete_entry deletes a directory entry by merging it with the
494 * previous entry
495 */
496int ocfs2_delete_entry(handle_t *handle,
497 struct inode *dir,
498 struct ocfs2_dir_entry *de_del,
499 struct buffer_head *bh)
500{
501 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
502 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
503
504 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
505}
506
Mark Fasheh8553cf42007-09-13 16:29:01 -0700507/*
508 * Check whether 'de' has enough room to hold an entry of
509 * 'new_rec_len' bytes.
510 */
511static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
512 unsigned int new_rec_len)
513{
514 unsigned int de_really_used;
515
516 /* Check whether this is an empty record with enough space */
517 if (le64_to_cpu(de->inode) == 0 &&
518 le16_to_cpu(de->rec_len) >= new_rec_len)
519 return 1;
520
521 /*
522 * Record might have free space at the end which we can
523 * use.
524 */
525 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
526 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
527 return 1;
528
529 return 0;
530}
531
Mark Fasheh316f4b92007-09-07 18:21:26 -0700532/* we don't always have a dentry for what we want to add, so people
533 * like orphan dir can call this instead.
534 *
535 * If you pass me insert_bh, I'll skip the search of the other dir
536 * blocks and put the record in there.
537 */
538int __ocfs2_add_entry(handle_t *handle,
539 struct inode *dir,
540 const char *name, int namelen,
541 struct inode *inode, u64 blkno,
542 struct buffer_head *parent_fe_bh,
543 struct buffer_head *insert_bh)
544{
545 unsigned long offset;
546 unsigned short rec_len;
547 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700548 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
549 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700550 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700551 unsigned int size = sb->s_blocksize;
552 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700553
554 mlog_entry_void();
555
Mark Fasheh316f4b92007-09-07 18:21:26 -0700556 if (!namelen)
557 return -EINVAL;
558
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700559 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
560 data_start = di->id2.i_data.id_data;
561 size = i_size_read(dir);
562
563 BUG_ON(insert_bh != parent_fe_bh);
564 }
565
Mark Fasheh316f4b92007-09-07 18:21:26 -0700566 rec_len = OCFS2_DIR_REC_LEN(namelen);
567 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700568 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700569 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700570 BUG_ON((char *)de >= (size + data_start));
571
Mark Fasheh316f4b92007-09-07 18:21:26 -0700572 /* These checks should've already been passed by the
573 * prepare function, but I guess we can leave them
574 * here anyway. */
575 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
576 retval = -ENOENT;
577 goto bail;
578 }
579 if (ocfs2_match(namelen, name, de)) {
580 retval = -EEXIST;
581 goto bail;
582 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700583
584 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700585 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
586 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
587 if (retval < 0) {
588 mlog_errno(retval);
589 goto bail;
590 }
591
Joel Becker13723d02008-10-17 19:25:01 -0700592 if (insert_bh == parent_fe_bh)
593 status = ocfs2_journal_access_di(handle, dir,
594 insert_bh,
595 OCFS2_JOURNAL_ACCESS_WRITE);
596 else
597 status = ocfs2_journal_access_db(handle, dir,
598 insert_bh,
599 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700600 /* By now the buffer is marked for journaling */
601 offset += le16_to_cpu(de->rec_len);
602 if (le64_to_cpu(de->inode)) {
603 de1 = (struct ocfs2_dir_entry *)((char *) de +
604 OCFS2_DIR_REC_LEN(de->name_len));
605 de1->rec_len =
606 cpu_to_le16(le16_to_cpu(de->rec_len) -
607 OCFS2_DIR_REC_LEN(de->name_len));
608 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
609 de = de1;
610 }
611 de->file_type = OCFS2_FT_UNKNOWN;
612 if (blkno) {
613 de->inode = cpu_to_le64(blkno);
614 ocfs2_set_de_type(de, inode->i_mode);
615 } else
616 de->inode = 0;
617 de->name_len = namelen;
618 memcpy(de->name, name, namelen);
619
620 dir->i_version++;
621 status = ocfs2_journal_dirty(handle, insert_bh);
622 retval = 0;
623 goto bail;
624 }
625 offset += le16_to_cpu(de->rec_len);
626 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
627 }
628
629 /* when you think about it, the assert above should prevent us
630 * from ever getting here. */
631 retval = -ENOSPC;
632bail:
633
634 mlog_exit(retval);
635 return retval;
636}
637
Mark Fasheh23193e52007-09-12 13:01:18 -0700638static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700639 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700640 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700641 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700642{
643 int ret, i, filldir_ret;
644 unsigned long offset = *f_pos;
645 struct buffer_head *di_bh = NULL;
646 struct ocfs2_dinode *di;
647 struct ocfs2_inline_data *data;
648 struct ocfs2_dir_entry *de;
649
Joel Beckerb657c952008-11-13 14:49:11 -0800650 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700651 if (ret) {
652 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
653 (unsigned long long)OCFS2_I(inode)->ip_blkno);
654 goto out;
655 }
656
657 di = (struct ocfs2_dinode *)di_bh->b_data;
658 data = &di->id2.i_data;
659
660 while (*f_pos < i_size_read(inode)) {
661revalidate:
662 /* If the dir block has changed since the last call to
663 * readdir(2), then we might be pointing to an invalid
664 * dirent right now. Scan from the start of the block
665 * to make sure. */
666 if (*f_version != inode->i_version) {
667 for (i = 0; i < i_size_read(inode) && i < offset; ) {
668 de = (struct ocfs2_dir_entry *)
669 (data->id_data + i);
670 /* It's too expensive to do a full
671 * dirent test each time round this
672 * loop, but we do have to test at
673 * least that it is non-zero. A
674 * failure will be detected in the
675 * dirent test below. */
676 if (le16_to_cpu(de->rec_len) <
677 OCFS2_DIR_REC_LEN(1))
678 break;
679 i += le16_to_cpu(de->rec_len);
680 }
681 *f_pos = offset = i;
682 *f_version = inode->i_version;
683 }
684
685 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
686 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
687 /* On error, skip the f_pos to the end. */
688 *f_pos = i_size_read(inode);
689 goto out;
690 }
691 offset += le16_to_cpu(de->rec_len);
692 if (le64_to_cpu(de->inode)) {
693 /* We might block in the next section
694 * if the data destination is
695 * currently swapped out. So, use a
696 * version stamp to detect whether or
697 * not the directory has been modified
698 * during the copy operation.
699 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700700 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700701 unsigned char d_type = DT_UNKNOWN;
702
703 if (de->file_type < OCFS2_FT_MAX)
704 d_type = ocfs2_filetype_table[de->file_type];
705
706 filldir_ret = filldir(priv, de->name,
707 de->name_len,
708 *f_pos,
709 le64_to_cpu(de->inode),
710 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700711 if (filldir_ret) {
712 if (filldir_err)
713 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700714 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700715 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700716 if (version != *f_version)
717 goto revalidate;
718 }
719 *f_pos += le16_to_cpu(de->rec_len);
720 }
721
722out:
723 brelse(di_bh);
724
725 return 0;
726}
727
728static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700729 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700730 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700731 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800732{
733 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700734 unsigned long offset, blk, last_ra_blk = 0;
735 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 struct buffer_head * bh, * tmp;
737 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800738 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700739 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800740
741 stored = 0;
742 bh = NULL;
743
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700744 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800745
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700746 while (!error && !stored && *f_pos < i_size_read(inode)) {
747 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800748 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
749 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700750 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800751 continue;
752 }
753
Mark Fashehaa958872006-04-21 13:49:02 -0700754 /* The idea here is to begin with 8k read-ahead and to stay
755 * 4k ahead of our current position.
756 *
757 * TODO: Use the pagecache for this. We just need to
758 * make sure it's cluster-safe... */
759 if (!last_ra_blk
760 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
761 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800762 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800763 tmp = NULL;
764 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
765 OCFS2_BH_READAHEAD))
766 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800767 }
Mark Fashehaa958872006-04-21 13:49:02 -0700768 last_ra_blk = blk;
769 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800770 }
771
772revalidate:
773 /* If the dir block has changed since the last call to
774 * readdir(2), then we might be pointing to an invalid
775 * dirent right now. Scan from the start of the block
776 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700777 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800778 for (i = 0; i < sb->s_blocksize && i < offset; ) {
779 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
780 /* It's too expensive to do a full
781 * dirent test each time round this
782 * loop, but we do have to test at
783 * least that it is non-zero. A
784 * failure will be detected in the
785 * dirent test below. */
786 if (le16_to_cpu(de->rec_len) <
787 OCFS2_DIR_REC_LEN(1))
788 break;
789 i += le16_to_cpu(de->rec_len);
790 }
791 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700792 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800793 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700794 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800795 }
796
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700797 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 && offset < sb->s_blocksize) {
799 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
800 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
801 /* On error, skip the f_pos to the
802 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700803 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700805 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 }
807 offset += le16_to_cpu(de->rec_len);
808 if (le64_to_cpu(de->inode)) {
809 /* We might block in the next section
810 * if the data destination is
811 * currently swapped out. So, use a
812 * version stamp to detect whether or
813 * not the directory has been modified
814 * during the copy operation.
815 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700816 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 unsigned char d_type = DT_UNKNOWN;
818
819 if (de->file_type < OCFS2_FT_MAX)
820 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700821 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800822 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700823 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700824 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700826 if (error) {
827 if (filldir_err)
828 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700830 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700831 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800832 goto revalidate;
833 stored ++;
834 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700835 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 }
837 offset = 0;
838 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -0800839 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800840 }
841
842 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700843out:
844 return stored;
845}
846
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700847static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -0700848 loff_t *f_pos, void *priv, filldir_t filldir,
849 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700850{
851 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
852 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700853 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700854
Mark Fashehe7b34012007-09-24 14:25:27 -0700855 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
856 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700857}
858
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700859/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700860 * This is intended to be called from inside other kernel functions,
861 * so we fake some arguments.
862 */
863int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
864 filldir_t filldir)
865{
Mark Fashehe7b34012007-09-24 14:25:27 -0700866 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700867 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700868
869 while (*f_pos < i_size_read(inode)) {
870 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700871 filldir, &filldir_err);
872 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700873 break;
874 }
875
Mark Fashehe7b34012007-09-24 14:25:27 -0700876 if (ret > 0)
877 ret = -EIO;
878
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700879 return 0;
880}
881
882/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700883 * ocfs2_readdir()
884 *
885 */
886int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
887{
888 int error = 0;
889 struct inode *inode = filp->f_path.dentry->d_inode;
890 int lock_level = 0;
891
892 mlog_entry("dirino=%llu\n",
893 (unsigned long long)OCFS2_I(inode)->ip_blkno);
894
Mark Fashehe63aecb62007-10-18 15:30:42 -0700895 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700896 if (lock_level && error >= 0) {
897 /* We release EX lock which used to update atime
898 * and get PR lock again to reduce contention
899 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700900 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700901 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700902 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700903 }
904 if (error < 0) {
905 if (error != -ENOENT)
906 mlog_errno(error);
907 /* we haven't got any yet, so propagate the error. */
908 goto bail_nolock;
909 }
910
911 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -0700912 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700913
Mark Fashehe63aecb62007-10-18 15:30:42 -0700914 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915
Mark Fashehaa958872006-04-21 13:49:02 -0700916bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700917 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -0800918
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700919 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800920}
921
922/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800923 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -0800924 */
925int ocfs2_find_files_on_disk(const char *name,
926 int namelen,
927 u64 *blkno,
928 struct inode *inode,
929 struct buffer_head **dirent_bh,
930 struct ocfs2_dir_entry **dirent)
931{
932 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800933
Joel Becker2b388c62006-05-10 18:28:59 -0700934 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
935 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800936
937 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
938 if (!*dirent_bh || !*dirent) {
939 status = -ENOENT;
940 goto leave;
941 }
942
943 *blkno = le64_to_cpu((*dirent)->inode);
944
945 status = 0;
946leave:
947 if (status < 0) {
948 *dirent = NULL;
Mark Fasheha81cb882008-10-07 14:25:16 -0700949 brelse(*dirent_bh);
950 *dirent_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 }
952
953 mlog_exit(status);
954 return status;
955}
956
Mark Fashehbe94d112007-09-11 15:22:06 -0700957/*
958 * Convenience function for callers which just want the block number
959 * mapped to a name and don't require the full dirent info, etc.
960 */
961int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
962 int namelen, u64 *blkno)
963{
964 int ret;
965 struct buffer_head *bh = NULL;
966 struct ocfs2_dir_entry *dirent = NULL;
967
968 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
969 brelse(bh);
970
971 return ret;
972}
973
Mark Fashehccd979b2005-12-15 14:31:24 -0800974/* Check for a name within a directory.
975 *
976 * Return 0 if the name does not exist
977 * Return -EEXIST if the directory contains the name
978 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800979 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 */
981int ocfs2_check_dir_for_entry(struct inode *dir,
982 const char *name,
983 int namelen)
984{
985 int ret;
986 struct buffer_head *dirent_bh = NULL;
987 struct ocfs2_dir_entry *dirent = NULL;
988
Mark Fashehb0697052006-03-03 10:24:33 -0800989 mlog_entry("dir %llu, name '%.*s'\n",
990 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800991
992 ret = -EEXIST;
993 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
994 if (dirent_bh)
995 goto bail;
996
997 ret = 0;
998bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700999 brelse(dirent_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001000
1001 mlog_exit(ret);
1002 return ret;
1003}
1004
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001005struct ocfs2_empty_dir_priv {
1006 unsigned seen_dot;
1007 unsigned seen_dot_dot;
1008 unsigned seen_other;
1009};
1010static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1011 loff_t pos, u64 ino, unsigned type)
1012{
1013 struct ocfs2_empty_dir_priv *p = priv;
1014
1015 /*
1016 * Check the positions of "." and ".." records to be sure
1017 * they're in the correct place.
1018 */
1019 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1020 p->seen_dot = 1;
1021 return 0;
1022 }
1023
1024 if (name_len == 2 && !strncmp("..", name, 2) &&
1025 pos == OCFS2_DIR_REC_LEN(1)) {
1026 p->seen_dot_dot = 1;
1027 return 0;
1028 }
1029
1030 p->seen_other = 1;
1031 return 1;
1032}
Mark Fashehccd979b2005-12-15 14:31:24 -08001033/*
1034 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001035 *
1036 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 */
1038int ocfs2_empty_dir(struct inode *inode)
1039{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001040 int ret;
1041 loff_t start = 0;
1042 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001043
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001044 memset(&priv, 0, sizeof(priv));
1045
1046 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1047 if (ret)
1048 mlog_errno(ret);
1049
1050 if (!priv.seen_dot || !priv.seen_dot_dot) {
1051 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001052 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001053 /*
1054 * XXX: Is it really safe to allow an unlink to continue?
1055 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 return 1;
1057 }
1058
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001059 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001060}
1061
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001062static void ocfs2_fill_initial_dirents(struct inode *inode,
1063 struct inode *parent,
1064 char *start, unsigned int size)
1065{
1066 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1067
1068 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1069 de->name_len = 1;
1070 de->rec_len =
1071 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1072 strcpy(de->name, ".");
1073 ocfs2_set_de_type(de, S_IFDIR);
1074
1075 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1076 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1077 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1078 de->name_len = 2;
1079 strcpy(de->name, "..");
1080 ocfs2_set_de_type(de, S_IFDIR);
1081}
1082
1083/*
1084 * This works together with code in ocfs2_mknod_locked() which sets
1085 * the inline-data flag and initializes the inline-data section.
1086 */
1087static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1088 handle_t *handle,
1089 struct inode *parent,
1090 struct inode *inode,
1091 struct buffer_head *di_bh)
1092{
1093 int ret;
1094 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1095 struct ocfs2_inline_data *data = &di->id2.i_data;
1096 unsigned int size = le16_to_cpu(data->id_count);
1097
Joel Becker13723d02008-10-17 19:25:01 -07001098 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1099 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001100 if (ret) {
1101 mlog_errno(ret);
1102 goto out;
1103 }
1104
1105 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1106
1107 ocfs2_journal_dirty(handle, di_bh);
1108 if (ret) {
1109 mlog_errno(ret);
1110 goto out;
1111 }
1112
1113 i_size_write(inode, size);
1114 inode->i_nlink = 2;
1115 inode->i_blocks = ocfs2_inode_sector_count(inode);
1116
1117 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1118 if (ret < 0)
1119 mlog_errno(ret);
1120
1121out:
1122 return ret;
1123}
1124
1125static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1126 handle_t *handle,
1127 struct inode *parent,
1128 struct inode *inode,
1129 struct buffer_head *fe_bh,
1130 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001131{
1132 int status;
1133 struct buffer_head *new_bh = NULL;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001134
1135 mlog_entry_void();
1136
1137 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1138 data_ac, NULL, &new_bh);
1139 if (status < 0) {
1140 mlog_errno(status);
1141 goto bail;
1142 }
1143
1144 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1145
Joel Becker13723d02008-10-17 19:25:01 -07001146 status = ocfs2_journal_access_db(handle, inode, new_bh,
1147 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001148 if (status < 0) {
1149 mlog_errno(status);
1150 goto bail;
1151 }
1152 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1153
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001154 ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
1155 osb->sb->s_blocksize);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001156
1157 status = ocfs2_journal_dirty(handle, new_bh);
1158 if (status < 0) {
1159 mlog_errno(status);
1160 goto bail;
1161 }
1162
1163 i_size_write(inode, inode->i_sb->s_blocksize);
1164 inode->i_nlink = 2;
1165 inode->i_blocks = ocfs2_inode_sector_count(inode);
1166 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1167 if (status < 0) {
1168 mlog_errno(status);
1169 goto bail;
1170 }
1171
1172 status = 0;
1173bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001174 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001175
1176 mlog_exit(status);
1177 return status;
1178}
1179
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001180int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1181 handle_t *handle,
1182 struct inode *parent,
1183 struct inode *inode,
1184 struct buffer_head *fe_bh,
1185 struct ocfs2_alloc_context *data_ac)
1186{
1187 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1188
1189 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1190 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1191
1192 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1193 data_ac);
1194}
1195
1196static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1197 unsigned int new_size)
1198{
1199 struct ocfs2_dir_entry *de;
1200 struct ocfs2_dir_entry *prev_de;
1201 char *de_buf, *limit;
1202 unsigned int bytes = new_size - old_size;
1203
1204 limit = start + old_size;
1205 de_buf = start;
1206 de = (struct ocfs2_dir_entry *)de_buf;
1207 do {
1208 prev_de = de;
1209 de_buf += le16_to_cpu(de->rec_len);
1210 de = (struct ocfs2_dir_entry *)de_buf;
1211 } while (de_buf < limit);
1212
1213 le16_add_cpu(&prev_de->rec_len, bytes);
1214}
1215
1216/*
1217 * We allocate enough clusters to fulfill "blocks_wanted", but set
1218 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1219 * rest automatically for us.
1220 *
1221 * *first_block_bh is a pointer to the 1st data block allocated to the
1222 * directory.
1223 */
1224static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1225 unsigned int blocks_wanted,
1226 struct buffer_head **first_block_bh)
1227{
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001228 u32 alloc, bit_off, len;
1229 struct super_block *sb = dir->i_sb;
Jan Karaa90714c2008-10-09 19:38:40 +02001230 int ret, credits = ocfs2_inline_to_extents_credits(sb);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001231 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1232 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1233 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1234 struct ocfs2_alloc_context *data_ac;
1235 struct buffer_head *dirdata_bh = NULL;
1236 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1237 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001238 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02001239 int did_quota = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001240
Joel Becker8d6220d2008-08-22 12:46:09 -07001241 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001242
1243 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1244
1245 /*
1246 * We should never need more than 2 clusters for this -
1247 * maximum dirent size is far less than one block. In fact,
1248 * the only time we'd need more than one cluster is if
1249 * blocksize == clustersize and the dirent won't fit in the
1250 * extra space that the expansion to a single block gives. As
1251 * of today, that only happens on 4k/4k file systems.
1252 */
1253 BUG_ON(alloc > 2);
1254
1255 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1256 if (ret) {
1257 mlog_errno(ret);
1258 goto out;
1259 }
1260
1261 down_write(&oi->ip_alloc_sem);
1262
1263 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001264 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001265 * extents.
1266 */
1267 if (alloc == 2)
1268 credits += OCFS2_SUBALLOC_ALLOC;
1269
1270 handle = ocfs2_start_trans(osb, credits);
1271 if (IS_ERR(handle)) {
1272 ret = PTR_ERR(handle);
1273 mlog_errno(ret);
1274 goto out_sem;
1275 }
1276
Jan Karaa90714c2008-10-09 19:38:40 +02001277 if (vfs_dq_alloc_space_nodirty(dir,
1278 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1279 ret = -EDQUOT;
1280 goto out_commit;
1281 }
1282 did_quota = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001283 /*
1284 * Try to claim as many clusters as the bitmap can give though
1285 * if we only get one now, that's enough to continue. The rest
1286 * will be claimed after the conversion to extents.
1287 */
1288 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1289 if (ret) {
1290 mlog_errno(ret);
1291 goto out_commit;
1292 }
1293
1294 /*
1295 * Operations are carefully ordered so that we set up the new
1296 * data block first. The conversion from inline data to
1297 * extents follows.
1298 */
1299 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1300 dirdata_bh = sb_getblk(sb, blkno);
1301 if (!dirdata_bh) {
1302 ret = -EIO;
1303 mlog_errno(ret);
1304 goto out_commit;
1305 }
1306
1307 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1308
Joel Becker13723d02008-10-17 19:25:01 -07001309 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1310 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001311 if (ret) {
1312 mlog_errno(ret);
1313 goto out_commit;
1314 }
1315
1316 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1317 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1318 sb->s_blocksize - i_size_read(dir));
1319 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
1320 sb->s_blocksize);
1321
1322 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1323 if (ret) {
1324 mlog_errno(ret);
1325 goto out_commit;
1326 }
1327
1328 /*
1329 * Set extent, i_size, etc on the directory. After this, the
1330 * inode should contain the same exact dirents as before and
1331 * be fully accessible from system calls.
1332 *
1333 * We let the later dirent insert modify c/mtime - to the user
1334 * the data hasn't changed.
1335 */
Joel Becker13723d02008-10-17 19:25:01 -07001336 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1337 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001338 if (ret) {
1339 mlog_errno(ret);
1340 goto out_commit;
1341 }
1342
1343 spin_lock(&oi->ip_lock);
1344 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1345 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1346 spin_unlock(&oi->ip_lock);
1347
1348 ocfs2_dinode_new_extent_list(dir, di);
1349
1350 i_size_write(dir, sb->s_blocksize);
1351 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1352
1353 di->i_size = cpu_to_le64(sb->s_blocksize);
1354 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1355 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001356
1357 /*
1358 * This should never fail as our extent list is empty and all
1359 * related blocks have been journaled already.
1360 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001361 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1362 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001363 if (ret) {
1364 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001365 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001366 }
1367
Mark Fasheh9780eb62008-08-05 11:32:46 -07001368 /*
1369 * Set i_blocks after the extent insert for the most up to
1370 * date ip_clusters value.
1371 */
1372 dir->i_blocks = ocfs2_inode_sector_count(dir);
1373
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001374 ret = ocfs2_journal_dirty(handle, di_bh);
1375 if (ret) {
1376 mlog_errno(ret);
1377 goto out_commit;
1378 }
1379
1380 /*
1381 * We asked for two clusters, but only got one in the 1st
1382 * pass. Claim the 2nd cluster as a separate extent.
1383 */
1384 if (alloc > len) {
1385 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1386 &len);
1387 if (ret) {
1388 mlog_errno(ret);
1389 goto out_commit;
1390 }
1391 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1392
Joel Beckerf99b9b72008-08-20 19:36:33 -07001393 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1394 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001395 if (ret) {
1396 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001397 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001398 }
1399 }
1400
1401 *first_block_bh = dirdata_bh;
1402 dirdata_bh = NULL;
1403
1404out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02001405 if (ret < 0 && did_quota)
1406 vfs_dq_free_space_nodirty(dir,
1407 ocfs2_clusters_to_bytes(osb->sb, 2));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001408 ocfs2_commit_trans(osb, handle);
1409
1410out_sem:
1411 up_write(&oi->ip_alloc_sem);
1412
1413out:
1414 if (data_ac)
1415 ocfs2_free_alloc_context(data_ac);
1416
1417 brelse(dirdata_bh);
1418
1419 return ret;
1420}
1421
Mark Fashehccd979b2005-12-15 14:31:24 -08001422/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001423static int ocfs2_do_extend_dir(struct super_block *sb,
1424 handle_t *handle,
1425 struct inode *dir,
1426 struct buffer_head *parent_fe_bh,
1427 struct ocfs2_alloc_context *data_ac,
1428 struct ocfs2_alloc_context *meta_ac,
1429 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001430{
1431 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02001432 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001433 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001434
1435 spin_lock(&OCFS2_I(dir)->ip_lock);
1436 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1437 spin_unlock(&OCFS2_I(dir)->ip_lock);
1438
1439 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001440 u32 offset = OCFS2_I(dir)->ip_clusters;
1441
Jan Karaa90714c2008-10-09 19:38:40 +02001442 if (vfs_dq_alloc_space_nodirty(dir,
1443 ocfs2_clusters_to_bytes(sb, 1))) {
1444 status = -EDQUOT;
1445 goto bail;
1446 }
1447 did_quota = 1;
1448
Tao Ma0eb8d472008-08-18 17:38:45 +08001449 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1450 1, 0, parent_fe_bh, handle,
1451 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452 BUG_ON(status == -EAGAIN);
1453 if (status < 0) {
1454 mlog_errno(status);
1455 goto bail;
1456 }
1457 }
1458
Mark Fasheh8110b072007-03-22 16:53:23 -07001459 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1460 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001461 if (status < 0) {
1462 mlog_errno(status);
1463 goto bail;
1464 }
1465
1466 *new_bh = sb_getblk(sb, p_blkno);
1467 if (!*new_bh) {
1468 status = -EIO;
1469 mlog_errno(status);
1470 goto bail;
1471 }
1472 status = 0;
1473bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001474 if (did_quota && status < 0)
1475 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08001476 mlog_exit(status);
1477 return status;
1478}
1479
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001480/*
1481 * Assumes you already have a cluster lock on the directory.
1482 *
1483 * 'blocks_wanted' is only used if we have an inline directory which
1484 * is to be turned into an extent based one. The size of the dirent to
1485 * insert might be larger than the space gained by growing to just one
1486 * block, so we may have to grow the inode by two blocks in that case.
1487 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001488static int ocfs2_extend_dir(struct ocfs2_super *osb,
1489 struct inode *dir,
1490 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001491 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001492 struct buffer_head **new_de_bh)
1493{
1494 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001495 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001496 loff_t dir_i_size;
1497 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001498 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 struct ocfs2_alloc_context *data_ac = NULL;
1500 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001501 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001502 struct buffer_head *new_bh = NULL;
1503 struct ocfs2_dir_entry * de;
1504 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001505 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001506
1507 mlog_entry_void();
1508
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001509 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1510 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1511 blocks_wanted, &new_bh);
1512 if (status) {
1513 mlog_errno(status);
1514 goto bail;
1515 }
1516
1517 if (blocks_wanted == 1) {
1518 /*
1519 * If the new dirent will fit inside the space
1520 * created by pushing out to one block, then
1521 * we can complete the operation
1522 * here. Otherwise we have to expand i_size
1523 * and format the 2nd block below.
1524 */
1525 BUG_ON(new_bh == NULL);
1526 goto bail_bh;
1527 }
1528
1529 /*
1530 * Get rid of 'new_bh' - we want to format the 2nd
1531 * data block and return that instead.
1532 */
1533 brelse(new_bh);
1534 new_bh = NULL;
1535
1536 dir_i_size = i_size_read(dir);
1537 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1538 goto do_extend;
1539 }
1540
Mark Fashehccd979b2005-12-15 14:31:24 -08001541 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -08001542 mlog(0, "extending dir %llu (i_size = %lld)\n",
1543 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001544
Mark Fashehccd979b2005-12-15 14:31:24 -08001545 /* dir->i_size is always block aligned. */
1546 spin_lock(&OCFS2_I(dir)->ip_lock);
1547 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1548 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001549 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001550 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001551 if (num_free_extents < 0) {
1552 status = num_free_extents;
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
1557 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001558 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001559 if (status < 0) {
1560 if (status != -ENOSPC)
1561 mlog_errno(status);
1562 goto bail;
1563 }
1564 }
1565
Mark Fashehda5cbf22006-10-06 18:34:35 -07001566 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001567 if (status < 0) {
1568 if (status != -ENOSPC)
1569 mlog_errno(status);
1570 goto bail;
1571 }
1572
Tao Ma811f9332008-08-18 17:38:43 +08001573 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001574 } else {
1575 spin_unlock(&OCFS2_I(dir)->ip_lock);
1576 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1577 }
1578
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001579do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001580 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1581 drop_alloc_sem = 1;
1582
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001583 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001584 if (IS_ERR(handle)) {
1585 status = PTR_ERR(handle);
1586 handle = NULL;
1587 mlog_errno(status);
1588 goto bail;
1589 }
1590
1591 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1592 data_ac, meta_ac, &new_bh);
1593 if (status < 0) {
1594 mlog_errno(status);
1595 goto bail;
1596 }
1597
1598 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1599
Joel Becker13723d02008-10-17 19:25:01 -07001600 status = ocfs2_journal_access_db(handle, dir, new_bh,
1601 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001602 if (status < 0) {
1603 mlog_errno(status);
1604 goto bail;
1605 }
1606 memset(new_bh->b_data, 0, sb->s_blocksize);
1607 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1608 de->inode = 0;
1609 de->rec_len = cpu_to_le16(sb->s_blocksize);
1610 status = ocfs2_journal_dirty(handle, new_bh);
1611 if (status < 0) {
1612 mlog_errno(status);
1613 goto bail;
1614 }
1615
1616 dir_i_size += dir->i_sb->s_blocksize;
1617 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001618 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001619 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1620 if (status < 0) {
1621 mlog_errno(status);
1622 goto bail;
1623 }
1624
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001625bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001626 *new_de_bh = new_bh;
1627 get_bh(*new_de_bh);
1628bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001629 if (drop_alloc_sem)
1630 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001631 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001632 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001633
1634 if (data_ac)
1635 ocfs2_free_alloc_context(data_ac);
1636 if (meta_ac)
1637 ocfs2_free_alloc_context(meta_ac);
1638
Mark Fasheha81cb882008-10-07 14:25:16 -07001639 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001640
1641 mlog_exit(status);
1642 return status;
1643}
1644
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001645static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1646 const char *name, int namelen,
1647 struct buffer_head **ret_de_bh,
1648 unsigned int *blocks_wanted)
1649{
1650 int ret;
1651 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1652 struct ocfs2_dir_entry *de, *last_de = NULL;
1653 char *de_buf, *limit;
1654 unsigned long offset = 0;
1655 unsigned int rec_len, new_rec_len;
1656
1657 de_buf = di->id2.i_data.id_data;
1658 limit = de_buf + i_size_read(dir);
1659 rec_len = OCFS2_DIR_REC_LEN(namelen);
1660
1661 while (de_buf < limit) {
1662 de = (struct ocfs2_dir_entry *)de_buf;
1663
1664 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1665 ret = -ENOENT;
1666 goto out;
1667 }
1668 if (ocfs2_match(namelen, name, de)) {
1669 ret = -EEXIST;
1670 goto out;
1671 }
1672 if (ocfs2_dirent_would_fit(de, rec_len)) {
1673 /* Ok, we found a spot. Return this bh and let
1674 * the caller actually fill it in. */
1675 *ret_de_bh = di_bh;
1676 get_bh(*ret_de_bh);
1677 ret = 0;
1678 goto out;
1679 }
1680
1681 last_de = de;
1682 de_buf += le16_to_cpu(de->rec_len);
1683 offset += le16_to_cpu(de->rec_len);
1684 }
1685
1686 /*
1687 * We're going to require expansion of the directory - figure
1688 * out how many blocks we'll need so that a place for the
1689 * dirent can be found.
1690 */
1691 *blocks_wanted = 1;
1692 new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
1693 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1694 *blocks_wanted = 2;
1695
1696 ret = -ENOSPC;
1697out:
1698 return ret;
1699}
1700
1701static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1702 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001703{
1704 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001705 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001706 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001707 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001708 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001709 int status;
1710
Joel Beckera22305c2008-11-13 14:49:17 -08001711 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1712 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001713 mlog_errno(status);
1714 goto bail;
1715 }
1716
1717 rec_len = OCFS2_DIR_REC_LEN(namelen);
1718 offset = 0;
1719 de = (struct ocfs2_dir_entry *) bh->b_data;
1720 while (1) {
1721 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1722 brelse(bh);
1723 bh = NULL;
1724
1725 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001726 /*
1727 * Caller will have to expand this
1728 * directory.
1729 */
1730 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 goto bail;
1732 }
Joel Beckera22305c2008-11-13 14:49:17 -08001733 status = ocfs2_read_dir_block(dir,
1734 offset >> sb->s_blocksize_bits,
1735 &bh, 0);
1736 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001737 mlog_errno(status);
1738 goto bail;
1739 }
1740 /* move to next block */
1741 de = (struct ocfs2_dir_entry *) bh->b_data;
1742 }
1743 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1744 status = -ENOENT;
1745 goto bail;
1746 }
1747 if (ocfs2_match(namelen, name, de)) {
1748 status = -EEXIST;
1749 goto bail;
1750 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001751 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001752 /* Ok, we found a spot. Return this bh and let
1753 * the caller actually fill it in. */
1754 *ret_de_bh = bh;
1755 get_bh(*ret_de_bh);
1756 status = 0;
1757 goto bail;
1758 }
1759 offset += le16_to_cpu(de->rec_len);
1760 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1761 }
1762
1763 status = 0;
1764bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001765 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001766
1767 mlog_exit(status);
1768 return status;
1769}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001770
1771int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1772 struct inode *dir,
1773 struct buffer_head *parent_fe_bh,
1774 const char *name,
1775 int namelen,
1776 struct buffer_head **ret_de_bh)
1777{
1778 int ret;
1779 unsigned int blocks_wanted = 1;
1780 struct buffer_head *bh = NULL;
1781
1782 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1783 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1784
1785 *ret_de_bh = NULL;
1786
1787 if (!namelen) {
1788 ret = -EINVAL;
1789 mlog_errno(ret);
1790 goto out;
1791 }
1792
1793 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1794 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1795 namelen, &bh, &blocks_wanted);
1796 } else
1797 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1798
1799 if (ret && ret != -ENOSPC) {
1800 mlog_errno(ret);
1801 goto out;
1802 }
1803
1804 if (ret == -ENOSPC) {
1805 /*
1806 * We have to expand the directory to add this name.
1807 */
1808 BUG_ON(bh);
1809
1810 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1811 &bh);
1812 if (ret) {
1813 if (ret != -ENOSPC)
1814 mlog_errno(ret);
1815 goto out;
1816 }
1817
1818 BUG_ON(!bh);
1819 }
1820
1821 *ret_de_bh = bh;
1822 bh = NULL;
1823out:
Mark Fasheha81cb882008-10-07 14:25:16 -07001824 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001825 return ret;
1826}