blob: 273fb7648fcee7b79110365ce6920d6f7bcac13e [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 Fasheh9b7895e2008-11-12 16:27:44 -080044#include <linux/sort.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#define MLOG_MASK_PREFIX ML_NAMEI
47#include <cluster/masklog.h>
48
49#include "ocfs2.h"
50
51#include "alloc.h"
Joel Beckerc175a512008-12-10 17:58:22 -080052#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053#include "dir.h"
54#include "dlmglue.h"
55#include "extent_map.h"
56#include "file.h"
57#include "inode.h"
58#include "journal.h"
59#include "namei.h"
60#include "suballoc.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070061#include "super.h"
Mark Fasheh9b7895e2008-11-12 16:27:44 -080062#include "sysfile.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080063#include "uptodate.h"
64
65#include "buffer_head_io.h"
66
Mark Fasheh316f4b92007-09-07 18:21:26 -070067#define NAMEI_RA_CHUNKS 2
68#define NAMEI_RA_BLOCKS 4
69#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
71
Mark Fashehccd979b2005-12-15 14:31:24 -080072static unsigned char ocfs2_filetype_table[] = {
73 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
74};
75
Mark Fasheh316f4b92007-09-07 18:21:26 -070076static int ocfs2_do_extend_dir(struct super_block *sb,
77 handle_t *handle,
78 struct inode *dir,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
Mark Fashehe7c17e42009-01-29 18:17:46 -080083static int ocfs2_dir_indexed(struct inode *inode);
Mark Fasheh316f4b92007-09-07 18:21:26 -070084
Mark Fasheh23193e52007-09-12 13:01:18 -070085/*
Mark Fasheh87d35a72008-12-10 17:36:25 -080086 * These are distinct checks because future versions of the file system will
87 * want to have a trailing dirent structure independent of indexing.
88 */
Mark Fashehe7c17e42009-01-29 18:17:46 -080089static int ocfs2_supports_dir_trailer(struct inode *dir)
Mark Fasheh87d35a72008-12-10 17:36:25 -080090{
Mark Fashehe7c17e42009-01-29 18:17:46 -080091 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
92
Mark Fasheh87d35a72008-12-10 17:36:25 -080093 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
95
Mark Fashehe7c17e42009-01-29 18:17:46 -080096 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
Mark Fasheh87d35a72008-12-10 17:36:25 -080097}
98
Mark Fashehe7c17e42009-01-29 18:17:46 -080099/*
100 * "new' here refers to the point at which we're creating a new
101 * directory via "mkdir()", but also when we're expanding an inline
102 * directory. In either case, we don't yet have the indexing bit set
103 * on the directory, so the standard checks will fail in when metaecc
104 * is turned off. Only directory-initialization type functions should
105 * use this then. Everything else wants ocfs2_supports_dir_trailer()
106 */
107static int ocfs2_new_dir_wants_trailer(struct inode *dir)
Mark Fasheh87d35a72008-12-10 17:36:25 -0800108{
Mark Fashehe7c17e42009-01-29 18:17:46 -0800109 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
110
111 return ocfs2_meta_ecc(osb) ||
112 ocfs2_supports_indexed_dirs(osb);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800113}
114
115static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
116{
117 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
118}
119
120#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
121
Joel Beckerc175a512008-12-10 17:58:22 -0800122/* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
123 * them more consistent? */
124struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
125 void *data)
126{
127 char *p = data;
128
129 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
130 return (struct ocfs2_dir_block_trailer *)p;
131}
132
Mark Fasheh87d35a72008-12-10 17:36:25 -0800133/*
134 * XXX: This is executed once on every dirent. We should consider optimizing
135 * it.
136 */
137static int ocfs2_skip_dir_trailer(struct inode *dir,
138 struct ocfs2_dir_entry *de,
139 unsigned long offset,
140 unsigned long blklen)
141{
142 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
143
Mark Fashehe7c17e42009-01-29 18:17:46 -0800144 if (!ocfs2_supports_dir_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -0800145 return 0;
146
147 if (offset != toff)
148 return 0;
149
150 return 1;
151}
152
153static void ocfs2_init_dir_trailer(struct inode *inode,
Mark Fashehe7c17e42009-01-29 18:17:46 -0800154 struct buffer_head *bh, u16 rec_len)
Mark Fasheh87d35a72008-12-10 17:36:25 -0800155{
156 struct ocfs2_dir_block_trailer *trailer;
157
158 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
159 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
160 trailer->db_compat_rec_len =
161 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
162 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
163 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
Mark Fashehe7c17e42009-01-29 18:17:46 -0800164 trailer->db_free_rec_len = cpu_to_le16(rec_len);
165}
166/*
167 * Link an unindexed block with a dir trailer structure into the index free
168 * list. This function will modify dirdata_bh, but assumes you've already
169 * passed it to the journal.
170 */
171static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
172 struct buffer_head *dx_root_bh,
173 struct buffer_head *dirdata_bh)
174{
175 int ret;
176 struct ocfs2_dx_root_block *dx_root;
177 struct ocfs2_dir_block_trailer *trailer;
178
179 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
180 OCFS2_JOURNAL_ACCESS_WRITE);
181 if (ret) {
182 mlog_errno(ret);
183 goto out;
184 }
185 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
186 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
187
188 trailer->db_free_next = dx_root->dr_free_blk;
189 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
190
191 ocfs2_journal_dirty(handle, dx_root_bh);
192
193out:
194 return ret;
195}
196
197static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
198{
199 return res->dl_prev_leaf_bh == NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -0800200}
201
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800202void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
203{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800204 brelse(res->dl_dx_root_bh);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800205 brelse(res->dl_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800206 brelse(res->dl_dx_leaf_bh);
Mark Fashehe7c17e42009-01-29 18:17:46 -0800207 brelse(res->dl_prev_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800208}
209
210static int ocfs2_dir_indexed(struct inode *inode)
211{
212 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
213 return 1;
214 return 0;
215}
216
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800217static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
218{
219 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
220}
221
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800222/*
223 * Hashing code adapted from ext3
224 */
225#define DELTA 0x9E3779B9
226
227static void TEA_transform(__u32 buf[4], __u32 const in[])
228{
229 __u32 sum = 0;
230 __u32 b0 = buf[0], b1 = buf[1];
231 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
232 int n = 16;
233
234 do {
235 sum += DELTA;
236 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
237 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
238 } while (--n);
239
240 buf[0] += b0;
241 buf[1] += b1;
242}
243
244static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
245{
246 __u32 pad, val;
247 int i;
248
249 pad = (__u32)len | ((__u32)len << 8);
250 pad |= pad << 16;
251
252 val = pad;
253 if (len > num*4)
254 len = num * 4;
255 for (i = 0; i < len; i++) {
256 if ((i % 4) == 0)
257 val = pad;
258 val = msg[i] + (val << 8);
259 if ((i % 4) == 3) {
260 *buf++ = val;
261 val = pad;
262 num--;
263 }
264 }
265 if (--num >= 0)
266 *buf++ = val;
267 while (--num >= 0)
268 *buf++ = pad;
269}
270
271static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
272 struct ocfs2_dx_hinfo *hinfo)
273{
274 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
275 const char *p;
276 __u32 in[8], buf[4];
277
278 /*
279 * XXX: Is this really necessary, if the index is never looked
280 * at by readdir? Is a hash value of '0' a bad idea?
281 */
282 if ((len == 1 && !strncmp(".", name, 1)) ||
283 (len == 2 && !strncmp("..", name, 2))) {
284 buf[0] = buf[1] = 0;
285 goto out;
286 }
287
288#ifdef OCFS2_DEBUG_DX_DIRS
289 /*
290 * This makes it very easy to debug indexing problems. We
291 * should never allow this to be selected without hand editing
292 * this file though.
293 */
294 buf[0] = buf[1] = len;
295 goto out;
296#endif
297
298 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
299
300 p = name;
301 while (len > 0) {
302 str2hashbuf(p, len, in, 4);
303 TEA_transform(buf, in);
304 len -= 16;
305 p += 16;
306 }
307
308out:
309 hinfo->major_hash = buf[0];
310 hinfo->minor_hash = buf[1];
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800311}
312
Mark Fasheh87d35a72008-12-10 17:36:25 -0800313/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700314 * bh passed here can be an inode block or a dir data block, depending
315 * on the inode inline data flag.
316 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700317static int ocfs2_check_dir_entry(struct inode * dir,
318 struct ocfs2_dir_entry * de,
319 struct buffer_head * bh,
320 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700321{
322 const char *error_msg = NULL;
323 const int rlen = le16_to_cpu(de->rec_len);
324
325 if (rlen < OCFS2_DIR_REC_LEN(1))
326 error_msg = "rec_len is smaller than minimal";
327 else if (rlen % 4 != 0)
328 error_msg = "rec_len % 4 != 0";
329 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
330 error_msg = "rec_len is too small for name_len";
331 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
332 error_msg = "directory entry across blocks";
333
334 if (error_msg != NULL)
335 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
336 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
337 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
338 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
339 de->name_len);
340 return error_msg == NULL ? 1 : 0;
341}
342
343static inline int ocfs2_match(int len,
344 const char * const name,
345 struct ocfs2_dir_entry *de)
346{
347 if (len != de->name_len)
348 return 0;
349 if (!de->inode)
350 return 0;
351 return !memcmp(name, de->name, len);
352}
353
354/*
355 * Returns 0 if not found, -1 on failure, and 1 on success
356 */
357static int inline ocfs2_search_dirblock(struct buffer_head *bh,
358 struct inode *dir,
359 const char *name, int namelen,
360 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700361 char *first_de,
362 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700363 struct ocfs2_dir_entry **res_dir)
364{
365 struct ocfs2_dir_entry *de;
366 char *dlimit, *de_buf;
367 int de_len;
368 int ret = 0;
369
370 mlog_entry_void();
371
Mark Fasheh23193e52007-09-12 13:01:18 -0700372 de_buf = first_de;
373 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700374
375 while (de_buf < dlimit) {
376 /* this code is executed quadratically often */
377 /* do minimal checking `by hand' */
378
379 de = (struct ocfs2_dir_entry *) de_buf;
380
381 if (de_buf + namelen <= dlimit &&
382 ocfs2_match(namelen, name, de)) {
383 /* found a match - just to be sure, do a full check */
384 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
385 ret = -1;
386 goto bail;
387 }
388 *res_dir = de;
389 ret = 1;
390 goto bail;
391 }
392
393 /* prevent looping on a bad block */
394 de_len = le16_to_cpu(de->rec_len);
395 if (de_len <= 0) {
396 ret = -1;
397 goto bail;
398 }
399
400 de_buf += de_len;
401 offset += de_len;
402 }
403
404bail:
405 mlog_exit(ret);
406 return ret;
407}
408
Mark Fasheh23193e52007-09-12 13:01:18 -0700409static struct buffer_head *ocfs2_find_entry_id(const char *name,
410 int namelen,
411 struct inode *dir,
412 struct ocfs2_dir_entry **res_dir)
413{
414 int ret, found;
415 struct buffer_head *di_bh = NULL;
416 struct ocfs2_dinode *di;
417 struct ocfs2_inline_data *data;
418
Joel Beckerb657c952008-11-13 14:49:11 -0800419 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700420 if (ret) {
421 mlog_errno(ret);
422 goto out;
423 }
424
425 di = (struct ocfs2_dinode *)di_bh->b_data;
426 data = &di->id2.i_data;
427
428 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
429 data->id_data, i_size_read(dir), res_dir);
430 if (found == 1)
431 return di_bh;
432
433 brelse(di_bh);
434out:
435 return NULL;
436}
437
Joel Beckera22305c2008-11-13 14:49:17 -0800438static int ocfs2_validate_dir_block(struct super_block *sb,
439 struct buffer_head *bh)
440{
Joel Beckerc175a512008-12-10 17:58:22 -0800441 int rc;
442 struct ocfs2_dir_block_trailer *trailer =
443 ocfs2_trailer_from_bh(bh, sb);
444
445
Joel Beckera22305c2008-11-13 14:49:17 -0800446 /*
Joel Beckerc175a512008-12-10 17:58:22 -0800447 * We don't validate dirents here, that's handled
Joel Beckera22305c2008-11-13 14:49:17 -0800448 * in-place when the code walks them.
449 */
Joel Becker970e4932008-11-13 14:49:19 -0800450 mlog(0, "Validating dirblock %llu\n",
451 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800452
Joel Beckerc175a512008-12-10 17:58:22 -0800453 BUG_ON(!buffer_uptodate(bh));
454
455 /*
456 * If the ecc fails, we return the error but otherwise
457 * leave the filesystem running. We know any error is
458 * local to this block.
459 *
460 * Note that we are safe to call this even if the directory
461 * doesn't have a trailer. Filesystems without metaecc will do
462 * nothing, and filesystems with it will have one.
463 */
464 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
465 if (rc)
466 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
467 (unsigned long long)bh->b_blocknr);
468
469 return rc;
Joel Beckera22305c2008-11-13 14:49:17 -0800470}
471
472/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800473 * Validate a directory trailer.
474 *
475 * We check the trailer here rather than in ocfs2_validate_dir_block()
476 * because that function doesn't have the inode to test.
477 */
478static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
479{
480 int rc = 0;
481 struct ocfs2_dir_block_trailer *trailer;
482
483 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
484 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
485 rc = -EINVAL;
486 ocfs2_error(dir->i_sb,
487 "Invalid dirblock #%llu: "
488 "signature = %.*s\n",
489 (unsigned long long)bh->b_blocknr, 7,
490 trailer->db_signature);
491 goto out;
492 }
493 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
494 rc = -EINVAL;
495 ocfs2_error(dir->i_sb,
496 "Directory block #%llu has an invalid "
497 "db_blkno of %llu",
498 (unsigned long long)bh->b_blocknr,
499 (unsigned long long)le64_to_cpu(trailer->db_blkno));
500 goto out;
501 }
502 if (le64_to_cpu(trailer->db_parent_dinode) !=
503 OCFS2_I(dir)->ip_blkno) {
504 rc = -EINVAL;
505 ocfs2_error(dir->i_sb,
506 "Directory block #%llu on dinode "
507 "#%llu has an invalid parent_dinode "
508 "of %llu",
509 (unsigned long long)bh->b_blocknr,
510 (unsigned long long)OCFS2_I(dir)->ip_blkno,
511 (unsigned long long)le64_to_cpu(trailer->db_blkno));
512 goto out;
513 }
514out:
515 return rc;
516}
517
518/*
Joel Beckera22305c2008-11-13 14:49:17 -0800519 * This function forces all errors to -EIO for consistency with its
520 * predecessor, ocfs2_bread(). We haven't audited what returning the
521 * real error codes would do to callers. We log the real codes with
522 * mlog_errno() before we squash them.
523 */
524static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
525 struct buffer_head **bh, int flags)
526{
527 int rc = 0;
528 struct buffer_head *tmp = *bh;
Joel Beckera22305c2008-11-13 14:49:17 -0800529
Joel Becker511308d2008-11-13 14:49:21 -0800530 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
531 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800532 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800533 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800534 goto out;
535 }
536
Mark Fasheh87d35a72008-12-10 17:36:25 -0800537 if (!(flags & OCFS2_BH_READAHEAD) &&
Mark Fashehe7c17e42009-01-29 18:17:46 -0800538 ocfs2_supports_dir_trailer(inode)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800539 rc = ocfs2_check_dir_trailer(inode, tmp);
540 if (rc) {
541 if (!*bh)
542 brelse(tmp);
543 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800544 goto out;
545 }
546 }
Joel Beckera22305c2008-11-13 14:49:17 -0800547
Joel Becker511308d2008-11-13 14:49:21 -0800548 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800549 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800550 *bh = tmp;
551
Mark Fasheh87d35a72008-12-10 17:36:25 -0800552out:
Joel Beckera22305c2008-11-13 14:49:17 -0800553 return rc ? -EIO : 0;
554}
555
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800556/*
557 * Read the block at 'phys' which belongs to this directory
558 * inode. This function does no virtual->physical block translation -
559 * what's passed in is assumed to be a valid directory block.
560 */
561static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
562 struct buffer_head **bh)
563{
564 int ret;
565 struct buffer_head *tmp = *bh;
566
Joel Becker8cb471e2009-02-10 20:00:41 -0800567 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
568 ocfs2_validate_dir_block);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800569 if (ret) {
570 mlog_errno(ret);
571 goto out;
572 }
573
574 if (ocfs2_supports_dir_trailer(dir)) {
575 ret = ocfs2_check_dir_trailer(dir, tmp);
576 if (ret) {
577 if (!*bh)
578 brelse(tmp);
579 mlog_errno(ret);
580 goto out;
581 }
582 }
583
584 if (!ret && !*bh)
585 *bh = tmp;
586out:
587 return ret;
588}
589
590static int ocfs2_validate_dx_root(struct super_block *sb,
591 struct buffer_head *bh)
592{
593 int ret;
594 struct ocfs2_dx_root_block *dx_root;
595
596 BUG_ON(!buffer_uptodate(bh));
597
598 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
599
600 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
601 if (ret) {
602 mlog(ML_ERROR,
603 "Checksum failed for dir index root block %llu\n",
604 (unsigned long long)bh->b_blocknr);
605 return ret;
606 }
607
608 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
609 ocfs2_error(sb,
610 "Dir Index Root # %llu has bad signature %.*s",
611 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
612 7, dx_root->dr_signature);
613 return -EINVAL;
614 }
615
616 return 0;
617}
618
619static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
620 struct buffer_head **dx_root_bh)
621{
622 int ret;
623 u64 blkno = le64_to_cpu(di->i_dx_root);
624 struct buffer_head *tmp = *dx_root_bh;
625
Joel Becker8cb471e2009-02-10 20:00:41 -0800626 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
627 ocfs2_validate_dx_root);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800628
629 /* If ocfs2_read_block() got us a new bh, pass it up. */
630 if (!ret && !*dx_root_bh)
631 *dx_root_bh = tmp;
632
633 return ret;
634}
635
636static int ocfs2_validate_dx_leaf(struct super_block *sb,
637 struct buffer_head *bh)
638{
639 int ret;
640 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
641
642 BUG_ON(!buffer_uptodate(bh));
643
644 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
645 if (ret) {
646 mlog(ML_ERROR,
647 "Checksum failed for dir index leaf block %llu\n",
648 (unsigned long long)bh->b_blocknr);
649 return ret;
650 }
651
652 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
653 ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
654 7, dx_leaf->dl_signature);
655 return -EROFS;
656 }
657
658 return 0;
659}
660
661static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
662 struct buffer_head **dx_leaf_bh)
663{
664 int ret;
665 struct buffer_head *tmp = *dx_leaf_bh;
666
Joel Becker8cb471e2009-02-10 20:00:41 -0800667 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
668 ocfs2_validate_dx_leaf);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800669
670 /* If ocfs2_read_block() got us a new bh, pass it up. */
671 if (!ret && !*dx_leaf_bh)
672 *dx_leaf_bh = tmp;
673
674 return ret;
675}
676
677/*
678 * Read a series of dx_leaf blocks. This expects all buffer_head
679 * pointers to be NULL on function entry.
680 */
681static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
682 struct buffer_head **dx_leaf_bhs)
683{
684 int ret;
685
Joel Becker8cb471e2009-02-10 20:00:41 -0800686 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800687 ocfs2_validate_dx_leaf);
688 if (ret)
689 mlog_errno(ret);
690
691 return ret;
692}
693
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200694static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
695 struct inode *dir,
696 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700697{
698 struct super_block *sb;
699 struct buffer_head *bh_use[NAMEI_RA_SIZE];
700 struct buffer_head *bh, *ret = NULL;
701 unsigned long start, block, b;
702 int ra_max = 0; /* Number of bh's in the readahead
703 buffer, bh_use[] */
704 int ra_ptr = 0; /* Current index into readahead
705 buffer */
706 int num = 0;
707 int nblocks, i, err;
708
709 mlog_entry_void();
710
Mark Fasheh316f4b92007-09-07 18:21:26 -0700711 sb = dir->i_sb;
712
713 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
714 start = OCFS2_I(dir)->ip_dir_start_lookup;
715 if (start >= nblocks)
716 start = 0;
717 block = start;
718
719restart:
720 do {
721 /*
722 * We deal with the read-ahead logic here.
723 */
724 if (ra_ptr >= ra_max) {
725 /* Refill the readahead buffer */
726 ra_ptr = 0;
727 b = block;
728 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
729 /*
730 * Terminate if we reach the end of the
731 * directory and must wrap, or if our
732 * search has finished at this block.
733 */
734 if (b >= nblocks || (num && block == start)) {
735 bh_use[ra_max] = NULL;
736 break;
737 }
738 num++;
739
Joel Beckera22305c2008-11-13 14:49:17 -0800740 bh = NULL;
741 err = ocfs2_read_dir_block(dir, b++, &bh,
742 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700743 bh_use[ra_max] = bh;
744 }
745 }
746 if ((bh = bh_use[ra_ptr++]) == NULL)
747 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800748 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700749 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800750 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700751 ocfs2_error(dir->i_sb, "reading directory %llu, "
752 "offset %lu\n",
753 (unsigned long long)OCFS2_I(dir)->ip_blkno,
754 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700755 goto next;
756 }
757 i = ocfs2_search_dirblock(bh, dir, name, namelen,
758 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700759 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700760 res_dir);
761 if (i == 1) {
762 OCFS2_I(dir)->ip_dir_start_lookup = block;
763 ret = bh;
764 goto cleanup_and_exit;
765 } else {
766 brelse(bh);
767 if (i < 0)
768 goto cleanup_and_exit;
769 }
770 next:
771 if (++block >= nblocks)
772 block = 0;
773 } while (block != start);
774
775 /*
776 * If the directory has grown while we were searching, then
777 * search the last part of the directory before giving up.
778 */
779 block = nblocks;
780 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
781 if (block < nblocks) {
782 start = 0;
783 goto restart;
784 }
785
786cleanup_and_exit:
787 /* Clean up the read-ahead blocks */
788 for (; ra_ptr < ra_max; ra_ptr++)
789 brelse(bh_use[ra_ptr]);
790
791 mlog_exit_ptr(ret);
792 return ret;
793}
794
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800795static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
796 struct ocfs2_extent_list *el,
797 u32 major_hash,
798 u32 *ret_cpos,
799 u64 *ret_phys_blkno,
800 unsigned int *ret_clen)
801{
802 int ret = 0, i, found;
803 struct buffer_head *eb_bh = NULL;
804 struct ocfs2_extent_block *eb;
805 struct ocfs2_extent_rec *rec = NULL;
806
807 if (el->l_tree_depth) {
808 ret = ocfs2_find_leaf(inode, el, major_hash, &eb_bh);
809 if (ret) {
810 mlog_errno(ret);
811 goto out;
812 }
813
814 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
815 el = &eb->h_list;
816
817 if (el->l_tree_depth) {
818 ocfs2_error(inode->i_sb,
819 "Inode %lu has non zero tree depth in "
820 "btree tree block %llu\n", inode->i_ino,
821 (unsigned long long)eb_bh->b_blocknr);
822 ret = -EROFS;
823 goto out;
824 }
825 }
826
827 found = 0;
828 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
829 rec = &el->l_recs[i];
830
831 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
832 found = 1;
833 break;
834 }
835 }
836
837 if (!found) {
838 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
839 "record (%u, %u, 0) in btree", inode->i_ino,
840 le32_to_cpu(rec->e_cpos),
841 ocfs2_rec_clusters(el, rec));
842 ret = -EROFS;
843 goto out;
844 }
845
846 if (ret_phys_blkno)
847 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
848 if (ret_cpos)
849 *ret_cpos = le32_to_cpu(rec->e_cpos);
850 if (ret_clen)
851 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
852
853out:
854 brelse(eb_bh);
855 return ret;
856}
857
858/*
859 * Returns the block index, from the start of the cluster which this
860 * hash belongs too.
861 */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800862static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
863 u32 minor_hash)
864{
865 return minor_hash & osb->osb_dx_mask;
866}
867
868static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800869 struct ocfs2_dx_hinfo *hinfo)
870{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800871 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800872}
873
874static int ocfs2_dx_dir_lookup(struct inode *inode,
875 struct ocfs2_extent_list *el,
876 struct ocfs2_dx_hinfo *hinfo,
877 u32 *ret_cpos,
878 u64 *ret_phys_blkno)
879{
880 int ret = 0;
881 unsigned int cend, uninitialized_var(clen);
882 u32 uninitialized_var(cpos);
883 u64 uninitialized_var(blkno);
884 u32 name_hash = hinfo->major_hash;
885
886 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
887 &clen);
888 if (ret) {
889 mlog_errno(ret);
890 goto out;
891 }
892
893 cend = cpos + clen;
894 if (name_hash >= cend) {
895 /* We want the last cluster */
896 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
897 cpos += clen - 1;
898 } else {
899 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
900 name_hash - cpos);
901 cpos = name_hash;
902 }
903
904 /*
905 * We now have the cluster which should hold our entry. To
906 * find the exact block from the start of the cluster to
907 * search, we take the lower bits of the hash.
908 */
909 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
910
911 if (ret_phys_blkno)
912 *ret_phys_blkno = blkno;
913 if (ret_cpos)
914 *ret_cpos = cpos;
915
916out:
917
918 return ret;
919}
920
921static int ocfs2_dx_dir_search(const char *name, int namelen,
922 struct inode *dir,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800923 struct ocfs2_dx_root_block *dx_root,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800924 struct ocfs2_dir_lookup_result *res)
925{
926 int ret, i, found;
927 u64 uninitialized_var(phys);
928 struct buffer_head *dx_leaf_bh = NULL;
929 struct ocfs2_dx_leaf *dx_leaf;
930 struct ocfs2_dx_entry *dx_entry = NULL;
931 struct buffer_head *dir_ent_bh = NULL;
932 struct ocfs2_dir_entry *dir_ent = NULL;
933 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800934 struct ocfs2_extent_list *dr_el;
935 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800936
937 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
938
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800939 if (ocfs2_dx_root_inline(dx_root)) {
940 entry_list = &dx_root->dr_entries;
941 goto search;
942 }
943
944 dr_el = &dx_root->dr_list;
945
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800946 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
947 if (ret) {
948 mlog_errno(ret);
949 goto out;
950 }
951
952 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
953 "returns: %llu\n",
954 (unsigned long long)OCFS2_I(dir)->ip_blkno,
955 namelen, name, hinfo->major_hash, hinfo->minor_hash,
956 (unsigned long long)phys);
957
958 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
959 if (ret) {
960 mlog_errno(ret);
961 goto out;
962 }
963
964 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
965
966 mlog(0, "leaf info: num_used: %d, count: %d\n",
967 le16_to_cpu(dx_leaf->dl_list.de_num_used),
968 le16_to_cpu(dx_leaf->dl_list.de_count));
969
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800970 entry_list = &dx_leaf->dl_list;
971
972search:
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800973 /*
974 * Empty leaf is legal, so no need to check for that.
975 */
976 found = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800977 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
978 dx_entry = &entry_list->de_entries[i];
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800979
980 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
981 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
982 continue;
983
984 /*
985 * Search unindexed leaf block now. We're not
986 * guaranteed to find anything.
987 */
988 ret = ocfs2_read_dir_block_direct(dir,
989 le64_to_cpu(dx_entry->dx_dirent_blk),
990 &dir_ent_bh);
991 if (ret) {
992 mlog_errno(ret);
993 goto out;
994 }
995
996 /*
997 * XXX: We should check the unindexed block here,
998 * before using it.
999 */
1000
1001 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
1002 0, dir_ent_bh->b_data,
1003 dir->i_sb->s_blocksize, &dir_ent);
1004 if (found == 1)
1005 break;
1006
1007 if (found == -1) {
1008 /* This means we found a bad directory entry. */
1009 ret = -EIO;
1010 mlog_errno(ret);
1011 goto out;
1012 }
1013
1014 brelse(dir_ent_bh);
1015 dir_ent_bh = NULL;
1016 }
1017
1018 if (found <= 0) {
1019 ret = -ENOENT;
1020 goto out;
1021 }
1022
1023 res->dl_leaf_bh = dir_ent_bh;
1024 res->dl_entry = dir_ent;
1025 res->dl_dx_leaf_bh = dx_leaf_bh;
1026 res->dl_dx_entry = dx_entry;
1027
1028 ret = 0;
1029out:
1030 if (ret) {
1031 brelse(dx_leaf_bh);
1032 brelse(dir_ent_bh);
1033 }
1034 return ret;
1035}
1036
1037static int ocfs2_find_entry_dx(const char *name, int namelen,
1038 struct inode *dir,
1039 struct ocfs2_dir_lookup_result *lookup)
1040{
1041 int ret;
1042 struct buffer_head *di_bh = NULL;
1043 struct ocfs2_dinode *di;
1044 struct buffer_head *dx_root_bh = NULL;
1045 struct ocfs2_dx_root_block *dx_root;
1046
1047 ret = ocfs2_read_inode_block(dir, &di_bh);
1048 if (ret) {
1049 mlog_errno(ret);
1050 goto out;
1051 }
1052
1053 di = (struct ocfs2_dinode *)di_bh->b_data;
1054
1055 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1056 if (ret) {
1057 mlog_errno(ret);
1058 goto out;
1059 }
1060 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1061
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001062 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001063 if (ret) {
1064 if (ret != -ENOENT)
1065 mlog_errno(ret);
1066 goto out;
1067 }
1068
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001069 lookup->dl_dx_root_bh = dx_root_bh;
1070 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001071out:
1072 brelse(di_bh);
1073 brelse(dx_root_bh);
1074 return ret;
1075}
1076
Mark Fasheh23193e52007-09-12 13:01:18 -07001077/*
1078 * Try to find an entry of the provided name within 'dir'.
1079 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001080 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1081 * returned and the struct 'res' will contain information useful to
1082 * other directory manipulation functions.
Mark Fasheh23193e52007-09-12 13:01:18 -07001083 *
1084 * Caller can NOT assume anything about the contents of the
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001085 * buffer_heads - they are passed back only so that it can be passed
1086 * into any one of the manipulation functions (add entry, delete
1087 * entry, etc). As an example, bh in the extent directory case is a
1088 * data block, in the inline-data case it actually points to an inode,
1089 * in the indexed directory case, multiple buffers are involved.
Mark Fasheh23193e52007-09-12 13:01:18 -07001090 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001091int ocfs2_find_entry(const char *name, int namelen,
1092 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh23193e52007-09-12 13:01:18 -07001093{
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001094 struct buffer_head *bh;
1095 struct ocfs2_dir_entry *res_dir = NULL;
Mark Fasheh23193e52007-09-12 13:01:18 -07001096
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001097 if (ocfs2_dir_indexed(dir))
1098 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1099
1100 /*
1101 * The unindexed dir code only uses part of the lookup
1102 * structure, so there's no reason to push it down further
1103 * than this.
1104 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001105 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001106 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1107 else
1108 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
Mark Fasheh23193e52007-09-12 13:01:18 -07001109
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001110 if (bh == NULL)
1111 return -ENOENT;
1112
1113 lookup->dl_leaf_bh = bh;
1114 lookup->dl_entry = res_dir;
1115 return 0;
Mark Fasheh23193e52007-09-12 13:01:18 -07001116}
1117
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001118/*
1119 * Update inode number and type of a previously found directory entry.
1120 */
Mark Fasheh38760e22007-09-11 17:21:56 -07001121int ocfs2_update_entry(struct inode *dir, handle_t *handle,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001122 struct ocfs2_dir_lookup_result *res,
Mark Fasheh38760e22007-09-11 17:21:56 -07001123 struct inode *new_entry_inode)
1124{
1125 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07001126 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001127 struct ocfs2_dir_entry *de = res->dl_entry;
1128 struct buffer_head *de_bh = res->dl_leaf_bh;
Mark Fasheh38760e22007-09-11 17:21:56 -07001129
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001130 /*
1131 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -07001132 * based directories, so no need to split this up. The only
1133 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001134 */
1135
Joel Becker13723d02008-10-17 19:25:01 -07001136 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1137 access = ocfs2_journal_access_di;
1138
1139 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -07001140 if (ret) {
1141 mlog_errno(ret);
1142 goto out;
1143 }
1144
1145 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1146 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1147
1148 ocfs2_journal_dirty(handle, de_bh);
1149
1150out:
1151 return ret;
1152}
1153
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001154/*
1155 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1156 * previous entry
1157 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001158static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1159 struct ocfs2_dir_entry *de_del,
1160 struct buffer_head *bh, char *first_de,
1161 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001162{
1163 struct ocfs2_dir_entry *de, *pde;
1164 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -07001165 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001166
1167 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1168
Joel Becker13723d02008-10-17 19:25:01 -07001169 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1170 access = ocfs2_journal_access_di;
1171
Mark Fasheh316f4b92007-09-07 18:21:26 -07001172 i = 0;
1173 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001174 de = (struct ocfs2_dir_entry *) first_de;
1175 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001176 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1177 status = -EIO;
1178 mlog_errno(status);
1179 goto bail;
1180 }
1181 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -07001182 status = access(handle, dir, bh,
1183 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001184 if (status < 0) {
1185 status = -EIO;
1186 mlog_errno(status);
1187 goto bail;
1188 }
1189 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +01001190 le16_add_cpu(&pde->rec_len,
1191 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -07001192 else
1193 de->inode = 0;
1194 dir->i_version++;
1195 status = ocfs2_journal_dirty(handle, bh);
1196 goto bail;
1197 }
1198 i += le16_to_cpu(de->rec_len);
1199 pde = de;
1200 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1201 }
1202bail:
1203 mlog_exit(status);
1204 return status;
1205}
1206
Mark Fashehe7c17e42009-01-29 18:17:46 -08001207static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1208{
1209 unsigned int hole;
1210
1211 if (le64_to_cpu(de->inode) == 0)
1212 hole = le16_to_cpu(de->rec_len);
1213 else
1214 hole = le16_to_cpu(de->rec_len) -
1215 OCFS2_DIR_REC_LEN(de->name_len);
1216
1217 return hole;
1218}
1219
1220static int ocfs2_find_max_rec_len(struct super_block *sb,
1221 struct buffer_head *dirblock_bh)
1222{
1223 int size, this_hole, largest_hole = 0;
1224 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1225 struct ocfs2_dir_entry *de;
1226
1227 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1228 size = ocfs2_dir_trailer_blk_off(sb);
1229 limit = start + size;
1230 de_buf = start;
1231 de = (struct ocfs2_dir_entry *)de_buf;
1232 do {
1233 if (de_buf != trailer) {
1234 this_hole = ocfs2_figure_dirent_hole(de);
1235 if (this_hole > largest_hole)
1236 largest_hole = this_hole;
1237 }
1238
1239 de_buf += le16_to_cpu(de->rec_len);
1240 de = (struct ocfs2_dir_entry *)de_buf;
1241 } while (de_buf < limit);
1242
1243 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1244 return largest_hole;
1245 return 0;
1246}
1247
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001248static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1249 int index)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001250{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001251 int num_used = le16_to_cpu(entry_list->de_num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001252
1253 if (num_used == 1 || index == (num_used - 1))
1254 goto clear;
1255
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001256 memmove(&entry_list->de_entries[index],
1257 &entry_list->de_entries[index + 1],
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001258 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1259clear:
1260 num_used--;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001261 memset(&entry_list->de_entries[num_used], 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001262 sizeof(struct ocfs2_dx_entry));
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001263 entry_list->de_num_used = cpu_to_le16(num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001264}
1265
1266static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1267 struct ocfs2_dir_lookup_result *lookup)
1268{
Mark Fashehe7c17e42009-01-29 18:17:46 -08001269 int ret, index, max_rec_len, add_to_free_list = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001270 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001271 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1272 struct ocfs2_dx_leaf *dx_leaf;
1273 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
Mark Fashehe7c17e42009-01-29 18:17:46 -08001274 struct ocfs2_dir_block_trailer *trailer;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001275 struct ocfs2_dx_root_block *dx_root;
1276 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001277
Mark Fashehe7c17e42009-01-29 18:17:46 -08001278 /*
1279 * This function gets a bit messy because we might have to
1280 * modify the root block, regardless of whether the indexed
1281 * entries are stored inline.
1282 */
1283
1284 /*
1285 * *Only* set 'entry_list' here, based on where we're looking
1286 * for the indexed entries. Later, we might still want to
1287 * journal both blocks, based on free list state.
1288 */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001289 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1290 if (ocfs2_dx_root_inline(dx_root)) {
1291 entry_list = &dx_root->dr_entries;
1292 } else {
1293 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1294 entry_list = &dx_leaf->dl_list;
1295 }
1296
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001297 /* Neither of these are a disk corruption - that should have
1298 * been caught by lookup, before we got here. */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001299 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1300 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001301
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001302 index = (char *)dx_entry - (char *)entry_list->de_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001303 index /= sizeof(*dx_entry);
1304
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001305 if (index >= le16_to_cpu(entry_list->de_num_used)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001306 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001307 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1308 entry_list, dx_entry);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001309 return -EIO;
1310 }
1311
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001312 /*
Mark Fashehe7c17e42009-01-29 18:17:46 -08001313 * We know that removal of this dirent will leave enough room
1314 * for a new one, so add this block to the free list if it
1315 * isn't already there.
1316 */
1317 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1318 if (trailer->db_free_rec_len == 0)
1319 add_to_free_list = 1;
1320
1321 /*
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001322 * Add the block holding our index into the journal before
1323 * removing the unindexed entry. If we get an error return
1324 * from __ocfs2_delete_entry(), then it hasn't removed the
1325 * entry yet. Likewise, successful return means we *must*
1326 * remove the indexed entry.
1327 *
Mark Fashehe3a93c22009-02-17 15:29:35 -08001328 * We're also careful to journal the root tree block here as
1329 * the entry count needs to be updated. Also, we might be
1330 * adding to the start of the free list.
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001331 */
Mark Fashehe3a93c22009-02-17 15:29:35 -08001332 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1333 OCFS2_JOURNAL_ACCESS_WRITE);
1334 if (ret) {
1335 mlog_errno(ret);
1336 goto out;
Mark Fashehe7c17e42009-01-29 18:17:46 -08001337 }
1338
1339 if (!ocfs2_dx_root_inline(dx_root)) {
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001340 ret = ocfs2_journal_access_dl(handle, dir,
1341 lookup->dl_dx_leaf_bh,
1342 OCFS2_JOURNAL_ACCESS_WRITE);
1343 if (ret) {
1344 mlog_errno(ret);
1345 goto out;
1346 }
1347 }
1348
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001349 mlog(0, "Dir %llu: delete entry at index: %d\n",
1350 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1351
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001352 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1353 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1354 if (ret) {
1355 mlog_errno(ret);
1356 goto out;
1357 }
1358
Mark Fashehe7c17e42009-01-29 18:17:46 -08001359 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1360 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1361 if (add_to_free_list) {
1362 trailer->db_free_next = dx_root->dr_free_blk;
1363 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1364 ocfs2_journal_dirty(handle, dx_root_bh);
1365 }
1366
1367 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1368 ocfs2_journal_dirty(handle, leaf_bh);
1369
Mark Fashehe3a93c22009-02-17 15:29:35 -08001370 le32_add_cpu(&dx_root->dr_num_entries, -1);
1371 ocfs2_journal_dirty(handle, dx_root_bh);
1372
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001373 ocfs2_dx_list_remove_entry(entry_list, index);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001374
Mark Fashehe3a93c22009-02-17 15:29:35 -08001375 if (!ocfs2_dx_root_inline(dx_root))
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001376 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001377
1378out:
1379 return ret;
1380}
1381
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001382static inline int ocfs2_delete_entry_id(handle_t *handle,
1383 struct inode *dir,
1384 struct ocfs2_dir_entry *de_del,
1385 struct buffer_head *bh)
1386{
1387 int ret;
1388 struct buffer_head *di_bh = NULL;
1389 struct ocfs2_dinode *di;
1390 struct ocfs2_inline_data *data;
1391
Joel Beckerb657c952008-11-13 14:49:11 -08001392 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001393 if (ret) {
1394 mlog_errno(ret);
1395 goto out;
1396 }
1397
1398 di = (struct ocfs2_dinode *)di_bh->b_data;
1399 data = &di->id2.i_data;
1400
1401 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1402 i_size_read(dir));
1403
1404 brelse(di_bh);
1405out:
1406 return ret;
1407}
1408
1409static inline int ocfs2_delete_entry_el(handle_t *handle,
1410 struct inode *dir,
1411 struct ocfs2_dir_entry *de_del,
1412 struct buffer_head *bh)
1413{
1414 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1415 bh->b_size);
1416}
1417
1418/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001419 * Delete a directory entry. Hide the details of directory
1420 * implementation from the caller.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001421 */
1422int ocfs2_delete_entry(handle_t *handle,
1423 struct inode *dir,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001424 struct ocfs2_dir_lookup_result *res)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001425{
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001426 if (ocfs2_dir_indexed(dir))
1427 return ocfs2_delete_entry_dx(handle, dir, res);
1428
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001429 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001430 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1431 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001432
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001433 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1434 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001435}
1436
Mark Fasheh8553cf42007-09-13 16:29:01 -07001437/*
1438 * Check whether 'de' has enough room to hold an entry of
1439 * 'new_rec_len' bytes.
1440 */
1441static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1442 unsigned int new_rec_len)
1443{
1444 unsigned int de_really_used;
1445
1446 /* Check whether this is an empty record with enough space */
1447 if (le64_to_cpu(de->inode) == 0 &&
1448 le16_to_cpu(de->rec_len) >= new_rec_len)
1449 return 1;
1450
1451 /*
1452 * Record might have free space at the end which we can
1453 * use.
1454 */
1455 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1456 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1457 return 1;
1458
1459 return 0;
1460}
1461
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001462static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1463 struct ocfs2_dx_entry *dx_new_entry)
1464{
1465 int i;
1466
1467 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1468 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1469
1470 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1471}
1472
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001473static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1474 struct ocfs2_dx_hinfo *hinfo,
1475 u64 dirent_blk)
1476{
1477 int i;
1478 struct ocfs2_dx_entry *dx_entry;
1479
1480 i = le16_to_cpu(entry_list->de_num_used);
1481 dx_entry = &entry_list->de_entries[i];
1482
1483 memset(dx_entry, 0, sizeof(*dx_entry));
1484 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1485 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1486 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1487
1488 le16_add_cpu(&entry_list->de_num_used, 1);
1489}
1490
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001491static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1492 struct ocfs2_dx_hinfo *hinfo,
1493 u64 dirent_blk,
1494 struct buffer_head *dx_leaf_bh)
1495{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001496 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001497 struct ocfs2_dx_leaf *dx_leaf;
1498
1499 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
1500 OCFS2_JOURNAL_ACCESS_WRITE);
1501 if (ret) {
1502 mlog_errno(ret);
1503 goto out;
1504 }
1505
1506 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001507 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001508 ocfs2_journal_dirty(handle, dx_leaf_bh);
1509
1510out:
1511 return ret;
1512}
1513
Mark Fashehe3a93c22009-02-17 15:29:35 -08001514static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1515 struct ocfs2_dx_hinfo *hinfo,
1516 u64 dirent_blk,
1517 struct ocfs2_dx_root_block *dx_root)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001518{
Mark Fashehe3a93c22009-02-17 15:29:35 -08001519 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1520}
1521
1522static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1523 struct ocfs2_dir_lookup_result *lookup)
1524{
1525 int ret = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001526 struct ocfs2_dx_root_block *dx_root;
Mark Fashehe3a93c22009-02-17 15:29:35 -08001527 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001528
1529 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1530 OCFS2_JOURNAL_ACCESS_WRITE);
1531 if (ret) {
1532 mlog_errno(ret);
1533 goto out;
1534 }
1535
Mark Fashehe3a93c22009-02-17 15:29:35 -08001536 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1537 if (ocfs2_dx_root_inline(dx_root)) {
1538 ocfs2_dx_inline_root_insert(dir, handle,
1539 &lookup->dl_hinfo,
1540 lookup->dl_leaf_bh->b_blocknr,
1541 dx_root);
1542 } else {
1543 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1544 lookup->dl_leaf_bh->b_blocknr,
1545 lookup->dl_dx_leaf_bh);
1546 if (ret)
1547 goto out;
1548 }
1549
1550 le32_add_cpu(&dx_root->dr_num_entries, 1);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001551 ocfs2_journal_dirty(handle, dx_root_bh);
1552
1553out:
1554 return ret;
1555}
1556
Mark Fashehe7c17e42009-01-29 18:17:46 -08001557static void ocfs2_remove_block_from_free_list(struct inode *dir,
1558 handle_t *handle,
1559 struct ocfs2_dir_lookup_result *lookup)
1560{
1561 struct ocfs2_dir_block_trailer *trailer, *prev;
1562 struct ocfs2_dx_root_block *dx_root;
1563 struct buffer_head *bh;
1564
1565 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1566
1567 if (ocfs2_free_list_at_root(lookup)) {
1568 bh = lookup->dl_dx_root_bh;
1569 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1570 dx_root->dr_free_blk = trailer->db_free_next;
1571 } else {
1572 bh = lookup->dl_prev_leaf_bh;
1573 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1574 prev->db_free_next = trailer->db_free_next;
1575 }
1576
1577 trailer->db_free_rec_len = cpu_to_le16(0);
1578 trailer->db_free_next = cpu_to_le64(0);
1579
1580 ocfs2_journal_dirty(handle, bh);
1581 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1582}
1583
1584/*
1585 * This expects that a journal write has been reserved on
1586 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1587 */
1588static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1589 struct ocfs2_dir_lookup_result *lookup)
1590{
1591 int max_rec_len;
1592 struct ocfs2_dir_block_trailer *trailer;
1593
1594 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1595 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1596 if (max_rec_len) {
1597 /*
1598 * There's still room in this block, so no need to remove it
1599 * from the free list. In this case, we just want to update
1600 * the rec len accounting.
1601 */
1602 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1603 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1604 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1605 } else {
1606 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1607 }
1608}
1609
Mark Fasheh316f4b92007-09-07 18:21:26 -07001610/* we don't always have a dentry for what we want to add, so people
1611 * like orphan dir can call this instead.
1612 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001613 * The lookup context must have been filled from
1614 * ocfs2_prepare_dir_for_insert.
Mark Fasheh316f4b92007-09-07 18:21:26 -07001615 */
1616int __ocfs2_add_entry(handle_t *handle,
1617 struct inode *dir,
1618 const char *name, int namelen,
1619 struct inode *inode, u64 blkno,
1620 struct buffer_head *parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001621 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001622{
1623 unsigned long offset;
1624 unsigned short rec_len;
1625 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001626 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1627 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001628 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001629 unsigned int size = sb->s_blocksize;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001630 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001631 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001632
1633 mlog_entry_void();
1634
Mark Fasheh316f4b92007-09-07 18:21:26 -07001635 if (!namelen)
1636 return -EINVAL;
1637
Mark Fashehe7c17e42009-01-29 18:17:46 -08001638 if (ocfs2_dir_indexed(dir)) {
1639 struct buffer_head *bh;
1640
1641 /*
1642 * An indexed dir may require that we update the free space
1643 * list. Reserve a write to the previous node in the list so
1644 * that we don't fail later.
1645 *
1646 * XXX: This can be either a dx_root_block, or an unindexed
1647 * directory tree leaf block.
1648 */
1649 if (ocfs2_free_list_at_root(lookup)) {
1650 bh = lookup->dl_dx_root_bh;
1651 retval = ocfs2_journal_access_dr(handle, dir, bh,
1652 OCFS2_JOURNAL_ACCESS_WRITE);
1653 } else {
1654 bh = lookup->dl_prev_leaf_bh;
1655 retval = ocfs2_journal_access_db(handle, dir, bh,
1656 OCFS2_JOURNAL_ACCESS_WRITE);
1657 }
1658 if (retval) {
1659 mlog_errno(retval);
1660 return retval;
1661 }
1662 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001663 data_start = di->id2.i_data.id_data;
1664 size = i_size_read(dir);
1665
1666 BUG_ON(insert_bh != parent_fe_bh);
1667 }
1668
Mark Fasheh316f4b92007-09-07 18:21:26 -07001669 rec_len = OCFS2_DIR_REC_LEN(namelen);
1670 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001671 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001672 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001673 BUG_ON((char *)de >= (size + data_start));
1674
Mark Fasheh316f4b92007-09-07 18:21:26 -07001675 /* These checks should've already been passed by the
1676 * prepare function, but I guess we can leave them
1677 * here anyway. */
1678 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1679 retval = -ENOENT;
1680 goto bail;
1681 }
1682 if (ocfs2_match(namelen, name, de)) {
1683 retval = -EEXIST;
1684 goto bail;
1685 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001686
Mark Fasheh87d35a72008-12-10 17:36:25 -08001687 /* We're guaranteed that we should have space, so we
1688 * can't possibly have hit the trailer...right? */
1689 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1690 "Hit dir trailer trying to insert %.*s "
1691 "(namelen %d) into directory %llu. "
1692 "offset is %lu, trailer offset is %d\n",
1693 namelen, name, namelen,
1694 (unsigned long long)parent_fe_bh->b_blocknr,
1695 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1696
Mark Fasheh8553cf42007-09-13 16:29:01 -07001697 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001698 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1699 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1700 if (retval < 0) {
1701 mlog_errno(retval);
1702 goto bail;
1703 }
1704
Joel Becker13723d02008-10-17 19:25:01 -07001705 if (insert_bh == parent_fe_bh)
1706 status = ocfs2_journal_access_di(handle, dir,
1707 insert_bh,
1708 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001709 else {
Joel Becker13723d02008-10-17 19:25:01 -07001710 status = ocfs2_journal_access_db(handle, dir,
1711 insert_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001712 OCFS2_JOURNAL_ACCESS_WRITE);
1713
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001714 if (ocfs2_dir_indexed(dir)) {
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001715 status = ocfs2_dx_dir_insert(dir,
1716 handle,
1717 lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001718 if (status) {
1719 mlog_errno(status);
1720 goto bail;
1721 }
1722 }
1723 }
1724
Mark Fasheh316f4b92007-09-07 18:21:26 -07001725 /* By now the buffer is marked for journaling */
1726 offset += le16_to_cpu(de->rec_len);
1727 if (le64_to_cpu(de->inode)) {
1728 de1 = (struct ocfs2_dir_entry *)((char *) de +
1729 OCFS2_DIR_REC_LEN(de->name_len));
1730 de1->rec_len =
1731 cpu_to_le16(le16_to_cpu(de->rec_len) -
1732 OCFS2_DIR_REC_LEN(de->name_len));
1733 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1734 de = de1;
1735 }
1736 de->file_type = OCFS2_FT_UNKNOWN;
1737 if (blkno) {
1738 de->inode = cpu_to_le64(blkno);
1739 ocfs2_set_de_type(de, inode->i_mode);
1740 } else
1741 de->inode = 0;
1742 de->name_len = namelen;
1743 memcpy(de->name, name, namelen);
1744
Mark Fashehe7c17e42009-01-29 18:17:46 -08001745 if (ocfs2_dir_indexed(dir))
1746 ocfs2_recalc_free_list(dir, handle, lookup);
1747
Mark Fasheh316f4b92007-09-07 18:21:26 -07001748 dir->i_version++;
1749 status = ocfs2_journal_dirty(handle, insert_bh);
1750 retval = 0;
1751 goto bail;
1752 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001753
Mark Fasheh316f4b92007-09-07 18:21:26 -07001754 offset += le16_to_cpu(de->rec_len);
1755 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1756 }
1757
1758 /* when you think about it, the assert above should prevent us
1759 * from ever getting here. */
1760 retval = -ENOSPC;
1761bail:
1762
1763 mlog_exit(retval);
1764 return retval;
1765}
1766
Mark Fasheh23193e52007-09-12 13:01:18 -07001767static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001768 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -07001769 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001770 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -07001771{
1772 int ret, i, filldir_ret;
1773 unsigned long offset = *f_pos;
1774 struct buffer_head *di_bh = NULL;
1775 struct ocfs2_dinode *di;
1776 struct ocfs2_inline_data *data;
1777 struct ocfs2_dir_entry *de;
1778
Joel Beckerb657c952008-11-13 14:49:11 -08001779 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -07001780 if (ret) {
1781 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1782 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1783 goto out;
1784 }
1785
1786 di = (struct ocfs2_dinode *)di_bh->b_data;
1787 data = &di->id2.i_data;
1788
1789 while (*f_pos < i_size_read(inode)) {
1790revalidate:
1791 /* If the dir block has changed since the last call to
1792 * readdir(2), then we might be pointing to an invalid
1793 * dirent right now. Scan from the start of the block
1794 * to make sure. */
1795 if (*f_version != inode->i_version) {
1796 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1797 de = (struct ocfs2_dir_entry *)
1798 (data->id_data + i);
1799 /* It's too expensive to do a full
1800 * dirent test each time round this
1801 * loop, but we do have to test at
1802 * least that it is non-zero. A
1803 * failure will be detected in the
1804 * dirent test below. */
1805 if (le16_to_cpu(de->rec_len) <
1806 OCFS2_DIR_REC_LEN(1))
1807 break;
1808 i += le16_to_cpu(de->rec_len);
1809 }
1810 *f_pos = offset = i;
1811 *f_version = inode->i_version;
1812 }
1813
1814 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
1815 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
1816 /* On error, skip the f_pos to the end. */
1817 *f_pos = i_size_read(inode);
1818 goto out;
1819 }
1820 offset += le16_to_cpu(de->rec_len);
1821 if (le64_to_cpu(de->inode)) {
1822 /* We might block in the next section
1823 * if the data destination is
1824 * currently swapped out. So, use a
1825 * version stamp to detect whether or
1826 * not the directory has been modified
1827 * during the copy operation.
1828 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001829 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -07001830 unsigned char d_type = DT_UNKNOWN;
1831
1832 if (de->file_type < OCFS2_FT_MAX)
1833 d_type = ocfs2_filetype_table[de->file_type];
1834
1835 filldir_ret = filldir(priv, de->name,
1836 de->name_len,
1837 *f_pos,
1838 le64_to_cpu(de->inode),
1839 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -07001840 if (filldir_ret) {
1841 if (filldir_err)
1842 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -07001843 break;
Mark Fashehe7b34012007-09-24 14:25:27 -07001844 }
Mark Fasheh23193e52007-09-12 13:01:18 -07001845 if (version != *f_version)
1846 goto revalidate;
1847 }
1848 *f_pos += le16_to_cpu(de->rec_len);
1849 }
1850
1851out:
1852 brelse(di_bh);
1853
1854 return 0;
1855}
1856
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001857/*
1858 * NOTE: This function can be called against unindexed directories,
1859 * and indexed ones.
1860 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001861static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001862 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -07001863 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001864 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -08001865{
1866 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -07001867 unsigned long offset, blk, last_ra_blk = 0;
1868 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 struct buffer_head * bh, * tmp;
1870 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -08001871 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -07001872 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -08001873
1874 stored = 0;
1875 bh = NULL;
1876
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001877 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001878
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001879 while (!error && !stored && *f_pos < i_size_read(inode)) {
1880 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -08001881 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1882 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001883 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -08001884 continue;
1885 }
1886
Mark Fashehaa958872006-04-21 13:49:02 -07001887 /* The idea here is to begin with 8k read-ahead and to stay
1888 * 4k ahead of our current position.
1889 *
1890 * TODO: Use the pagecache for this. We just need to
1891 * make sure it's cluster-safe... */
1892 if (!last_ra_blk
1893 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1894 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -08001895 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -08001896 tmp = NULL;
1897 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1898 OCFS2_BH_READAHEAD))
1899 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 }
Mark Fashehaa958872006-04-21 13:49:02 -07001901 last_ra_blk = blk;
1902 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08001903 }
1904
1905revalidate:
1906 /* If the dir block has changed since the last call to
1907 * readdir(2), then we might be pointing to an invalid
1908 * dirent right now. Scan from the start of the block
1909 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001910 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001911 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1912 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1913 /* It's too expensive to do a full
1914 * dirent test each time round this
1915 * loop, but we do have to test at
1916 * least that it is non-zero. A
1917 * failure will be detected in the
1918 * dirent test below. */
1919 if (le16_to_cpu(de->rec_len) <
1920 OCFS2_DIR_REC_LEN(1))
1921 break;
1922 i += le16_to_cpu(de->rec_len);
1923 }
1924 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001925 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -08001926 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001927 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 }
1929
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001930 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001931 && offset < sb->s_blocksize) {
1932 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1933 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1934 /* On error, skip the f_pos to the
1935 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001936 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001937 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001938 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 }
1940 offset += le16_to_cpu(de->rec_len);
1941 if (le64_to_cpu(de->inode)) {
1942 /* We might block in the next section
1943 * if the data destination is
1944 * currently swapped out. So, use a
1945 * version stamp to detect whether or
1946 * not the directory has been modified
1947 * during the copy operation.
1948 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001949 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -08001950 unsigned char d_type = DT_UNKNOWN;
1951
1952 if (de->file_type < OCFS2_FT_MAX)
1953 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001954 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -08001955 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001956 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -07001957 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -08001958 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -07001959 if (error) {
1960 if (filldir_err)
1961 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001962 break;
Mark Fashehe7b34012007-09-24 14:25:27 -07001963 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001964 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -08001965 goto revalidate;
1966 stored ++;
1967 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001968 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -08001969 }
1970 offset = 0;
1971 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -08001972 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001973 }
1974
1975 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001976out:
1977 return stored;
1978}
1979
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001980static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -07001981 loff_t *f_pos, void *priv, filldir_t filldir,
1982 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -07001983{
1984 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1985 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001986 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001987
Mark Fashehe7b34012007-09-24 14:25:27 -07001988 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1989 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001990}
1991
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001992/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001993 * This is intended to be called from inside other kernel functions,
1994 * so we fake some arguments.
1995 */
1996int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1997 filldir_t filldir)
1998{
Mark Fashehe7b34012007-09-24 14:25:27 -07001999 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002000 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07002001
2002 while (*f_pos < i_size_read(inode)) {
2003 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07002004 filldir, &filldir_err);
2005 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -07002006 break;
2007 }
2008
Mark Fashehe7b34012007-09-24 14:25:27 -07002009 if (ret > 0)
2010 ret = -EIO;
2011
Mark Fasheh5eae5b92007-09-10 17:50:51 -07002012 return 0;
2013}
2014
2015/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002016 * ocfs2_readdir()
2017 *
2018 */
2019int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
2020{
2021 int error = 0;
2022 struct inode *inode = filp->f_path.dentry->d_inode;
2023 int lock_level = 0;
2024
2025 mlog_entry("dirino=%llu\n",
2026 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2027
Mark Fashehe63aecb62007-10-18 15:30:42 -07002028 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002029 if (lock_level && error >= 0) {
2030 /* We release EX lock which used to update atime
2031 * and get PR lock again to reduce contention
2032 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002033 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002034 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002035 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002036 }
2037 if (error < 0) {
2038 if (error != -ENOENT)
2039 mlog_errno(error);
2040 /* we haven't got any yet, so propagate the error. */
2041 goto bail_nolock;
2042 }
2043
2044 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -07002045 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002046
Mark Fashehe63aecb62007-10-18 15:30:42 -07002047 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002048
Mark Fashehaa958872006-04-21 13:49:02 -07002049bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002050 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08002051
Mark Fashehb8bc5f42007-09-10 17:17:52 -07002052 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08002053}
2054
2055/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002056 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 */
2058int ocfs2_find_files_on_disk(const char *name,
2059 int namelen,
2060 u64 *blkno,
2061 struct inode *inode,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002062 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08002063{
2064 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08002065
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002066 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
2067 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002068
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002069 status = ocfs2_find_entry(name, namelen, inode, lookup);
2070 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002071 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002072
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002073 *blkno = le64_to_cpu(lookup->dl_entry->inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002074
2075 status = 0;
2076leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08002077
Mark Fashehccd979b2005-12-15 14:31:24 -08002078 return status;
2079}
2080
Mark Fashehbe94d112007-09-11 15:22:06 -07002081/*
2082 * Convenience function for callers which just want the block number
2083 * mapped to a name and don't require the full dirent info, etc.
2084 */
2085int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2086 int namelen, u64 *blkno)
2087{
2088 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002089 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehbe94d112007-09-11 15:22:06 -07002090
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002091 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2092 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehbe94d112007-09-11 15:22:06 -07002093
2094 return ret;
2095}
2096
Mark Fashehccd979b2005-12-15 14:31:24 -08002097/* Check for a name within a directory.
2098 *
2099 * Return 0 if the name does not exist
2100 * Return -EEXIST if the directory contains the name
2101 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002102 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08002103 */
2104int ocfs2_check_dir_for_entry(struct inode *dir,
2105 const char *name,
2106 int namelen)
2107{
2108 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002109 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08002110
Mark Fashehb06970532006-03-03 10:24:33 -08002111 mlog_entry("dir %llu, name '%.*s'\n",
2112 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002113
2114 ret = -EEXIST;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002115 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08002116 goto bail;
2117
2118 ret = 0;
2119bail:
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002120 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002121
2122 mlog_exit(ret);
2123 return ret;
2124}
2125
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002126struct ocfs2_empty_dir_priv {
2127 unsigned seen_dot;
2128 unsigned seen_dot_dot;
2129 unsigned seen_other;
Mark Fashehe3a93c22009-02-17 15:29:35 -08002130 unsigned dx_dir;
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002131};
2132static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
2133 loff_t pos, u64 ino, unsigned type)
2134{
2135 struct ocfs2_empty_dir_priv *p = priv;
2136
2137 /*
2138 * Check the positions of "." and ".." records to be sure
2139 * they're in the correct place.
Mark Fashehe3a93c22009-02-17 15:29:35 -08002140 *
2141 * Indexed directories don't need to proceed past the first
2142 * two entries, so we end the scan after seeing '..'. Despite
2143 * that, we allow the scan to proceed In the event that we
2144 * have a corrupted indexed directory (no dot or dot dot
2145 * entries). This allows us to double check for existing
2146 * entries which might not have been found in the index.
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002147 */
2148 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2149 p->seen_dot = 1;
2150 return 0;
2151 }
2152
2153 if (name_len == 2 && !strncmp("..", name, 2) &&
2154 pos == OCFS2_DIR_REC_LEN(1)) {
2155 p->seen_dot_dot = 1;
Mark Fashehe3a93c22009-02-17 15:29:35 -08002156
2157 if (p->dx_dir && p->seen_dot)
2158 return 1;
2159
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002160 return 0;
2161 }
2162
2163 p->seen_other = 1;
2164 return 1;
2165}
Mark Fashehe3a93c22009-02-17 15:29:35 -08002166
2167static int ocfs2_empty_dir_dx(struct inode *inode,
2168 struct ocfs2_empty_dir_priv *priv)
2169{
2170 int ret;
2171 struct buffer_head *di_bh = NULL;
2172 struct buffer_head *dx_root_bh = NULL;
2173 struct ocfs2_dinode *di;
2174 struct ocfs2_dx_root_block *dx_root;
2175
2176 priv->dx_dir = 1;
2177
2178 ret = ocfs2_read_inode_block(inode, &di_bh);
2179 if (ret) {
2180 mlog_errno(ret);
2181 goto out;
2182 }
2183 di = (struct ocfs2_dinode *)di_bh->b_data;
2184
2185 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2186 if (ret) {
2187 mlog_errno(ret);
2188 goto out;
2189 }
2190 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2191
2192 if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2193 priv->seen_other = 1;
2194
2195out:
2196 brelse(di_bh);
2197 brelse(dx_root_bh);
2198 return ret;
2199}
2200
Mark Fashehccd979b2005-12-15 14:31:24 -08002201/*
2202 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002203 *
2204 * Returns 1 if dir is empty, zero otherwise.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002205 *
Mark Fashehe3a93c22009-02-17 15:29:35 -08002206 * XXX: This is a performance problem for unindexed directories.
Mark Fashehccd979b2005-12-15 14:31:24 -08002207 */
2208int ocfs2_empty_dir(struct inode *inode)
2209{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002210 int ret;
2211 loff_t start = 0;
2212 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08002213
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002214 memset(&priv, 0, sizeof(priv));
2215
Mark Fashehe3a93c22009-02-17 15:29:35 -08002216 if (ocfs2_dir_indexed(inode)) {
2217 ret = ocfs2_empty_dir_dx(inode, &priv);
2218 if (ret)
2219 mlog_errno(ret);
2220 /*
2221 * We still run ocfs2_dir_foreach to get the checks
2222 * for "." and "..".
2223 */
2224 }
2225
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002226 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
2227 if (ret)
2228 mlog_errno(ret);
2229
2230 if (!priv.seen_dot || !priv.seen_dot_dot) {
2231 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002232 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002233 /*
2234 * XXX: Is it really safe to allow an unlink to continue?
2235 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002236 return 1;
2237 }
2238
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002239 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08002240}
2241
Mark Fasheh87d35a72008-12-10 17:36:25 -08002242/*
2243 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2244 * "..", which might be used during creation of a directory with a trailing
2245 * header. It is otherwise safe to ignore the return code.
2246 */
2247static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2248 struct inode *parent,
2249 char *start,
2250 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002251{
2252 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2253
2254 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2255 de->name_len = 1;
2256 de->rec_len =
2257 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2258 strcpy(de->name, ".");
2259 ocfs2_set_de_type(de, S_IFDIR);
2260
2261 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2262 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2263 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2264 de->name_len = 2;
2265 strcpy(de->name, "..");
2266 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08002267
2268 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002269}
2270
2271/*
2272 * This works together with code in ocfs2_mknod_locked() which sets
2273 * the inline-data flag and initializes the inline-data section.
2274 */
2275static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2276 handle_t *handle,
2277 struct inode *parent,
2278 struct inode *inode,
2279 struct buffer_head *di_bh)
2280{
2281 int ret;
2282 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2283 struct ocfs2_inline_data *data = &di->id2.i_data;
2284 unsigned int size = le16_to_cpu(data->id_count);
2285
Joel Becker13723d02008-10-17 19:25:01 -07002286 ret = ocfs2_journal_access_di(handle, inode, di_bh,
2287 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002288 if (ret) {
2289 mlog_errno(ret);
2290 goto out;
2291 }
2292
2293 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2294
2295 ocfs2_journal_dirty(handle, di_bh);
2296 if (ret) {
2297 mlog_errno(ret);
2298 goto out;
2299 }
2300
2301 i_size_write(inode, size);
2302 inode->i_nlink = 2;
2303 inode->i_blocks = ocfs2_inode_sector_count(inode);
2304
2305 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2306 if (ret < 0)
2307 mlog_errno(ret);
2308
2309out:
2310 return ret;
2311}
2312
2313static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2314 handle_t *handle,
2315 struct inode *parent,
2316 struct inode *inode,
2317 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002318 struct ocfs2_alloc_context *data_ac,
2319 struct buffer_head **ret_new_bh)
Mark Fasheh316f4b92007-09-07 18:21:26 -07002320{
2321 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002322 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002323 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002324 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002325
2326 mlog_entry_void();
2327
Mark Fashehe7c17e42009-01-29 18:17:46 -08002328 if (ocfs2_new_dir_wants_trailer(inode))
Mark Fasheh87d35a72008-12-10 17:36:25 -08002329 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2330
Mark Fasheh316f4b92007-09-07 18:21:26 -07002331 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2332 data_ac, NULL, &new_bh);
2333 if (status < 0) {
2334 mlog_errno(status);
2335 goto bail;
2336 }
2337
Joel Becker8cb471e2009-02-10 20:00:41 -08002338 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002339
Joel Becker13723d02008-10-17 19:25:01 -07002340 status = ocfs2_journal_access_db(handle, inode, new_bh,
2341 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002342 if (status < 0) {
2343 mlog_errno(status);
2344 goto bail;
2345 }
2346 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2347
Mark Fasheh87d35a72008-12-10 17:36:25 -08002348 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002349 if (ocfs2_new_dir_wants_trailer(inode)) {
2350 int size = le16_to_cpu(de->rec_len);
2351
2352 /*
2353 * Figure out the size of the hole left over after
2354 * insertion of '.' and '..'. The trailer wants this
2355 * information.
2356 */
2357 size -= OCFS2_DIR_REC_LEN(2);
2358 size -= sizeof(struct ocfs2_dir_block_trailer);
2359
2360 ocfs2_init_dir_trailer(inode, new_bh, size);
2361 }
Mark Fasheh316f4b92007-09-07 18:21:26 -07002362
2363 status = ocfs2_journal_dirty(handle, new_bh);
2364 if (status < 0) {
2365 mlog_errno(status);
2366 goto bail;
2367 }
2368
2369 i_size_write(inode, inode->i_sb->s_blocksize);
2370 inode->i_nlink = 2;
2371 inode->i_blocks = ocfs2_inode_sector_count(inode);
2372 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2373 if (status < 0) {
2374 mlog_errno(status);
2375 goto bail;
2376 }
2377
2378 status = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002379 if (ret_new_bh) {
2380 *ret_new_bh = new_bh;
2381 new_bh = NULL;
2382 }
Mark Fasheh316f4b92007-09-07 18:21:26 -07002383bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002384 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002385
2386 mlog_exit(status);
2387 return status;
2388}
2389
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002390static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2391 handle_t *handle, struct inode *dir,
2392 struct buffer_head *di_bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08002393 struct buffer_head *dirdata_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002394 struct ocfs2_alloc_context *meta_ac,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002395 int dx_inline, u32 num_entries,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002396 struct buffer_head **ret_dx_root_bh)
2397{
2398 int ret;
2399 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2400 u16 dr_suballoc_bit;
2401 u64 dr_blkno;
2402 unsigned int num_bits;
2403 struct buffer_head *dx_root_bh = NULL;
2404 struct ocfs2_dx_root_block *dx_root;
Mark Fashehe7c17e42009-01-29 18:17:46 -08002405 struct ocfs2_dir_block_trailer *trailer =
2406 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002407
2408 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit,
2409 &num_bits, &dr_blkno);
2410 if (ret) {
2411 mlog_errno(ret);
2412 goto out;
2413 }
2414
2415 mlog(0, "Dir %llu, attach new index block: %llu\n",
2416 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2417 (unsigned long long)dr_blkno);
2418
2419 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2420 if (dx_root_bh == NULL) {
2421 ret = -EIO;
2422 goto out;
2423 }
Joel Becker8cb471e2009-02-10 20:00:41 -08002424 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002425
2426 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
2427 OCFS2_JOURNAL_ACCESS_CREATE);
2428 if (ret < 0) {
2429 mlog_errno(ret);
2430 goto out;
2431 }
2432
2433 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2434 memset(dx_root, 0, osb->sb->s_blocksize);
2435 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2436 dx_root->dr_suballoc_slot = cpu_to_le16(osb->slot_num);
2437 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2438 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2439 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2440 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
Mark Fashehe3a93c22009-02-17 15:29:35 -08002441 dx_root->dr_num_entries = cpu_to_le32(num_entries);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002442 if (le16_to_cpu(trailer->db_free_rec_len))
2443 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2444 else
2445 dx_root->dr_free_blk = cpu_to_le64(0);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002446
2447 if (dx_inline) {
2448 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2449 dx_root->dr_entries.de_count =
2450 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2451 } else {
2452 dx_root->dr_list.l_count =
2453 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2454 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002455
2456 ret = ocfs2_journal_dirty(handle, dx_root_bh);
2457 if (ret)
2458 mlog_errno(ret);
2459
2460 ret = ocfs2_journal_access_di(handle, dir, di_bh,
2461 OCFS2_JOURNAL_ACCESS_CREATE);
2462 if (ret) {
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466
2467 di->i_dx_root = cpu_to_le64(dr_blkno);
2468
2469 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2470 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2471
2472 ret = ocfs2_journal_dirty(handle, di_bh);
2473 if (ret)
2474 mlog_errno(ret);
2475
2476 *ret_dx_root_bh = dx_root_bh;
2477 dx_root_bh = NULL;
2478
2479out:
2480 brelse(dx_root_bh);
2481 return ret;
2482}
2483
2484static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2485 handle_t *handle, struct inode *dir,
2486 struct buffer_head **dx_leaves,
2487 int num_dx_leaves, u64 start_blk)
2488{
2489 int ret, i;
2490 struct ocfs2_dx_leaf *dx_leaf;
2491 struct buffer_head *bh;
2492
2493 for (i = 0; i < num_dx_leaves; i++) {
2494 bh = sb_getblk(osb->sb, start_blk + i);
2495 if (bh == NULL) {
2496 ret = -EIO;
2497 goto out;
2498 }
2499 dx_leaves[i] = bh;
2500
Joel Becker8cb471e2009-02-10 20:00:41 -08002501 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002502
2503 ret = ocfs2_journal_access_dl(handle, dir, bh,
2504 OCFS2_JOURNAL_ACCESS_CREATE);
2505 if (ret < 0) {
2506 mlog_errno(ret);
2507 goto out;
2508 }
2509
2510 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2511
2512 memset(dx_leaf, 0, osb->sb->s_blocksize);
2513 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2514 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2515 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2516 dx_leaf->dl_list.de_count =
2517 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2518
2519 mlog(0,
2520 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2521 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2522 (unsigned long long)bh->b_blocknr,
2523 le16_to_cpu(dx_leaf->dl_list.de_count));
2524
2525 ocfs2_journal_dirty(handle, bh);
2526 }
2527
2528 ret = 0;
2529out:
2530 return ret;
2531}
2532
2533/*
2534 * Allocates and formats a new cluster for use in an indexed dir
2535 * leaf. This version will not do the extent insert, so that it can be
2536 * used by operations which need careful ordering.
2537 */
2538static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2539 u32 cpos, handle_t *handle,
2540 struct ocfs2_alloc_context *data_ac,
2541 struct buffer_head **dx_leaves,
2542 int num_dx_leaves, u64 *ret_phys_blkno)
2543{
2544 int ret;
2545 u32 phys, num;
2546 u64 phys_blkno;
2547 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2548
2549 /*
2550 * XXX: For create, this should claim cluster for the index
2551 * *before* the unindexed insert so that we have a better
2552 * chance of contiguousness as the directory grows in number
2553 * of entries.
2554 */
2555 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 1, &phys, &num);
2556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2559 }
2560
2561 /*
2562 * Format the new cluster first. That way, we're inserting
2563 * valid data.
2564 */
2565 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2566 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2567 num_dx_leaves, phys_blkno);
2568 if (ret) {
2569 mlog_errno(ret);
2570 goto out;
2571 }
2572
2573 *ret_phys_blkno = phys_blkno;
2574out:
2575 return ret;
2576}
2577
2578static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2579 struct ocfs2_extent_tree *et,
2580 u32 cpos, handle_t *handle,
2581 struct ocfs2_alloc_context *data_ac,
2582 struct ocfs2_alloc_context *meta_ac,
2583 struct buffer_head **dx_leaves,
2584 int num_dx_leaves)
2585{
2586 int ret;
2587 u64 phys_blkno;
2588 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2589
2590 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2591 num_dx_leaves, &phys_blkno);
2592 if (ret) {
2593 mlog_errno(ret);
2594 goto out;
2595 }
2596
2597 ret = ocfs2_insert_extent(osb, handle, dir, et, cpos, phys_blkno, 1, 0,
2598 meta_ac);
2599 if (ret)
2600 mlog_errno(ret);
2601out:
2602 return ret;
2603}
2604
2605static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2606 int *ret_num_leaves)
2607{
2608 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2609 struct buffer_head **dx_leaves;
2610
2611 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2612 GFP_NOFS);
2613 if (dx_leaves && ret_num_leaves)
2614 *ret_num_leaves = num_dx_leaves;
2615
2616 return dx_leaves;
2617}
2618
2619static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2620 handle_t *handle,
2621 struct inode *parent,
2622 struct inode *inode,
2623 struct buffer_head *di_bh,
2624 struct ocfs2_alloc_context *data_ac,
2625 struct ocfs2_alloc_context *meta_ac)
2626{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002627 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002628 struct buffer_head *leaf_bh = NULL;
2629 struct buffer_head *dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002630 struct ocfs2_dx_hinfo hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002631 struct ocfs2_dx_root_block *dx_root;
2632 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002633
2634 /*
2635 * Our strategy is to create the directory as though it were
2636 * unindexed, then add the index block. This works with very
2637 * little complication since the state of a new directory is a
2638 * very well known quantity.
2639 *
2640 * Essentially, we have two dirents ("." and ".."), in the 1st
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002641 * block which need indexing. These are easily inserted into
2642 * the index block.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002643 */
2644
2645 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2646 data_ac, &leaf_bh);
2647 if (ret) {
2648 mlog_errno(ret);
2649 goto out;
2650 }
2651
Mark Fashehe7c17e42009-01-29 18:17:46 -08002652 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002653 meta_ac, 1, 2, &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002654 if (ret) {
2655 mlog_errno(ret);
2656 goto out;
2657 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002658 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2659 entry_list = &dx_root->dr_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002660
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002661 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
Mark Fashehe7c17e42009-01-29 18:17:46 -08002662 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002663 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002664
2665 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002666 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002667
2668out:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002669 brelse(dx_root_bh);
2670 brelse(leaf_bh);
2671 return ret;
2672}
2673
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002674int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2675 handle_t *handle,
2676 struct inode *parent,
2677 struct inode *inode,
2678 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002679 struct ocfs2_alloc_context *data_ac,
2680 struct ocfs2_alloc_context *meta_ac)
2681
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002682{
2683 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2684
2685 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2686 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2687
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002688 if (ocfs2_supports_indexed_dirs(osb))
2689 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2690 data_ac, meta_ac);
2691
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002692 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002693 data_ac, NULL);
2694}
2695
2696static int ocfs2_dx_dir_index_block(struct inode *dir,
2697 handle_t *handle,
2698 struct buffer_head **dx_leaves,
2699 int num_dx_leaves,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002700 u32 *num_dx_entries,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002701 struct buffer_head *dirent_bh)
2702{
Tao Ma0fba8132009-03-19 05:08:43 +08002703 int ret = 0, namelen, i;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002704 char *de_buf, *limit;
2705 struct ocfs2_dir_entry *de;
2706 struct buffer_head *dx_leaf_bh;
2707 struct ocfs2_dx_hinfo hinfo;
2708 u64 dirent_blk = dirent_bh->b_blocknr;
2709
2710 de_buf = dirent_bh->b_data;
2711 limit = de_buf + dir->i_sb->s_blocksize;
2712
2713 while (de_buf < limit) {
2714 de = (struct ocfs2_dir_entry *)de_buf;
2715
2716 namelen = de->name_len;
2717 if (!namelen || !de->inode)
2718 goto inc;
2719
2720 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2721
2722 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2723 dx_leaf_bh = dx_leaves[i];
2724
2725 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2726 dirent_blk, dx_leaf_bh);
2727 if (ret) {
2728 mlog_errno(ret);
2729 goto out;
2730 }
2731
Mark Fashehe3a93c22009-02-17 15:29:35 -08002732 *num_dx_entries = *num_dx_entries + 1;
2733
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002734inc:
2735 de_buf += le16_to_cpu(de->rec_len);
2736 }
2737
2738out:
2739 return ret;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002740}
Mark Fashehe7c17e42009-01-29 18:17:46 -08002741
2742/*
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002743 * XXX: This expects dx_root_bh to already be part of the transaction.
2744 */
2745static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2746 struct buffer_head *dx_root_bh,
2747 struct buffer_head *dirent_bh)
2748{
2749 char *de_buf, *limit;
2750 struct ocfs2_dx_root_block *dx_root;
2751 struct ocfs2_dir_entry *de;
2752 struct ocfs2_dx_hinfo hinfo;
2753 u64 dirent_blk = dirent_bh->b_blocknr;
2754
2755 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2756
2757 de_buf = dirent_bh->b_data;
2758 limit = de_buf + dir->i_sb->s_blocksize;
2759
2760 while (de_buf < limit) {
2761 de = (struct ocfs2_dir_entry *)de_buf;
2762
2763 if (!de->name_len || !de->inode)
2764 goto inc;
2765
2766 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2767
2768 mlog(0,
2769 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2770 (unsigned long long)dir->i_ino, hinfo.major_hash,
2771 hinfo.minor_hash,
2772 le16_to_cpu(dx_root->dr_entries.de_num_used),
2773 de->name_len, de->name);
2774
2775 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2776 dirent_blk);
Mark Fashehe3a93c22009-02-17 15:29:35 -08002777
2778 le32_add_cpu(&dx_root->dr_num_entries, 1);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002779inc:
2780 de_buf += le16_to_cpu(de->rec_len);
2781 }
2782}
2783
2784/*
2785 * Count the number of inline directory entries in di_bh and compare
2786 * them against the number of entries we can hold in an inline dx root
2787 * block.
2788 */
2789static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2790 struct buffer_head *di_bh)
2791{
2792 int dirent_count = 0;
2793 char *de_buf, *limit;
2794 struct ocfs2_dir_entry *de;
2795 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2796
2797 de_buf = di->id2.i_data.id_data;
2798 limit = de_buf + i_size_read(dir);
2799
2800 while (de_buf < limit) {
2801 de = (struct ocfs2_dir_entry *)de_buf;
2802
2803 if (de->name_len && de->inode)
2804 dirent_count++;
2805
2806 de_buf += le16_to_cpu(de->rec_len);
2807 }
2808
2809 /* We are careful to leave room for one extra record. */
2810 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2811}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002812
Mark Fasheh87d35a72008-12-10 17:36:25 -08002813/*
2814 * Expand rec_len of the rightmost dirent in a directory block so that it
2815 * contains the end of our valid space for dirents. We do this during
2816 * expansion from an inline directory to one with extents. The first dir block
2817 * in that case is taken from the inline data portion of the inode block.
2818 *
Mark Fashehe7c17e42009-01-29 18:17:46 -08002819 * This will also return the largest amount of contiguous space for a dirent
2820 * in the block. That value is *not* necessarily the last dirent, even after
2821 * expansion. The directory indexing code wants this value for free space
2822 * accounting. We do this here since we're already walking the entire dir
2823 * block.
2824 *
Mark Fasheh87d35a72008-12-10 17:36:25 -08002825 * We add the dir trailer if this filesystem wants it.
2826 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08002827static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2828 struct inode *dir)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002829{
Mark Fashehe7c17e42009-01-29 18:17:46 -08002830 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002831 struct ocfs2_dir_entry *de;
2832 struct ocfs2_dir_entry *prev_de;
2833 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002834 unsigned int new_size = sb->s_blocksize;
Mark Fashehe7c17e42009-01-29 18:17:46 -08002835 unsigned int bytes, this_hole;
2836 unsigned int largest_hole = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002837
Mark Fashehe7c17e42009-01-29 18:17:46 -08002838 if (ocfs2_new_dir_wants_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -08002839 new_size = ocfs2_dir_trailer_blk_off(sb);
2840
2841 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002842
2843 limit = start + old_size;
2844 de_buf = start;
2845 de = (struct ocfs2_dir_entry *)de_buf;
2846 do {
Mark Fashehe7c17e42009-01-29 18:17:46 -08002847 this_hole = ocfs2_figure_dirent_hole(de);
2848 if (this_hole > largest_hole)
2849 largest_hole = this_hole;
2850
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002851 prev_de = de;
2852 de_buf += le16_to_cpu(de->rec_len);
2853 de = (struct ocfs2_dir_entry *)de_buf;
2854 } while (de_buf < limit);
2855
2856 le16_add_cpu(&prev_de->rec_len, bytes);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002857
2858 /* We need to double check this after modification of the final
2859 * dirent. */
2860 this_hole = ocfs2_figure_dirent_hole(prev_de);
2861 if (this_hole > largest_hole)
2862 largest_hole = this_hole;
2863
2864 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2865 return largest_hole;
2866 return 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002867}
2868
2869/*
2870 * We allocate enough clusters to fulfill "blocks_wanted", but set
2871 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2872 * rest automatically for us.
2873 *
2874 * *first_block_bh is a pointer to the 1st data block allocated to the
2875 * directory.
2876 */
2877static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2878 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002879 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002880 struct buffer_head **first_block_bh)
2881{
Mark Fashehe3a93c22009-02-17 15:29:35 -08002882 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002883 struct super_block *sb = dir->i_sb;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002884 int ret, i, num_dx_leaves = 0, dx_inline = 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002885 credits = ocfs2_inline_to_extents_credits(sb);
2886 u64 dx_insert_blkno, blkno,
2887 bytes = blocks_wanted << sb->s_blocksize_bits;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002888 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2889 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2890 struct ocfs2_alloc_context *data_ac;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002891 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002892 struct buffer_head *dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002893 struct buffer_head *dx_root_bh = NULL;
2894 struct buffer_head **dx_leaves = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002895 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2896 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002897 struct ocfs2_extent_tree et;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002898 struct ocfs2_extent_tree dx_et;
2899 int did_quota = 0, bytes_allocated = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002900
Joel Becker8d6220d2008-08-22 12:46:09 -07002901 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002902
2903 alloc = ocfs2_clusters_for_bytes(sb, bytes);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002904 dx_alloc = 0;
2905
Jan Karaedd45c02009-06-02 14:24:03 +02002906 down_write(&oi->ip_alloc_sem);
2907
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002908 if (ocfs2_supports_indexed_dirs(osb)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002909 credits += ocfs2_add_dir_index_credits(sb);
2910
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002911 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2912 if (!dx_inline) {
2913 /* Add one more cluster for an index leaf */
2914 dx_alloc++;
2915 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2916 &num_dx_leaves);
2917 if (!dx_leaves) {
2918 ret = -ENOMEM;
2919 mlog_errno(ret);
2920 goto out;
2921 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002922 }
2923
2924 /* This gets us the dx_root */
2925 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2926 if (ret) {
2927 mlog_errno(ret);
2928 goto out;
2929 }
2930 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002931
2932 /*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002933 * We should never need more than 2 clusters for the unindexed
2934 * tree - maximum dirent size is far less than one block. In
2935 * fact, the only time we'd need more than one cluster is if
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002936 * blocksize == clustersize and the dirent won't fit in the
2937 * extra space that the expansion to a single block gives. As
2938 * of today, that only happens on 4k/4k file systems.
2939 */
2940 BUG_ON(alloc > 2);
2941
Tao Ma035a5712009-04-07 07:40:57 +08002942 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002943 if (ret) {
2944 mlog_errno(ret);
2945 goto out;
2946 }
2947
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002948 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02002949 * Prepare for worst case allocation scenario of two separate
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002950 * extents in the unindexed tree.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002951 */
2952 if (alloc == 2)
2953 credits += OCFS2_SUBALLOC_ALLOC;
2954
2955 handle = ocfs2_start_trans(osb, credits);
2956 if (IS_ERR(handle)) {
2957 ret = PTR_ERR(handle);
2958 mlog_errno(ret);
Jan Karaedd45c02009-06-02 14:24:03 +02002959 goto out;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002960 }
2961
Jan Karaa90714c2008-10-09 19:38:40 +02002962 if (vfs_dq_alloc_space_nodirty(dir,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002963 ocfs2_clusters_to_bytes(osb->sb,
2964 alloc + dx_alloc))) {
Jan Karaa90714c2008-10-09 19:38:40 +02002965 ret = -EDQUOT;
2966 goto out_commit;
2967 }
2968 did_quota = 1;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002969
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002970 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002971 /*
2972 * Allocate our index cluster first, to maximize the
2973 * possibility that unindexed leaves grow
2974 * contiguously.
2975 */
2976 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2977 dx_leaves, num_dx_leaves,
2978 &dx_insert_blkno);
2979 if (ret) {
2980 mlog_errno(ret);
2981 goto out_commit;
2982 }
2983 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2984 }
2985
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002986 /*
2987 * Try to claim as many clusters as the bitmap can give though
2988 * if we only get one now, that's enough to continue. The rest
2989 * will be claimed after the conversion to extents.
2990 */
2991 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2992 if (ret) {
2993 mlog_errno(ret);
2994 goto out_commit;
2995 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002996 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002997
2998 /*
2999 * Operations are carefully ordered so that we set up the new
3000 * data block first. The conversion from inline data to
3001 * extents follows.
3002 */
3003 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3004 dirdata_bh = sb_getblk(sb, blkno);
3005 if (!dirdata_bh) {
3006 ret = -EIO;
3007 mlog_errno(ret);
3008 goto out_commit;
3009 }
3010
Joel Becker8cb471e2009-02-10 20:00:41 -08003011 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003012
Joel Becker13723d02008-10-17 19:25:01 -07003013 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
3014 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003015 if (ret) {
3016 mlog_errno(ret);
3017 goto out_commit;
3018 }
3019
3020 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
3021 memset(dirdata_bh->b_data + i_size_read(dir), 0,
3022 sb->s_blocksize - i_size_read(dir));
Mark Fashehe7c17e42009-01-29 18:17:46 -08003023 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
3024 if (ocfs2_new_dir_wants_trailer(dir)) {
3025 /*
3026 * Prepare the dir trailer up front. It will otherwise look
3027 * like a valid dirent. Even if inserting the index fails
3028 * (unlikely), then all we'll have done is given first dir
3029 * block a small amount of fragmentation.
3030 */
3031 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
3032 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003033
3034 ret = ocfs2_journal_dirty(handle, dirdata_bh);
3035 if (ret) {
3036 mlog_errno(ret);
3037 goto out_commit;
3038 }
3039
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003040 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
3041 /*
3042 * Dx dirs with an external cluster need to do this up
3043 * front. Inline dx root's get handled later, after
Mark Fashehe3a93c22009-02-17 15:29:35 -08003044 * we've allocated our root block. We get passed back
3045 * a total number of items so that dr_num_entries can
3046 * be correctly set once the dx_root has been
3047 * allocated.
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003048 */
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003049 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
Mark Fashehe3a93c22009-02-17 15:29:35 -08003050 num_dx_leaves, &num_dx_entries,
3051 dirdata_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003052 if (ret) {
3053 mlog_errno(ret);
3054 goto out_commit;
3055 }
3056 }
3057
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003058 /*
3059 * Set extent, i_size, etc on the directory. After this, the
3060 * inode should contain the same exact dirents as before and
3061 * be fully accessible from system calls.
3062 *
3063 * We let the later dirent insert modify c/mtime - to the user
3064 * the data hasn't changed.
3065 */
Joel Becker13723d02008-10-17 19:25:01 -07003066 ret = ocfs2_journal_access_di(handle, dir, di_bh,
3067 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003068 if (ret) {
3069 mlog_errno(ret);
3070 goto out_commit;
3071 }
3072
3073 spin_lock(&oi->ip_lock);
3074 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
3075 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
3076 spin_unlock(&oi->ip_lock);
3077
3078 ocfs2_dinode_new_extent_list(dir, di);
3079
3080 i_size_write(dir, sb->s_blocksize);
3081 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3082
3083 di->i_size = cpu_to_le64(sb->s_blocksize);
3084 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
3085 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003086
3087 /*
3088 * This should never fail as our extent list is empty and all
3089 * related blocks have been journaled already.
3090 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07003091 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
3092 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003093 if (ret) {
3094 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08003095 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003096 }
3097
Mark Fasheh9780eb62008-08-05 11:32:46 -07003098 /*
3099 * Set i_blocks after the extent insert for the most up to
3100 * date ip_clusters value.
3101 */
3102 dir->i_blocks = ocfs2_inode_sector_count(dir);
3103
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003104 ret = ocfs2_journal_dirty(handle, di_bh);
3105 if (ret) {
3106 mlog_errno(ret);
3107 goto out_commit;
3108 }
3109
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003110 if (ocfs2_supports_indexed_dirs(osb)) {
3111 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08003112 dirdata_bh, meta_ac, dx_inline,
Mark Fashehe3a93c22009-02-17 15:29:35 -08003113 num_dx_entries, &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003114 if (ret) {
3115 mlog_errno(ret);
3116 goto out_commit;
3117 }
3118
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003119 if (dx_inline) {
3120 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3121 dirdata_bh);
3122 } else {
3123 ocfs2_init_dx_root_extent_tree(&dx_et, dir, dx_root_bh);
3124 ret = ocfs2_insert_extent(osb, handle, dir, &dx_et, 0,
3125 dx_insert_blkno, 1, 0, NULL);
3126 if (ret)
3127 mlog_errno(ret);
3128 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003129 }
3130
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003131 /*
3132 * We asked for two clusters, but only got one in the 1st
3133 * pass. Claim the 2nd cluster as a separate extent.
3134 */
3135 if (alloc > len) {
3136 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
3137 &len);
3138 if (ret) {
3139 mlog_errno(ret);
3140 goto out_commit;
3141 }
3142 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3143
Joel Beckerf99b9b72008-08-20 19:36:33 -07003144 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
3145 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003146 if (ret) {
3147 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08003148 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003149 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003150 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003151 }
3152
3153 *first_block_bh = dirdata_bh;
3154 dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003155 if (ocfs2_supports_indexed_dirs(osb)) {
3156 unsigned int off;
3157
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003158 if (!dx_inline) {
3159 /*
3160 * We need to return the correct block within the
3161 * cluster which should hold our entry.
3162 */
3163 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
3164 &lookup->dl_hinfo);
3165 get_bh(dx_leaves[off]);
3166 lookup->dl_dx_leaf_bh = dx_leaves[off];
3167 }
3168 lookup->dl_dx_root_bh = dx_root_bh;
3169 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003170 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003171
3172out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02003173 if (ret < 0 && did_quota)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003174 vfs_dq_free_space_nodirty(dir, bytes_allocated);
3175
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003176 ocfs2_commit_trans(osb, handle);
3177
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003178out:
Jan Karaedd45c02009-06-02 14:24:03 +02003179 up_write(&oi->ip_alloc_sem);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003180 if (data_ac)
3181 ocfs2_free_alloc_context(data_ac);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003182 if (meta_ac)
3183 ocfs2_free_alloc_context(meta_ac);
3184
3185 if (dx_leaves) {
3186 for (i = 0; i < num_dx_leaves; i++)
3187 brelse(dx_leaves[i]);
3188 kfree(dx_leaves);
3189 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003190
3191 brelse(dirdata_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003192 brelse(dx_root_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003193
3194 return ret;
3195}
3196
Mark Fashehccd979b2005-12-15 14:31:24 -08003197/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07003198static int ocfs2_do_extend_dir(struct super_block *sb,
3199 handle_t *handle,
3200 struct inode *dir,
3201 struct buffer_head *parent_fe_bh,
3202 struct ocfs2_alloc_context *data_ac,
3203 struct ocfs2_alloc_context *meta_ac,
3204 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08003205{
3206 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02003207 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07003208 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08003209
3210 spin_lock(&OCFS2_I(dir)->ip_lock);
3211 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3212 spin_unlock(&OCFS2_I(dir)->ip_lock);
3213
3214 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08003215 u32 offset = OCFS2_I(dir)->ip_clusters;
3216
Jan Karaa90714c2008-10-09 19:38:40 +02003217 if (vfs_dq_alloc_space_nodirty(dir,
3218 ocfs2_clusters_to_bytes(sb, 1))) {
3219 status = -EDQUOT;
3220 goto bail;
3221 }
3222 did_quota = 1;
3223
Tao Ma0eb8d47e2008-08-18 17:38:45 +08003224 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3225 1, 0, parent_fe_bh, handle,
3226 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003227 BUG_ON(status == -EAGAIN);
3228 if (status < 0) {
3229 mlog_errno(status);
3230 goto bail;
3231 }
3232 }
3233
Mark Fasheh8110b072007-03-22 16:53:23 -07003234 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3235 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003236 if (status < 0) {
3237 mlog_errno(status);
3238 goto bail;
3239 }
3240
3241 *new_bh = sb_getblk(sb, p_blkno);
3242 if (!*new_bh) {
3243 status = -EIO;
3244 mlog_errno(status);
3245 goto bail;
3246 }
3247 status = 0;
3248bail:
Jan Karaa90714c2008-10-09 19:38:40 +02003249 if (did_quota && status < 0)
3250 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08003251 mlog_exit(status);
3252 return status;
3253}
3254
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003255/*
3256 * Assumes you already have a cluster lock on the directory.
3257 *
3258 * 'blocks_wanted' is only used if we have an inline directory which
3259 * is to be turned into an extent based one. The size of the dirent to
3260 * insert might be larger than the space gained by growing to just one
3261 * block, so we may have to grow the inode by two blocks in that case.
Mark Fashehe7c17e42009-01-29 18:17:46 -08003262 *
3263 * If the directory is already indexed, dx_root_bh must be provided.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003264 */
Mark Fashehccd979b2005-12-15 14:31:24 -08003265static int ocfs2_extend_dir(struct ocfs2_super *osb,
3266 struct inode *dir,
3267 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003268 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003269 struct ocfs2_dir_lookup_result *lookup,
Mark Fashehccd979b2005-12-15 14:31:24 -08003270 struct buffer_head **new_de_bh)
3271{
3272 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07003273 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003274 loff_t dir_i_size;
3275 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08003276 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08003277 struct ocfs2_alloc_context *data_ac = NULL;
3278 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07003279 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003280 struct buffer_head *new_bh = NULL;
3281 struct ocfs2_dir_entry * de;
3282 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07003283 struct ocfs2_extent_tree et;
Mark Fashehe7c17e42009-01-29 18:17:46 -08003284 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -08003285
3286 mlog_entry_void();
3287
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003288 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08003289 /*
3290 * This would be a code error as an inline directory should
3291 * never have an index root.
3292 */
3293 BUG_ON(dx_root_bh);
3294
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003295 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003296 blocks_wanted, lookup,
3297 &new_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003298 if (status) {
3299 mlog_errno(status);
3300 goto bail;
3301 }
3302
Mark Fashehe7c17e42009-01-29 18:17:46 -08003303 /* Expansion from inline to an indexed directory will
3304 * have given us this. */
3305 dx_root_bh = lookup->dl_dx_root_bh;
3306
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003307 if (blocks_wanted == 1) {
3308 /*
3309 * If the new dirent will fit inside the space
3310 * created by pushing out to one block, then
3311 * we can complete the operation
3312 * here. Otherwise we have to expand i_size
3313 * and format the 2nd block below.
3314 */
3315 BUG_ON(new_bh == NULL);
3316 goto bail_bh;
3317 }
3318
3319 /*
3320 * Get rid of 'new_bh' - we want to format the 2nd
3321 * data block and return that instead.
3322 */
3323 brelse(new_bh);
3324 new_bh = NULL;
3325
Jan Karaedd45c02009-06-02 14:24:03 +02003326 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3327 drop_alloc_sem = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003328 dir_i_size = i_size_read(dir);
3329 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3330 goto do_extend;
3331 }
3332
Jan Karaedd45c02009-06-02 14:24:03 +02003333 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3334 drop_alloc_sem = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003335 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -08003336 mlog(0, "extending dir %llu (i_size = %lld)\n",
3337 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08003338
Mark Fashehccd979b2005-12-15 14:31:24 -08003339 /* dir->i_size is always block aligned. */
3340 spin_lock(&OCFS2_I(dir)->ip_lock);
3341 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3342 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07003343 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003344 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08003345 if (num_free_extents < 0) {
3346 status = num_free_extents;
3347 mlog_errno(status);
3348 goto bail;
3349 }
3350
3351 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08003352 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003353 if (status < 0) {
3354 if (status != -ENOSPC)
3355 mlog_errno(status);
3356 goto bail;
3357 }
3358 }
3359
Mark Fashehda5cbf22006-10-06 18:34:35 -07003360 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003361 if (status < 0) {
3362 if (status != -ENOSPC)
3363 mlog_errno(status);
3364 goto bail;
3365 }
3366
Tao Ma811f9332008-08-18 17:38:43 +08003367 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08003368 } else {
3369 spin_unlock(&OCFS2_I(dir)->ip_lock);
3370 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3371 }
3372
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003373do_extend:
Mark Fashehe7c17e42009-01-29 18:17:46 -08003374 if (ocfs2_dir_indexed(dir))
3375 credits++; /* For attaching the new dirent block to the
3376 * dx_root */
3377
Mark Fasheh65eff9c2006-10-09 17:26:22 -07003378 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08003379 if (IS_ERR(handle)) {
3380 status = PTR_ERR(handle);
3381 handle = NULL;
3382 mlog_errno(status);
3383 goto bail;
3384 }
3385
3386 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3387 data_ac, meta_ac, &new_bh);
3388 if (status < 0) {
3389 mlog_errno(status);
3390 goto bail;
3391 }
3392
Joel Becker8cb471e2009-02-10 20:00:41 -08003393 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003394
Joel Becker13723d02008-10-17 19:25:01 -07003395 status = ocfs2_journal_access_db(handle, dir, new_bh,
3396 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08003397 if (status < 0) {
3398 mlog_errno(status);
3399 goto bail;
3400 }
3401 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08003402
Mark Fashehccd979b2005-12-15 14:31:24 -08003403 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3404 de->inode = 0;
Mark Fashehe7c17e42009-01-29 18:17:46 -08003405 if (ocfs2_supports_dir_trailer(dir)) {
Mark Fasheh87d35a72008-12-10 17:36:25 -08003406 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
Mark Fashehe7c17e42009-01-29 18:17:46 -08003407
3408 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3409
3410 if (ocfs2_dir_indexed(dir)) {
3411 status = ocfs2_dx_dir_link_trailer(dir, handle,
3412 dx_root_bh, new_bh);
3413 if (status) {
3414 mlog_errno(status);
3415 goto bail;
3416 }
3417 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003418 } else {
3419 de->rec_len = cpu_to_le16(sb->s_blocksize);
3420 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003421 status = ocfs2_journal_dirty(handle, new_bh);
3422 if (status < 0) {
3423 mlog_errno(status);
3424 goto bail;
3425 }
3426
3427 dir_i_size += dir->i_sb->s_blocksize;
3428 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07003429 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08003430 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3431 if (status < 0) {
3432 mlog_errno(status);
3433 goto bail;
3434 }
3435
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003436bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08003437 *new_de_bh = new_bh;
3438 get_bh(*new_de_bh);
3439bail:
3440 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07003441 ocfs2_commit_trans(osb, handle);
Jan Karaedd45c02009-06-02 14:24:03 +02003442 if (drop_alloc_sem)
3443 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08003444
3445 if (data_ac)
3446 ocfs2_free_alloc_context(data_ac);
3447 if (meta_ac)
3448 ocfs2_free_alloc_context(meta_ac);
3449
Mark Fasheha81cb882008-10-07 14:25:16 -07003450 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003451
3452 mlog_exit(status);
3453 return status;
3454}
3455
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003456static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3457 const char *name, int namelen,
3458 struct buffer_head **ret_de_bh,
3459 unsigned int *blocks_wanted)
3460{
3461 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003462 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003463 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3464 struct ocfs2_dir_entry *de, *last_de = NULL;
3465 char *de_buf, *limit;
3466 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003467 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3468
3469 /*
3470 * This calculates how many free bytes we'd have in block zero, should
3471 * this function force expansion to an extent tree.
3472 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08003473 if (ocfs2_new_dir_wants_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -08003474 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3475 else
3476 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003477
3478 de_buf = di->id2.i_data.id_data;
3479 limit = de_buf + i_size_read(dir);
3480 rec_len = OCFS2_DIR_REC_LEN(namelen);
3481
3482 while (de_buf < limit) {
3483 de = (struct ocfs2_dir_entry *)de_buf;
3484
3485 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3486 ret = -ENOENT;
3487 goto out;
3488 }
3489 if (ocfs2_match(namelen, name, de)) {
3490 ret = -EEXIST;
3491 goto out;
3492 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003493 /*
3494 * No need to check for a trailing dirent record here as
3495 * they're not used for inline dirs.
3496 */
3497
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003498 if (ocfs2_dirent_would_fit(de, rec_len)) {
3499 /* Ok, we found a spot. Return this bh and let
3500 * the caller actually fill it in. */
3501 *ret_de_bh = di_bh;
3502 get_bh(*ret_de_bh);
3503 ret = 0;
3504 goto out;
3505 }
3506
3507 last_de = de;
3508 de_buf += le16_to_cpu(de->rec_len);
3509 offset += le16_to_cpu(de->rec_len);
3510 }
3511
3512 /*
3513 * We're going to require expansion of the directory - figure
3514 * out how many blocks we'll need so that a place for the
3515 * dirent can be found.
3516 */
3517 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003518 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003519 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3520 *blocks_wanted = 2;
3521
3522 ret = -ENOSPC;
3523out:
3524 return ret;
3525}
3526
3527static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3528 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08003529{
3530 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003531 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003532 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08003533 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003534 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08003535 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003536 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08003537
Joel Beckera22305c2008-11-13 14:49:17 -08003538 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3539 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003540 mlog_errno(status);
3541 goto bail;
3542 }
3543
3544 rec_len = OCFS2_DIR_REC_LEN(namelen);
3545 offset = 0;
3546 de = (struct ocfs2_dir_entry *) bh->b_data;
3547 while (1) {
3548 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3549 brelse(bh);
3550 bh = NULL;
3551
3552 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003553 /*
3554 * Caller will have to expand this
3555 * directory.
3556 */
3557 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08003558 goto bail;
3559 }
Joel Beckera22305c2008-11-13 14:49:17 -08003560 status = ocfs2_read_dir_block(dir,
3561 offset >> sb->s_blocksize_bits,
3562 &bh, 0);
3563 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003564 mlog_errno(status);
3565 goto bail;
3566 }
3567 /* move to next block */
3568 de = (struct ocfs2_dir_entry *) bh->b_data;
3569 }
3570 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3571 status = -ENOENT;
3572 goto bail;
3573 }
3574 if (ocfs2_match(namelen, name, de)) {
3575 status = -EEXIST;
3576 goto bail;
3577 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003578
3579 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3580 blocksize))
3581 goto next;
3582
Mark Fasheh8553cf42007-09-13 16:29:01 -07003583 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003584 /* Ok, we found a spot. Return this bh and let
3585 * the caller actually fill it in. */
3586 *ret_de_bh = bh;
3587 get_bh(*ret_de_bh);
3588 status = 0;
3589 goto bail;
3590 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003591next:
Mark Fashehccd979b2005-12-15 14:31:24 -08003592 offset += le16_to_cpu(de->rec_len);
3593 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3594 }
3595
3596 status = 0;
3597bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07003598 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003599
3600 mlog_exit(status);
3601 return status;
3602}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003603
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003604static int dx_leaf_sort_cmp(const void *a, const void *b)
3605{
3606 const struct ocfs2_dx_entry *entry1 = a;
3607 const struct ocfs2_dx_entry *entry2 = b;
3608 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3609 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3610 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3611 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3612
3613 if (major_hash1 > major_hash2)
3614 return 1;
3615 if (major_hash1 < major_hash2)
3616 return -1;
3617
3618 /*
3619 * It is not strictly necessary to sort by minor
3620 */
3621 if (minor_hash1 > minor_hash2)
3622 return 1;
3623 if (minor_hash1 < minor_hash2)
3624 return -1;
3625 return 0;
3626}
3627
3628static void dx_leaf_sort_swap(void *a, void *b, int size)
3629{
3630 struct ocfs2_dx_entry *entry1 = a;
3631 struct ocfs2_dx_entry *entry2 = b;
3632 struct ocfs2_dx_entry tmp;
3633
3634 BUG_ON(size != sizeof(*entry1));
3635
3636 tmp = *entry1;
3637 *entry1 = *entry2;
3638 *entry2 = tmp;
3639}
3640
3641static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3642{
3643 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3644 int i, num = le16_to_cpu(dl_list->de_num_used);
3645
3646 for (i = 0; i < (num - 1); i++) {
3647 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3648 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3649 return 0;
3650 }
3651
3652 return 1;
3653}
3654
3655/*
3656 * Find the optimal value to split this leaf on. This expects the leaf
3657 * entries to be in sorted order.
3658 *
3659 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3660 * the hash we want to insert.
3661 *
3662 * This function is only concerned with the major hash - that which
3663 * determines which cluster an item belongs to.
3664 */
3665static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3666 u32 leaf_cpos, u32 insert_hash,
3667 u32 *split_hash)
3668{
3669 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3670 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3671 int allsame;
3672
3673 /*
3674 * There's a couple rare, but nasty corner cases we have to
3675 * check for here. All of them involve a leaf where all value
3676 * have the same hash, which is what we look for first.
3677 *
3678 * Most of the time, all of the above is false, and we simply
3679 * pick the median value for a split.
3680 */
3681 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3682 if (allsame) {
3683 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3684
3685 if (val == insert_hash) {
3686 /*
3687 * No matter where we would choose to split,
3688 * the new entry would want to occupy the same
3689 * block as these. Since there's no space left
3690 * in their existing block, we know there
3691 * won't be space after the split.
3692 */
3693 return -ENOSPC;
3694 }
3695
3696 if (val == leaf_cpos) {
3697 /*
3698 * Because val is the same as leaf_cpos (which
3699 * is the smallest value this leaf can have),
3700 * yet is not equal to insert_hash, then we
3701 * know that insert_hash *must* be larger than
3702 * val (and leaf_cpos). At least cpos+1 in value.
3703 *
3704 * We also know then, that there cannot be an
3705 * adjacent extent (otherwise we'd be looking
3706 * at it). Choosing this value gives us a
3707 * chance to get some contiguousness.
3708 */
3709 *split_hash = leaf_cpos + 1;
3710 return 0;
3711 }
3712
3713 if (val > insert_hash) {
3714 /*
3715 * val can not be the same as insert hash, and
3716 * also must be larger than leaf_cpos. Also,
3717 * we know that there can't be a leaf between
3718 * cpos and val, otherwise the entries with
3719 * hash 'val' would be there.
3720 */
3721 *split_hash = val;
3722 return 0;
3723 }
3724
3725 *split_hash = insert_hash;
3726 return 0;
3727 }
3728
3729 /*
3730 * Since the records are sorted and the checks above
3731 * guaranteed that not all records in this block are the same,
3732 * we simple travel forward, from the median, and pick the 1st
3733 * record whose value is larger than leaf_cpos.
3734 */
3735 for (i = (num_used / 2); i < num_used; i++)
3736 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3737 leaf_cpos)
3738 break;
3739
3740 BUG_ON(i == num_used); /* Should be impossible */
3741 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3742 return 0;
3743}
3744
3745/*
3746 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3747 * larger than split_hash into new_dx_leaves. We use a temporary
3748 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3749 *
3750 * Since the block offset inside a leaf (cluster) is a constant mask
3751 * of minor_hash, we can optimize - an item at block offset X within
3752 * the original cluster, will be at offset X within the new cluster.
3753 */
3754static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3755 handle_t *handle,
3756 struct ocfs2_dx_leaf *tmp_dx_leaf,
3757 struct buffer_head **orig_dx_leaves,
3758 struct buffer_head **new_dx_leaves,
3759 int num_dx_leaves)
3760{
3761 int i, j, num_used;
3762 u32 major_hash;
3763 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3764 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3765 struct ocfs2_dx_entry *dx_entry;
3766
3767 tmp_list = &tmp_dx_leaf->dl_list;
3768
3769 for (i = 0; i < num_dx_leaves; i++) {
3770 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3771 orig_list = &orig_dx_leaf->dl_list;
3772 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3773 new_list = &new_dx_leaf->dl_list;
3774
3775 num_used = le16_to_cpu(orig_list->de_num_used);
3776
3777 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3778 tmp_list->de_num_used = cpu_to_le16(0);
3779 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3780
3781 for (j = 0; j < num_used; j++) {
3782 dx_entry = &orig_list->de_entries[j];
3783 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3784 if (major_hash >= split_hash)
3785 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3786 dx_entry);
3787 else
3788 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3789 dx_entry);
3790 }
3791 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3792
3793 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3794 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3795 }
3796}
3797
3798static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3799 struct ocfs2_dx_root_block *dx_root)
3800{
3801 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3802
3803 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
3804 credits += ocfs2_quota_trans_credits(osb->sb);
3805 return credits;
3806}
3807
3808/*
3809 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3810 * half our entries into.
3811 */
3812static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3813 struct buffer_head *dx_root_bh,
3814 struct buffer_head *dx_leaf_bh,
3815 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3816 u64 leaf_blkno)
3817{
3818 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3819 int credits, ret, i, num_used, did_quota = 0;
3820 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3821 u64 orig_leaves_start;
3822 int num_dx_leaves;
3823 struct buffer_head **orig_dx_leaves = NULL;
3824 struct buffer_head **new_dx_leaves = NULL;
3825 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3826 struct ocfs2_extent_tree et;
3827 handle_t *handle = NULL;
3828 struct ocfs2_dx_root_block *dx_root;
3829 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3830
3831 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3832 (unsigned long long)OCFS2_I(dir)->ip_blkno,
3833 (unsigned long long)leaf_blkno, insert_hash);
3834
3835 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
3836
3837 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3838 /*
3839 * XXX: This is a rather large limit. We should use a more
3840 * realistic value.
3841 */
3842 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3843 return -ENOSPC;
3844
3845 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3846 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3847 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3848 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3849 (unsigned long long)leaf_blkno, num_used);
3850 ret = -EIO;
3851 goto out;
3852 }
3853
3854 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3855 if (!orig_dx_leaves) {
3856 ret = -ENOMEM;
3857 mlog_errno(ret);
3858 goto out;
3859 }
3860
3861 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3862 if (!new_dx_leaves) {
3863 ret = -ENOMEM;
3864 mlog_errno(ret);
3865 goto out;
3866 }
3867
3868 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3869 if (ret) {
3870 if (ret != -ENOSPC)
3871 mlog_errno(ret);
3872 goto out;
3873 }
3874
3875 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3876 handle = ocfs2_start_trans(osb, credits);
3877 if (IS_ERR(handle)) {
3878 ret = PTR_ERR(handle);
3879 handle = NULL;
3880 mlog_errno(ret);
3881 goto out;
3882 }
3883
3884 if (vfs_dq_alloc_space_nodirty(dir,
3885 ocfs2_clusters_to_bytes(dir->i_sb, 1))) {
3886 ret = -EDQUOT;
3887 goto out_commit;
3888 }
3889 did_quota = 1;
3890
3891 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
3892 OCFS2_JOURNAL_ACCESS_WRITE);
3893 if (ret) {
3894 mlog_errno(ret);
3895 goto out_commit;
3896 }
3897
3898 /*
3899 * This block is changing anyway, so we can sort it in place.
3900 */
3901 sort(dx_leaf->dl_list.de_entries, num_used,
3902 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3903 dx_leaf_sort_swap);
3904
3905 ret = ocfs2_journal_dirty(handle, dx_leaf_bh);
3906 if (ret) {
3907 mlog_errno(ret);
3908 goto out_commit;
3909 }
3910
3911 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3912 &split_hash);
3913 if (ret) {
3914 mlog_errno(ret);
3915 goto out_commit;
3916 }
3917
3918 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3919 leaf_cpos, split_hash, insert_hash);
3920
3921 /*
3922 * We have to carefully order operations here. There are items
3923 * which want to be in the new cluster before insert, but in
3924 * order to put those items in the new cluster, we alter the
3925 * old cluster. A failure to insert gets nasty.
3926 *
3927 * So, start by reserving writes to the old
3928 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3929 * the new cluster for us, before inserting it. The insert
3930 * won't happen if there's an error before that. Once the
3931 * insert is done then, we can transfer from one leaf into the
3932 * other without fear of hitting any error.
3933 */
3934
3935 /*
3936 * The leaf transfer wants some scratch space so that we don't
3937 * wind up doing a bunch of expensive memmove().
3938 */
3939 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3940 if (!tmp_dx_leaf) {
3941 ret = -ENOMEM;
3942 mlog_errno(ret);
3943 goto out_commit;
3944 }
3945
Mark Fasheh1d46dc02009-02-19 13:17:05 -08003946 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003947 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3948 orig_dx_leaves);
3949 if (ret) {
3950 mlog_errno(ret);
3951 goto out_commit;
3952 }
3953
3954 for (i = 0; i < num_dx_leaves; i++) {
3955 ret = ocfs2_journal_access_dl(handle, dir, orig_dx_leaves[i],
3956 OCFS2_JOURNAL_ACCESS_WRITE);
3957 if (ret) {
3958 mlog_errno(ret);
3959 goto out_commit;
3960 }
3961 }
3962
3963 cpos = split_hash;
3964 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3965 data_ac, meta_ac, new_dx_leaves,
3966 num_dx_leaves);
3967 if (ret) {
3968 mlog_errno(ret);
3969 goto out_commit;
3970 }
3971
3972 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3973 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3974
3975out_commit:
3976 if (ret < 0 && did_quota)
3977 vfs_dq_free_space_nodirty(dir,
3978 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3979
3980 ocfs2_commit_trans(osb, handle);
3981
3982out:
3983 if (orig_dx_leaves || new_dx_leaves) {
3984 for (i = 0; i < num_dx_leaves; i++) {
3985 if (orig_dx_leaves)
3986 brelse(orig_dx_leaves[i]);
3987 if (new_dx_leaves)
3988 brelse(new_dx_leaves[i]);
3989 }
3990 kfree(orig_dx_leaves);
3991 kfree(new_dx_leaves);
3992 }
3993
3994 if (meta_ac)
3995 ocfs2_free_alloc_context(meta_ac);
3996 if (data_ac)
3997 ocfs2_free_alloc_context(data_ac);
3998
3999 kfree(tmp_dx_leaf);
4000 return ret;
4001}
4002
Mark Fashehe7c17e42009-01-29 18:17:46 -08004003static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
4004 struct buffer_head *di_bh,
4005 struct buffer_head *dx_root_bh,
4006 const char *name, int namelen,
4007 struct ocfs2_dir_lookup_result *lookup)
4008{
4009 int ret, rebalanced = 0;
4010 struct ocfs2_dx_root_block *dx_root;
4011 struct buffer_head *dx_leaf_bh = NULL;
4012 struct ocfs2_dx_leaf *dx_leaf;
4013 u64 blkno;
4014 u32 leaf_cpos;
4015
4016 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4017
4018restart_search:
4019 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
4020 &leaf_cpos, &blkno);
4021 if (ret) {
4022 mlog_errno(ret);
4023 goto out;
4024 }
4025
4026 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
4027 if (ret) {
4028 mlog_errno(ret);
4029 goto out;
4030 }
4031
4032 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
4033
4034 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
4035 le16_to_cpu(dx_leaf->dl_list.de_count)) {
4036 if (rebalanced) {
4037 /*
4038 * Rebalancing should have provided us with
4039 * space in an appropriate leaf.
4040 *
4041 * XXX: Is this an abnormal condition then?
4042 * Should we print a message here?
4043 */
4044 ret = -ENOSPC;
4045 goto out;
4046 }
4047
4048 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
4049 &lookup->dl_hinfo, leaf_cpos,
4050 blkno);
4051 if (ret) {
4052 if (ret != -ENOSPC)
4053 mlog_errno(ret);
4054 goto out;
4055 }
4056
4057 /*
4058 * Restart the lookup. The rebalance might have
4059 * changed which block our item fits into. Mark our
4060 * progress, so we only execute this once.
4061 */
4062 brelse(dx_leaf_bh);
4063 dx_leaf_bh = NULL;
4064 rebalanced = 1;
4065 goto restart_search;
4066 }
4067
4068 lookup->dl_dx_leaf_bh = dx_leaf_bh;
4069 dx_leaf_bh = NULL;
4070
4071out:
4072 brelse(dx_leaf_bh);
4073 return ret;
4074}
4075
4076static int ocfs2_search_dx_free_list(struct inode *dir,
4077 struct buffer_head *dx_root_bh,
4078 int namelen,
4079 struct ocfs2_dir_lookup_result *lookup)
4080{
4081 int ret = -ENOSPC;
4082 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
4083 struct ocfs2_dir_block_trailer *db;
4084 u64 next_block;
4085 int rec_len = OCFS2_DIR_REC_LEN(namelen);
4086 struct ocfs2_dx_root_block *dx_root;
4087
4088 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4089 next_block = le64_to_cpu(dx_root->dr_free_blk);
4090
4091 while (next_block) {
4092 brelse(prev_leaf_bh);
4093 prev_leaf_bh = leaf_bh;
4094 leaf_bh = NULL;
4095
4096 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
4097 if (ret) {
4098 mlog_errno(ret);
4099 goto out;
4100 }
4101
4102 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
4103 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
4104 lookup->dl_leaf_bh = leaf_bh;
4105 lookup->dl_prev_leaf_bh = prev_leaf_bh;
4106 leaf_bh = NULL;
4107 prev_leaf_bh = NULL;
4108 break;
4109 }
4110
4111 next_block = le64_to_cpu(db->db_free_next);
4112 }
4113
4114 if (!next_block)
4115 ret = -ENOSPC;
4116
4117out:
4118
4119 brelse(leaf_bh);
4120 brelse(prev_leaf_bh);
4121 return ret;
4122}
4123
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004124static int ocfs2_expand_inline_dx_root(struct inode *dir,
4125 struct buffer_head *dx_root_bh)
4126{
4127 int ret, num_dx_leaves, i, j, did_quota = 0;
4128 struct buffer_head **dx_leaves = NULL;
4129 struct ocfs2_extent_tree et;
4130 u64 insert_blkno;
4131 struct ocfs2_alloc_context *data_ac = NULL;
4132 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4133 handle_t *handle = NULL;
4134 struct ocfs2_dx_root_block *dx_root;
4135 struct ocfs2_dx_entry_list *entry_list;
4136 struct ocfs2_dx_entry *dx_entry;
4137 struct ocfs2_dx_leaf *target_leaf;
4138
4139 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4140 if (ret) {
4141 mlog_errno(ret);
4142 goto out;
4143 }
4144
4145 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4146 if (!dx_leaves) {
4147 ret = -ENOMEM;
4148 mlog_errno(ret);
4149 goto out;
4150 }
4151
4152 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4153 if (IS_ERR(handle)) {
4154 ret = PTR_ERR(handle);
4155 mlog_errno(ret);
4156 goto out;
4157 }
4158
4159 if (vfs_dq_alloc_space_nodirty(dir,
4160 ocfs2_clusters_to_bytes(osb->sb, 1))) {
4161 ret = -EDQUOT;
4162 goto out_commit;
4163 }
4164 did_quota = 1;
4165
4166 /*
4167 * We do this up front, before the allocation, so that a
4168 * failure to add the dx_root_bh to the journal won't result
4169 * us losing clusters.
4170 */
4171 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
4172 OCFS2_JOURNAL_ACCESS_WRITE);
4173 if (ret) {
4174 mlog_errno(ret);
4175 goto out_commit;
4176 }
4177
4178 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4179 num_dx_leaves, &insert_blkno);
4180 if (ret) {
4181 mlog_errno(ret);
4182 goto out_commit;
4183 }
4184
4185 /*
4186 * Transfer the entries from our dx_root into the appropriate
4187 * block
4188 */
4189 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4190 entry_list = &dx_root->dr_entries;
4191
4192 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4193 dx_entry = &entry_list->de_entries[i];
4194
4195 j = __ocfs2_dx_dir_hash_idx(osb,
4196 le32_to_cpu(dx_entry->dx_minor_hash));
4197 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4198
4199 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4200
4201 /* Each leaf has been passed to the journal already
4202 * via __ocfs2_dx_dir_new_cluster() */
4203 }
4204
4205 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4206 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
4207 offsetof(struct ocfs2_dx_root_block, dr_list));
4208 dx_root->dr_list.l_count =
4209 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4210
4211 /* This should never fail considering we start with an empty
4212 * dx_root. */
4213 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
4214 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0,
4215 insert_blkno, 1, 0, NULL);
4216 if (ret)
4217 mlog_errno(ret);
4218 did_quota = 0;
4219
4220 ocfs2_journal_dirty(handle, dx_root_bh);
4221
4222out_commit:
4223 if (ret < 0 && did_quota)
4224 vfs_dq_free_space_nodirty(dir,
4225 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4226
4227 ocfs2_commit_trans(osb, handle);
4228
4229out:
4230 if (data_ac)
4231 ocfs2_free_alloc_context(data_ac);
4232
4233 if (dx_leaves) {
4234 for (i = 0; i < num_dx_leaves; i++)
4235 brelse(dx_leaves[i]);
4236 kfree(dx_leaves);
4237 }
4238 return ret;
4239}
4240
4241static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4242{
4243 struct ocfs2_dx_root_block *dx_root;
4244 struct ocfs2_dx_entry_list *entry_list;
4245
4246 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4247 entry_list = &dx_root->dr_entries;
4248
4249 if (le16_to_cpu(entry_list->de_num_used) >=
4250 le16_to_cpu(entry_list->de_count))
4251 return -ENOSPC;
4252
4253 return 0;
4254}
4255
Mark Fashehe7c17e42009-01-29 18:17:46 -08004256static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4257 struct buffer_head *di_bh,
4258 const char *name,
4259 int namelen,
4260 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004261{
Mark Fashehe7c17e42009-01-29 18:17:46 -08004262 int ret, free_dx_root = 1;
4263 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004264 struct buffer_head *dx_root_bh = NULL;
Mark Fashehe7c17e42009-01-29 18:17:46 -08004265 struct buffer_head *leaf_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fashehe7c17e42009-01-29 18:17:46 -08004267 struct ocfs2_dx_root_block *dx_root;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004268
4269 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4270 if (ret) {
4271 mlog_errno(ret);
4272 goto out;
4273 }
4274
4275 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
Mark Fashehe3a93c22009-02-17 15:29:35 -08004276 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4277 ret = -ENOSPC;
4278 mlog_errno(ret);
4279 goto out;
4280 }
4281
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004282 if (ocfs2_dx_root_inline(dx_root)) {
4283 ret = ocfs2_inline_dx_has_space(dx_root_bh);
4284
4285 if (ret == 0)
4286 goto search_el;
4287
4288 /*
4289 * We ran out of room in the root block. Expand it to
4290 * an extent, then allow ocfs2_find_dir_space_dx to do
4291 * the rest.
4292 */
4293 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4294 if (ret) {
4295 mlog_errno(ret);
4296 goto out;
4297 }
4298 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004299
Mark Fashehe7c17e42009-01-29 18:17:46 -08004300 /*
4301 * Insert preparation for an indexed directory is split into two
4302 * steps. The call to find_dir_space_dx reserves room in the index for
4303 * an additional item. If we run out of space there, it's a real error
4304 * we can't continue on.
4305 */
4306 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4307 namelen, lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004308 if (ret) {
4309 mlog_errno(ret);
4310 goto out;
4311 }
4312
Mark Fashehe7c17e42009-01-29 18:17:46 -08004313search_el:
4314 /*
4315 * Next, we need to find space in the unindexed tree. This call
4316 * searches using the free space linked list. If the unindexed tree
4317 * lacks sufficient space, we'll expand it below. The expansion code
4318 * is smart enough to add any new blocks to the free space list.
4319 */
4320 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4321 if (ret && ret != -ENOSPC) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004322 mlog_errno(ret);
4323 goto out;
4324 }
4325
Mark Fashehe7c17e42009-01-29 18:17:46 -08004326 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4327 lookup->dl_dx_root_bh = dx_root_bh;
4328 free_dx_root = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004329
Mark Fashehe7c17e42009-01-29 18:17:46 -08004330 if (ret == -ENOSPC) {
4331 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004332
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004333 if (ret) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08004334 mlog_errno(ret);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004335 goto out;
4336 }
4337
4338 /*
Mark Fashehe7c17e42009-01-29 18:17:46 -08004339 * We make the assumption here that new leaf blocks are added
4340 * to the front of our free list.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004341 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08004342 lookup->dl_prev_leaf_bh = NULL;
4343 lookup->dl_leaf_bh = leaf_bh;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004344 }
4345
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004346out:
Mark Fashehe7c17e42009-01-29 18:17:46 -08004347 if (free_dx_root)
4348 brelse(dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004349 return ret;
4350}
4351
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004352/*
4353 * Get a directory ready for insert. Any directory allocation required
4354 * happens here. Success returns zero, and enough context in the dir
4355 * lookup result that ocfs2_add_entry() will be able complete the task
4356 * with minimal performance impact.
4357 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004358int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4359 struct inode *dir,
4360 struct buffer_head *parent_fe_bh,
4361 const char *name,
4362 int namelen,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004363 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004364{
4365 int ret;
4366 unsigned int blocks_wanted = 1;
4367 struct buffer_head *bh = NULL;
4368
4369 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
4370 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
4371
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004372 if (!namelen) {
4373 ret = -EINVAL;
4374 mlog_errno(ret);
4375 goto out;
4376 }
4377
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004378 /*
4379 * Do this up front to reduce confusion.
4380 *
4381 * The directory might start inline, then be turned into an
4382 * indexed one, in which case we'd need to hash deep inside
4383 * ocfs2_find_dir_space_id(). Since
4384 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4385 * done, there seems no point in spreading out the calls. We
4386 * can optimize away the case where the file system doesn't
4387 * support indexing.
4388 */
4389 if (ocfs2_supports_indexed_dirs(osb))
4390 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4391
4392 if (ocfs2_dir_indexed(dir)) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08004393 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4394 name, namelen, lookup);
4395 if (ret)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004396 mlog_errno(ret);
Mark Fashehe7c17e42009-01-29 18:17:46 -08004397 goto out;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004398 }
4399
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004400 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4401 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4402 namelen, &bh, &blocks_wanted);
4403 } else
4404 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4405
4406 if (ret && ret != -ENOSPC) {
4407 mlog_errno(ret);
4408 goto out;
4409 }
4410
4411 if (ret == -ENOSPC) {
4412 /*
4413 * We have to expand the directory to add this name.
4414 */
4415 BUG_ON(bh);
4416
4417 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004418 lookup, &bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004419 if (ret) {
4420 if (ret != -ENOSPC)
4421 mlog_errno(ret);
4422 goto out;
4423 }
4424
4425 BUG_ON(!bh);
4426 }
4427
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004428 lookup->dl_leaf_bh = bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004429 bh = NULL;
4430out:
Mark Fasheha81cb882008-10-07 14:25:16 -07004431 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004432 return ret;
4433}
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004434
4435static int ocfs2_dx_dir_remove_index(struct inode *dir,
4436 struct buffer_head *di_bh,
4437 struct buffer_head *dx_root_bh)
4438{
4439 int ret;
4440 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4441 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4442 struct ocfs2_dx_root_block *dx_root;
4443 struct inode *dx_alloc_inode = NULL;
4444 struct buffer_head *dx_alloc_bh = NULL;
4445 handle_t *handle;
4446 u64 blk;
4447 u16 bit;
4448 u64 bg_blkno;
4449
4450 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4451
4452 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4453 EXTENT_ALLOC_SYSTEM_INODE,
4454 le16_to_cpu(dx_root->dr_suballoc_slot));
4455 if (!dx_alloc_inode) {
4456 ret = -ENOMEM;
4457 mlog_errno(ret);
4458 goto out;
4459 }
4460 mutex_lock(&dx_alloc_inode->i_mutex);
4461
4462 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4463 if (ret) {
4464 mlog_errno(ret);
4465 goto out_mutex;
4466 }
4467
4468 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4469 if (IS_ERR(handle)) {
4470 ret = PTR_ERR(handle);
4471 mlog_errno(ret);
4472 goto out_unlock;
4473 }
4474
4475 ret = ocfs2_journal_access_di(handle, dir, di_bh,
4476 OCFS2_JOURNAL_ACCESS_WRITE);
4477 if (ret) {
4478 mlog_errno(ret);
4479 goto out_commit;
4480 }
4481
4482 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4483 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4484 di->i_dx_root = cpu_to_le64(0ULL);
4485
4486 ocfs2_journal_dirty(handle, di_bh);
4487
4488 blk = le64_to_cpu(dx_root->dr_blkno);
4489 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4490 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4491 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4492 bit, bg_blkno, 1);
4493 if (ret)
4494 mlog_errno(ret);
4495
4496out_commit:
4497 ocfs2_commit_trans(osb, handle);
4498
4499out_unlock:
4500 ocfs2_inode_unlock(dx_alloc_inode, 1);
4501
4502out_mutex:
4503 mutex_unlock(&dx_alloc_inode->i_mutex);
4504 brelse(dx_alloc_bh);
4505out:
4506 iput(dx_alloc_inode);
4507 return ret;
4508}
4509
4510int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4511{
4512 int ret;
4513 unsigned int uninitialized_var(clen);
4514 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4515 u64 uninitialized_var(blkno);
4516 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4517 struct buffer_head *dx_root_bh = NULL;
4518 struct ocfs2_dx_root_block *dx_root;
4519 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4520 struct ocfs2_cached_dealloc_ctxt dealloc;
4521 struct ocfs2_extent_tree et;
4522
4523 ocfs2_init_dealloc_ctxt(&dealloc);
4524
4525 if (!ocfs2_dir_indexed(dir))
4526 return 0;
4527
4528 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4529 if (ret) {
4530 mlog_errno(ret);
4531 goto out;
4532 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004533 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4534
4535 if (ocfs2_dx_root_inline(dx_root))
4536 goto remove_index;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004537
4538 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
4539
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004540 /* XXX: What if dr_clusters is too large? */
4541 while (le32_to_cpu(dx_root->dr_clusters)) {
4542 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4543 major_hash, &cpos, &blkno, &clen);
4544 if (ret) {
4545 mlog_errno(ret);
4546 goto out;
4547 }
4548
4549 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4550
4551 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen,
4552 &dealloc);
4553 if (ret) {
4554 mlog_errno(ret);
4555 goto out;
4556 }
4557
4558 if (cpos == 0)
4559 break;
4560
4561 major_hash = cpos - 1;
4562 }
4563
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004564remove_index:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004565 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4566 if (ret) {
4567 mlog_errno(ret);
4568 goto out;
4569 }
4570
Joel Becker8cb471e2009-02-10 20:00:41 -08004571 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004572out:
4573 ocfs2_schedule_truncate_log_flush(osb, 1);
4574 ocfs2_run_deallocs(osb, &dealloc);
4575
4576 brelse(dx_root_bh);
4577 return ret;
4578}