aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Whitehouse <swhiteho@redhat.com>2006-03-20 12:30:04 -0500
committerSteven Whitehouse <swhiteho@redhat.com>2006-03-20 12:30:04 -0500
commitc752666c17f870fa8ae9f16804dd457e9e6daaec (patch)
treec3c48383f386a24edbdf3c6292f25b587e6d9368
parent419c93e0b6b9eef0bf26b8ad415f2a5bf4300119 (diff)
[GFS2] Fix bug in directory code and tidy up
Due to a typo, the dir leaf split operation was (for the first split in a directory) writing the new hash vaules at the wrong offset. This is now fixed. Also some other tidy ups are included: - We use GFS2's hash function for dentries (see ops_dentry.c) so that we don't have to keep recalculating the hash values. - A lot of common code is eliminated between the various directory lookup routines. - Better error checking on directory lookup (previously different routines checked for different errors) - The leaf split operation has a couple of redundant operations removed from it, so it should be faster. There is still further scope for further clean ups in the directory code, and readdir in particular could do with slimming down a bit. Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>
-rw-r--r--fs/gfs2/dir.c1360
-rw-r--r--fs/gfs2/dir.h39
-rw-r--r--fs/gfs2/inode.c63
-rw-r--r--fs/gfs2/inode.h16
-rw-r--r--fs/gfs2/ondisk.c13
-rw-r--r--fs/gfs2/ops_dentry.c21
-rw-r--r--fs/gfs2/ops_export.c13
-rw-r--r--fs/gfs2/ops_fstype.c56
-rw-r--r--fs/gfs2/ops_inode.c72
-rw-r--r--fs/gfs2/super.c11
-rw-r--r--include/linux/gfs2_ondisk.h3
11 files changed, 683 insertions, 984 deletions
diff --git a/fs/gfs2/dir.c b/fs/gfs2/dir.c
index 37f70ca558cc..f31f163da1a1 100644
--- a/fs/gfs2/dir.c
+++ b/fs/gfs2/dir.c
@@ -130,7 +130,7 @@ static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
130 return error; 130 return error;
131 131
132 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 132 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_inode), buf, size); 133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
134 if (ip->i_di.di_size < offset + size) 134 if (ip->i_di.di_size < offset + size)
135 ip->i_di.di_size = offset + size; 135 ip->i_di.di_size = offset + size;
136 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); 136 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
@@ -177,7 +177,7 @@ static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
177 if (gfs2_is_stuffed(ip)) { 177 if (gfs2_is_stuffed(ip)) {
178 error = gfs2_unstuff_dinode(ip, NULL, NULL); 178 error = gfs2_unstuff_dinode(ip, NULL, NULL);
179 if (error) 179 if (error)
180 return error; 180 return error;
181 } 181 }
182 182
183 lblock = offset; 183 lblock = offset;
@@ -244,7 +244,7 @@ fail:
244} 244}
245 245
246static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf, 246static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
247 unsigned int offset, unsigned int size) 247 unsigned int offset, unsigned int size)
248{ 248{
249 struct buffer_head *dibh; 249 struct buffer_head *dibh;
250 int error; 250 int error;
@@ -343,26 +343,159 @@ fail:
343 return (copied) ? copied : error; 343 return (copied) ? copied : error;
344} 344}
345 345
346/** 346typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
347 * int gfs2_filecmp - Compare two filenames 347 const struct qstr *name);
348 * @file1: The first filename 348
349 * @file2: The second filename 349static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
350 * @len_of_file2: The length of the second file 350 const struct qstr *name, int ret)
351 * 351{
352 * This routine compares two filenames and returns 1 if they are equal. 352 if (dent->de_inum.no_addr != 0 &&
353 * 353 be32_to_cpu(dent->de_hash) == name->hash &&
354 * Returns: 1 if the files are the same, otherwise 0. 354 be16_to_cpu(dent->de_name_len) == name->len &&
355 memcmp((char *)(dent+1), name->name, name->len) == 0)
356 return ret;
357 return 0;
358}
359
360static int gfs2_dirent_find(const struct gfs2_dirent *dent,
361 const struct qstr *name)
362{
363 return __gfs2_dirent_find(dent, name, 1);
364}
365
366static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
367 const struct qstr *name)
368{
369 return __gfs2_dirent_find(dent, name, 2);
370}
371
372/*
373 * name->name holds ptr to start of block.
374 * name->len holds size of block.
355 */ 375 */
376static int gfs2_dirent_last(const struct gfs2_dirent *dent,
377 const struct qstr *name)
378{
379 const char *start = name->name;
380 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
381 if (name->len == (end - start))
382 return 1;
383 return 0;
384}
356 385
357int gfs2_filecmp(struct qstr *file1, char *file2, int len_of_file2) 386static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
387 const struct qstr *name)
358{ 388{
359 if (file1->len != len_of_file2) 389 unsigned required = GFS2_DIRENT_SIZE(name->len);
360 return 0; 390 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
361 if (memcmp(file1->name, file2, file1->len)) 391 unsigned totlen = be16_to_cpu(dent->de_rec_len);
362 return 0; 392
363 return 1; 393 if ((totlen - actual) >= required)
394 return 1;
395 return 0;
364} 396}
365 397
398/*
399 * Other possible things to check:
400 * - Inode located within filesystem size (and on valid block)
401 * - Valid directory entry type
402 * Not sure how heavy-weight we want to make this... could also check
403 * hash is correct for example, but that would take a lot of extra time.
404 * For now the most important thing is to check that the various sizes
405 * are correct.
406 */
407static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
408 unsigned int size, unsigned int len, int first)
409{
410 const char *msg = "gfs2_dirent too small";
411 if (unlikely(size < sizeof(struct gfs2_dirent)))
412 goto error;
413 msg = "gfs2_dirent misaligned";
414 if (unlikely(offset & 0x7))
415 goto error;
416 msg = "gfs2_dirent points beyond end of block";
417 if (unlikely(offset + size > len))
418 goto error;
419 msg = "zero inode number";
420 if (unlikely(!first && !dent->de_inum.no_addr))
421 goto error;
422 msg = "name length is greater than space in dirent";
423 if (dent->de_inum.no_addr &&
424 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
425 size))
426 goto error;
427 return 0;
428error:
429 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
430 first ? "first in block" : "not first in block");
431 return -EIO;
432}
433
434static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
435 void *buf,
436 unsigned int len, gfs2_dscan_t scan,
437 const struct qstr *name)
438{
439 struct gfs2_meta_header *h = buf;
440 struct gfs2_dirent *dent, *prev;
441 unsigned offset;
442 unsigned size;
443 int ret = 0;
444
445 BUG_ON(buf == NULL);
446 BUG_ON(name == NULL);
447
448 switch(be16_to_cpu(h->mh_type)) {
449 case GFS2_METATYPE_LF:
450 offset = sizeof(struct gfs2_leaf);
451 break;
452 case GFS2_METATYPE_DI:
453 offset = sizeof(struct gfs2_dinode);
454 break;
455 default:
456 goto wrong_type;
457 }
458
459 prev = NULL;
460 dent = (struct gfs2_dirent *)(buf + offset);
461 size = be16_to_cpu(dent->de_rec_len);
462 if (gfs2_check_dirent(dent, offset, size, len, 1))
463 goto consist_inode;
464 do {
465 ret = scan(dent, name);
466 if (ret)
467 break;
468 offset += size;
469 if (offset == len)
470 break;
471 prev = dent;
472 dent = (struct gfs2_dirent *)(buf + offset);
473 size = be16_to_cpu(dent->de_rec_len);
474 if (gfs2_check_dirent(dent, offset, size, len, 0))
475 goto consist_inode;
476 } while(1);
477
478 switch(ret) {
479 case 0:
480 return NULL;
481 case 1:
482 return dent;
483 case 2:
484 return prev ? prev : dent;
485 default:
486 BUG_ON(ret > 0);
487 return ERR_PTR(ret);
488 }
489
490wrong_type:
491 printk(KERN_WARNING "gfs2_scan_dirent: %p wrong block type %u\n", scan,
492 be16_to_cpu(h->mh_type));
493consist_inode:
494 gfs2_consist_inode(inode->u.generic_ip);
495 return ERR_PTR(-EIO);
496}
497
498
366/** 499/**
367 * dirent_first - Return the first dirent 500 * dirent_first - Return the first dirent
368 * @dip: the directory 501 * @dip: the directory
@@ -489,180 +622,39 @@ static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
489 prev->de_rec_len = cpu_to_be16(prev_rec_len); 622 prev->de_rec_len = cpu_to_be16(prev_rec_len);
490} 623}
491 624
492/** 625/*
493 * gfs2_dirent_alloc - Allocate a directory entry 626 * Takes a dent from which to grab space as an argument. Returns the
494 * @dip: The GFS2 inode 627 * newly created dent.
495 * @bh: The buffer
496 * @name_len: The length of the name
497 * @dent_out: Pointer to list of dirents
498 *
499 * Returns: 0 on success, error code otherwise
500 */ 628 */
501 629struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
502int gfs2_dirent_alloc(struct gfs2_inode *dip, struct buffer_head *bh, 630 struct gfs2_dirent *dent,
503 int name_len, struct gfs2_dirent **dent_out) 631 const struct qstr *name,
632 struct buffer_head *bh)
504{ 633{
505 struct gfs2_dirent *dent, *new; 634 struct gfs2_inode *ip = inode->u.generic_ip;
506 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len); 635 struct gfs2_dirent *ndent;
507 unsigned int entries = 0, offset = 0; 636 unsigned offset = 0, totlen;
508 int type; 637
509 638 if (dent->de_inum.no_addr)
510 type = dirent_first(dip, bh, &dent); 639 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
511 if (type < 0) 640 totlen = be16_to_cpu(dent->de_rec_len);
512 return type; 641 BUG_ON(offset + name->len > totlen);
513 642 gfs2_trans_add_bh(ip->i_gl, bh, 1);
514 if (type == IS_LEAF) { 643 ndent = (struct gfs2_dirent *)((char *)dent + offset);
515 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; 644 dent->de_rec_len = cpu_to_be16(offset);
516 entries = be16_to_cpu(leaf->lf_entries); 645 gfs2_qstr2dirent(name, totlen - offset, ndent);
517 offset = sizeof(struct gfs2_leaf); 646 return ndent;
518 } else {
519 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
520 entries = be32_to_cpu(dinode->di_entries);
521 offset = sizeof(struct gfs2_dinode);
522 }
523
524 if (!entries) {
525 if (dent->de_inum.no_addr) {
526 gfs2_consist_inode(dip);
527 return -EIO;
528 }
529
530 gfs2_trans_add_bh(dip->i_gl, bh, 1);
531
532 dent->de_rec_len = cpu_to_be16(bh->b_size - offset);
533 dent->de_name_len = cpu_to_be16(name_len);
534
535 *dent_out = dent;
536 return 0;
537 }
538
539 do {
540 uint16_t cur_rec_len;
541 uint16_t cur_name_len;
542
543 cur_rec_len = be16_to_cpu(dent->de_rec_len);
544 cur_name_len = be16_to_cpu(dent->de_name_len);
545
546 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
547 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len)) {
548 gfs2_trans_add_bh(dip->i_gl, bh, 1);
549
550 if (dent->de_inum.no_addr) {
551 new = (struct gfs2_dirent *)((char *)dent +
552 GFS2_DIRENT_SIZE(cur_name_len));
553 memset(new, 0, sizeof(struct gfs2_dirent));
554
555 new->de_rec_len = cpu_to_be16(cur_rec_len -
556 GFS2_DIRENT_SIZE(cur_name_len));
557 new->de_name_len = cpu_to_be16(name_len);
558
559 dent->de_rec_len = cpu_to_be16(cur_rec_len -
560 be16_to_cpu(new->de_rec_len));
561
562 *dent_out = new;
563 return 0;
564 }
565
566 dent->de_name_len = cpu_to_be16(name_len);
567
568 *dent_out = dent;
569 return 0;
570 }
571 } while (dirent_next(dip, bh, &dent) == 0);
572
573 return -ENOSPC;
574} 647}
575 648
576/** 649static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
577 * dirent_fits - See if we can fit a entry in this buffer 650 struct buffer_head *bh,
578 * @dip: The GFS2 inode 651 const struct qstr *name)
579 * @bh: The buffer
580 * @name_len: The length of the name
581 *
582 * Returns: 1 if it can fit, 0 otherwise
583 */
584
585static int dirent_fits(struct gfs2_inode *dip, struct buffer_head *bh,
586 int name_len)
587{ 652{
588 struct gfs2_dirent *dent; 653 struct gfs2_dirent *dent;
589 unsigned int rec_len = GFS2_DIRENT_SIZE(name_len); 654 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, gfs2_dirent_find_space, name);
590 unsigned int entries = 0; 655 if (!dent || IS_ERR(dent))
591 int type; 656 return dent;
592 657 return gfs2_init_dirent(inode, dent, name, bh);
593 type = dirent_first(dip, bh, &dent);
594 if (type < 0)
595 return type;
596
597 if (type == IS_LEAF) {
598 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
599 entries = be16_to_cpu(leaf->lf_entries);
600 } else {
601 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
602 entries = be32_to_cpu(dinode->di_entries);
603 }
604
605 if (!entries)
606 return 1;
607
608 do {
609 uint16_t cur_rec_len;
610 uint32_t cur_name_len;
611
612 cur_rec_len = be16_to_cpu(dent->de_rec_len);
613 cur_name_len = be16_to_cpu(dent->de_name_len);
614
615 if ((!dent->de_inum.no_addr && cur_rec_len >= rec_len) ||
616 (cur_rec_len >= GFS2_DIRENT_SIZE(cur_name_len) + rec_len))
617 return 1;
618 } while (dirent_next(dip, bh, &dent) == 0);
619
620 return 0;
621}
622
623static int leaf_search(struct gfs2_inode *dip, struct buffer_head *bh,
624 struct qstr *filename, struct gfs2_dirent **dent_out,
625 struct gfs2_dirent **dent_prev)
626{
627 uint32_t hash;
628 struct gfs2_dirent *dent, *prev = NULL;
629 unsigned int entries = 0;
630 int type;
631
632 type = dirent_first(dip, bh, &dent);
633 if (type < 0)
634 return type;
635
636 if (type == IS_LEAF) {
637 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
638 entries = be16_to_cpu(leaf->lf_entries);
639 } else if (type == IS_DINODE) {
640 struct gfs2_dinode *dinode = (struct gfs2_dinode *)bh->b_data;
641 entries = be32_to_cpu(dinode->di_entries);
642 }
643
644 hash = gfs2_disk_hash(filename->name, filename->len);
645
646 do {
647 if (!dent->de_inum.no_addr) {
648 prev = dent;
649 continue;
650 }
651
652 if (be32_to_cpu(dent->de_hash) == hash &&
653 gfs2_filecmp(filename, (char *)(dent + 1),
654 be16_to_cpu(dent->de_name_len))) {
655 *dent_out = dent;
656 if (dent_prev)
657 *dent_prev = prev;
658
659 return 0;
660 }
661
662 prev = dent;
663 } while (dirent_next(dip, bh, &dent) == 0);
664
665 return -ENOENT;
666} 658}
667 659
668static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no, 660static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
@@ -716,75 +708,81 @@ static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
716 return error; 708 return error;
717} 709}
718 710
719static int get_next_leaf(struct gfs2_inode *dip, struct buffer_head *bh_in, 711static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
720 struct buffer_head **bh_out) 712 const struct qstr *name,
713 gfs2_dscan_t scan,
714 struct buffer_head **pbh)
721{ 715{
722 struct gfs2_leaf *leaf; 716 struct buffer_head *bh;
723 int error; 717 struct gfs2_dirent *dent;
724 718 struct gfs2_inode *ip = inode->u.generic_ip;
725 leaf = (struct gfs2_leaf *)bh_in->b_data;
726
727 if (!leaf->lf_next)
728 error = -ENOENT;
729 else
730 error = get_leaf(dip, be64_to_cpu(leaf->lf_next), bh_out);
731
732 return error;
733}
734
735static int linked_leaf_search(struct gfs2_inode *dip, struct qstr *filename,
736 struct gfs2_dirent **dent_out,
737 struct gfs2_dirent **dent_prev,
738 struct buffer_head **bh_out)
739{
740 struct buffer_head *bh = NULL, *bh_next;
741 uint32_t hsize, index;
742 uint32_t hash;
743 int error; 719 int error;
744 720
745 hsize = 1 << dip->i_di.di_depth; 721 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
746 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) { 722 struct gfs2_leaf *leaf;
747 gfs2_consist_inode(dip); 723 unsigned hsize = 1 << ip->i_di.di_depth;
748 return -EIO; 724 unsigned index;
749 } 725 u64 ln;
750 726 if (hsize * sizeof(u64) != ip->i_di.di_size) {
751 /* Figure out the address of the leaf node. */ 727 gfs2_consist_inode(ip);
752 728 return ERR_PTR(-EIO);
753 hash = gfs2_disk_hash(filename->name, filename->len); 729 }
754 index = hash >> (32 - dip->i_di.di_depth);
755
756 error = get_first_leaf(dip, index, &bh_next);
757 if (error)
758 return error;
759
760 /* Find the entry */
761
762 do {
763 brelse(bh);
764
765 bh = bh_next;
766
767 error = leaf_search(dip, bh, filename, dent_out, dent_prev);
768 switch (error) {
769 case 0:
770 *bh_out = bh;
771 return 0;
772
773 case -ENOENT:
774 break;
775 730
776 default: 731 index = name->hash >> (32 - ip->i_di.di_depth);
732 error = get_first_leaf(ip, index, &bh);
733 if (error)
734 return ERR_PTR(error);
735 do {
736 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
737 scan, name);
738 if (dent)
739 goto got_dent;
740 leaf = (struct gfs2_leaf *)bh->b_data;
741 ln = be64_to_cpu(leaf->lf_next);
777 brelse(bh); 742 brelse(bh);
778 return error; 743 if (!ln)
779 } 744 break;
745 error = get_leaf(ip, ln, &bh);
746 } while(!error);
780 747
781 error = get_next_leaf(dip, bh, &bh_next); 748 return error ? ERR_PTR(error) : NULL;
782 } 749 }
783 while (!error);
784 750
785 brelse(bh); 751 error = gfs2_meta_inode_buffer(ip, &bh);
752 if (error)
753 return ERR_PTR(error);
754 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name);
755got_dent:
756 *pbh = bh;
757 return dent;
758}
786 759
787 return error; 760static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
761{
762 struct gfs2_inode *ip = inode->u.generic_ip;
763 u64 bn = gfs2_alloc_meta(ip);
764 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
765 struct gfs2_leaf *leaf;
766 struct gfs2_dirent *dent;
767 if (!bh)
768 return NULL;
769 gfs2_trans_add_bh(ip->i_gl, bh, 1);
770 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
771 leaf = (struct gfs2_leaf *)bh->b_data;
772 leaf->lf_depth = cpu_to_be16(depth);
773 leaf->lf_entries = cpu_to_be16(0);
774 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
775 leaf->lf_next = cpu_to_be64(0);
776 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
777 dent = (struct gfs2_dirent *)(leaf+1);
778 dent->de_inum.no_formal_ino = cpu_to_be64(0);
779 dent->de_inum.no_addr = cpu_to_be64(0);
780 dent->de_hash = cpu_to_be32(0);
781 dent->de_rec_len = cpu_to_be16(bh->b_size - sizeof(struct gfs2_leaf));
782 dent->de_name_len = cpu_to_be16(0);
783 dent->de_type = cpu_to_be16(0);
784 *pbh = bh;
785 return leaf;
788} 786}
789 787
790/** 788/**
@@ -794,10 +792,12 @@ static int linked_leaf_search(struct gfs2_inode *dip, struct qstr *filename,
794 * Returns: 0 on success, error code otherwise 792 * Returns: 0 on success, error code otherwise
795 */ 793 */
796 794
797static int dir_make_exhash(struct gfs2_inode *dip) 795static int dir_make_exhash(struct inode *inode)
798{ 796{
797 struct gfs2_inode *dip = inode->u.generic_ip;
799 struct gfs2_sbd *sdp = dip->i_sbd; 798 struct gfs2_sbd *sdp = dip->i_sbd;
800 struct gfs2_dirent *dent; 799 struct gfs2_dirent *dent;
800 struct qstr args;
801 struct buffer_head *bh, *dibh; 801 struct buffer_head *bh, *dibh;
802 struct gfs2_leaf *leaf; 802 struct gfs2_leaf *leaf;
803 int y; 803 int y;
@@ -809,24 +809,14 @@ static int dir_make_exhash(struct gfs2_inode *dip)
809 if (error) 809 if (error)
810 return error; 810 return error;
811 811
812 /* Allocate a new block for the first leaf node */
813
814 bn = gfs2_alloc_meta(dip);
815
816 /* Turn over a new leaf */ 812 /* Turn over a new leaf */
817 813
818 bh = gfs2_meta_new(dip->i_gl, bn); 814 leaf = new_leaf(inode, &bh, 0);
819 gfs2_trans_add_bh(dip->i_gl, bh, 1); 815 if (!leaf)
820 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); 816 return -ENOSPC;
821 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); 817 bn = bh->b_blocknr;
822
823 /* Fill in the leaf structure */
824
825 leaf = (struct gfs2_leaf *)bh->b_data;
826 818
827 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16)); 819 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
828
829 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
830 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries); 820 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
831 821
832 /* Copy dirents */ 822 /* Copy dirents */
@@ -837,15 +827,21 @@ static int dir_make_exhash(struct gfs2_inode *dip)
837 /* Find last entry */ 827 /* Find last entry */
838 828
839 x = 0; 829 x = 0;
840 dirent_first(dip, bh, &dent); 830 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
841 831 sizeof(struct gfs2_leaf);
842 do { 832 args.name = bh->b_data;
843 if (!dent->de_inum.no_addr) 833 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
844 continue; 834 gfs2_dirent_last, &args);
845 if (++x == dip->i_di.di_entries) 835 if (!dent) {
846 break; 836 brelse(bh);
837 brelse(dibh);
838 return -EIO;
839 }
840 if (IS_ERR(dent)) {
841 brelse(bh);
842 brelse(dibh);
843 return PTR_ERR(dent);
847 } 844 }
848 while (dirent_next(dip, bh, &dent) == 0);
849 845
850 /* Adjust the last dirent's record length 846 /* Adjust the last dirent's record length
851 (Remember that dent still points to the last entry.) */ 847 (Remember that dent still points to the last entry.) */
@@ -891,45 +887,39 @@ static int dir_make_exhash(struct gfs2_inode *dip)
891 * Returns: 0 on success, error code on failure 887 * Returns: 0 on success, error code on failure
892 */ 888 */
893 889
894static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index, 890static int dir_split_leaf(struct inode *inode, const struct qstr *name)
895 uint64_t leaf_no)
896{ 891{
892 struct gfs2_inode *dip = inode->u.generic_ip;
897 struct buffer_head *nbh, *obh, *dibh; 893 struct buffer_head *nbh, *obh, *dibh;
898 struct gfs2_leaf *nleaf, *oleaf; 894 struct gfs2_leaf *nleaf, *oleaf;
899 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new; 895 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
900 uint32_t start, len, half_len, divider; 896 uint32_t start, len, half_len, divider;
901 uint64_t bn, *lp; 897 uint64_t bn, *lp, leaf_no;
902 uint32_t name_len; 898 uint32_t index;
903 int x, moved = 0; 899 int x, moved = 0;
904 int error; 900 int error;
905 901
906 /* Allocate the new leaf block */ 902 index = name->hash >> (32 - dip->i_di.di_depth);
907 903 error = get_leaf_nr(dip, index, &leaf_no);
908 bn = gfs2_alloc_meta(dip); 904 if (error)
909 905 return error;
910 /* Get the new leaf block */
911
912 nbh = gfs2_meta_new(dip->i_gl, bn);
913 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
914 gfs2_metatype_set(nbh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
915 gfs2_buffer_clear_tail(nbh, sizeof(struct gfs2_meta_header));
916
917 nleaf = (struct gfs2_leaf *)nbh->b_data;
918
919 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
920 906
921 /* Get the old leaf block */ 907 /* Get the old leaf block */
922
923 error = get_leaf(dip, leaf_no, &obh); 908 error = get_leaf(dip, leaf_no, &obh);
924 if (error) 909 if (error)
925 goto fail; 910 goto fail;
926 911
927 gfs2_trans_add_bh(dip->i_gl, obh, 1); 912 gfs2_trans_add_bh(dip->i_gl, obh, 1);
928
929 oleaf = (struct gfs2_leaf *)obh->b_data; 913 oleaf = (struct gfs2_leaf *)obh->b_data;
930 914
931 /* Compute the start and len of leaf pointers in the hash table. */ 915 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
916 if (!nleaf) {
917 brelse(obh);
918 return -ENOSPC;
919 }
920 bn = nbh->b_blocknr;
932 921
922 /* Compute the start and len of leaf pointers in the hash table. */
933 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth)); 923 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
934 half_len = len >> 1; 924 half_len = len >> 1;
935 if (!half_len) { 925 if (!half_len) {
@@ -943,19 +933,8 @@ static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
943 /* Change the pointers. 933 /* Change the pointers.
944 Don't bother distinguishing stuffed from non-stuffed. 934 Don't bother distinguishing stuffed from non-stuffed.
945 This code is complicated enough already. */ 935 This code is complicated enough already. */
946 936 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
947 lp = kcalloc(half_len, sizeof(uint64_t), GFP_KERNEL | __GFP_NOFAIL);
948
949 error = gfs2_dir_read_data(dip, (char *)lp, start * sizeof(uint64_t),
950 half_len * sizeof(uint64_t));
951 if (error != half_len * sizeof(uint64_t)) {
952 if (error >= 0)
953 error = -EIO;
954 goto fail_lpfree;
955 }
956
957 /* Change the pointers */ 937 /* Change the pointers */
958
959 for (x = 0; x < half_len; x++) 938 for (x = 0; x < half_len; x++)
960 lp[x] = cpu_to_be64(bn); 939 lp[x] = cpu_to_be64(bn);
961 940
@@ -970,11 +949,9 @@ static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
970 kfree(lp); 949 kfree(lp);
971 950
972 /* Compute the divider */ 951 /* Compute the divider */
973
974 divider = (start + half_len) << (32 - dip->i_di.di_depth); 952 divider = (start + half_len) << (32 - dip->i_di.di_depth);
975 953
976 /* Copy the entries */ 954 /* Copy the entries */
977
978 dirent_first(dip, obh, &dent); 955 dirent_first(dip, obh, &dent);
979 956
980 do { 957 do {
@@ -984,48 +961,37 @@ static int dir_split_leaf(struct gfs2_inode *dip, uint32_t index,
984 961
985 if (dent->de_inum.no_addr && 962 if (dent->de_inum.no_addr &&
986 be32_to_cpu(dent->de_hash) < divider) { 963 be32_to_cpu(dent->de_hash) < divider) {
987 name_len = be16_to_cpu(dent->de_name_len); 964 struct qstr str;
988 965 str.name = (char*)(dent+1);
989 gfs2_dirent_alloc(dip, nbh, name_len, &new); 966 str.len = be16_to_cpu(dent->de_name_len);
967 str.hash = be32_to_cpu(dent->de_hash);
968 new = gfs2_dirent_alloc(dip->i_vnode, nbh, &str);
969 if (IS_ERR(new)) {
970 error = PTR_ERR(new);
971 break;
972 }
990 973
991 new->de_inum = dent->de_inum; /* No endian worries */ 974 new->de_inum = dent->de_inum; /* No endian worries */
992 new->de_hash = dent->de_hash; /* No endian worries */
993 new->de_type = dent->de_type; /* No endian worries */ 975 new->de_type = dent->de_type; /* No endian worries */
994 memcpy((char *)(new + 1), (char *)(dent + 1), 976 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
995 name_len);
996
997 nleaf->lf_entries = be16_to_cpu(nleaf->lf_entries)+1;
998 nleaf->lf_entries = cpu_to_be16(nleaf->lf_entries);
999 977
1000 dirent_del(dip, obh, prev, dent); 978 dirent_del(dip, obh, prev, dent);
1001 979
1002 if (!oleaf->lf_entries) 980 if (!oleaf->lf_entries)
1003 gfs2_consist_inode(dip); 981 gfs2_consist_inode(dip);
1004 oleaf->lf_entries = be16_to_cpu(oleaf->lf_entries)-1; 982 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1005 oleaf->lf_entries = cpu_to_be16(oleaf->lf_entries);
1006 983
1007 if (!prev) 984 if (!prev)
1008 prev = dent; 985 prev = dent;
1009 986
1010 moved = 1; 987 moved = 1;
1011 } else 988 } else {
1012 prev = dent; 989 prev = dent;
1013 990 }
1014 dent = next; 991 dent = next;
1015 } 992 } while (dent);
1016 while (dent);
1017
1018 /* If none of the entries got moved into the new leaf,
1019 artificially fill in the first entry. */
1020 993
1021 if (!moved) { 994 oleaf->lf_depth = nleaf->lf_depth;
1022 gfs2_dirent_alloc(dip, nbh, 0, &new);
1023 new->de_inum.no_addr = 0;
1024 }
1025
1026 oleaf->lf_depth = be16_to_cpu(oleaf->lf_depth) + 1;
1027 oleaf->lf_depth = cpu_to_be16(oleaf->lf_depth);
1028 nleaf->lf_depth = oleaf->lf_depth;
1029 995
1030 error = gfs2_meta_inode_buffer(dip, &dibh); 996 error = gfs2_meta_inode_buffer(dip, &dibh);
1031 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) { 997 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
@@ -1142,12 +1108,10 @@ static int compare_dents(const void *a, const void *b)
1142 int ret = 0; 1108 int ret = 0;
1143 1109
1144 dent_a = *(struct gfs2_dirent **)a; 1110 dent_a = *(struct gfs2_dirent **)a;
1145 hash_a = dent_a->de_hash; 1111 hash_a = be32_to_cpu(dent_a->de_hash);
1146 hash_a = be32_to_cpu(hash_a);
1147 1112
1148 dent_b = *(struct gfs2_dirent **)b; 1113 dent_b = *(struct gfs2_dirent **)b;
1149 hash_b = dent_b->de_hash; 1114 hash_b = be32_to_cpu(dent_b->de_hash);
1150 hash_b = be32_to_cpu(hash_b);
1151 1115
1152 if (hash_a > hash_b) 1116 if (hash_a > hash_b)
1153 ret = 1; 1117 ret = 1;
@@ -1292,8 +1256,7 @@ static int do_filldir_single(struct gfs2_inode *dip, uint64_t *offset,
1292 goto out; 1256 goto out;
1293 } 1257 }
1294 darr[e++] = de; 1258 darr[e++] = de;
1295 } 1259 } while (dirent_next(dip, bh, &de) == 0);
1296 while (dirent_next(dip, bh, &de) == 0);
1297 1260
1298 if (e != entries) { 1261 if (e != entries) {
1299 gfs2_consist_inode(dip); 1262 gfs2_consist_inode(dip);
@@ -1341,11 +1304,9 @@ static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1341 1304
1342 leaf = (struct gfs2_leaf *)bh->b_data; 1305 leaf = (struct gfs2_leaf *)bh->b_data;
1343 entries = be16_to_cpu(leaf->lf_entries); 1306 entries = be16_to_cpu(leaf->lf_entries);
1344 ln = leaf->lf_next; 1307 ln = be64_to_cpu(leaf->lf_next);
1345 1308
1346 while (ln) { 1309 while (ln) {
1347 ln = be64_to_cpu(ln);
1348
1349 error = get_leaf(dip, ln, &tmp_bh); 1310 error = get_leaf(dip, ln, &tmp_bh);
1350 if (error) 1311 if (error)
1351 return error; 1312 return error;
@@ -1355,7 +1316,7 @@ static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1355 entries += be16_to_cpu(leaf->lf_entries); 1316 entries += be16_to_cpu(leaf->lf_entries);
1356 leaves++; 1317 leaves++;
1357 } 1318 }
1358 ln = leaf->lf_next; 1319 ln = be64_to_cpu(leaf->lf_next);
1359 1320
1360 brelse(tmp_bh); 1321 brelse(tmp_bh);
1361 } 1322 }
@@ -1387,14 +1348,11 @@ static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1387 goto out; 1348 goto out;
1388 } 1349 }
1389 darr[e++] = de; 1350 darr[e++] = de;
1390 } 1351 } while (dirent_next(dip, bh, &de) == 0);
1391 while (dirent_next(dip, bh, &de) == 0);
1392 } 1352 }
1393 ln = leaf->lf_next; 1353 ln = be64_to_cpu(leaf->lf_next);
1394 1354
1395 while (ln) { 1355 while (ln) {
1396 ln = be64_to_cpu(ln);
1397
1398 error = get_leaf(dip, ln, &tmp_bh); 1356 error = get_leaf(dip, ln, &tmp_bh);
1399 if (error) 1357 if (error)
1400 goto out; 1358 goto out;
@@ -1411,14 +1369,13 @@ static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1411 goto out; 1369 goto out;
1412 } 1370 }
1413 darr[e++] = de; 1371 darr[e++] = de;
1414 } 1372 } while (dirent_next(dip, tmp_bh, &de) == 0);
1415 while (dirent_next(dip, tmp_bh, &de) == 0);
1416 1373
1417 larr[l++] = tmp_bh; 1374 larr[l++] = tmp_bh;
1418 1375
1419 ln = leaf->lf_next; 1376 ln = be64_to_cpu(leaf->lf_next);
1420 } else { 1377 } else {
1421 ln = leaf->lf_next; 1378 ln = be64_to_cpu(leaf->lf_next);
1422 brelse(tmp_bh); 1379 brelse(tmp_bh);
1423 } 1380 }
1424 } 1381 }
@@ -1446,207 +1403,6 @@ static int do_filldir_multi(struct gfs2_inode *dip, uint64_t *offset,
1446} 1403}
1447 1404
1448/** 1405/**
1449 * dir_e_search - Search exhash (leaf) dir for inode matching name
1450 * @dip: The GFS2 inode
1451 * @filename: Filename string
1452 * @inode: If non-NULL, function fills with formal inode # and block address
1453 * @type: If non-NULL, function fills with DT_... dinode type
1454 *
1455 * Returns:
1456 */
1457
1458static int dir_e_search(struct gfs2_inode *dip, struct qstr *filename,
1459 struct gfs2_inum *inum, unsigned int *type)
1460{
1461 struct buffer_head *bh;
1462 struct gfs2_dirent *dent;
1463 int error;
1464
1465 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1466 if (error)
1467 return error;
1468
1469 if (inum)
1470 gfs2_inum_in(inum, (char *)&dent->de_inum);
1471 if (type)
1472 *type = be16_to_cpu(dent->de_type);
1473
1474 brelse(bh);
1475
1476 return 0;
1477}
1478
1479static int dir_e_add(struct gfs2_inode *dip, struct qstr *filename,
1480 struct gfs2_inum *inum, unsigned int type)
1481{
1482 struct buffer_head *bh, *nbh, *dibh;
1483 struct gfs2_leaf *leaf, *nleaf;
1484 struct gfs2_dirent *dent;
1485 uint32_t hsize, index;
1486 uint32_t hash;
1487 uint64_t leaf_no, bn;
1488 int error;
1489
1490 restart:
1491 hsize = 1 << dip->i_di.di_depth;
1492 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1493 gfs2_consist_inode(dip);
1494 return -EIO;
1495 }
1496
1497 /* Figure out the address of the leaf node. */
1498
1499 hash = gfs2_disk_hash(filename->name, filename->len);
1500 index = hash >> (32 - dip->i_di.di_depth);
1501
1502 error = get_leaf_nr(dip, index, &leaf_no);
1503 if (error)
1504 return error;
1505
1506 /* Add entry to the leaf */
1507
1508 for (;;) {
1509 error = get_leaf(dip, leaf_no, &bh);
1510 if (error)
1511 return error;
1512
1513 leaf = (struct gfs2_leaf *)bh->b_data;
1514
1515 if (gfs2_dirent_alloc(dip, bh, filename->len, &dent)) {
1516
1517 if (be16_to_cpu(leaf->lf_depth) < dip->i_di.di_depth) {
1518 /* Can we split the leaf? */
1519
1520 brelse(bh);
1521
1522 error = dir_split_leaf(dip, index, leaf_no);
1523 if (error)
1524 return error;
1525
1526 goto restart;
1527
1528 } else if (dip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1529 /* Can we double the hash table? */
1530
1531 brelse(bh);
1532
1533 error = dir_double_exhash(dip);
1534 if (error)
1535 return error;
1536
1537 goto restart;
1538
1539 } else if (leaf->lf_next) {
1540 /* Can we try the next leaf in the list? */
1541 leaf_no = be64_to_cpu(leaf->lf_next);
1542 brelse(bh);
1543 continue;
1544
1545 } else {
1546 /* Create a new leaf and add it to the list. */
1547
1548 bn = gfs2_alloc_meta(dip);
1549
1550 nbh = gfs2_meta_new(dip->i_gl, bn);
1551 gfs2_trans_add_bh(dip->i_gl, nbh, 1);
1552 gfs2_metatype_set(nbh,
1553 GFS2_METATYPE_LF,
1554 GFS2_FORMAT_LF);
1555 gfs2_buffer_clear_tail(nbh,
1556 sizeof(struct gfs2_meta_header));
1557
1558 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1559 leaf->lf_next = cpu_to_be64(bn);
1560
1561 nleaf = (struct gfs2_leaf *)nbh->b_data;
1562 nleaf->lf_depth = leaf->lf_depth;
1563 nleaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
1564
1565 gfs2_dirent_alloc(dip, nbh, filename->len,
1566 &dent);
1567
1568 dip->i_di.di_blocks++;
1569
1570 brelse(bh);
1571
1572 bh = nbh;
1573 leaf = nleaf;
1574 }
1575 }
1576
1577 /* If the gfs2_dirent_alloc() succeeded, it pinned the "bh" */
1578
1579 gfs2_inum_out(inum, (char *)&dent->de_inum);
1580 dent->de_hash = cpu_to_be32(hash);
1581 dent->de_type = cpu_to_be16(type);
1582 memcpy((char *)(dent + 1), filename->name, filename->len);
1583
1584 leaf->lf_entries = be16_to_cpu(leaf->lf_entries) + 1;
1585 leaf->lf_entries = cpu_to_be16(leaf->lf_entries);
1586
1587 brelse(bh);
1588
1589 error = gfs2_meta_inode_buffer(dip, &dibh);
1590 if (error)
1591 return error;
1592
1593 dip->i_di.di_entries++;
1594 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1595
1596 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1597 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1598 brelse(dibh);
1599
1600 return 0;
1601 }
1602
1603 return -ENOENT;
1604}
1605
1606static int dir_e_del(struct gfs2_inode *dip, struct qstr *filename)
1607{
1608 struct buffer_head *bh, *dibh;
1609 struct gfs2_dirent *dent, *prev;
1610 struct gfs2_leaf *leaf;
1611 unsigned int entries;
1612 int error;
1613
1614 error = linked_leaf_search(dip, filename, &dent, &prev, &bh);
1615 if (error == -ENOENT) {
1616 gfs2_consist_inode(dip);
1617 return -EIO;
1618 }
1619 if (error)
1620 return error;
1621
1622 dirent_del(dip, bh, prev, dent); /* Pins bh */
1623
1624 leaf = (struct gfs2_leaf *)bh->b_data;
1625 entries = be16_to_cpu(leaf->lf_entries);
1626 if (!entries)
1627 gfs2_consist_inode(dip);
1628 entries--;
1629 leaf->lf_entries = cpu_to_be16(entries);
1630
1631 brelse(bh);
1632
1633 error = gfs2_meta_inode_buffer(dip, &dibh);
1634 if (error)
1635 return error;
1636
1637 if (!dip->i_di.di_entries)
1638 gfs2_consist_inode(dip);
1639 dip->i_di.di_entries--;
1640 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1641
1642 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1643 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1644 brelse(dibh);
1645
1646 return 0;
1647}
1648
1649/**
1650 * dir_e_read - Reads the entries from a directory into a filldir buffer 1406 * dir_e_read - Reads the entries from a directory into a filldir buffer
1651 * @dip: dinode pointer 1407 * @dip: dinode pointer
1652 * @offset: the hash of the last entry read shifted to the right once 1408 * @offset: the hash of the last entry read shifted to the right once
@@ -1661,7 +1417,7 @@ static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1661{ 1417{
1662 struct gfs2_sbd *sdp = dip->i_sbd; 1418 struct gfs2_sbd *sdp = dip->i_sbd;
1663 struct buffer_head *bh; 1419 struct buffer_head *bh;
1664 struct gfs2_leaf leaf; 1420 struct gfs2_leaf *leaf;
1665 uint32_t hsize, len; 1421 uint32_t hsize, len;
1666 uint32_t ht_offset, lp_offset, ht_offset_cur = -1; 1422 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1667 uint32_t hash, index; 1423 uint32_t hash, index;
@@ -1702,14 +1458,15 @@ static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1702 if (error) 1458 if (error)
1703 goto out; 1459 goto out;
1704 1460
1705 gfs2_leaf_in(&leaf, bh->b_data); 1461 leaf = (struct gfs2_leaf *)bh->b_data;
1706 1462 if (leaf->lf_next)
1707 if (leaf.lf_next)
1708 error = do_filldir_multi(dip, offset, opaque, filldir, 1463 error = do_filldir_multi(dip, offset, opaque, filldir,
1709 bh, &copied); 1464 bh, &copied);
1710 else 1465 else
1711 error = do_filldir_single(dip, offset, opaque, filldir, 1466 error = do_filldir_single(dip, offset, opaque, filldir,
1712 bh, leaf.lf_entries, &copied); 1467 bh,
1468 be16_to_cpu(leaf->lf_entries),
1469 &copied);
1713 1470
1714 brelse(bh); 1471 brelse(bh);
1715 1472
@@ -1719,7 +1476,7 @@ static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1719 goto out; 1476 goto out;
1720 } 1477 }
1721 1478
1722 len = 1 << (dip->i_di.di_depth - leaf.lf_depth); 1479 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1723 index = (index & ~(len - 1)) + len; 1480 index = (index & ~(len - 1)) + len;
1724 } 1481 }
1725 1482
@@ -1729,165 +1486,6 @@ static int dir_e_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1729 return error; 1486 return error;
1730} 1487}
1731 1488
1732static int dir_e_mvino(struct gfs2_inode *dip, struct qstr *filename,
1733 struct gfs2_inum *inum, unsigned int new_type)
1734{
1735 struct buffer_head *bh, *dibh;
1736 struct gfs2_dirent *dent;
1737 int error;
1738
1739 error = linked_leaf_search(dip, filename, &dent, NULL, &bh);
1740 if (error == -ENOENT) {
1741 gfs2_consist_inode(dip);
1742 return -EIO;
1743 }
1744 if (error)
1745 return error;
1746
1747 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1748
1749 gfs2_inum_out(inum, (char *)&dent->de_inum);
1750 dent->de_type = cpu_to_be16(new_type);
1751
1752 brelse(bh);
1753
1754 error = gfs2_meta_inode_buffer(dip, &dibh);
1755 if (error)
1756 return error;
1757
1758 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1759
1760 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1761 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1762 brelse(dibh);
1763
1764 return 0;
1765}
1766
1767/**
1768 * dir_l_search - Search linear (stuffed dinode) dir for inode matching name
1769 * @dip: The GFS2 inode
1770 * @filename: Filename string
1771 * @inode: If non-NULL, function fills with formal inode # and block address
1772 * @type: If non-NULL, function fills with DT_... dinode type
1773 *
1774 * Returns:
1775 */
1776
1777static int dir_l_search(struct gfs2_inode *dip, struct qstr *filename,
1778 struct gfs2_inum *inum, unsigned int *type)
1779{
1780 struct buffer_head *dibh;
1781 struct gfs2_dirent *dent;
1782 int error;
1783
1784 if (!gfs2_is_stuffed(dip)) {
1785 gfs2_consist_inode(dip);
1786 return -EIO;
1787 }
1788
1789 error = gfs2_meta_inode_buffer(dip, &dibh);
1790 if (error)
1791 return error;
1792
1793 error = leaf_search(dip, dibh, filename, &dent, NULL);
1794 if (!error) {
1795 if (inum)
1796 gfs2_inum_in(inum, (char *)&dent->de_inum);
1797 if (type)
1798 *type = be16_to_cpu(dent->de_type);
1799 }
1800
1801 brelse(dibh);
1802
1803 return error;
1804}
1805
1806static int dir_l_add(struct gfs2_inode *dip, struct qstr *filename,
1807 struct gfs2_inum *inum, unsigned int type)
1808{
1809 struct buffer_head *dibh;
1810 struct gfs2_dirent *dent;
1811 int error;
1812
1813 if (!gfs2_is_stuffed(dip)) {
1814 gfs2_consist_inode(dip);
1815 return -EIO;
1816 }
1817
1818 error = gfs2_meta_inode_buffer(dip, &dibh);
1819 if (error)
1820 return error;
1821
1822 if (gfs2_dirent_alloc(dip, dibh, filename->len, &dent)) {
1823 brelse(dibh);
1824
1825 error = dir_make_exhash(dip);
1826 if (!error)
1827 error = dir_e_add(dip, filename, inum, type);
1828
1829 return error;
1830 }
1831
1832 /* gfs2_dirent_alloc() pins */
1833
1834 gfs2_inum_out(inum, (char *)&dent->de_inum);
1835 dent->de_hash = gfs2_disk_hash(filename->name, filename->len);
1836 dent->de_hash = cpu_to_be32(dent->de_hash);
1837 dent->de_type = cpu_to_be16(type);
1838 memcpy((char *)(dent + 1), filename->name, filename->len);
1839
1840 dip->i_di.di_entries++;
1841 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1842
1843 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1844 brelse(dibh);
1845
1846 return 0;
1847}
1848
1849static int dir_l_del(struct gfs2_inode *dip, struct qstr *filename)
1850{
1851 struct buffer_head *dibh;
1852 struct gfs2_dirent *dent, *prev;
1853 int error;
1854
1855 if (!gfs2_is_stuffed(dip)) {
1856 gfs2_consist_inode(dip);
1857 return -EIO;
1858 }
1859
1860 error = gfs2_meta_inode_buffer(dip, &dibh);
1861 if (error)
1862 return error;
1863
1864 error = leaf_search(dip, dibh, filename, &dent, &prev);
1865 if (error == -ENOENT) {
1866 gfs2_consist_inode(dip);
1867 error = -EIO;
1868 goto out;
1869 }
1870 if (error)
1871 goto out;
1872
1873 dirent_del(dip, dibh, prev, dent);
1874
1875 /* dirent_del() pins */
1876
1877 if (!dip->i_di.di_entries)
1878 gfs2_consist_inode(dip);
1879 dip->i_di.di_entries--;
1880
1881 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1882
1883 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1884
1885 out:
1886 brelse(dibh);
1887
1888 return error;
1889}
1890
1891static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque, 1489static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1892 gfs2_filldir_t filldir) 1490 gfs2_filldir_t filldir)
1893{ 1491{
@@ -1919,46 +1517,6 @@ static int dir_l_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
1919 return error; 1517 return error;
1920} 1518}
1921 1519
1922static int dir_l_mvino(struct gfs2_inode *dip, struct qstr *filename,
1923 struct gfs2_inum *inum, unsigned int new_type)
1924{
1925 struct buffer_head *dibh;
1926 struct gfs2_dirent *dent;
1927 int error;
1928
1929 if (!gfs2_is_stuffed(dip)) {
1930 gfs2_consist_inode(dip);
1931 return -EIO;
1932 }
1933
1934 error = gfs2_meta_inode_buffer(dip, &dibh);
1935 if (error)
1936 return error;
1937
1938 error = leaf_search(dip, dibh, filename, &dent, NULL);
1939 if (error == -ENOENT) {
1940 gfs2_consist_inode(dip);
1941 error = -EIO;
1942 goto out;
1943 }
1944 if (error)
1945 goto out;
1946
1947 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1948
1949 gfs2_inum_out(inum, (char *)&dent->de_inum);
1950 dent->de_type = cpu_to_be16(new_type);
1951
1952 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1953
1954 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1955
1956 out:
1957 brelse(dibh);
1958
1959 return error;
1960}
1961
1962/** 1520/**
1963 * gfs2_dir_search - Search a directory 1521 * gfs2_dir_search - Search a directory
1964 * @dip: The GFS2 inode 1522 * @dip: The GFS2 inode
@@ -1971,17 +1529,69 @@ static int dir_l_mvino(struct gfs2_inode *dip, struct qstr *filename,
1971 * Returns: errno 1529 * Returns: errno
1972 */ 1530 */
1973 1531
1974int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename, 1532int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1975 struct gfs2_inum *inum, unsigned int *type) 1533 struct gfs2_inum *inum, unsigned int *type)
1976{ 1534{
1535 struct buffer_head *bh;
1536 struct gfs2_dirent *dent;
1537
1538 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1539 if (dent) {
1540 if (IS_ERR(dent))
1541 return PTR_ERR(dent);
1542 if (inum)
1543 gfs2_inum_in(inum, (char *)&dent->de_inum);
1544 if (type)
1545 *type = be16_to_cpu(dent->de_type);
1546 brelse(bh);
1547 return 0;
1548 }
1549 return -ENOENT;
1550}
1551
1552static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1553{
1554 struct buffer_head *bh, *obh;
1555 struct gfs2_inode *ip = inode->u.generic_ip;
1556 struct gfs2_leaf *leaf, *oleaf;
1977 int error; 1557 int error;
1558 u32 index;
1559 u64 bn;
1978 1560
1979 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) 1561 index = name->hash >> (32 - ip->i_di.di_depth);
1980 error = dir_e_search(dip, filename, inum, type); 1562 error = get_first_leaf(ip, index, &obh);
1981 else 1563 if (error)
1982 error = dir_l_search(dip, filename, inum, type); 1564 return error;
1565 do {
1566 oleaf = (struct gfs2_leaf *)obh->b_data;
1567 bn = be64_to_cpu(oleaf->lf_next);
1568 if (!bn)
1569 break;
1570 brelse(obh);
1571 error = get_leaf(ip, bn, &obh);
1572 if (error)
1573 return error;
1574 } while(1);
1983 1575
1984 return error; 1576 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1577
1578 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1579 if (!leaf) {
1580 brelse(obh);
1581 return -ENOSPC;
1582 }
1583 oleaf->lf_next = cpu_to_be64(bn);
1584 brelse(bh);
1585 brelse(obh);
1586
1587 error = gfs2_meta_inode_buffer(ip, &bh);
1588 if (error)
1589 return error;
1590 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1591 ip->i_di.di_blocks++;
1592 gfs2_dinode_out(&ip->i_di, bh->b_data);
1593 brelse(bh);
1594 return 0;
1985} 1595}
1986 1596
1987/** 1597/**
@@ -1994,19 +1604,70 @@ int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename,
1994 * Returns: 0 on success, error code on failure 1604 * Returns: 0 on success, error code on failure
1995 */ 1605 */
1996 1606
1997int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename, 1607int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1998 struct gfs2_inum *inum, unsigned int type) 1608 const struct gfs2_inum *inum, unsigned type)
1999{ 1609{
1610 struct gfs2_inode *ip = inode->u.generic_ip;
1611 struct buffer_head *bh;
1612 struct gfs2_dirent *dent;
1613 struct gfs2_leaf *leaf;
2000 int error; 1614 int error;
2001 1615
2002 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) 1616 while(1) {
2003 error = dir_e_add(dip, filename, inum, type); 1617 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
2004 else 1618 &bh);
2005 error = dir_l_add(dip, filename, inum, type); 1619 if (dent) {
2006 1620 if (IS_ERR(dent))
1621 return PTR_ERR(dent);
1622 dent = gfs2_init_dirent(inode, dent, name, bh);
1623 gfs2_inum_out(inum, (char *)&dent->de_inum);
1624 dent->de_type = cpu_to_be16(type);
1625 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1626 leaf = (struct gfs2_leaf *)bh->b_data;
1627 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1628 }
1629 brelse(bh);
1630 error = gfs2_meta_inode_buffer(ip, &bh);
1631 if (error)
1632 break;
1633 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1634 ip->i_di.di_entries++;
1635 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1636 gfs2_dinode_out(&ip->i_di, bh->b_data);
1637 brelse(bh);
1638 error = 0;
1639 break;
1640 }
1641 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1642 error = dir_make_exhash(inode);
1643 if (error)
1644 break;
1645 continue;
1646 }
1647 error = dir_split_leaf(inode, name);
1648 if (error == 0)
1649 continue;
1650 if (error != -ENOSPC)
1651 break;
1652 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1653 error = dir_double_exhash(ip);
1654 if (error)
1655 break;
1656 error = dir_split_leaf(inode, name);
1657 if (error)
1658 break;
1659 continue;
1660 }
1661 error = dir_new_leaf(inode, name);
1662 if (!error)
1663 continue;
1664 error = -ENOSPC;
1665 break;
1666 }
2007 return error; 1667 return error;
2008} 1668}
2009 1669
1670
2010/** 1671/**
2011 * gfs2_dir_del - Delete a directory entry 1672 * gfs2_dir_del - Delete a directory entry
2012 * @dip: The GFS2 inode 1673 * @dip: The GFS2 inode
@@ -2015,14 +1676,50 @@ int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename,
2015 * Returns: 0 on success, error code on failure 1676 * Returns: 0 on success, error code on failure
2016 */ 1677 */
2017 1678
2018int gfs2_dir_del(struct gfs2_inode *dip, struct qstr *filename) 1679int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
2019{ 1680{
1681 struct gfs2_dirent *dent, *prev = NULL;
1682 struct buffer_head *bh;
2020 int error; 1683 int error;
2021 1684
2022 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) 1685 /* Returns _either_ the entry (if its first in block) or the
2023 error = dir_e_del(dip, filename); 1686 previous entry otherwise */
2024 else 1687 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
2025 error = dir_l_del(dip, filename); 1688 if (!dent) {
1689 gfs2_consist_inode(dip);
1690 return -EIO;
1691 }
1692 if (IS_ERR(dent)) {
1693 gfs2_consist_inode(dip);
1694 return PTR_ERR(dent);
1695 }
1696 /* If not first in block, adjust pointers accordingly */
1697 if (gfs2_dirent_find(dent, name) == 0) {
1698 prev = dent;
1699 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1700 }
1701
1702 dirent_del(dip, bh, prev, dent);
1703 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1704 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1705 u16 entries = be16_to_cpu(leaf->lf_entries);
1706 if (!entries)
1707 gfs2_consist_inode(dip);
1708 leaf->lf_entries = cpu_to_be16(--entries);
1709 brelse(bh);
1710 }
1711
1712 error = gfs2_meta_inode_buffer(dip, &bh);
1713 if (error)
1714 return error;
1715
1716 if (!dip->i_di.di_entries)
1717 gfs2_consist_inode(dip);
1718 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1719 dip->i_di.di_entries--;
1720 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1721 gfs2_dinode_out(&dip->i_di, bh->b_data);
1722 brelse(bh);
2026 1723
2027 return error; 1724 return error;
2028} 1725}
@@ -2053,17 +1750,37 @@ int gfs2_dir_read(struct gfs2_inode *dip, uint64_t *offset, void *opaque,
2053 * Returns: errno 1750 * Returns: errno
2054 */ 1751 */
2055 1752
2056int gfs2_dir_mvino(struct gfs2_inode *dip, struct qstr *filename, 1753int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
2057 struct gfs2_inum *inum, unsigned int new_type) 1754 struct gfs2_inum *inum, unsigned int new_type)
2058{ 1755{
1756 struct buffer_head *bh;
1757 struct gfs2_dirent *dent;
2059 int error; 1758 int error;
2060 1759
2061 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) 1760 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
2062 error = dir_e_mvino(dip, filename, inum, new_type); 1761 if (!dent) {
2063 else 1762 gfs2_consist_inode(dip);
2064 error = dir_l_mvino(dip, filename, inum, new_type); 1763 return -EIO;
1764 }
1765 if (IS_ERR(dent))
1766 return PTR_ERR(dent);
2065 1767
2066 return error; 1768 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1769 gfs2_inum_out(inum, (char *)&dent->de_inum);
1770 dent->de_type = cpu_to_be16(new_type);
1771
1772 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1773 brelse(bh);
1774 error = gfs2_meta_inode_buffer(dip, &bh);
1775 if (error)
1776 return error;
1777 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1778 }
1779
1780 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1781 gfs2_dinode_out(&dip->i_di, bh->b_data);
1782 brelse(bh);
1783 return 0;
2067} 1784}
2068 1785
2069/** 1786/**
@@ -2079,7 +1796,7 @@ static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
2079{ 1796{
2080 struct gfs2_sbd *sdp = dip->i_sbd; 1797 struct gfs2_sbd *sdp = dip->i_sbd;
2081 struct buffer_head *bh; 1798 struct buffer_head *bh;
2082 struct gfs2_leaf leaf; 1799 struct gfs2_leaf *leaf;
2083 uint32_t hsize, len; 1800 uint32_t hsize, len;
2084 uint32_t ht_offset, lp_offset, ht_offset_cur = -1; 1801 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
2085 uint32_t index = 0; 1802 uint32_t index = 0;
@@ -2118,10 +1835,10 @@ static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
2118 error = get_leaf(dip, leaf_no, &bh); 1835 error = get_leaf(dip, leaf_no, &bh);
2119 if (error) 1836 if (error)
2120 goto out; 1837 goto out;
2121 gfs2_leaf_in(&leaf, bh->b_data); 1838 leaf = (struct gfs2_leaf *)bh->b_data;
2122 brelse(bh); 1839 brelse(bh);
2123 1840
2124 len = 1 << (dip->i_di.di_depth - leaf.lf_depth); 1841 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
2125 1842
2126 error = lc(dip, index, len, leaf_no, data); 1843 error = lc(dip, index, len, leaf_no, data);
2127 if (error) 1844 if (error)
@@ -2158,10 +1875,10 @@ static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
2158 uint64_t leaf_no, void *data) 1875 uint64_t leaf_no, void *data)
2159{ 1876{
2160 struct gfs2_sbd *sdp = dip->i_sbd; 1877 struct gfs2_sbd *sdp = dip->i_sbd;
2161 struct gfs2_leaf tmp_leaf; 1878 struct gfs2_leaf *tmp_leaf;
2162 struct gfs2_rgrp_list rlist; 1879 struct gfs2_rgrp_list rlist;
2163 struct buffer_head *bh, *dibh; 1880 struct buffer_head *bh, *dibh;
2164 uint64_t blk; 1881 uint64_t blk, nblk;
2165 unsigned int rg_blocks = 0, l_blocks = 0; 1882 unsigned int rg_blocks = 0, l_blocks = 0;
2166 char *ht; 1883 char *ht;
2167 unsigned int x, size = len * sizeof(uint64_t); 1884 unsigned int x, size = len * sizeof(uint64_t);
@@ -2185,11 +1902,12 @@ static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
2185 1902
2186 /* Count the number of leaves */ 1903 /* Count the number of leaves */
2187 1904
2188 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) { 1905 for (blk = leaf_no; blk; blk = nblk) {
2189 error = get_leaf(dip, blk, &bh); 1906 error = get_leaf(dip, blk, &bh);
2190 if (error) 1907 if (error)
2191 goto out_rlist; 1908 goto out_rlist;
2192 gfs2_leaf_in(&tmp_leaf, (bh)->b_data); 1909 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1910 nblk = be64_to_cpu(tmp_leaf->lf_next);
2193 brelse(bh); 1911 brelse(bh);
2194 1912
2195 gfs2_rlist_add(sdp, &rlist, blk); 1913 gfs2_rlist_add(sdp, &rlist, blk);
@@ -2214,11 +1932,12 @@ static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
2214 if (error) 1932 if (error)
2215 goto out_rg_gunlock; 1933 goto out_rg_gunlock;
2216 1934
2217 for (blk = leaf_no; blk; blk = tmp_leaf.lf_next) { 1935 for (blk = leaf_no; blk; blk = nblk) {
2218 error = get_leaf(dip, blk, &bh); 1936 error = get_leaf(dip, blk, &bh);
2219 if (error) 1937 if (error)
2220 goto out_end_trans; 1938 goto out_end_trans;
2221 gfs2_leaf_in(&tmp_leaf, bh->b_data); 1939 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1940 nblk = be64_to_cpu(tmp_leaf->lf_next);
2222 brelse(bh); 1941 brelse(bh);
2223 1942
2224 gfs2_free_meta(dip, blk, 1); 1943 gfs2_free_meta(dip, blk, 1);
@@ -2308,63 +2027,22 @@ int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2308 * gfs2_diradd_alloc_required - find if adding entry will require an allocation 2027 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2309 * @ip: the file being written to 2028 * @ip: the file being written to
2310 * @filname: the filename that's going to be added 2029 * @filname: the filename that's going to be added
2311 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
2312 * 2030 *
2313 * Returns: errno 2031 * Returns: 1 if alloc required, 0 if not, -ve on error
2314 */ 2032 */
2315 2033
2316int gfs2_diradd_alloc_required(struct gfs2_inode *dip, struct qstr *filename, 2034int gfs2_diradd_alloc_required(struct inode *inode,
2317 int *alloc_required) 2035 const struct qstr *name)
2318{ 2036{
2319 struct buffer_head *bh = NULL, *bh_next; 2037 struct gfs2_dirent *dent;
2320 uint32_t hsize, hash, index; 2038 struct buffer_head *bh;
2321 int error = 0;
2322
2323 *alloc_required = 0;
2324
2325 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
2326 hsize = 1 << dip->i_di.di_depth;
2327 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
2328 gfs2_consist_inode(dip);
2329 return -EIO;
2330 }
2331
2332 hash = gfs2_disk_hash(filename->name, filename->len);
2333 index = hash >> (32 - dip->i_di.di_depth);
2334
2335 error = get_first_leaf(dip, index, &bh_next);
2336 if (error)
2337 return error;
2338
2339 do {
2340 brelse(bh);
2341
2342 bh = bh_next;
2343
2344 if (dirent_fits(dip, bh, filename->len))
2345 break;
2346
2347 error = get_next_leaf(dip, bh, &bh_next);
2348 if (error == -ENOENT) {
2349 *alloc_required = 1;
2350 error = 0;
2351 break;
2352 }
2353 }
2354 while (!error);
2355
2356 brelse(bh);
2357 } else {
2358 error = gfs2_meta_inode_buffer(dip, &bh);
2359 if (error)
2360 return error;
2361
2362 if (!dirent_fits(dip, bh, filename->len))
2363 *alloc_required = 1;
2364
2365 brelse(bh);
2366 }
2367 2039
2368 return error; 2040 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
2041 if (!dent)
2042 return 1;
2043 if (IS_ERR(dent))
2044 return PTR_ERR(dent);
2045 brelse(bh);
2046 return 0;
2369} 2047}
2370 2048
diff --git a/fs/gfs2/dir.h b/fs/gfs2/dir.h
index 5b01497b3ab3..8fd4dc0f700e 100644
--- a/fs/gfs2/dir.h
+++ b/fs/gfs2/dir.h
@@ -27,25 +27,34 @@ typedef int (*gfs2_filldir_t) (void *opaque,
27 uint64_t offset, 27 uint64_t offset,
28 struct gfs2_inum *inum, unsigned int type); 28 struct gfs2_inum *inum, unsigned int type);
29 29
30int gfs2_filecmp(struct qstr *file1, char *file2, int len_of_file2); 30int gfs2_dir_search(struct inode *dir, const struct qstr *filename,
31int gfs2_dirent_alloc(struct gfs2_inode *dip, struct buffer_head *bh, 31 struct gfs2_inum *inum, unsigned int *type);
32 int name_len, struct gfs2_dirent **dent_out); 32int gfs2_dir_add(struct inode *inode, const struct qstr *filename,
33 33 const struct gfs2_inum *inum, unsigned int type);
34int gfs2_dir_search(struct gfs2_inode *dip, struct qstr *filename, 34int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *filename);
35 struct gfs2_inum *inum, unsigned int *type);
36int gfs2_dir_add(struct gfs2_inode *dip, struct qstr *filename,
37 struct gfs2_inum *inum, unsigned int type);
38int gfs2_dir_del(struct gfs2_inode *dip, struct qstr *filename);
39int gfs2_dir_read(struct gfs2_inode *dip, uint64_t * offset, void *opaque, 35int gfs2_dir_read(struct gfs2_inode *dip, uint64_t * offset, void *opaque,
40 gfs2_filldir_t filldir); 36 gfs2_filldir_t filldir);
41int gfs2_dir_mvino(struct gfs2_inode *dip, struct qstr *filename, 37int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
42 struct gfs2_inum *new_inum, unsigned int new_type); 38 struct gfs2_inum *new_inum, unsigned int new_type);
43 39
44int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip); 40int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip);
45 41
46int gfs2_diradd_alloc_required(struct gfs2_inode *dip, struct qstr *filename, 42int gfs2_diradd_alloc_required(struct inode *dir,
47 int *alloc_required); 43 const struct qstr *filename);
48int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new, 44int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
49 struct buffer_head **bhp); 45 struct buffer_head **bhp);
46
47/* N.B. This probably ought to take inum & type as args as well */
48static inline void gfs2_qstr2dirent(const struct qstr *name, u16 reclen, struct gfs2_dirent *dent)
49{
50 dent->de_inum.no_addr = cpu_to_be64(0);
51 dent->de_inum.no_formal_ino = cpu_to_be64(0);
52 dent->de_hash = cpu_to_be32(name->hash);
53 dent->de_rec_len = cpu_to_be16(reclen);
54 dent->de_name_len = cpu_to_be16(name->len);
55 dent->de_type = cpu_to_be16(0);
56 memset(dent->__pad, 0, sizeof(dent->__pad));
57 memcpy((char*)(dent+1), name->name, name->len);
58}
50 59
51#endif /* __DIR_DOT_H__ */ 60#endif /* __DIR_DOT_H__ */
diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
index cd1de61bff2f..d403d51d5b0f 100644
--- a/fs/gfs2/inode.c
+++ b/fs/gfs2/inode.c
@@ -228,12 +228,10 @@ struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
228 228
229void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type) 229void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
230{ 230{
231 spin_lock(&ip->i_spin);
232 if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) { 231 if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
233 ip->i_di.di_nlink = 1; 232 ip->i_di.di_nlink = 1;
234 ip->i_di.di_mode = DT2IF(type); 233 ip->i_di.di_mode = DT2IF(type);
235 } 234 }
236 spin_unlock(&ip->i_spin);
237} 235}
238 236
239/** 237/**
@@ -257,10 +255,8 @@ int gfs2_inode_refresh(struct gfs2_inode *ip)
257 return -EIO; 255 return -EIO;
258 } 256 }
259 257
260 spin_lock(&ip->i_spin);
261 gfs2_dinode_in(&ip->i_di, dibh->b_data); 258 gfs2_dinode_in(&ip->i_di, dibh->b_data);
262 set_bit(GIF_MIN_INIT, &ip->i_flags); 259 set_bit(GIF_MIN_INIT, &ip->i_flags);
263 spin_unlock(&ip->i_spin);
264 260
265 brelse(dibh); 261 brelse(dibh);
266 262
@@ -702,6 +698,16 @@ int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
702 return 0; 698 return 0;
703} 699}
704 700
701struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
702{
703 struct qstr qstr;
704 qstr.name = name;
705 qstr.len = strlen(name);
706 qstr.hash = gfs2_disk_hash(qstr.name, qstr.len);
707 return gfs2_lookupi(dip, &qstr, 1, NULL);
708}
709
710
705/** 711/**
706 * gfs2_lookupi - Look up a filename in a directory and return its inode 712 * gfs2_lookupi - Look up a filename in a directory and return its inode
707 * @d_gh: An initialized holder for the directory glock 713 * @d_gh: An initialized holder for the directory glock
@@ -715,8 +721,9 @@ int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
715 * Returns: errno 721 * Returns: errno
716 */ 722 */
717 723
718int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root, 724struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
719 struct inode **inodep) 725 struct nameidata *nd)
726
720{ 727{
721 struct super_block *sb = dir->i_sb; 728 struct super_block *sb = dir->i_sb;
722 struct gfs2_inode *ipp; 729 struct gfs2_inode *ipp;
@@ -727,14 +734,14 @@ int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
727 unsigned int type; 734 unsigned int type;
728 struct gfs2_glock *gl; 735 struct gfs2_glock *gl;
729 int error = 0; 736 int error = 0;
730 737 struct inode *inode = NULL;
731 *inodep = NULL;
732 738
733 if (!name->len || name->len > GFS2_FNAMESIZE) 739 if (!name->len || name->len > GFS2_FNAMESIZE)
734 return -ENAMETOOLONG; 740 return ERR_PTR(-ENAMETOOLONG);
735 741
736 if (gfs2_filecmp(name, ".", 1) || 742 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
737 (gfs2_filecmp(name, "..", 2) && dir == sb->s_root->d_inode)) { 743 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
744 dir == sb->s_root->d_inode)) {
738 gfs2_inode_hold(dip); 745 gfs2_inode_hold(dip);
739 ipp = dip; 746 ipp = dip;
740 goto done; 747 goto done;
@@ -742,7 +749,7 @@ int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
742 749
743 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); 750 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
744 if (error) 751 if (error)
745 return error; 752 return ERR_PTR(error);
746 753
747 if (!is_root) { 754 if (!is_root) {
748 error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL); 755 error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
@@ -750,7 +757,7 @@ int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
750 goto out; 757 goto out;
751 } 758 }
752 759
753 error = gfs2_dir_search(dip, name, &inum, &type); 760 error = gfs2_dir_search(dir, name, &inum, &type);
754 if (error) 761 if (error)
755 goto out; 762 goto out;
756 763
@@ -768,13 +775,16 @@ int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
768out: 775out:
769 gfs2_glock_dq_uninit(&d_gh); 776 gfs2_glock_dq_uninit(&d_gh);
770done: 777done:
778 if (error == -ENOENT)
779 return NULL;
771 if (error == 0) { 780 if (error == 0) {
772 *inodep = gfs2_ip2v(ipp); 781 inode = gfs2_ip2v(ipp);
773 if (!*inodep)
774 error = -ENOMEM;
775 gfs2_inode_put(ipp); 782 gfs2_inode_put(ipp);
783 if (!inode)
784 return ERR_PTR(-ENOMEM);
785 return inode;
776 } 786 }
777 return error; 787 return ERR_PTR(error);
778} 788}
779 789
780static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino) 790static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
@@ -918,7 +928,7 @@ static int create_ok(struct gfs2_inode *dip, struct qstr *name,
918 if (!dip->i_di.di_nlink) 928 if (!dip->i_di.di_nlink)
919 return -EPERM; 929 return -EPERM;
920 930
921 error = gfs2_dir_search(dip, name, NULL, NULL); 931 error = gfs2_dir_search(dip->i_vnode, name, NULL, NULL);
922 switch (error) { 932 switch (error) {
923 case -ENOENT: 933 case -ENOENT:
924 error = 0; 934 error = 0;
@@ -1116,7 +1126,9 @@ static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1116 if (error) 1126 if (error)
1117 goto fail; 1127 goto fail;
1118 1128
1119 error = gfs2_diradd_alloc_required(dip, name, &alloc_required); 1129 error = alloc_required = gfs2_diradd_alloc_required(dip->i_vnode, name);
1130 if (alloc_required < 0)
1131 goto fail;
1120 if (alloc_required) { 1132 if (alloc_required) {
1121 error = gfs2_quota_check(dip, dip->i_di.di_uid, 1133 error = gfs2_quota_check(dip, dip->i_di.di_uid,
1122 dip->i_di.di_gid); 1134 dip->i_di.di_gid);
@@ -1145,7 +1157,7 @@ static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1145 goto fail_quota_locks; 1157 goto fail_quota_locks;
1146 } 1158 }
1147 1159
1148 error = gfs2_dir_add(dip, name, &ip->i_num, IF2DT(ip->i_di.di_mode)); 1160 error = gfs2_dir_add(dip->i_vnode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
1149 if (error) 1161 if (error)
1150 goto fail_end_trans; 1162 goto fail_end_trans;
1151 1163
@@ -1379,12 +1391,14 @@ int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1379 1391
1380 dotname.len = 1; 1392 dotname.len = 1;
1381 dotname.name = "."; 1393 dotname.name = ".";
1394 dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
1382 error = gfs2_dir_del(ip, &dotname); 1395 error = gfs2_dir_del(ip, &dotname);
1383 if (error) 1396 if (error)
1384 return error; 1397 return error;
1385 1398
1386 dotname.len = 2; 1399 dotname.len = 2;
1387 dotname.name = ".."; 1400 dotname.name = "..";
1401 dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
1388 error = gfs2_dir_del(ip, &dotname); 1402 error = gfs2_dir_del(ip, &dotname);
1389 if (error) 1403 if (error)
1390 return error; 1404 return error;
@@ -1439,7 +1453,7 @@ int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1439 if (error) 1453 if (error)
1440 return error; 1454 return error;
1441 1455
1442 error = gfs2_dir_search(dip, name, &inum, &type); 1456 error = gfs2_dir_search(dip->i_vnode, name, &inum, &type);
1443 if (error) 1457 if (error)
1444 return error; 1458 return error;
1445 1459
@@ -1476,6 +1490,7 @@ int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1476 memset(&dotdot, 0, sizeof(struct qstr)); 1490 memset(&dotdot, 0, sizeof(struct qstr));
1477 dotdot.name = ".."; 1491 dotdot.name = "..";
1478 dotdot.len = 2; 1492 dotdot.len = 2;
1493 dotdot.hash = gfs2_disk_hash(dotdot.name, dotdot.len);
1479 1494
1480 igrab(dir); 1495 igrab(dir);
1481 1496
@@ -1489,9 +1504,11 @@ int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1489 break; 1504 break;
1490 } 1505 }
1491 1506
1492 error = gfs2_lookupi(dir, &dotdot, 1, &tmp); 1507 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1493 if (error) 1508 if (IS_ERR(tmp)) {
1509 error = PTR_ERR(tmp);
1494 break; 1510 break;
1511 }
1495 1512
1496 iput(dir); 1513 iput(dir);
1497 dir = tmp; 1514 dir = tmp;
diff --git a/fs/gfs2/inode.h b/fs/gfs2/inode.h
index c3fa6cfce169..0dd2a26626ec 100644
--- a/fs/gfs2/inode.h
+++ b/fs/gfs2/inode.h
@@ -44,8 +44,8 @@ void gfs2_inode_destroy(struct gfs2_inode *ip);
44int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul); 44int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul);
45 45
46int gfs2_change_nlink(struct gfs2_inode *ip, int diff); 46int gfs2_change_nlink(struct gfs2_inode *ip, int diff);
47int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root, 47struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
48 struct inode **ipp); 48 struct nameidata *nd);
49struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name, 49struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
50 unsigned int mode); 50 unsigned int mode);
51int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name, 51int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
@@ -66,17 +66,7 @@ int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr);
66 66
67int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd); 67int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd);
68 68
69static inline int gfs2_lookup_simple(struct inode *dip, char *name, 69struct inode *gfs2_lookup_simple(struct inode *dip, const char *name);
70 struct inode **ipp)
71{
72 struct qstr qstr;
73 int err;
74 memset(&qstr, 0, sizeof(struct qstr));
75 qstr.name = name;
76 qstr.len = strlen(name);
77 err = gfs2_lookupi(dip, &qstr, 1, ipp);
78 return err;
79}
80 70
81#endif /* __INODE_DOT_H__ */ 71#endif /* __INODE_DOT_H__ */
82 72
diff --git a/fs/gfs2/ondisk.c b/fs/gfs2/ondisk.c
index 5a0bdc22a1f4..3be060f1cbe7 100644
--- a/fs/gfs2/ondisk.c
+++ b/fs/gfs2/ondisk.c
@@ -64,7 +64,7 @@ void gfs2_inum_in(struct gfs2_inum *no, char *buf)
64 no->no_addr = be64_to_cpu(str->no_addr); 64 no->no_addr = be64_to_cpu(str->no_addr);
65} 65}
66 66
67void gfs2_inum_out(struct gfs2_inum *no, char *buf) 67void gfs2_inum_out(const struct gfs2_inum *no, char *buf)
68{ 68{
69 struct gfs2_inum *str = (struct gfs2_inum *)buf; 69 struct gfs2_inum *str = (struct gfs2_inum *)buf;
70 70
@@ -342,17 +342,6 @@ void gfs2_dirent_print(struct gfs2_dirent *de, char *name)
342 printk(KERN_INFO " name = %s\n", buf); 342 printk(KERN_INFO " name = %s\n", buf);
343} 343}
344 344
345void gfs2_leaf_in(struct gfs2_leaf *lf, char *buf)
346{
347 struct gfs2_leaf *str = (struct gfs2_leaf *)buf;
348
349 gfs2_meta_header_in(&lf->lf_header, buf);
350 lf->lf_depth = be16_to_cpu(str->lf_depth);
351 lf->lf_entries = be16_to_cpu(str->lf_entries);
352 lf->lf_dirent_format = be32_to_cpu(str->lf_dirent_format);
353 lf->lf_next = be64_to_cpu(str->lf_next);
354}
355
356void gfs2_leaf_print(struct gfs2_leaf *lf) 345void gfs2_leaf_print(struct gfs2_leaf *lf)
357{ 346{
358 gfs2_meta_header_print(&lf->lf_header); 347 gfs2_meta_header_print(&lf->lf_header);
diff --git a/fs/gfs2/ops_dentry.c b/fs/gfs2/ops_dentry.c
index 7f6139288519..b54608f9df50 100644
--- a/fs/gfs2/ops_dentry.c
+++ b/fs/gfs2/ops_dentry.c
@@ -38,25 +38,26 @@
38static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd) 38static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd)
39{ 39{
40 struct dentry *parent = dget_parent(dentry); 40 struct dentry *parent = dget_parent(dentry);
41 struct gfs2_sbd *sdp = parent->d_inode->i_sb->s_fs_info;
41 struct gfs2_inode *dip = parent->d_inode->u.generic_ip; 42 struct gfs2_inode *dip = parent->d_inode->u.generic_ip;
42 struct inode *inode; 43 struct inode *inode = dentry->d_inode;
43 struct gfs2_holder d_gh; 44 struct gfs2_holder d_gh;
44 struct gfs2_inode *ip; 45 struct gfs2_inode *ip;
45 struct gfs2_inum inum; 46 struct gfs2_inum inum;
46 unsigned int type; 47 unsigned int type;
47 int error; 48 int error;
48 49
49 lock_kernel();
50
51 inode = dentry->d_inode;
52 if (inode && is_bad_inode(inode)) 50 if (inode && is_bad_inode(inode))
53 goto invalid; 51 goto invalid;
54 52
53 if (sdp->sd_args.ar_localcaching)
54 goto valid;
55
55 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); 56 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
56 if (error) 57 if (error)
57 goto fail; 58 goto fail;
58 59
59 error = gfs2_dir_search(dip, &dentry->d_name, &inum, &type); 60 error = gfs2_dir_search(parent->d_inode, &dentry->d_name, &inum, &type);
60 switch (error) { 61 switch (error) {
61 case 0: 62 case 0:
62 if (!inode) 63 if (!inode)
@@ -84,7 +85,6 @@ static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd)
84 gfs2_glock_dq_uninit(&d_gh); 85 gfs2_glock_dq_uninit(&d_gh);
85 86
86 valid: 87 valid:
87 unlock_kernel();
88 dput(parent); 88 dput(parent);
89 return 1; 89 return 1;
90 90
@@ -99,7 +99,6 @@ static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd)
99 } 99 }
100 d_drop(dentry); 100 d_drop(dentry);
101 101
102 unlock_kernel();
103 dput(parent); 102 dput(parent);
104 return 0; 103 return 0;
105 104
@@ -107,12 +106,18 @@ static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd)
107 gfs2_glock_dq_uninit(&d_gh); 106 gfs2_glock_dq_uninit(&d_gh);
108 107
109 fail: 108 fail:
110 unlock_kernel();
111 dput(parent); 109 dput(parent);
112 return 0; 110 return 0;
113} 111}
114 112
113static int gfs2_dhash(struct dentry *dentry, struct qstr *str)
114{
115 str->hash = gfs2_disk_hash(str->name, str->len);
116 return 0;
117}
118
115struct dentry_operations gfs2_dops = { 119struct dentry_operations gfs2_dops = {
116 .d_revalidate = gfs2_drevalidate, 120 .d_revalidate = gfs2_drevalidate,
121 .d_hash = gfs2_dhash,
117}; 122};
118 123
diff --git a/fs/gfs2/ops_export.c b/fs/gfs2/ops_export.c
index a346943363c6..b27bce74a795 100644
--- a/fs/gfs2/ops_export.c
+++ b/fs/gfs2/ops_export.c
@@ -24,6 +24,7 @@
24#include "inode.h" 24#include "inode.h"
25#include "ops_export.h" 25#include "ops_export.h"
26#include "rgrp.h" 26#include "rgrp.h"
27#include "util.h"
27 28
28static struct dentry *gfs2_decode_fh(struct super_block *sb, 29static struct dentry *gfs2_decode_fh(struct super_block *sb,
29 __u32 *fh, 30 __u32 *fh,
@@ -167,11 +168,15 @@ static struct dentry *gfs2_get_parent(struct dentry *child)
167 struct qstr dotdot = { .name = "..", .len = 2 }; 168 struct qstr dotdot = { .name = "..", .len = 2 };
168 struct inode *inode; 169 struct inode *inode;
169 struct dentry *dentry; 170 struct dentry *dentry;
170 int error;
171 171
172 error = gfs2_lookupi(child->d_inode, &dotdot, 1, &inode); 172 dotdot.hash = gfs2_disk_hash(dotdot.name, dotdot.len);
173 if (error) 173
174 return ERR_PTR(error); 174 inode = gfs2_lookupi(child->d_inode, &dotdot, 1, NULL);
175
176 if (!inode)
177 return ERR_PTR(-ENOENT);
178 if (IS_ERR(inode))
179 return ERR_PTR(PTR_ERR(inode));
175 180
176 dentry = d_alloc_anon(inode); 181 dentry = d_alloc_anon(inode);
177 if (!dentry) { 182 if (!dentry) {
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 8d6d94143561..8d2c557b3ff4 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -379,11 +379,10 @@ static int init_journal(struct gfs2_sbd *sdp, int undo)
379 goto fail_recoverd; 379 goto fail_recoverd;
380 } 380 }
381 381
382 error = gfs2_lookup_simple(sdp->sd_master_dir, "jindex", 382 sdp->sd_jindex = gfs2_lookup_simple(sdp->sd_master_dir, "jindex");
383 &sdp->sd_jindex); 383 if (IS_ERR(sdp->sd_jindex)) {
384 if (error) {
385 fs_err(sdp, "can't lookup journal index: %d\n", error); 384 fs_err(sdp, "can't lookup journal index: %d\n", error);
386 return error; 385 return PTR_ERR(sdp->sd_jindex);
387 } 386 }
388 ip = sdp->sd_jindex->u.generic_ip; 387 ip = sdp->sd_jindex->u.generic_ip;
389 set_bit(GLF_STICKY, &ip->i_gl->gl_flags); 388 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
@@ -531,26 +530,26 @@ static int init_inodes(struct gfs2_sbd *sdp, int undo)
531 goto fail_master; 530 goto fail_master;
532 531
533 /* Read in the master inode number inode */ 532 /* Read in the master inode number inode */
534 error = gfs2_lookup_simple(sdp->sd_master_dir, "inum", 533 sdp->sd_inum_inode = gfs2_lookup_simple(sdp->sd_master_dir, "inum");
535 &sdp->sd_inum_inode); 534 if (IS_ERR(sdp->sd_inum_inode)) {
536 if (error) { 535 error = PTR_ERR(sdp->sd_inum_inode);
537 fs_err(sdp, "can't read in inum inode: %d\n", error); 536 fs_err(sdp, "can't read in inum inode: %d\n", error);
538 goto fail_journal; 537 goto fail_journal;
539 } 538 }
540 539
541 540
542 /* Read in the master statfs inode */ 541 /* Read in the master statfs inode */
543 error = gfs2_lookup_simple(sdp->sd_master_dir, "statfs", 542 sdp->sd_statfs_inode = gfs2_lookup_simple(sdp->sd_master_dir, "statfs");
544 &sdp->sd_statfs_inode); 543 if (IS_ERR(sdp->sd_statfs_inode)) {
545 if (error) { 544 error = PTR_ERR(sdp->sd_statfs_inode);
546 fs_err(sdp, "can't read in statfs inode: %d\n", error); 545 fs_err(sdp, "can't read in statfs inode: %d\n", error);
547 goto fail_inum; 546 goto fail_inum;
548 } 547 }
549 548
550 /* Read in the resource index inode */ 549 /* Read in the resource index inode */
551 error = gfs2_lookup_simple(sdp->sd_master_dir, "rindex", 550 sdp->sd_rindex = gfs2_lookup_simple(sdp->sd_master_dir, "rindex");
552 &sdp->sd_rindex); 551 if (IS_ERR(sdp->sd_rindex)) {
553 if (error) { 552 error = PTR_ERR(sdp->sd_rindex);
554 fs_err(sdp, "can't get resource index inode: %d\n", error); 553 fs_err(sdp, "can't get resource index inode: %d\n", error);
555 goto fail_statfs; 554 goto fail_statfs;
556 } 555 }
@@ -559,9 +558,9 @@ static int init_inodes(struct gfs2_sbd *sdp, int undo)
559 sdp->sd_rindex_vn = ip->i_gl->gl_vn - 1; 558 sdp->sd_rindex_vn = ip->i_gl->gl_vn - 1;
560 559
561 /* Read in the quota inode */ 560 /* Read in the quota inode */
562 error = gfs2_lookup_simple(sdp->sd_master_dir, "quota", 561 sdp->sd_quota_inode = gfs2_lookup_simple(sdp->sd_master_dir, "quota");
563 &sdp->sd_quota_inode); 562 if (IS_ERR(sdp->sd_quota_inode)) {
564 if (error) { 563 error = PTR_ERR(sdp->sd_quota_inode);
565 fs_err(sdp, "can't get quota file inode: %d\n", error); 564 fs_err(sdp, "can't get quota file inode: %d\n", error);
566 goto fail_rindex; 565 goto fail_rindex;
567 } 566 }
@@ -600,36 +599,41 @@ static int init_per_node(struct gfs2_sbd *sdp, int undo)
600 if (undo) 599 if (undo)
601 goto fail_qc_gh; 600 goto fail_qc_gh;
602 601
603 error = gfs2_lookup_simple(sdp->sd_master_dir, "per_node", &pn); 602 pn = gfs2_lookup_simple(sdp->sd_master_dir, "per_node");
604 if (error) { 603 if (IS_ERR(pn)) {
604 error = PTR_ERR(pn);
605 fs_err(sdp, "can't find per_node directory: %d\n", error); 605 fs_err(sdp, "can't find per_node directory: %d\n", error);
606 return error; 606 return error;
607 } 607 }
608 608
609 sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid); 609 sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid);
610 error = gfs2_lookup_simple(pn, buf, &sdp->sd_ir_inode); 610 sdp->sd_ir_inode = gfs2_lookup_simple(pn, buf);
611 if (error) { 611 if (IS_ERR(sdp->sd_ir_inode)) {
612 error = PTR_ERR(sdp->sd_ir_inode);
612 fs_err(sdp, "can't find local \"ir\" file: %d\n", error); 613 fs_err(sdp, "can't find local \"ir\" file: %d\n", error);
613 goto fail; 614 goto fail;
614 } 615 }
615 616
616 sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid); 617 sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
617 error = gfs2_lookup_simple(pn, buf, &sdp->sd_sc_inode); 618 sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
618 if (error) { 619 if (IS_ERR(sdp->sd_sc_inode)) {
620 error = PTR_ERR(sdp->sd_sc_inode);
619 fs_err(sdp, "can't find local \"sc\" file: %d\n", error); 621 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
620 goto fail_ir_i; 622 goto fail_ir_i;
621 } 623 }
622 624
623 sprintf(buf, "unlinked_tag%u", sdp->sd_jdesc->jd_jid); 625 sprintf(buf, "unlinked_tag%u", sdp->sd_jdesc->jd_jid);
624 error = gfs2_lookup_simple(pn, buf, &sdp->sd_ut_inode); 626 sdp->sd_ut_inode = gfs2_lookup_simple(pn, buf);
625 if (error) { 627 if (IS_ERR(sdp->sd_ut_inode)) {
628 error = PTR_ERR(sdp->sd_ut_inode);
626 fs_err(sdp, "can't find local \"ut\" file: %d\n", error); 629 fs_err(sdp, "can't find local \"ut\" file: %d\n", error);
627 goto fail_sc_i; 630 goto fail_sc_i;
628 } 631 }
629 632
630 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid); 633 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
631 error = gfs2_lookup_simple(pn, buf, &sdp->sd_qc_inode); 634 sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
632 if (error) { 635 if (IS_ERR(sdp->sd_qc_inode)) {
636 error = PTR_ERR(sdp->sd_qc_inode);
633 fs_err(sdp, "can't find local \"qc\" file: %d\n", error); 637 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
634 goto fail_ut_i; 638 goto fail_ut_i;
635 } 639 }
diff --git a/fs/gfs2/ops_inode.c b/fs/gfs2/ops_inode.c
index 7633a8584b0d..e8ab9d254b76 100644
--- a/fs/gfs2/ops_inode.c
+++ b/fs/gfs2/ops_inode.c
@@ -58,7 +58,6 @@ static int gfs2_create(struct inode *dir, struct dentry *dentry,
58 struct gfs2_holder ghs[2]; 58 struct gfs2_holder ghs[2];
59 struct inode *inode; 59 struct inode *inode;
60 int new = 1; 60 int new = 1;
61 int error;
62 61
63 gfs2_holder_init(dip->i_gl, 0, 0, ghs); 62 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
64 63
@@ -78,14 +77,16 @@ static int gfs2_create(struct inode *dir, struct dentry *dentry,
78 return PTR_ERR(inode); 77 return PTR_ERR(inode);
79 } 78 }
80 79
81 error = gfs2_lookupi(dir, &dentry->d_name, 0, &inode); 80 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
82 if (!error) { 81 if (inode) {
83 new = 0; 82 if (!IS_ERR(inode)) {
84 gfs2_holder_uninit(ghs); 83 new = 0;
85 break; 84 gfs2_holder_uninit(ghs);
86 } else if (error != -ENOENT) { 85 break;
87 gfs2_holder_uninit(ghs); 86 } else {
88 return error; 87 gfs2_holder_uninit(ghs);
88 return PTR_ERR(inode);
89 }
89 } 90 }
90 } 91 }
91 92
@@ -110,17 +111,13 @@ static int gfs2_create(struct inode *dir, struct dentry *dentry,
110static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, 111static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
111 struct nameidata *nd) 112 struct nameidata *nd)
112{ 113{
113 struct gfs2_inode *dip = dir->u.generic_ip;
114 struct gfs2_sbd *sdp = dip->i_sbd;
115 struct inode *inode = NULL; 114 struct inode *inode = NULL;
116 int error;
117 115
118 if (!sdp->sd_args.ar_localcaching) 116 dentry->d_op = &gfs2_dops;
119 dentry->d_op = &gfs2_dops;
120 117
121 error = gfs2_lookupi(dir, &dentry->d_name, 0, &inode); 118 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
122 if (error && error != -ENOENT) 119 if (inode && IS_ERR(inode))
123 return ERR_PTR(error); 120 return ERR_PTR(PTR_ERR(inode));
124 121
125 if (inode) 122 if (inode)
126 return d_splice_alias(inode, dentry); 123 return d_splice_alias(inode, dentry);
@@ -166,7 +163,7 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
166 if (error) 163 if (error)
167 goto out_gunlock; 164 goto out_gunlock;
168 165
169 error = gfs2_dir_search(dip, &dentry->d_name, NULL, NULL); 166 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
170 switch (error) { 167 switch (error) {
171 case -ENOENT: 168 case -ENOENT:
172 break; 169 break;
@@ -192,10 +189,10 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
192 if (ip->i_di.di_nlink == (uint32_t)-1) 189 if (ip->i_di.di_nlink == (uint32_t)-1)
193 goto out_gunlock; 190 goto out_gunlock;
194 191
195 error = gfs2_diradd_alloc_required(dip, &dentry->d_name, 192 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
196 &alloc_required); 193 if (error < 0)
197 if (error)
198 goto out_gunlock; 194 goto out_gunlock;
195 error = 0;
199 196
200 if (alloc_required) { 197 if (alloc_required) {
201 struct gfs2_alloc *al = gfs2_alloc_get(dip); 198 struct gfs2_alloc *al = gfs2_alloc_get(dip);
@@ -228,7 +225,7 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
228 goto out_ipres; 225 goto out_ipres;
229 } 226 }
230 227
231 error = gfs2_dir_add(dip, &dentry->d_name, &ip->i_num, 228 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
232 IF2DT(ip->i_di.di_mode)); 229 IF2DT(ip->i_di.di_mode));
233 if (error) 230 if (error)
234 goto out_end_trans; 231 goto out_end_trans;
@@ -419,24 +416,24 @@ static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
419 416
420 if (!gfs2_assert_withdraw(sdp, !error)) { 417 if (!gfs2_assert_withdraw(sdp, !error)) {
421 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; 418 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
422 struct gfs2_dirent *dent; 419 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
423 420 struct qstr str = { .name = ".", .len = 1 };
424 gfs2_dirent_alloc(ip, dibh, 1, &dent); 421 str.hash = gfs2_disk_hash(str.name, str.len);
425 422
423 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
424 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
426 dent->de_inum = di->di_num; /* already GFS2 endian */ 425 dent->de_inum = di->di_num; /* already GFS2 endian */
427 dent->de_hash = gfs2_disk_hash(".", 1);
428 dent->de_hash = cpu_to_be32(dent->de_hash);
429 dent->de_type = DT_DIR; 426 dent->de_type = DT_DIR;
430 memcpy((char *) (dent + 1), ".", 1);
431 di->di_entries = cpu_to_be32(1); 427 di->di_entries = cpu_to_be32(1);
432 428
433 gfs2_dirent_alloc(ip, dibh, 2, &dent); 429 str.name = "..";
430 str.len = 2;
431 str.hash = gfs2_disk_hash(str.name, str.len);
432 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
433 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
434 434
435 gfs2_inum_out(&dip->i_num, (char *) &dent->de_inum); 435 gfs2_inum_out(&dip->i_num, (char *) &dent->de_inum);
436 dent->de_hash = gfs2_disk_hash("..", 2);
437 dent->de_hash = cpu_to_be32(dent->de_hash);
438 dent->de_type = DT_DIR; 436 dent->de_type = DT_DIR;
439 memcpy((char *) (dent + 1), "..", 2);
440 437
441 gfs2_dinode_out(&ip->i_di, (char *)di); 438 gfs2_dinode_out(&ip->i_di, (char *)di);
442 439
@@ -687,7 +684,7 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
687 if (error) 684 if (error)
688 goto out_gunlock; 685 goto out_gunlock;
689 686
690 error = gfs2_dir_search(ndip, &ndentry->d_name, NULL, NULL); 687 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
691 switch (error) { 688 switch (error) {
692 case -ENOENT: 689 case -ENOENT:
693 error = 0; 690 error = 0;
@@ -723,10 +720,10 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
723 goto out_gunlock; 720 goto out_gunlock;
724 } 721 }
725 722
726 error = gfs2_diradd_alloc_required(ndip, &ndentry->d_name, 723 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
727 &alloc_required); 724 if (error < 0)
728 if (error)
729 goto out_gunlock; 725 goto out_gunlock;
726 error = 0;
730 727
731 if (alloc_required) { 728 if (alloc_required) {
732 struct gfs2_alloc *al = gfs2_alloc_get(ndip); 729 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
@@ -777,6 +774,7 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
777 struct qstr name; 774 struct qstr name;
778 name.len = 2; 775 name.len = 2;
779 name.name = ".."; 776 name.name = "..";
777 name.hash = gfs2_disk_hash(name.name, name.len);
780 778
781 error = gfs2_change_nlink(ndip, +1); 779 error = gfs2_change_nlink(ndip, +1);
782 if (error) 780 if (error)
@@ -803,7 +801,7 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
803 if (error) 801 if (error)
804 goto out_end_trans; 802 goto out_end_trans;
805 803
806 error = gfs2_dir_add(ndip, &ndentry->d_name, &ip->i_num, 804 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
807 IF2DT(ip->i_di.di_mode)); 805 IF2DT(ip->i_di.di_mode));
808 if (error) 806 if (error)
809 goto out_end_trans; 807 goto out_end_trans;
diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index fff5a96f4152..71cca7629403 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -299,8 +299,9 @@ int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
299 break; 299 break;
300 300
301 name.len = sprintf(buf, "journal%u", sdp->sd_journals); 301 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
302 name.hash = gfs2_disk_hash(name.name, name.len);
302 303
303 error = gfs2_dir_search(sdp->sd_jindex->u.generic_ip, 304 error = gfs2_dir_search(sdp->sd_jindex,
304 &name, NULL, NULL); 305 &name, NULL, NULL);
305 if (error == -ENOENT) { 306 if (error == -ENOENT) {
306 error = 0; 307 error = 0;
@@ -317,8 +318,12 @@ int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
317 if (!jd) 318 if (!jd)
318 break; 319 break;
319 320
320 error = gfs2_lookupi(sdp->sd_jindex, &name, 1, &jd->jd_inode); 321 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
321 if (error) { 322 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
323 if (!jd->jd_inode)
324 error = -ENOENT;
325 else
326 error = PTR_ERR(jd->jd_inode);
322 kfree(jd); 327 kfree(jd);
323 break; 328 break;
324 } 329 }
diff --git a/include/linux/gfs2_ondisk.h b/include/linux/gfs2_ondisk.h
index 8d4f0445df47..a5fb4f99aa45 100644
--- a/include/linux/gfs2_ondisk.h
+++ b/include/linux/gfs2_ondisk.h
@@ -411,7 +411,7 @@ struct gfs2_quota_change {
411/* Translation functions */ 411/* Translation functions */
412 412
413extern void gfs2_inum_in(struct gfs2_inum *no, char *buf); 413extern void gfs2_inum_in(struct gfs2_inum *no, char *buf);
414extern void gfs2_inum_out(struct gfs2_inum *no, char *buf); 414extern void gfs2_inum_out(const struct gfs2_inum *no, char *buf);
415extern void gfs2_sb_in(struct gfs2_sb *sb, char *buf); 415extern void gfs2_sb_in(struct gfs2_sb *sb, char *buf);
416extern void gfs2_rindex_in(struct gfs2_rindex *ri, char *buf); 416extern void gfs2_rindex_in(struct gfs2_rindex *ri, char *buf);
417extern void gfs2_rindex_out(struct gfs2_rindex *ri, char *buf); 417extern void gfs2_rindex_out(struct gfs2_rindex *ri, char *buf);
@@ -421,7 +421,6 @@ extern void gfs2_quota_in(struct gfs2_quota *qu, char *buf);
421extern void gfs2_quota_out(struct gfs2_quota *qu, char *buf); 421extern void gfs2_quota_out(struct gfs2_quota *qu, char *buf);
422extern void gfs2_dinode_in(struct gfs2_dinode *di, char *buf); 422extern void gfs2_dinode_in(struct gfs2_dinode *di, char *buf);
423extern void gfs2_dinode_out(struct gfs2_dinode *di, char *buf); 423extern void gfs2_dinode_out(struct gfs2_dinode *di, char *buf);
424extern void gfs2_leaf_in(struct gfs2_leaf *lf, char *buf);
425extern void gfs2_ea_header_in(struct gfs2_ea_header *ea, char *buf); 424extern void gfs2_ea_header_in(struct gfs2_ea_header *ea, char *buf);
426extern void gfs2_ea_header_out(struct gfs2_ea_header *ea, char *buf); 425extern void gfs2_ea_header_out(struct gfs2_ea_header *ea, char *buf);
427extern void gfs2_log_header_in(struct gfs2_log_header *lh, char *buf); 426extern void gfs2_log_header_in(struct gfs2_log_header *lh, char *buf);