ocfs2: move ip_created_trans to struct ocfs2_caching_info
[linux-2.6-block.git] / fs / ocfs2 / dir.c
CommitLineData
ccd979bd
MF
1/* -*- 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>
a90714c1 43#include <linux/quotaops.h>
9b7895ef 44#include <linux/sort.h>
ccd979bd
MF
45
46#define MLOG_MASK_PREFIX ML_NAMEI
47#include <cluster/masklog.h>
48
49#include "ocfs2.h"
50
51#include "alloc.h"
c175a518 52#include "blockcheck.h"
ccd979bd
MF
53#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"
316f4b9f 61#include "super.h"
9b7895ef 62#include "sysfile.h"
ccd979bd
MF
63#include "uptodate.h"
64
65#include "buffer_head_io.h"
66
316f4b9f
MF
67#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
ccd979bd
MF
72static unsigned char ocfs2_filetype_table[] = {
73 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
74};
75
316f4b9f
MF
76static 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);
e7c17e43 83static int ocfs2_dir_indexed(struct inode *inode);
316f4b9f 84
87d35a74
MF
85/*
86 * These are distinct checks because future versions of the file system will
87 * want to have a trailing dirent structure independent of indexing.
88 */
e7c17e43 89static int ocfs2_supports_dir_trailer(struct inode *dir)
87d35a74 90{
e7c17e43
MF
91 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
92
87d35a74
MF
93 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
95
e7c17e43 96 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
87d35a74
MF
97}
98
e7c17e43
MF
99/*
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)
87d35a74 108{
e7c17e43
MF
109 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
110
111 return ocfs2_meta_ecc(osb) ||
112 ocfs2_supports_indexed_dirs(osb);
87d35a74
MF
113}
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
c175a518
JB
122/* 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
87d35a74
MF
133/*
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
e7c17e43 144 if (!ocfs2_supports_dir_trailer(dir))
87d35a74
MF
145 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,
e7c17e43 154 struct buffer_head *bh, u16 rec_len)
87d35a74
MF
155{
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);
e7c17e43
MF
164 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;
87d35a74
MF
200}
201
4a12ca3a
MF
202void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
203{
4ed8a6bb 204 brelse(res->dl_dx_root_bh);
4a12ca3a 205 brelse(res->dl_leaf_bh);
9b7895ef 206 brelse(res->dl_dx_leaf_bh);
e7c17e43 207 brelse(res->dl_prev_leaf_bh);
9b7895ef
MF
208}
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
4ed8a6bb
MF
217static 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
9b7895ef
MF
222/*
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];
4a12ca3a
MF
311}
312
23193e51
MF
313/*
314 * bh passed here can be an inode block or a dir data block, depending
315 * on the inode inline data flag.
316 */
5eae5b96
MF
317static int ocfs2_check_dir_entry(struct inode * dir,
318 struct ocfs2_dir_entry * de,
319 struct buffer_head * bh,
320 unsigned long offset)
316f4b9f
MF
321{
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,
23193e51
MF
361 char *first_de,
362 unsigned int bytes,
316f4b9f
MF
363 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
23193e51
MF
372 de_buf = first_de;
373 dlimit = de_buf + bytes;
316f4b9f
MF
374
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
23193e51
MF
409static 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
b657c95c 419 ret = ocfs2_read_inode_block(dir, &di_bh);
23193e51
MF
420 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
a22305cc
JB
438static int ocfs2_validate_dir_block(struct super_block *sb,
439 struct buffer_head *bh)
440{
c175a518
JB
441 int rc;
442 struct ocfs2_dir_block_trailer *trailer =
443 ocfs2_trailer_from_bh(bh, sb);
444
445
a22305cc 446 /*
c175a518 447 * We don't validate dirents here, that's handled
a22305cc
JB
448 * in-place when the code walks them.
449 */
970e4936
JB
450 mlog(0, "Validating dirblock %llu\n",
451 (unsigned long long)bh->b_blocknr);
a22305cc 452
c175a518
JB
453 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;
a22305cc
JB
470}
471
9b7895ef
MF
472/*
473 * 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
a22305cc
JB
518/*
519 * 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;
a22305cc 529
511308d9
JB
530 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
531 ocfs2_validate_dir_block);
87d35a74 532 if (rc) {
a22305cc 533 mlog_errno(rc);
87d35a74
MF
534 goto out;
535 }
536
87d35a74 537 if (!(flags & OCFS2_BH_READAHEAD) &&
e7c17e43 538 ocfs2_supports_dir_trailer(inode)) {
9b7895ef
MF
539 rc = ocfs2_check_dir_trailer(inode, tmp);
540 if (rc) {
541 if (!*bh)
542 brelse(tmp);
543 mlog_errno(rc);
87d35a74
MF
544 goto out;
545 }
546 }
a22305cc 547
511308d9 548 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
87d35a74 549 if (!*bh)
a22305cc
JB
550 *bh = tmp;
551
87d35a74 552out:
a22305cc
JB
553 return rc ? -EIO : 0;
554}
555
9b7895ef
MF
556/*
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
8cb471e8
JB
567 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
568 ocfs2_validate_dir_block);
9b7895ef
MF
569 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
8cb471e8
JB
626 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
627 ocfs2_validate_dx_root);
9b7895ef
MF
628
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
8cb471e8
JB
667 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
668 ocfs2_validate_dx_leaf);
9b7895ef
MF
669
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
8cb471e8 686 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
9b7895ef
MF
687 ocfs2_validate_dx_leaf);
688 if (ret)
689 mlog_errno(ret);
690
691 return ret;
692}
693
0af4bd38
AB
694static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
695 struct inode *dir,
696 struct ocfs2_dir_entry **res_dir)
316f4b9f
MF
697{
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
316f4b9f
MF
711 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
a22305cc
JB
740 bh = NULL;
741 err = ocfs2_read_dir_block(dir, b++, &bh,
742 OCFS2_BH_READAHEAD);
316f4b9f
MF
743 bh_use[ra_max] = bh;
744 }
745 }
746 if ((bh = bh_use[ra_ptr++]) == NULL)
747 goto next;
a22305cc 748 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
5e0b3dec 749 /* read error, skip block & hope for the best.
a22305cc 750 * ocfs2_read_dir_block() has released the bh. */
316f4b9f
MF
751 ocfs2_error(dir->i_sb, "reading directory %llu, "
752 "offset %lu\n",
753 (unsigned long long)OCFS2_I(dir)->ip_blkno,
754 block);
316f4b9f
MF
755 goto next;
756 }
757 i = ocfs2_search_dirblock(bh, dir, name, namelen,
758 block << sb->s_blocksize_bits,
23193e51 759 bh->b_data, sb->s_blocksize,
316f4b9f
MF
760 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
9b7895ef
MF
795static 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)
23193e51 801{
9b7895ef
MF
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;
23193e51 806
9b7895ef
MF
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 }
23193e51 813
9b7895ef
MF
814 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
815 el = &eb->h_list;
4a12ca3a 816
9b7895ef
MF
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;
23193e51
MF
856}
857
5b6a3a2b 858/*
9b7895ef
MF
859 * Returns the block index, from the start of the cluster which this
860 * hash belongs too.
5b6a3a2b 861 */
4ed8a6bb
MF
862static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
863 u32 minor_hash)
38760e24 864{
9b7895ef
MF
865 return minor_hash & osb->osb_dx_mask;
866}
5b6a3a2b 867
4ed8a6bb
MF
868static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
869 struct ocfs2_dx_hinfo *hinfo)
870{
871 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
872}
873
9b7895ef
MF
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;
13723d00 885
9b7895ef
MF
886 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
887 &clen);
38760e24
MF
888 if (ret) {
889 mlog_errno(ret);
890 goto out;
891 }
892
9b7895ef
MF
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 }
38760e24 903
9b7895ef
MF
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;
38760e24
MF
915
916out:
9b7895ef 917
38760e24
MF
918 return ret;
919}
920
9b7895ef
MF
921static int ocfs2_dx_dir_search(const char *name, int namelen,
922 struct inode *dir,
4ed8a6bb 923 struct ocfs2_dx_root_block *dx_root,
9b7895ef 924 struct ocfs2_dir_lookup_result *res)
316f4b9f 925{
9b7895ef
MF
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;
4ed8a6bb
MF
934 struct ocfs2_extent_list *dr_el;
935 struct ocfs2_dx_entry_list *entry_list;
9b7895ef
MF
936
937 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
938
4ed8a6bb
MF
939 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
9b7895ef
MF
946 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
947 if (ret) {
948 mlog_errno(ret);
949 goto out;
950 }
316f4b9f 951
9b7895ef
MF
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);
316f4b9f 957
9b7895ef
MF
958 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
959 if (ret) {
960 mlog_errno(ret);
961 goto out;
962 }
13723d00 963
9b7895ef
MF
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
4ed8a6bb
MF
970 entry_list = &dx_leaf->dl_list;
971
972search:
9b7895ef
MF
973 /*
974 * Empty leaf is legal, so no need to check for that.
975 */
976 found = 0;
4ed8a6bb
MF
977 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
978 dx_entry = &entry_list->de_entries[i];
9b7895ef
MF
979
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;
316f4b9f 994 }
9b7895ef
MF
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
4ed8a6bb 1062 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
9b7895ef
MF
1063 if (ret) {
1064 if (ret != -ENOENT)
1065 mlog_errno(ret);
1066 goto out;
1067 }
1068
4ed8a6bb
MF
1069 lookup->dl_dx_root_bh = dx_root_bh;
1070 dx_root_bh = NULL;
9b7895ef
MF
1071out:
1072 brelse(di_bh);
1073 brelse(dx_root_bh);
1074 return ret;
1075}
1076
1077/*
1078 * Try to find an entry of the provided name within 'dir'.
1079 *
1080 * 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.
1083 *
1084 * Caller can NOT assume anything about the contents of the
1085 * 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.
1090 */
1091int ocfs2_find_entry(const char *name, int namelen,
1092 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1093{
1094 struct buffer_head *bh;
1095 struct ocfs2_dir_entry *res_dir = NULL;
1096
1097 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 */
1105 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1106 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1107 else
1108 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1109
1110 if (bh == NULL)
1111 return -ENOENT;
1112
1113 lookup->dl_leaf_bh = bh;
1114 lookup->dl_entry = res_dir;
1115 return 0;
1116}
1117
1118/*
1119 * Update inode number and type of a previously found directory entry.
1120 */
1121int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1122 struct ocfs2_dir_lookup_result *res,
1123 struct inode *new_entry_inode)
1124{
1125 int ret;
1126 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1127 struct ocfs2_dir_entry *de = res->dl_entry;
1128 struct buffer_head *de_bh = res->dl_leaf_bh;
1129
1130 /*
1131 * The same code works fine for both inline-data and extent
1132 * based directories, so no need to split this up. The only
1133 * difference is the journal_access function.
1134 */
1135
1136 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);
1140 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
1154/*
1155 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1156 * previous entry
1157 */
1158static 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)
1162{
1163 struct ocfs2_dir_entry *de, *pde;
1164 int i, status = -ENOENT;
1165 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1166
1167 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1168
1169 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1170 access = ocfs2_journal_access_di;
1171
1172 i = 0;
1173 pde = NULL;
1174 de = (struct ocfs2_dir_entry *) first_de;
1175 while (i < bytes) {
1176 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) {
1182 status = access(handle, dir, bh,
13723d00 1183 OCFS2_JOURNAL_ACCESS_WRITE);
316f4b9f
MF
1184 if (status < 0) {
1185 status = -EIO;
1186 mlog_errno(status);
1187 goto bail;
1188 }
1189 if (pde)
0dd3256e
MS
1190 le16_add_cpu(&pde->rec_len,
1191 le16_to_cpu(de->rec_len));
316f4b9f
MF
1192 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
e7c17e43
MF
1207static 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
4ed8a6bb
MF
1248static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1249 int index)
9b7895ef 1250{
4ed8a6bb 1251 int num_used = le16_to_cpu(entry_list->de_num_used);
9b7895ef
MF
1252
1253 if (num_used == 1 || index == (num_used - 1))
1254 goto clear;
1255
4ed8a6bb
MF
1256 memmove(&entry_list->de_entries[index],
1257 &entry_list->de_entries[index + 1],
9b7895ef
MF
1258 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1259clear:
1260 num_used--;
4ed8a6bb 1261 memset(&entry_list->de_entries[num_used], 0,
9b7895ef 1262 sizeof(struct ocfs2_dx_entry));
4ed8a6bb 1263 entry_list->de_num_used = cpu_to_le16(num_used);
9b7895ef
MF
1264}
1265
1266static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1267 struct ocfs2_dir_lookup_result *lookup)
1268{
e7c17e43 1269 int ret, index, max_rec_len, add_to_free_list = 0;
4ed8a6bb 1270 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895ef
MF
1271 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;
e7c17e43 1274 struct ocfs2_dir_block_trailer *trailer;
4ed8a6bb
MF
1275 struct ocfs2_dx_root_block *dx_root;
1276 struct ocfs2_dx_entry_list *entry_list;
1277
e7c17e43
MF
1278 /*
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 */
4ed8a6bb
MF
1289 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 }
9b7895ef 1296
9b7895ef
MF
1297 /* Neither of these are a disk corruption - that should have
1298 * been caught by lookup, before we got here. */
4ed8a6bb
MF
1299 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1300 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
9b7895ef 1301
4ed8a6bb 1302 index = (char *)dx_entry - (char *)entry_list->de_entries;
9b7895ef
MF
1303 index /= sizeof(*dx_entry);
1304
4ed8a6bb 1305 if (index >= le16_to_cpu(entry_list->de_num_used)) {
9b7895ef 1306 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
4ed8a6bb
MF
1307 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1308 entry_list, dx_entry);
9b7895ef
MF
1309 return -EIO;
1310 }
1311
e7c17e43
MF
1312 /*
1313 * 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
9b7895ef 1321 /*
4ed8a6bb
MF
1322 * 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 *
e3a93c2d
MF
1328 * 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.
9b7895ef 1331 */
e3a93c2d
MF
1332 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;
e7c17e43
MF
1337 }
1338
1339 if (!ocfs2_dx_root_inline(dx_root)) {
4ed8a6bb
MF
1340 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 }
9b7895ef
MF
1347 }
1348
4ed8a6bb
MF
1349 mlog(0, "Dir %llu: delete entry at index: %d\n",
1350 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1351
9b7895ef
MF
1352 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
e7c17e43
MF
1359 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
e3a93c2d
MF
1370 le32_add_cpu(&dx_root->dr_num_entries, -1);
1371 ocfs2_journal_dirty(handle, dx_root_bh);
1372
4ed8a6bb 1373 ocfs2_dx_list_remove_entry(entry_list, index);
9b7895ef 1374
e3a93c2d 1375 if (!ocfs2_dx_root_inline(dx_root))
4ed8a6bb 1376 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
9b7895ef
MF
1377
1378out:
1379 return ret;
1380}
1381
5b6a3a2b
MF
1382static 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
b657c95c 1392 ret = ocfs2_read_inode_block(dir, &di_bh);
5b6a3a2b
MF
1393 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/*
9b7895ef
MF
1419 * Delete a directory entry. Hide the details of directory
1420 * implementation from the caller.
5b6a3a2b
MF
1421 */
1422int ocfs2_delete_entry(handle_t *handle,
1423 struct inode *dir,
4a12ca3a 1424 struct ocfs2_dir_lookup_result *res)
5b6a3a2b 1425{
9b7895ef
MF
1426 if (ocfs2_dir_indexed(dir))
1427 return ocfs2_delete_entry_dx(handle, dir, res);
1428
5b6a3a2b 1429 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
4a12ca3a
MF
1430 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1431 res->dl_leaf_bh);
5b6a3a2b 1432
4a12ca3a
MF
1433 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1434 res->dl_leaf_bh);
5b6a3a2b
MF
1435}
1436
8553cf4f
MF
1437/*
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
9b7895ef
MF
1462static 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
4ed8a6bb
MF
1473static 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
9b7895ef
MF
1491static 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{
4ed8a6bb 1496 int ret;
9b7895ef
MF
1497 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;
4ed8a6bb
MF
1507 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1508 ocfs2_journal_dirty(handle, dx_leaf_bh);
9b7895ef 1509
4ed8a6bb
MF
1510out:
1511 return ret;
1512}
9b7895ef 1513
e3a93c2d
MF
1514static 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)
4ed8a6bb 1518{
e3a93c2d
MF
1519 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;
4ed8a6bb 1526 struct ocfs2_dx_root_block *dx_root;
e3a93c2d 1527 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895ef 1528
4ed8a6bb
MF
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
e3a93c2d
MF
1536 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);
4ed8a6bb 1551 ocfs2_journal_dirty(handle, dx_root_bh);
9b7895ef
MF
1552
1553out:
1554 return ret;
1555}
1556
e7c17e43
MF
1557static 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
316f4b9f
MF
1610/* we don't always have a dentry for what we want to add, so people
1611 * like orphan dir can call this instead.
1612 *
4a12ca3a
MF
1613 * The lookup context must have been filled from
1614 * ocfs2_prepare_dir_for_insert.
316f4b9f
MF
1615 */
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,
4a12ca3a 1621 struct ocfs2_dir_lookup_result *lookup)
316f4b9f
MF
1622{
1623 unsigned long offset;
1624 unsigned short rec_len;
1625 struct ocfs2_dir_entry *de, *de1;
5b6a3a2b
MF
1626 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1627 struct super_block *sb = dir->i_sb;
316f4b9f 1628 int retval, status;
5b6a3a2b 1629 unsigned int size = sb->s_blocksize;
4a12ca3a 1630 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
5b6a3a2b 1631 char *data_start = insert_bh->b_data;
316f4b9f
MF
1632
1633 mlog_entry_void();
1634
316f4b9f
MF
1635 if (!namelen)
1636 return -EINVAL;
1637
e7c17e43
MF
1638 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) {
5b6a3a2b
MF
1663 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
316f4b9f
MF
1669 rec_len = OCFS2_DIR_REC_LEN(namelen);
1670 offset = 0;
5b6a3a2b 1671 de = (struct ocfs2_dir_entry *) data_start;
316f4b9f 1672 while (1) {
5b6a3a2b
MF
1673 BUG_ON((char *)de >= (size + data_start));
1674
316f4b9f
MF
1675 /* 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 }
8553cf4f 1686
87d35a74
MF
1687 /* 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
8553cf4f 1697 if (ocfs2_dirent_would_fit(de, rec_len)) {
316f4b9f
MF
1698 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
13723d00
JB
1705 if (insert_bh == parent_fe_bh)
1706 status = ocfs2_journal_access_di(handle, dir,
1707 insert_bh,
1708 OCFS2_JOURNAL_ACCESS_WRITE);
9b7895ef 1709 else {
13723d00
JB
1710 status = ocfs2_journal_access_db(handle, dir,
1711 insert_bh,
4ed8a6bb
MF
1712 OCFS2_JOURNAL_ACCESS_WRITE);
1713
9b7895ef 1714 if (ocfs2_dir_indexed(dir)) {
4ed8a6bb
MF
1715 status = ocfs2_dx_dir_insert(dir,
1716 handle,
1717 lookup);
9b7895ef
MF
1718 if (status) {
1719 mlog_errno(status);
1720 goto bail;
1721 }
1722 }
1723 }
1724
316f4b9f
MF
1725 /* 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
e7c17e43
MF
1745 if (ocfs2_dir_indexed(dir))
1746 ocfs2_recalc_free_list(dir, handle, lookup);
1747
316f4b9f
MF
1748 dir->i_version++;
1749 status = ocfs2_journal_dirty(handle, insert_bh);
1750 retval = 0;
1751 goto bail;
1752 }
87d35a74 1753
316f4b9f
MF
1754 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
23193e51 1767static int ocfs2_dir_foreach_blk_id(struct inode *inode,
2b47c361 1768 u64 *f_version,
23193e51 1769 loff_t *f_pos, void *priv,
e7b34019 1770 filldir_t filldir, int *filldir_err)
23193e51
MF
1771{
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
b657c95c 1779 ret = ocfs2_read_inode_block(inode, &di_bh);
23193e51
MF
1780 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 */
2b47c361 1829 u64 version = *f_version;
23193e51
MF
1830 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);
e7b34019
MF
1840 if (filldir_ret) {
1841 if (filldir_err)
1842 *filldir_err = filldir_ret;
23193e51 1843 break;
e7b34019 1844 }
23193e51
MF
1845 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
9b7895ef
MF
1857/*
1858 * NOTE: This function can be called against unindexed directories,
1859 * and indexed ones.
1860 */
23193e51 1861static int ocfs2_dir_foreach_blk_el(struct inode *inode,
2b47c361 1862 u64 *f_version,
23193e51 1863 loff_t *f_pos, void *priv,
e7b34019 1864 filldir_t filldir, int *filldir_err)
ccd979bd
MF
1865{
1866 int error = 0;
aa958874
MF
1867 unsigned long offset, blk, last_ra_blk = 0;
1868 int i, stored;
ccd979bd
MF
1869 struct buffer_head * bh, * tmp;
1870 struct ocfs2_dir_entry * de;
ccd979bd 1871 struct super_block * sb = inode->i_sb;
aa958874 1872 unsigned int ra_sectors = 16;
ccd979bd
MF
1873
1874 stored = 0;
1875 bh = NULL;
1876
b8bc5f4f 1877 offset = (*f_pos) & (sb->s_blocksize - 1);
ccd979bd 1878
b8bc5f4f
MF
1879 while (!error && !stored && *f_pos < i_size_read(inode)) {
1880 blk = (*f_pos) >> sb->s_blocksize_bits;
a22305cc
JB
1881 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1882 /* Skip the corrupt dirblock and keep trying */
b8bc5f4f 1883 *f_pos += sb->s_blocksize - offset;
ccd979bd
MF
1884 continue;
1885 }
1886
aa958874
MF
1887 /* 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);
ccd979bd 1895 i > 0; i--) {
a22305cc
JB
1896 tmp = NULL;
1897 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1898 OCFS2_BH_READAHEAD))
1899 brelse(tmp);
ccd979bd 1900 }
aa958874
MF
1901 last_ra_blk = blk;
1902 ra_sectors = 8;
ccd979bd
MF
1903 }
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. */
b8bc5f4f 1910 if (*f_version != inode->i_version) {
ccd979bd
MF
1911 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;
b8bc5f4f 1925 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
ccd979bd 1926 | offset;
b8bc5f4f 1927 *f_version = inode->i_version;
ccd979bd
MF
1928 }
1929
b8bc5f4f 1930 while (!error && *f_pos < i_size_read(inode)
ccd979bd
MF
1931 && 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. */
b8bc5f4f 1936 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
ccd979bd 1937 brelse(bh);
b8bc5f4f 1938 goto out;
ccd979bd
MF
1939 }
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 */
b8bc5f4f 1949 unsigned long version = *f_version;
ccd979bd
MF
1950 unsigned char d_type = DT_UNKNOWN;
1951
1952 if (de->file_type < OCFS2_FT_MAX)
1953 d_type = ocfs2_filetype_table[de->file_type];
b8bc5f4f 1954 error = filldir(priv, de->name,
ccd979bd 1955 de->name_len,
b8bc5f4f 1956 *f_pos,
7e853679 1957 le64_to_cpu(de->inode),
ccd979bd 1958 d_type);
e7b34019
MF
1959 if (error) {
1960 if (filldir_err)
1961 *filldir_err = error;
ccd979bd 1962 break;
e7b34019 1963 }
b8bc5f4f 1964 if (version != *f_version)
ccd979bd
MF
1965 goto revalidate;
1966 stored ++;
1967 }
b8bc5f4f 1968 *f_pos += le16_to_cpu(de->rec_len);
ccd979bd
MF
1969 }
1970 offset = 0;
1971 brelse(bh);
a22305cc 1972 bh = NULL;
ccd979bd
MF
1973 }
1974
1975 stored = 0;
b8bc5f4f
MF
1976out:
1977 return stored;
1978}
1979
2b47c361 1980static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
e7b34019
MF
1981 loff_t *f_pos, void *priv, filldir_t filldir,
1982 int *filldir_err)
23193e51
MF
1983{
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,
e7b34019 1986 filldir, filldir_err);
23193e51 1987
e7b34019
MF
1988 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1989 filldir_err);
23193e51
MF
1990}
1991
5eae5b96
MF
1992/*
1993 * 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{
e7b34019 1999 int ret = 0, filldir_err = 0;
2b47c361 2000 u64 version = inode->i_version;
5eae5b96
MF
2001
2002 while (*f_pos < i_size_read(inode)) {
2003 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
e7b34019
MF
2004 filldir, &filldir_err);
2005 if (ret || filldir_err)
5eae5b96
MF
2006 break;
2007 }
2008
e7b34019
MF
2009 if (ret > 0)
2010 ret = -EIO;
2011
5eae5b96
MF
2012 return 0;
2013}
2014
b8bc5f4f
MF
2015/*
2016 * 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
e63aecb6 2028 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
b8bc5f4f
MF
2029 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. */
e63aecb6 2033 ocfs2_inode_unlock(inode, 1);
b8bc5f4f 2034 lock_level = 0;
e63aecb6 2035 error = ocfs2_inode_lock(inode, NULL, 0);
b8bc5f4f
MF
2036 }
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,
e7b34019 2045 dirent, filldir, NULL);
b8bc5f4f 2046
e63aecb6 2047 ocfs2_inode_unlock(inode, lock_level);
ccd979bd 2048
aa958874 2049bail_nolock:
b8bc5f4f 2050 mlog_exit(error);
ccd979bd 2051
b8bc5f4f 2052 return error;
ccd979bd
MF
2053}
2054
2055/*
1b1dcc1b 2056 * NOTE: this should always be called with parent dir i_mutex taken.
ccd979bd
MF
2057 */
2058int ocfs2_find_files_on_disk(const char *name,
2059 int namelen,
2060 u64 *blkno,
2061 struct inode *inode,
4a12ca3a 2062 struct ocfs2_dir_lookup_result *lookup)
ccd979bd
MF
2063{
2064 int status = -ENOENT;
ccd979bd 2065
4a12ca3a
MF
2066 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
2067 (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd 2068
4a12ca3a
MF
2069 status = ocfs2_find_entry(name, namelen, inode, lookup);
2070 if (status)
ccd979bd 2071 goto leave;
ccd979bd 2072
4a12ca3a 2073 *blkno = le64_to_cpu(lookup->dl_entry->inode);
ccd979bd
MF
2074
2075 status = 0;
2076leave:
ccd979bd 2077
ccd979bd
MF
2078 return status;
2079}
2080
be94d117
MF
2081/*
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;
4a12ca3a 2089 struct ocfs2_dir_lookup_result lookup = { NULL, };
be94d117 2090
4a12ca3a
MF
2091 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2092 ocfs2_free_dir_lookup_result(&lookup);
be94d117
MF
2093
2094 return ret;
2095}
2096
ccd979bd
MF
2097/* 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 *
1b1dcc1b 2102 * Callers should have i_mutex + a cluster lock on dir
ccd979bd
MF
2103 */
2104int ocfs2_check_dir_for_entry(struct inode *dir,
2105 const char *name,
2106 int namelen)
2107{
2108 int ret;
4a12ca3a 2109 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd 2110
b0697053
MF
2111 mlog_entry("dir %llu, name '%.*s'\n",
2112 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
ccd979bd
MF
2113
2114 ret = -EEXIST;
4a12ca3a 2115 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
ccd979bd
MF
2116 goto bail;
2117
2118 ret = 0;
2119bail:
4a12ca3a 2120 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd
MF
2121
2122 mlog_exit(ret);
2123 return ret;
2124}
2125
0bfbbf62
MF
2126struct ocfs2_empty_dir_priv {
2127 unsigned seen_dot;
2128 unsigned seen_dot_dot;
2129 unsigned seen_other;
e3a93c2d 2130 unsigned dx_dir;
0bfbbf62
MF
2131};
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.
e3a93c2d
MF
2140 *
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.
0bfbbf62
MF
2147 */
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;
e3a93c2d
MF
2156
2157 if (p->dx_dir && p->seen_dot)
2158 return 1;
2159
0bfbbf62
MF
2160 return 0;
2161 }
2162
2163 p->seen_other = 1;
2164 return 1;
2165}
e3a93c2d
MF
2166
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
ccd979bd
MF
2201/*
2202 * routine to check that the specified directory is empty (for rmdir)
0bfbbf62
MF
2203 *
2204 * Returns 1 if dir is empty, zero otherwise.
9b7895ef 2205 *
e3a93c2d 2206 * XXX: This is a performance problem for unindexed directories.
ccd979bd
MF
2207 */
2208int ocfs2_empty_dir(struct inode *inode)
2209{
0bfbbf62
MF
2210 int ret;
2211 loff_t start = 0;
2212 struct ocfs2_empty_dir_priv priv;
ccd979bd 2213
0bfbbf62 2214 memset(&priv, 0, sizeof(priv));
ccd979bd 2215
e3a93c2d
MF
2216 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
0bfbbf62
MF
2226 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",
b0697053 2232 (unsigned long long)OCFS2_I(inode)->ip_blkno);
0bfbbf62
MF
2233 /*
2234 * XXX: Is it really safe to allow an unlink to continue?
2235 */
ccd979bd
MF
2236 return 1;
2237 }
0bfbbf62
MF
2238
2239 return !priv.seen_other;
ccd979bd
MF
2240}
2241
87d35a74
MF
2242/*
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)
5b6a3a2b
MF
2251{
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);
87d35a74
MF
2267
2268 return de;
5b6a3a2b
MF
2269}
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
13723d00
JB
2286 ret = ocfs2_journal_access_di(handle, inode, di_bh,
2287 OCFS2_JOURNAL_ACCESS_WRITE);
5b6a3a2b
MF
2288 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,
9b7895ef
MF
2318 struct ocfs2_alloc_context *data_ac,
2319 struct buffer_head **ret_new_bh)
316f4b9f
MF
2320{
2321 int status;
87d35a74 2322 unsigned int size = osb->sb->s_blocksize;
316f4b9f 2323 struct buffer_head *new_bh = NULL;
87d35a74 2324 struct ocfs2_dir_entry *de;
316f4b9f
MF
2325
2326 mlog_entry_void();
2327
e7c17e43 2328 if (ocfs2_new_dir_wants_trailer(inode))
87d35a74
MF
2329 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2330
316f4b9f
MF
2331 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
8cb471e8 2338 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
316f4b9f 2339
13723d00
JB
2340 status = ocfs2_journal_access_db(handle, inode, new_bh,
2341 OCFS2_JOURNAL_ACCESS_CREATE);
316f4b9f
MF
2342 if (status < 0) {
2343 mlog_errno(status);
2344 goto bail;
2345 }
2346 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2347
87d35a74 2348 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
e7c17e43
MF
2349 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 }
316f4b9f
MF
2362
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;
9b7895ef
MF
2379 if (ret_new_bh) {
2380 *ret_new_bh = new_bh;
2381 new_bh = NULL;
2382 }
316f4b9f 2383bail:
a81cb88b 2384 brelse(new_bh);
316f4b9f
MF
2385
2386 mlog_exit(status);
2387 return status;
2388}
2389
9b7895ef
MF
2390static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2391 handle_t *handle, struct inode *dir,
2392 struct buffer_head *di_bh,
e7c17e43 2393 struct buffer_head *dirdata_bh,
9b7895ef 2394 struct ocfs2_alloc_context *meta_ac,
e3a93c2d 2395 int dx_inline, u32 num_entries,
9b7895ef 2396 struct buffer_head **ret_dx_root_bh)
5b6a3a2b 2397{
9b7895ef
MF
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;
e7c17e43
MF
2405 struct ocfs2_dir_block_trailer *trailer =
2406 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
9b7895ef
MF
2407
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 }
5b6a3a2b 2414
9b7895ef
MF
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);
5b6a3a2b 2418
9b7895ef
MF
2419 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2420 if (dx_root_bh == NULL) {
2421 ret = -EIO;
2422 goto out;
2423 }
8cb471e8 2424 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
87d35a74 2425
9b7895ef
MF
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 }
87d35a74 2432
9b7895ef
MF
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);
e3a93c2d 2441 dx_root->dr_num_entries = cpu_to_le32(num_entries);
e7c17e43
MF
2442 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);
4ed8a6bb
MF
2446
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 }
9b7895ef
MF
2455
2456 ret = ocfs2_journal_dirty(handle, dx_root_bh);
2457 if (ret)
2458 mlog_errno(ret);
5b6a3a2b 2459
9b7895ef
MF
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
8cb471e8 2501 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
9b7895ef
MF
2502
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{
4ed8a6bb 2627 int ret;
9b7895ef
MF
2628 struct buffer_head *leaf_bh = NULL;
2629 struct buffer_head *dx_root_bh = NULL;
9b7895ef 2630 struct ocfs2_dx_hinfo hinfo;
4ed8a6bb
MF
2631 struct ocfs2_dx_root_block *dx_root;
2632 struct ocfs2_dx_entry_list *entry_list;
9b7895ef
MF
2633
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
4ed8a6bb
MF
2641 * block which need indexing. These are easily inserted into
2642 * the index block.
9b7895ef
MF
2643 */
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
e7c17e43 2652 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
e3a93c2d 2653 meta_ac, 1, 2, &dx_root_bh);
9b7895ef
MF
2654 if (ret) {
2655 mlog_errno(ret);
2656 goto out;
2657 }
4ed8a6bb
MF
2658 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2659 entry_list = &dx_root->dr_entries;
9b7895ef 2660
4ed8a6bb 2661 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
e7c17e43 2662 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
4ed8a6bb 2663 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895ef
MF
2664
2665 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
4ed8a6bb 2666 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895ef
MF
2667
2668out:
9b7895ef
MF
2669 brelse(dx_root_bh);
2670 brelse(leaf_bh);
2671 return ret;
2672}
2673
2674int 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,
2679 struct ocfs2_alloc_context *data_ac,
2680 struct ocfs2_alloc_context *meta_ac)
2681
2682{
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
2688 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
2692 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2693 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,
e3a93c2d 2700 u32 *num_dx_entries,
9b7895ef
MF
2701 struct buffer_head *dirent_bh)
2702{
0fba8137 2703 int ret = 0, namelen, i;
9b7895ef
MF
2704 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
e3a93c2d
MF
2732 *num_dx_entries = *num_dx_entries + 1;
2733
9b7895ef
MF
2734inc:
2735 de_buf += le16_to_cpu(de->rec_len);
2736 }
2737
2738out:
2739 return ret;
2740}
e7c17e43
MF
2741
2742/*
4ed8a6bb
MF
2743 * 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);
e3a93c2d
MF
2777
2778 le32_add_cpu(&dx_root->dr_num_entries, 1);
4ed8a6bb
MF
2779inc:
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}
9b7895ef
MF
2812
2813/*
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 *
e7c17e43
MF
2819 * 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 *
9b7895ef
MF
2825 * We add the dir trailer if this filesystem wants it.
2826 */
e7c17e43
MF
2827static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2828 struct inode *dir)
9b7895ef 2829{
e7c17e43 2830 struct super_block *sb = dir->i_sb;
9b7895ef
MF
2831 struct ocfs2_dir_entry *de;
2832 struct ocfs2_dir_entry *prev_de;
2833 char *de_buf, *limit;
2834 unsigned int new_size = sb->s_blocksize;
e7c17e43
MF
2835 unsigned int bytes, this_hole;
2836 unsigned int largest_hole = 0;
9b7895ef 2837
e7c17e43 2838 if (ocfs2_new_dir_wants_trailer(dir))
9b7895ef
MF
2839 new_size = ocfs2_dir_trailer_blk_off(sb);
2840
2841 bytes = new_size - old_size;
2842
2843 limit = start + old_size;
2844 de_buf = start;
2845 de = (struct ocfs2_dir_entry *)de_buf;
5b6a3a2b 2846 do {
e7c17e43
MF
2847 this_hole = ocfs2_figure_dirent_hole(de);
2848 if (this_hole > largest_hole)
2849 largest_hole = this_hole;
2850
5b6a3a2b
MF
2851 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);
e7c17e43
MF
2857
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;
5b6a3a2b
MF
2867}
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,
9b7895ef 2879 struct ocfs2_dir_lookup_result *lookup,
5b6a3a2b
MF
2880 struct buffer_head **first_block_bh)
2881{
e3a93c2d 2882 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
5b6a3a2b 2883 struct super_block *sb = dir->i_sb;
4ed8a6bb 2884 int ret, i, num_dx_leaves = 0, dx_inline = 0,
9b7895ef
MF
2885 credits = ocfs2_inline_to_extents_credits(sb);
2886 u64 dx_insert_blkno, blkno,
2887 bytes = blocks_wanted << sb->s_blocksize_bits;
5b6a3a2b
MF
2888 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;
9b7895ef 2891 struct ocfs2_alloc_context *meta_ac = NULL;
5b6a3a2b 2892 struct buffer_head *dirdata_bh = NULL;
9b7895ef
MF
2893 struct buffer_head *dx_root_bh = NULL;
2894 struct buffer_head **dx_leaves = NULL;
5b6a3a2b
MF
2895 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2896 handle_t *handle;
f99b9b7c 2897 struct ocfs2_extent_tree et;
9b7895ef
MF
2898 struct ocfs2_extent_tree dx_et;
2899 int did_quota = 0, bytes_allocated = 0;
f99b9b7c 2900
8d6220d6 2901 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
5b6a3a2b
MF
2902
2903 alloc = ocfs2_clusters_for_bytes(sb, bytes);
9b7895ef
MF
2904 dx_alloc = 0;
2905
edd45c08
JK
2906 down_write(&oi->ip_alloc_sem);
2907
9b7895ef 2908 if (ocfs2_supports_indexed_dirs(osb)) {
9b7895ef
MF
2909 credits += ocfs2_add_dir_index_credits(sb);
2910
4ed8a6bb
MF
2911 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 }
9b7895ef
MF
2922 }
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 }
5b6a3a2b
MF
2931
2932 /*
9b7895ef
MF
2933 * 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
5b6a3a2b
MF
2936 * 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
035a5711 2942 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
5b6a3a2b
MF
2943 if (ret) {
2944 mlog_errno(ret);
2945 goto out;
2946 }
2947
5b6a3a2b 2948 /*
c78bad11 2949 * Prepare for worst case allocation scenario of two separate
9b7895ef 2950 * extents in the unindexed tree.
5b6a3a2b
MF
2951 */
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);
edd45c08 2959 goto out;
5b6a3a2b
MF
2960 }
2961
a90714c1 2962 if (vfs_dq_alloc_space_nodirty(dir,
9b7895ef
MF
2963 ocfs2_clusters_to_bytes(osb->sb,
2964 alloc + dx_alloc))) {
a90714c1
JK
2965 ret = -EDQUOT;
2966 goto out_commit;
2967 }
2968 did_quota = 1;
9b7895ef 2969
4ed8a6bb 2970 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
9b7895ef
MF
2971 /*
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
5b6a3a2b
MF
2986 /*
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 }
9b7895ef 2996 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b
MF
2997
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
8cb471e8 3011 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
5b6a3a2b 3012
13723d00
JB
3013 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
3014 OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b
MF
3015 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));
e7c17e43
MF
3023 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 }
5b6a3a2b
MF
3033
3034 ret = ocfs2_journal_dirty(handle, dirdata_bh);
3035 if (ret) {
3036 mlog_errno(ret);
3037 goto out_commit;
3038 }
3039
4ed8a6bb
MF
3040 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
e3a93c2d
MF
3044 * 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.
4ed8a6bb 3048 */
9b7895ef 3049 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
e3a93c2d
MF
3050 num_dx_leaves, &num_dx_entries,
3051 dirdata_bh);
9b7895ef
MF
3052 if (ret) {
3053 mlog_errno(ret);
3054 goto out_commit;
3055 }
3056 }
3057
5b6a3a2b
MF
3058 /*
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 */
13723d00
JB
3066 ret = ocfs2_journal_access_di(handle, dir, di_bh,
3067 OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b
MF
3068 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);
5b6a3a2b
MF
3086
3087 /*
3088 * This should never fail as our extent list is empty and all
3089 * related blocks have been journaled already.
3090 */
f99b9b7c
JB
3091 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
3092 0, NULL);
5b6a3a2b
MF
3093 if (ret) {
3094 mlog_errno(ret);
83cab533 3095 goto out_commit;
5b6a3a2b
MF
3096 }
3097
9780eb6c
MF
3098 /*
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
5b6a3a2b
MF
3104 ret = ocfs2_journal_dirty(handle, di_bh);
3105 if (ret) {
3106 mlog_errno(ret);
3107 goto out_commit;
3108 }
3109
9b7895ef
MF
3110 if (ocfs2_supports_indexed_dirs(osb)) {
3111 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
e7c17e43 3112 dirdata_bh, meta_ac, dx_inline,
e3a93c2d 3113 num_dx_entries, &dx_root_bh);
9b7895ef
MF
3114 if (ret) {
3115 mlog_errno(ret);
3116 goto out_commit;
3117 }
3118
4ed8a6bb
MF
3119 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 }
9b7895ef
MF
3129 }
3130
5b6a3a2b
MF
3131 /*
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
f99b9b7c
JB
3144 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
3145 blkno, len, 0, NULL);
5b6a3a2b
MF
3146 if (ret) {
3147 mlog_errno(ret);
83cab533 3148 goto out_commit;
5b6a3a2b 3149 }
9b7895ef 3150 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b
MF
3151 }
3152
3153 *first_block_bh = dirdata_bh;
3154 dirdata_bh = NULL;
9b7895ef
MF
3155 if (ocfs2_supports_indexed_dirs(osb)) {
3156 unsigned int off;
3157
4ed8a6bb
MF
3158 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;
9b7895ef 3170 }
5b6a3a2b
MF
3171
3172out_commit:
a90714c1 3173 if (ret < 0 && did_quota)
9b7895ef
MF
3174 vfs_dq_free_space_nodirty(dir, bytes_allocated);
3175
5b6a3a2b
MF
3176 ocfs2_commit_trans(osb, handle);
3177
5b6a3a2b 3178out:
edd45c08 3179 up_write(&oi->ip_alloc_sem);
5b6a3a2b
MF
3180 if (data_ac)
3181 ocfs2_free_alloc_context(data_ac);
9b7895ef
MF
3182 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 }
5b6a3a2b
MF
3190
3191 brelse(dirdata_bh);
9b7895ef 3192 brelse(dx_root_bh);
5b6a3a2b
MF
3193
3194 return ret;
3195}
3196
ccd979bd 3197/* returns a bh of the 1st new block in the allocation. */
316f4b9f
MF
3198static 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)
ccd979bd
MF
3205{
3206 int status;
a90714c1 3207 int extend, did_quota = 0;
8110b073 3208 u64 p_blkno, v_blkno;
ccd979bd
MF
3209
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) {
dcd0538f
MF
3215 u32 offset = OCFS2_I(dir)->ip_clusters;
3216
a90714c1
JK
3217 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
0eb8d47e
TM
3224 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3225 1, 0, parent_fe_bh, handle,
3226 data_ac, meta_ac, NULL);
ccd979bd
MF
3227 BUG_ON(status == -EAGAIN);
3228 if (status < 0) {
3229 mlog_errno(status);
3230 goto bail;
3231 }
3232 }
3233
8110b073
MF
3234 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);
ccd979bd
MF
3236 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:
a90714c1
JK
3249 if (did_quota && status < 0)
3250 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
ccd979bd
MF
3251 mlog_exit(status);
3252 return status;
3253}
3254
5b6a3a2b
MF
3255/*
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.
e7c17e43
MF
3262 *
3263 * If the directory is already indexed, dx_root_bh must be provided.
5b6a3a2b 3264 */
ccd979bd
MF
3265static int ocfs2_extend_dir(struct ocfs2_super *osb,
3266 struct inode *dir,
3267 struct buffer_head *parent_fe_bh,
5b6a3a2b 3268 unsigned int blocks_wanted,
9b7895ef 3269 struct ocfs2_dir_lookup_result *lookup,
ccd979bd
MF
3270 struct buffer_head **new_de_bh)
3271{
3272 int status = 0;
ee19a779 3273 int credits, num_free_extents, drop_alloc_sem = 0;
ccd979bd
MF
3274 loff_t dir_i_size;
3275 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
811f933d 3276 struct ocfs2_extent_list *el = &fe->id2.i_list;
ccd979bd
MF
3277 struct ocfs2_alloc_context *data_ac = NULL;
3278 struct ocfs2_alloc_context *meta_ac = NULL;
1fabe148 3279 handle_t *handle = NULL;
ccd979bd
MF
3280 struct buffer_head *new_bh = NULL;
3281 struct ocfs2_dir_entry * de;
3282 struct super_block *sb = osb->sb;
f99b9b7c 3283 struct ocfs2_extent_tree et;
e7c17e43 3284 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
ccd979bd
MF
3285
3286 mlog_entry_void();
3287
5b6a3a2b 3288 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
e7c17e43
MF
3289 /*
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
5b6a3a2b 3295 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
9b7895ef
MF
3296 blocks_wanted, lookup,
3297 &new_bh);
5b6a3a2b
MF
3298 if (status) {
3299 mlog_errno(status);
3300 goto bail;
3301 }
3302
e7c17e43
MF
3303 /* Expansion from inline to an indexed directory will
3304 * have given us this. */
3305 dx_root_bh = lookup->dl_dx_root_bh;
3306
5b6a3a2b
MF
3307 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
edd45c08
JK
3326 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3327 drop_alloc_sem = 1;
5b6a3a2b
MF
3328 dir_i_size = i_size_read(dir);
3329 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3330 goto do_extend;
3331 }
3332
edd45c08
JK
3333 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3334 drop_alloc_sem = 1;
ccd979bd 3335 dir_i_size = i_size_read(dir);
b0697053
MF
3336 mlog(0, "extending dir %llu (i_size = %lld)\n",
3337 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
ccd979bd 3338
ccd979bd
MF
3339 /* 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);
8d6220d6 3343 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
f99b9b7c 3344 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
ccd979bd
MF
3345 if (num_free_extents < 0) {
3346 status = num_free_extents;
3347 mlog_errno(status);
3348 goto bail;
3349 }
3350
3351 if (!num_free_extents) {
811f933d 3352 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
ccd979bd
MF
3353 if (status < 0) {
3354 if (status != -ENOSPC)
3355 mlog_errno(status);
3356 goto bail;
3357 }
3358 }
3359
da5cbf2f 3360 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
ccd979bd
MF
3361 if (status < 0) {
3362 if (status != -ENOSPC)
3363 mlog_errno(status);
3364 goto bail;
3365 }
3366
811f933d 3367 credits = ocfs2_calc_extend_credits(sb, el, 1);
ccd979bd
MF
3368 } else {
3369 spin_unlock(&OCFS2_I(dir)->ip_lock);
3370 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3371 }
3372
5b6a3a2b 3373do_extend:
e7c17e43
MF
3374 if (ocfs2_dir_indexed(dir))
3375 credits++; /* For attaching the new dirent block to the
3376 * dx_root */
3377
65eff9cc 3378 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
3379 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
8cb471e8 3393 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
ccd979bd 3394
13723d00
JB
3395 status = ocfs2_journal_access_db(handle, dir, new_bh,
3396 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bd
MF
3397 if (status < 0) {
3398 mlog_errno(status);
3399 goto bail;
3400 }
3401 memset(new_bh->b_data, 0, sb->s_blocksize);
87d35a74 3402
ccd979bd
MF
3403 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3404 de->inode = 0;
e7c17e43 3405 if (ocfs2_supports_dir_trailer(dir)) {
87d35a74 3406 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
e7c17e43
MF
3407
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 }
87d35a74
MF
3418 } else {
3419 de->rec_len = cpu_to_le16(sb->s_blocksize);
3420 }
ccd979bd
MF
3421 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);
8110b073 3429 dir->i_blocks = ocfs2_inode_sector_count(dir);
ccd979bd
MF
3430 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3431 if (status < 0) {
3432 mlog_errno(status);
3433 goto bail;
3434 }
3435
5b6a3a2b 3436bail_bh:
ccd979bd
MF
3437 *new_de_bh = new_bh;
3438 get_bh(*new_de_bh);
3439bail:
3440 if (handle)
02dc1af4 3441 ocfs2_commit_trans(osb, handle);
edd45c08
JK
3442 if (drop_alloc_sem)
3443 up_write(&OCFS2_I(dir)->ip_alloc_sem);
ccd979bd
MF
3444
3445 if (data_ac)
3446 ocfs2_free_alloc_context(data_ac);
3447 if (meta_ac)
3448 ocfs2_free_alloc_context(meta_ac);
3449
a81cb88b 3450 brelse(new_bh);
ccd979bd
MF
3451
3452 mlog_exit(status);
3453 return status;
3454}
3455
5b6a3a2b
MF
3456static 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)
ccd979bd 3460{
5b6a3a2b 3461 int ret;
87d35a74 3462 struct super_block *sb = dir->i_sb;
5b6a3a2b
MF
3463 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;
87d35a74
MF
3467 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 */
e7c17e43 3473 if (ocfs2_new_dir_wants_trailer(dir))
87d35a74
MF
3474 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);
5b6a3a2b
MF
3477
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);
ccd979bd 3481
5b6a3a2b
MF
3482 while (de_buf < limit) {
3483 de = (struct ocfs2_dir_entry *)de_buf;
ccd979bd 3484
5b6a3a2b
MF
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 }
87d35a74
MF
3493 /*
3494 * No need to check for a trailing dirent record here as
3495 * they're not used for inline dirs.
3496 */
3497
5b6a3a2b
MF
3498 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 }
ccd979bd 3506
5b6a3a2b
MF
3507 last_de = de;
3508 de_buf += le16_to_cpu(de->rec_len);
3509 offset += le16_to_cpu(de->rec_len);
3510 }
ccd979bd 3511
5b6a3a2b
MF
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;
87d35a74 3518 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
5b6a3a2b
MF
3519 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3520 *blocks_wanted = 2;
ccd979bd 3521
5b6a3a2b
MF
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)
3529{
3530 unsigned long offset;
3531 struct buffer_head *bh = NULL;
3532 unsigned short rec_len;
3533 struct ocfs2_dir_entry *de;
3534 struct super_block *sb = dir->i_sb;
3535 int status;
87d35a74 3536 int blocksize = dir->i_sb->s_blocksize;
ccd979bd 3537
a22305cc
JB
3538 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3539 if (status) {
ccd979bd
MF
3540 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) {
5b6a3a2b
MF
3553 /*
3554 * Caller will have to expand this
3555 * directory.
3556 */
3557 status = -ENOSPC;
ccd979bd
MF
3558 goto bail;
3559 }
a22305cc
JB
3560 status = ocfs2_read_dir_block(dir,
3561 offset >> sb->s_blocksize_bits,
3562 &bh, 0);
3563 if (status) {
ccd979bd
MF
3564 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 }
87d35a74
MF
3578
3579 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3580 blocksize))
3581 goto next;
3582
8553cf4f 3583 if (ocfs2_dirent_would_fit(de, rec_len)) {
ccd979bd
MF
3584 /* 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 }
87d35a74 3591next:
ccd979bd
MF
3592 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:
a81cb88b 3598 brelse(bh);
ccd979bd
MF
3599
3600 mlog_exit(status);
3601 return status;
3602}
5b6a3a2b 3603
9b7895ef 3604static int dx_leaf_sort_cmp(const void *a, const void *b)
5b6a3a2b 3605{
9b7895ef
MF
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
1d46dc08 3946 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
9b7895ef
MF
3947 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
e7c17e43
MF
4003static 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
4ed8a6bb
MF
4124static 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
e7c17e43
MF
4256static 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)
9b7895ef 4261{
e7c17e43
MF
4262 int ret, free_dx_root = 1;
4263 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
9b7895ef 4264 struct buffer_head *dx_root_bh = NULL;
e7c17e43 4265 struct buffer_head *leaf_bh = NULL;
9b7895ef 4266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
e7c17e43 4267 struct ocfs2_dx_root_block *dx_root;
9b7895ef
MF
4268
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;
e3a93c2d
MF
4276 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
4ed8a6bb
MF
4282 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 }
9b7895ef 4299
e7c17e43
MF
4300 /*
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);
9b7895ef
MF
4308 if (ret) {
4309 mlog_errno(ret);
4310 goto out;
4311 }
4312
e7c17e43
MF
4313search_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) {
9b7895ef
MF
4322 mlog_errno(ret);
4323 goto out;
4324 }
4325
e7c17e43
MF
4326 /* 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;
9b7895ef 4329
e7c17e43
MF
4330 if (ret == -ENOSPC) {
4331 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
9b7895ef 4332
9b7895ef 4333 if (ret) {
e7c17e43 4334 mlog_errno(ret);
9b7895ef
MF
4335 goto out;
4336 }
4337
4338 /*
e7c17e43
MF
4339 * We make the assumption here that new leaf blocks are added
4340 * to the front of our free list.
9b7895ef 4341 */
e7c17e43
MF
4342 lookup->dl_prev_leaf_bh = NULL;
4343 lookup->dl_leaf_bh = leaf_bh;
9b7895ef
MF
4344 }
4345
9b7895ef 4346out:
e7c17e43
MF
4347 if (free_dx_root)
4348 brelse(dx_root_bh);
9b7895ef
MF
4349 return ret;
4350}
4351
4352/*
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 */
4358int 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,
4363 struct ocfs2_dir_lookup_result *lookup)
4364{
4365 int ret;
5b6a3a2b
MF
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
5b6a3a2b
MF
4372 if (!namelen) {
4373 ret = -EINVAL;
4374 mlog_errno(ret);
4375 goto out;
4376 }
4377
9b7895ef
MF
4378 /*
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)) {
e7c17e43
MF
4393 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4394 name, namelen, lookup);
4395 if (ret)
9b7895ef 4396 mlog_errno(ret);
e7c17e43 4397 goto out;
9b7895ef
MF
4398 }
4399
5b6a3a2b
MF
4400 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,
9b7895ef 4418 lookup, &bh);
5b6a3a2b
MF
4419 if (ret) {
4420 if (ret != -ENOSPC)
4421 mlog_errno(ret);
4422 goto out;
4423 }
4424
4425 BUG_ON(!bh);
4426 }
4427
4a12ca3a 4428 lookup->dl_leaf_bh = bh;
5b6a3a2b
MF
4429 bh = NULL;
4430out:
a81cb88b 4431 brelse(bh);
5b6a3a2b
MF
4432 return ret;
4433}
9b7895ef
MF
4434
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 }
4ed8a6bb 4533 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
9b7895ef 4534
4ed8a6bb
MF
4535 if (ocfs2_dx_root_inline(dx_root))
4536 goto remove_index;
9b7895ef 4537
4ed8a6bb 4538 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
9b7895ef
MF
4539
4540 /* 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
4ed8a6bb 4564remove_index:
9b7895ef
MF
4565 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4566 if (ret) {
4567 mlog_errno(ret);
4568 goto out;
4569 }
4570
8cb471e8 4571 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
9b7895ef
MF
4572out:
4573 ocfs2_schedule_truncate_log_flush(osb, 1);
4574 ocfs2_run_deallocs(osb, &dealloc);
4575
4576 brelse(dx_root_bh);
4577 return ret;
4578}