ocfs2: Remove EXIT from masklog.
[deliverable/linux.git] / fs / ocfs2 / dir.c
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>
43 #include <linux/quotaops.h>
44 #include <linux/sort.h>
45
46 #define MLOG_MASK_PREFIX ML_NAMEI
47 #include <cluster/masklog.h>
48
49 #include "ocfs2.h"
50
51 #include "alloc.h"
52 #include "blockcheck.h"
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"
61 #include "super.h"
62 #include "sysfile.h"
63 #include "uptodate.h"
64
65 #include "buffer_head_io.h"
66
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
72 static unsigned char ocfs2_filetype_table[] = {
73 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
74 };
75
76 static 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);
83 static int ocfs2_dir_indexed(struct inode *inode);
84
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 */
89 static int ocfs2_supports_dir_trailer(struct inode *dir)
90 {
91 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
92
93 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94 return 0;
95
96 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
97 }
98
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 */
107 static int ocfs2_new_dir_wants_trailer(struct inode *dir)
108 {
109 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
110
111 return ocfs2_meta_ecc(osb) ||
112 ocfs2_supports_indexed_dirs(osb);
113 }
114
115 static 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
122 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
123 * them more consistent? */
124 struct 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
133 /*
134 * XXX: This is executed once on every dirent. We should consider optimizing
135 * it.
136 */
137 static 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
144 if (!ocfs2_supports_dir_trailer(dir))
145 return 0;
146
147 if (offset != toff)
148 return 0;
149
150 return 1;
151 }
152
153 static void ocfs2_init_dir_trailer(struct inode *inode,
154 struct buffer_head *bh, u16 rec_len)
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);
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 */
171 static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
172 struct buffer_head *dx_root_bh,
173 struct buffer_head *dirdata_bh)
174 {
175 int ret;
176 struct ocfs2_dx_root_block *dx_root;
177 struct ocfs2_dir_block_trailer *trailer;
178
179 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
180 OCFS2_JOURNAL_ACCESS_WRITE);
181 if (ret) {
182 mlog_errno(ret);
183 goto out;
184 }
185 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
186 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
187
188 trailer->db_free_next = dx_root->dr_free_blk;
189 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
190
191 ocfs2_journal_dirty(handle, dx_root_bh);
192
193 out:
194 return ret;
195 }
196
197 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
198 {
199 return res->dl_prev_leaf_bh == NULL;
200 }
201
202 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
203 {
204 brelse(res->dl_dx_root_bh);
205 brelse(res->dl_leaf_bh);
206 brelse(res->dl_dx_leaf_bh);
207 brelse(res->dl_prev_leaf_bh);
208 }
209
210 static 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
217 static 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
222 /*
223 * Hashing code adapted from ext3
224 */
225 #define DELTA 0x9E3779B9
226
227 static 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
244 static 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
271 static 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
308 out:
309 hinfo->major_hash = buf[0];
310 hinfo->minor_hash = buf[1];
311 }
312
313 /*
314 * bh passed here can be an inode block or a dir data block, depending
315 * on the inode inline data flag.
316 */
317 static int ocfs2_check_dir_entry(struct inode * dir,
318 struct ocfs2_dir_entry * de,
319 struct buffer_head * bh,
320 unsigned long offset)
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
343 static 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 */
357 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
358 struct inode *dir,
359 const char *name, int namelen,
360 unsigned long offset,
361 char *first_de,
362 unsigned int bytes,
363 struct ocfs2_dir_entry **res_dir)
364 {
365 struct ocfs2_dir_entry *de;
366 char *dlimit, *de_buf;
367 int de_len;
368 int ret = 0;
369
370 de_buf = first_de;
371 dlimit = de_buf + bytes;
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
402 bail:
403 mlog(0, "ret = %d\n", ret);
404 return ret;
405 }
406
407 static 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
417 ret = ocfs2_read_inode_block(dir, &di_bh);
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);
432 out:
433 return NULL;
434 }
435
436 static int ocfs2_validate_dir_block(struct super_block *sb,
437 struct buffer_head *bh)
438 {
439 int rc;
440 struct ocfs2_dir_block_trailer *trailer =
441 ocfs2_trailer_from_bh(bh, sb);
442
443
444 /*
445 * We don't validate dirents here, that's handled
446 * in-place when the code walks them.
447 */
448 mlog(0, "Validating dirblock %llu\n",
449 (unsigned long long)bh->b_blocknr);
450
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;
468 }
469
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 */
476 static 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 }
512 out:
513 return rc;
514 }
515
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 */
522 static 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;
527
528 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
529 ocfs2_validate_dir_block);
530 if (rc) {
531 mlog_errno(rc);
532 goto out;
533 }
534
535 if (!(flags & OCFS2_BH_READAHEAD) &&
536 ocfs2_supports_dir_trailer(inode)) {
537 rc = ocfs2_check_dir_trailer(inode, tmp);
538 if (rc) {
539 if (!*bh)
540 brelse(tmp);
541 mlog_errno(rc);
542 goto out;
543 }
544 }
545
546 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
547 if (!*bh)
548 *bh = tmp;
549
550 out:
551 return rc ? -EIO : 0;
552 }
553
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 */
559 static 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
565 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
566 ocfs2_validate_dir_block);
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;
584 out:
585 return ret;
586 }
587
588 static 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
617 static 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
624 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
625 ocfs2_validate_dx_root);
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
634 static 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
659 static 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
665 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
666 ocfs2_validate_dx_leaf);
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 */
679 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
680 struct buffer_head **dx_leaf_bhs)
681 {
682 int ret;
683
684 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
685 ocfs2_validate_dx_leaf);
686 if (ret)
687 mlog_errno(ret);
688
689 return ret;
690 }
691
692 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
693 struct inode *dir,
694 struct ocfs2_dir_entry **res_dir)
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
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
715 restart:
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
736 bh = NULL;
737 err = ocfs2_read_dir_block(dir, b++, &bh,
738 OCFS2_BH_READAHEAD);
739 bh_use[ra_max] = bh;
740 }
741 }
742 if ((bh = bh_use[ra_ptr++]) == NULL)
743 goto next;
744 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
745 /* read error, skip block & hope for the best.
746 * ocfs2_read_dir_block() has released the bh. */
747 ocfs2_error(dir->i_sb, "reading directory %llu, "
748 "offset %lu\n",
749 (unsigned long long)OCFS2_I(dir)->ip_blkno,
750 block);
751 goto next;
752 }
753 i = ocfs2_search_dirblock(bh, dir, name, namelen,
754 block << sb->s_blocksize_bits,
755 bh->b_data, sb->s_blocksize,
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
782 cleanup_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(0, "ret = %p\n", ret);
788 return ret;
789 }
790
791 static 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)
797 {
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;
802
803 if (el->l_tree_depth) {
804 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
805 &eb_bh);
806 if (ret) {
807 mlog_errno(ret);
808 goto out;
809 }
810
811 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
812 el = &eb->h_list;
813
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
850 out:
851 brelse(eb_bh);
852 return ret;
853 }
854
855 /*
856 * Returns the block index, from the start of the cluster which this
857 * hash belongs too.
858 */
859 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
860 u32 minor_hash)
861 {
862 return minor_hash & osb->osb_dx_mask;
863 }
864
865 static 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
871 static 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;
882
883 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
884 &clen);
885 if (ret) {
886 mlog_errno(ret);
887 goto out;
888 }
889
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 }
900
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;
912
913 out:
914
915 return ret;
916 }
917
918 static int ocfs2_dx_dir_search(const char *name, int namelen,
919 struct inode *dir,
920 struct ocfs2_dx_root_block *dx_root,
921 struct ocfs2_dir_lookup_result *res)
922 {
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;
931 struct ocfs2_extent_list *dr_el;
932 struct ocfs2_dx_entry_list *entry_list;
933
934 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
935
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
943 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
944 if (ret) {
945 mlog_errno(ret);
946 goto out;
947 }
948
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);
954
955 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
956 if (ret) {
957 mlog_errno(ret);
958 goto out;
959 }
960
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
967 entry_list = &dx_leaf->dl_list;
968
969 search:
970 /*
971 * Empty leaf is legal, so no need to check for that.
972 */
973 found = 0;
974 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
975 dx_entry = &entry_list->de_entries[i];
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;
991 }
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;
1026 out:
1027 if (ret) {
1028 brelse(dx_leaf_bh);
1029 brelse(dir_ent_bh);
1030 }
1031 return ret;
1032 }
1033
1034 static 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
1059 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
1060 if (ret) {
1061 if (ret != -ENOENT)
1062 mlog_errno(ret);
1063 goto out;
1064 }
1065
1066 lookup->dl_dx_root_bh = dx_root_bh;
1067 dx_root_bh = NULL;
1068 out:
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 */
1088 int 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 */
1118 int 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
1136 ret = access(handle, INODE_CACHE(dir), de_bh,
1137 OCFS2_JOURNAL_ACCESS_WRITE);
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
1148 out:
1149 return ret;
1150 }
1151
1152 /*
1153 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1154 * previous entry
1155 */
1156 static 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
1165 mlog(0, "(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
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) {
1180 status = access(handle, INODE_CACHE(dir), bh,
1181 OCFS2_JOURNAL_ACCESS_WRITE);
1182 if (status < 0) {
1183 status = -EIO;
1184 mlog_errno(status);
1185 goto bail;
1186 }
1187 if (pde)
1188 le16_add_cpu(&pde->rec_len,
1189 le16_to_cpu(de->rec_len));
1190 else
1191 de->inode = 0;
1192 dir->i_version++;
1193 ocfs2_journal_dirty(handle, bh);
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 }
1200 bail:
1201 return status;
1202 }
1203
1204 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1205 {
1206 unsigned int hole;
1207
1208 if (le64_to_cpu(de->inode) == 0)
1209 hole = le16_to_cpu(de->rec_len);
1210 else
1211 hole = le16_to_cpu(de->rec_len) -
1212 OCFS2_DIR_REC_LEN(de->name_len);
1213
1214 return hole;
1215 }
1216
1217 static int ocfs2_find_max_rec_len(struct super_block *sb,
1218 struct buffer_head *dirblock_bh)
1219 {
1220 int size, this_hole, largest_hole = 0;
1221 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1222 struct ocfs2_dir_entry *de;
1223
1224 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1225 size = ocfs2_dir_trailer_blk_off(sb);
1226 limit = start + size;
1227 de_buf = start;
1228 de = (struct ocfs2_dir_entry *)de_buf;
1229 do {
1230 if (de_buf != trailer) {
1231 this_hole = ocfs2_figure_dirent_hole(de);
1232 if (this_hole > largest_hole)
1233 largest_hole = this_hole;
1234 }
1235
1236 de_buf += le16_to_cpu(de->rec_len);
1237 de = (struct ocfs2_dir_entry *)de_buf;
1238 } while (de_buf < limit);
1239
1240 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1241 return largest_hole;
1242 return 0;
1243 }
1244
1245 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1246 int index)
1247 {
1248 int num_used = le16_to_cpu(entry_list->de_num_used);
1249
1250 if (num_used == 1 || index == (num_used - 1))
1251 goto clear;
1252
1253 memmove(&entry_list->de_entries[index],
1254 &entry_list->de_entries[index + 1],
1255 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1256 clear:
1257 num_used--;
1258 memset(&entry_list->de_entries[num_used], 0,
1259 sizeof(struct ocfs2_dx_entry));
1260 entry_list->de_num_used = cpu_to_le16(num_used);
1261 }
1262
1263 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1264 struct ocfs2_dir_lookup_result *lookup)
1265 {
1266 int ret, index, max_rec_len, add_to_free_list = 0;
1267 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1268 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1269 struct ocfs2_dx_leaf *dx_leaf;
1270 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
1271 struct ocfs2_dir_block_trailer *trailer;
1272 struct ocfs2_dx_root_block *dx_root;
1273 struct ocfs2_dx_entry_list *entry_list;
1274
1275 /*
1276 * This function gets a bit messy because we might have to
1277 * modify the root block, regardless of whether the indexed
1278 * entries are stored inline.
1279 */
1280
1281 /*
1282 * *Only* set 'entry_list' here, based on where we're looking
1283 * for the indexed entries. Later, we might still want to
1284 * journal both blocks, based on free list state.
1285 */
1286 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1287 if (ocfs2_dx_root_inline(dx_root)) {
1288 entry_list = &dx_root->dr_entries;
1289 } else {
1290 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1291 entry_list = &dx_leaf->dl_list;
1292 }
1293
1294 /* Neither of these are a disk corruption - that should have
1295 * been caught by lookup, before we got here. */
1296 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1297 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
1298
1299 index = (char *)dx_entry - (char *)entry_list->de_entries;
1300 index /= sizeof(*dx_entry);
1301
1302 if (index >= le16_to_cpu(entry_list->de_num_used)) {
1303 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1304 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1305 entry_list, dx_entry);
1306 return -EIO;
1307 }
1308
1309 /*
1310 * We know that removal of this dirent will leave enough room
1311 * for a new one, so add this block to the free list if it
1312 * isn't already there.
1313 */
1314 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1315 if (trailer->db_free_rec_len == 0)
1316 add_to_free_list = 1;
1317
1318 /*
1319 * Add the block holding our index into the journal before
1320 * removing the unindexed entry. If we get an error return
1321 * from __ocfs2_delete_entry(), then it hasn't removed the
1322 * entry yet. Likewise, successful return means we *must*
1323 * remove the indexed entry.
1324 *
1325 * We're also careful to journal the root tree block here as
1326 * the entry count needs to be updated. Also, we might be
1327 * adding to the start of the free list.
1328 */
1329 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1330 OCFS2_JOURNAL_ACCESS_WRITE);
1331 if (ret) {
1332 mlog_errno(ret);
1333 goto out;
1334 }
1335
1336 if (!ocfs2_dx_root_inline(dx_root)) {
1337 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
1338 lookup->dl_dx_leaf_bh,
1339 OCFS2_JOURNAL_ACCESS_WRITE);
1340 if (ret) {
1341 mlog_errno(ret);
1342 goto out;
1343 }
1344 }
1345
1346 mlog(0, "Dir %llu: delete entry at index: %d\n",
1347 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1348
1349 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1350 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1351 if (ret) {
1352 mlog_errno(ret);
1353 goto out;
1354 }
1355
1356 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1357 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1358 if (add_to_free_list) {
1359 trailer->db_free_next = dx_root->dr_free_blk;
1360 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1361 ocfs2_journal_dirty(handle, dx_root_bh);
1362 }
1363
1364 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1365 ocfs2_journal_dirty(handle, leaf_bh);
1366
1367 le32_add_cpu(&dx_root->dr_num_entries, -1);
1368 ocfs2_journal_dirty(handle, dx_root_bh);
1369
1370 ocfs2_dx_list_remove_entry(entry_list, index);
1371
1372 if (!ocfs2_dx_root_inline(dx_root))
1373 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
1374
1375 out:
1376 return ret;
1377 }
1378
1379 static inline int ocfs2_delete_entry_id(handle_t *handle,
1380 struct inode *dir,
1381 struct ocfs2_dir_entry *de_del,
1382 struct buffer_head *bh)
1383 {
1384 int ret;
1385 struct buffer_head *di_bh = NULL;
1386 struct ocfs2_dinode *di;
1387 struct ocfs2_inline_data *data;
1388
1389 ret = ocfs2_read_inode_block(dir, &di_bh);
1390 if (ret) {
1391 mlog_errno(ret);
1392 goto out;
1393 }
1394
1395 di = (struct ocfs2_dinode *)di_bh->b_data;
1396 data = &di->id2.i_data;
1397
1398 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1399 i_size_read(dir));
1400
1401 brelse(di_bh);
1402 out:
1403 return ret;
1404 }
1405
1406 static inline int ocfs2_delete_entry_el(handle_t *handle,
1407 struct inode *dir,
1408 struct ocfs2_dir_entry *de_del,
1409 struct buffer_head *bh)
1410 {
1411 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1412 bh->b_size);
1413 }
1414
1415 /*
1416 * Delete a directory entry. Hide the details of directory
1417 * implementation from the caller.
1418 */
1419 int ocfs2_delete_entry(handle_t *handle,
1420 struct inode *dir,
1421 struct ocfs2_dir_lookup_result *res)
1422 {
1423 if (ocfs2_dir_indexed(dir))
1424 return ocfs2_delete_entry_dx(handle, dir, res);
1425
1426 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1427 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1428 res->dl_leaf_bh);
1429
1430 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1431 res->dl_leaf_bh);
1432 }
1433
1434 /*
1435 * Check whether 'de' has enough room to hold an entry of
1436 * 'new_rec_len' bytes.
1437 */
1438 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1439 unsigned int new_rec_len)
1440 {
1441 unsigned int de_really_used;
1442
1443 /* Check whether this is an empty record with enough space */
1444 if (le64_to_cpu(de->inode) == 0 &&
1445 le16_to_cpu(de->rec_len) >= new_rec_len)
1446 return 1;
1447
1448 /*
1449 * Record might have free space at the end which we can
1450 * use.
1451 */
1452 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1453 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1454 return 1;
1455
1456 return 0;
1457 }
1458
1459 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1460 struct ocfs2_dx_entry *dx_new_entry)
1461 {
1462 int i;
1463
1464 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1465 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1466
1467 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1468 }
1469
1470 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1471 struct ocfs2_dx_hinfo *hinfo,
1472 u64 dirent_blk)
1473 {
1474 int i;
1475 struct ocfs2_dx_entry *dx_entry;
1476
1477 i = le16_to_cpu(entry_list->de_num_used);
1478 dx_entry = &entry_list->de_entries[i];
1479
1480 memset(dx_entry, 0, sizeof(*dx_entry));
1481 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1482 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1483 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1484
1485 le16_add_cpu(&entry_list->de_num_used, 1);
1486 }
1487
1488 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1489 struct ocfs2_dx_hinfo *hinfo,
1490 u64 dirent_blk,
1491 struct buffer_head *dx_leaf_bh)
1492 {
1493 int ret;
1494 struct ocfs2_dx_leaf *dx_leaf;
1495
1496 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
1497 OCFS2_JOURNAL_ACCESS_WRITE);
1498 if (ret) {
1499 mlog_errno(ret);
1500 goto out;
1501 }
1502
1503 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
1504 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1505 ocfs2_journal_dirty(handle, dx_leaf_bh);
1506
1507 out:
1508 return ret;
1509 }
1510
1511 static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1512 struct ocfs2_dx_hinfo *hinfo,
1513 u64 dirent_blk,
1514 struct ocfs2_dx_root_block *dx_root)
1515 {
1516 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1517 }
1518
1519 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1520 struct ocfs2_dir_lookup_result *lookup)
1521 {
1522 int ret = 0;
1523 struct ocfs2_dx_root_block *dx_root;
1524 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1525
1526 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1527 OCFS2_JOURNAL_ACCESS_WRITE);
1528 if (ret) {
1529 mlog_errno(ret);
1530 goto out;
1531 }
1532
1533 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1534 if (ocfs2_dx_root_inline(dx_root)) {
1535 ocfs2_dx_inline_root_insert(dir, handle,
1536 &lookup->dl_hinfo,
1537 lookup->dl_leaf_bh->b_blocknr,
1538 dx_root);
1539 } else {
1540 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1541 lookup->dl_leaf_bh->b_blocknr,
1542 lookup->dl_dx_leaf_bh);
1543 if (ret)
1544 goto out;
1545 }
1546
1547 le32_add_cpu(&dx_root->dr_num_entries, 1);
1548 ocfs2_journal_dirty(handle, dx_root_bh);
1549
1550 out:
1551 return ret;
1552 }
1553
1554 static void ocfs2_remove_block_from_free_list(struct inode *dir,
1555 handle_t *handle,
1556 struct ocfs2_dir_lookup_result *lookup)
1557 {
1558 struct ocfs2_dir_block_trailer *trailer, *prev;
1559 struct ocfs2_dx_root_block *dx_root;
1560 struct buffer_head *bh;
1561
1562 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1563
1564 if (ocfs2_free_list_at_root(lookup)) {
1565 bh = lookup->dl_dx_root_bh;
1566 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1567 dx_root->dr_free_blk = trailer->db_free_next;
1568 } else {
1569 bh = lookup->dl_prev_leaf_bh;
1570 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1571 prev->db_free_next = trailer->db_free_next;
1572 }
1573
1574 trailer->db_free_rec_len = cpu_to_le16(0);
1575 trailer->db_free_next = cpu_to_le64(0);
1576
1577 ocfs2_journal_dirty(handle, bh);
1578 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1579 }
1580
1581 /*
1582 * This expects that a journal write has been reserved on
1583 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1584 */
1585 static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1586 struct ocfs2_dir_lookup_result *lookup)
1587 {
1588 int max_rec_len;
1589 struct ocfs2_dir_block_trailer *trailer;
1590
1591 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1592 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1593 if (max_rec_len) {
1594 /*
1595 * There's still room in this block, so no need to remove it
1596 * from the free list. In this case, we just want to update
1597 * the rec len accounting.
1598 */
1599 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1600 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1601 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1602 } else {
1603 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1604 }
1605 }
1606
1607 /* we don't always have a dentry for what we want to add, so people
1608 * like orphan dir can call this instead.
1609 *
1610 * The lookup context must have been filled from
1611 * ocfs2_prepare_dir_for_insert.
1612 */
1613 int __ocfs2_add_entry(handle_t *handle,
1614 struct inode *dir,
1615 const char *name, int namelen,
1616 struct inode *inode, u64 blkno,
1617 struct buffer_head *parent_fe_bh,
1618 struct ocfs2_dir_lookup_result *lookup)
1619 {
1620 unsigned long offset;
1621 unsigned short rec_len;
1622 struct ocfs2_dir_entry *de, *de1;
1623 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1624 struct super_block *sb = dir->i_sb;
1625 int retval, status;
1626 unsigned int size = sb->s_blocksize;
1627 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
1628 char *data_start = insert_bh->b_data;
1629
1630 if (!namelen)
1631 return -EINVAL;
1632
1633 if (ocfs2_dir_indexed(dir)) {
1634 struct buffer_head *bh;
1635
1636 /*
1637 * An indexed dir may require that we update the free space
1638 * list. Reserve a write to the previous node in the list so
1639 * that we don't fail later.
1640 *
1641 * XXX: This can be either a dx_root_block, or an unindexed
1642 * directory tree leaf block.
1643 */
1644 if (ocfs2_free_list_at_root(lookup)) {
1645 bh = lookup->dl_dx_root_bh;
1646 retval = ocfs2_journal_access_dr(handle,
1647 INODE_CACHE(dir), bh,
1648 OCFS2_JOURNAL_ACCESS_WRITE);
1649 } else {
1650 bh = lookup->dl_prev_leaf_bh;
1651 retval = ocfs2_journal_access_db(handle,
1652 INODE_CACHE(dir), bh,
1653 OCFS2_JOURNAL_ACCESS_WRITE);
1654 }
1655 if (retval) {
1656 mlog_errno(retval);
1657 return retval;
1658 }
1659 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1660 data_start = di->id2.i_data.id_data;
1661 size = i_size_read(dir);
1662
1663 BUG_ON(insert_bh != parent_fe_bh);
1664 }
1665
1666 rec_len = OCFS2_DIR_REC_LEN(namelen);
1667 offset = 0;
1668 de = (struct ocfs2_dir_entry *) data_start;
1669 while (1) {
1670 BUG_ON((char *)de >= (size + data_start));
1671
1672 /* These checks should've already been passed by the
1673 * prepare function, but I guess we can leave them
1674 * here anyway. */
1675 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1676 retval = -ENOENT;
1677 goto bail;
1678 }
1679 if (ocfs2_match(namelen, name, de)) {
1680 retval = -EEXIST;
1681 goto bail;
1682 }
1683
1684 /* We're guaranteed that we should have space, so we
1685 * can't possibly have hit the trailer...right? */
1686 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1687 "Hit dir trailer trying to insert %.*s "
1688 "(namelen %d) into directory %llu. "
1689 "offset is %lu, trailer offset is %d\n",
1690 namelen, name, namelen,
1691 (unsigned long long)parent_fe_bh->b_blocknr,
1692 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1693
1694 if (ocfs2_dirent_would_fit(de, rec_len)) {
1695 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1696 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1697 if (retval < 0) {
1698 mlog_errno(retval);
1699 goto bail;
1700 }
1701
1702 if (insert_bh == parent_fe_bh)
1703 status = ocfs2_journal_access_di(handle,
1704 INODE_CACHE(dir),
1705 insert_bh,
1706 OCFS2_JOURNAL_ACCESS_WRITE);
1707 else {
1708 status = ocfs2_journal_access_db(handle,
1709 INODE_CACHE(dir),
1710 insert_bh,
1711 OCFS2_JOURNAL_ACCESS_WRITE);
1712
1713 if (ocfs2_dir_indexed(dir)) {
1714 status = ocfs2_dx_dir_insert(dir,
1715 handle,
1716 lookup);
1717 if (status) {
1718 mlog_errno(status);
1719 goto bail;
1720 }
1721 }
1722 }
1723
1724 /* By now the buffer is marked for journaling */
1725 offset += le16_to_cpu(de->rec_len);
1726 if (le64_to_cpu(de->inode)) {
1727 de1 = (struct ocfs2_dir_entry *)((char *) de +
1728 OCFS2_DIR_REC_LEN(de->name_len));
1729 de1->rec_len =
1730 cpu_to_le16(le16_to_cpu(de->rec_len) -
1731 OCFS2_DIR_REC_LEN(de->name_len));
1732 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1733 de = de1;
1734 }
1735 de->file_type = OCFS2_FT_UNKNOWN;
1736 if (blkno) {
1737 de->inode = cpu_to_le64(blkno);
1738 ocfs2_set_de_type(de, inode->i_mode);
1739 } else
1740 de->inode = 0;
1741 de->name_len = namelen;
1742 memcpy(de->name, name, namelen);
1743
1744 if (ocfs2_dir_indexed(dir))
1745 ocfs2_recalc_free_list(dir, handle, lookup);
1746
1747 dir->i_version++;
1748 ocfs2_journal_dirty(handle, insert_bh);
1749 retval = 0;
1750 goto bail;
1751 }
1752
1753 offset += le16_to_cpu(de->rec_len);
1754 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1755 }
1756
1757 /* when you think about it, the assert above should prevent us
1758 * from ever getting here. */
1759 retval = -ENOSPC;
1760 bail:
1761 if (retval)
1762 mlog_errno(retval);
1763
1764 return retval;
1765 }
1766
1767 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
1768 u64 *f_version,
1769 loff_t *f_pos, void *priv,
1770 filldir_t filldir, int *filldir_err)
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
1779 ret = ocfs2_read_inode_block(inode, &di_bh);
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)) {
1790 revalidate:
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 */
1829 u64 version = *f_version;
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);
1840 if (filldir_ret) {
1841 if (filldir_err)
1842 *filldir_err = filldir_ret;
1843 break;
1844 }
1845 if (version != *f_version)
1846 goto revalidate;
1847 }
1848 *f_pos += le16_to_cpu(de->rec_len);
1849 }
1850
1851 out:
1852 brelse(di_bh);
1853
1854 return 0;
1855 }
1856
1857 /*
1858 * NOTE: This function can be called against unindexed directories,
1859 * and indexed ones.
1860 */
1861 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
1862 u64 *f_version,
1863 loff_t *f_pos, void *priv,
1864 filldir_t filldir, int *filldir_err)
1865 {
1866 int error = 0;
1867 unsigned long offset, blk, last_ra_blk = 0;
1868 int i, stored;
1869 struct buffer_head * bh, * tmp;
1870 struct ocfs2_dir_entry * de;
1871 struct super_block * sb = inode->i_sb;
1872 unsigned int ra_sectors = 16;
1873
1874 stored = 0;
1875 bh = NULL;
1876
1877 offset = (*f_pos) & (sb->s_blocksize - 1);
1878
1879 while (!error && !stored && *f_pos < i_size_read(inode)) {
1880 blk = (*f_pos) >> sb->s_blocksize_bits;
1881 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1882 /* Skip the corrupt dirblock and keep trying */
1883 *f_pos += sb->s_blocksize - offset;
1884 continue;
1885 }
1886
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);
1895 i > 0; i--) {
1896 tmp = NULL;
1897 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1898 OCFS2_BH_READAHEAD))
1899 brelse(tmp);
1900 }
1901 last_ra_blk = blk;
1902 ra_sectors = 8;
1903 }
1904
1905 revalidate:
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. */
1910 if (*f_version != inode->i_version) {
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;
1925 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
1926 | offset;
1927 *f_version = inode->i_version;
1928 }
1929
1930 while (!error && *f_pos < i_size_read(inode)
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. */
1936 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
1937 brelse(bh);
1938 goto out;
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 */
1949 unsigned long version = *f_version;
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];
1954 error = filldir(priv, de->name,
1955 de->name_len,
1956 *f_pos,
1957 le64_to_cpu(de->inode),
1958 d_type);
1959 if (error) {
1960 if (filldir_err)
1961 *filldir_err = error;
1962 break;
1963 }
1964 if (version != *f_version)
1965 goto revalidate;
1966 stored ++;
1967 }
1968 *f_pos += le16_to_cpu(de->rec_len);
1969 }
1970 offset = 0;
1971 brelse(bh);
1972 bh = NULL;
1973 }
1974
1975 stored = 0;
1976 out:
1977 return stored;
1978 }
1979
1980 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1981 loff_t *f_pos, void *priv, filldir_t filldir,
1982 int *filldir_err)
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,
1986 filldir, filldir_err);
1987
1988 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1989 filldir_err);
1990 }
1991
1992 /*
1993 * This is intended to be called from inside other kernel functions,
1994 * so we fake some arguments.
1995 */
1996 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1997 filldir_t filldir)
1998 {
1999 int ret = 0, filldir_err = 0;
2000 u64 version = inode->i_version;
2001
2002 while (*f_pos < i_size_read(inode)) {
2003 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
2004 filldir, &filldir_err);
2005 if (ret || filldir_err)
2006 break;
2007 }
2008
2009 if (ret > 0)
2010 ret = -EIO;
2011
2012 return 0;
2013 }
2014
2015 /*
2016 * ocfs2_readdir()
2017 *
2018 */
2019 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
2020 {
2021 int error = 0;
2022 struct inode *inode = filp->f_path.dentry->d_inode;
2023 int lock_level = 0;
2024
2025 mlog(0, "dirino=%llu\n",
2026 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2027
2028 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
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. */
2033 ocfs2_inode_unlock(inode, 1);
2034 lock_level = 0;
2035 error = ocfs2_inode_lock(inode, NULL, 0);
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,
2045 dirent, filldir, NULL);
2046
2047 ocfs2_inode_unlock(inode, lock_level);
2048 if (error)
2049 mlog_errno(error);
2050
2051 bail_nolock:
2052
2053 return error;
2054 }
2055
2056 /*
2057 * NOTE: this should always be called with parent dir i_mutex taken.
2058 */
2059 int ocfs2_find_files_on_disk(const char *name,
2060 int namelen,
2061 u64 *blkno,
2062 struct inode *inode,
2063 struct ocfs2_dir_lookup_result *lookup)
2064 {
2065 int status = -ENOENT;
2066
2067 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
2068 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2069
2070 status = ocfs2_find_entry(name, namelen, inode, lookup);
2071 if (status)
2072 goto leave;
2073
2074 *blkno = le64_to_cpu(lookup->dl_entry->inode);
2075
2076 status = 0;
2077 leave:
2078
2079 return status;
2080 }
2081
2082 /*
2083 * Convenience function for callers which just want the block number
2084 * mapped to a name and don't require the full dirent info, etc.
2085 */
2086 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2087 int namelen, u64 *blkno)
2088 {
2089 int ret;
2090 struct ocfs2_dir_lookup_result lookup = { NULL, };
2091
2092 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2093 ocfs2_free_dir_lookup_result(&lookup);
2094
2095 return ret;
2096 }
2097
2098 /* Check for a name within a directory.
2099 *
2100 * Return 0 if the name does not exist
2101 * Return -EEXIST if the directory contains the name
2102 *
2103 * Callers should have i_mutex + a cluster lock on dir
2104 */
2105 int ocfs2_check_dir_for_entry(struct inode *dir,
2106 const char *name,
2107 int namelen)
2108 {
2109 int ret;
2110 struct ocfs2_dir_lookup_result lookup = { NULL, };
2111
2112 mlog(0, "dir %llu, name '%.*s'\n",
2113 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
2114
2115 ret = -EEXIST;
2116 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
2117 goto bail;
2118
2119 ret = 0;
2120 bail:
2121 ocfs2_free_dir_lookup_result(&lookup);
2122
2123 if (ret)
2124 mlog_errno(ret);
2125 return ret;
2126 }
2127
2128 struct ocfs2_empty_dir_priv {
2129 unsigned seen_dot;
2130 unsigned seen_dot_dot;
2131 unsigned seen_other;
2132 unsigned dx_dir;
2133 };
2134 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
2135 loff_t pos, u64 ino, unsigned type)
2136 {
2137 struct ocfs2_empty_dir_priv *p = priv;
2138
2139 /*
2140 * Check the positions of "." and ".." records to be sure
2141 * they're in the correct place.
2142 *
2143 * Indexed directories don't need to proceed past the first
2144 * two entries, so we end the scan after seeing '..'. Despite
2145 * that, we allow the scan to proceed In the event that we
2146 * have a corrupted indexed directory (no dot or dot dot
2147 * entries). This allows us to double check for existing
2148 * entries which might not have been found in the index.
2149 */
2150 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2151 p->seen_dot = 1;
2152 return 0;
2153 }
2154
2155 if (name_len == 2 && !strncmp("..", name, 2) &&
2156 pos == OCFS2_DIR_REC_LEN(1)) {
2157 p->seen_dot_dot = 1;
2158
2159 if (p->dx_dir && p->seen_dot)
2160 return 1;
2161
2162 return 0;
2163 }
2164
2165 p->seen_other = 1;
2166 return 1;
2167 }
2168
2169 static int ocfs2_empty_dir_dx(struct inode *inode,
2170 struct ocfs2_empty_dir_priv *priv)
2171 {
2172 int ret;
2173 struct buffer_head *di_bh = NULL;
2174 struct buffer_head *dx_root_bh = NULL;
2175 struct ocfs2_dinode *di;
2176 struct ocfs2_dx_root_block *dx_root;
2177
2178 priv->dx_dir = 1;
2179
2180 ret = ocfs2_read_inode_block(inode, &di_bh);
2181 if (ret) {
2182 mlog_errno(ret);
2183 goto out;
2184 }
2185 di = (struct ocfs2_dinode *)di_bh->b_data;
2186
2187 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2188 if (ret) {
2189 mlog_errno(ret);
2190 goto out;
2191 }
2192 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2193
2194 if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2195 priv->seen_other = 1;
2196
2197 out:
2198 brelse(di_bh);
2199 brelse(dx_root_bh);
2200 return ret;
2201 }
2202
2203 /*
2204 * routine to check that the specified directory is empty (for rmdir)
2205 *
2206 * Returns 1 if dir is empty, zero otherwise.
2207 *
2208 * XXX: This is a performance problem for unindexed directories.
2209 */
2210 int ocfs2_empty_dir(struct inode *inode)
2211 {
2212 int ret;
2213 loff_t start = 0;
2214 struct ocfs2_empty_dir_priv priv;
2215
2216 memset(&priv, 0, sizeof(priv));
2217
2218 if (ocfs2_dir_indexed(inode)) {
2219 ret = ocfs2_empty_dir_dx(inode, &priv);
2220 if (ret)
2221 mlog_errno(ret);
2222 /*
2223 * We still run ocfs2_dir_foreach to get the checks
2224 * for "." and "..".
2225 */
2226 }
2227
2228 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
2229 if (ret)
2230 mlog_errno(ret);
2231
2232 if (!priv.seen_dot || !priv.seen_dot_dot) {
2233 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
2234 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2235 /*
2236 * XXX: Is it really safe to allow an unlink to continue?
2237 */
2238 return 1;
2239 }
2240
2241 return !priv.seen_other;
2242 }
2243
2244 /*
2245 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2246 * "..", which might be used during creation of a directory with a trailing
2247 * header. It is otherwise safe to ignore the return code.
2248 */
2249 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2250 struct inode *parent,
2251 char *start,
2252 unsigned int size)
2253 {
2254 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2255
2256 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2257 de->name_len = 1;
2258 de->rec_len =
2259 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2260 strcpy(de->name, ".");
2261 ocfs2_set_de_type(de, S_IFDIR);
2262
2263 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2264 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2265 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2266 de->name_len = 2;
2267 strcpy(de->name, "..");
2268 ocfs2_set_de_type(de, S_IFDIR);
2269
2270 return de;
2271 }
2272
2273 /*
2274 * This works together with code in ocfs2_mknod_locked() which sets
2275 * the inline-data flag and initializes the inline-data section.
2276 */
2277 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2278 handle_t *handle,
2279 struct inode *parent,
2280 struct inode *inode,
2281 struct buffer_head *di_bh)
2282 {
2283 int ret;
2284 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2285 struct ocfs2_inline_data *data = &di->id2.i_data;
2286 unsigned int size = le16_to_cpu(data->id_count);
2287
2288 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2289 OCFS2_JOURNAL_ACCESS_WRITE);
2290 if (ret) {
2291 mlog_errno(ret);
2292 goto out;
2293 }
2294
2295 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2296 ocfs2_journal_dirty(handle, di_bh);
2297
2298 i_size_write(inode, size);
2299 inode->i_nlink = 2;
2300 inode->i_blocks = ocfs2_inode_sector_count(inode);
2301
2302 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2303 if (ret < 0)
2304 mlog_errno(ret);
2305
2306 out:
2307 return ret;
2308 }
2309
2310 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2311 handle_t *handle,
2312 struct inode *parent,
2313 struct inode *inode,
2314 struct buffer_head *fe_bh,
2315 struct ocfs2_alloc_context *data_ac,
2316 struct buffer_head **ret_new_bh)
2317 {
2318 int status;
2319 unsigned int size = osb->sb->s_blocksize;
2320 struct buffer_head *new_bh = NULL;
2321 struct ocfs2_dir_entry *de;
2322
2323 if (ocfs2_new_dir_wants_trailer(inode))
2324 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2325
2326 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2327 data_ac, NULL, &new_bh);
2328 if (status < 0) {
2329 mlog_errno(status);
2330 goto bail;
2331 }
2332
2333 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2334
2335 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
2336 OCFS2_JOURNAL_ACCESS_CREATE);
2337 if (status < 0) {
2338 mlog_errno(status);
2339 goto bail;
2340 }
2341 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2342
2343 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2344 if (ocfs2_new_dir_wants_trailer(inode)) {
2345 int size = le16_to_cpu(de->rec_len);
2346
2347 /*
2348 * Figure out the size of the hole left over after
2349 * insertion of '.' and '..'. The trailer wants this
2350 * information.
2351 */
2352 size -= OCFS2_DIR_REC_LEN(2);
2353 size -= sizeof(struct ocfs2_dir_block_trailer);
2354
2355 ocfs2_init_dir_trailer(inode, new_bh, size);
2356 }
2357
2358 ocfs2_journal_dirty(handle, new_bh);
2359
2360 i_size_write(inode, inode->i_sb->s_blocksize);
2361 inode->i_nlink = 2;
2362 inode->i_blocks = ocfs2_inode_sector_count(inode);
2363 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2364 if (status < 0) {
2365 mlog_errno(status);
2366 goto bail;
2367 }
2368
2369 status = 0;
2370 if (ret_new_bh) {
2371 *ret_new_bh = new_bh;
2372 new_bh = NULL;
2373 }
2374 bail:
2375 brelse(new_bh);
2376
2377 return status;
2378 }
2379
2380 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2381 handle_t *handle, struct inode *dir,
2382 struct buffer_head *di_bh,
2383 struct buffer_head *dirdata_bh,
2384 struct ocfs2_alloc_context *meta_ac,
2385 int dx_inline, u32 num_entries,
2386 struct buffer_head **ret_dx_root_bh)
2387 {
2388 int ret;
2389 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2390 u16 dr_suballoc_bit;
2391 u64 suballoc_loc, dr_blkno;
2392 unsigned int num_bits;
2393 struct buffer_head *dx_root_bh = NULL;
2394 struct ocfs2_dx_root_block *dx_root;
2395 struct ocfs2_dir_block_trailer *trailer =
2396 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
2397
2398 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2399 &dr_suballoc_bit, &num_bits, &dr_blkno);
2400 if (ret) {
2401 mlog_errno(ret);
2402 goto out;
2403 }
2404
2405 mlog(0, "Dir %llu, attach new index block: %llu\n",
2406 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2407 (unsigned long long)dr_blkno);
2408
2409 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2410 if (dx_root_bh == NULL) {
2411 ret = -EIO;
2412 goto out;
2413 }
2414 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
2415
2416 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
2417 OCFS2_JOURNAL_ACCESS_CREATE);
2418 if (ret < 0) {
2419 mlog_errno(ret);
2420 goto out;
2421 }
2422
2423 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2424 memset(dx_root, 0, osb->sb->s_blocksize);
2425 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2426 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2427 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
2428 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2429 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2430 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2431 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
2432 dx_root->dr_num_entries = cpu_to_le32(num_entries);
2433 if (le16_to_cpu(trailer->db_free_rec_len))
2434 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2435 else
2436 dx_root->dr_free_blk = cpu_to_le64(0);
2437
2438 if (dx_inline) {
2439 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2440 dx_root->dr_entries.de_count =
2441 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2442 } else {
2443 dx_root->dr_list.l_count =
2444 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2445 }
2446 ocfs2_journal_dirty(handle, dx_root_bh);
2447
2448 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
2449 OCFS2_JOURNAL_ACCESS_CREATE);
2450 if (ret) {
2451 mlog_errno(ret);
2452 goto out;
2453 }
2454
2455 di->i_dx_root = cpu_to_le64(dr_blkno);
2456
2457 spin_lock(&OCFS2_I(dir)->ip_lock);
2458 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2459 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2460 spin_unlock(&OCFS2_I(dir)->ip_lock);
2461
2462 ocfs2_journal_dirty(handle, di_bh);
2463
2464 *ret_dx_root_bh = dx_root_bh;
2465 dx_root_bh = NULL;
2466
2467 out:
2468 brelse(dx_root_bh);
2469 return ret;
2470 }
2471
2472 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2473 handle_t *handle, struct inode *dir,
2474 struct buffer_head **dx_leaves,
2475 int num_dx_leaves, u64 start_blk)
2476 {
2477 int ret, i;
2478 struct ocfs2_dx_leaf *dx_leaf;
2479 struct buffer_head *bh;
2480
2481 for (i = 0; i < num_dx_leaves; i++) {
2482 bh = sb_getblk(osb->sb, start_blk + i);
2483 if (bh == NULL) {
2484 ret = -EIO;
2485 goto out;
2486 }
2487 dx_leaves[i] = bh;
2488
2489 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
2490
2491 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
2492 OCFS2_JOURNAL_ACCESS_CREATE);
2493 if (ret < 0) {
2494 mlog_errno(ret);
2495 goto out;
2496 }
2497
2498 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2499
2500 memset(dx_leaf, 0, osb->sb->s_blocksize);
2501 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2502 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2503 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2504 dx_leaf->dl_list.de_count =
2505 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2506
2507 mlog(0,
2508 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2509 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2510 (unsigned long long)bh->b_blocknr,
2511 le16_to_cpu(dx_leaf->dl_list.de_count));
2512
2513 ocfs2_journal_dirty(handle, bh);
2514 }
2515
2516 ret = 0;
2517 out:
2518 return ret;
2519 }
2520
2521 /*
2522 * Allocates and formats a new cluster for use in an indexed dir
2523 * leaf. This version will not do the extent insert, so that it can be
2524 * used by operations which need careful ordering.
2525 */
2526 static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2527 u32 cpos, handle_t *handle,
2528 struct ocfs2_alloc_context *data_ac,
2529 struct buffer_head **dx_leaves,
2530 int num_dx_leaves, u64 *ret_phys_blkno)
2531 {
2532 int ret;
2533 u32 phys, num;
2534 u64 phys_blkno;
2535 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2536
2537 /*
2538 * XXX: For create, this should claim cluster for the index
2539 * *before* the unindexed insert so that we have a better
2540 * chance of contiguousness as the directory grows in number
2541 * of entries.
2542 */
2543 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
2544 if (ret) {
2545 mlog_errno(ret);
2546 goto out;
2547 }
2548
2549 /*
2550 * Format the new cluster first. That way, we're inserting
2551 * valid data.
2552 */
2553 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2554 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2555 num_dx_leaves, phys_blkno);
2556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2559 }
2560
2561 *ret_phys_blkno = phys_blkno;
2562 out:
2563 return ret;
2564 }
2565
2566 static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2567 struct ocfs2_extent_tree *et,
2568 u32 cpos, handle_t *handle,
2569 struct ocfs2_alloc_context *data_ac,
2570 struct ocfs2_alloc_context *meta_ac,
2571 struct buffer_head **dx_leaves,
2572 int num_dx_leaves)
2573 {
2574 int ret;
2575 u64 phys_blkno;
2576
2577 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2578 num_dx_leaves, &phys_blkno);
2579 if (ret) {
2580 mlog_errno(ret);
2581 goto out;
2582 }
2583
2584 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
2585 meta_ac);
2586 if (ret)
2587 mlog_errno(ret);
2588 out:
2589 return ret;
2590 }
2591
2592 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2593 int *ret_num_leaves)
2594 {
2595 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2596 struct buffer_head **dx_leaves;
2597
2598 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2599 GFP_NOFS);
2600 if (dx_leaves && ret_num_leaves)
2601 *ret_num_leaves = num_dx_leaves;
2602
2603 return dx_leaves;
2604 }
2605
2606 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2607 handle_t *handle,
2608 struct inode *parent,
2609 struct inode *inode,
2610 struct buffer_head *di_bh,
2611 struct ocfs2_alloc_context *data_ac,
2612 struct ocfs2_alloc_context *meta_ac)
2613 {
2614 int ret;
2615 struct buffer_head *leaf_bh = NULL;
2616 struct buffer_head *dx_root_bh = NULL;
2617 struct ocfs2_dx_hinfo hinfo;
2618 struct ocfs2_dx_root_block *dx_root;
2619 struct ocfs2_dx_entry_list *entry_list;
2620
2621 /*
2622 * Our strategy is to create the directory as though it were
2623 * unindexed, then add the index block. This works with very
2624 * little complication since the state of a new directory is a
2625 * very well known quantity.
2626 *
2627 * Essentially, we have two dirents ("." and ".."), in the 1st
2628 * block which need indexing. These are easily inserted into
2629 * the index block.
2630 */
2631
2632 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2633 data_ac, &leaf_bh);
2634 if (ret) {
2635 mlog_errno(ret);
2636 goto out;
2637 }
2638
2639 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
2640 meta_ac, 1, 2, &dx_root_bh);
2641 if (ret) {
2642 mlog_errno(ret);
2643 goto out;
2644 }
2645 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2646 entry_list = &dx_root->dr_entries;
2647
2648 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2649 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
2650 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2651
2652 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
2653 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2654
2655 out:
2656 brelse(dx_root_bh);
2657 brelse(leaf_bh);
2658 return ret;
2659 }
2660
2661 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2662 handle_t *handle,
2663 struct inode *parent,
2664 struct inode *inode,
2665 struct buffer_head *fe_bh,
2666 struct ocfs2_alloc_context *data_ac,
2667 struct ocfs2_alloc_context *meta_ac)
2668
2669 {
2670 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2671
2672 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2673 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2674
2675 if (ocfs2_supports_indexed_dirs(osb))
2676 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2677 data_ac, meta_ac);
2678
2679 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2680 data_ac, NULL);
2681 }
2682
2683 static int ocfs2_dx_dir_index_block(struct inode *dir,
2684 handle_t *handle,
2685 struct buffer_head **dx_leaves,
2686 int num_dx_leaves,
2687 u32 *num_dx_entries,
2688 struct buffer_head *dirent_bh)
2689 {
2690 int ret = 0, namelen, i;
2691 char *de_buf, *limit;
2692 struct ocfs2_dir_entry *de;
2693 struct buffer_head *dx_leaf_bh;
2694 struct ocfs2_dx_hinfo hinfo;
2695 u64 dirent_blk = dirent_bh->b_blocknr;
2696
2697 de_buf = dirent_bh->b_data;
2698 limit = de_buf + dir->i_sb->s_blocksize;
2699
2700 while (de_buf < limit) {
2701 de = (struct ocfs2_dir_entry *)de_buf;
2702
2703 namelen = de->name_len;
2704 if (!namelen || !de->inode)
2705 goto inc;
2706
2707 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2708
2709 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2710 dx_leaf_bh = dx_leaves[i];
2711
2712 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2713 dirent_blk, dx_leaf_bh);
2714 if (ret) {
2715 mlog_errno(ret);
2716 goto out;
2717 }
2718
2719 *num_dx_entries = *num_dx_entries + 1;
2720
2721 inc:
2722 de_buf += le16_to_cpu(de->rec_len);
2723 }
2724
2725 out:
2726 return ret;
2727 }
2728
2729 /*
2730 * XXX: This expects dx_root_bh to already be part of the transaction.
2731 */
2732 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2733 struct buffer_head *dx_root_bh,
2734 struct buffer_head *dirent_bh)
2735 {
2736 char *de_buf, *limit;
2737 struct ocfs2_dx_root_block *dx_root;
2738 struct ocfs2_dir_entry *de;
2739 struct ocfs2_dx_hinfo hinfo;
2740 u64 dirent_blk = dirent_bh->b_blocknr;
2741
2742 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2743
2744 de_buf = dirent_bh->b_data;
2745 limit = de_buf + dir->i_sb->s_blocksize;
2746
2747 while (de_buf < limit) {
2748 de = (struct ocfs2_dir_entry *)de_buf;
2749
2750 if (!de->name_len || !de->inode)
2751 goto inc;
2752
2753 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2754
2755 mlog(0,
2756 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2757 (unsigned long long)dir->i_ino, hinfo.major_hash,
2758 hinfo.minor_hash,
2759 le16_to_cpu(dx_root->dr_entries.de_num_used),
2760 de->name_len, de->name);
2761
2762 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2763 dirent_blk);
2764
2765 le32_add_cpu(&dx_root->dr_num_entries, 1);
2766 inc:
2767 de_buf += le16_to_cpu(de->rec_len);
2768 }
2769 }
2770
2771 /*
2772 * Count the number of inline directory entries in di_bh and compare
2773 * them against the number of entries we can hold in an inline dx root
2774 * block.
2775 */
2776 static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2777 struct buffer_head *di_bh)
2778 {
2779 int dirent_count = 0;
2780 char *de_buf, *limit;
2781 struct ocfs2_dir_entry *de;
2782 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2783
2784 de_buf = di->id2.i_data.id_data;
2785 limit = de_buf + i_size_read(dir);
2786
2787 while (de_buf < limit) {
2788 de = (struct ocfs2_dir_entry *)de_buf;
2789
2790 if (de->name_len && de->inode)
2791 dirent_count++;
2792
2793 de_buf += le16_to_cpu(de->rec_len);
2794 }
2795
2796 /* We are careful to leave room for one extra record. */
2797 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2798 }
2799
2800 /*
2801 * Expand rec_len of the rightmost dirent in a directory block so that it
2802 * contains the end of our valid space for dirents. We do this during
2803 * expansion from an inline directory to one with extents. The first dir block
2804 * in that case is taken from the inline data portion of the inode block.
2805 *
2806 * This will also return the largest amount of contiguous space for a dirent
2807 * in the block. That value is *not* necessarily the last dirent, even after
2808 * expansion. The directory indexing code wants this value for free space
2809 * accounting. We do this here since we're already walking the entire dir
2810 * block.
2811 *
2812 * We add the dir trailer if this filesystem wants it.
2813 */
2814 static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2815 struct inode *dir)
2816 {
2817 struct super_block *sb = dir->i_sb;
2818 struct ocfs2_dir_entry *de;
2819 struct ocfs2_dir_entry *prev_de;
2820 char *de_buf, *limit;
2821 unsigned int new_size = sb->s_blocksize;
2822 unsigned int bytes, this_hole;
2823 unsigned int largest_hole = 0;
2824
2825 if (ocfs2_new_dir_wants_trailer(dir))
2826 new_size = ocfs2_dir_trailer_blk_off(sb);
2827
2828 bytes = new_size - old_size;
2829
2830 limit = start + old_size;
2831 de_buf = start;
2832 de = (struct ocfs2_dir_entry *)de_buf;
2833 do {
2834 this_hole = ocfs2_figure_dirent_hole(de);
2835 if (this_hole > largest_hole)
2836 largest_hole = this_hole;
2837
2838 prev_de = de;
2839 de_buf += le16_to_cpu(de->rec_len);
2840 de = (struct ocfs2_dir_entry *)de_buf;
2841 } while (de_buf < limit);
2842
2843 le16_add_cpu(&prev_de->rec_len, bytes);
2844
2845 /* We need to double check this after modification of the final
2846 * dirent. */
2847 this_hole = ocfs2_figure_dirent_hole(prev_de);
2848 if (this_hole > largest_hole)
2849 largest_hole = this_hole;
2850
2851 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2852 return largest_hole;
2853 return 0;
2854 }
2855
2856 /*
2857 * We allocate enough clusters to fulfill "blocks_wanted", but set
2858 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2859 * rest automatically for us.
2860 *
2861 * *first_block_bh is a pointer to the 1st data block allocated to the
2862 * directory.
2863 */
2864 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2865 unsigned int blocks_wanted,
2866 struct ocfs2_dir_lookup_result *lookup,
2867 struct buffer_head **first_block_bh)
2868 {
2869 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
2870 struct super_block *sb = dir->i_sb;
2871 int ret, i, num_dx_leaves = 0, dx_inline = 0,
2872 credits = ocfs2_inline_to_extents_credits(sb);
2873 u64 dx_insert_blkno, blkno,
2874 bytes = blocks_wanted << sb->s_blocksize_bits;
2875 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2876 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2877 struct ocfs2_alloc_context *data_ac;
2878 struct ocfs2_alloc_context *meta_ac = NULL;
2879 struct buffer_head *dirdata_bh = NULL;
2880 struct buffer_head *dx_root_bh = NULL;
2881 struct buffer_head **dx_leaves = NULL;
2882 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2883 handle_t *handle;
2884 struct ocfs2_extent_tree et;
2885 struct ocfs2_extent_tree dx_et;
2886 int did_quota = 0, bytes_allocated = 0;
2887
2888 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
2889
2890 alloc = ocfs2_clusters_for_bytes(sb, bytes);
2891 dx_alloc = 0;
2892
2893 down_write(&oi->ip_alloc_sem);
2894
2895 if (ocfs2_supports_indexed_dirs(osb)) {
2896 credits += ocfs2_add_dir_index_credits(sb);
2897
2898 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2899 if (!dx_inline) {
2900 /* Add one more cluster for an index leaf */
2901 dx_alloc++;
2902 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2903 &num_dx_leaves);
2904 if (!dx_leaves) {
2905 ret = -ENOMEM;
2906 mlog_errno(ret);
2907 goto out;
2908 }
2909 }
2910
2911 /* This gets us the dx_root */
2912 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2913 if (ret) {
2914 mlog_errno(ret);
2915 goto out;
2916 }
2917 }
2918
2919 /*
2920 * We should never need more than 2 clusters for the unindexed
2921 * tree - maximum dirent size is far less than one block. In
2922 * fact, the only time we'd need more than one cluster is if
2923 * blocksize == clustersize and the dirent won't fit in the
2924 * extra space that the expansion to a single block gives. As
2925 * of today, that only happens on 4k/4k file systems.
2926 */
2927 BUG_ON(alloc > 2);
2928
2929 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
2930 if (ret) {
2931 mlog_errno(ret);
2932 goto out;
2933 }
2934
2935 /*
2936 * Prepare for worst case allocation scenario of two separate
2937 * extents in the unindexed tree.
2938 */
2939 if (alloc == 2)
2940 credits += OCFS2_SUBALLOC_ALLOC;
2941
2942 handle = ocfs2_start_trans(osb, credits);
2943 if (IS_ERR(handle)) {
2944 ret = PTR_ERR(handle);
2945 mlog_errno(ret);
2946 goto out;
2947 }
2948
2949 ret = dquot_alloc_space_nodirty(dir,
2950 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2951 if (ret)
2952 goto out_commit;
2953 did_quota = 1;
2954
2955 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2956 /*
2957 * Allocate our index cluster first, to maximize the
2958 * possibility that unindexed leaves grow
2959 * contiguously.
2960 */
2961 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2962 dx_leaves, num_dx_leaves,
2963 &dx_insert_blkno);
2964 if (ret) {
2965 mlog_errno(ret);
2966 goto out_commit;
2967 }
2968 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2969 }
2970
2971 /*
2972 * Try to claim as many clusters as the bitmap can give though
2973 * if we only get one now, that's enough to continue. The rest
2974 * will be claimed after the conversion to extents.
2975 */
2976 if (ocfs2_dir_resv_allowed(osb))
2977 data_ac->ac_resv = &oi->ip_la_data_resv;
2978 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
2979 if (ret) {
2980 mlog_errno(ret);
2981 goto out_commit;
2982 }
2983 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2984
2985 /*
2986 * Operations are carefully ordered so that we set up the new
2987 * data block first. The conversion from inline data to
2988 * extents follows.
2989 */
2990 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2991 dirdata_bh = sb_getblk(sb, blkno);
2992 if (!dirdata_bh) {
2993 ret = -EIO;
2994 mlog_errno(ret);
2995 goto out_commit;
2996 }
2997
2998 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
2999
3000 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
3001 OCFS2_JOURNAL_ACCESS_CREATE);
3002 if (ret) {
3003 mlog_errno(ret);
3004 goto out_commit;
3005 }
3006
3007 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
3008 memset(dirdata_bh->b_data + i_size_read(dir), 0,
3009 sb->s_blocksize - i_size_read(dir));
3010 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
3011 if (ocfs2_new_dir_wants_trailer(dir)) {
3012 /*
3013 * Prepare the dir trailer up front. It will otherwise look
3014 * like a valid dirent. Even if inserting the index fails
3015 * (unlikely), then all we'll have done is given first dir
3016 * block a small amount of fragmentation.
3017 */
3018 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
3019 }
3020
3021 ocfs2_journal_dirty(handle, dirdata_bh);
3022
3023 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
3024 /*
3025 * Dx dirs with an external cluster need to do this up
3026 * front. Inline dx root's get handled later, after
3027 * we've allocated our root block. We get passed back
3028 * a total number of items so that dr_num_entries can
3029 * be correctly set once the dx_root has been
3030 * allocated.
3031 */
3032 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
3033 num_dx_leaves, &num_dx_entries,
3034 dirdata_bh);
3035 if (ret) {
3036 mlog_errno(ret);
3037 goto out_commit;
3038 }
3039 }
3040
3041 /*
3042 * Set extent, i_size, etc on the directory. After this, the
3043 * inode should contain the same exact dirents as before and
3044 * be fully accessible from system calls.
3045 *
3046 * We let the later dirent insert modify c/mtime - to the user
3047 * the data hasn't changed.
3048 */
3049 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
3050 OCFS2_JOURNAL_ACCESS_CREATE);
3051 if (ret) {
3052 mlog_errno(ret);
3053 goto out_commit;
3054 }
3055
3056 spin_lock(&oi->ip_lock);
3057 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
3058 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
3059 spin_unlock(&oi->ip_lock);
3060
3061 ocfs2_dinode_new_extent_list(dir, di);
3062
3063 i_size_write(dir, sb->s_blocksize);
3064 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3065
3066 di->i_size = cpu_to_le64(sb->s_blocksize);
3067 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
3068 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
3069
3070 /*
3071 * This should never fail as our extent list is empty and all
3072 * related blocks have been journaled already.
3073 */
3074 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
3075 0, NULL);
3076 if (ret) {
3077 mlog_errno(ret);
3078 goto out_commit;
3079 }
3080
3081 /*
3082 * Set i_blocks after the extent insert for the most up to
3083 * date ip_clusters value.
3084 */
3085 dir->i_blocks = ocfs2_inode_sector_count(dir);
3086
3087 ocfs2_journal_dirty(handle, di_bh);
3088
3089 if (ocfs2_supports_indexed_dirs(osb)) {
3090 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
3091 dirdata_bh, meta_ac, dx_inline,
3092 num_dx_entries, &dx_root_bh);
3093 if (ret) {
3094 mlog_errno(ret);
3095 goto out_commit;
3096 }
3097
3098 if (dx_inline) {
3099 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3100 dirdata_bh);
3101 } else {
3102 ocfs2_init_dx_root_extent_tree(&dx_et,
3103 INODE_CACHE(dir),
3104 dx_root_bh);
3105 ret = ocfs2_insert_extent(handle, &dx_et, 0,
3106 dx_insert_blkno, 1, 0, NULL);
3107 if (ret)
3108 mlog_errno(ret);
3109 }
3110 }
3111
3112 /*
3113 * We asked for two clusters, but only got one in the 1st
3114 * pass. Claim the 2nd cluster as a separate extent.
3115 */
3116 if (alloc > len) {
3117 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
3118 &len);
3119 if (ret) {
3120 mlog_errno(ret);
3121 goto out_commit;
3122 }
3123 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3124
3125 ret = ocfs2_insert_extent(handle, &et, 1,
3126 blkno, len, 0, NULL);
3127 if (ret) {
3128 mlog_errno(ret);
3129 goto out_commit;
3130 }
3131 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
3132 }
3133
3134 *first_block_bh = dirdata_bh;
3135 dirdata_bh = NULL;
3136 if (ocfs2_supports_indexed_dirs(osb)) {
3137 unsigned int off;
3138
3139 if (!dx_inline) {
3140 /*
3141 * We need to return the correct block within the
3142 * cluster which should hold our entry.
3143 */
3144 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
3145 &lookup->dl_hinfo);
3146 get_bh(dx_leaves[off]);
3147 lookup->dl_dx_leaf_bh = dx_leaves[off];
3148 }
3149 lookup->dl_dx_root_bh = dx_root_bh;
3150 dx_root_bh = NULL;
3151 }
3152
3153 out_commit:
3154 if (ret < 0 && did_quota)
3155 dquot_free_space_nodirty(dir, bytes_allocated);
3156
3157 ocfs2_commit_trans(osb, handle);
3158
3159 out:
3160 up_write(&oi->ip_alloc_sem);
3161 if (data_ac)
3162 ocfs2_free_alloc_context(data_ac);
3163 if (meta_ac)
3164 ocfs2_free_alloc_context(meta_ac);
3165
3166 if (dx_leaves) {
3167 for (i = 0; i < num_dx_leaves; i++)
3168 brelse(dx_leaves[i]);
3169 kfree(dx_leaves);
3170 }
3171
3172 brelse(dirdata_bh);
3173 brelse(dx_root_bh);
3174
3175 return ret;
3176 }
3177
3178 /* returns a bh of the 1st new block in the allocation. */
3179 static int ocfs2_do_extend_dir(struct super_block *sb,
3180 handle_t *handle,
3181 struct inode *dir,
3182 struct buffer_head *parent_fe_bh,
3183 struct ocfs2_alloc_context *data_ac,
3184 struct ocfs2_alloc_context *meta_ac,
3185 struct buffer_head **new_bh)
3186 {
3187 int status;
3188 int extend, did_quota = 0;
3189 u64 p_blkno, v_blkno;
3190
3191 spin_lock(&OCFS2_I(dir)->ip_lock);
3192 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3193 spin_unlock(&OCFS2_I(dir)->ip_lock);
3194
3195 if (extend) {
3196 u32 offset = OCFS2_I(dir)->ip_clusters;
3197
3198 status = dquot_alloc_space_nodirty(dir,
3199 ocfs2_clusters_to_bytes(sb, 1));
3200 if (status)
3201 goto bail;
3202 did_quota = 1;
3203
3204 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3205 1, 0, parent_fe_bh, handle,
3206 data_ac, meta_ac, NULL);
3207 BUG_ON(status == -EAGAIN);
3208 if (status < 0) {
3209 mlog_errno(status);
3210 goto bail;
3211 }
3212 }
3213
3214 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3215 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
3216 if (status < 0) {
3217 mlog_errno(status);
3218 goto bail;
3219 }
3220
3221 *new_bh = sb_getblk(sb, p_blkno);
3222 if (!*new_bh) {
3223 status = -EIO;
3224 mlog_errno(status);
3225 goto bail;
3226 }
3227 status = 0;
3228 bail:
3229 if (did_quota && status < 0)
3230 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
3231 return status;
3232 }
3233
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.
3241 *
3242 * If the directory is already indexed, dx_root_bh must be provided.
3243 */
3244 static int ocfs2_extend_dir(struct ocfs2_super *osb,
3245 struct inode *dir,
3246 struct buffer_head *parent_fe_bh,
3247 unsigned int blocks_wanted,
3248 struct ocfs2_dir_lookup_result *lookup,
3249 struct buffer_head **new_de_bh)
3250 {
3251 int status = 0;
3252 int credits, num_free_extents, drop_alloc_sem = 0;
3253 loff_t dir_i_size;
3254 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
3255 struct ocfs2_extent_list *el = &fe->id2.i_list;
3256 struct ocfs2_alloc_context *data_ac = NULL;
3257 struct ocfs2_alloc_context *meta_ac = NULL;
3258 handle_t *handle = NULL;
3259 struct buffer_head *new_bh = NULL;
3260 struct ocfs2_dir_entry * de;
3261 struct super_block *sb = osb->sb;
3262 struct ocfs2_extent_tree et;
3263 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
3264
3265 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
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
3272 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
3273 blocks_wanted, lookup,
3274 &new_bh);
3275 if (status) {
3276 mlog_errno(status);
3277 goto bail;
3278 }
3279
3280 /* Expansion from inline to an indexed directory will
3281 * have given us this. */
3282 dx_root_bh = lookup->dl_dx_root_bh;
3283
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
3303 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3304 drop_alloc_sem = 1;
3305 dir_i_size = i_size_read(dir);
3306 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3307 goto do_extend;
3308 }
3309
3310 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3311 drop_alloc_sem = 1;
3312 dir_i_size = i_size_read(dir);
3313 mlog(0, "extending dir %llu (i_size = %lld)\n",
3314 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
3315
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);
3320 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3321 parent_fe_bh);
3322 num_free_extents = ocfs2_num_free_extents(osb, &et);
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) {
3330 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
3331 if (status < 0) {
3332 if (status != -ENOSPC)
3333 mlog_errno(status);
3334 goto bail;
3335 }
3336 }
3337
3338 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
3339 if (status < 0) {
3340 if (status != -ENOSPC)
3341 mlog_errno(status);
3342 goto bail;
3343 }
3344
3345 if (ocfs2_dir_resv_allowed(osb))
3346 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
3347
3348 credits = ocfs2_calc_extend_credits(sb, el, 1);
3349 } else {
3350 spin_unlock(&OCFS2_I(dir)->ip_lock);
3351 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3352 }
3353
3354 do_extend:
3355 if (ocfs2_dir_indexed(dir))
3356 credits++; /* For attaching the new dirent block to the
3357 * dx_root */
3358
3359 handle = ocfs2_start_trans(osb, credits);
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
3374 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
3375
3376 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
3377 OCFS2_JOURNAL_ACCESS_CREATE);
3378 if (status < 0) {
3379 mlog_errno(status);
3380 goto bail;
3381 }
3382 memset(new_bh->b_data, 0, sb->s_blocksize);
3383
3384 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3385 de->inode = 0;
3386 if (ocfs2_supports_dir_trailer(dir)) {
3387 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
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 }
3399 } else {
3400 de->rec_len = cpu_to_le16(sb->s_blocksize);
3401 }
3402 ocfs2_journal_dirty(handle, new_bh);
3403
3404 dir_i_size += dir->i_sb->s_blocksize;
3405 i_size_write(dir, dir_i_size);
3406 dir->i_blocks = ocfs2_inode_sector_count(dir);
3407 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3408 if (status < 0) {
3409 mlog_errno(status);
3410 goto bail;
3411 }
3412
3413 bail_bh:
3414 *new_de_bh = new_bh;
3415 get_bh(*new_de_bh);
3416 bail:
3417 if (handle)
3418 ocfs2_commit_trans(osb, handle);
3419 if (drop_alloc_sem)
3420 up_write(&OCFS2_I(dir)->ip_alloc_sem);
3421
3422 if (data_ac)
3423 ocfs2_free_alloc_context(data_ac);
3424 if (meta_ac)
3425 ocfs2_free_alloc_context(meta_ac);
3426
3427 brelse(new_bh);
3428
3429 return status;
3430 }
3431
3432 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3433 const char *name, int namelen,
3434 struct buffer_head **ret_de_bh,
3435 unsigned int *blocks_wanted)
3436 {
3437 int ret;
3438 struct super_block *sb = dir->i_sb;
3439 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3440 struct ocfs2_dir_entry *de, *last_de = NULL;
3441 char *de_buf, *limit;
3442 unsigned long offset = 0;
3443 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3444
3445 /*
3446 * This calculates how many free bytes we'd have in block zero, should
3447 * this function force expansion to an extent tree.
3448 */
3449 if (ocfs2_new_dir_wants_trailer(dir))
3450 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3451 else
3452 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
3453
3454 de_buf = di->id2.i_data.id_data;
3455 limit = de_buf + i_size_read(dir);
3456 rec_len = OCFS2_DIR_REC_LEN(namelen);
3457
3458 while (de_buf < limit) {
3459 de = (struct ocfs2_dir_entry *)de_buf;
3460
3461 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3462 ret = -ENOENT;
3463 goto out;
3464 }
3465 if (ocfs2_match(namelen, name, de)) {
3466 ret = -EEXIST;
3467 goto out;
3468 }
3469 /*
3470 * No need to check for a trailing dirent record here as
3471 * they're not used for inline dirs.
3472 */
3473
3474 if (ocfs2_dirent_would_fit(de, rec_len)) {
3475 /* Ok, we found a spot. Return this bh and let
3476 * the caller actually fill it in. */
3477 *ret_de_bh = di_bh;
3478 get_bh(*ret_de_bh);
3479 ret = 0;
3480 goto out;
3481 }
3482
3483 last_de = de;
3484 de_buf += le16_to_cpu(de->rec_len);
3485 offset += le16_to_cpu(de->rec_len);
3486 }
3487
3488 /*
3489 * We're going to require expansion of the directory - figure
3490 * out how many blocks we'll need so that a place for the
3491 * dirent can be found.
3492 */
3493 *blocks_wanted = 1;
3494 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
3495 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3496 *blocks_wanted = 2;
3497
3498 ret = -ENOSPC;
3499 out:
3500 return ret;
3501 }
3502
3503 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3504 int namelen, struct buffer_head **ret_de_bh)
3505 {
3506 unsigned long offset;
3507 struct buffer_head *bh = NULL;
3508 unsigned short rec_len;
3509 struct ocfs2_dir_entry *de;
3510 struct super_block *sb = dir->i_sb;
3511 int status;
3512 int blocksize = dir->i_sb->s_blocksize;
3513
3514 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3515 if (status) {
3516 mlog_errno(status);
3517 goto bail;
3518 }
3519
3520 rec_len = OCFS2_DIR_REC_LEN(namelen);
3521 offset = 0;
3522 de = (struct ocfs2_dir_entry *) bh->b_data;
3523 while (1) {
3524 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3525 brelse(bh);
3526 bh = NULL;
3527
3528 if (i_size_read(dir) <= offset) {
3529 /*
3530 * Caller will have to expand this
3531 * directory.
3532 */
3533 status = -ENOSPC;
3534 goto bail;
3535 }
3536 status = ocfs2_read_dir_block(dir,
3537 offset >> sb->s_blocksize_bits,
3538 &bh, 0);
3539 if (status) {
3540 mlog_errno(status);
3541 goto bail;
3542 }
3543 /* move to next block */
3544 de = (struct ocfs2_dir_entry *) bh->b_data;
3545 }
3546 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3547 status = -ENOENT;
3548 goto bail;
3549 }
3550 if (ocfs2_match(namelen, name, de)) {
3551 status = -EEXIST;
3552 goto bail;
3553 }
3554
3555 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3556 blocksize))
3557 goto next;
3558
3559 if (ocfs2_dirent_would_fit(de, rec_len)) {
3560 /* Ok, we found a spot. Return this bh and let
3561 * the caller actually fill it in. */
3562 *ret_de_bh = bh;
3563 get_bh(*ret_de_bh);
3564 status = 0;
3565 goto bail;
3566 }
3567 next:
3568 offset += le16_to_cpu(de->rec_len);
3569 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3570 }
3571
3572 status = 0;
3573 bail:
3574 brelse(bh);
3575 if (status)
3576 mlog_errno(status);
3577
3578 return status;
3579 }
3580
3581 static int dx_leaf_sort_cmp(const void *a, const void *b)
3582 {
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
3605 static 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
3618 static 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 */
3642 static 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 */
3731 static 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
3775 static 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 */
3789 static 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
3812 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
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
3861 ret = dquot_alloc_space_nodirty(dir,
3862 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3863 if (ret)
3864 goto out_commit;
3865 did_quota = 1;
3866
3867 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
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
3881 ocfs2_journal_dirty(handle, dx_leaf_bh);
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
3918 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
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
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
3935 for (i = 0; i < num_dx_leaves; i++) {
3936 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3937 orig_dx_leaves[i],
3938 OCFS2_JOURNAL_ACCESS_WRITE);
3939 if (ret) {
3940 mlog_errno(ret);
3941 goto out_commit;
3942 }
3943
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 }
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
3956 out_commit:
3957 if (ret < 0 && did_quota)
3958 dquot_free_space_nodirty(dir,
3959 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3960
3961 ocfs2_commit_trans(osb, handle);
3962
3963 out:
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
3984 static 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
3999 restart_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
4052 out:
4053 brelse(dx_leaf_bh);
4054 return ret;
4055 }
4056
4057 static 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
4098 out:
4099
4100 brelse(leaf_bh);
4101 brelse(prev_leaf_bh);
4102 return ret;
4103 }
4104
4105 static 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
4140 ret = dquot_alloc_space_nodirty(dir,
4141 ocfs2_clusters_to_bytes(osb->sb, 1));
4142 if (ret)
4143 goto out_commit;
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 */
4151 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
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. */
4193 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4194 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4195 if (ret)
4196 mlog_errno(ret);
4197 did_quota = 0;
4198
4199 ocfs2_journal_dirty(handle, dx_root_bh);
4200
4201 out_commit:
4202 if (ret < 0 && did_quota)
4203 dquot_free_space_nodirty(dir,
4204 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4205
4206 ocfs2_commit_trans(osb, handle);
4207
4208 out:
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
4220 static 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
4235 static 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)
4240 {
4241 int ret, free_dx_root = 1;
4242 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4243 struct buffer_head *dx_root_bh = NULL;
4244 struct buffer_head *leaf_bh = NULL;
4245 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4246 struct ocfs2_dx_root_block *dx_root;
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;
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
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 }
4278
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);
4287 if (ret) {
4288 mlog_errno(ret);
4289 goto out;
4290 }
4291
4292 search_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) {
4301 mlog_errno(ret);
4302 goto out;
4303 }
4304
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;
4308
4309 if (ret == -ENOSPC) {
4310 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
4311
4312 if (ret) {
4313 mlog_errno(ret);
4314 goto out;
4315 }
4316
4317 /*
4318 * We make the assumption here that new leaf blocks are added
4319 * to the front of our free list.
4320 */
4321 lookup->dl_prev_leaf_bh = NULL;
4322 lookup->dl_leaf_bh = leaf_bh;
4323 }
4324
4325 out:
4326 if (free_dx_root)
4327 brelse(dx_root_bh);
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 */
4337 int 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;
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
4351 if (!namelen) {
4352 ret = -EINVAL;
4353 mlog_errno(ret);
4354 goto out;
4355 }
4356
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)) {
4372 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4373 name, namelen, lookup);
4374 if (ret)
4375 mlog_errno(ret);
4376 goto out;
4377 }
4378
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,
4397 lookup, &bh);
4398 if (ret) {
4399 if (ret != -ENOSPC)
4400 mlog_errno(ret);
4401 goto out;
4402 }
4403
4404 BUG_ON(!bh);
4405 }
4406
4407 lookup->dl_leaf_bh = bh;
4408 bh = NULL;
4409 out:
4410 brelse(bh);
4411 return ret;
4412 }
4413
4414 static 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
4454 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
4455 OCFS2_JOURNAL_ACCESS_WRITE);
4456 if (ret) {
4457 mlog_errno(ret);
4458 goto out_commit;
4459 }
4460
4461 spin_lock(&OCFS2_I(dir)->ip_lock);
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);
4464 spin_unlock(&OCFS2_I(dir)->ip_lock);
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);
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);
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
4480 out_commit:
4481 ocfs2_commit_trans(osb, handle);
4482
4483 out_unlock:
4484 ocfs2_inode_unlock(dx_alloc_inode, 1);
4485
4486 out_mutex:
4487 mutex_unlock(&dx_alloc_inode->i_mutex);
4488 brelse(dx_alloc_bh);
4489 out:
4490 iput(dx_alloc_inode);
4491 return ret;
4492 }
4493
4494 int 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 }
4517 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4518
4519 if (ocfs2_dx_root_inline(dx_root))
4520 goto remove_index;
4521
4522 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
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
4535 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
4536 &dealloc, 0);
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
4548 remove_index:
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
4555 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
4556 out:
4557 ocfs2_schedule_truncate_log_flush(osb, 1);
4558 ocfs2_run_deallocs(osb, &dealloc);
4559
4560 brelse(dx_root_bh);
4561 return ret;
4562 }
This page took 0.177764 seconds and 5 git commands to generate.