ocfs2: Remove ENTRY from masklog.
[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
0cf2f763 179 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
e7c17e43
MF
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
23193e51
MF
370 de_buf = first_de;
371 dlimit = de_buf + bytes;
316f4b9f
MF
372
373 while (de_buf < dlimit) {
374 /* this code is executed quadratically often */
375 /* do minimal checking `by hand' */
376
377 de = (struct ocfs2_dir_entry *) de_buf;
378
379 if (de_buf + namelen <= dlimit &&
380 ocfs2_match(namelen, name, de)) {
381 /* found a match - just to be sure, do a full check */
382 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
383 ret = -1;
384 goto bail;
385 }
386 *res_dir = de;
387 ret = 1;
388 goto bail;
389 }
390
391 /* prevent looping on a bad block */
392 de_len = le16_to_cpu(de->rec_len);
393 if (de_len <= 0) {
394 ret = -1;
395 goto bail;
396 }
397
398 de_buf += de_len;
399 offset += de_len;
400 }
401
402bail:
403 mlog_exit(ret);
404 return ret;
405}
406
23193e51
MF
407static struct buffer_head *ocfs2_find_entry_id(const char *name,
408 int namelen,
409 struct inode *dir,
410 struct ocfs2_dir_entry **res_dir)
411{
412 int ret, found;
413 struct buffer_head *di_bh = NULL;
414 struct ocfs2_dinode *di;
415 struct ocfs2_inline_data *data;
416
b657c95c 417 ret = ocfs2_read_inode_block(dir, &di_bh);
23193e51
MF
418 if (ret) {
419 mlog_errno(ret);
420 goto out;
421 }
422
423 di = (struct ocfs2_dinode *)di_bh->b_data;
424 data = &di->id2.i_data;
425
426 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
427 data->id_data, i_size_read(dir), res_dir);
428 if (found == 1)
429 return di_bh;
430
431 brelse(di_bh);
432out:
433 return NULL;
434}
435
a22305cc
JB
436static int ocfs2_validate_dir_block(struct super_block *sb,
437 struct buffer_head *bh)
438{
c175a518
JB
439 int rc;
440 struct ocfs2_dir_block_trailer *trailer =
441 ocfs2_trailer_from_bh(bh, sb);
442
443
a22305cc 444 /*
c175a518 445 * We don't validate dirents here, that's handled
a22305cc
JB
446 * in-place when the code walks them.
447 */
970e4936
JB
448 mlog(0, "Validating dirblock %llu\n",
449 (unsigned long long)bh->b_blocknr);
a22305cc 450
c175a518
JB
451 BUG_ON(!buffer_uptodate(bh));
452
453 /*
454 * If the ecc fails, we return the error but otherwise
455 * leave the filesystem running. We know any error is
456 * local to this block.
457 *
458 * Note that we are safe to call this even if the directory
459 * doesn't have a trailer. Filesystems without metaecc will do
460 * nothing, and filesystems with it will have one.
461 */
462 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
463 if (rc)
464 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
465 (unsigned long long)bh->b_blocknr);
466
467 return rc;
a22305cc
JB
468}
469
9b7895ef
MF
470/*
471 * Validate a directory trailer.
472 *
473 * We check the trailer here rather than in ocfs2_validate_dir_block()
474 * because that function doesn't have the inode to test.
475 */
476static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
477{
478 int rc = 0;
479 struct ocfs2_dir_block_trailer *trailer;
480
481 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
482 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
483 rc = -EINVAL;
484 ocfs2_error(dir->i_sb,
485 "Invalid dirblock #%llu: "
486 "signature = %.*s\n",
487 (unsigned long long)bh->b_blocknr, 7,
488 trailer->db_signature);
489 goto out;
490 }
491 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
492 rc = -EINVAL;
493 ocfs2_error(dir->i_sb,
494 "Directory block #%llu has an invalid "
495 "db_blkno of %llu",
496 (unsigned long long)bh->b_blocknr,
497 (unsigned long long)le64_to_cpu(trailer->db_blkno));
498 goto out;
499 }
500 if (le64_to_cpu(trailer->db_parent_dinode) !=
501 OCFS2_I(dir)->ip_blkno) {
502 rc = -EINVAL;
503 ocfs2_error(dir->i_sb,
504 "Directory block #%llu on dinode "
505 "#%llu has an invalid parent_dinode "
506 "of %llu",
507 (unsigned long long)bh->b_blocknr,
508 (unsigned long long)OCFS2_I(dir)->ip_blkno,
509 (unsigned long long)le64_to_cpu(trailer->db_blkno));
510 goto out;
511 }
512out:
513 return rc;
514}
515
a22305cc
JB
516/*
517 * This function forces all errors to -EIO for consistency with its
518 * predecessor, ocfs2_bread(). We haven't audited what returning the
519 * real error codes would do to callers. We log the real codes with
520 * mlog_errno() before we squash them.
521 */
522static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
523 struct buffer_head **bh, int flags)
524{
525 int rc = 0;
526 struct buffer_head *tmp = *bh;
a22305cc 527
511308d9
JB
528 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
529 ocfs2_validate_dir_block);
87d35a74 530 if (rc) {
a22305cc 531 mlog_errno(rc);
87d35a74
MF
532 goto out;
533 }
534
87d35a74 535 if (!(flags & OCFS2_BH_READAHEAD) &&
e7c17e43 536 ocfs2_supports_dir_trailer(inode)) {
9b7895ef
MF
537 rc = ocfs2_check_dir_trailer(inode, tmp);
538 if (rc) {
539 if (!*bh)
540 brelse(tmp);
541 mlog_errno(rc);
87d35a74
MF
542 goto out;
543 }
544 }
a22305cc 545
511308d9 546 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
87d35a74 547 if (!*bh)
a22305cc
JB
548 *bh = tmp;
549
87d35a74 550out:
a22305cc
JB
551 return rc ? -EIO : 0;
552}
553
9b7895ef
MF
554/*
555 * Read the block at 'phys' which belongs to this directory
556 * inode. This function does no virtual->physical block translation -
557 * what's passed in is assumed to be a valid directory block.
558 */
559static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
560 struct buffer_head **bh)
561{
562 int ret;
563 struct buffer_head *tmp = *bh;
564
8cb471e8
JB
565 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
566 ocfs2_validate_dir_block);
9b7895ef
MF
567 if (ret) {
568 mlog_errno(ret);
569 goto out;
570 }
571
572 if (ocfs2_supports_dir_trailer(dir)) {
573 ret = ocfs2_check_dir_trailer(dir, tmp);
574 if (ret) {
575 if (!*bh)
576 brelse(tmp);
577 mlog_errno(ret);
578 goto out;
579 }
580 }
581
582 if (!ret && !*bh)
583 *bh = tmp;
584out:
585 return ret;
586}
587
588static int ocfs2_validate_dx_root(struct super_block *sb,
589 struct buffer_head *bh)
590{
591 int ret;
592 struct ocfs2_dx_root_block *dx_root;
593
594 BUG_ON(!buffer_uptodate(bh));
595
596 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
597
598 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
599 if (ret) {
600 mlog(ML_ERROR,
601 "Checksum failed for dir index root block %llu\n",
602 (unsigned long long)bh->b_blocknr);
603 return ret;
604 }
605
606 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
607 ocfs2_error(sb,
608 "Dir Index Root # %llu has bad signature %.*s",
609 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
610 7, dx_root->dr_signature);
611 return -EINVAL;
612 }
613
614 return 0;
615}
616
617static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
618 struct buffer_head **dx_root_bh)
619{
620 int ret;
621 u64 blkno = le64_to_cpu(di->i_dx_root);
622 struct buffer_head *tmp = *dx_root_bh;
623
8cb471e8
JB
624 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
625 ocfs2_validate_dx_root);
9b7895ef
MF
626
627 /* If ocfs2_read_block() got us a new bh, pass it up. */
628 if (!ret && !*dx_root_bh)
629 *dx_root_bh = tmp;
630
631 return ret;
632}
633
634static int ocfs2_validate_dx_leaf(struct super_block *sb,
635 struct buffer_head *bh)
636{
637 int ret;
638 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
639
640 BUG_ON(!buffer_uptodate(bh));
641
642 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
643 if (ret) {
644 mlog(ML_ERROR,
645 "Checksum failed for dir index leaf block %llu\n",
646 (unsigned long long)bh->b_blocknr);
647 return ret;
648 }
649
650 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
651 ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
652 7, dx_leaf->dl_signature);
653 return -EROFS;
654 }
655
656 return 0;
657}
658
659static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
660 struct buffer_head **dx_leaf_bh)
661{
662 int ret;
663 struct buffer_head *tmp = *dx_leaf_bh;
664
8cb471e8
JB
665 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
666 ocfs2_validate_dx_leaf);
9b7895ef
MF
667
668 /* If ocfs2_read_block() got us a new bh, pass it up. */
669 if (!ret && !*dx_leaf_bh)
670 *dx_leaf_bh = tmp;
671
672 return ret;
673}
674
675/*
676 * Read a series of dx_leaf blocks. This expects all buffer_head
677 * pointers to be NULL on function entry.
678 */
679static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
680 struct buffer_head **dx_leaf_bhs)
681{
682 int ret;
683
8cb471e8 684 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
9b7895ef
MF
685 ocfs2_validate_dx_leaf);
686 if (ret)
687 mlog_errno(ret);
688
689 return ret;
690}
691
0af4bd38
AB
692static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
693 struct inode *dir,
694 struct ocfs2_dir_entry **res_dir)
316f4b9f
MF
695{
696 struct super_block *sb;
697 struct buffer_head *bh_use[NAMEI_RA_SIZE];
698 struct buffer_head *bh, *ret = NULL;
699 unsigned long start, block, b;
700 int ra_max = 0; /* Number of bh's in the readahead
701 buffer, bh_use[] */
702 int ra_ptr = 0; /* Current index into readahead
703 buffer */
704 int num = 0;
705 int nblocks, i, err;
706
316f4b9f
MF
707 sb = dir->i_sb;
708
709 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
710 start = OCFS2_I(dir)->ip_dir_start_lookup;
711 if (start >= nblocks)
712 start = 0;
713 block = start;
714
715restart:
716 do {
717 /*
718 * We deal with the read-ahead logic here.
719 */
720 if (ra_ptr >= ra_max) {
721 /* Refill the readahead buffer */
722 ra_ptr = 0;
723 b = block;
724 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
725 /*
726 * Terminate if we reach the end of the
727 * directory and must wrap, or if our
728 * search has finished at this block.
729 */
730 if (b >= nblocks || (num && block == start)) {
731 bh_use[ra_max] = NULL;
732 break;
733 }
734 num++;
735
a22305cc
JB
736 bh = NULL;
737 err = ocfs2_read_dir_block(dir, b++, &bh,
738 OCFS2_BH_READAHEAD);
316f4b9f
MF
739 bh_use[ra_max] = bh;
740 }
741 }
742 if ((bh = bh_use[ra_ptr++]) == NULL)
743 goto next;
a22305cc 744 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
5e0b3dec 745 /* read error, skip block & hope for the best.
a22305cc 746 * ocfs2_read_dir_block() has released the bh. */
316f4b9f
MF
747 ocfs2_error(dir->i_sb, "reading directory %llu, "
748 "offset %lu\n",
749 (unsigned long long)OCFS2_I(dir)->ip_blkno,
750 block);
316f4b9f
MF
751 goto next;
752 }
753 i = ocfs2_search_dirblock(bh, dir, name, namelen,
754 block << sb->s_blocksize_bits,
23193e51 755 bh->b_data, sb->s_blocksize,
316f4b9f
MF
756 res_dir);
757 if (i == 1) {
758 OCFS2_I(dir)->ip_dir_start_lookup = block;
759 ret = bh;
760 goto cleanup_and_exit;
761 } else {
762 brelse(bh);
763 if (i < 0)
764 goto cleanup_and_exit;
765 }
766 next:
767 if (++block >= nblocks)
768 block = 0;
769 } while (block != start);
770
771 /*
772 * If the directory has grown while we were searching, then
773 * search the last part of the directory before giving up.
774 */
775 block = nblocks;
776 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
777 if (block < nblocks) {
778 start = 0;
779 goto restart;
780 }
781
782cleanup_and_exit:
783 /* Clean up the read-ahead blocks */
784 for (; ra_ptr < ra_max; ra_ptr++)
785 brelse(bh_use[ra_ptr]);
786
787 mlog_exit_ptr(ret);
788 return ret;
789}
790
9b7895ef
MF
791static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
792 struct ocfs2_extent_list *el,
793 u32 major_hash,
794 u32 *ret_cpos,
795 u64 *ret_phys_blkno,
796 unsigned int *ret_clen)
23193e51 797{
9b7895ef
MF
798 int ret = 0, i, found;
799 struct buffer_head *eb_bh = NULL;
800 struct ocfs2_extent_block *eb;
801 struct ocfs2_extent_rec *rec = NULL;
23193e51 802
9b7895ef 803 if (el->l_tree_depth) {
facdb77f
JB
804 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
805 &eb_bh);
9b7895ef
MF
806 if (ret) {
807 mlog_errno(ret);
808 goto out;
809 }
23193e51 810
9b7895ef
MF
811 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
812 el = &eb->h_list;
4a12ca3a 813
9b7895ef
MF
814 if (el->l_tree_depth) {
815 ocfs2_error(inode->i_sb,
816 "Inode %lu has non zero tree depth in "
817 "btree tree block %llu\n", inode->i_ino,
818 (unsigned long long)eb_bh->b_blocknr);
819 ret = -EROFS;
820 goto out;
821 }
822 }
823
824 found = 0;
825 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
826 rec = &el->l_recs[i];
827
828 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
829 found = 1;
830 break;
831 }
832 }
833
834 if (!found) {
835 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
836 "record (%u, %u, 0) in btree", inode->i_ino,
837 le32_to_cpu(rec->e_cpos),
838 ocfs2_rec_clusters(el, rec));
839 ret = -EROFS;
840 goto out;
841 }
842
843 if (ret_phys_blkno)
844 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
845 if (ret_cpos)
846 *ret_cpos = le32_to_cpu(rec->e_cpos);
847 if (ret_clen)
848 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
849
850out:
851 brelse(eb_bh);
852 return ret;
23193e51
MF
853}
854
5b6a3a2b 855/*
9b7895ef
MF
856 * Returns the block index, from the start of the cluster which this
857 * hash belongs too.
5b6a3a2b 858 */
4ed8a6bb
MF
859static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
860 u32 minor_hash)
38760e24 861{
9b7895ef
MF
862 return minor_hash & osb->osb_dx_mask;
863}
5b6a3a2b 864
4ed8a6bb
MF
865static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
866 struct ocfs2_dx_hinfo *hinfo)
867{
868 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
869}
870
9b7895ef
MF
871static int ocfs2_dx_dir_lookup(struct inode *inode,
872 struct ocfs2_extent_list *el,
873 struct ocfs2_dx_hinfo *hinfo,
874 u32 *ret_cpos,
875 u64 *ret_phys_blkno)
876{
877 int ret = 0;
878 unsigned int cend, uninitialized_var(clen);
879 u32 uninitialized_var(cpos);
880 u64 uninitialized_var(blkno);
881 u32 name_hash = hinfo->major_hash;
13723d00 882
9b7895ef
MF
883 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
884 &clen);
38760e24
MF
885 if (ret) {
886 mlog_errno(ret);
887 goto out;
888 }
889
9b7895ef
MF
890 cend = cpos + clen;
891 if (name_hash >= cend) {
892 /* We want the last cluster */
893 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
894 cpos += clen - 1;
895 } else {
896 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
897 name_hash - cpos);
898 cpos = name_hash;
899 }
38760e24 900
9b7895ef
MF
901 /*
902 * We now have the cluster which should hold our entry. To
903 * find the exact block from the start of the cluster to
904 * search, we take the lower bits of the hash.
905 */
906 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
907
908 if (ret_phys_blkno)
909 *ret_phys_blkno = blkno;
910 if (ret_cpos)
911 *ret_cpos = cpos;
38760e24
MF
912
913out:
9b7895ef 914
38760e24
MF
915 return ret;
916}
917
9b7895ef
MF
918static int ocfs2_dx_dir_search(const char *name, int namelen,
919 struct inode *dir,
4ed8a6bb 920 struct ocfs2_dx_root_block *dx_root,
9b7895ef 921 struct ocfs2_dir_lookup_result *res)
316f4b9f 922{
9b7895ef
MF
923 int ret, i, found;
924 u64 uninitialized_var(phys);
925 struct buffer_head *dx_leaf_bh = NULL;
926 struct ocfs2_dx_leaf *dx_leaf;
927 struct ocfs2_dx_entry *dx_entry = NULL;
928 struct buffer_head *dir_ent_bh = NULL;
929 struct ocfs2_dir_entry *dir_ent = NULL;
930 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
4ed8a6bb
MF
931 struct ocfs2_extent_list *dr_el;
932 struct ocfs2_dx_entry_list *entry_list;
9b7895ef
MF
933
934 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
935
4ed8a6bb
MF
936 if (ocfs2_dx_root_inline(dx_root)) {
937 entry_list = &dx_root->dr_entries;
938 goto search;
939 }
940
941 dr_el = &dx_root->dr_list;
942
9b7895ef
MF
943 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
944 if (ret) {
945 mlog_errno(ret);
946 goto out;
947 }
316f4b9f 948
9b7895ef
MF
949 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
950 "returns: %llu\n",
951 (unsigned long long)OCFS2_I(dir)->ip_blkno,
952 namelen, name, hinfo->major_hash, hinfo->minor_hash,
953 (unsigned long long)phys);
316f4b9f 954
9b7895ef
MF
955 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
956 if (ret) {
957 mlog_errno(ret);
958 goto out;
959 }
13723d00 960
9b7895ef
MF
961 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
962
963 mlog(0, "leaf info: num_used: %d, count: %d\n",
964 le16_to_cpu(dx_leaf->dl_list.de_num_used),
965 le16_to_cpu(dx_leaf->dl_list.de_count));
966
4ed8a6bb
MF
967 entry_list = &dx_leaf->dl_list;
968
969search:
9b7895ef
MF
970 /*
971 * Empty leaf is legal, so no need to check for that.
972 */
973 found = 0;
4ed8a6bb
MF
974 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
975 dx_entry = &entry_list->de_entries[i];
9b7895ef
MF
976
977 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
978 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
979 continue;
980
981 /*
982 * Search unindexed leaf block now. We're not
983 * guaranteed to find anything.
984 */
985 ret = ocfs2_read_dir_block_direct(dir,
986 le64_to_cpu(dx_entry->dx_dirent_blk),
987 &dir_ent_bh);
988 if (ret) {
989 mlog_errno(ret);
990 goto out;
316f4b9f 991 }
9b7895ef
MF
992
993 /*
994 * XXX: We should check the unindexed block here,
995 * before using it.
996 */
997
998 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
999 0, dir_ent_bh->b_data,
1000 dir->i_sb->s_blocksize, &dir_ent);
1001 if (found == 1)
1002 break;
1003
1004 if (found == -1) {
1005 /* This means we found a bad directory entry. */
1006 ret = -EIO;
1007 mlog_errno(ret);
1008 goto out;
1009 }
1010
1011 brelse(dir_ent_bh);
1012 dir_ent_bh = NULL;
1013 }
1014
1015 if (found <= 0) {
1016 ret = -ENOENT;
1017 goto out;
1018 }
1019
1020 res->dl_leaf_bh = dir_ent_bh;
1021 res->dl_entry = dir_ent;
1022 res->dl_dx_leaf_bh = dx_leaf_bh;
1023 res->dl_dx_entry = dx_entry;
1024
1025 ret = 0;
1026out:
1027 if (ret) {
1028 brelse(dx_leaf_bh);
1029 brelse(dir_ent_bh);
1030 }
1031 return ret;
1032}
1033
1034static int ocfs2_find_entry_dx(const char *name, int namelen,
1035 struct inode *dir,
1036 struct ocfs2_dir_lookup_result *lookup)
1037{
1038 int ret;
1039 struct buffer_head *di_bh = NULL;
1040 struct ocfs2_dinode *di;
1041 struct buffer_head *dx_root_bh = NULL;
1042 struct ocfs2_dx_root_block *dx_root;
1043
1044 ret = ocfs2_read_inode_block(dir, &di_bh);
1045 if (ret) {
1046 mlog_errno(ret);
1047 goto out;
1048 }
1049
1050 di = (struct ocfs2_dinode *)di_bh->b_data;
1051
1052 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1053 if (ret) {
1054 mlog_errno(ret);
1055 goto out;
1056 }
1057 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1058
4ed8a6bb 1059 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
9b7895ef
MF
1060 if (ret) {
1061 if (ret != -ENOENT)
1062 mlog_errno(ret);
1063 goto out;
1064 }
1065
4ed8a6bb
MF
1066 lookup->dl_dx_root_bh = dx_root_bh;
1067 dx_root_bh = NULL;
9b7895ef
MF
1068out:
1069 brelse(di_bh);
1070 brelse(dx_root_bh);
1071 return ret;
1072}
1073
1074/*
1075 * Try to find an entry of the provided name within 'dir'.
1076 *
1077 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1078 * returned and the struct 'res' will contain information useful to
1079 * other directory manipulation functions.
1080 *
1081 * Caller can NOT assume anything about the contents of the
1082 * buffer_heads - they are passed back only so that it can be passed
1083 * into any one of the manipulation functions (add entry, delete
1084 * entry, etc). As an example, bh in the extent directory case is a
1085 * data block, in the inline-data case it actually points to an inode,
1086 * in the indexed directory case, multiple buffers are involved.
1087 */
1088int ocfs2_find_entry(const char *name, int namelen,
1089 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1090{
1091 struct buffer_head *bh;
1092 struct ocfs2_dir_entry *res_dir = NULL;
1093
1094 if (ocfs2_dir_indexed(dir))
1095 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1096
1097 /*
1098 * The unindexed dir code only uses part of the lookup
1099 * structure, so there's no reason to push it down further
1100 * than this.
1101 */
1102 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1103 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1104 else
1105 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1106
1107 if (bh == NULL)
1108 return -ENOENT;
1109
1110 lookup->dl_leaf_bh = bh;
1111 lookup->dl_entry = res_dir;
1112 return 0;
1113}
1114
1115/*
1116 * Update inode number and type of a previously found directory entry.
1117 */
1118int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1119 struct ocfs2_dir_lookup_result *res,
1120 struct inode *new_entry_inode)
1121{
1122 int ret;
1123 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1124 struct ocfs2_dir_entry *de = res->dl_entry;
1125 struct buffer_head *de_bh = res->dl_leaf_bh;
1126
1127 /*
1128 * The same code works fine for both inline-data and extent
1129 * based directories, so no need to split this up. The only
1130 * difference is the journal_access function.
1131 */
1132
1133 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1134 access = ocfs2_journal_access_di;
1135
0cf2f763
JB
1136 ret = access(handle, INODE_CACHE(dir), de_bh,
1137 OCFS2_JOURNAL_ACCESS_WRITE);
9b7895ef
MF
1138 if (ret) {
1139 mlog_errno(ret);
1140 goto out;
1141 }
1142
1143 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1144 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1145
1146 ocfs2_journal_dirty(handle, de_bh);
1147
1148out:
1149 return ret;
1150}
1151
1152/*
1153 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1154 * previous entry
1155 */
1156static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1157 struct ocfs2_dir_entry *de_del,
1158 struct buffer_head *bh, char *first_de,
1159 unsigned int bytes)
1160{
1161 struct ocfs2_dir_entry *de, *pde;
1162 int i, status = -ENOENT;
1163 ocfs2_journal_access_func access = ocfs2_journal_access_db;
1164
ef6b689b 1165 mlog(0, "(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
9b7895ef
MF
1166
1167 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1168 access = ocfs2_journal_access_di;
1169
1170 i = 0;
1171 pde = NULL;
1172 de = (struct ocfs2_dir_entry *) first_de;
1173 while (i < bytes) {
1174 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1175 status = -EIO;
1176 mlog_errno(status);
1177 goto bail;
1178 }
1179 if (de == de_del) {
0cf2f763 1180 status = access(handle, INODE_CACHE(dir), bh,
13723d00 1181 OCFS2_JOURNAL_ACCESS_WRITE);
316f4b9f
MF
1182 if (status < 0) {
1183 status = -EIO;
1184 mlog_errno(status);
1185 goto bail;
1186 }
1187 if (pde)
0dd3256e
MS
1188 le16_add_cpu(&pde->rec_len,
1189 le16_to_cpu(de->rec_len));
316f4b9f
MF
1190 else
1191 de->inode = 0;
1192 dir->i_version++;
ec20cec7 1193 ocfs2_journal_dirty(handle, bh);
316f4b9f
MF
1194 goto bail;
1195 }
1196 i += le16_to_cpu(de->rec_len);
1197 pde = de;
1198 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1199 }
1200bail:
1201 mlog_exit(status);
1202 return status;
1203}
1204
e7c17e43
MF
1205static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1206{
1207 unsigned int hole;
1208
1209 if (le64_to_cpu(de->inode) == 0)
1210 hole = le16_to_cpu(de->rec_len);
1211 else
1212 hole = le16_to_cpu(de->rec_len) -
1213 OCFS2_DIR_REC_LEN(de->name_len);
1214
1215 return hole;
1216}
1217
1218static int ocfs2_find_max_rec_len(struct super_block *sb,
1219 struct buffer_head *dirblock_bh)
1220{
1221 int size, this_hole, largest_hole = 0;
1222 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1223 struct ocfs2_dir_entry *de;
1224
1225 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1226 size = ocfs2_dir_trailer_blk_off(sb);
1227 limit = start + size;
1228 de_buf = start;
1229 de = (struct ocfs2_dir_entry *)de_buf;
1230 do {
1231 if (de_buf != trailer) {
1232 this_hole = ocfs2_figure_dirent_hole(de);
1233 if (this_hole > largest_hole)
1234 largest_hole = this_hole;
1235 }
1236
1237 de_buf += le16_to_cpu(de->rec_len);
1238 de = (struct ocfs2_dir_entry *)de_buf;
1239 } while (de_buf < limit);
1240
1241 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1242 return largest_hole;
1243 return 0;
1244}
1245
4ed8a6bb
MF
1246static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1247 int index)
9b7895ef 1248{
4ed8a6bb 1249 int num_used = le16_to_cpu(entry_list->de_num_used);
9b7895ef
MF
1250
1251 if (num_used == 1 || index == (num_used - 1))
1252 goto clear;
1253
4ed8a6bb
MF
1254 memmove(&entry_list->de_entries[index],
1255 &entry_list->de_entries[index + 1],
9b7895ef
MF
1256 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1257clear:
1258 num_used--;
4ed8a6bb 1259 memset(&entry_list->de_entries[num_used], 0,
9b7895ef 1260 sizeof(struct ocfs2_dx_entry));
4ed8a6bb 1261 entry_list->de_num_used = cpu_to_le16(num_used);
9b7895ef
MF
1262}
1263
1264static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1265 struct ocfs2_dir_lookup_result *lookup)
1266{
e7c17e43 1267 int ret, index, max_rec_len, add_to_free_list = 0;
4ed8a6bb 1268 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895ef
MF
1269 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1270 struct ocfs2_dx_leaf *dx_leaf;
1271 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
e7c17e43 1272 struct ocfs2_dir_block_trailer *trailer;
4ed8a6bb
MF
1273 struct ocfs2_dx_root_block *dx_root;
1274 struct ocfs2_dx_entry_list *entry_list;
1275
e7c17e43
MF
1276 /*
1277 * This function gets a bit messy because we might have to
1278 * modify the root block, regardless of whether the indexed
1279 * entries are stored inline.
1280 */
1281
1282 /*
1283 * *Only* set 'entry_list' here, based on where we're looking
1284 * for the indexed entries. Later, we might still want to
1285 * journal both blocks, based on free list state.
1286 */
4ed8a6bb
MF
1287 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1288 if (ocfs2_dx_root_inline(dx_root)) {
1289 entry_list = &dx_root->dr_entries;
1290 } else {
1291 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1292 entry_list = &dx_leaf->dl_list;
1293 }
9b7895ef 1294
9b7895ef
MF
1295 /* Neither of these are a disk corruption - that should have
1296 * been caught by lookup, before we got here. */
4ed8a6bb
MF
1297 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1298 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
9b7895ef 1299
4ed8a6bb 1300 index = (char *)dx_entry - (char *)entry_list->de_entries;
9b7895ef
MF
1301 index /= sizeof(*dx_entry);
1302
4ed8a6bb 1303 if (index >= le16_to_cpu(entry_list->de_num_used)) {
9b7895ef 1304 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
4ed8a6bb
MF
1305 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1306 entry_list, dx_entry);
9b7895ef
MF
1307 return -EIO;
1308 }
1309
e7c17e43
MF
1310 /*
1311 * We know that removal of this dirent will leave enough room
1312 * for a new one, so add this block to the free list if it
1313 * isn't already there.
1314 */
1315 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1316 if (trailer->db_free_rec_len == 0)
1317 add_to_free_list = 1;
1318
9b7895ef 1319 /*
4ed8a6bb
MF
1320 * Add the block holding our index into the journal before
1321 * removing the unindexed entry. If we get an error return
1322 * from __ocfs2_delete_entry(), then it hasn't removed the
1323 * entry yet. Likewise, successful return means we *must*
1324 * remove the indexed entry.
1325 *
e3a93c2d
MF
1326 * We're also careful to journal the root tree block here as
1327 * the entry count needs to be updated. Also, we might be
1328 * adding to the start of the free list.
9b7895ef 1329 */
0cf2f763 1330 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
e3a93c2d
MF
1331 OCFS2_JOURNAL_ACCESS_WRITE);
1332 if (ret) {
1333 mlog_errno(ret);
1334 goto out;
e7c17e43
MF
1335 }
1336
1337 if (!ocfs2_dx_root_inline(dx_root)) {
0cf2f763 1338 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
4ed8a6bb
MF
1339 lookup->dl_dx_leaf_bh,
1340 OCFS2_JOURNAL_ACCESS_WRITE);
1341 if (ret) {
1342 mlog_errno(ret);
1343 goto out;
1344 }
9b7895ef
MF
1345 }
1346
4ed8a6bb
MF
1347 mlog(0, "Dir %llu: delete entry at index: %d\n",
1348 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1349
9b7895ef
MF
1350 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1351 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1352 if (ret) {
1353 mlog_errno(ret);
1354 goto out;
1355 }
1356
e7c17e43
MF
1357 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1358 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1359 if (add_to_free_list) {
1360 trailer->db_free_next = dx_root->dr_free_blk;
1361 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1362 ocfs2_journal_dirty(handle, dx_root_bh);
1363 }
1364
1365 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1366 ocfs2_journal_dirty(handle, leaf_bh);
1367
e3a93c2d
MF
1368 le32_add_cpu(&dx_root->dr_num_entries, -1);
1369 ocfs2_journal_dirty(handle, dx_root_bh);
1370
4ed8a6bb 1371 ocfs2_dx_list_remove_entry(entry_list, index);
9b7895ef 1372
e3a93c2d 1373 if (!ocfs2_dx_root_inline(dx_root))
4ed8a6bb 1374 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
9b7895ef
MF
1375
1376out:
1377 return ret;
1378}
1379
5b6a3a2b
MF
1380static inline int ocfs2_delete_entry_id(handle_t *handle,
1381 struct inode *dir,
1382 struct ocfs2_dir_entry *de_del,
1383 struct buffer_head *bh)
1384{
1385 int ret;
1386 struct buffer_head *di_bh = NULL;
1387 struct ocfs2_dinode *di;
1388 struct ocfs2_inline_data *data;
1389
b657c95c 1390 ret = ocfs2_read_inode_block(dir, &di_bh);
5b6a3a2b
MF
1391 if (ret) {
1392 mlog_errno(ret);
1393 goto out;
1394 }
1395
1396 di = (struct ocfs2_dinode *)di_bh->b_data;
1397 data = &di->id2.i_data;
1398
1399 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1400 i_size_read(dir));
1401
1402 brelse(di_bh);
1403out:
1404 return ret;
1405}
1406
1407static inline int ocfs2_delete_entry_el(handle_t *handle,
1408 struct inode *dir,
1409 struct ocfs2_dir_entry *de_del,
1410 struct buffer_head *bh)
1411{
1412 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1413 bh->b_size);
1414}
1415
1416/*
9b7895ef
MF
1417 * Delete a directory entry. Hide the details of directory
1418 * implementation from the caller.
5b6a3a2b
MF
1419 */
1420int ocfs2_delete_entry(handle_t *handle,
1421 struct inode *dir,
4a12ca3a 1422 struct ocfs2_dir_lookup_result *res)
5b6a3a2b 1423{
9b7895ef
MF
1424 if (ocfs2_dir_indexed(dir))
1425 return ocfs2_delete_entry_dx(handle, dir, res);
1426
5b6a3a2b 1427 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
4a12ca3a
MF
1428 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1429 res->dl_leaf_bh);
5b6a3a2b 1430
4a12ca3a
MF
1431 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1432 res->dl_leaf_bh);
5b6a3a2b
MF
1433}
1434
8553cf4f
MF
1435/*
1436 * Check whether 'de' has enough room to hold an entry of
1437 * 'new_rec_len' bytes.
1438 */
1439static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1440 unsigned int new_rec_len)
1441{
1442 unsigned int de_really_used;
1443
1444 /* Check whether this is an empty record with enough space */
1445 if (le64_to_cpu(de->inode) == 0 &&
1446 le16_to_cpu(de->rec_len) >= new_rec_len)
1447 return 1;
1448
1449 /*
1450 * Record might have free space at the end which we can
1451 * use.
1452 */
1453 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1454 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1455 return 1;
1456
1457 return 0;
1458}
1459
9b7895ef
MF
1460static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1461 struct ocfs2_dx_entry *dx_new_entry)
1462{
1463 int i;
1464
1465 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1466 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1467
1468 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1469}
1470
4ed8a6bb
MF
1471static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1472 struct ocfs2_dx_hinfo *hinfo,
1473 u64 dirent_blk)
1474{
1475 int i;
1476 struct ocfs2_dx_entry *dx_entry;
1477
1478 i = le16_to_cpu(entry_list->de_num_used);
1479 dx_entry = &entry_list->de_entries[i];
1480
1481 memset(dx_entry, 0, sizeof(*dx_entry));
1482 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1483 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1484 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1485
1486 le16_add_cpu(&entry_list->de_num_used, 1);
1487}
1488
9b7895ef
MF
1489static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1490 struct ocfs2_dx_hinfo *hinfo,
1491 u64 dirent_blk,
1492 struct buffer_head *dx_leaf_bh)
1493{
4ed8a6bb 1494 int ret;
9b7895ef
MF
1495 struct ocfs2_dx_leaf *dx_leaf;
1496
0cf2f763 1497 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
9b7895ef
MF
1498 OCFS2_JOURNAL_ACCESS_WRITE);
1499 if (ret) {
1500 mlog_errno(ret);
1501 goto out;
1502 }
1503
1504 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
4ed8a6bb
MF
1505 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1506 ocfs2_journal_dirty(handle, dx_leaf_bh);
9b7895ef 1507
4ed8a6bb
MF
1508out:
1509 return ret;
1510}
9b7895ef 1511
e3a93c2d
MF
1512static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1513 struct ocfs2_dx_hinfo *hinfo,
1514 u64 dirent_blk,
1515 struct ocfs2_dx_root_block *dx_root)
4ed8a6bb 1516{
e3a93c2d
MF
1517 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1518}
1519
1520static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1521 struct ocfs2_dir_lookup_result *lookup)
1522{
1523 int ret = 0;
4ed8a6bb 1524 struct ocfs2_dx_root_block *dx_root;
e3a93c2d 1525 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895ef 1526
0cf2f763 1527 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4ed8a6bb
MF
1528 OCFS2_JOURNAL_ACCESS_WRITE);
1529 if (ret) {
1530 mlog_errno(ret);
1531 goto out;
1532 }
1533
e3a93c2d
MF
1534 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1535 if (ocfs2_dx_root_inline(dx_root)) {
1536 ocfs2_dx_inline_root_insert(dir, handle,
1537 &lookup->dl_hinfo,
1538 lookup->dl_leaf_bh->b_blocknr,
1539 dx_root);
1540 } else {
1541 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1542 lookup->dl_leaf_bh->b_blocknr,
1543 lookup->dl_dx_leaf_bh);
1544 if (ret)
1545 goto out;
1546 }
1547
1548 le32_add_cpu(&dx_root->dr_num_entries, 1);
4ed8a6bb 1549 ocfs2_journal_dirty(handle, dx_root_bh);
9b7895ef
MF
1550
1551out:
1552 return ret;
1553}
1554
e7c17e43
MF
1555static void ocfs2_remove_block_from_free_list(struct inode *dir,
1556 handle_t *handle,
1557 struct ocfs2_dir_lookup_result *lookup)
1558{
1559 struct ocfs2_dir_block_trailer *trailer, *prev;
1560 struct ocfs2_dx_root_block *dx_root;
1561 struct buffer_head *bh;
1562
1563 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1564
1565 if (ocfs2_free_list_at_root(lookup)) {
1566 bh = lookup->dl_dx_root_bh;
1567 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1568 dx_root->dr_free_blk = trailer->db_free_next;
1569 } else {
1570 bh = lookup->dl_prev_leaf_bh;
1571 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1572 prev->db_free_next = trailer->db_free_next;
1573 }
1574
1575 trailer->db_free_rec_len = cpu_to_le16(0);
1576 trailer->db_free_next = cpu_to_le64(0);
1577
1578 ocfs2_journal_dirty(handle, bh);
1579 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1580}
1581
1582/*
1583 * This expects that a journal write has been reserved on
1584 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1585 */
1586static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1587 struct ocfs2_dir_lookup_result *lookup)
1588{
1589 int max_rec_len;
1590 struct ocfs2_dir_block_trailer *trailer;
1591
1592 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1593 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1594 if (max_rec_len) {
1595 /*
1596 * There's still room in this block, so no need to remove it
1597 * from the free list. In this case, we just want to update
1598 * the rec len accounting.
1599 */
1600 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1601 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1602 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1603 } else {
1604 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1605 }
1606}
1607
316f4b9f
MF
1608/* we don't always have a dentry for what we want to add, so people
1609 * like orphan dir can call this instead.
1610 *
4a12ca3a
MF
1611 * The lookup context must have been filled from
1612 * ocfs2_prepare_dir_for_insert.
316f4b9f
MF
1613 */
1614int __ocfs2_add_entry(handle_t *handle,
1615 struct inode *dir,
1616 const char *name, int namelen,
1617 struct inode *inode, u64 blkno,
1618 struct buffer_head *parent_fe_bh,
4a12ca3a 1619 struct ocfs2_dir_lookup_result *lookup)
316f4b9f
MF
1620{
1621 unsigned long offset;
1622 unsigned short rec_len;
1623 struct ocfs2_dir_entry *de, *de1;
5b6a3a2b
MF
1624 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1625 struct super_block *sb = dir->i_sb;
316f4b9f 1626 int retval, status;
5b6a3a2b 1627 unsigned int size = sb->s_blocksize;
4a12ca3a 1628 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
5b6a3a2b 1629 char *data_start = insert_bh->b_data;
316f4b9f 1630
316f4b9f
MF
1631 if (!namelen)
1632 return -EINVAL;
1633
e7c17e43
MF
1634 if (ocfs2_dir_indexed(dir)) {
1635 struct buffer_head *bh;
1636
1637 /*
1638 * An indexed dir may require that we update the free space
1639 * list. Reserve a write to the previous node in the list so
1640 * that we don't fail later.
1641 *
1642 * XXX: This can be either a dx_root_block, or an unindexed
1643 * directory tree leaf block.
1644 */
1645 if (ocfs2_free_list_at_root(lookup)) {
1646 bh = lookup->dl_dx_root_bh;
0cf2f763
JB
1647 retval = ocfs2_journal_access_dr(handle,
1648 INODE_CACHE(dir), bh,
e7c17e43
MF
1649 OCFS2_JOURNAL_ACCESS_WRITE);
1650 } else {
1651 bh = lookup->dl_prev_leaf_bh;
0cf2f763
JB
1652 retval = ocfs2_journal_access_db(handle,
1653 INODE_CACHE(dir), bh,
e7c17e43
MF
1654 OCFS2_JOURNAL_ACCESS_WRITE);
1655 }
1656 if (retval) {
1657 mlog_errno(retval);
1658 return retval;
1659 }
1660 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
5b6a3a2b
MF
1661 data_start = di->id2.i_data.id_data;
1662 size = i_size_read(dir);
1663
1664 BUG_ON(insert_bh != parent_fe_bh);
1665 }
1666
316f4b9f
MF
1667 rec_len = OCFS2_DIR_REC_LEN(namelen);
1668 offset = 0;
5b6a3a2b 1669 de = (struct ocfs2_dir_entry *) data_start;
316f4b9f 1670 while (1) {
5b6a3a2b
MF
1671 BUG_ON((char *)de >= (size + data_start));
1672
316f4b9f
MF
1673 /* These checks should've already been passed by the
1674 * prepare function, but I guess we can leave them
1675 * here anyway. */
1676 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1677 retval = -ENOENT;
1678 goto bail;
1679 }
1680 if (ocfs2_match(namelen, name, de)) {
1681 retval = -EEXIST;
1682 goto bail;
1683 }
8553cf4f 1684
87d35a74
MF
1685 /* We're guaranteed that we should have space, so we
1686 * can't possibly have hit the trailer...right? */
1687 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1688 "Hit dir trailer trying to insert %.*s "
1689 "(namelen %d) into directory %llu. "
1690 "offset is %lu, trailer offset is %d\n",
1691 namelen, name, namelen,
1692 (unsigned long long)parent_fe_bh->b_blocknr,
1693 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1694
8553cf4f 1695 if (ocfs2_dirent_would_fit(de, rec_len)) {
316f4b9f
MF
1696 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1697 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1698 if (retval < 0) {
1699 mlog_errno(retval);
1700 goto bail;
1701 }
1702
13723d00 1703 if (insert_bh == parent_fe_bh)
0cf2f763
JB
1704 status = ocfs2_journal_access_di(handle,
1705 INODE_CACHE(dir),
13723d00
JB
1706 insert_bh,
1707 OCFS2_JOURNAL_ACCESS_WRITE);
9b7895ef 1708 else {
0cf2f763
JB
1709 status = ocfs2_journal_access_db(handle,
1710 INODE_CACHE(dir),
13723d00 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 1748 dir->i_version++;
ec20cec7 1749 ocfs2_journal_dirty(handle, insert_bh);
316f4b9f
MF
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
ef6b689b
TM
2025 mlog(0, "dirino=%llu\n",
2026 (unsigned long long)OCFS2_I(inode)->ip_blkno);
b8bc5f4f 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
ef6b689b
TM
2111 mlog(0, "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
0cf2f763 2286 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00 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);
5b6a3a2b 2294 ocfs2_journal_dirty(handle, di_bh);
5b6a3a2b
MF
2295
2296 i_size_write(inode, size);
2297 inode->i_nlink = 2;
2298 inode->i_blocks = ocfs2_inode_sector_count(inode);
2299
2300 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2301 if (ret < 0)
2302 mlog_errno(ret);
2303
2304out:
2305 return ret;
2306}
2307
2308static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2309 handle_t *handle,
2310 struct inode *parent,
2311 struct inode *inode,
2312 struct buffer_head *fe_bh,
9b7895ef
MF
2313 struct ocfs2_alloc_context *data_ac,
2314 struct buffer_head **ret_new_bh)
316f4b9f
MF
2315{
2316 int status;
87d35a74 2317 unsigned int size = osb->sb->s_blocksize;
316f4b9f 2318 struct buffer_head *new_bh = NULL;
87d35a74 2319 struct ocfs2_dir_entry *de;
316f4b9f 2320
e7c17e43 2321 if (ocfs2_new_dir_wants_trailer(inode))
87d35a74
MF
2322 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2323
316f4b9f
MF
2324 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2325 data_ac, NULL, &new_bh);
2326 if (status < 0) {
2327 mlog_errno(status);
2328 goto bail;
2329 }
2330
8cb471e8 2331 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
316f4b9f 2332
0cf2f763 2333 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
13723d00 2334 OCFS2_JOURNAL_ACCESS_CREATE);
316f4b9f
MF
2335 if (status < 0) {
2336 mlog_errno(status);
2337 goto bail;
2338 }
2339 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2340
87d35a74 2341 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
e7c17e43
MF
2342 if (ocfs2_new_dir_wants_trailer(inode)) {
2343 int size = le16_to_cpu(de->rec_len);
2344
2345 /*
2346 * Figure out the size of the hole left over after
2347 * insertion of '.' and '..'. The trailer wants this
2348 * information.
2349 */
2350 size -= OCFS2_DIR_REC_LEN(2);
2351 size -= sizeof(struct ocfs2_dir_block_trailer);
2352
2353 ocfs2_init_dir_trailer(inode, new_bh, size);
2354 }
316f4b9f 2355
ec20cec7 2356 ocfs2_journal_dirty(handle, new_bh);
316f4b9f
MF
2357
2358 i_size_write(inode, inode->i_sb->s_blocksize);
2359 inode->i_nlink = 2;
2360 inode->i_blocks = ocfs2_inode_sector_count(inode);
2361 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2362 if (status < 0) {
2363 mlog_errno(status);
2364 goto bail;
2365 }
2366
2367 status = 0;
9b7895ef
MF
2368 if (ret_new_bh) {
2369 *ret_new_bh = new_bh;
2370 new_bh = NULL;
2371 }
316f4b9f 2372bail:
a81cb88b 2373 brelse(new_bh);
316f4b9f
MF
2374
2375 mlog_exit(status);
2376 return status;
2377}
2378
9b7895ef
MF
2379static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2380 handle_t *handle, struct inode *dir,
2381 struct buffer_head *di_bh,
e7c17e43 2382 struct buffer_head *dirdata_bh,
9b7895ef 2383 struct ocfs2_alloc_context *meta_ac,
e3a93c2d 2384 int dx_inline, u32 num_entries,
9b7895ef 2385 struct buffer_head **ret_dx_root_bh)
5b6a3a2b 2386{
9b7895ef
MF
2387 int ret;
2388 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2389 u16 dr_suballoc_bit;
2b6cb576 2390 u64 suballoc_loc, dr_blkno;
9b7895ef
MF
2391 unsigned int num_bits;
2392 struct buffer_head *dx_root_bh = NULL;
2393 struct ocfs2_dx_root_block *dx_root;
e7c17e43
MF
2394 struct ocfs2_dir_block_trailer *trailer =
2395 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
9b7895ef 2396
2b6cb576
JB
2397 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2398 &dr_suballoc_bit, &num_bits, &dr_blkno);
9b7895ef
MF
2399 if (ret) {
2400 mlog_errno(ret);
2401 goto out;
2402 }
5b6a3a2b 2403
9b7895ef
MF
2404 mlog(0, "Dir %llu, attach new index block: %llu\n",
2405 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2406 (unsigned long long)dr_blkno);
5b6a3a2b 2407
9b7895ef
MF
2408 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2409 if (dx_root_bh == NULL) {
2410 ret = -EIO;
2411 goto out;
2412 }
8cb471e8 2413 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
87d35a74 2414
0cf2f763 2415 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
9b7895ef
MF
2416 OCFS2_JOURNAL_ACCESS_CREATE);
2417 if (ret < 0) {
2418 mlog_errno(ret);
2419 goto out;
2420 }
87d35a74 2421
9b7895ef
MF
2422 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2423 memset(dx_root, 0, osb->sb->s_blocksize);
2424 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
b89c5428 2425 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2b6cb576 2426 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
9b7895ef
MF
2427 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2428 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2429 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2430 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
e3a93c2d 2431 dx_root->dr_num_entries = cpu_to_le32(num_entries);
e7c17e43
MF
2432 if (le16_to_cpu(trailer->db_free_rec_len))
2433 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2434 else
2435 dx_root->dr_free_blk = cpu_to_le64(0);
4ed8a6bb
MF
2436
2437 if (dx_inline) {
2438 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2439 dx_root->dr_entries.de_count =
2440 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2441 } else {
2442 dx_root->dr_list.l_count =
2443 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2444 }
ec20cec7 2445 ocfs2_journal_dirty(handle, dx_root_bh);
5b6a3a2b 2446
0cf2f763 2447 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
9b7895ef
MF
2448 OCFS2_JOURNAL_ACCESS_CREATE);
2449 if (ret) {
2450 mlog_errno(ret);
2451 goto out;
2452 }
2453
2454 di->i_dx_root = cpu_to_le64(dr_blkno);
2455
8ac33dc8 2456 spin_lock(&OCFS2_I(dir)->ip_lock);
9b7895ef
MF
2457 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2458 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
8ac33dc8 2459 spin_unlock(&OCFS2_I(dir)->ip_lock);
9b7895ef 2460
ec20cec7 2461 ocfs2_journal_dirty(handle, di_bh);
9b7895ef
MF
2462
2463 *ret_dx_root_bh = dx_root_bh;
2464 dx_root_bh = NULL;
2465
2466out:
2467 brelse(dx_root_bh);
2468 return ret;
2469}
2470
2471static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2472 handle_t *handle, struct inode *dir,
2473 struct buffer_head **dx_leaves,
2474 int num_dx_leaves, u64 start_blk)
2475{
2476 int ret, i;
2477 struct ocfs2_dx_leaf *dx_leaf;
2478 struct buffer_head *bh;
2479
2480 for (i = 0; i < num_dx_leaves; i++) {
2481 bh = sb_getblk(osb->sb, start_blk + i);
2482 if (bh == NULL) {
2483 ret = -EIO;
2484 goto out;
2485 }
2486 dx_leaves[i] = bh;
2487
8cb471e8 2488 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
9b7895ef 2489
0cf2f763 2490 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
9b7895ef
MF
2491 OCFS2_JOURNAL_ACCESS_CREATE);
2492 if (ret < 0) {
2493 mlog_errno(ret);
2494 goto out;
2495 }
2496
2497 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2498
2499 memset(dx_leaf, 0, osb->sb->s_blocksize);
2500 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2501 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2502 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2503 dx_leaf->dl_list.de_count =
2504 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2505
2506 mlog(0,
2507 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2508 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2509 (unsigned long long)bh->b_blocknr,
2510 le16_to_cpu(dx_leaf->dl_list.de_count));
2511
2512 ocfs2_journal_dirty(handle, bh);
2513 }
2514
2515 ret = 0;
2516out:
2517 return ret;
2518}
2519
2520/*
2521 * Allocates and formats a new cluster for use in an indexed dir
2522 * leaf. This version will not do the extent insert, so that it can be
2523 * used by operations which need careful ordering.
2524 */
2525static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2526 u32 cpos, handle_t *handle,
2527 struct ocfs2_alloc_context *data_ac,
2528 struct buffer_head **dx_leaves,
2529 int num_dx_leaves, u64 *ret_phys_blkno)
2530{
2531 int ret;
2532 u32 phys, num;
2533 u64 phys_blkno;
2534 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2535
2536 /*
2537 * XXX: For create, this should claim cluster for the index
2538 * *before* the unindexed insert so that we have a better
2539 * chance of contiguousness as the directory grows in number
2540 * of entries.
2541 */
1ed9b777 2542 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
9b7895ef
MF
2543 if (ret) {
2544 mlog_errno(ret);
2545 goto out;
2546 }
2547
2548 /*
2549 * Format the new cluster first. That way, we're inserting
2550 * valid data.
2551 */
2552 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2553 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2554 num_dx_leaves, phys_blkno);
2555 if (ret) {
2556 mlog_errno(ret);
2557 goto out;
2558 }
2559
2560 *ret_phys_blkno = phys_blkno;
2561out:
2562 return ret;
2563}
2564
2565static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2566 struct ocfs2_extent_tree *et,
2567 u32 cpos, handle_t *handle,
2568 struct ocfs2_alloc_context *data_ac,
2569 struct ocfs2_alloc_context *meta_ac,
2570 struct buffer_head **dx_leaves,
2571 int num_dx_leaves)
2572{
2573 int ret;
2574 u64 phys_blkno;
9b7895ef
MF
2575
2576 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2577 num_dx_leaves, &phys_blkno);
2578 if (ret) {
2579 mlog_errno(ret);
2580 goto out;
2581 }
2582
cc79d8c1 2583 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
9b7895ef
MF
2584 meta_ac);
2585 if (ret)
2586 mlog_errno(ret);
2587out:
2588 return ret;
2589}
2590
2591static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2592 int *ret_num_leaves)
2593{
2594 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2595 struct buffer_head **dx_leaves;
2596
2597 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2598 GFP_NOFS);
2599 if (dx_leaves && ret_num_leaves)
2600 *ret_num_leaves = num_dx_leaves;
2601
2602 return dx_leaves;
2603}
2604
2605static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2606 handle_t *handle,
2607 struct inode *parent,
2608 struct inode *inode,
2609 struct buffer_head *di_bh,
2610 struct ocfs2_alloc_context *data_ac,
2611 struct ocfs2_alloc_context *meta_ac)
2612{
4ed8a6bb 2613 int ret;
9b7895ef
MF
2614 struct buffer_head *leaf_bh = NULL;
2615 struct buffer_head *dx_root_bh = NULL;
9b7895ef 2616 struct ocfs2_dx_hinfo hinfo;
4ed8a6bb
MF
2617 struct ocfs2_dx_root_block *dx_root;
2618 struct ocfs2_dx_entry_list *entry_list;
9b7895ef
MF
2619
2620 /*
2621 * Our strategy is to create the directory as though it were
2622 * unindexed, then add the index block. This works with very
2623 * little complication since the state of a new directory is a
2624 * very well known quantity.
2625 *
2626 * Essentially, we have two dirents ("." and ".."), in the 1st
4ed8a6bb
MF
2627 * block which need indexing. These are easily inserted into
2628 * the index block.
9b7895ef
MF
2629 */
2630
2631 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2632 data_ac, &leaf_bh);
2633 if (ret) {
2634 mlog_errno(ret);
2635 goto out;
2636 }
2637
e7c17e43 2638 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
e3a93c2d 2639 meta_ac, 1, 2, &dx_root_bh);
9b7895ef
MF
2640 if (ret) {
2641 mlog_errno(ret);
2642 goto out;
2643 }
4ed8a6bb
MF
2644 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2645 entry_list = &dx_root->dr_entries;
9b7895ef 2646
4ed8a6bb 2647 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
e7c17e43 2648 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
4ed8a6bb 2649 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895ef
MF
2650
2651 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
4ed8a6bb 2652 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895ef
MF
2653
2654out:
9b7895ef
MF
2655 brelse(dx_root_bh);
2656 brelse(leaf_bh);
2657 return ret;
2658}
2659
2660int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2661 handle_t *handle,
2662 struct inode *parent,
2663 struct inode *inode,
2664 struct buffer_head *fe_bh,
2665 struct ocfs2_alloc_context *data_ac,
2666 struct ocfs2_alloc_context *meta_ac)
2667
2668{
2669 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2670
2671 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2672 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2673
2674 if (ocfs2_supports_indexed_dirs(osb))
2675 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2676 data_ac, meta_ac);
2677
2678 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2679 data_ac, NULL);
2680}
2681
2682static int ocfs2_dx_dir_index_block(struct inode *dir,
2683 handle_t *handle,
2684 struct buffer_head **dx_leaves,
2685 int num_dx_leaves,
e3a93c2d 2686 u32 *num_dx_entries,
9b7895ef
MF
2687 struct buffer_head *dirent_bh)
2688{
0fba8137 2689 int ret = 0, namelen, i;
9b7895ef
MF
2690 char *de_buf, *limit;
2691 struct ocfs2_dir_entry *de;
2692 struct buffer_head *dx_leaf_bh;
2693 struct ocfs2_dx_hinfo hinfo;
2694 u64 dirent_blk = dirent_bh->b_blocknr;
2695
2696 de_buf = dirent_bh->b_data;
2697 limit = de_buf + dir->i_sb->s_blocksize;
2698
2699 while (de_buf < limit) {
2700 de = (struct ocfs2_dir_entry *)de_buf;
2701
2702 namelen = de->name_len;
2703 if (!namelen || !de->inode)
2704 goto inc;
2705
2706 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2707
2708 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2709 dx_leaf_bh = dx_leaves[i];
2710
2711 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2712 dirent_blk, dx_leaf_bh);
2713 if (ret) {
2714 mlog_errno(ret);
2715 goto out;
2716 }
2717
e3a93c2d
MF
2718 *num_dx_entries = *num_dx_entries + 1;
2719
9b7895ef
MF
2720inc:
2721 de_buf += le16_to_cpu(de->rec_len);
2722 }
2723
2724out:
2725 return ret;
2726}
e7c17e43
MF
2727
2728/*
4ed8a6bb
MF
2729 * XXX: This expects dx_root_bh to already be part of the transaction.
2730 */
2731static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2732 struct buffer_head *dx_root_bh,
2733 struct buffer_head *dirent_bh)
2734{
2735 char *de_buf, *limit;
2736 struct ocfs2_dx_root_block *dx_root;
2737 struct ocfs2_dir_entry *de;
2738 struct ocfs2_dx_hinfo hinfo;
2739 u64 dirent_blk = dirent_bh->b_blocknr;
2740
2741 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2742
2743 de_buf = dirent_bh->b_data;
2744 limit = de_buf + dir->i_sb->s_blocksize;
2745
2746 while (de_buf < limit) {
2747 de = (struct ocfs2_dir_entry *)de_buf;
2748
2749 if (!de->name_len || !de->inode)
2750 goto inc;
2751
2752 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2753
2754 mlog(0,
2755 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2756 (unsigned long long)dir->i_ino, hinfo.major_hash,
2757 hinfo.minor_hash,
2758 le16_to_cpu(dx_root->dr_entries.de_num_used),
2759 de->name_len, de->name);
2760
2761 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2762 dirent_blk);
e3a93c2d
MF
2763
2764 le32_add_cpu(&dx_root->dr_num_entries, 1);
4ed8a6bb
MF
2765inc:
2766 de_buf += le16_to_cpu(de->rec_len);
2767 }
2768}
2769
2770/*
2771 * Count the number of inline directory entries in di_bh and compare
2772 * them against the number of entries we can hold in an inline dx root
2773 * block.
2774 */
2775static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2776 struct buffer_head *di_bh)
2777{
2778 int dirent_count = 0;
2779 char *de_buf, *limit;
2780 struct ocfs2_dir_entry *de;
2781 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2782
2783 de_buf = di->id2.i_data.id_data;
2784 limit = de_buf + i_size_read(dir);
2785
2786 while (de_buf < limit) {
2787 de = (struct ocfs2_dir_entry *)de_buf;
2788
2789 if (de->name_len && de->inode)
2790 dirent_count++;
2791
2792 de_buf += le16_to_cpu(de->rec_len);
2793 }
2794
2795 /* We are careful to leave room for one extra record. */
2796 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2797}
9b7895ef
MF
2798
2799/*
2800 * Expand rec_len of the rightmost dirent in a directory block so that it
2801 * contains the end of our valid space for dirents. We do this during
2802 * expansion from an inline directory to one with extents. The first dir block
2803 * in that case is taken from the inline data portion of the inode block.
2804 *
e7c17e43
MF
2805 * This will also return the largest amount of contiguous space for a dirent
2806 * in the block. That value is *not* necessarily the last dirent, even after
2807 * expansion. The directory indexing code wants this value for free space
2808 * accounting. We do this here since we're already walking the entire dir
2809 * block.
2810 *
9b7895ef
MF
2811 * We add the dir trailer if this filesystem wants it.
2812 */
e7c17e43
MF
2813static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2814 struct inode *dir)
9b7895ef 2815{
e7c17e43 2816 struct super_block *sb = dir->i_sb;
9b7895ef
MF
2817 struct ocfs2_dir_entry *de;
2818 struct ocfs2_dir_entry *prev_de;
2819 char *de_buf, *limit;
2820 unsigned int new_size = sb->s_blocksize;
e7c17e43
MF
2821 unsigned int bytes, this_hole;
2822 unsigned int largest_hole = 0;
9b7895ef 2823
e7c17e43 2824 if (ocfs2_new_dir_wants_trailer(dir))
9b7895ef
MF
2825 new_size = ocfs2_dir_trailer_blk_off(sb);
2826
2827 bytes = new_size - old_size;
2828
2829 limit = start + old_size;
2830 de_buf = start;
2831 de = (struct ocfs2_dir_entry *)de_buf;
5b6a3a2b 2832 do {
e7c17e43
MF
2833 this_hole = ocfs2_figure_dirent_hole(de);
2834 if (this_hole > largest_hole)
2835 largest_hole = this_hole;
2836
5b6a3a2b
MF
2837 prev_de = de;
2838 de_buf += le16_to_cpu(de->rec_len);
2839 de = (struct ocfs2_dir_entry *)de_buf;
2840 } while (de_buf < limit);
2841
2842 le16_add_cpu(&prev_de->rec_len, bytes);
e7c17e43
MF
2843
2844 /* We need to double check this after modification of the final
2845 * dirent. */
2846 this_hole = ocfs2_figure_dirent_hole(prev_de);
2847 if (this_hole > largest_hole)
2848 largest_hole = this_hole;
2849
2850 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2851 return largest_hole;
2852 return 0;
5b6a3a2b
MF
2853}
2854
2855/*
2856 * We allocate enough clusters to fulfill "blocks_wanted", but set
2857 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2858 * rest automatically for us.
2859 *
2860 * *first_block_bh is a pointer to the 1st data block allocated to the
2861 * directory.
2862 */
2863static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2864 unsigned int blocks_wanted,
9b7895ef 2865 struct ocfs2_dir_lookup_result *lookup,
5b6a3a2b
MF
2866 struct buffer_head **first_block_bh)
2867{
e3a93c2d 2868 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
5b6a3a2b 2869 struct super_block *sb = dir->i_sb;
4ed8a6bb 2870 int ret, i, num_dx_leaves = 0, dx_inline = 0,
9b7895ef
MF
2871 credits = ocfs2_inline_to_extents_credits(sb);
2872 u64 dx_insert_blkno, blkno,
2873 bytes = blocks_wanted << sb->s_blocksize_bits;
5b6a3a2b
MF
2874 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2875 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2876 struct ocfs2_alloc_context *data_ac;
9b7895ef 2877 struct ocfs2_alloc_context *meta_ac = NULL;
5b6a3a2b 2878 struct buffer_head *dirdata_bh = NULL;
9b7895ef
MF
2879 struct buffer_head *dx_root_bh = NULL;
2880 struct buffer_head **dx_leaves = NULL;
5b6a3a2b
MF
2881 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2882 handle_t *handle;
f99b9b7c 2883 struct ocfs2_extent_tree et;
9b7895ef
MF
2884 struct ocfs2_extent_tree dx_et;
2885 int did_quota = 0, bytes_allocated = 0;
f99b9b7c 2886
5e404e9e 2887 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
5b6a3a2b
MF
2888
2889 alloc = ocfs2_clusters_for_bytes(sb, bytes);
9b7895ef
MF
2890 dx_alloc = 0;
2891
edd45c08
JK
2892 down_write(&oi->ip_alloc_sem);
2893
9b7895ef 2894 if (ocfs2_supports_indexed_dirs(osb)) {
9b7895ef
MF
2895 credits += ocfs2_add_dir_index_credits(sb);
2896
4ed8a6bb
MF
2897 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2898 if (!dx_inline) {
2899 /* Add one more cluster for an index leaf */
2900 dx_alloc++;
2901 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2902 &num_dx_leaves);
2903 if (!dx_leaves) {
2904 ret = -ENOMEM;
2905 mlog_errno(ret);
2906 goto out;
2907 }
9b7895ef
MF
2908 }
2909
2910 /* This gets us the dx_root */
2911 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2912 if (ret) {
2913 mlog_errno(ret);
2914 goto out;
2915 }
2916 }
5b6a3a2b
MF
2917
2918 /*
9b7895ef
MF
2919 * We should never need more than 2 clusters for the unindexed
2920 * tree - maximum dirent size is far less than one block. In
2921 * fact, the only time we'd need more than one cluster is if
5b6a3a2b
MF
2922 * blocksize == clustersize and the dirent won't fit in the
2923 * extra space that the expansion to a single block gives. As
2924 * of today, that only happens on 4k/4k file systems.
2925 */
2926 BUG_ON(alloc > 2);
2927
035a5711 2928 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
5b6a3a2b
MF
2929 if (ret) {
2930 mlog_errno(ret);
2931 goto out;
2932 }
2933
5b6a3a2b 2934 /*
c78bad11 2935 * Prepare for worst case allocation scenario of two separate
9b7895ef 2936 * extents in the unindexed tree.
5b6a3a2b
MF
2937 */
2938 if (alloc == 2)
2939 credits += OCFS2_SUBALLOC_ALLOC;
2940
2941 handle = ocfs2_start_trans(osb, credits);
2942 if (IS_ERR(handle)) {
2943 ret = PTR_ERR(handle);
2944 mlog_errno(ret);
edd45c08 2945 goto out;
5b6a3a2b
MF
2946 }
2947
5dd4056d
CH
2948 ret = dquot_alloc_space_nodirty(dir,
2949 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2950 if (ret)
a90714c1 2951 goto out_commit;
a90714c1 2952 did_quota = 1;
9b7895ef 2953
4ed8a6bb 2954 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
9b7895ef
MF
2955 /*
2956 * Allocate our index cluster first, to maximize the
2957 * possibility that unindexed leaves grow
2958 * contiguously.
2959 */
2960 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2961 dx_leaves, num_dx_leaves,
2962 &dx_insert_blkno);
2963 if (ret) {
2964 mlog_errno(ret);
2965 goto out_commit;
2966 }
2967 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2968 }
2969
5b6a3a2b
MF
2970 /*
2971 * Try to claim as many clusters as the bitmap can give though
2972 * if we only get one now, that's enough to continue. The rest
2973 * will be claimed after the conversion to extents.
2974 */
83f92318
MF
2975 if (ocfs2_dir_resv_allowed(osb))
2976 data_ac->ac_resv = &oi->ip_la_data_resv;
1ed9b777 2977 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
5b6a3a2b
MF
2978 if (ret) {
2979 mlog_errno(ret);
2980 goto out_commit;
2981 }
9b7895ef 2982 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b
MF
2983
2984 /*
2985 * Operations are carefully ordered so that we set up the new
2986 * data block first. The conversion from inline data to
2987 * extents follows.
2988 */
2989 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2990 dirdata_bh = sb_getblk(sb, blkno);
2991 if (!dirdata_bh) {
2992 ret = -EIO;
2993 mlog_errno(ret);
2994 goto out_commit;
2995 }
2996
8cb471e8 2997 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
5b6a3a2b 2998
0cf2f763 2999 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
13723d00 3000 OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b
MF
3001 if (ret) {
3002 mlog_errno(ret);
3003 goto out_commit;
3004 }
3005
3006 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
3007 memset(dirdata_bh->b_data + i_size_read(dir), 0,
3008 sb->s_blocksize - i_size_read(dir));
e7c17e43
MF
3009 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
3010 if (ocfs2_new_dir_wants_trailer(dir)) {
3011 /*
3012 * Prepare the dir trailer up front. It will otherwise look
3013 * like a valid dirent. Even if inserting the index fails
3014 * (unlikely), then all we'll have done is given first dir
3015 * block a small amount of fragmentation.
3016 */
3017 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
3018 }
5b6a3a2b 3019
ec20cec7 3020 ocfs2_journal_dirty(handle, dirdata_bh);
5b6a3a2b 3021
4ed8a6bb
MF
3022 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
3023 /*
3024 * Dx dirs with an external cluster need to do this up
3025 * front. Inline dx root's get handled later, after
e3a93c2d
MF
3026 * we've allocated our root block. We get passed back
3027 * a total number of items so that dr_num_entries can
3028 * be correctly set once the dx_root has been
3029 * allocated.
4ed8a6bb 3030 */
9b7895ef 3031 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
e3a93c2d
MF
3032 num_dx_leaves, &num_dx_entries,
3033 dirdata_bh);
9b7895ef
MF
3034 if (ret) {
3035 mlog_errno(ret);
3036 goto out_commit;
3037 }
3038 }
3039
5b6a3a2b
MF
3040 /*
3041 * Set extent, i_size, etc on the directory. After this, the
3042 * inode should contain the same exact dirents as before and
3043 * be fully accessible from system calls.
3044 *
3045 * We let the later dirent insert modify c/mtime - to the user
3046 * the data hasn't changed.
3047 */
0cf2f763 3048 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
13723d00 3049 OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b
MF
3050 if (ret) {
3051 mlog_errno(ret);
3052 goto out_commit;
3053 }
3054
3055 spin_lock(&oi->ip_lock);
3056 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
3057 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
3058 spin_unlock(&oi->ip_lock);
3059
3060 ocfs2_dinode_new_extent_list(dir, di);
3061
3062 i_size_write(dir, sb->s_blocksize);
3063 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3064
3065 di->i_size = cpu_to_le64(sb->s_blocksize);
3066 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
3067 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
5b6a3a2b
MF
3068
3069 /*
3070 * This should never fail as our extent list is empty and all
3071 * related blocks have been journaled already.
3072 */
cc79d8c1 3073 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
f99b9b7c 3074 0, NULL);
5b6a3a2b
MF
3075 if (ret) {
3076 mlog_errno(ret);
83cab533 3077 goto out_commit;
5b6a3a2b
MF
3078 }
3079
9780eb6c
MF
3080 /*
3081 * Set i_blocks after the extent insert for the most up to
3082 * date ip_clusters value.
3083 */
3084 dir->i_blocks = ocfs2_inode_sector_count(dir);
3085
ec20cec7 3086 ocfs2_journal_dirty(handle, di_bh);
5b6a3a2b 3087
9b7895ef
MF
3088 if (ocfs2_supports_indexed_dirs(osb)) {
3089 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
e7c17e43 3090 dirdata_bh, meta_ac, dx_inline,
e3a93c2d 3091 num_dx_entries, &dx_root_bh);
9b7895ef
MF
3092 if (ret) {
3093 mlog_errno(ret);
3094 goto out_commit;
3095 }
3096
4ed8a6bb
MF
3097 if (dx_inline) {
3098 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3099 dirdata_bh);
3100 } else {
5e404e9e
JB
3101 ocfs2_init_dx_root_extent_tree(&dx_et,
3102 INODE_CACHE(dir),
3103 dx_root_bh);
cc79d8c1 3104 ret = ocfs2_insert_extent(handle, &dx_et, 0,
4ed8a6bb
MF
3105 dx_insert_blkno, 1, 0, NULL);
3106 if (ret)
3107 mlog_errno(ret);
3108 }
9b7895ef
MF
3109 }
3110
5b6a3a2b
MF
3111 /*
3112 * We asked for two clusters, but only got one in the 1st
3113 * pass. Claim the 2nd cluster as a separate extent.
3114 */
3115 if (alloc > len) {
1ed9b777 3116 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
5b6a3a2b
MF
3117 &len);
3118 if (ret) {
3119 mlog_errno(ret);
3120 goto out_commit;
3121 }
3122 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3123
cc79d8c1 3124 ret = ocfs2_insert_extent(handle, &et, 1,
f99b9b7c 3125 blkno, len, 0, NULL);
5b6a3a2b
MF
3126 if (ret) {
3127 mlog_errno(ret);
83cab533 3128 goto out_commit;
5b6a3a2b 3129 }
9b7895ef 3130 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b
MF
3131 }
3132
3133 *first_block_bh = dirdata_bh;
3134 dirdata_bh = NULL;
9b7895ef
MF
3135 if (ocfs2_supports_indexed_dirs(osb)) {
3136 unsigned int off;
3137
4ed8a6bb
MF
3138 if (!dx_inline) {
3139 /*
3140 * We need to return the correct block within the
3141 * cluster which should hold our entry.
3142 */
3143 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
3144 &lookup->dl_hinfo);
3145 get_bh(dx_leaves[off]);
3146 lookup->dl_dx_leaf_bh = dx_leaves[off];
3147 }
3148 lookup->dl_dx_root_bh = dx_root_bh;
3149 dx_root_bh = NULL;
9b7895ef 3150 }
5b6a3a2b
MF
3151
3152out_commit:
a90714c1 3153 if (ret < 0 && did_quota)
5dd4056d 3154 dquot_free_space_nodirty(dir, bytes_allocated);
9b7895ef 3155
5b6a3a2b
MF
3156 ocfs2_commit_trans(osb, handle);
3157
5b6a3a2b 3158out:
edd45c08 3159 up_write(&oi->ip_alloc_sem);
5b6a3a2b
MF
3160 if (data_ac)
3161 ocfs2_free_alloc_context(data_ac);
9b7895ef
MF
3162 if (meta_ac)
3163 ocfs2_free_alloc_context(meta_ac);
3164
3165 if (dx_leaves) {
3166 for (i = 0; i < num_dx_leaves; i++)
3167 brelse(dx_leaves[i]);
3168 kfree(dx_leaves);
3169 }
5b6a3a2b
MF
3170
3171 brelse(dirdata_bh);
9b7895ef 3172 brelse(dx_root_bh);
5b6a3a2b
MF
3173
3174 return ret;
3175}
3176
ccd979bd 3177/* returns a bh of the 1st new block in the allocation. */
316f4b9f
MF
3178static int ocfs2_do_extend_dir(struct super_block *sb,
3179 handle_t *handle,
3180 struct inode *dir,
3181 struct buffer_head *parent_fe_bh,
3182 struct ocfs2_alloc_context *data_ac,
3183 struct ocfs2_alloc_context *meta_ac,
3184 struct buffer_head **new_bh)
ccd979bd
MF
3185{
3186 int status;
a90714c1 3187 int extend, did_quota = 0;
8110b073 3188 u64 p_blkno, v_blkno;
ccd979bd
MF
3189
3190 spin_lock(&OCFS2_I(dir)->ip_lock);
3191 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3192 spin_unlock(&OCFS2_I(dir)->ip_lock);
3193
3194 if (extend) {
dcd0538f
MF
3195 u32 offset = OCFS2_I(dir)->ip_clusters;
3196
5dd4056d
CH
3197 status = dquot_alloc_space_nodirty(dir,
3198 ocfs2_clusters_to_bytes(sb, 1));
3199 if (status)
a90714c1 3200 goto bail;
a90714c1
JK
3201 did_quota = 1;
3202
0eb8d47e
TM
3203 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3204 1, 0, parent_fe_bh, handle,
3205 data_ac, meta_ac, NULL);
ccd979bd
MF
3206 BUG_ON(status == -EAGAIN);
3207 if (status < 0) {
3208 mlog_errno(status);
3209 goto bail;
3210 }
3211 }
3212
8110b073
MF
3213 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3214 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
ccd979bd
MF
3215 if (status < 0) {
3216 mlog_errno(status);
3217 goto bail;
3218 }
3219
3220 *new_bh = sb_getblk(sb, p_blkno);
3221 if (!*new_bh) {
3222 status = -EIO;
3223 mlog_errno(status);
3224 goto bail;
3225 }
3226 status = 0;
3227bail:
a90714c1 3228 if (did_quota && status < 0)
5dd4056d 3229 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
ccd979bd
MF
3230 mlog_exit(status);
3231 return status;
3232}
3233
5b6a3a2b
MF
3234/*
3235 * Assumes you already have a cluster lock on the directory.
3236 *
3237 * 'blocks_wanted' is only used if we have an inline directory which
3238 * is to be turned into an extent based one. The size of the dirent to
3239 * insert might be larger than the space gained by growing to just one
3240 * block, so we may have to grow the inode by two blocks in that case.
e7c17e43
MF
3241 *
3242 * If the directory is already indexed, dx_root_bh must be provided.
5b6a3a2b 3243 */
ccd979bd
MF
3244static int ocfs2_extend_dir(struct ocfs2_super *osb,
3245 struct inode *dir,
3246 struct buffer_head *parent_fe_bh,
5b6a3a2b 3247 unsigned int blocks_wanted,
9b7895ef 3248 struct ocfs2_dir_lookup_result *lookup,
ccd979bd
MF
3249 struct buffer_head **new_de_bh)
3250{
3251 int status = 0;
ee19a779 3252 int credits, num_free_extents, drop_alloc_sem = 0;
ccd979bd
MF
3253 loff_t dir_i_size;
3254 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
811f933d 3255 struct ocfs2_extent_list *el = &fe->id2.i_list;
ccd979bd
MF
3256 struct ocfs2_alloc_context *data_ac = NULL;
3257 struct ocfs2_alloc_context *meta_ac = NULL;
1fabe148 3258 handle_t *handle = NULL;
ccd979bd
MF
3259 struct buffer_head *new_bh = NULL;
3260 struct ocfs2_dir_entry * de;
3261 struct super_block *sb = osb->sb;
f99b9b7c 3262 struct ocfs2_extent_tree et;
e7c17e43 3263 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
ccd979bd 3264
5b6a3a2b 3265 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
e7c17e43
MF
3266 /*
3267 * This would be a code error as an inline directory should
3268 * never have an index root.
3269 */
3270 BUG_ON(dx_root_bh);
3271
5b6a3a2b 3272 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
9b7895ef
MF
3273 blocks_wanted, lookup,
3274 &new_bh);
5b6a3a2b
MF
3275 if (status) {
3276 mlog_errno(status);
3277 goto bail;
3278 }
3279
e7c17e43
MF
3280 /* Expansion from inline to an indexed directory will
3281 * have given us this. */
3282 dx_root_bh = lookup->dl_dx_root_bh;
3283
5b6a3a2b
MF
3284 if (blocks_wanted == 1) {
3285 /*
3286 * If the new dirent will fit inside the space
3287 * created by pushing out to one block, then
3288 * we can complete the operation
3289 * here. Otherwise we have to expand i_size
3290 * and format the 2nd block below.
3291 */
3292 BUG_ON(new_bh == NULL);
3293 goto bail_bh;
3294 }
3295
3296 /*
3297 * Get rid of 'new_bh' - we want to format the 2nd
3298 * data block and return that instead.
3299 */
3300 brelse(new_bh);
3301 new_bh = NULL;
3302
edd45c08
JK
3303 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3304 drop_alloc_sem = 1;
5b6a3a2b
MF
3305 dir_i_size = i_size_read(dir);
3306 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3307 goto do_extend;
3308 }
3309
edd45c08
JK
3310 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3311 drop_alloc_sem = 1;
ccd979bd 3312 dir_i_size = i_size_read(dir);
b0697053
MF
3313 mlog(0, "extending dir %llu (i_size = %lld)\n",
3314 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
ccd979bd 3315
ccd979bd
MF
3316 /* dir->i_size is always block aligned. */
3317 spin_lock(&OCFS2_I(dir)->ip_lock);
3318 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3319 spin_unlock(&OCFS2_I(dir)->ip_lock);
5e404e9e
JB
3320 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3321 parent_fe_bh);
3d03a305 3322 num_free_extents = ocfs2_num_free_extents(osb, &et);
ccd979bd
MF
3323 if (num_free_extents < 0) {
3324 status = num_free_extents;
3325 mlog_errno(status);
3326 goto bail;
3327 }
3328
3329 if (!num_free_extents) {
811f933d 3330 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
ccd979bd
MF
3331 if (status < 0) {
3332 if (status != -ENOSPC)
3333 mlog_errno(status);
3334 goto bail;
3335 }
3336 }
3337
da5cbf2f 3338 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
ccd979bd
MF
3339 if (status < 0) {
3340 if (status != -ENOSPC)
3341 mlog_errno(status);
3342 goto bail;
3343 }
3344
83f92318
MF
3345 if (ocfs2_dir_resv_allowed(osb))
3346 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
e3b4a97d 3347
811f933d 3348 credits = ocfs2_calc_extend_credits(sb, el, 1);
ccd979bd
MF
3349 } else {
3350 spin_unlock(&OCFS2_I(dir)->ip_lock);
3351 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3352 }
3353
5b6a3a2b 3354do_extend:
e7c17e43
MF
3355 if (ocfs2_dir_indexed(dir))
3356 credits++; /* For attaching the new dirent block to the
3357 * dx_root */
3358
65eff9cc 3359 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
3360 if (IS_ERR(handle)) {
3361 status = PTR_ERR(handle);
3362 handle = NULL;
3363 mlog_errno(status);
3364 goto bail;
3365 }
3366
3367 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3368 data_ac, meta_ac, &new_bh);
3369 if (status < 0) {
3370 mlog_errno(status);
3371 goto bail;
3372 }
3373
8cb471e8 3374 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
ccd979bd 3375
0cf2f763 3376 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
13723d00 3377 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bd
MF
3378 if (status < 0) {
3379 mlog_errno(status);
3380 goto bail;
3381 }
3382 memset(new_bh->b_data, 0, sb->s_blocksize);
87d35a74 3383
ccd979bd
MF
3384 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3385 de->inode = 0;
e7c17e43 3386 if (ocfs2_supports_dir_trailer(dir)) {
87d35a74 3387 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
e7c17e43
MF
3388
3389 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3390
3391 if (ocfs2_dir_indexed(dir)) {
3392 status = ocfs2_dx_dir_link_trailer(dir, handle,
3393 dx_root_bh, new_bh);
3394 if (status) {
3395 mlog_errno(status);
3396 goto bail;
3397 }
3398 }
87d35a74
MF
3399 } else {
3400 de->rec_len = cpu_to_le16(sb->s_blocksize);
3401 }
ec20cec7 3402 ocfs2_journal_dirty(handle, new_bh);
ccd979bd
MF
3403
3404 dir_i_size += dir->i_sb->s_blocksize;
3405 i_size_write(dir, dir_i_size);
8110b073 3406 dir->i_blocks = ocfs2_inode_sector_count(dir);
ccd979bd
MF
3407 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3408 if (status < 0) {
3409 mlog_errno(status);
3410 goto bail;
3411 }
3412
5b6a3a2b 3413bail_bh:
ccd979bd
MF
3414 *new_de_bh = new_bh;
3415 get_bh(*new_de_bh);
3416bail:
3417 if (handle)
02dc1af4 3418 ocfs2_commit_trans(osb, handle);
edd45c08
JK
3419 if (drop_alloc_sem)
3420 up_write(&OCFS2_I(dir)->ip_alloc_sem);
ccd979bd
MF
3421
3422 if (data_ac)
3423 ocfs2_free_alloc_context(data_ac);
3424 if (meta_ac)
3425 ocfs2_free_alloc_context(meta_ac);
3426
a81cb88b 3427 brelse(new_bh);
ccd979bd
MF
3428
3429 mlog_exit(status);
3430 return status;
3431}
3432
5b6a3a2b
MF
3433static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3434 const char *name, int namelen,
3435 struct buffer_head **ret_de_bh,
3436 unsigned int *blocks_wanted)
ccd979bd 3437{
5b6a3a2b 3438 int ret;
87d35a74 3439 struct super_block *sb = dir->i_sb;
5b6a3a2b
MF
3440 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3441 struct ocfs2_dir_entry *de, *last_de = NULL;
3442 char *de_buf, *limit;
3443 unsigned long offset = 0;
87d35a74
MF
3444 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3445
3446 /*
3447 * This calculates how many free bytes we'd have in block zero, should
3448 * this function force expansion to an extent tree.
3449 */
e7c17e43 3450 if (ocfs2_new_dir_wants_trailer(dir))
87d35a74
MF
3451 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3452 else
3453 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
5b6a3a2b
MF
3454
3455 de_buf = di->id2.i_data.id_data;
3456 limit = de_buf + i_size_read(dir);
3457 rec_len = OCFS2_DIR_REC_LEN(namelen);
ccd979bd 3458
5b6a3a2b
MF
3459 while (de_buf < limit) {
3460 de = (struct ocfs2_dir_entry *)de_buf;
ccd979bd 3461
5b6a3a2b
MF
3462 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3463 ret = -ENOENT;
3464 goto out;
3465 }
3466 if (ocfs2_match(namelen, name, de)) {
3467 ret = -EEXIST;
3468 goto out;
3469 }
87d35a74
MF
3470 /*
3471 * No need to check for a trailing dirent record here as
3472 * they're not used for inline dirs.
3473 */
3474
5b6a3a2b
MF
3475 if (ocfs2_dirent_would_fit(de, rec_len)) {
3476 /* Ok, we found a spot. Return this bh and let
3477 * the caller actually fill it in. */
3478 *ret_de_bh = di_bh;
3479 get_bh(*ret_de_bh);
3480 ret = 0;
3481 goto out;
3482 }
ccd979bd 3483
5b6a3a2b
MF
3484 last_de = de;
3485 de_buf += le16_to_cpu(de->rec_len);
3486 offset += le16_to_cpu(de->rec_len);
3487 }
ccd979bd 3488
5b6a3a2b
MF
3489 /*
3490 * We're going to require expansion of the directory - figure
3491 * out how many blocks we'll need so that a place for the
3492 * dirent can be found.
3493 */
3494 *blocks_wanted = 1;
87d35a74 3495 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
5b6a3a2b
MF
3496 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3497 *blocks_wanted = 2;
ccd979bd 3498
5b6a3a2b
MF
3499 ret = -ENOSPC;
3500out:
3501 return ret;
3502}
3503
3504static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3505 int namelen, struct buffer_head **ret_de_bh)
3506{
3507 unsigned long offset;
3508 struct buffer_head *bh = NULL;
3509 unsigned short rec_len;
3510 struct ocfs2_dir_entry *de;
3511 struct super_block *sb = dir->i_sb;
3512 int status;
87d35a74 3513 int blocksize = dir->i_sb->s_blocksize;
ccd979bd 3514
a22305cc
JB
3515 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3516 if (status) {
ccd979bd
MF
3517 mlog_errno(status);
3518 goto bail;
3519 }
3520
3521 rec_len = OCFS2_DIR_REC_LEN(namelen);
3522 offset = 0;
3523 de = (struct ocfs2_dir_entry *) bh->b_data;
3524 while (1) {
3525 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3526 brelse(bh);
3527 bh = NULL;
3528
3529 if (i_size_read(dir) <= offset) {
5b6a3a2b
MF
3530 /*
3531 * Caller will have to expand this
3532 * directory.
3533 */
3534 status = -ENOSPC;
ccd979bd
MF
3535 goto bail;
3536 }
a22305cc
JB
3537 status = ocfs2_read_dir_block(dir,
3538 offset >> sb->s_blocksize_bits,
3539 &bh, 0);
3540 if (status) {
ccd979bd
MF
3541 mlog_errno(status);
3542 goto bail;
3543 }
3544 /* move to next block */
3545 de = (struct ocfs2_dir_entry *) bh->b_data;
3546 }
3547 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3548 status = -ENOENT;
3549 goto bail;
3550 }
3551 if (ocfs2_match(namelen, name, de)) {
3552 status = -EEXIST;
3553 goto bail;
3554 }
87d35a74
MF
3555
3556 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3557 blocksize))
3558 goto next;
3559
8553cf4f 3560 if (ocfs2_dirent_would_fit(de, rec_len)) {
ccd979bd
MF
3561 /* Ok, we found a spot. Return this bh and let
3562 * the caller actually fill it in. */
3563 *ret_de_bh = bh;
3564 get_bh(*ret_de_bh);
3565 status = 0;
3566 goto bail;
3567 }
87d35a74 3568next:
ccd979bd
MF
3569 offset += le16_to_cpu(de->rec_len);
3570 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3571 }
3572
3573 status = 0;
3574bail:
a81cb88b 3575 brelse(bh);
ccd979bd
MF
3576
3577 mlog_exit(status);
3578 return status;
3579}
5b6a3a2b 3580
9b7895ef 3581static int dx_leaf_sort_cmp(const void *a, const void *b)
5b6a3a2b 3582{
9b7895ef
MF
3583 const struct ocfs2_dx_entry *entry1 = a;
3584 const struct ocfs2_dx_entry *entry2 = b;
3585 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3586 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3587 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3588 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3589
3590 if (major_hash1 > major_hash2)
3591 return 1;
3592 if (major_hash1 < major_hash2)
3593 return -1;
3594
3595 /*
3596 * It is not strictly necessary to sort by minor
3597 */
3598 if (minor_hash1 > minor_hash2)
3599 return 1;
3600 if (minor_hash1 < minor_hash2)
3601 return -1;
3602 return 0;
3603}
3604
3605static void dx_leaf_sort_swap(void *a, void *b, int size)
3606{
3607 struct ocfs2_dx_entry *entry1 = a;
3608 struct ocfs2_dx_entry *entry2 = b;
3609 struct ocfs2_dx_entry tmp;
3610
3611 BUG_ON(size != sizeof(*entry1));
3612
3613 tmp = *entry1;
3614 *entry1 = *entry2;
3615 *entry2 = tmp;
3616}
3617
3618static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3619{
3620 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3621 int i, num = le16_to_cpu(dl_list->de_num_used);
3622
3623 for (i = 0; i < (num - 1); i++) {
3624 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3625 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3626 return 0;
3627 }
3628
3629 return 1;
3630}
3631
3632/*
3633 * Find the optimal value to split this leaf on. This expects the leaf
3634 * entries to be in sorted order.
3635 *
3636 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3637 * the hash we want to insert.
3638 *
3639 * This function is only concerned with the major hash - that which
3640 * determines which cluster an item belongs to.
3641 */
3642static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3643 u32 leaf_cpos, u32 insert_hash,
3644 u32 *split_hash)
3645{
3646 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3647 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3648 int allsame;
3649
3650 /*
3651 * There's a couple rare, but nasty corner cases we have to
3652 * check for here. All of them involve a leaf where all value
3653 * have the same hash, which is what we look for first.
3654 *
3655 * Most of the time, all of the above is false, and we simply
3656 * pick the median value for a split.
3657 */
3658 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3659 if (allsame) {
3660 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3661
3662 if (val == insert_hash) {
3663 /*
3664 * No matter where we would choose to split,
3665 * the new entry would want to occupy the same
3666 * block as these. Since there's no space left
3667 * in their existing block, we know there
3668 * won't be space after the split.
3669 */
3670 return -ENOSPC;
3671 }
3672
3673 if (val == leaf_cpos) {
3674 /*
3675 * Because val is the same as leaf_cpos (which
3676 * is the smallest value this leaf can have),
3677 * yet is not equal to insert_hash, then we
3678 * know that insert_hash *must* be larger than
3679 * val (and leaf_cpos). At least cpos+1 in value.
3680 *
3681 * We also know then, that there cannot be an
3682 * adjacent extent (otherwise we'd be looking
3683 * at it). Choosing this value gives us a
3684 * chance to get some contiguousness.
3685 */
3686 *split_hash = leaf_cpos + 1;
3687 return 0;
3688 }
3689
3690 if (val > insert_hash) {
3691 /*
3692 * val can not be the same as insert hash, and
3693 * also must be larger than leaf_cpos. Also,
3694 * we know that there can't be a leaf between
3695 * cpos and val, otherwise the entries with
3696 * hash 'val' would be there.
3697 */
3698 *split_hash = val;
3699 return 0;
3700 }
3701
3702 *split_hash = insert_hash;
3703 return 0;
3704 }
3705
3706 /*
3707 * Since the records are sorted and the checks above
3708 * guaranteed that not all records in this block are the same,
3709 * we simple travel forward, from the median, and pick the 1st
3710 * record whose value is larger than leaf_cpos.
3711 */
3712 for (i = (num_used / 2); i < num_used; i++)
3713 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3714 leaf_cpos)
3715 break;
3716
3717 BUG_ON(i == num_used); /* Should be impossible */
3718 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3719 return 0;
3720}
3721
3722/*
3723 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3724 * larger than split_hash into new_dx_leaves. We use a temporary
3725 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3726 *
3727 * Since the block offset inside a leaf (cluster) is a constant mask
3728 * of minor_hash, we can optimize - an item at block offset X within
3729 * the original cluster, will be at offset X within the new cluster.
3730 */
3731static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3732 handle_t *handle,
3733 struct ocfs2_dx_leaf *tmp_dx_leaf,
3734 struct buffer_head **orig_dx_leaves,
3735 struct buffer_head **new_dx_leaves,
3736 int num_dx_leaves)
3737{
3738 int i, j, num_used;
3739 u32 major_hash;
3740 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3741 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3742 struct ocfs2_dx_entry *dx_entry;
3743
3744 tmp_list = &tmp_dx_leaf->dl_list;
3745
3746 for (i = 0; i < num_dx_leaves; i++) {
3747 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3748 orig_list = &orig_dx_leaf->dl_list;
3749 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3750 new_list = &new_dx_leaf->dl_list;
3751
3752 num_used = le16_to_cpu(orig_list->de_num_used);
3753
3754 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3755 tmp_list->de_num_used = cpu_to_le16(0);
3756 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3757
3758 for (j = 0; j < num_used; j++) {
3759 dx_entry = &orig_list->de_entries[j];
3760 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3761 if (major_hash >= split_hash)
3762 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3763 dx_entry);
3764 else
3765 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3766 dx_entry);
3767 }
3768 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3769
3770 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3771 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3772 }
3773}
3774
3775static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3776 struct ocfs2_dx_root_block *dx_root)
3777{
3778 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3779
3780 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
3781 credits += ocfs2_quota_trans_credits(osb->sb);
3782 return credits;
3783}
3784
3785/*
3786 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3787 * half our entries into.
3788 */
3789static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3790 struct buffer_head *dx_root_bh,
3791 struct buffer_head *dx_leaf_bh,
3792 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3793 u64 leaf_blkno)
3794{
3795 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3796 int credits, ret, i, num_used, did_quota = 0;
3797 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3798 u64 orig_leaves_start;
3799 int num_dx_leaves;
3800 struct buffer_head **orig_dx_leaves = NULL;
3801 struct buffer_head **new_dx_leaves = NULL;
3802 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3803 struct ocfs2_extent_tree et;
3804 handle_t *handle = NULL;
3805 struct ocfs2_dx_root_block *dx_root;
3806 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3807
3808 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3809 (unsigned long long)OCFS2_I(dir)->ip_blkno,
3810 (unsigned long long)leaf_blkno, insert_hash);
3811
5e404e9e 3812 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
9b7895ef
MF
3813
3814 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3815 /*
3816 * XXX: This is a rather large limit. We should use a more
3817 * realistic value.
3818 */
3819 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3820 return -ENOSPC;
3821
3822 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3823 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3824 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3825 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3826 (unsigned long long)leaf_blkno, num_used);
3827 ret = -EIO;
3828 goto out;
3829 }
3830
3831 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3832 if (!orig_dx_leaves) {
3833 ret = -ENOMEM;
3834 mlog_errno(ret);
3835 goto out;
3836 }
3837
3838 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3839 if (!new_dx_leaves) {
3840 ret = -ENOMEM;
3841 mlog_errno(ret);
3842 goto out;
3843 }
3844
3845 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3846 if (ret) {
3847 if (ret != -ENOSPC)
3848 mlog_errno(ret);
3849 goto out;
3850 }
3851
3852 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3853 handle = ocfs2_start_trans(osb, credits);
3854 if (IS_ERR(handle)) {
3855 ret = PTR_ERR(handle);
3856 handle = NULL;
3857 mlog_errno(ret);
3858 goto out;
3859 }
3860
5dd4056d
CH
3861 ret = dquot_alloc_space_nodirty(dir,
3862 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3863 if (ret)
9b7895ef 3864 goto out_commit;
9b7895ef
MF
3865 did_quota = 1;
3866
0cf2f763 3867 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
9b7895ef
MF
3868 OCFS2_JOURNAL_ACCESS_WRITE);
3869 if (ret) {
3870 mlog_errno(ret);
3871 goto out_commit;
3872 }
3873
3874 /*
3875 * This block is changing anyway, so we can sort it in place.
3876 */
3877 sort(dx_leaf->dl_list.de_entries, num_used,
3878 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3879 dx_leaf_sort_swap);
3880
ec20cec7 3881 ocfs2_journal_dirty(handle, dx_leaf_bh);
9b7895ef
MF
3882
3883 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3884 &split_hash);
3885 if (ret) {
3886 mlog_errno(ret);
3887 goto out_commit;
3888 }
3889
3890 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3891 leaf_cpos, split_hash, insert_hash);
3892
3893 /*
3894 * We have to carefully order operations here. There are items
3895 * which want to be in the new cluster before insert, but in
3896 * order to put those items in the new cluster, we alter the
3897 * old cluster. A failure to insert gets nasty.
3898 *
3899 * So, start by reserving writes to the old
3900 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3901 * the new cluster for us, before inserting it. The insert
3902 * won't happen if there's an error before that. Once the
3903 * insert is done then, we can transfer from one leaf into the
3904 * other without fear of hitting any error.
3905 */
3906
3907 /*
3908 * The leaf transfer wants some scratch space so that we don't
3909 * wind up doing a bunch of expensive memmove().
3910 */
3911 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3912 if (!tmp_dx_leaf) {
3913 ret = -ENOMEM;
3914 mlog_errno(ret);
3915 goto out_commit;
3916 }
3917
1d46dc08 3918 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
9b7895ef
MF
3919 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3920 orig_dx_leaves);
3921 if (ret) {
3922 mlog_errno(ret);
3923 goto out_commit;
3924 }
3925
0f4da216
TY
3926 cpos = split_hash;
3927 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3928 data_ac, meta_ac, new_dx_leaves,
3929 num_dx_leaves);
3930 if (ret) {
3931 mlog_errno(ret);
3932 goto out_commit;
3933 }
3934
9b7895ef 3935 for (i = 0; i < num_dx_leaves; i++) {
0cf2f763
JB
3936 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3937 orig_dx_leaves[i],
9b7895ef
MF
3938 OCFS2_JOURNAL_ACCESS_WRITE);
3939 if (ret) {
3940 mlog_errno(ret);
3941 goto out_commit;
3942 }
9b7895ef 3943
0f4da216
TY
3944 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3945 new_dx_leaves[i],
3946 OCFS2_JOURNAL_ACCESS_WRITE);
3947 if (ret) {
3948 mlog_errno(ret);
3949 goto out_commit;
3950 }
9b7895ef
MF
3951 }
3952
3953 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3954 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3955
3956out_commit:
3957 if (ret < 0 && did_quota)
5dd4056d 3958 dquot_free_space_nodirty(dir,
9b7895ef
MF
3959 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3960
3961 ocfs2_commit_trans(osb, handle);
3962
3963out:
3964 if (orig_dx_leaves || new_dx_leaves) {
3965 for (i = 0; i < num_dx_leaves; i++) {
3966 if (orig_dx_leaves)
3967 brelse(orig_dx_leaves[i]);
3968 if (new_dx_leaves)
3969 brelse(new_dx_leaves[i]);
3970 }
3971 kfree(orig_dx_leaves);
3972 kfree(new_dx_leaves);
3973 }
3974
3975 if (meta_ac)
3976 ocfs2_free_alloc_context(meta_ac);
3977 if (data_ac)
3978 ocfs2_free_alloc_context(data_ac);
3979
3980 kfree(tmp_dx_leaf);
3981 return ret;
3982}
3983
e7c17e43
MF
3984static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3985 struct buffer_head *di_bh,
3986 struct buffer_head *dx_root_bh,
3987 const char *name, int namelen,
3988 struct ocfs2_dir_lookup_result *lookup)
3989{
3990 int ret, rebalanced = 0;
3991 struct ocfs2_dx_root_block *dx_root;
3992 struct buffer_head *dx_leaf_bh = NULL;
3993 struct ocfs2_dx_leaf *dx_leaf;
3994 u64 blkno;
3995 u32 leaf_cpos;
3996
3997 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3998
3999restart_search:
4000 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
4001 &leaf_cpos, &blkno);
4002 if (ret) {
4003 mlog_errno(ret);
4004 goto out;
4005 }
4006
4007 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
4008 if (ret) {
4009 mlog_errno(ret);
4010 goto out;
4011 }
4012
4013 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
4014
4015 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
4016 le16_to_cpu(dx_leaf->dl_list.de_count)) {
4017 if (rebalanced) {
4018 /*
4019 * Rebalancing should have provided us with
4020 * space in an appropriate leaf.
4021 *
4022 * XXX: Is this an abnormal condition then?
4023 * Should we print a message here?
4024 */
4025 ret = -ENOSPC;
4026 goto out;
4027 }
4028
4029 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
4030 &lookup->dl_hinfo, leaf_cpos,
4031 blkno);
4032 if (ret) {
4033 if (ret != -ENOSPC)
4034 mlog_errno(ret);
4035 goto out;
4036 }
4037
4038 /*
4039 * Restart the lookup. The rebalance might have
4040 * changed which block our item fits into. Mark our
4041 * progress, so we only execute this once.
4042 */
4043 brelse(dx_leaf_bh);
4044 dx_leaf_bh = NULL;
4045 rebalanced = 1;
4046 goto restart_search;
4047 }
4048
4049 lookup->dl_dx_leaf_bh = dx_leaf_bh;
4050 dx_leaf_bh = NULL;
4051
4052out:
4053 brelse(dx_leaf_bh);
4054 return ret;
4055}
4056
4057static int ocfs2_search_dx_free_list(struct inode *dir,
4058 struct buffer_head *dx_root_bh,
4059 int namelen,
4060 struct ocfs2_dir_lookup_result *lookup)
4061{
4062 int ret = -ENOSPC;
4063 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
4064 struct ocfs2_dir_block_trailer *db;
4065 u64 next_block;
4066 int rec_len = OCFS2_DIR_REC_LEN(namelen);
4067 struct ocfs2_dx_root_block *dx_root;
4068
4069 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4070 next_block = le64_to_cpu(dx_root->dr_free_blk);
4071
4072 while (next_block) {
4073 brelse(prev_leaf_bh);
4074 prev_leaf_bh = leaf_bh;
4075 leaf_bh = NULL;
4076
4077 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
4078 if (ret) {
4079 mlog_errno(ret);
4080 goto out;
4081 }
4082
4083 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
4084 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
4085 lookup->dl_leaf_bh = leaf_bh;
4086 lookup->dl_prev_leaf_bh = prev_leaf_bh;
4087 leaf_bh = NULL;
4088 prev_leaf_bh = NULL;
4089 break;
4090 }
4091
4092 next_block = le64_to_cpu(db->db_free_next);
4093 }
4094
4095 if (!next_block)
4096 ret = -ENOSPC;
4097
4098out:
4099
4100 brelse(leaf_bh);
4101 brelse(prev_leaf_bh);
4102 return ret;
4103}
4104
4ed8a6bb
MF
4105static int ocfs2_expand_inline_dx_root(struct inode *dir,
4106 struct buffer_head *dx_root_bh)
4107{
4108 int ret, num_dx_leaves, i, j, did_quota = 0;
4109 struct buffer_head **dx_leaves = NULL;
4110 struct ocfs2_extent_tree et;
4111 u64 insert_blkno;
4112 struct ocfs2_alloc_context *data_ac = NULL;
4113 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4114 handle_t *handle = NULL;
4115 struct ocfs2_dx_root_block *dx_root;
4116 struct ocfs2_dx_entry_list *entry_list;
4117 struct ocfs2_dx_entry *dx_entry;
4118 struct ocfs2_dx_leaf *target_leaf;
4119
4120 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4121 if (ret) {
4122 mlog_errno(ret);
4123 goto out;
4124 }
4125
4126 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4127 if (!dx_leaves) {
4128 ret = -ENOMEM;
4129 mlog_errno(ret);
4130 goto out;
4131 }
4132
4133 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4134 if (IS_ERR(handle)) {
4135 ret = PTR_ERR(handle);
4136 mlog_errno(ret);
4137 goto out;
4138 }
4139
5dd4056d
CH
4140 ret = dquot_alloc_space_nodirty(dir,
4141 ocfs2_clusters_to_bytes(osb->sb, 1));
4142 if (ret)
4ed8a6bb 4143 goto out_commit;
4ed8a6bb
MF
4144 did_quota = 1;
4145
4146 /*
4147 * We do this up front, before the allocation, so that a
4148 * failure to add the dx_root_bh to the journal won't result
4149 * us losing clusters.
4150 */
0cf2f763 4151 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4ed8a6bb
MF
4152 OCFS2_JOURNAL_ACCESS_WRITE);
4153 if (ret) {
4154 mlog_errno(ret);
4155 goto out_commit;
4156 }
4157
4158 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4159 num_dx_leaves, &insert_blkno);
4160 if (ret) {
4161 mlog_errno(ret);
4162 goto out_commit;
4163 }
4164
4165 /*
4166 * Transfer the entries from our dx_root into the appropriate
4167 * block
4168 */
4169 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4170 entry_list = &dx_root->dr_entries;
4171
4172 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4173 dx_entry = &entry_list->de_entries[i];
4174
4175 j = __ocfs2_dx_dir_hash_idx(osb,
4176 le32_to_cpu(dx_entry->dx_minor_hash));
4177 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4178
4179 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4180
4181 /* Each leaf has been passed to the journal already
4182 * via __ocfs2_dx_dir_new_cluster() */
4183 }
4184
4185 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4186 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
4187 offsetof(struct ocfs2_dx_root_block, dr_list));
4188 dx_root->dr_list.l_count =
4189 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4190
4191 /* This should never fail considering we start with an empty
4192 * dx_root. */
5e404e9e 4193 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
cc79d8c1 4194 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4ed8a6bb
MF
4195 if (ret)
4196 mlog_errno(ret);
4197 did_quota = 0;
4198
4199 ocfs2_journal_dirty(handle, dx_root_bh);
4200
4201out_commit:
4202 if (ret < 0 && did_quota)
5dd4056d 4203 dquot_free_space_nodirty(dir,
4ed8a6bb
MF
4204 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4205
4206 ocfs2_commit_trans(osb, handle);
4207
4208out:
4209 if (data_ac)
4210 ocfs2_free_alloc_context(data_ac);
4211
4212 if (dx_leaves) {
4213 for (i = 0; i < num_dx_leaves; i++)
4214 brelse(dx_leaves[i]);
4215 kfree(dx_leaves);
4216 }
4217 return ret;
4218}
4219
4220static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4221{
4222 struct ocfs2_dx_root_block *dx_root;
4223 struct ocfs2_dx_entry_list *entry_list;
4224
4225 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4226 entry_list = &dx_root->dr_entries;
4227
4228 if (le16_to_cpu(entry_list->de_num_used) >=
4229 le16_to_cpu(entry_list->de_count))
4230 return -ENOSPC;
4231
4232 return 0;
4233}
4234
e7c17e43
MF
4235static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4236 struct buffer_head *di_bh,
4237 const char *name,
4238 int namelen,
4239 struct ocfs2_dir_lookup_result *lookup)
9b7895ef 4240{
e7c17e43
MF
4241 int ret, free_dx_root = 1;
4242 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
9b7895ef 4243 struct buffer_head *dx_root_bh = NULL;
e7c17e43 4244 struct buffer_head *leaf_bh = NULL;
9b7895ef 4245 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
e7c17e43 4246 struct ocfs2_dx_root_block *dx_root;
9b7895ef
MF
4247
4248 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4249 if (ret) {
4250 mlog_errno(ret);
4251 goto out;
4252 }
4253
4254 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
e3a93c2d
MF
4255 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4256 ret = -ENOSPC;
4257 mlog_errno(ret);
4258 goto out;
4259 }
4260
4ed8a6bb
MF
4261 if (ocfs2_dx_root_inline(dx_root)) {
4262 ret = ocfs2_inline_dx_has_space(dx_root_bh);
4263
4264 if (ret == 0)
4265 goto search_el;
4266
4267 /*
4268 * We ran out of room in the root block. Expand it to
4269 * an extent, then allow ocfs2_find_dir_space_dx to do
4270 * the rest.
4271 */
4272 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4273 if (ret) {
4274 mlog_errno(ret);
4275 goto out;
4276 }
4277 }
9b7895ef 4278
e7c17e43
MF
4279 /*
4280 * Insert preparation for an indexed directory is split into two
4281 * steps. The call to find_dir_space_dx reserves room in the index for
4282 * an additional item. If we run out of space there, it's a real error
4283 * we can't continue on.
4284 */
4285 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4286 namelen, lookup);
9b7895ef
MF
4287 if (ret) {
4288 mlog_errno(ret);
4289 goto out;
4290 }
4291
e7c17e43
MF
4292search_el:
4293 /*
4294 * Next, we need to find space in the unindexed tree. This call
4295 * searches using the free space linked list. If the unindexed tree
4296 * lacks sufficient space, we'll expand it below. The expansion code
4297 * is smart enough to add any new blocks to the free space list.
4298 */
4299 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4300 if (ret && ret != -ENOSPC) {
9b7895ef
MF
4301 mlog_errno(ret);
4302 goto out;
4303 }
4304
e7c17e43
MF
4305 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4306 lookup->dl_dx_root_bh = dx_root_bh;
4307 free_dx_root = 0;
9b7895ef 4308
e7c17e43
MF
4309 if (ret == -ENOSPC) {
4310 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
9b7895ef 4311
9b7895ef 4312 if (ret) {
e7c17e43 4313 mlog_errno(ret);
9b7895ef
MF
4314 goto out;
4315 }
4316
4317 /*
e7c17e43
MF
4318 * We make the assumption here that new leaf blocks are added
4319 * to the front of our free list.
9b7895ef 4320 */
e7c17e43
MF
4321 lookup->dl_prev_leaf_bh = NULL;
4322 lookup->dl_leaf_bh = leaf_bh;
9b7895ef
MF
4323 }
4324
9b7895ef 4325out:
e7c17e43
MF
4326 if (free_dx_root)
4327 brelse(dx_root_bh);
9b7895ef
MF
4328 return ret;
4329}
4330
4331/*
4332 * Get a directory ready for insert. Any directory allocation required
4333 * happens here. Success returns zero, and enough context in the dir
4334 * lookup result that ocfs2_add_entry() will be able complete the task
4335 * with minimal performance impact.
4336 */
4337int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4338 struct inode *dir,
4339 struct buffer_head *parent_fe_bh,
4340 const char *name,
4341 int namelen,
4342 struct ocfs2_dir_lookup_result *lookup)
4343{
4344 int ret;
5b6a3a2b
MF
4345 unsigned int blocks_wanted = 1;
4346 struct buffer_head *bh = NULL;
4347
4348 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
4349 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
4350
5b6a3a2b
MF
4351 if (!namelen) {
4352 ret = -EINVAL;
4353 mlog_errno(ret);
4354 goto out;
4355 }
4356
9b7895ef
MF
4357 /*
4358 * Do this up front to reduce confusion.
4359 *
4360 * The directory might start inline, then be turned into an
4361 * indexed one, in which case we'd need to hash deep inside
4362 * ocfs2_find_dir_space_id(). Since
4363 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4364 * done, there seems no point in spreading out the calls. We
4365 * can optimize away the case where the file system doesn't
4366 * support indexing.
4367 */
4368 if (ocfs2_supports_indexed_dirs(osb))
4369 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4370
4371 if (ocfs2_dir_indexed(dir)) {
e7c17e43
MF
4372 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4373 name, namelen, lookup);
4374 if (ret)
9b7895ef 4375 mlog_errno(ret);
e7c17e43 4376 goto out;
9b7895ef
MF
4377 }
4378
5b6a3a2b
MF
4379 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4380 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4381 namelen, &bh, &blocks_wanted);
4382 } else
4383 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4384
4385 if (ret && ret != -ENOSPC) {
4386 mlog_errno(ret);
4387 goto out;
4388 }
4389
4390 if (ret == -ENOSPC) {
4391 /*
4392 * We have to expand the directory to add this name.
4393 */
4394 BUG_ON(bh);
4395
4396 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
9b7895ef 4397 lookup, &bh);
5b6a3a2b
MF
4398 if (ret) {
4399 if (ret != -ENOSPC)
4400 mlog_errno(ret);
4401 goto out;
4402 }
4403
4404 BUG_ON(!bh);
4405 }
4406
4a12ca3a 4407 lookup->dl_leaf_bh = bh;
5b6a3a2b
MF
4408 bh = NULL;
4409out:
a81cb88b 4410 brelse(bh);
5b6a3a2b
MF
4411 return ret;
4412}
9b7895ef
MF
4413
4414static int ocfs2_dx_dir_remove_index(struct inode *dir,
4415 struct buffer_head *di_bh,
4416 struct buffer_head *dx_root_bh)
4417{
4418 int ret;
4419 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4420 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4421 struct ocfs2_dx_root_block *dx_root;
4422 struct inode *dx_alloc_inode = NULL;
4423 struct buffer_head *dx_alloc_bh = NULL;
4424 handle_t *handle;
4425 u64 blk;
4426 u16 bit;
4427 u64 bg_blkno;
4428
4429 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4430
4431 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4432 EXTENT_ALLOC_SYSTEM_INODE,
4433 le16_to_cpu(dx_root->dr_suballoc_slot));
4434 if (!dx_alloc_inode) {
4435 ret = -ENOMEM;
4436 mlog_errno(ret);
4437 goto out;
4438 }
4439 mutex_lock(&dx_alloc_inode->i_mutex);
4440
4441 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4442 if (ret) {
4443 mlog_errno(ret);
4444 goto out_mutex;
4445 }
4446
4447 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4448 if (IS_ERR(handle)) {
4449 ret = PTR_ERR(handle);
4450 mlog_errno(ret);
4451 goto out_unlock;
4452 }
4453
0cf2f763 4454 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
9b7895ef
MF
4455 OCFS2_JOURNAL_ACCESS_WRITE);
4456 if (ret) {
4457 mlog_errno(ret);
4458 goto out_commit;
4459 }
4460
8ac33dc8 4461 spin_lock(&OCFS2_I(dir)->ip_lock);
9b7895ef
MF
4462 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4463 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
8ac33dc8 4464 spin_unlock(&OCFS2_I(dir)->ip_lock);
9b7895ef
MF
4465 di->i_dx_root = cpu_to_le64(0ULL);
4466
4467 ocfs2_journal_dirty(handle, di_bh);
4468
4469 blk = le64_to_cpu(dx_root->dr_blkno);
4470 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
74380c47
TM
4471 if (dx_root->dr_suballoc_loc)
4472 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4473 else
4474 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
9b7895ef
MF
4475 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4476 bit, bg_blkno, 1);
4477 if (ret)
4478 mlog_errno(ret);
4479
4480out_commit:
4481 ocfs2_commit_trans(osb, handle);
4482
4483out_unlock:
4484 ocfs2_inode_unlock(dx_alloc_inode, 1);
4485
4486out_mutex:
4487 mutex_unlock(&dx_alloc_inode->i_mutex);
4488 brelse(dx_alloc_bh);
4489out:
4490 iput(dx_alloc_inode);
4491 return ret;
4492}
4493
4494int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4495{
4496 int ret;
4497 unsigned int uninitialized_var(clen);
4498 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4499 u64 uninitialized_var(blkno);
4500 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4501 struct buffer_head *dx_root_bh = NULL;
4502 struct ocfs2_dx_root_block *dx_root;
4503 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4504 struct ocfs2_cached_dealloc_ctxt dealloc;
4505 struct ocfs2_extent_tree et;
4506
4507 ocfs2_init_dealloc_ctxt(&dealloc);
4508
4509 if (!ocfs2_dir_indexed(dir))
4510 return 0;
4511
4512 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4513 if (ret) {
4514 mlog_errno(ret);
4515 goto out;
4516 }
4ed8a6bb 4517 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
9b7895ef 4518
4ed8a6bb
MF
4519 if (ocfs2_dx_root_inline(dx_root))
4520 goto remove_index;
9b7895ef 4521
5e404e9e 4522 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
9b7895ef
MF
4523
4524 /* XXX: What if dr_clusters is too large? */
4525 while (le32_to_cpu(dx_root->dr_clusters)) {
4526 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4527 major_hash, &cpos, &blkno, &clen);
4528 if (ret) {
4529 mlog_errno(ret);
4530 goto out;
4531 }
4532
4533 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4534
78f94673
TY
4535 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
4536 &dealloc, 0);
9b7895ef
MF
4537 if (ret) {
4538 mlog_errno(ret);
4539 goto out;
4540 }
4541
4542 if (cpos == 0)
4543 break;
4544
4545 major_hash = cpos - 1;
4546 }
4547
4ed8a6bb 4548remove_index:
9b7895ef
MF
4549 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4550 if (ret) {
4551 mlog_errno(ret);
4552 goto out;
4553 }
4554
8cb471e8 4555 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
9b7895ef
MF
4556out:
4557 ocfs2_schedule_truncate_log_flush(osb, 1);
4558 ocfs2_run_deallocs(osb, &dealloc);
4559
4560 brelse(dx_root_bh);
4561 return ret;
4562}