blob: 1efd0ab680cffe8974ed892b810419393fe09f5b [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/*
Mark Fasheh87d35a72008-12-10 17:36:25 -080087 * These are distinct checks because future versions of the file system will
88 * want to have a trailing dirent structure independent of indexing.
89 */
90static int ocfs2_dir_has_trailer(struct inode *dir)
91{
92 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
93 return 0;
94
95 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
96}
97
98static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
99{
100 return ocfs2_meta_ecc(osb);
101}
102
103static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
104{
105 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
106}
107
108#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
109
110/*
111 * XXX: This is executed once on every dirent. We should consider optimizing
112 * it.
113 */
114static int ocfs2_skip_dir_trailer(struct inode *dir,
115 struct ocfs2_dir_entry *de,
116 unsigned long offset,
117 unsigned long blklen)
118{
119 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
120
121 if (!ocfs2_dir_has_trailer(dir))
122 return 0;
123
124 if (offset != toff)
125 return 0;
126
127 return 1;
128}
129
130static void ocfs2_init_dir_trailer(struct inode *inode,
131 struct buffer_head *bh)
132{
133 struct ocfs2_dir_block_trailer *trailer;
134
135 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
136 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
137 trailer->db_compat_rec_len =
138 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
139 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
140 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
141}
142
143/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700144 * bh passed here can be an inode block or a dir data block, depending
145 * on the inode inline data flag.
146 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700147static int ocfs2_check_dir_entry(struct inode * dir,
148 struct ocfs2_dir_entry * de,
149 struct buffer_head * bh,
150 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700151{
152 const char *error_msg = NULL;
153 const int rlen = le16_to_cpu(de->rec_len);
154
155 if (rlen < OCFS2_DIR_REC_LEN(1))
156 error_msg = "rec_len is smaller than minimal";
157 else if (rlen % 4 != 0)
158 error_msg = "rec_len % 4 != 0";
159 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
160 error_msg = "rec_len is too small for name_len";
161 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
162 error_msg = "directory entry across blocks";
163
164 if (error_msg != NULL)
165 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
166 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
167 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
168 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
169 de->name_len);
170 return error_msg == NULL ? 1 : 0;
171}
172
173static inline int ocfs2_match(int len,
174 const char * const name,
175 struct ocfs2_dir_entry *de)
176{
177 if (len != de->name_len)
178 return 0;
179 if (!de->inode)
180 return 0;
181 return !memcmp(name, de->name, len);
182}
183
184/*
185 * Returns 0 if not found, -1 on failure, and 1 on success
186 */
187static int inline ocfs2_search_dirblock(struct buffer_head *bh,
188 struct inode *dir,
189 const char *name, int namelen,
190 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700191 char *first_de,
192 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700193 struct ocfs2_dir_entry **res_dir)
194{
195 struct ocfs2_dir_entry *de;
196 char *dlimit, *de_buf;
197 int de_len;
198 int ret = 0;
199
200 mlog_entry_void();
201
Mark Fasheh23193e52007-09-12 13:01:18 -0700202 de_buf = first_de;
203 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700204
205 while (de_buf < dlimit) {
206 /* this code is executed quadratically often */
207 /* do minimal checking `by hand' */
208
209 de = (struct ocfs2_dir_entry *) de_buf;
210
211 if (de_buf + namelen <= dlimit &&
212 ocfs2_match(namelen, name, de)) {
213 /* found a match - just to be sure, do a full check */
214 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
215 ret = -1;
216 goto bail;
217 }
218 *res_dir = de;
219 ret = 1;
220 goto bail;
221 }
222
223 /* prevent looping on a bad block */
224 de_len = le16_to_cpu(de->rec_len);
225 if (de_len <= 0) {
226 ret = -1;
227 goto bail;
228 }
229
230 de_buf += de_len;
231 offset += de_len;
232 }
233
234bail:
235 mlog_exit(ret);
236 return ret;
237}
238
Mark Fasheh23193e52007-09-12 13:01:18 -0700239static struct buffer_head *ocfs2_find_entry_id(const char *name,
240 int namelen,
241 struct inode *dir,
242 struct ocfs2_dir_entry **res_dir)
243{
244 int ret, found;
245 struct buffer_head *di_bh = NULL;
246 struct ocfs2_dinode *di;
247 struct ocfs2_inline_data *data;
248
Joel Beckerb657c952008-11-13 14:49:11 -0800249 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700250 if (ret) {
251 mlog_errno(ret);
252 goto out;
253 }
254
255 di = (struct ocfs2_dinode *)di_bh->b_data;
256 data = &di->id2.i_data;
257
258 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
259 data->id_data, i_size_read(dir), res_dir);
260 if (found == 1)
261 return di_bh;
262
263 brelse(di_bh);
264out:
265 return NULL;
266}
267
Joel Beckera22305c2008-11-13 14:49:17 -0800268static int ocfs2_validate_dir_block(struct super_block *sb,
269 struct buffer_head *bh)
270{
271 /*
272 * Nothing yet. We don't validate dirents here, that's handled
273 * in-place when the code walks them.
274 */
Joel Becker970e4932008-11-13 14:49:19 -0800275 mlog(0, "Validating dirblock %llu\n",
276 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800277
278 return 0;
279}
280
281/*
282 * This function forces all errors to -EIO for consistency with its
283 * predecessor, ocfs2_bread(). We haven't audited what returning the
284 * real error codes would do to callers. We log the real codes with
285 * mlog_errno() before we squash them.
286 */
287static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
288 struct buffer_head **bh, int flags)
289{
290 int rc = 0;
291 struct buffer_head *tmp = *bh;
Mark Fasheh87d35a72008-12-10 17:36:25 -0800292 struct ocfs2_dir_block_trailer *trailer;
Joel Beckera22305c2008-11-13 14:49:17 -0800293
Joel Becker511308d2008-11-13 14:49:21 -0800294 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
295 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800296 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800297 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800298 goto out;
299 }
300
301 /*
302 * We check the trailer here rather than in
303 * ocfs2_validate_dir_block() because that function doesn't have
304 * the inode to test.
305 */
306 if (!(flags & OCFS2_BH_READAHEAD) &&
307 ocfs2_dir_has_trailer(inode)) {
308 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
309 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
310 rc = -EINVAL;
311 ocfs2_error(inode->i_sb,
312 "Invalid dirblock #%llu: "
313 "signature = %.*s\n",
314 (unsigned long long)tmp->b_blocknr, 7,
315 trailer->db_signature);
316 goto out;
317 }
318 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
319 rc = -EINVAL;
320 ocfs2_error(inode->i_sb,
321 "Directory block #%llu has an invalid "
322 "db_blkno of %llu",
323 (unsigned long long)tmp->b_blocknr,
324 (unsigned long long)le64_to_cpu(trailer->db_blkno));
325 goto out;
326 }
327 if (le64_to_cpu(trailer->db_parent_dinode) !=
328 OCFS2_I(inode)->ip_blkno) {
329 rc = -EINVAL;
330 ocfs2_error(inode->i_sb,
331 "Directory block #%llu on dinode "
332 "#%llu has an invalid parent_dinode "
333 "of %llu",
334 (unsigned long long)tmp->b_blocknr,
335 (unsigned long long)OCFS2_I(inode)->ip_blkno,
336 (unsigned long long)le64_to_cpu(trailer->db_blkno));
337 goto out;
338 }
339 }
Joel Beckera22305c2008-11-13 14:49:17 -0800340
Joel Becker511308d2008-11-13 14:49:21 -0800341 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800342 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800343 *bh = tmp;
344
Mark Fasheh87d35a72008-12-10 17:36:25 -0800345out:
Joel Beckera22305c2008-11-13 14:49:17 -0800346 return rc ? -EIO : 0;
347}
348
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200349static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
350 struct inode *dir,
351 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700352{
353 struct super_block *sb;
354 struct buffer_head *bh_use[NAMEI_RA_SIZE];
355 struct buffer_head *bh, *ret = NULL;
356 unsigned long start, block, b;
357 int ra_max = 0; /* Number of bh's in the readahead
358 buffer, bh_use[] */
359 int ra_ptr = 0; /* Current index into readahead
360 buffer */
361 int num = 0;
362 int nblocks, i, err;
363
364 mlog_entry_void();
365
Mark Fasheh316f4b92007-09-07 18:21:26 -0700366 sb = dir->i_sb;
367
368 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
369 start = OCFS2_I(dir)->ip_dir_start_lookup;
370 if (start >= nblocks)
371 start = 0;
372 block = start;
373
374restart:
375 do {
376 /*
377 * We deal with the read-ahead logic here.
378 */
379 if (ra_ptr >= ra_max) {
380 /* Refill the readahead buffer */
381 ra_ptr = 0;
382 b = block;
383 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
384 /*
385 * Terminate if we reach the end of the
386 * directory and must wrap, or if our
387 * search has finished at this block.
388 */
389 if (b >= nblocks || (num && block == start)) {
390 bh_use[ra_max] = NULL;
391 break;
392 }
393 num++;
394
Joel Beckera22305c2008-11-13 14:49:17 -0800395 bh = NULL;
396 err = ocfs2_read_dir_block(dir, b++, &bh,
397 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700398 bh_use[ra_max] = bh;
399 }
400 }
401 if ((bh = bh_use[ra_ptr++]) == NULL)
402 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800403 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700404 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800405 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700406 ocfs2_error(dir->i_sb, "reading directory %llu, "
407 "offset %lu\n",
408 (unsigned long long)OCFS2_I(dir)->ip_blkno,
409 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700410 goto next;
411 }
412 i = ocfs2_search_dirblock(bh, dir, name, namelen,
413 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700414 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700415 res_dir);
416 if (i == 1) {
417 OCFS2_I(dir)->ip_dir_start_lookup = block;
418 ret = bh;
419 goto cleanup_and_exit;
420 } else {
421 brelse(bh);
422 if (i < 0)
423 goto cleanup_and_exit;
424 }
425 next:
426 if (++block >= nblocks)
427 block = 0;
428 } while (block != start);
429
430 /*
431 * If the directory has grown while we were searching, then
432 * search the last part of the directory before giving up.
433 */
434 block = nblocks;
435 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
436 if (block < nblocks) {
437 start = 0;
438 goto restart;
439 }
440
441cleanup_and_exit:
442 /* Clean up the read-ahead blocks */
443 for (; ra_ptr < ra_max; ra_ptr++)
444 brelse(bh_use[ra_ptr]);
445
446 mlog_exit_ptr(ret);
447 return ret;
448}
449
Mark Fasheh23193e52007-09-12 13:01:18 -0700450/*
451 * Try to find an entry of the provided name within 'dir'.
452 *
453 * If nothing was found, NULL is returned. Otherwise, a buffer_head
454 * and pointer to the dir entry are passed back.
455 *
456 * Caller can NOT assume anything about the contents of the
457 * buffer_head - it is passed back only so that it can be passed into
458 * any one of the manipulation functions (add entry, delete entry,
459 * etc). As an example, bh in the extent directory case is a data
460 * block, in the inline-data case it actually points to an inode.
461 */
462struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
463 struct inode *dir,
464 struct ocfs2_dir_entry **res_dir)
465{
466 *res_dir = NULL;
467
468 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
469 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
470
471 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
472}
473
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700474/*
475 * Update inode number and type of a previously found directory entry.
476 */
Mark Fasheh38760e22007-09-11 17:21:56 -0700477int ocfs2_update_entry(struct inode *dir, handle_t *handle,
478 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
479 struct inode *new_entry_inode)
480{
481 int ret;
Joel Becker13723d02008-10-17 19:25:01 -0700482 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh38760e22007-09-11 17:21:56 -0700483
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700484 /*
485 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -0700486 * based directories, so no need to split this up. The only
487 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700488 */
489
Joel Becker13723d02008-10-17 19:25:01 -0700490 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
491 access = ocfs2_journal_access_di;
492
493 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -0700494 if (ret) {
495 mlog_errno(ret);
496 goto out;
497 }
498
499 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
500 ocfs2_set_de_type(de, new_entry_inode->i_mode);
501
502 ocfs2_journal_dirty(handle, de_bh);
503
504out:
505 return ret;
506}
507
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700508static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
509 struct ocfs2_dir_entry *de_del,
510 struct buffer_head *bh, char *first_de,
511 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700512{
513 struct ocfs2_dir_entry *de, *pde;
514 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -0700515 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700516
517 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
518
Joel Becker13723d02008-10-17 19:25:01 -0700519 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
520 access = ocfs2_journal_access_di;
521
Mark Fasheh316f4b92007-09-07 18:21:26 -0700522 i = 0;
523 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700524 de = (struct ocfs2_dir_entry *) first_de;
525 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700526 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
527 status = -EIO;
528 mlog_errno(status);
529 goto bail;
530 }
531 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -0700532 status = access(handle, dir, bh,
533 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700534 if (status < 0) {
535 status = -EIO;
536 mlog_errno(status);
537 goto bail;
538 }
539 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100540 le16_add_cpu(&pde->rec_len,
541 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -0700542 else
543 de->inode = 0;
544 dir->i_version++;
545 status = ocfs2_journal_dirty(handle, bh);
546 goto bail;
547 }
548 i += le16_to_cpu(de->rec_len);
549 pde = de;
550 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
551 }
552bail:
553 mlog_exit(status);
554 return status;
555}
556
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700557static inline int ocfs2_delete_entry_id(handle_t *handle,
558 struct inode *dir,
559 struct ocfs2_dir_entry *de_del,
560 struct buffer_head *bh)
561{
562 int ret;
563 struct buffer_head *di_bh = NULL;
564 struct ocfs2_dinode *di;
565 struct ocfs2_inline_data *data;
566
Joel Beckerb657c952008-11-13 14:49:11 -0800567 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700568 if (ret) {
569 mlog_errno(ret);
570 goto out;
571 }
572
573 di = (struct ocfs2_dinode *)di_bh->b_data;
574 data = &di->id2.i_data;
575
576 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
577 i_size_read(dir));
578
579 brelse(di_bh);
580out:
581 return ret;
582}
583
584static inline int ocfs2_delete_entry_el(handle_t *handle,
585 struct inode *dir,
586 struct ocfs2_dir_entry *de_del,
587 struct buffer_head *bh)
588{
589 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
590 bh->b_size);
591}
592
593/*
594 * ocfs2_delete_entry deletes a directory entry by merging it with the
595 * previous entry
596 */
597int ocfs2_delete_entry(handle_t *handle,
598 struct inode *dir,
599 struct ocfs2_dir_entry *de_del,
600 struct buffer_head *bh)
601{
602 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
603 return ocfs2_delete_entry_id(handle, dir, de_del, bh);
604
605 return ocfs2_delete_entry_el(handle, dir, de_del, bh);
606}
607
Mark Fasheh8553cf42007-09-13 16:29:01 -0700608/*
609 * Check whether 'de' has enough room to hold an entry of
610 * 'new_rec_len' bytes.
611 */
612static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
613 unsigned int new_rec_len)
614{
615 unsigned int de_really_used;
616
617 /* Check whether this is an empty record with enough space */
618 if (le64_to_cpu(de->inode) == 0 &&
619 le16_to_cpu(de->rec_len) >= new_rec_len)
620 return 1;
621
622 /*
623 * Record might have free space at the end which we can
624 * use.
625 */
626 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
627 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
628 return 1;
629
630 return 0;
631}
632
Mark Fasheh316f4b92007-09-07 18:21:26 -0700633/* we don't always have a dentry for what we want to add, so people
634 * like orphan dir can call this instead.
635 *
636 * If you pass me insert_bh, I'll skip the search of the other dir
637 * blocks and put the record in there.
638 */
639int __ocfs2_add_entry(handle_t *handle,
640 struct inode *dir,
641 const char *name, int namelen,
642 struct inode *inode, u64 blkno,
643 struct buffer_head *parent_fe_bh,
644 struct buffer_head *insert_bh)
645{
646 unsigned long offset;
647 unsigned short rec_len;
648 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700649 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
650 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700651 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700652 unsigned int size = sb->s_blocksize;
653 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700654
655 mlog_entry_void();
656
Mark Fasheh316f4b92007-09-07 18:21:26 -0700657 if (!namelen)
658 return -EINVAL;
659
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700660 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
661 data_start = di->id2.i_data.id_data;
662 size = i_size_read(dir);
663
664 BUG_ON(insert_bh != parent_fe_bh);
665 }
666
Mark Fasheh316f4b92007-09-07 18:21:26 -0700667 rec_len = OCFS2_DIR_REC_LEN(namelen);
668 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700669 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700670 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700671 BUG_ON((char *)de >= (size + data_start));
672
Mark Fasheh316f4b92007-09-07 18:21:26 -0700673 /* These checks should've already been passed by the
674 * prepare function, but I guess we can leave them
675 * here anyway. */
676 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
677 retval = -ENOENT;
678 goto bail;
679 }
680 if (ocfs2_match(namelen, name, de)) {
681 retval = -EEXIST;
682 goto bail;
683 }
Mark Fasheh8553cf42007-09-13 16:29:01 -0700684
Mark Fasheh87d35a72008-12-10 17:36:25 -0800685 /* We're guaranteed that we should have space, so we
686 * can't possibly have hit the trailer...right? */
687 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
688 "Hit dir trailer trying to insert %.*s "
689 "(namelen %d) into directory %llu. "
690 "offset is %lu, trailer offset is %d\n",
691 namelen, name, namelen,
692 (unsigned long long)parent_fe_bh->b_blocknr,
693 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
694
Mark Fasheh8553cf42007-09-13 16:29:01 -0700695 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -0700696 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
697 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
698 if (retval < 0) {
699 mlog_errno(retval);
700 goto bail;
701 }
702
Joel Becker13723d02008-10-17 19:25:01 -0700703 if (insert_bh == parent_fe_bh)
704 status = ocfs2_journal_access_di(handle, dir,
705 insert_bh,
706 OCFS2_JOURNAL_ACCESS_WRITE);
707 else
708 status = ocfs2_journal_access_db(handle, dir,
709 insert_bh,
710 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700711 /* By now the buffer is marked for journaling */
712 offset += le16_to_cpu(de->rec_len);
713 if (le64_to_cpu(de->inode)) {
714 de1 = (struct ocfs2_dir_entry *)((char *) de +
715 OCFS2_DIR_REC_LEN(de->name_len));
716 de1->rec_len =
717 cpu_to_le16(le16_to_cpu(de->rec_len) -
718 OCFS2_DIR_REC_LEN(de->name_len));
719 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
720 de = de1;
721 }
722 de->file_type = OCFS2_FT_UNKNOWN;
723 if (blkno) {
724 de->inode = cpu_to_le64(blkno);
725 ocfs2_set_de_type(de, inode->i_mode);
726 } else
727 de->inode = 0;
728 de->name_len = namelen;
729 memcpy(de->name, name, namelen);
730
731 dir->i_version++;
732 status = ocfs2_journal_dirty(handle, insert_bh);
733 retval = 0;
734 goto bail;
735 }
Mark Fasheh87d35a72008-12-10 17:36:25 -0800736
Mark Fasheh316f4b92007-09-07 18:21:26 -0700737 offset += le16_to_cpu(de->rec_len);
738 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
739 }
740
741 /* when you think about it, the assert above should prevent us
742 * from ever getting here. */
743 retval = -ENOSPC;
744bail:
745
746 mlog_exit(retval);
747 return retval;
748}
749
Mark Fasheh23193e52007-09-12 13:01:18 -0700750static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700751 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700752 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700753 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700754{
755 int ret, i, filldir_ret;
756 unsigned long offset = *f_pos;
757 struct buffer_head *di_bh = NULL;
758 struct ocfs2_dinode *di;
759 struct ocfs2_inline_data *data;
760 struct ocfs2_dir_entry *de;
761
Joel Beckerb657c952008-11-13 14:49:11 -0800762 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700763 if (ret) {
764 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
765 (unsigned long long)OCFS2_I(inode)->ip_blkno);
766 goto out;
767 }
768
769 di = (struct ocfs2_dinode *)di_bh->b_data;
770 data = &di->id2.i_data;
771
772 while (*f_pos < i_size_read(inode)) {
773revalidate:
774 /* If the dir block has changed since the last call to
775 * readdir(2), then we might be pointing to an invalid
776 * dirent right now. Scan from the start of the block
777 * to make sure. */
778 if (*f_version != inode->i_version) {
779 for (i = 0; i < i_size_read(inode) && i < offset; ) {
780 de = (struct ocfs2_dir_entry *)
781 (data->id_data + i);
782 /* It's too expensive to do a full
783 * dirent test each time round this
784 * loop, but we do have to test at
785 * least that it is non-zero. A
786 * failure will be detected in the
787 * dirent test below. */
788 if (le16_to_cpu(de->rec_len) <
789 OCFS2_DIR_REC_LEN(1))
790 break;
791 i += le16_to_cpu(de->rec_len);
792 }
793 *f_pos = offset = i;
794 *f_version = inode->i_version;
795 }
796
797 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
798 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
799 /* On error, skip the f_pos to the end. */
800 *f_pos = i_size_read(inode);
801 goto out;
802 }
803 offset += le16_to_cpu(de->rec_len);
804 if (le64_to_cpu(de->inode)) {
805 /* We might block in the next section
806 * if the data destination is
807 * currently swapped out. So, use a
808 * version stamp to detect whether or
809 * not the directory has been modified
810 * during the copy operation.
811 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700812 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -0700813 unsigned char d_type = DT_UNKNOWN;
814
815 if (de->file_type < OCFS2_FT_MAX)
816 d_type = ocfs2_filetype_table[de->file_type];
817
818 filldir_ret = filldir(priv, de->name,
819 de->name_len,
820 *f_pos,
821 le64_to_cpu(de->inode),
822 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700823 if (filldir_ret) {
824 if (filldir_err)
825 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -0700826 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700827 }
Mark Fasheh23193e52007-09-12 13:01:18 -0700828 if (version != *f_version)
829 goto revalidate;
830 }
831 *f_pos += le16_to_cpu(de->rec_len);
832 }
833
834out:
835 brelse(di_bh);
836
837 return 0;
838}
839
840static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700841 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -0700842 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700843 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -0800844{
845 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -0700846 unsigned long offset, blk, last_ra_blk = 0;
847 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -0800848 struct buffer_head * bh, * tmp;
849 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -0800850 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -0700851 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -0800852
853 stored = 0;
854 bh = NULL;
855
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700856 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800857
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700858 while (!error && !stored && *f_pos < i_size_read(inode)) {
859 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -0800860 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
861 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700862 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800863 continue;
864 }
865
Mark Fashehaa958872006-04-21 13:49:02 -0700866 /* The idea here is to begin with 8k read-ahead and to stay
867 * 4k ahead of our current position.
868 *
869 * TODO: Use the pagecache for this. We just need to
870 * make sure it's cluster-safe... */
871 if (!last_ra_blk
872 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
873 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -0800874 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -0800875 tmp = NULL;
876 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
877 OCFS2_BH_READAHEAD))
878 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -0800879 }
Mark Fashehaa958872006-04-21 13:49:02 -0700880 last_ra_blk = blk;
881 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 }
883
884revalidate:
885 /* If the dir block has changed since the last call to
886 * readdir(2), then we might be pointing to an invalid
887 * dirent right now. Scan from the start of the block
888 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700889 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800890 for (i = 0; i < sb->s_blocksize && i < offset; ) {
891 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
892 /* It's too expensive to do a full
893 * dirent test each time round this
894 * loop, but we do have to test at
895 * least that it is non-zero. A
896 * failure will be detected in the
897 * dirent test below. */
898 if (le16_to_cpu(de->rec_len) <
899 OCFS2_DIR_REC_LEN(1))
900 break;
901 i += le16_to_cpu(de->rec_len);
902 }
903 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700904 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -0800905 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700906 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 }
908
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700909 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 && offset < sb->s_blocksize) {
911 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
912 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
913 /* On error, skip the f_pos to the
914 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700915 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800916 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700917 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 }
919 offset += le16_to_cpu(de->rec_len);
920 if (le64_to_cpu(de->inode)) {
921 /* We might block in the next section
922 * if the data destination is
923 * currently swapped out. So, use a
924 * version stamp to detect whether or
925 * not the directory has been modified
926 * during the copy operation.
927 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700928 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 unsigned char d_type = DT_UNKNOWN;
930
931 if (de->file_type < OCFS2_FT_MAX)
932 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700933 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -0800934 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700935 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -0700936 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -0800937 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -0700938 if (error) {
939 if (filldir_err)
940 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -0800941 break;
Mark Fashehe7b34012007-09-24 14:25:27 -0700942 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700943 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -0800944 goto revalidate;
945 stored ++;
946 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700947 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -0800948 }
949 offset = 0;
950 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -0800951 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800952 }
953
954 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700955out:
956 return stored;
957}
958
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700959static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -0700960 loff_t *f_pos, void *priv, filldir_t filldir,
961 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -0700962{
963 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
964 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700965 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700966
Mark Fashehe7b34012007-09-24 14:25:27 -0700967 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
968 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -0700969}
970
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700971/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700972 * This is intended to be called from inside other kernel functions,
973 * so we fake some arguments.
974 */
975int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
976 filldir_t filldir)
977{
Mark Fashehe7b34012007-09-24 14:25:27 -0700978 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -0700979 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700980
981 while (*f_pos < i_size_read(inode)) {
982 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -0700983 filldir, &filldir_err);
984 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700985 break;
986 }
987
Mark Fashehe7b34012007-09-24 14:25:27 -0700988 if (ret > 0)
989 ret = -EIO;
990
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700991 return 0;
992}
993
994/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -0700995 * ocfs2_readdir()
996 *
997 */
998int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
999{
1000 int error = 0;
1001 struct inode *inode = filp->f_path.dentry->d_inode;
1002 int lock_level = 0;
1003
1004 mlog_entry("dirino=%llu\n",
1005 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1006
Mark Fashehe63aecb62007-10-18 15:30:42 -07001007 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001008 if (lock_level && error >= 0) {
1009 /* We release EX lock which used to update atime
1010 * and get PR lock again to reduce contention
1011 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001012 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001013 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001014 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001015 }
1016 if (error < 0) {
1017 if (error != -ENOENT)
1018 mlog_errno(error);
1019 /* we haven't got any yet, so propagate the error. */
1020 goto bail_nolock;
1021 }
1022
1023 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -07001024 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001025
Mark Fashehe63aecb62007-10-18 15:30:42 -07001026 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001027
Mark Fashehaa958872006-04-21 13:49:02 -07001028bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001029 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08001030
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001031 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001032}
1033
1034/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001035 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 */
1037int ocfs2_find_files_on_disk(const char *name,
1038 int namelen,
1039 u64 *blkno,
1040 struct inode *inode,
1041 struct buffer_head **dirent_bh,
1042 struct ocfs2_dir_entry **dirent)
1043{
1044 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001045
Joel Becker2b388c62006-05-10 18:28:59 -07001046 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
1047 namelen, name, blkno, inode, dirent_bh, dirent);
Mark Fashehccd979b2005-12-15 14:31:24 -08001048
1049 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
1050 if (!*dirent_bh || !*dirent) {
1051 status = -ENOENT;
1052 goto leave;
1053 }
1054
1055 *blkno = le64_to_cpu((*dirent)->inode);
1056
1057 status = 0;
1058leave:
1059 if (status < 0) {
1060 *dirent = NULL;
Mark Fasheha81cb882008-10-07 14:25:16 -07001061 brelse(*dirent_bh);
1062 *dirent_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 }
1064
1065 mlog_exit(status);
1066 return status;
1067}
1068
Mark Fashehbe94d112007-09-11 15:22:06 -07001069/*
1070 * Convenience function for callers which just want the block number
1071 * mapped to a name and don't require the full dirent info, etc.
1072 */
1073int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1074 int namelen, u64 *blkno)
1075{
1076 int ret;
1077 struct buffer_head *bh = NULL;
1078 struct ocfs2_dir_entry *dirent = NULL;
1079
1080 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
1081 brelse(bh);
1082
1083 return ret;
1084}
1085
Mark Fashehccd979b2005-12-15 14:31:24 -08001086/* Check for a name within a directory.
1087 *
1088 * Return 0 if the name does not exist
1089 * Return -EEXIST if the directory contains the name
1090 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001091 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 */
1093int ocfs2_check_dir_for_entry(struct inode *dir,
1094 const char *name,
1095 int namelen)
1096{
1097 int ret;
1098 struct buffer_head *dirent_bh = NULL;
1099 struct ocfs2_dir_entry *dirent = NULL;
1100
Mark Fashehb0697052006-03-03 10:24:33 -08001101 mlog_entry("dir %llu, name '%.*s'\n",
1102 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103
1104 ret = -EEXIST;
1105 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
1106 if (dirent_bh)
1107 goto bail;
1108
1109 ret = 0;
1110bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001111 brelse(dirent_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001112
1113 mlog_exit(ret);
1114 return ret;
1115}
1116
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001117struct ocfs2_empty_dir_priv {
1118 unsigned seen_dot;
1119 unsigned seen_dot_dot;
1120 unsigned seen_other;
1121};
1122static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1123 loff_t pos, u64 ino, unsigned type)
1124{
1125 struct ocfs2_empty_dir_priv *p = priv;
1126
1127 /*
1128 * Check the positions of "." and ".." records to be sure
1129 * they're in the correct place.
1130 */
1131 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1132 p->seen_dot = 1;
1133 return 0;
1134 }
1135
1136 if (name_len == 2 && !strncmp("..", name, 2) &&
1137 pos == OCFS2_DIR_REC_LEN(1)) {
1138 p->seen_dot_dot = 1;
1139 return 0;
1140 }
1141
1142 p->seen_other = 1;
1143 return 1;
1144}
Mark Fashehccd979b2005-12-15 14:31:24 -08001145/*
1146 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001147 *
1148 * Returns 1 if dir is empty, zero otherwise.
Mark Fashehccd979b2005-12-15 14:31:24 -08001149 */
1150int ocfs2_empty_dir(struct inode *inode)
1151{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001152 int ret;
1153 loff_t start = 0;
1154 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001155
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001156 memset(&priv, 0, sizeof(priv));
1157
1158 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1159 if (ret)
1160 mlog_errno(ret);
1161
1162 if (!priv.seen_dot || !priv.seen_dot_dot) {
1163 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001164 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001165 /*
1166 * XXX: Is it really safe to allow an unlink to continue?
1167 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 return 1;
1169 }
1170
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001171 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001172}
1173
Mark Fasheh87d35a72008-12-10 17:36:25 -08001174/*
1175 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1176 * "..", which might be used during creation of a directory with a trailing
1177 * header. It is otherwise safe to ignore the return code.
1178 */
1179static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1180 struct inode *parent,
1181 char *start,
1182 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001183{
1184 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1185
1186 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1187 de->name_len = 1;
1188 de->rec_len =
1189 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1190 strcpy(de->name, ".");
1191 ocfs2_set_de_type(de, S_IFDIR);
1192
1193 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1194 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1195 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1196 de->name_len = 2;
1197 strcpy(de->name, "..");
1198 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001199
1200 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001201}
1202
1203/*
1204 * This works together with code in ocfs2_mknod_locked() which sets
1205 * the inline-data flag and initializes the inline-data section.
1206 */
1207static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1208 handle_t *handle,
1209 struct inode *parent,
1210 struct inode *inode,
1211 struct buffer_head *di_bh)
1212{
1213 int ret;
1214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1215 struct ocfs2_inline_data *data = &di->id2.i_data;
1216 unsigned int size = le16_to_cpu(data->id_count);
1217
Joel Becker13723d02008-10-17 19:25:01 -07001218 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1219 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001220 if (ret) {
1221 mlog_errno(ret);
1222 goto out;
1223 }
1224
1225 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1226
1227 ocfs2_journal_dirty(handle, di_bh);
1228 if (ret) {
1229 mlog_errno(ret);
1230 goto out;
1231 }
1232
1233 i_size_write(inode, size);
1234 inode->i_nlink = 2;
1235 inode->i_blocks = ocfs2_inode_sector_count(inode);
1236
1237 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1238 if (ret < 0)
1239 mlog_errno(ret);
1240
1241out:
1242 return ret;
1243}
1244
1245static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1246 handle_t *handle,
1247 struct inode *parent,
1248 struct inode *inode,
1249 struct buffer_head *fe_bh,
1250 struct ocfs2_alloc_context *data_ac)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001251{
1252 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001253 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001254 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001255 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001256
1257 mlog_entry_void();
1258
Mark Fasheh87d35a72008-12-10 17:36:25 -08001259 if (ocfs2_supports_dir_trailer(osb))
1260 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1261
Mark Fasheh316f4b92007-09-07 18:21:26 -07001262 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1263 data_ac, NULL, &new_bh);
1264 if (status < 0) {
1265 mlog_errno(status);
1266 goto bail;
1267 }
1268
1269 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1270
Joel Becker13723d02008-10-17 19:25:01 -07001271 status = ocfs2_journal_access_db(handle, inode, new_bh,
1272 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001273 if (status < 0) {
1274 mlog_errno(status);
1275 goto bail;
1276 }
1277 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1278
Mark Fasheh87d35a72008-12-10 17:36:25 -08001279 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1280 if (ocfs2_supports_dir_trailer(osb))
1281 ocfs2_init_dir_trailer(inode, new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001282
1283 status = ocfs2_journal_dirty(handle, new_bh);
1284 if (status < 0) {
1285 mlog_errno(status);
1286 goto bail;
1287 }
1288
1289 i_size_write(inode, inode->i_sb->s_blocksize);
1290 inode->i_nlink = 2;
1291 inode->i_blocks = ocfs2_inode_sector_count(inode);
1292 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1293 if (status < 0) {
1294 mlog_errno(status);
1295 goto bail;
1296 }
1297
1298 status = 0;
1299bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001300 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001301
1302 mlog_exit(status);
1303 return status;
1304}
1305
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001306int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1307 handle_t *handle,
1308 struct inode *parent,
1309 struct inode *inode,
1310 struct buffer_head *fe_bh,
1311 struct ocfs2_alloc_context *data_ac)
1312{
1313 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1314
1315 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1316 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1317
1318 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1319 data_ac);
1320}
1321
Mark Fasheh87d35a72008-12-10 17:36:25 -08001322/*
1323 * Expand rec_len of the rightmost dirent in a directory block so that it
1324 * contains the end of our valid space for dirents. We do this during
1325 * expansion from an inline directory to one with extents. The first dir block
1326 * in that case is taken from the inline data portion of the inode block.
1327 *
1328 * We add the dir trailer if this filesystem wants it.
1329 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001330static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
Mark Fasheh87d35a72008-12-10 17:36:25 -08001331 struct super_block *sb)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001332{
1333 struct ocfs2_dir_entry *de;
1334 struct ocfs2_dir_entry *prev_de;
1335 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001336 unsigned int new_size = sb->s_blocksize;
1337 unsigned int bytes;
1338
1339 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1340 new_size = ocfs2_dir_trailer_blk_off(sb);
1341
1342 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001343
1344 limit = start + old_size;
1345 de_buf = start;
1346 de = (struct ocfs2_dir_entry *)de_buf;
1347 do {
1348 prev_de = de;
1349 de_buf += le16_to_cpu(de->rec_len);
1350 de = (struct ocfs2_dir_entry *)de_buf;
1351 } while (de_buf < limit);
1352
1353 le16_add_cpu(&prev_de->rec_len, bytes);
1354}
1355
1356/*
1357 * We allocate enough clusters to fulfill "blocks_wanted", but set
1358 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1359 * rest automatically for us.
1360 *
1361 * *first_block_bh is a pointer to the 1st data block allocated to the
1362 * directory.
1363 */
1364static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1365 unsigned int blocks_wanted,
1366 struct buffer_head **first_block_bh)
1367{
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001368 u32 alloc, bit_off, len;
1369 struct super_block *sb = dir->i_sb;
Jan Karaa90714c2008-10-09 19:38:40 +02001370 int ret, credits = ocfs2_inline_to_extents_credits(sb);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001371 u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1372 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1373 struct ocfs2_inode_info *oi = OCFS2_I(dir);
1374 struct ocfs2_alloc_context *data_ac;
1375 struct buffer_head *dirdata_bh = NULL;
1376 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1377 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001378 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02001379 int did_quota = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001380
Joel Becker8d6220d2008-08-22 12:46:09 -07001381 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001382
1383 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1384
1385 /*
1386 * We should never need more than 2 clusters for this -
1387 * maximum dirent size is far less than one block. In fact,
1388 * the only time we'd need more than one cluster is if
1389 * blocksize == clustersize and the dirent won't fit in the
1390 * extra space that the expansion to a single block gives. As
1391 * of today, that only happens on 4k/4k file systems.
1392 */
1393 BUG_ON(alloc > 2);
1394
1395 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1396 if (ret) {
1397 mlog_errno(ret);
1398 goto out;
1399 }
1400
1401 down_write(&oi->ip_alloc_sem);
1402
1403 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02001404 * Prepare for worst case allocation scenario of two separate
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001405 * extents.
1406 */
1407 if (alloc == 2)
1408 credits += OCFS2_SUBALLOC_ALLOC;
1409
1410 handle = ocfs2_start_trans(osb, credits);
1411 if (IS_ERR(handle)) {
1412 ret = PTR_ERR(handle);
1413 mlog_errno(ret);
1414 goto out_sem;
1415 }
1416
Jan Karaa90714c2008-10-09 19:38:40 +02001417 if (vfs_dq_alloc_space_nodirty(dir,
1418 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1419 ret = -EDQUOT;
1420 goto out_commit;
1421 }
1422 did_quota = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001423 /*
1424 * Try to claim as many clusters as the bitmap can give though
1425 * if we only get one now, that's enough to continue. The rest
1426 * will be claimed after the conversion to extents.
1427 */
1428 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out_commit;
1432 }
1433
1434 /*
1435 * Operations are carefully ordered so that we set up the new
1436 * data block first. The conversion from inline data to
1437 * extents follows.
1438 */
1439 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1440 dirdata_bh = sb_getblk(sb, blkno);
1441 if (!dirdata_bh) {
1442 ret = -EIO;
1443 mlog_errno(ret);
1444 goto out_commit;
1445 }
1446
1447 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1448
Joel Becker13723d02008-10-17 19:25:01 -07001449 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1450 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001451 if (ret) {
1452 mlog_errno(ret);
1453 goto out_commit;
1454 }
1455
1456 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1457 memset(dirdata_bh->b_data + i_size_read(dir), 0,
1458 sb->s_blocksize - i_size_read(dir));
Mark Fasheh87d35a72008-12-10 17:36:25 -08001459 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1460 if (ocfs2_supports_dir_trailer(osb))
1461 ocfs2_init_dir_trailer(dir, dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001462
1463 ret = ocfs2_journal_dirty(handle, dirdata_bh);
1464 if (ret) {
1465 mlog_errno(ret);
1466 goto out_commit;
1467 }
1468
1469 /*
1470 * Set extent, i_size, etc on the directory. After this, the
1471 * inode should contain the same exact dirents as before and
1472 * be fully accessible from system calls.
1473 *
1474 * We let the later dirent insert modify c/mtime - to the user
1475 * the data hasn't changed.
1476 */
Joel Becker13723d02008-10-17 19:25:01 -07001477 ret = ocfs2_journal_access_di(handle, dir, di_bh,
1478 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001479 if (ret) {
1480 mlog_errno(ret);
1481 goto out_commit;
1482 }
1483
1484 spin_lock(&oi->ip_lock);
1485 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1486 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1487 spin_unlock(&oi->ip_lock);
1488
1489 ocfs2_dinode_new_extent_list(dir, di);
1490
1491 i_size_write(dir, sb->s_blocksize);
1492 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1493
1494 di->i_size = cpu_to_le64(sb->s_blocksize);
1495 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1496 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001497
1498 /*
1499 * This should never fail as our extent list is empty and all
1500 * related blocks have been journaled already.
1501 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001502 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1503 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001504 if (ret) {
1505 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001506 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001507 }
1508
Mark Fasheh9780eb62008-08-05 11:32:46 -07001509 /*
1510 * Set i_blocks after the extent insert for the most up to
1511 * date ip_clusters value.
1512 */
1513 dir->i_blocks = ocfs2_inode_sector_count(dir);
1514
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001515 ret = ocfs2_journal_dirty(handle, di_bh);
1516 if (ret) {
1517 mlog_errno(ret);
1518 goto out_commit;
1519 }
1520
1521 /*
1522 * We asked for two clusters, but only got one in the 1st
1523 * pass. Claim the 2nd cluster as a separate extent.
1524 */
1525 if (alloc > len) {
1526 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1527 &len);
1528 if (ret) {
1529 mlog_errno(ret);
1530 goto out_commit;
1531 }
1532 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1533
Joel Beckerf99b9b72008-08-20 19:36:33 -07001534 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1535 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001536 if (ret) {
1537 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08001538 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001539 }
1540 }
1541
1542 *first_block_bh = dirdata_bh;
1543 dirdata_bh = NULL;
1544
1545out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02001546 if (ret < 0 && did_quota)
1547 vfs_dq_free_space_nodirty(dir,
1548 ocfs2_clusters_to_bytes(osb->sb, 2));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001549 ocfs2_commit_trans(osb, handle);
1550
1551out_sem:
1552 up_write(&oi->ip_alloc_sem);
1553
1554out:
1555 if (data_ac)
1556 ocfs2_free_alloc_context(data_ac);
1557
1558 brelse(dirdata_bh);
1559
1560 return ret;
1561}
1562
Mark Fashehccd979b2005-12-15 14:31:24 -08001563/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07001564static int ocfs2_do_extend_dir(struct super_block *sb,
1565 handle_t *handle,
1566 struct inode *dir,
1567 struct buffer_head *parent_fe_bh,
1568 struct ocfs2_alloc_context *data_ac,
1569 struct ocfs2_alloc_context *meta_ac,
1570 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001571{
1572 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02001573 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001574 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001575
1576 spin_lock(&OCFS2_I(dir)->ip_lock);
1577 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1578 spin_unlock(&OCFS2_I(dir)->ip_lock);
1579
1580 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001581 u32 offset = OCFS2_I(dir)->ip_clusters;
1582
Jan Karaa90714c2008-10-09 19:38:40 +02001583 if (vfs_dq_alloc_space_nodirty(dir,
1584 ocfs2_clusters_to_bytes(sb, 1))) {
1585 status = -EDQUOT;
1586 goto bail;
1587 }
1588 did_quota = 1;
1589
Tao Ma0eb8d472008-08-18 17:38:45 +08001590 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1591 1, 0, parent_fe_bh, handle,
1592 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001593 BUG_ON(status == -EAGAIN);
1594 if (status < 0) {
1595 mlog_errno(status);
1596 goto bail;
1597 }
1598 }
1599
Mark Fasheh8110b072007-03-22 16:53:23 -07001600 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1601 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001602 if (status < 0) {
1603 mlog_errno(status);
1604 goto bail;
1605 }
1606
1607 *new_bh = sb_getblk(sb, p_blkno);
1608 if (!*new_bh) {
1609 status = -EIO;
1610 mlog_errno(status);
1611 goto bail;
1612 }
1613 status = 0;
1614bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001615 if (did_quota && status < 0)
1616 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08001617 mlog_exit(status);
1618 return status;
1619}
1620
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001621/*
1622 * Assumes you already have a cluster lock on the directory.
1623 *
1624 * 'blocks_wanted' is only used if we have an inline directory which
1625 * is to be turned into an extent based one. The size of the dirent to
1626 * insert might be larger than the space gained by growing to just one
1627 * block, so we may have to grow the inode by two blocks in that case.
1628 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001629static int ocfs2_extend_dir(struct ocfs2_super *osb,
1630 struct inode *dir,
1631 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001632 unsigned int blocks_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -08001633 struct buffer_head **new_de_bh)
1634{
1635 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07001636 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001637 loff_t dir_i_size;
1638 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08001639 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08001640 struct ocfs2_alloc_context *data_ac = NULL;
1641 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001642 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001643 struct buffer_head *new_bh = NULL;
1644 struct ocfs2_dir_entry * de;
1645 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07001646 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08001647
1648 mlog_entry_void();
1649
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001650 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1651 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1652 blocks_wanted, &new_bh);
1653 if (status) {
1654 mlog_errno(status);
1655 goto bail;
1656 }
1657
1658 if (blocks_wanted == 1) {
1659 /*
1660 * If the new dirent will fit inside the space
1661 * created by pushing out to one block, then
1662 * we can complete the operation
1663 * here. Otherwise we have to expand i_size
1664 * and format the 2nd block below.
1665 */
1666 BUG_ON(new_bh == NULL);
1667 goto bail_bh;
1668 }
1669
1670 /*
1671 * Get rid of 'new_bh' - we want to format the 2nd
1672 * data block and return that instead.
1673 */
1674 brelse(new_bh);
1675 new_bh = NULL;
1676
1677 dir_i_size = i_size_read(dir);
1678 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1679 goto do_extend;
1680 }
1681
Mark Fashehccd979b2005-12-15 14:31:24 -08001682 dir_i_size = i_size_read(dir);
Mark Fashehb0697052006-03-03 10:24:33 -08001683 mlog(0, "extending dir %llu (i_size = %lld)\n",
1684 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001685
Mark Fashehccd979b2005-12-15 14:31:24 -08001686 /* dir->i_size is always block aligned. */
1687 spin_lock(&OCFS2_I(dir)->ip_lock);
1688 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1689 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07001690 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07001691 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001692 if (num_free_extents < 0) {
1693 status = num_free_extents;
1694 mlog_errno(status);
1695 goto bail;
1696 }
1697
1698 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08001699 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 if (status < 0) {
1701 if (status != -ENOSPC)
1702 mlog_errno(status);
1703 goto bail;
1704 }
1705 }
1706
Mark Fashehda5cbf22006-10-06 18:34:35 -07001707 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001708 if (status < 0) {
1709 if (status != -ENOSPC)
1710 mlog_errno(status);
1711 goto bail;
1712 }
1713
Tao Ma811f9332008-08-18 17:38:43 +08001714 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001715 } else {
1716 spin_unlock(&OCFS2_I(dir)->ip_lock);
1717 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1718 }
1719
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001720do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07001721 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1722 drop_alloc_sem = 1;
1723
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001724 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 if (IS_ERR(handle)) {
1726 status = PTR_ERR(handle);
1727 handle = NULL;
1728 mlog_errno(status);
1729 goto bail;
1730 }
1731
1732 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1733 data_ac, meta_ac, &new_bh);
1734 if (status < 0) {
1735 mlog_errno(status);
1736 goto bail;
1737 }
1738
1739 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1740
Joel Becker13723d02008-10-17 19:25:01 -07001741 status = ocfs2_journal_access_db(handle, dir, new_bh,
1742 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001743 if (status < 0) {
1744 mlog_errno(status);
1745 goto bail;
1746 }
1747 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001748
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1750 de->inode = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001751 if (ocfs2_dir_has_trailer(dir)) {
1752 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1753 ocfs2_init_dir_trailer(dir, new_bh);
1754 } else {
1755 de->rec_len = cpu_to_le16(sb->s_blocksize);
1756 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001757 status = ocfs2_journal_dirty(handle, new_bh);
1758 if (status < 0) {
1759 mlog_errno(status);
1760 goto bail;
1761 }
1762
1763 dir_i_size += dir->i_sb->s_blocksize;
1764 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07001765 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001766 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto bail;
1770 }
1771
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001772bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08001773 *new_de_bh = new_bh;
1774 get_bh(*new_de_bh);
1775bail:
Joel Beckeree19a772007-03-28 18:27:07 -07001776 if (drop_alloc_sem)
1777 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08001778 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001779 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001780
1781 if (data_ac)
1782 ocfs2_free_alloc_context(data_ac);
1783 if (meta_ac)
1784 ocfs2_free_alloc_context(meta_ac);
1785
Mark Fasheha81cb882008-10-07 14:25:16 -07001786 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001787
1788 mlog_exit(status);
1789 return status;
1790}
1791
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001792static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1793 const char *name, int namelen,
1794 struct buffer_head **ret_de_bh,
1795 unsigned int *blocks_wanted)
1796{
1797 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001798 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001799 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1800 struct ocfs2_dir_entry *de, *last_de = NULL;
1801 char *de_buf, *limit;
1802 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001803 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1804
1805 /*
1806 * This calculates how many free bytes we'd have in block zero, should
1807 * this function force expansion to an extent tree.
1808 */
1809 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1810 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1811 else
1812 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001813
1814 de_buf = di->id2.i_data.id_data;
1815 limit = de_buf + i_size_read(dir);
1816 rec_len = OCFS2_DIR_REC_LEN(namelen);
1817
1818 while (de_buf < limit) {
1819 de = (struct ocfs2_dir_entry *)de_buf;
1820
1821 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1822 ret = -ENOENT;
1823 goto out;
1824 }
1825 if (ocfs2_match(namelen, name, de)) {
1826 ret = -EEXIST;
1827 goto out;
1828 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001829 /*
1830 * No need to check for a trailing dirent record here as
1831 * they're not used for inline dirs.
1832 */
1833
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001834 if (ocfs2_dirent_would_fit(de, rec_len)) {
1835 /* Ok, we found a spot. Return this bh and let
1836 * the caller actually fill it in. */
1837 *ret_de_bh = di_bh;
1838 get_bh(*ret_de_bh);
1839 ret = 0;
1840 goto out;
1841 }
1842
1843 last_de = de;
1844 de_buf += le16_to_cpu(de->rec_len);
1845 offset += le16_to_cpu(de->rec_len);
1846 }
1847
1848 /*
1849 * We're going to require expansion of the directory - figure
1850 * out how many blocks we'll need so that a place for the
1851 * dirent can be found.
1852 */
1853 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001854 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001855 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1856 *blocks_wanted = 2;
1857
1858 ret = -ENOSPC;
1859out:
1860 return ret;
1861}
1862
1863static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1864 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001865{
1866 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001867 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001870 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001871 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08001872 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001873
Joel Beckera22305c2008-11-13 14:49:17 -08001874 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1875 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001876 mlog_errno(status);
1877 goto bail;
1878 }
1879
1880 rec_len = OCFS2_DIR_REC_LEN(namelen);
1881 offset = 0;
1882 de = (struct ocfs2_dir_entry *) bh->b_data;
1883 while (1) {
1884 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1885 brelse(bh);
1886 bh = NULL;
1887
1888 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001889 /*
1890 * Caller will have to expand this
1891 * directory.
1892 */
1893 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001894 goto bail;
1895 }
Joel Beckera22305c2008-11-13 14:49:17 -08001896 status = ocfs2_read_dir_block(dir,
1897 offset >> sb->s_blocksize_bits,
1898 &bh, 0);
1899 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 mlog_errno(status);
1901 goto bail;
1902 }
1903 /* move to next block */
1904 de = (struct ocfs2_dir_entry *) bh->b_data;
1905 }
1906 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1907 status = -ENOENT;
1908 goto bail;
1909 }
1910 if (ocfs2_match(namelen, name, de)) {
1911 status = -EEXIST;
1912 goto bail;
1913 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001914
1915 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1916 blocksize))
1917 goto next;
1918
Mark Fasheh8553cf42007-09-13 16:29:01 -07001919 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001920 /* Ok, we found a spot. Return this bh and let
1921 * the caller actually fill it in. */
1922 *ret_de_bh = bh;
1923 get_bh(*ret_de_bh);
1924 status = 0;
1925 goto bail;
1926 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001927next:
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 offset += le16_to_cpu(de->rec_len);
1929 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1930 }
1931
1932 status = 0;
1933bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001934 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001935
1936 mlog_exit(status);
1937 return status;
1938}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001939
1940int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1941 struct inode *dir,
1942 struct buffer_head *parent_fe_bh,
1943 const char *name,
1944 int namelen,
1945 struct buffer_head **ret_de_bh)
1946{
1947 int ret;
1948 unsigned int blocks_wanted = 1;
1949 struct buffer_head *bh = NULL;
1950
1951 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1952 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1953
1954 *ret_de_bh = NULL;
1955
1956 if (!namelen) {
1957 ret = -EINVAL;
1958 mlog_errno(ret);
1959 goto out;
1960 }
1961
1962 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1963 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
1964 namelen, &bh, &blocks_wanted);
1965 } else
1966 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
1967
1968 if (ret && ret != -ENOSPC) {
1969 mlog_errno(ret);
1970 goto out;
1971 }
1972
1973 if (ret == -ENOSPC) {
1974 /*
1975 * We have to expand the directory to add this name.
1976 */
1977 BUG_ON(bh);
1978
1979 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
1980 &bh);
1981 if (ret) {
1982 if (ret != -ENOSPC)
1983 mlog_errno(ret);
1984 goto out;
1985 }
1986
1987 BUG_ON(!bh);
1988 }
1989
1990 *ret_de_bh = bh;
1991 bh = NULL;
1992out:
Mark Fasheha81cb882008-10-07 14:25:16 -07001993 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001994 return ret;
1995}