aboutsummaryrefslogtreecommitdiffstats
path: root/fs/gfs2
diff options
context:
space:
mode:
Diffstat (limited to 'fs/gfs2')
-rw-r--r--fs/gfs2/Kconfig2
-rw-r--r--fs/gfs2/Makefile6
-rw-r--r--fs/gfs2/acl.c8
-rw-r--r--fs/gfs2/acl.h2
-rw-r--r--fs/gfs2/aops.c44
-rw-r--r--fs/gfs2/bmap.c288
-rw-r--r--fs/gfs2/bmap.h20
-rw-r--r--fs/gfs2/dentry.c24
-rw-r--r--fs/gfs2/dir.c228
-rw-r--r--fs/gfs2/dir.h34
-rw-r--r--fs/gfs2/export.c76
-rw-r--r--fs/gfs2/file.c379
-rw-r--r--fs/gfs2/glock.c577
-rw-r--r--fs/gfs2/glock.h68
-rw-r--r--fs/gfs2/glops.c214
-rw-r--r--fs/gfs2/glops.h2
-rw-r--r--fs/gfs2/incore.h38
-rw-r--r--fs/gfs2/inode.c1751
-rw-r--r--fs/gfs2/inode.h33
-rw-r--r--fs/gfs2/lock_dlm.c33
-rw-r--r--fs/gfs2/log.c248
-rw-r--r--fs/gfs2/log.h2
-rw-r--r--fs/gfs2/lops.c55
-rw-r--r--fs/gfs2/main.c24
-rw-r--r--fs/gfs2/meta_io.c9
-rw-r--r--fs/gfs2/meta_io.h2
-rw-r--r--fs/gfs2/ops_fstype.c185
-rw-r--r--fs/gfs2/ops_inode.c1390
-rw-r--r--fs/gfs2/quota.c70
-rw-r--r--fs/gfs2/quota.h4
-rw-r--r--fs/gfs2/recovery.c15
-rw-r--r--fs/gfs2/rgrp.c244
-rw-r--r--fs/gfs2/rgrp.h11
-rw-r--r--fs/gfs2/super.c219
-rw-r--r--fs/gfs2/sys.c35
-rw-r--r--fs/gfs2/trace_gfs2.h39
-rw-r--r--fs/gfs2/trans.h9
-rw-r--r--fs/gfs2/xattr.c25
38 files changed, 3180 insertions, 3233 deletions
diff --git a/fs/gfs2/Kconfig b/fs/gfs2/Kconfig
index cc9665522148..c465ae066c62 100644
--- a/fs/gfs2/Kconfig
+++ b/fs/gfs2/Kconfig
@@ -1,6 +1,6 @@
1config GFS2_FS 1config GFS2_FS
2 tristate "GFS2 file system support" 2 tristate "GFS2 file system support"
3 depends on EXPERIMENTAL && (64BIT || LBDAF) 3 depends on (64BIT || LBDAF)
4 select DLM if GFS2_FS_LOCKING_DLM 4 select DLM if GFS2_FS_LOCKING_DLM
5 select CONFIGFS_FS if GFS2_FS_LOCKING_DLM 5 select CONFIGFS_FS if GFS2_FS_LOCKING_DLM
6 select SYSFS if GFS2_FS_LOCKING_DLM 6 select SYSFS if GFS2_FS_LOCKING_DLM
diff --git a/fs/gfs2/Makefile b/fs/gfs2/Makefile
index 21f7e46da4c0..86128202384f 100644
--- a/fs/gfs2/Makefile
+++ b/fs/gfs2/Makefile
@@ -1,9 +1,9 @@
1EXTRA_CFLAGS := -I$(src) 1ccflags-y := -I$(src)
2obj-$(CONFIG_GFS2_FS) += gfs2.o 2obj-$(CONFIG_GFS2_FS) += gfs2.o
3gfs2-y := acl.o bmap.o dir.o xattr.o glock.o \ 3gfs2-y := acl.o bmap.o dir.o xattr.o glock.o \
4 glops.o inode.o log.o lops.o main.o meta_io.o \ 4 glops.o log.o lops.o main.o meta_io.o \
5 aops.o dentry.o export.o file.o \ 5 aops.o dentry.o export.o file.o \
6 ops_fstype.o ops_inode.o quota.o \ 6 ops_fstype.o inode.o quota.o \
7 recovery.o rgrp.o super.o sys.o trans.o util.o 7 recovery.o rgrp.o super.o sys.o trans.o util.o
8 8
9gfs2-$(CONFIG_GFS2_FS_LOCKING_DLM) += lock_dlm.o 9gfs2-$(CONFIG_GFS2_FS_LOCKING_DLM) += lock_dlm.o
diff --git a/fs/gfs2/acl.c b/fs/gfs2/acl.c
index 48171f4c943d..cbc07155b1a0 100644
--- a/fs/gfs2/acl.c
+++ b/fs/gfs2/acl.c
@@ -75,11 +75,17 @@ static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
75 * Returns: errno 75 * Returns: errno
76 */ 76 */
77 77
78int gfs2_check_acl(struct inode *inode, int mask) 78int gfs2_check_acl(struct inode *inode, int mask, unsigned int flags)
79{ 79{
80 struct posix_acl *acl; 80 struct posix_acl *acl;
81 int error; 81 int error;
82 82
83 if (flags & IPERM_FLAG_RCU) {
84 if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
85 return -ECHILD;
86 return -EAGAIN;
87 }
88
83 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS); 89 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
84 if (IS_ERR(acl)) 90 if (IS_ERR(acl))
85 return PTR_ERR(acl); 91 return PTR_ERR(acl);
diff --git a/fs/gfs2/acl.h b/fs/gfs2/acl.h
index b522b0cb39ea..a93907c8159b 100644
--- a/fs/gfs2/acl.h
+++ b/fs/gfs2/acl.h
@@ -16,7 +16,7 @@
16#define GFS2_POSIX_ACL_DEFAULT "posix_acl_default" 16#define GFS2_POSIX_ACL_DEFAULT "posix_acl_default"
17#define GFS2_ACL_MAX_ENTRIES 25 17#define GFS2_ACL_MAX_ENTRIES 25
18 18
19extern int gfs2_check_acl(struct inode *inode, int mask); 19extern int gfs2_check_acl(struct inode *inode, int mask, unsigned int);
20extern int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode); 20extern int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode);
21extern int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr); 21extern int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr);
22extern const struct xattr_handler gfs2_xattr_system_handler; 22extern const struct xattr_handler gfs2_xattr_system_handler;
diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
index 194fe16d8418..f9fbbe96c222 100644
--- a/fs/gfs2/aops.c
+++ b/fs/gfs2/aops.c
@@ -36,8 +36,8 @@
36#include "glops.h" 36#include "glops.h"
37 37
38 38
39static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page, 39void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
40 unsigned int from, unsigned int to) 40 unsigned int from, unsigned int to)
41{ 41{
42 struct buffer_head *head = page_buffers(page); 42 struct buffer_head *head = page_buffers(page);
43 unsigned int bsize = head->b_size; 43 unsigned int bsize = head->b_size;
@@ -615,10 +615,9 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
615 unsigned int data_blocks = 0, ind_blocks = 0, rblocks; 615 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
616 int alloc_required; 616 int alloc_required;
617 int error = 0; 617 int error = 0;
618 struct gfs2_alloc *al; 618 struct gfs2_alloc *al = NULL;
619 pgoff_t index = pos >> PAGE_CACHE_SHIFT; 619 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
620 unsigned from = pos & (PAGE_CACHE_SIZE - 1); 620 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
621 unsigned to = from + len;
622 struct page *page; 621 struct page *page;
623 622
624 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh); 623 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
@@ -663,6 +662,8 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
663 rblocks += RES_STATFS + RES_QUOTA; 662 rblocks += RES_STATFS + RES_QUOTA;
664 if (&ip->i_inode == sdp->sd_rindex) 663 if (&ip->i_inode == sdp->sd_rindex)
665 rblocks += 2 * RES_STATFS; 664 rblocks += 2 * RES_STATFS;
665 if (alloc_required)
666 rblocks += gfs2_rg_blocks(al);
666 667
667 error = gfs2_trans_begin(sdp, rblocks, 668 error = gfs2_trans_begin(sdp, rblocks,
668 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize); 669 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
@@ -689,20 +690,19 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
689 } 690 }
690 691
691prepare_write: 692prepare_write:
692 error = block_prepare_write(page, from, to, gfs2_block_map); 693 error = __block_write_begin(page, from, len, gfs2_block_map);
693out: 694out:
694 if (error == 0) 695 if (error == 0)
695 return 0; 696 return 0;
696 697
698 unlock_page(page);
697 page_cache_release(page); 699 page_cache_release(page);
698 700
699 /* 701 gfs2_trans_end(sdp);
700 * XXX(truncate): the call below should probably be replaced with
701 * a call to the gfs2-specific truncate blocks helper to actually
702 * release disk blocks..
703 */
704 if (pos + len > ip->i_inode.i_size) 702 if (pos + len > ip->i_inode.i_size)
705 truncate_setsize(&ip->i_inode, ip->i_inode.i_size); 703 gfs2_trim_blocks(&ip->i_inode);
704 goto out_trans_fail;
705
706out_endtrans: 706out_endtrans:
707 gfs2_trans_end(sdp); 707 gfs2_trans_end(sdp);
708out_trans_fail: 708out_trans_fail:
@@ -802,10 +802,8 @@ static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
802 page_cache_release(page); 802 page_cache_release(page);
803 803
804 if (copied) { 804 if (copied) {
805 if (inode->i_size < to) { 805 if (inode->i_size < to)
806 i_size_write(inode, to); 806 i_size_write(inode, to);
807 ip->i_disksize = inode->i_size;
808 }
809 gfs2_dinode_out(ip, di); 807 gfs2_dinode_out(ip, di);
810 mark_inode_dirty(inode); 808 mark_inode_dirty(inode);
811 } 809 }
@@ -876,8 +874,6 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
876 874
877 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); 875 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
878 if (ret > 0) { 876 if (ret > 0) {
879 if (inode->i_size > ip->i_disksize)
880 ip->i_disksize = inode->i_size;
881 gfs2_dinode_out(ip, dibh->b_data); 877 gfs2_dinode_out(ip, dibh->b_data);
882 mark_inode_dirty(inode); 878 mark_inode_dirty(inode);
883 } 879 }
@@ -888,8 +884,8 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
888 } 884 }
889 885
890 brelse(dibh); 886 brelse(dibh);
891 gfs2_trans_end(sdp);
892failed: 887failed:
888 gfs2_trans_end(sdp);
893 if (al) { 889 if (al) {
894 gfs2_inplace_release(ip); 890 gfs2_inplace_release(ip);
895 gfs2_quota_unlock(ip); 891 gfs2_quota_unlock(ip);
@@ -1073,6 +1069,7 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
1073 return 0; 1069 return 0;
1074 1070
1075 gfs2_log_lock(sdp); 1071 gfs2_log_lock(sdp);
1072 spin_lock(&sdp->sd_ail_lock);
1076 head = bh = page_buffers(page); 1073 head = bh = page_buffers(page);
1077 do { 1074 do {
1078 if (atomic_read(&bh->b_count)) 1075 if (atomic_read(&bh->b_count))
@@ -1080,10 +1077,11 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
1080 bd = bh->b_private; 1077 bd = bh->b_private;
1081 if (bd && bd->bd_ail) 1078 if (bd && bd->bd_ail)
1082 goto cannot_release; 1079 goto cannot_release;
1083 gfs2_assert_warn(sdp, !buffer_pinned(bh)); 1080 if (buffer_pinned(bh) || buffer_dirty(bh))
1084 gfs2_assert_warn(sdp, !buffer_dirty(bh)); 1081 goto not_possible;
1085 bh = bh->b_this_page; 1082 bh = bh->b_this_page;
1086 } while(bh != head); 1083 } while(bh != head);
1084 spin_unlock(&sdp->sd_ail_lock);
1087 gfs2_log_unlock(sdp); 1085 gfs2_log_unlock(sdp);
1088 1086
1089 head = bh = page_buffers(page); 1087 head = bh = page_buffers(page);
@@ -1111,7 +1109,12 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
1111 } while (bh != head); 1109 } while (bh != head);
1112 1110
1113 return try_to_free_buffers(page); 1111 return try_to_free_buffers(page);
1112
1113not_possible: /* Should never happen */
1114 WARN_ON(buffer_dirty(bh));
1115 WARN_ON(buffer_pinned(bh));
1114cannot_release: 1116cannot_release:
1117 spin_unlock(&sdp->sd_ail_lock);
1115 gfs2_log_unlock(sdp); 1118 gfs2_log_unlock(sdp);
1116 return 0; 1119 return 0;
1117} 1120}
@@ -1121,7 +1124,6 @@ static const struct address_space_operations gfs2_writeback_aops = {
1121 .writepages = gfs2_writeback_writepages, 1124 .writepages = gfs2_writeback_writepages,
1122 .readpage = gfs2_readpage, 1125 .readpage = gfs2_readpage,
1123 .readpages = gfs2_readpages, 1126 .readpages = gfs2_readpages,
1124 .sync_page = block_sync_page,
1125 .write_begin = gfs2_write_begin, 1127 .write_begin = gfs2_write_begin,
1126 .write_end = gfs2_write_end, 1128 .write_end = gfs2_write_end,
1127 .bmap = gfs2_bmap, 1129 .bmap = gfs2_bmap,
@@ -1137,7 +1139,6 @@ static const struct address_space_operations gfs2_ordered_aops = {
1137 .writepage = gfs2_ordered_writepage, 1139 .writepage = gfs2_ordered_writepage,
1138 .readpage = gfs2_readpage, 1140 .readpage = gfs2_readpage,
1139 .readpages = gfs2_readpages, 1141 .readpages = gfs2_readpages,
1140 .sync_page = block_sync_page,
1141 .write_begin = gfs2_write_begin, 1142 .write_begin = gfs2_write_begin,
1142 .write_end = gfs2_write_end, 1143 .write_end = gfs2_write_end,
1143 .set_page_dirty = gfs2_set_page_dirty, 1144 .set_page_dirty = gfs2_set_page_dirty,
@@ -1155,7 +1156,6 @@ static const struct address_space_operations gfs2_jdata_aops = {
1155 .writepages = gfs2_jdata_writepages, 1156 .writepages = gfs2_jdata_writepages,
1156 .readpage = gfs2_readpage, 1157 .readpage = gfs2_readpage,
1157 .readpages = gfs2_readpages, 1158 .readpages = gfs2_readpages,
1158 .sync_page = block_sync_page,
1159 .write_begin = gfs2_write_begin, 1159 .write_begin = gfs2_write_begin,
1160 .write_end = gfs2_write_end, 1160 .write_end = gfs2_write_end,
1161 .set_page_dirty = gfs2_set_page_dirty, 1161 .set_page_dirty = gfs2_set_page_dirty,
diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 6f482809d1a3..e65493a8ac00 100644
--- a/fs/gfs2/bmap.c
+++ b/fs/gfs2/bmap.c
@@ -21,6 +21,7 @@
21#include "meta_io.h" 21#include "meta_io.h"
22#include "quota.h" 22#include "quota.h"
23#include "rgrp.h" 23#include "rgrp.h"
24#include "super.h"
24#include "trans.h" 25#include "trans.h"
25#include "dir.h" 26#include "dir.h"
26#include "util.h" 27#include "util.h"
@@ -50,7 +51,7 @@ struct strip_mine {
50 * @ip: the inode 51 * @ip: the inode
51 * @dibh: the dinode buffer 52 * @dibh: the dinode buffer
52 * @block: the block number that was allocated 53 * @block: the block number that was allocated
53 * @private: any locked page held by the caller process 54 * @page: The (optional) page. This is looked up if @page is NULL
54 * 55 *
55 * Returns: errno 56 * Returns: errno
56 */ 57 */
@@ -109,8 +110,7 @@ static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
109/** 110/**
110 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big 111 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
111 * @ip: The GFS2 inode to unstuff 112 * @ip: The GFS2 inode to unstuff
112 * @unstuffer: the routine that handles unstuffing a non-zero length file 113 * @page: The (optional) page. This is looked up if the @page is NULL
113 * @private: private data for the unstuffer
114 * 114 *
115 * This routine unstuffs a dinode and returns it to a "normal" state such 115 * This routine unstuffs a dinode and returns it to a "normal" state such
116 * that the height can be grown in the traditional way. 116 * that the height can be grown in the traditional way.
@@ -132,7 +132,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
132 if (error) 132 if (error)
133 goto out; 133 goto out;
134 134
135 if (ip->i_disksize) { 135 if (i_size_read(&ip->i_inode)) {
136 /* Get a free block, fill it with the stuffed data, 136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */ 137 and write it out to disk */
138 138
@@ -161,7 +161,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
161 di = (struct gfs2_dinode *)dibh->b_data; 161 di = (struct gfs2_dinode *)dibh->b_data;
162 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 162 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
163 163
164 if (ip->i_disksize) { 164 if (i_size_read(&ip->i_inode)) {
165 *(__be64 *)(di + 1) = cpu_to_be64(block); 165 *(__be64 *)(di + 1) = cpu_to_be64(block);
166 gfs2_add_inode_blocks(&ip->i_inode, 1); 166 gfs2_add_inode_blocks(&ip->i_inode, 1);
167 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode)); 167 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
@@ -758,13 +758,13 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
758 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 758 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
759 struct gfs2_rgrp_list rlist; 759 struct gfs2_rgrp_list rlist;
760 u64 bn, bstart; 760 u64 bn, bstart;
761 u32 blen; 761 u32 blen, btotal;
762 __be64 *p; 762 __be64 *p;
763 unsigned int rg_blocks = 0; 763 unsigned int rg_blocks = 0;
764 int metadata; 764 int metadata;
765 unsigned int revokes = 0; 765 unsigned int revokes = 0;
766 int x; 766 int x;
767 int error; 767 int error = 0;
768 768
769 if (!*top) 769 if (!*top)
770 sm->sm_first = 0; 770 sm->sm_first = 0;
@@ -780,8 +780,14 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
780 metadata = (height != ip->i_height - 1); 780 metadata = (height != ip->i_height - 1);
781 if (metadata) 781 if (metadata)
782 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs; 782 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
783 else if (ip->i_depth)
784 revokes = sdp->sd_inptrs;
785
786 if (ip != GFS2_I(sdp->sd_rindex))
787 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
788 else if (!sdp->sd_rgrps)
789 error = gfs2_ri_update(ip);
783 790
784 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
785 if (error) 791 if (error)
786 return error; 792 return error;
787 793
@@ -836,6 +842,7 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
836 842
837 bstart = 0; 843 bstart = 0;
838 blen = 0; 844 blen = 0;
845 btotal = 0;
839 846
840 for (p = top; p < bottom; p++) { 847 for (p = top; p < bottom; p++) {
841 if (!*p) 848 if (!*p)
@@ -848,9 +855,11 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
848 else { 855 else {
849 if (bstart) { 856 if (bstart) {
850 if (metadata) 857 if (metadata)
851 gfs2_free_meta(ip, bstart, blen); 858 __gfs2_free_meta(ip, bstart, blen);
852 else 859 else
853 gfs2_free_data(ip, bstart, blen); 860 __gfs2_free_data(ip, bstart, blen);
861
862 btotal += blen;
854 } 863 }
855 864
856 bstart = bn; 865 bstart = bn;
@@ -862,11 +871,17 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
862 } 871 }
863 if (bstart) { 872 if (bstart) {
864 if (metadata) 873 if (metadata)
865 gfs2_free_meta(ip, bstart, blen); 874 __gfs2_free_meta(ip, bstart, blen);
866 else 875 else
867 gfs2_free_data(ip, bstart, blen); 876 __gfs2_free_data(ip, bstart, blen);
877
878 btotal += blen;
868 } 879 }
869 880
881 gfs2_statfs_change(sdp, 0, +btotal, 0);
882 gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
883 ip->i_inode.i_gid);
884
870 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; 885 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
871 886
872 gfs2_dinode_out(ip, dibh->b_data); 887 gfs2_dinode_out(ip, dibh->b_data);
@@ -880,88 +895,20 @@ out_rg_gunlock:
880out_rlist: 895out_rlist:
881 gfs2_rlist_free(&rlist); 896 gfs2_rlist_free(&rlist);
882out: 897out:
883 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh); 898 if (ip != GFS2_I(sdp->sd_rindex))
884 return error; 899 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
885}
886
887/**
888 * do_grow - Make a file look bigger than it is
889 * @ip: the inode
890 * @size: the size to set the file to
891 *
892 * Called with an exclusive lock on @ip.
893 *
894 * Returns: errno
895 */
896
897static int do_grow(struct gfs2_inode *ip, u64 size)
898{
899 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
900 struct gfs2_alloc *al;
901 struct buffer_head *dibh;
902 int error;
903
904 al = gfs2_alloc_get(ip);
905 if (!al)
906 return -ENOMEM;
907
908 error = gfs2_quota_lock_check(ip);
909 if (error)
910 goto out;
911
912 al->al_requested = sdp->sd_max_height + RES_DATA;
913
914 error = gfs2_inplace_reserve(ip);
915 if (error)
916 goto out_gunlock_q;
917
918 error = gfs2_trans_begin(sdp,
919 sdp->sd_max_height + al->al_rgd->rd_length +
920 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
921 if (error)
922 goto out_ipres;
923
924 error = gfs2_meta_inode_buffer(ip, &dibh);
925 if (error)
926 goto out_end_trans;
927
928 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
929 if (gfs2_is_stuffed(ip)) {
930 error = gfs2_unstuff_dinode(ip, NULL);
931 if (error)
932 goto out_brelse;
933 }
934 }
935
936 ip->i_disksize = size;
937 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
938 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
939 gfs2_dinode_out(ip, dibh->b_data);
940
941out_brelse:
942 brelse(dibh);
943out_end_trans:
944 gfs2_trans_end(sdp);
945out_ipres:
946 gfs2_inplace_release(ip);
947out_gunlock_q:
948 gfs2_quota_unlock(ip);
949out:
950 gfs2_alloc_put(ip);
951 return error; 900 return error;
952} 901}
953 902
954
955/** 903/**
956 * gfs2_block_truncate_page - Deal with zeroing out data for truncate 904 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
957 * 905 *
958 * This is partly borrowed from ext3. 906 * This is partly borrowed from ext3.
959 */ 907 */
960static int gfs2_block_truncate_page(struct address_space *mapping) 908static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
961{ 909{
962 struct inode *inode = mapping->host; 910 struct inode *inode = mapping->host;
963 struct gfs2_inode *ip = GFS2_I(inode); 911 struct gfs2_inode *ip = GFS2_I(inode);
964 loff_t from = inode->i_size;
965 unsigned long index = from >> PAGE_CACHE_SHIFT; 912 unsigned long index = from >> PAGE_CACHE_SHIFT;
966 unsigned offset = from & (PAGE_CACHE_SIZE-1); 913 unsigned offset = from & (PAGE_CACHE_SIZE-1);
967 unsigned blocksize, iblock, length, pos; 914 unsigned blocksize, iblock, length, pos;
@@ -1023,9 +970,11 @@ unlock:
1023 return err; 970 return err;
1024} 971}
1025 972
1026static int trunc_start(struct gfs2_inode *ip, u64 size) 973static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
1027{ 974{
1028 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 975 struct gfs2_inode *ip = GFS2_I(inode);
976 struct gfs2_sbd *sdp = GFS2_SB(inode);
977 struct address_space *mapping = inode->i_mapping;
1029 struct buffer_head *dibh; 978 struct buffer_head *dibh;
1030 int journaled = gfs2_is_jdata(ip); 979 int journaled = gfs2_is_jdata(ip);
1031 int error; 980 int error;
@@ -1039,31 +988,26 @@ static int trunc_start(struct gfs2_inode *ip, u64 size)
1039 if (error) 988 if (error)
1040 goto out; 989 goto out;
1041 990
991 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
992
1042 if (gfs2_is_stuffed(ip)) { 993 if (gfs2_is_stuffed(ip)) {
1043 u64 dsize = size + sizeof(struct gfs2_dinode); 994 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
1044 ip->i_disksize = size;
1045 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1046 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1047 gfs2_dinode_out(ip, dibh->b_data);
1048 if (dsize > dibh->b_size)
1049 dsize = dibh->b_size;
1050 gfs2_buffer_clear_tail(dibh, dsize);
1051 error = 1;
1052 } else { 995 } else {
1053 if (size & (u64)(sdp->sd_sb.sb_bsize - 1)) 996 if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
1054 error = gfs2_block_truncate_page(ip->i_inode.i_mapping); 997 error = gfs2_block_truncate_page(mapping, newsize);
1055 998 if (error)
1056 if (!error) { 999 goto out_brelse;
1057 ip->i_disksize = size;
1058 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1059 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1060 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1061 gfs2_dinode_out(ip, dibh->b_data);
1062 } 1000 }
1001 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1063 } 1002 }
1064 1003
1065 brelse(dibh); 1004 i_size_write(inode, newsize);
1005 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1006 gfs2_dinode_out(ip, dibh->b_data);
1066 1007
1008 truncate_pagecache(inode, oldsize, newsize);
1009out_brelse:
1010 brelse(dibh);
1067out: 1011out:
1068 gfs2_trans_end(sdp); 1012 gfs2_trans_end(sdp);
1069 return error; 1013 return error;
@@ -1123,7 +1067,7 @@ static int trunc_end(struct gfs2_inode *ip)
1123 if (error) 1067 if (error)
1124 goto out; 1068 goto out;
1125 1069
1126 if (!ip->i_disksize) { 1070 if (!i_size_read(&ip->i_inode)) {
1127 ip->i_height = 0; 1071 ip->i_height = 0;
1128 ip->i_goal = ip->i_no_addr; 1072 ip->i_goal = ip->i_no_addr;
1129 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 1073 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
@@ -1143,92 +1087,154 @@ out:
1143 1087
1144/** 1088/**
1145 * do_shrink - make a file smaller 1089 * do_shrink - make a file smaller
1146 * @ip: the inode 1090 * @inode: the inode
1147 * @size: the size to make the file 1091 * @oldsize: the current inode size
1148 * @truncator: function to truncate the last partial block 1092 * @newsize: the size to make the file
1149 * 1093 *
1150 * Called with an exclusive lock on @ip. 1094 * Called with an exclusive lock on @inode. The @size must
1095 * be equal to or smaller than the current inode size.
1151 * 1096 *
1152 * Returns: errno 1097 * Returns: errno
1153 */ 1098 */
1154 1099
1155static int do_shrink(struct gfs2_inode *ip, u64 size) 1100static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
1156{ 1101{
1102 struct gfs2_inode *ip = GFS2_I(inode);
1157 int error; 1103 int error;
1158 1104
1159 error = trunc_start(ip, size); 1105 error = trunc_start(inode, oldsize, newsize);
1160 if (error < 0) 1106 if (error < 0)
1161 return error; 1107 return error;
1162 if (error > 0) 1108 if (gfs2_is_stuffed(ip))
1163 return 0; 1109 return 0;
1164 1110
1165 error = trunc_dealloc(ip, size); 1111 error = trunc_dealloc(ip, newsize);
1166 if (!error) 1112 if (error == 0)
1167 error = trunc_end(ip); 1113 error = trunc_end(ip);
1168 1114
1169 return error; 1115 return error;
1170} 1116}
1171 1117
1172static int do_touch(struct gfs2_inode *ip, u64 size) 1118void gfs2_trim_blocks(struct inode *inode)
1173{ 1119{
1174 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1120 u64 size = inode->i_size;
1121 int ret;
1122
1123 ret = do_shrink(inode, size, size);
1124 WARN_ON(ret != 0);
1125}
1126
1127/**
1128 * do_grow - Touch and update inode size
1129 * @inode: The inode
1130 * @size: The new size
1131 *
1132 * This function updates the timestamps on the inode and
1133 * may also increase the size of the inode. This function
1134 * must not be called with @size any smaller than the current
1135 * inode size.
1136 *
1137 * Although it is not strictly required to unstuff files here,
1138 * earlier versions of GFS2 have a bug in the stuffed file reading
1139 * code which will result in a buffer overrun if the size is larger
1140 * than the max stuffed file size. In order to prevent this from
1141 * occurring, such files are unstuffed, but in other cases we can
1142 * just update the inode size directly.
1143 *
1144 * Returns: 0 on success, or -ve on error
1145 */
1146
1147static int do_grow(struct inode *inode, u64 size)
1148{
1149 struct gfs2_inode *ip = GFS2_I(inode);
1150 struct gfs2_sbd *sdp = GFS2_SB(inode);
1175 struct buffer_head *dibh; 1151 struct buffer_head *dibh;
1152 struct gfs2_alloc *al = NULL;
1176 int error; 1153 int error;
1177 1154
1178 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 1155 if (gfs2_is_stuffed(ip) &&
1156 (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
1157 al = gfs2_alloc_get(ip);
1158 if (al == NULL)
1159 return -ENOMEM;
1160
1161 error = gfs2_quota_lock_check(ip);
1162 if (error)
1163 goto do_grow_alloc_put;
1164
1165 al->al_requested = 1;
1166 error = gfs2_inplace_reserve(ip);
1167 if (error)
1168 goto do_grow_qunlock;
1169 }
1170
1171 error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT, 0);
1179 if (error) 1172 if (error)
1180 return error; 1173 goto do_grow_release;
1181 1174
1182 down_write(&ip->i_rw_mutex); 1175 if (al) {
1176 error = gfs2_unstuff_dinode(ip, NULL);
1177 if (error)
1178 goto do_end_trans;
1179 }
1183 1180
1184 error = gfs2_meta_inode_buffer(ip, &dibh); 1181 error = gfs2_meta_inode_buffer(ip, &dibh);
1185 if (error) 1182 if (error)
1186 goto do_touch_out; 1183 goto do_end_trans;
1187 1184
1185 i_size_write(inode, size);
1188 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; 1186 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1189 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 1187 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1190 gfs2_dinode_out(ip, dibh->b_data); 1188 gfs2_dinode_out(ip, dibh->b_data);
1191 brelse(dibh); 1189 brelse(dibh);
1192 1190
1193do_touch_out: 1191do_end_trans:
1194 up_write(&ip->i_rw_mutex);
1195 gfs2_trans_end(sdp); 1192 gfs2_trans_end(sdp);
1193do_grow_release:
1194 if (al) {
1195 gfs2_inplace_release(ip);
1196do_grow_qunlock:
1197 gfs2_quota_unlock(ip);
1198do_grow_alloc_put:
1199 gfs2_alloc_put(ip);
1200 }
1196 return error; 1201 return error;
1197} 1202}
1198 1203
1199/** 1204/**
1200 * gfs2_truncatei - make a file a given size 1205 * gfs2_setattr_size - make a file a given size
1201 * @ip: the inode 1206 * @inode: the inode
1202 * @size: the size to make the file 1207 * @newsize: the size to make the file
1203 * @truncator: function to truncate the last partial block
1204 * 1208 *
1205 * The file size can grow, shrink, or stay the same size. 1209 * The file size can grow, shrink, or stay the same size. This
1210 * is called holding i_mutex and an exclusive glock on the inode
1211 * in question.
1206 * 1212 *
1207 * Returns: errno 1213 * Returns: errno
1208 */ 1214 */
1209 1215
1210int gfs2_truncatei(struct gfs2_inode *ip, u64 size) 1216int gfs2_setattr_size(struct inode *inode, u64 newsize)
1211{ 1217{
1212 int error; 1218 int ret;
1219 u64 oldsize;
1213 1220
1214 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode))) 1221 BUG_ON(!S_ISREG(inode->i_mode));
1215 return -EINVAL;
1216 1222
1217 if (size > ip->i_disksize) 1223 ret = inode_newsize_ok(inode, newsize);
1218 error = do_grow(ip, size); 1224 if (ret)
1219 else if (size < ip->i_disksize) 1225 return ret;
1220 error = do_shrink(ip, size);
1221 else
1222 /* update time stamps */
1223 error = do_touch(ip, size);
1224 1226
1225 return error; 1227 oldsize = inode->i_size;
1228 if (newsize >= oldsize)
1229 return do_grow(inode, newsize);
1230
1231 return do_shrink(inode, oldsize, newsize);
1226} 1232}
1227 1233
1228int gfs2_truncatei_resume(struct gfs2_inode *ip) 1234int gfs2_truncatei_resume(struct gfs2_inode *ip)
1229{ 1235{
1230 int error; 1236 int error;
1231 error = trunc_dealloc(ip, ip->i_disksize); 1237 error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
1232 if (!error) 1238 if (!error)
1233 error = trunc_end(ip); 1239 error = trunc_end(ip);
1234 return error; 1240 return error;
@@ -1269,7 +1275,7 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1269 1275
1270 shift = sdp->sd_sb.sb_bsize_shift; 1276 shift = sdp->sd_sb.sb_bsize_shift;
1271 BUG_ON(gfs2_is_dir(ip)); 1277 BUG_ON(gfs2_is_dir(ip));
1272 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift; 1278 end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
1273 lblock = offset >> shift; 1279 lblock = offset >> shift;
1274 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; 1280 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1275 if (lblock_stop > end_of_file) 1281 if (lblock_stop > end_of_file)
diff --git a/fs/gfs2/bmap.h b/fs/gfs2/bmap.h
index a20a5213135a..42fea03e2bd9 100644
--- a/fs/gfs2/bmap.h
+++ b/fs/gfs2/bmap.h
@@ -44,14 +44,16 @@ static inline void gfs2_write_calc_reserv(const struct gfs2_inode *ip,
44 } 44 }
45} 45}
46 46
47int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page); 47extern int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page);
48int gfs2_block_map(struct inode *inode, sector_t lblock, struct buffer_head *bh, int create); 48extern int gfs2_block_map(struct inode *inode, sector_t lblock,
49int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen); 49 struct buffer_head *bh, int create);
50 50extern int gfs2_extent_map(struct inode *inode, u64 lblock, int *new,
51int gfs2_truncatei(struct gfs2_inode *ip, u64 size); 51 u64 *dblock, unsigned *extlen);
52int gfs2_truncatei_resume(struct gfs2_inode *ip); 52extern int gfs2_setattr_size(struct inode *inode, u64 size);
53int gfs2_file_dealloc(struct gfs2_inode *ip); 53extern void gfs2_trim_blocks(struct inode *inode);
54int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset, 54extern int gfs2_truncatei_resume(struct gfs2_inode *ip);
55 unsigned int len); 55extern int gfs2_file_dealloc(struct gfs2_inode *ip);
56extern int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
57 unsigned int len);
56 58
57#endif /* __BMAP_DOT_H__ */ 59#endif /* __BMAP_DOT_H__ */
diff --git a/fs/gfs2/dentry.c b/fs/gfs2/dentry.c
index bb7907bde3d8..0da8da2c991d 100644
--- a/fs/gfs2/dentry.c
+++ b/fs/gfs2/dentry.c
@@ -11,6 +11,7 @@
11#include <linux/completion.h> 11#include <linux/completion.h>
12#include <linux/buffer_head.h> 12#include <linux/buffer_head.h>
13#include <linux/gfs2_ondisk.h> 13#include <linux/gfs2_ondisk.h>
14#include <linux/namei.h>
14#include <linux/crc32.h> 15#include <linux/crc32.h>
15 16
16#include "gfs2.h" 17#include "gfs2.h"
@@ -34,22 +35,30 @@
34 35
35static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd) 36static int gfs2_drevalidate(struct dentry *dentry, struct nameidata *nd)
36{ 37{
37 struct dentry *parent = dget_parent(dentry); 38 struct dentry *parent;
38 struct gfs2_sbd *sdp = GFS2_SB(parent->d_inode); 39 struct gfs2_sbd *sdp;
39 struct gfs2_inode *dip = GFS2_I(parent->d_inode); 40 struct gfs2_inode *dip;
40 struct inode *inode = dentry->d_inode; 41 struct inode *inode;
41 struct gfs2_holder d_gh; 42 struct gfs2_holder d_gh;
42 struct gfs2_inode *ip = NULL; 43 struct gfs2_inode *ip = NULL;
43 int error; 44 int error;
44 int had_lock = 0; 45 int had_lock = 0;
45 46
47 if (nd && nd->flags & LOOKUP_RCU)
48 return -ECHILD;
49
50 parent = dget_parent(dentry);
51 sdp = GFS2_SB(parent->d_inode);
52 dip = GFS2_I(parent->d_inode);
53 inode = dentry->d_inode;
54
46 if (inode) { 55 if (inode) {
47 if (is_bad_inode(inode)) 56 if (is_bad_inode(inode))
48 goto invalid; 57 goto invalid;
49 ip = GFS2_I(inode); 58 ip = GFS2_I(inode);
50 } 59 }
51 60
52 if (sdp->sd_args.ar_localcaching) 61 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
53 goto valid; 62 goto valid;
54 63
55 had_lock = (gfs2_glock_is_locked_by_me(dip->i_gl) != NULL); 64 had_lock = (gfs2_glock_is_locked_by_me(dip->i_gl) != NULL);
@@ -100,13 +109,14 @@ fail:
100 return 0; 109 return 0;
101} 110}
102 111
103static int gfs2_dhash(struct dentry *dentry, struct qstr *str) 112static int gfs2_dhash(const struct dentry *dentry, const struct inode *inode,
113 struct qstr *str)
104{ 114{
105 str->hash = gfs2_disk_hash(str->name, str->len); 115 str->hash = gfs2_disk_hash(str->name, str->len);
106 return 0; 116 return 0;
107} 117}
108 118
109static int gfs2_dentry_delete(struct dentry *dentry) 119static int gfs2_dentry_delete(const struct dentry *dentry)
110{ 120{
111 struct gfs2_inode *ginode; 121 struct gfs2_inode *ginode;
112 122
diff --git a/fs/gfs2/dir.c b/fs/gfs2/dir.c
index b9dd88a78dd4..091ee4779538 100644
--- a/fs/gfs2/dir.c
+++ b/fs/gfs2/dir.c
@@ -79,12 +79,12 @@
79#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1) 79#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
80#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1)) 80#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
81 81
82typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len, 82struct qstr gfs2_qdot __read_mostly;
83 u64 leaf_no, void *data); 83struct qstr gfs2_qdotdot __read_mostly;
84
84typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent, 85typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
85 const struct qstr *name, void *opaque); 86 const struct qstr *name, void *opaque);
86 87
87
88int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, 88int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
89 struct buffer_head **bhp) 89 struct buffer_head **bhp)
90{ 90{
@@ -127,8 +127,8 @@ static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
127 127
128 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 128 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
129 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); 129 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
130 if (ip->i_disksize < offset + size) 130 if (ip->i_inode.i_size < offset + size)
131 ip->i_disksize = offset + size; 131 i_size_write(&ip->i_inode, offset + size);
132 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; 132 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
133 gfs2_dinode_out(ip, dibh->b_data); 133 gfs2_dinode_out(ip, dibh->b_data);
134 134
@@ -225,8 +225,8 @@ out:
225 if (error) 225 if (error)
226 return error; 226 return error;
227 227
228 if (ip->i_disksize < offset + copied) 228 if (ip->i_inode.i_size < offset + copied)
229 ip->i_disksize = offset + copied; 229 i_size_write(&ip->i_inode, offset + copied);
230 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; 230 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
231 231
232 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 232 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
@@ -275,12 +275,13 @@ static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
275 unsigned int o; 275 unsigned int o;
276 int copied = 0; 276 int copied = 0;
277 int error = 0; 277 int error = 0;
278 u64 disksize = i_size_read(&ip->i_inode);
278 279
279 if (offset >= ip->i_disksize) 280 if (offset >= disksize)
280 return 0; 281 return 0;
281 282
282 if (offset + size > ip->i_disksize) 283 if (offset + size > disksize)
283 size = ip->i_disksize - offset; 284 size = disksize - offset;
284 285
285 if (!size) 286 if (!size)
286 return 0; 287 return 0;
@@ -727,7 +728,7 @@ static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
727 unsigned hsize = 1 << ip->i_depth; 728 unsigned hsize = 1 << ip->i_depth;
728 unsigned index; 729 unsigned index;
729 u64 ln; 730 u64 ln;
730 if (hsize * sizeof(u64) != ip->i_disksize) { 731 if (hsize * sizeof(u64) != i_size_read(inode)) {
731 gfs2_consist_inode(ip); 732 gfs2_consist_inode(ip);
732 return ERR_PTR(-EIO); 733 return ERR_PTR(-EIO);
733 } 734 }
@@ -879,7 +880,7 @@ static int dir_make_exhash(struct inode *inode)
879 for (x = sdp->sd_hash_ptrs; x--; lp++) 880 for (x = sdp->sd_hash_ptrs; x--; lp++)
880 *lp = cpu_to_be64(bn); 881 *lp = cpu_to_be64(bn);
881 882
882 dip->i_disksize = sdp->sd_sb.sb_bsize / 2; 883 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
883 gfs2_add_inode_blocks(&dip->i_inode, 1); 884 gfs2_add_inode_blocks(&dip->i_inode, 1);
884 dip->i_diskflags |= GFS2_DIF_EXHASH; 885 dip->i_diskflags |= GFS2_DIF_EXHASH;
885 886
@@ -1057,11 +1058,12 @@ static int dir_double_exhash(struct gfs2_inode *dip)
1057 u64 *buf; 1058 u64 *buf;
1058 u64 *from, *to; 1059 u64 *from, *to;
1059 u64 block; 1060 u64 block;
1061 u64 disksize = i_size_read(&dip->i_inode);
1060 int x; 1062 int x;
1061 int error = 0; 1063 int error = 0;
1062 1064
1063 hsize = 1 << dip->i_depth; 1065 hsize = 1 << dip->i_depth;
1064 if (hsize * sizeof(u64) != dip->i_disksize) { 1066 if (hsize * sizeof(u64) != disksize) {
1065 gfs2_consist_inode(dip); 1067 gfs2_consist_inode(dip);
1066 return -EIO; 1068 return -EIO;
1067 } 1069 }
@@ -1072,7 +1074,7 @@ static int dir_double_exhash(struct gfs2_inode *dip)
1072 if (!buf) 1074 if (!buf)
1073 return -ENOMEM; 1075 return -ENOMEM;
1074 1076
1075 for (block = dip->i_disksize >> sdp->sd_hash_bsize_shift; block--;) { 1077 for (block = disksize >> sdp->sd_hash_bsize_shift; block--;) {
1076 error = gfs2_dir_read_data(dip, (char *)buf, 1078 error = gfs2_dir_read_data(dip, (char *)buf,
1077 block * sdp->sd_hash_bsize, 1079 block * sdp->sd_hash_bsize,
1078 sdp->sd_hash_bsize, 1); 1080 sdp->sd_hash_bsize, 1);
@@ -1370,7 +1372,7 @@ static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
1370 unsigned depth = 0; 1372 unsigned depth = 0;
1371 1373
1372 hsize = 1 << dip->i_depth; 1374 hsize = 1 << dip->i_depth;
1373 if (hsize * sizeof(u64) != dip->i_disksize) { 1375 if (hsize * sizeof(u64) != i_size_read(inode)) {
1374 gfs2_consist_inode(dip); 1376 gfs2_consist_inode(dip);
1375 return -EIO; 1377 return -EIO;
1376 } 1378 }
@@ -1501,7 +1503,7 @@ struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
1501 inode = gfs2_inode_lookup(dir->i_sb, 1503 inode = gfs2_inode_lookup(dir->i_sb,
1502 be16_to_cpu(dent->de_type), 1504 be16_to_cpu(dent->de_type),
1503 be64_to_cpu(dent->de_inum.no_addr), 1505 be64_to_cpu(dent->de_inum.no_addr),
1504 be64_to_cpu(dent->de_inum.no_formal_ino)); 1506 be64_to_cpu(dent->de_inum.no_formal_ino), 0);
1505 brelse(bh); 1507 brelse(bh);
1506 return inode; 1508 return inode;
1507 } 1509 }
@@ -1595,7 +1597,7 @@ static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1595 */ 1597 */
1596 1598
1597int gfs2_dir_add(struct inode *inode, const struct qstr *name, 1599int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1598 const struct gfs2_inode *nip, unsigned type) 1600 const struct gfs2_inode *nip)
1599{ 1601{
1600 struct gfs2_inode *ip = GFS2_I(inode); 1602 struct gfs2_inode *ip = GFS2_I(inode);
1601 struct buffer_head *bh; 1603 struct buffer_head *bh;
@@ -1611,7 +1613,7 @@ int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1611 return PTR_ERR(dent); 1613 return PTR_ERR(dent);
1612 dent = gfs2_init_dirent(inode, dent, name, bh); 1614 dent = gfs2_init_dirent(inode, dent, name, bh);
1613 gfs2_inum_out(nip, dent); 1615 gfs2_inum_out(nip, dent);
1614 dent->de_type = cpu_to_be16(type); 1616 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
1615 if (ip->i_diskflags & GFS2_DIF_EXHASH) { 1617 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
1616 leaf = (struct gfs2_leaf *)bh->b_data; 1618 leaf = (struct gfs2_leaf *)bh->b_data;
1617 be16_add_cpu(&leaf->lf_entries, 1); 1619 be16_add_cpu(&leaf->lf_entries, 1);
@@ -1623,6 +1625,8 @@ int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1623 gfs2_trans_add_bh(ip->i_gl, bh, 1); 1625 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1624 ip->i_entries++; 1626 ip->i_entries++;
1625 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; 1627 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1628 if (S_ISDIR(nip->i_inode.i_mode))
1629 inc_nlink(&ip->i_inode);
1626 gfs2_dinode_out(ip, bh->b_data); 1630 gfs2_dinode_out(ip, bh->b_data);
1627 brelse(bh); 1631 brelse(bh);
1628 error = 0; 1632 error = 0;
@@ -1667,8 +1671,9 @@ int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1667 * Returns: 0 on success, error code on failure 1671 * Returns: 0 on success, error code on failure
1668 */ 1672 */
1669 1673
1670int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name) 1674int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
1671{ 1675{
1676 const struct qstr *name = &dentry->d_name;
1672 struct gfs2_dirent *dent, *prev = NULL; 1677 struct gfs2_dirent *dent, *prev = NULL;
1673 struct buffer_head *bh; 1678 struct buffer_head *bh;
1674 int error; 1679 int error;
@@ -1709,6 +1714,8 @@ int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1709 gfs2_trans_add_bh(dip->i_gl, bh, 1); 1714 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1710 dip->i_entries--; 1715 dip->i_entries--;
1711 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME; 1716 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
1717 if (S_ISDIR(dentry->d_inode->i_mode))
1718 drop_nlink(&dip->i_inode);
1712 gfs2_dinode_out(dip, bh->b_data); 1719 gfs2_dinode_out(dip, bh->b_data);
1713 brelse(bh); 1720 brelse(bh);
1714 mark_inode_dirty(&dip->i_inode); 1721 mark_inode_dirty(&dip->i_inode);
@@ -1763,94 +1770,20 @@ int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1763} 1770}
1764 1771
1765/** 1772/**
1766 * foreach_leaf - call a function for each leaf in a directory
1767 * @dip: the directory
1768 * @lc: the function to call for each each
1769 * @data: private data to pass to it
1770 *
1771 * Returns: errno
1772 */
1773
1774static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1775{
1776 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1777 struct buffer_head *bh;
1778 struct gfs2_leaf *leaf;
1779 u32 hsize, len;
1780 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1781 u32 index = 0;
1782 __be64 *lp;
1783 u64 leaf_no;
1784 int error = 0;
1785
1786 hsize = 1 << dip->i_depth;
1787 if (hsize * sizeof(u64) != dip->i_disksize) {
1788 gfs2_consist_inode(dip);
1789 return -EIO;
1790 }
1791
1792 lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
1793 if (!lp)
1794 return -ENOMEM;
1795
1796 while (index < hsize) {
1797 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1798 ht_offset = index - lp_offset;
1799
1800 if (ht_offset_cur != ht_offset) {
1801 error = gfs2_dir_read_data(dip, (char *)lp,
1802 ht_offset * sizeof(__be64),
1803 sdp->sd_hash_bsize, 1);
1804 if (error != sdp->sd_hash_bsize) {
1805 if (error >= 0)
1806 error = -EIO;
1807 goto out;
1808 }
1809 ht_offset_cur = ht_offset;
1810 }
1811
1812 leaf_no = be64_to_cpu(lp[lp_offset]);
1813 if (leaf_no) {
1814 error = get_leaf(dip, leaf_no, &bh);
1815 if (error)
1816 goto out;
1817 leaf = (struct gfs2_leaf *)bh->b_data;
1818 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1819 brelse(bh);
1820
1821 error = lc(dip, index, len, leaf_no, data);
1822 if (error)
1823 goto out;
1824
1825 index = (index & ~(len - 1)) + len;
1826 } else
1827 index++;
1828 }
1829
1830 if (index != hsize) {
1831 gfs2_consist_inode(dip);
1832 error = -EIO;
1833 }
1834
1835out:
1836 kfree(lp);
1837
1838 return error;
1839}
1840
1841/**
1842 * leaf_dealloc - Deallocate a directory leaf 1773 * leaf_dealloc - Deallocate a directory leaf
1843 * @dip: the directory 1774 * @dip: the directory
1844 * @index: the hash table offset in the directory 1775 * @index: the hash table offset in the directory
1845 * @len: the number of pointers to this leaf 1776 * @len: the number of pointers to this leaf
1846 * @leaf_no: the leaf number 1777 * @leaf_no: the leaf number
1847 * @data: not used 1778 * @leaf_bh: buffer_head for the starting leaf
1779 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
1848 * 1780 *
1849 * Returns: errno 1781 * Returns: errno
1850 */ 1782 */
1851 1783
1852static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len, 1784static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1853 u64 leaf_no, void *data) 1785 u64 leaf_no, struct buffer_head *leaf_bh,
1786 int last_dealloc)
1854{ 1787{
1855 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 1788 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1856 struct gfs2_leaf *tmp_leaf; 1789 struct gfs2_leaf *tmp_leaf;
@@ -1882,14 +1815,18 @@ static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1882 goto out_qs; 1815 goto out_qs;
1883 1816
1884 /* Count the number of leaves */ 1817 /* Count the number of leaves */
1818 bh = leaf_bh;
1885 1819
1886 for (blk = leaf_no; blk; blk = nblk) { 1820 for (blk = leaf_no; blk; blk = nblk) {
1887 error = get_leaf(dip, blk, &bh); 1821 if (blk != leaf_no) {
1888 if (error) 1822 error = get_leaf(dip, blk, &bh);
1889 goto out_rlist; 1823 if (error)
1824 goto out_rlist;
1825 }
1890 tmp_leaf = (struct gfs2_leaf *)bh->b_data; 1826 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1891 nblk = be64_to_cpu(tmp_leaf->lf_next); 1827 nblk = be64_to_cpu(tmp_leaf->lf_next);
1892 brelse(bh); 1828 if (blk != leaf_no)
1829 brelse(bh);
1893 1830
1894 gfs2_rlist_add(sdp, &rlist, blk); 1831 gfs2_rlist_add(sdp, &rlist, blk);
1895 l_blocks++; 1832 l_blocks++;
@@ -1913,13 +1850,18 @@ static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1913 if (error) 1850 if (error)
1914 goto out_rg_gunlock; 1851 goto out_rg_gunlock;
1915 1852
1853 bh = leaf_bh;
1854
1916 for (blk = leaf_no; blk; blk = nblk) { 1855 for (blk = leaf_no; blk; blk = nblk) {
1917 error = get_leaf(dip, blk, &bh); 1856 if (blk != leaf_no) {
1918 if (error) 1857 error = get_leaf(dip, blk, &bh);
1919 goto out_end_trans; 1858 if (error)
1859 goto out_end_trans;
1860 }
1920 tmp_leaf = (struct gfs2_leaf *)bh->b_data; 1861 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1921 nblk = be64_to_cpu(tmp_leaf->lf_next); 1862 nblk = be64_to_cpu(tmp_leaf->lf_next);
1922 brelse(bh); 1863 if (blk != leaf_no)
1864 brelse(bh);
1923 1865
1924 gfs2_free_meta(dip, blk, 1); 1866 gfs2_free_meta(dip, blk, 1);
1925 gfs2_add_inode_blocks(&dip->i_inode, -1); 1867 gfs2_add_inode_blocks(&dip->i_inode, -1);
@@ -1937,6 +1879,10 @@ static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1937 goto out_end_trans; 1879 goto out_end_trans;
1938 1880
1939 gfs2_trans_add_bh(dip->i_gl, dibh, 1); 1881 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1882 /* On the last dealloc, make this a regular file in case we crash.
1883 (We don't want to free these blocks a second time.) */
1884 if (last_dealloc)
1885 dip->i_inode.i_mode = S_IFREG;
1940 gfs2_dinode_out(dip, dibh->b_data); 1886 gfs2_dinode_out(dip, dibh->b_data);
1941 brelse(dibh); 1887 brelse(dibh);
1942 1888
@@ -1970,29 +1916,67 @@ int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1970{ 1916{
1971 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 1917 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1972 struct buffer_head *bh; 1918 struct buffer_head *bh;
1973 int error; 1919 struct gfs2_leaf *leaf;
1920 u32 hsize, len;
1921 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1922 u32 index = 0, next_index;
1923 __be64 *lp;
1924 u64 leaf_no;
1925 int error = 0, last;
1974 1926
1975 /* Dealloc on-disk leaves to FREEMETA state */ 1927 hsize = 1 << dip->i_depth;
1976 error = foreach_leaf(dip, leaf_dealloc, NULL); 1928 if (hsize * sizeof(u64) != i_size_read(&dip->i_inode)) {
1977 if (error) 1929 gfs2_consist_inode(dip);
1978 return error; 1930 return -EIO;
1931 }
1979 1932
1980 /* Make this a regular file in case we crash. 1933 lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
1981 (We don't want to free these blocks a second time.) */ 1934 if (!lp)
1935 return -ENOMEM;
1982 1936
1983 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 1937 while (index < hsize) {
1984 if (error) 1938 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1985 return error; 1939 ht_offset = index - lp_offset;
1986 1940
1987 error = gfs2_meta_inode_buffer(dip, &bh); 1941 if (ht_offset_cur != ht_offset) {
1988 if (!error) { 1942 error = gfs2_dir_read_data(dip, (char *)lp,
1989 gfs2_trans_add_bh(dip->i_gl, bh, 1); 1943 ht_offset * sizeof(__be64),
1990 ((struct gfs2_dinode *)bh->b_data)->di_mode = 1944 sdp->sd_hash_bsize, 1);
1991 cpu_to_be32(S_IFREG); 1945 if (error != sdp->sd_hash_bsize) {
1992 brelse(bh); 1946 if (error >= 0)
1947 error = -EIO;
1948 goto out;
1949 }
1950 ht_offset_cur = ht_offset;
1951 }
1952
1953 leaf_no = be64_to_cpu(lp[lp_offset]);
1954 if (leaf_no) {
1955 error = get_leaf(dip, leaf_no, &bh);
1956 if (error)
1957 goto out;
1958 leaf = (struct gfs2_leaf *)bh->b_data;
1959 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1960
1961 next_index = (index & ~(len - 1)) + len;
1962 last = ((next_index >= hsize) ? 1 : 0);
1963 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1964 last);
1965 brelse(bh);
1966 if (error)
1967 goto out;
1968 index = next_index;
1969 } else
1970 index++;
1993 } 1971 }
1994 1972
1995 gfs2_trans_end(sdp); 1973 if (index != hsize) {
1974 gfs2_consist_inode(dip);
1975 error = -EIO;
1976 }
1977
1978out:
1979 kfree(lp);
1996 1980
1997 return error; 1981 return error;
1998} 1982}
diff --git a/fs/gfs2/dir.h b/fs/gfs2/dir.h
index 4f919440c3be..e686af11becd 100644
--- a/fs/gfs2/dir.h
+++ b/fs/gfs2/dir.h
@@ -17,23 +17,24 @@ struct inode;
17struct gfs2_inode; 17struct gfs2_inode;
18struct gfs2_inum; 18struct gfs2_inum;
19 19
20struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *filename); 20extern struct inode *gfs2_dir_search(struct inode *dir,
21int gfs2_dir_check(struct inode *dir, const struct qstr *filename, 21 const struct qstr *filename);
22 const struct gfs2_inode *ip); 22extern int gfs2_dir_check(struct inode *dir, const struct qstr *filename,
23int gfs2_dir_add(struct inode *inode, const struct qstr *filename, 23 const struct gfs2_inode *ip);
24 const struct gfs2_inode *ip, unsigned int type); 24extern int gfs2_dir_add(struct inode *inode, const struct qstr *filename,
25int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *filename); 25 const struct gfs2_inode *ip);
26int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque, 26extern int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry);
27 filldir_t filldir); 27extern int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
28int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename, 28 filldir_t filldir);
29 const struct gfs2_inode *nip, unsigned int new_type); 29extern int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
30 const struct gfs2_inode *nip, unsigned int new_type);
30 31
31int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip); 32extern int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip);
32 33
33int gfs2_diradd_alloc_required(struct inode *dir, 34extern int gfs2_diradd_alloc_required(struct inode *dir,
34 const struct qstr *filename); 35 const struct qstr *filename);
35int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, 36extern int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
36 struct buffer_head **bhp); 37 struct buffer_head **bhp);
37 38
38static inline u32 gfs2_disk_hash(const char *data, int len) 39static inline u32 gfs2_disk_hash(const char *data, int len)
39{ 40{
@@ -61,4 +62,7 @@ static inline void gfs2_qstr2dirent(const struct qstr *name, u16 reclen, struct
61 memcpy(dent + 1, name->name, name->len); 62 memcpy(dent + 1, name->name, name->len);
62} 63}
63 64
65extern struct qstr gfs2_qdot;
66extern struct qstr gfs2_qdotdot;
67
64#endif /* __DIR_DOT_H__ */ 68#endif /* __DIR_DOT_H__ */
diff --git a/fs/gfs2/export.c b/fs/gfs2/export.c
index dfe237a3f8ad..fe9945f2ff72 100644
--- a/fs/gfs2/export.c
+++ b/fs/gfs2/export.c
@@ -36,9 +36,13 @@ static int gfs2_encode_fh(struct dentry *dentry, __u32 *p, int *len,
36 struct super_block *sb = inode->i_sb; 36 struct super_block *sb = inode->i_sb;
37 struct gfs2_inode *ip = GFS2_I(inode); 37 struct gfs2_inode *ip = GFS2_I(inode);
38 38
39 if (*len < GFS2_SMALL_FH_SIZE || 39 if (connectable && (*len < GFS2_LARGE_FH_SIZE)) {
40 (connectable && *len < GFS2_LARGE_FH_SIZE)) 40 *len = GFS2_LARGE_FH_SIZE;
41 return 255; 41 return 255;
42 } else if (*len < GFS2_SMALL_FH_SIZE) {
43 *len = GFS2_SMALL_FH_SIZE;
44 return 255;
45 }
42 46
43 fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32); 47 fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32);
44 fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF); 48 fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
@@ -126,31 +130,16 @@ static int gfs2_get_name(struct dentry *parent, char *name,
126 130
127static struct dentry *gfs2_get_parent(struct dentry *child) 131static struct dentry *gfs2_get_parent(struct dentry *child)
128{ 132{
129 struct qstr dotdot; 133 return d_obtain_alias(gfs2_lookupi(child->d_inode, &gfs2_qdotdot, 1));
130 struct dentry *dentry;
131
132 /*
133 * XXX(hch): it would be a good idea to keep this around as a
134 * static variable.
135 */
136 gfs2_str2qstr(&dotdot, "..");
137
138 dentry = d_obtain_alias(gfs2_lookupi(child->d_inode, &dotdot, 1));
139 if (!IS_ERR(dentry))
140 dentry->d_op = &gfs2_dops;
141 return dentry;
142} 134}
143 135
144static struct dentry *gfs2_get_dentry(struct super_block *sb, 136static struct dentry *gfs2_get_dentry(struct super_block *sb,
145 struct gfs2_inum_host *inum) 137 struct gfs2_inum_host *inum)
146{ 138{
147 struct gfs2_sbd *sdp = sb->s_fs_info; 139 struct gfs2_sbd *sdp = sb->s_fs_info;
148 struct gfs2_holder i_gh;
149 struct inode *inode; 140 struct inode *inode;
150 struct dentry *dentry;
151 int error;
152 141
153 inode = gfs2_ilookup(sb, inum->no_addr); 142 inode = gfs2_ilookup(sb, inum->no_addr, 0);
154 if (inode) { 143 if (inode) {
155 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) { 144 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
156 iput(inode); 145 iput(inode);
@@ -159,52 +148,13 @@ static struct dentry *gfs2_get_dentry(struct super_block *sb,
159 goto out_inode; 148 goto out_inode;
160 } 149 }
161 150
162 error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops, 151 inode = gfs2_lookup_by_inum(sdp, inum->no_addr, &inum->no_formal_ino,
163 LM_ST_SHARED, LM_FLAG_ANY, &i_gh); 152 GFS2_BLKST_DINODE);
164 if (error) 153 if (IS_ERR(inode))
165 return ERR_PTR(error); 154 return ERR_CAST(inode);
166
167 error = gfs2_check_blk_type(sdp, inum->no_addr, GFS2_BLKST_DINODE);
168 if (error)
169 goto fail;
170
171 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, inum->no_addr, 0);
172 if (IS_ERR(inode)) {
173 error = PTR_ERR(inode);
174 goto fail;
175 }
176
177 error = gfs2_inode_refresh(GFS2_I(inode));
178 if (error) {
179 iput(inode);
180 goto fail;
181 }
182
183 /* Pick up the works we bypass in gfs2_inode_lookup */
184 if (inode->i_state & I_NEW)
185 gfs2_set_iop(inode);
186
187 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
188 iput(inode);
189 goto fail;
190 }
191
192 error = -EIO;
193 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM) {
194 iput(inode);
195 goto fail;
196 }
197
198 gfs2_glock_dq_uninit(&i_gh);
199 155
200out_inode: 156out_inode:
201 dentry = d_obtain_alias(inode); 157 return d_obtain_alias(inode);
202 if (!IS_ERR(dentry))
203 dentry->d_op = &gfs2_dops;
204 return dentry;
205fail:
206 gfs2_glock_dq_uninit(&i_gh);
207 return ERR_PTR(error);
208} 158}
209 159
210static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid, 160static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
index 4edd662c8232..a9f5cbe45cd9 100644
--- a/fs/gfs2/file.c
+++ b/fs/gfs2/file.c
@@ -19,6 +19,8 @@
19#include <linux/fs.h> 19#include <linux/fs.h>
20#include <linux/gfs2_ondisk.h> 20#include <linux/gfs2_ondisk.h>
21#include <linux/ext2_fs.h> 21#include <linux/ext2_fs.h>
22#include <linux/falloc.h>
23#include <linux/swap.h>
22#include <linux/crc32.h> 24#include <linux/crc32.h>
23#include <linux/writeback.h> 25#include <linux/writeback.h>
24#include <asm/uaccess.h> 26#include <asm/uaccess.h>
@@ -219,7 +221,7 @@ static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
219 goto out_drop_write; 221 goto out_drop_write;
220 222
221 error = -EACCES; 223 error = -EACCES;
222 if (!is_owner_or_cap(inode)) 224 if (!inode_owner_or_capable(inode))
223 goto out; 225 goto out;
224 226
225 error = 0; 227 error = 0;
@@ -241,7 +243,7 @@ static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
241 !capable(CAP_LINUX_IMMUTABLE)) 243 !capable(CAP_LINUX_IMMUTABLE))
242 goto out; 244 goto out;
243 if (!IS_IMMUTABLE(inode)) { 245 if (!IS_IMMUTABLE(inode)) {
244 error = gfs2_permission(inode, MAY_WRITE); 246 error = gfs2_permission(inode, MAY_WRITE, 0);
245 if (error) 247 if (error)
246 goto out; 248 goto out;
247 } 249 }
@@ -382,8 +384,10 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
382 rblocks = RES_DINODE + ind_blocks; 384 rblocks = RES_DINODE + ind_blocks;
383 if (gfs2_is_jdata(ip)) 385 if (gfs2_is_jdata(ip))
384 rblocks += data_blocks ? data_blocks : 1; 386 rblocks += data_blocks ? data_blocks : 1;
385 if (ind_blocks || data_blocks) 387 if (ind_blocks || data_blocks) {
386 rblocks += RES_STATFS + RES_QUOTA; 388 rblocks += RES_STATFS + RES_QUOTA;
389 rblocks += gfs2_rg_blocks(al);
390 }
387 ret = gfs2_trans_begin(sdp, rblocks, 0); 391 ret = gfs2_trans_begin(sdp, rblocks, 0);
388 if (ret) 392 if (ret)
389 goto out_trans_fail; 393 goto out_trans_fail;
@@ -444,15 +448,20 @@ static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
444{ 448{
445 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); 449 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
446 450
447 if (!(file->f_flags & O_NOATIME)) { 451 if (!(file->f_flags & O_NOATIME) &&
452 !IS_NOATIME(&ip->i_inode)) {
448 struct gfs2_holder i_gh; 453 struct gfs2_holder i_gh;
449 int error; 454 int error;
450 455
451 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); 456 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
452 error = gfs2_glock_nq(&i_gh); 457 error = gfs2_glock_nq(&i_gh);
453 file_accessed(file); 458 if (error == 0) {
454 if (error == 0) 459 file_accessed(file);
455 gfs2_glock_dq_uninit(&i_gh); 460 gfs2_glock_dq(&i_gh);
461 }
462 gfs2_holder_uninit(&i_gh);
463 if (error)
464 return error;
456 } 465 }
457 vma->vm_ops = &gfs2_vm_ops; 466 vma->vm_ops = &gfs2_vm_ops;
458 vma->vm_flags |= VM_CAN_NONLINEAR; 467 vma->vm_flags |= VM_CAN_NONLINEAR;
@@ -491,7 +500,7 @@ static int gfs2_open(struct inode *inode, struct file *file)
491 goto fail; 500 goto fail;
492 501
493 if (!(file->f_flags & O_LARGEFILE) && 502 if (!(file->f_flags & O_LARGEFILE) &&
494 ip->i_disksize > MAX_NON_LFS) { 503 i_size_read(inode) > MAX_NON_LFS) {
495 error = -EOVERFLOW; 504 error = -EOVERFLOW;
496 goto fail_gunlock; 505 goto fail_gunlock;
497 } 506 }
@@ -536,18 +545,10 @@ static int gfs2_close(struct inode *inode, struct file *file)
536/** 545/**
537 * gfs2_fsync - sync the dirty data for a file (across the cluster) 546 * gfs2_fsync - sync the dirty data for a file (across the cluster)
538 * @file: the file that points to the dentry (we ignore this) 547 * @file: the file that points to the dentry (we ignore this)
539 * @dentry: the dentry that points to the inode to sync 548 * @datasync: set if we can ignore timestamp changes
540 * 549 *
541 * The VFS will flush "normal" data for us. We only need to worry 550 * The VFS will flush data for us. We only need to worry
542 * about metadata here. For journaled data, we just do a log flush 551 * about metadata here.
543 * as we can't avoid it. Otherwise we can just bale out if datasync
544 * is set. For stuffed inodes we must flush the log in order to
545 * ensure that all data is on disk.
546 *
547 * The call to write_inode_now() is there to write back metadata and
548 * the inode itself. It does also try and write the data, but thats
549 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
550 * for us.
551 * 552 *
552 * Returns: errno 553 * Returns: errno
553 */ 554 */
@@ -556,22 +557,20 @@ static int gfs2_fsync(struct file *file, int datasync)
556{ 557{
557 struct inode *inode = file->f_mapping->host; 558 struct inode *inode = file->f_mapping->host;
558 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC); 559 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
559 int ret = 0; 560 struct gfs2_inode *ip = GFS2_I(inode);
560 561 int ret;
561 if (gfs2_is_jdata(GFS2_I(inode))) {
562 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
563 return 0;
564 }
565 562
566 if (sync_state != 0) { 563 if (datasync)
567 if (!datasync) 564 sync_state &= ~I_DIRTY_SYNC;
568 ret = write_inode_now(inode, 0);
569 565
570 if (gfs2_is_stuffed(GFS2_I(inode))) 566 if (sync_state) {
571 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl); 567 ret = sync_inode_metadata(inode, 1);
568 if (ret)
569 return ret;
570 gfs2_ail_flush(ip->i_gl);
572 } 571 }
573 572
574 return ret; 573 return 0;
575} 574}
576 575
577/** 576/**
@@ -608,6 +607,310 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
608 return generic_file_aio_write(iocb, iov, nr_segs, pos); 607 return generic_file_aio_write(iocb, iov, nr_segs, pos);
609} 608}
610 609
610static int empty_write_end(struct page *page, unsigned from,
611 unsigned to, int mode)
612{
613 struct inode *inode = page->mapping->host;
614 struct gfs2_inode *ip = GFS2_I(inode);
615 struct buffer_head *bh;
616 unsigned offset, blksize = 1 << inode->i_blkbits;
617 pgoff_t end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
618
619 zero_user(page, from, to-from);
620 mark_page_accessed(page);
621
622 if (page->index < end_index || !(mode & FALLOC_FL_KEEP_SIZE)) {
623 if (!gfs2_is_writeback(ip))
624 gfs2_page_add_databufs(ip, page, from, to);
625
626 block_commit_write(page, from, to);
627 return 0;
628 }
629
630 offset = 0;
631 bh = page_buffers(page);
632 while (offset < to) {
633 if (offset >= from) {
634 set_buffer_uptodate(bh);
635 mark_buffer_dirty(bh);
636 clear_buffer_new(bh);
637 write_dirty_buffer(bh, WRITE);
638 }
639 offset += blksize;
640 bh = bh->b_this_page;
641 }
642
643 offset = 0;
644 bh = page_buffers(page);
645 while (offset < to) {
646 if (offset >= from) {
647 wait_on_buffer(bh);
648 if (!buffer_uptodate(bh))
649 return -EIO;
650 }
651 offset += blksize;
652 bh = bh->b_this_page;
653 }
654 return 0;
655}
656
657static int needs_empty_write(sector_t block, struct inode *inode)
658{
659 int error;
660 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
661
662 bh_map.b_size = 1 << inode->i_blkbits;
663 error = gfs2_block_map(inode, block, &bh_map, 0);
664 if (unlikely(error))
665 return error;
666 return !buffer_mapped(&bh_map);
667}
668
669static int write_empty_blocks(struct page *page, unsigned from, unsigned to,
670 int mode)
671{
672 struct inode *inode = page->mapping->host;
673 unsigned start, end, next, blksize;
674 sector_t block = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
675 int ret;
676
677 blksize = 1 << inode->i_blkbits;
678 next = end = 0;
679 while (next < from) {
680 next += blksize;
681 block++;
682 }
683 start = next;
684 do {
685 next += blksize;
686 ret = needs_empty_write(block, inode);
687 if (unlikely(ret < 0))
688 return ret;
689 if (ret == 0) {
690 if (end) {
691 ret = __block_write_begin(page, start, end - start,
692 gfs2_block_map);
693 if (unlikely(ret))
694 return ret;
695 ret = empty_write_end(page, start, end, mode);
696 if (unlikely(ret))
697 return ret;
698 end = 0;
699 }
700 start = next;
701 }
702 else
703 end = next;
704 block++;
705 } while (next < to);
706
707 if (end) {
708 ret = __block_write_begin(page, start, end - start, gfs2_block_map);
709 if (unlikely(ret))
710 return ret;
711 ret = empty_write_end(page, start, end, mode);
712 if (unlikely(ret))
713 return ret;
714 }
715
716 return 0;
717}
718
719static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
720 int mode)
721{
722 struct gfs2_inode *ip = GFS2_I(inode);
723 struct buffer_head *dibh;
724 int error;
725 u64 start = offset >> PAGE_CACHE_SHIFT;
726 unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
727 u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
728 pgoff_t curr;
729 struct page *page;
730 unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
731 unsigned int from, to;
732
733 if (!end_offset)
734 end_offset = PAGE_CACHE_SIZE;
735
736 error = gfs2_meta_inode_buffer(ip, &dibh);
737 if (unlikely(error))
738 goto out;
739
740 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
741
742 if (gfs2_is_stuffed(ip)) {
743 error = gfs2_unstuff_dinode(ip, NULL);
744 if (unlikely(error))
745 goto out;
746 }
747
748 curr = start;
749 offset = start << PAGE_CACHE_SHIFT;
750 from = start_offset;
751 to = PAGE_CACHE_SIZE;
752 while (curr <= end) {
753 page = grab_cache_page_write_begin(inode->i_mapping, curr,
754 AOP_FLAG_NOFS);
755 if (unlikely(!page)) {
756 error = -ENOMEM;
757 goto out;
758 }
759
760 if (curr == end)
761 to = end_offset;
762 error = write_empty_blocks(page, from, to, mode);
763 if (!error && offset + to > inode->i_size &&
764 !(mode & FALLOC_FL_KEEP_SIZE)) {
765 i_size_write(inode, offset + to);
766 }
767 unlock_page(page);
768 page_cache_release(page);
769 if (error)
770 goto out;
771 curr++;
772 offset += PAGE_CACHE_SIZE;
773 from = 0;
774 }
775
776 gfs2_dinode_out(ip, dibh->b_data);
777 mark_inode_dirty(inode);
778
779 brelse(dibh);
780
781out:
782 return error;
783}
784
785static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
786 unsigned int *data_blocks, unsigned int *ind_blocks)
787{
788 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
789 unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
790 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
791
792 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
793 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
794 max_data -= tmp;
795 }
796 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
797 so it might end up with fewer data blocks */
798 if (max_data <= *data_blocks)
799 return;
800 *data_blocks = max_data;
801 *ind_blocks = max_blocks - max_data;
802 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
803 if (*len > max) {
804 *len = max;
805 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
806 }
807}
808
809static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
810 loff_t len)
811{
812 struct inode *inode = file->f_path.dentry->d_inode;
813 struct gfs2_sbd *sdp = GFS2_SB(inode);
814 struct gfs2_inode *ip = GFS2_I(inode);
815 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
816 loff_t bytes, max_bytes;
817 struct gfs2_alloc *al;
818 int error;
819 loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
820 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
821 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
822
823 /* We only support the FALLOC_FL_KEEP_SIZE mode */
824 if (mode & ~FALLOC_FL_KEEP_SIZE)
825 return -EOPNOTSUPP;
826
827 offset &= bsize_mask;
828
829 len = next - offset;
830 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
831 if (!bytes)
832 bytes = UINT_MAX;
833 bytes &= bsize_mask;
834 if (bytes == 0)
835 bytes = sdp->sd_sb.sb_bsize;
836
837 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
838 error = gfs2_glock_nq(&ip->i_gh);
839 if (unlikely(error))
840 goto out_uninit;
841
842 if (!gfs2_write_alloc_required(ip, offset, len))
843 goto out_unlock;
844
845 while (len > 0) {
846 if (len < bytes)
847 bytes = len;
848 al = gfs2_alloc_get(ip);
849 if (!al) {
850 error = -ENOMEM;
851 goto out_unlock;
852 }
853
854 error = gfs2_quota_lock_check(ip);
855 if (error)
856 goto out_alloc_put;
857
858retry:
859 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
860
861 al->al_requested = data_blocks + ind_blocks;
862 error = gfs2_inplace_reserve(ip);
863 if (error) {
864 if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
865 bytes >>= 1;
866 bytes &= bsize_mask;
867 if (bytes == 0)
868 bytes = sdp->sd_sb.sb_bsize;
869 goto retry;
870 }
871 goto out_qunlock;
872 }
873 max_bytes = bytes;
874 calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
875 al->al_requested = data_blocks + ind_blocks;
876
877 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
878 RES_RG_HDR + gfs2_rg_blocks(al);
879 if (gfs2_is_jdata(ip))
880 rblocks += data_blocks ? data_blocks : 1;
881
882 error = gfs2_trans_begin(sdp, rblocks,
883 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
884 if (error)
885 goto out_trans_fail;
886
887 error = fallocate_chunk(inode, offset, max_bytes, mode);
888 gfs2_trans_end(sdp);
889
890 if (error)
891 goto out_trans_fail;
892
893 len -= max_bytes;
894 offset += max_bytes;
895 gfs2_inplace_release(ip);
896 gfs2_quota_unlock(ip);
897 gfs2_alloc_put(ip);
898 }
899 goto out_unlock;
900
901out_trans_fail:
902 gfs2_inplace_release(ip);
903out_qunlock:
904 gfs2_quota_unlock(ip);
905out_alloc_put:
906 gfs2_alloc_put(ip);
907out_unlock:
908 gfs2_glock_dq(&ip->i_gh);
909out_uninit:
910 gfs2_holder_uninit(&ip->i_gh);
911 return error;
912}
913
611#ifdef CONFIG_GFS2_FS_LOCKING_DLM 914#ifdef CONFIG_GFS2_FS_LOCKING_DLM
612 915
613/** 916/**
@@ -620,6 +923,8 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
620 * cluster; until we do, disable leases (by just returning -EINVAL), 923 * cluster; until we do, disable leases (by just returning -EINVAL),
621 * unless the administrator has requested purely local locking. 924 * unless the administrator has requested purely local locking.
622 * 925 *
926 * Locking: called under lock_flocks
927 *
623 * Returns: errno 928 * Returns: errno
624 */ 929 */
625 930
@@ -716,8 +1021,10 @@ static void do_unflock(struct file *file, struct file_lock *fl)
716 1021
717 mutex_lock(&fp->f_fl_mutex); 1022 mutex_lock(&fp->f_fl_mutex);
718 flock_lock_file_wait(file, fl); 1023 flock_lock_file_wait(file, fl);
719 if (fl_gh->gh_gl) 1024 if (fl_gh->gh_gl) {
720 gfs2_glock_dq_uninit(fl_gh); 1025 gfs2_glock_dq_wait(fl_gh);
1026 gfs2_holder_uninit(fl_gh);
1027 }
721 mutex_unlock(&fp->f_fl_mutex); 1028 mutex_unlock(&fp->f_fl_mutex);
722} 1029}
723 1030
@@ -761,6 +1068,7 @@ const struct file_operations gfs2_file_fops = {
761 .splice_read = generic_file_splice_read, 1068 .splice_read = generic_file_splice_read,
762 .splice_write = generic_file_splice_write, 1069 .splice_write = generic_file_splice_write,
763 .setlease = gfs2_setlease, 1070 .setlease = gfs2_setlease,
1071 .fallocate = gfs2_fallocate,
764}; 1072};
765 1073
766const struct file_operations gfs2_dir_fops = { 1074const struct file_operations gfs2_dir_fops = {
@@ -771,6 +1079,7 @@ const struct file_operations gfs2_dir_fops = {
771 .fsync = gfs2_fsync, 1079 .fsync = gfs2_fsync,
772 .lock = gfs2_lock, 1080 .lock = gfs2_lock,
773 .flock = gfs2_flock, 1081 .flock = gfs2_flock,
1082 .llseek = default_llseek,
774}; 1083};
775 1084
776#endif /* CONFIG_GFS2_FS_LOCKING_DLM */ 1085#endif /* CONFIG_GFS2_FS_LOCKING_DLM */
@@ -789,6 +1098,7 @@ const struct file_operations gfs2_file_fops_nolock = {
789 .splice_read = generic_file_splice_read, 1098 .splice_read = generic_file_splice_read,
790 .splice_write = generic_file_splice_write, 1099 .splice_write = generic_file_splice_write,
791 .setlease = generic_setlease, 1100 .setlease = generic_setlease,
1101 .fallocate = gfs2_fallocate,
792}; 1102};
793 1103
794const struct file_operations gfs2_dir_fops_nolock = { 1104const struct file_operations gfs2_dir_fops_nolock = {
@@ -797,5 +1107,6 @@ const struct file_operations gfs2_dir_fops_nolock = {
797 .open = gfs2_open, 1107 .open = gfs2_open,
798 .release = gfs2_close, 1108 .release = gfs2_close,
799 .fsync = gfs2_fsync, 1109 .fsync = gfs2_fsync,
1110 .llseek = default_llseek,
800}; 1111};
801 1112
diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c
index 9adf8f924e08..1c1336e7b3b2 100644
--- a/fs/gfs2/glock.c
+++ b/fs/gfs2/glock.c
@@ -26,6 +26,9 @@
26#include <linux/freezer.h> 26#include <linux/freezer.h>
27#include <linux/workqueue.h> 27#include <linux/workqueue.h>
28#include <linux/jiffies.h> 28#include <linux/jiffies.h>
29#include <linux/rcupdate.h>
30#include <linux/rculist_bl.h>
31#include <linux/bit_spinlock.h>
29 32
30#include "gfs2.h" 33#include "gfs2.h"
31#include "incore.h" 34#include "incore.h"
@@ -41,10 +44,6 @@
41#define CREATE_TRACE_POINTS 44#define CREATE_TRACE_POINTS
42#include "trace_gfs2.h" 45#include "trace_gfs2.h"
43 46
44struct gfs2_gl_hash_bucket {
45 struct hlist_head hb_list;
46};
47
48struct gfs2_glock_iter { 47struct gfs2_glock_iter {
49 int hash; /* hash bucket index */ 48 int hash; /* hash bucket index */
50 struct gfs2_sbd *sdp; /* incore superblock */ 49 struct gfs2_sbd *sdp; /* incore superblock */
@@ -54,7 +53,6 @@ struct gfs2_glock_iter {
54 53
55typedef void (*glock_examiner) (struct gfs2_glock * gl); 54typedef void (*glock_examiner) (struct gfs2_glock * gl);
56 55
57static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
58static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl); 56static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
59#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0) 57#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
60static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target); 58static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
@@ -70,57 +68,9 @@ static DEFINE_SPINLOCK(lru_lock);
70#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT) 68#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
71#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1) 69#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
72 70
73static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE]; 71static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
74static struct dentry *gfs2_root; 72static struct dentry *gfs2_root;
75 73
76/*
77 * Despite what you might think, the numbers below are not arbitrary :-)
78 * They are taken from the ipv4 routing hash code, which is well tested
79 * and thus should be nearly optimal. Later on we might tweek the numbers
80 * but for now this should be fine.
81 *
82 * The reason for putting the locks in a separate array from the list heads
83 * is that we can have fewer locks than list heads and save memory. We use
84 * the same hash function for both, but with a different hash mask.
85 */
86#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
87 defined(CONFIG_PROVE_LOCKING)
88
89#ifdef CONFIG_LOCKDEP
90# define GL_HASH_LOCK_SZ 256
91#else
92# if NR_CPUS >= 32
93# define GL_HASH_LOCK_SZ 4096
94# elif NR_CPUS >= 16
95# define GL_HASH_LOCK_SZ 2048
96# elif NR_CPUS >= 8
97# define GL_HASH_LOCK_SZ 1024
98# elif NR_CPUS >= 4
99# define GL_HASH_LOCK_SZ 512
100# else
101# define GL_HASH_LOCK_SZ 256
102# endif
103#endif
104
105/* We never want more locks than chains */
106#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
107# undef GL_HASH_LOCK_SZ
108# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
109#endif
110
111static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
112
113static inline rwlock_t *gl_lock_addr(unsigned int x)
114{
115 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
116}
117#else /* not SMP, so no spinlocks required */
118static inline rwlock_t *gl_lock_addr(unsigned int x)
119{
120 return NULL;
121}
122#endif
123
124/** 74/**
125 * gl_hash() - Turn glock number into hash bucket number 75 * gl_hash() - Turn glock number into hash bucket number
126 * @lock: The glock number 76 * @lock: The glock number
@@ -141,25 +91,33 @@ static unsigned int gl_hash(const struct gfs2_sbd *sdp,
141 return h; 91 return h;
142} 92}
143 93
144/** 94static inline void spin_lock_bucket(unsigned int hash)
145 * glock_free() - Perform a few checks and then release struct gfs2_glock 95{
146 * @gl: The glock to release 96 hlist_bl_lock(&gl_hash_table[hash]);
147 * 97}
148 * Also calls lock module to release its internal structure for this glock. 98
149 * 99static inline void spin_unlock_bucket(unsigned int hash)
150 */ 100{
101 hlist_bl_unlock(&gl_hash_table[hash]);
102}
103
104static void gfs2_glock_dealloc(struct rcu_head *rcu)
105{
106 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
107
108 if (gl->gl_ops->go_flags & GLOF_ASPACE)
109 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
110 else
111 kmem_cache_free(gfs2_glock_cachep, gl);
112}
151 113
152static void glock_free(struct gfs2_glock *gl) 114void gfs2_glock_free(struct gfs2_glock *gl)
153{ 115{
154 struct gfs2_sbd *sdp = gl->gl_sbd; 116 struct gfs2_sbd *sdp = gl->gl_sbd;
155 struct address_space *mapping = gfs2_glock2aspace(gl);
156 struct kmem_cache *cachep = gfs2_glock_cachep;
157 117
158 GLOCK_BUG_ON(gl, mapping && mapping->nrpages); 118 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
159 trace_gfs2_glock_put(gl); 119 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
160 if (mapping) 120 wake_up(&sdp->sd_glock_wait);
161 cachep = gfs2_glock_aspace_cachep;
162 sdp->sd_lockstruct.ls_ops->lm_put_lock(cachep, gl);
163} 121}
164 122
165/** 123/**
@@ -194,28 +152,47 @@ static int demote_ok(const struct gfs2_glock *gl)
194 return 1; 152 return 1;
195} 153}
196 154
197/**
198 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
199 * @gl: the glock
200 *
201 */
202 155
203static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl) 156void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
204{ 157{
205 int may_reclaim;
206 may_reclaim = (demote_ok(gl) &&
207 (atomic_read(&gl->gl_ref) == 1 ||
208 (gl->gl_name.ln_type == LM_TYPE_INODE &&
209 atomic_read(&gl->gl_ref) <= 2)));
210 spin_lock(&lru_lock); 158 spin_lock(&lru_lock);
211 if (list_empty(&gl->gl_lru) && may_reclaim) { 159
212 list_add_tail(&gl->gl_lru, &lru_list); 160 if (!list_empty(&gl->gl_lru))
161 list_del_init(&gl->gl_lru);
162 else
213 atomic_inc(&lru_count); 163 atomic_inc(&lru_count);
164
165 list_add_tail(&gl->gl_lru, &lru_list);
166 set_bit(GLF_LRU, &gl->gl_flags);
167 spin_unlock(&lru_lock);
168}
169
170static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
171{
172 spin_lock(&lru_lock);
173 if (!list_empty(&gl->gl_lru)) {
174 list_del_init(&gl->gl_lru);
175 atomic_dec(&lru_count);
176 clear_bit(GLF_LRU, &gl->gl_flags);
214 } 177 }
215 spin_unlock(&lru_lock); 178 spin_unlock(&lru_lock);
216} 179}
217 180
218/** 181/**
182 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
183 * @gl: the glock
184 *
185 * If the glock is demotable, then we add it (or move it) to the end
186 * of the glock LRU list.
187 */
188
189static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
190{
191 if (demote_ok(gl))
192 gfs2_glock_add_to_lru(gl);
193}
194
195/**
219 * gfs2_glock_put_nolock() - Decrement reference count on glock 196 * gfs2_glock_put_nolock() - Decrement reference count on glock
220 * @gl: The glock to put 197 * @gl: The glock to put
221 * 198 *
@@ -227,7 +204,6 @@ void gfs2_glock_put_nolock(struct gfs2_glock *gl)
227{ 204{
228 if (atomic_dec_and_test(&gl->gl_ref)) 205 if (atomic_dec_and_test(&gl->gl_ref))
229 GLOCK_BUG_ON(gl, 1); 206 GLOCK_BUG_ON(gl, 1);
230 gfs2_glock_schedule_for_reclaim(gl);
231} 207}
232 208
233/** 209/**
@@ -236,30 +212,21 @@ void gfs2_glock_put_nolock(struct gfs2_glock *gl)
236 * 212 *
237 */ 213 */
238 214
239int gfs2_glock_put(struct gfs2_glock *gl) 215void gfs2_glock_put(struct gfs2_glock *gl)
240{ 216{
241 int rv = 0; 217 struct gfs2_sbd *sdp = gl->gl_sbd;
218 struct address_space *mapping = gfs2_glock2aspace(gl);
242 219
243 write_lock(gl_lock_addr(gl->gl_hash)); 220 if (atomic_dec_and_test(&gl->gl_ref)) {
244 if (atomic_dec_and_lock(&gl->gl_ref, &lru_lock)) { 221 spin_lock_bucket(gl->gl_hash);
245 hlist_del(&gl->gl_list); 222 hlist_bl_del_rcu(&gl->gl_list);
246 if (!list_empty(&gl->gl_lru)) { 223 spin_unlock_bucket(gl->gl_hash);
247 list_del_init(&gl->gl_lru); 224 gfs2_glock_remove_from_lru(gl);
248 atomic_dec(&lru_count);
249 }
250 spin_unlock(&lru_lock);
251 write_unlock(gl_lock_addr(gl->gl_hash));
252 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders)); 225 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
253 glock_free(gl); 226 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
254 rv = 1; 227 trace_gfs2_glock_put(gl);
255 goto out; 228 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
256 } 229 }
257 spin_lock(&gl->gl_spin);
258 gfs2_glock_schedule_for_reclaim(gl);
259 spin_unlock(&gl->gl_spin);
260 write_unlock(gl_lock_addr(gl->gl_hash));
261out:
262 return rv;
263} 230}
264 231
265/** 232/**
@@ -275,17 +242,15 @@ static struct gfs2_glock *search_bucket(unsigned int hash,
275 const struct lm_lockname *name) 242 const struct lm_lockname *name)
276{ 243{
277 struct gfs2_glock *gl; 244 struct gfs2_glock *gl;
278 struct hlist_node *h; 245 struct hlist_bl_node *h;
279 246
280 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) { 247 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
281 if (!lm_name_equal(&gl->gl_name, name)) 248 if (!lm_name_equal(&gl->gl_name, name))
282 continue; 249 continue;
283 if (gl->gl_sbd != sdp) 250 if (gl->gl_sbd != sdp)
284 continue; 251 continue;
285 252 if (atomic_inc_not_zero(&gl->gl_ref))
286 atomic_inc(&gl->gl_ref); 253 return gl;
287
288 return gl;
289 } 254 }
290 255
291 return NULL; 256 return NULL;
@@ -441,6 +406,8 @@ static void state_change(struct gfs2_glock *gl, unsigned int new_state)
441 else 406 else
442 gfs2_glock_put_nolock(gl); 407 gfs2_glock_put_nolock(gl);
443 } 408 }
409 if (held1 && held2 && list_empty(&gl->gl_holders))
410 clear_bit(GLF_QUEUED, &gl->gl_flags);
444 411
445 gl->gl_state = new_state; 412 gl->gl_state = new_state;
446 gl->gl_tchange = jiffies; 413 gl->gl_tchange = jiffies;
@@ -539,21 +506,6 @@ out_locked:
539 spin_unlock(&gl->gl_spin); 506 spin_unlock(&gl->gl_spin);
540} 507}
541 508
542static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
543 unsigned int req_state,
544 unsigned int flags)
545{
546 int ret = LM_OUT_ERROR;
547
548 if (!sdp->sd_lockstruct.ls_ops->lm_lock)
549 return req_state == LM_ST_UNLOCKED ? 0 : req_state;
550
551 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
552 ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock,
553 req_state, flags);
554 return ret;
555}
556
557/** 509/**
558 * do_xmote - Calls the DLM to change the state of a lock 510 * do_xmote - Calls the DLM to change the state of a lock
559 * @gl: The lock state 511 * @gl: The lock state
@@ -573,13 +525,14 @@ __acquires(&gl->gl_spin)
573 525
574 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP | 526 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
575 LM_FLAG_PRIORITY); 527 LM_FLAG_PRIORITY);
576 BUG_ON(gl->gl_state == target); 528 GLOCK_BUG_ON(gl, gl->gl_state == target);
577 BUG_ON(gl->gl_state == gl->gl_target); 529 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
578 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) && 530 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
579 glops->go_inval) { 531 glops->go_inval) {
580 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); 532 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
581 do_error(gl, 0); /* Fail queued try locks */ 533 do_error(gl, 0); /* Fail queued try locks */
582 } 534 }
535 gl->gl_req = target;
583 spin_unlock(&gl->gl_spin); 536 spin_unlock(&gl->gl_spin);
584 if (glops->go_xmote_th) 537 if (glops->go_xmote_th)
585 glops->go_xmote_th(gl); 538 glops->go_xmote_th(gl);
@@ -588,19 +541,16 @@ __acquires(&gl->gl_spin)
588 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); 541 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
589 542
590 gfs2_glock_hold(gl); 543 gfs2_glock_hold(gl);
591 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED || 544 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
592 gl->gl_state == LM_ST_DEFERRED) && 545 /* lock_dlm */
593 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) 546 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
594 lck_flags |= LM_FLAG_TRY_1CB; 547 GLOCK_BUG_ON(gl, ret);
595 ret = gfs2_lm_lock(sdp, gl, target, lck_flags); 548 } else { /* lock_nolock */
596 549 finish_xmote(gl, target);
597 if (!(ret & LM_OUT_ASYNC)) {
598 finish_xmote(gl, ret);
599 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) 550 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
600 gfs2_glock_put(gl); 551 gfs2_glock_put(gl);
601 } else {
602 GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
603 } 552 }
553
604 spin_lock(&gl->gl_spin); 554 spin_lock(&gl->gl_spin);
605} 555}
606 556
@@ -684,21 +634,20 @@ static void delete_work_func(struct work_struct *work)
684{ 634{
685 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete); 635 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
686 struct gfs2_sbd *sdp = gl->gl_sbd; 636 struct gfs2_sbd *sdp = gl->gl_sbd;
687 struct gfs2_inode *ip = NULL; 637 struct gfs2_inode *ip;
688 struct inode *inode; 638 struct inode *inode;
689 u64 no_addr = 0; 639 u64 no_addr = gl->gl_name.ln_number;
640
641 ip = gl->gl_object;
642 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
690 643
691 spin_lock(&gl->gl_spin);
692 ip = (struct gfs2_inode *)gl->gl_object;
693 if (ip) 644 if (ip)
694 no_addr = ip->i_no_addr; 645 inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
695 spin_unlock(&gl->gl_spin); 646 else
696 if (ip) { 647 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
697 inode = gfs2_ilookup(sdp->sd_vfs, no_addr); 648 if (inode && !IS_ERR(inode)) {
698 if (inode) { 649 d_prune_aliases(inode);
699 d_prune_aliases(inode); 650 iput(inode);
700 iput(inode);
701 }
702 } 651 }
703 gfs2_glock_put(gl); 652 gfs2_glock_put(gl);
704} 653}
@@ -714,14 +663,19 @@ static void glock_work_func(struct work_struct *work)
714 drop_ref = 1; 663 drop_ref = 1;
715 } 664 }
716 spin_lock(&gl->gl_spin); 665 spin_lock(&gl->gl_spin);
717 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && 666 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
718 gl->gl_state != LM_ST_UNLOCKED && 667 gl->gl_state != LM_ST_UNLOCKED &&
719 gl->gl_demote_state != LM_ST_EXCLUSIVE) { 668 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
720 unsigned long holdtime, now = jiffies; 669 unsigned long holdtime, now = jiffies;
670
721 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time; 671 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
722 if (time_before(now, holdtime)) 672 if (time_before(now, holdtime))
723 delay = holdtime - now; 673 delay = holdtime - now;
724 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags); 674
675 if (!delay) {
676 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
677 set_bit(GLF_DEMOTE, &gl->gl_flags);
678 }
725 } 679 }
726 run_queue(gl, 0); 680 run_queue(gl, 0);
727 spin_unlock(&gl->gl_spin); 681 spin_unlock(&gl->gl_spin);
@@ -754,10 +708,11 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
754 struct gfs2_glock *gl, *tmp; 708 struct gfs2_glock *gl, *tmp;
755 unsigned int hash = gl_hash(sdp, &name); 709 unsigned int hash = gl_hash(sdp, &name);
756 struct address_space *mapping; 710 struct address_space *mapping;
711 struct kmem_cache *cachep;
757 712
758 read_lock(gl_lock_addr(hash)); 713 rcu_read_lock();
759 gl = search_bucket(hash, sdp, &name); 714 gl = search_bucket(hash, sdp, &name);
760 read_unlock(gl_lock_addr(hash)); 715 rcu_read_unlock();
761 716
762 *glp = gl; 717 *glp = gl;
763 if (gl) 718 if (gl)
@@ -766,9 +721,10 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
766 return -ENOENT; 721 return -ENOENT;
767 722
768 if (glops->go_flags & GLOF_ASPACE) 723 if (glops->go_flags & GLOF_ASPACE)
769 gl = kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_KERNEL); 724 cachep = gfs2_glock_aspace_cachep;
770 else 725 else
771 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL); 726 cachep = gfs2_glock_cachep;
727 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
772 if (!gl) 728 if (!gl)
773 return -ENOMEM; 729 return -ENOMEM;
774 730
@@ -801,15 +757,16 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
801 mapping->writeback_index = 0; 757 mapping->writeback_index = 0;
802 } 758 }
803 759
804 write_lock(gl_lock_addr(hash)); 760 spin_lock_bucket(hash);
805 tmp = search_bucket(hash, sdp, &name); 761 tmp = search_bucket(hash, sdp, &name);
806 if (tmp) { 762 if (tmp) {
807 write_unlock(gl_lock_addr(hash)); 763 spin_unlock_bucket(hash);
808 glock_free(gl); 764 kmem_cache_free(cachep, gl);
765 atomic_dec(&sdp->sd_glock_disposal);
809 gl = tmp; 766 gl = tmp;
810 } else { 767 } else {
811 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list); 768 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
812 write_unlock(gl_lock_addr(hash)); 769 spin_unlock_bucket(hash);
813 } 770 }
814 771
815 *glp = gl; 772 *glp = gl;
@@ -950,17 +907,22 @@ int gfs2_glock_wait(struct gfs2_holder *gh)
950 907
951void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...) 908void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
952{ 909{
910 struct va_format vaf;
953 va_list args; 911 va_list args;
954 912
955 va_start(args, fmt); 913 va_start(args, fmt);
914
956 if (seq) { 915 if (seq) {
957 struct gfs2_glock_iter *gi = seq->private; 916 struct gfs2_glock_iter *gi = seq->private;
958 vsprintf(gi->string, fmt, args); 917 vsprintf(gi->string, fmt, args);
959 seq_printf(seq, gi->string); 918 seq_printf(seq, gi->string);
960 } else { 919 } else {
961 printk(KERN_ERR " "); 920 vaf.fmt = fmt;
962 vprintk(fmt, args); 921 vaf.va = &args;
922
923 printk(KERN_ERR " %pV", &vaf);
963 } 924 }
925
964 va_end(args); 926 va_end(args);
965} 927}
966 928
@@ -1012,13 +974,14 @@ fail:
1012 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt)) 974 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1013 insert_pt = &gh2->gh_list; 975 insert_pt = &gh2->gh_list;
1014 } 976 }
977 set_bit(GLF_QUEUED, &gl->gl_flags);
978 trace_gfs2_glock_queue(gh, 1);
1015 if (likely(insert_pt == NULL)) { 979 if (likely(insert_pt == NULL)) {
1016 list_add_tail(&gh->gh_list, &gl->gl_holders); 980 list_add_tail(&gh->gh_list, &gl->gl_holders);
1017 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY)) 981 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1018 goto do_cancel; 982 goto do_cancel;
1019 return; 983 return;
1020 } 984 }
1021 trace_gfs2_glock_queue(gh, 1);
1022 list_add_tail(&gh->gh_list, insert_pt); 985 list_add_tail(&gh->gh_list, insert_pt);
1023do_cancel: 986do_cancel:
1024 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list); 987 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
@@ -1061,6 +1024,9 @@ int gfs2_glock_nq(struct gfs2_holder *gh)
1061 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) 1024 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1062 return -EIO; 1025 return -EIO;
1063 1026
1027 if (test_bit(GLF_LRU, &gl->gl_flags))
1028 gfs2_glock_remove_from_lru(gl);
1029
1064 spin_lock(&gl->gl_spin); 1030 spin_lock(&gl->gl_spin);
1065 add_to_queue(gh); 1031 add_to_queue(gh);
1066 if ((LM_FLAG_NOEXP & gh->gh_flags) && 1032 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
@@ -1118,6 +1084,8 @@ void gfs2_glock_dq(struct gfs2_holder *gh)
1118 !test_bit(GLF_DEMOTE, &gl->gl_flags)) 1084 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1119 fast_path = 1; 1085 fast_path = 1;
1120 } 1086 }
1087 if (!test_bit(GLF_LFLUSH, &gl->gl_flags))
1088 __gfs2_glock_schedule_for_reclaim(gl);
1121 trace_gfs2_glock_queue(gh, 0); 1089 trace_gfs2_glock_queue(gh, 0);
1122 spin_unlock(&gl->gl_spin); 1090 spin_unlock(&gl->gl_spin);
1123 if (likely(fast_path)) 1091 if (likely(fast_path))
@@ -1156,7 +1124,7 @@ void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1156 * @number: the lock number 1124 * @number: the lock number
1157 * @glops: the glock operations for the type of glock 1125 * @glops: the glock operations for the type of glock
1158 * @state: the state to acquire the glock in 1126 * @state: the state to acquire the glock in
1159 * @flags: modifier flags for the aquisition 1127 * @flags: modifier flags for the acquisition
1160 * @gh: the struct gfs2_holder 1128 * @gh: the struct gfs2_holder
1161 * 1129 *
1162 * Returns: errno 1130 * Returns: errno
@@ -1281,10 +1249,8 @@ int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1281 1249
1282void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) 1250void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1283{ 1251{
1284 unsigned int x; 1252 while (num_gh--)
1285 1253 gfs2_glock_dq(&ghs[num_gh]);
1286 for (x = 0; x < num_gh; x++)
1287 gfs2_glock_dq(&ghs[x]);
1288} 1254}
1289 1255
1290/** 1256/**
@@ -1296,10 +1262,8 @@ void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1296 1262
1297void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs) 1263void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1298{ 1264{
1299 unsigned int x; 1265 while (num_gh--)
1300 1266 gfs2_glock_dq_uninit(&ghs[num_gh]);
1301 for (x = 0; x < num_gh; x++)
1302 gfs2_glock_dq_uninit(&ghs[x]);
1303} 1267}
1304 1268
1305void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state) 1269void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
@@ -1310,10 +1274,12 @@ void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1310 1274
1311 gfs2_glock_hold(gl); 1275 gfs2_glock_hold(gl);
1312 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time; 1276 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
1313 if (time_before(now, holdtime)) 1277 if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
1314 delay = holdtime - now; 1278 if (time_before(now, holdtime))
1315 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags)) 1279 delay = holdtime - now;
1316 delay = gl->gl_ops->go_min_hold_time; 1280 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1281 delay = gl->gl_ops->go_min_hold_time;
1282 }
1317 1283
1318 spin_lock(&gl->gl_spin); 1284 spin_lock(&gl->gl_spin);
1319 handle_callback(gl, state, delay); 1285 handle_callback(gl, state, delay);
@@ -1357,35 +1323,42 @@ static int gfs2_should_freeze(const struct gfs2_glock *gl)
1357 * @gl: Pointer to the glock 1323 * @gl: Pointer to the glock
1358 * @ret: The return value from the dlm 1324 * @ret: The return value from the dlm
1359 * 1325 *
1326 * The gl_reply field is under the gl_spin lock so that it is ok
1327 * to use a bitfield shared with other glock state fields.
1360 */ 1328 */
1361 1329
1362void gfs2_glock_complete(struct gfs2_glock *gl, int ret) 1330void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1363{ 1331{
1364 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct; 1332 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
1365 1333
1334 spin_lock(&gl->gl_spin);
1366 gl->gl_reply = ret; 1335 gl->gl_reply = ret;
1367 1336
1368 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) { 1337 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
1369 spin_lock(&gl->gl_spin);
1370 if (gfs2_should_freeze(gl)) { 1338 if (gfs2_should_freeze(gl)) {
1371 set_bit(GLF_FROZEN, &gl->gl_flags); 1339 set_bit(GLF_FROZEN, &gl->gl_flags);
1372 spin_unlock(&gl->gl_spin); 1340 spin_unlock(&gl->gl_spin);
1373 return; 1341 return;
1374 } 1342 }
1375 spin_unlock(&gl->gl_spin);
1376 } 1343 }
1344
1345 spin_unlock(&gl->gl_spin);
1377 set_bit(GLF_REPLY_PENDING, &gl->gl_flags); 1346 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1347 smp_wmb();
1378 gfs2_glock_hold(gl); 1348 gfs2_glock_hold(gl);
1379 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) 1349 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1380 gfs2_glock_put(gl); 1350 gfs2_glock_put(gl);
1381} 1351}
1382 1352
1383 1353
1384static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask) 1354static int gfs2_shrink_glock_memory(struct shrinker *shrink,
1355 struct shrink_control *sc)
1385{ 1356{
1386 struct gfs2_glock *gl; 1357 struct gfs2_glock *gl;
1387 int may_demote; 1358 int may_demote;
1388 int nr_skipped = 0; 1359 int nr_skipped = 0;
1360 int nr = sc->nr_to_scan;
1361 gfp_t gfp_mask = sc->gfp_mask;
1389 LIST_HEAD(skipped); 1362 LIST_HEAD(skipped);
1390 1363
1391 if (nr == 0) 1364 if (nr == 0)
@@ -1398,6 +1371,7 @@ static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_m
1398 while(nr && !list_empty(&lru_list)) { 1371 while(nr && !list_empty(&lru_list)) {
1399 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru); 1372 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1400 list_del_init(&gl->gl_lru); 1373 list_del_init(&gl->gl_lru);
1374 clear_bit(GLF_LRU, &gl->gl_flags);
1401 atomic_dec(&lru_count); 1375 atomic_dec(&lru_count);
1402 1376
1403 /* Test for being demotable */ 1377 /* Test for being demotable */
@@ -1420,6 +1394,7 @@ static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_m
1420 } 1394 }
1421 nr_skipped++; 1395 nr_skipped++;
1422 list_add(&gl->gl_lru, &skipped); 1396 list_add(&gl->gl_lru, &skipped);
1397 set_bit(GLF_LRU, &gl->gl_flags);
1423 } 1398 }
1424 list_splice(&skipped, &lru_list); 1399 list_splice(&skipped, &lru_list);
1425 atomic_add(nr_skipped, &lru_count); 1400 atomic_add(nr_skipped, &lru_count);
@@ -1439,42 +1414,30 @@ static struct shrinker glock_shrinker = {
1439 * @sdp: the filesystem 1414 * @sdp: the filesystem
1440 * @bucket: the bucket 1415 * @bucket: the bucket
1441 * 1416 *
1442 * Returns: 1 if the bucket has entries
1443 */ 1417 */
1444 1418
1445static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp, 1419static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
1446 unsigned int hash) 1420 unsigned int hash)
1447{ 1421{
1448 struct gfs2_glock *gl, *prev = NULL; 1422 struct gfs2_glock *gl;
1449 int has_entries = 0; 1423 struct hlist_bl_head *head = &gl_hash_table[hash];
1450 struct hlist_head *head = &gl_hash_table[hash].hb_list; 1424 struct hlist_bl_node *pos;
1451 1425
1452 read_lock(gl_lock_addr(hash)); 1426 rcu_read_lock();
1453 /* Can't use hlist_for_each_entry - don't want prefetch here */ 1427 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
1454 if (hlist_empty(head)) 1428 if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
1455 goto out;
1456 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1457 while(1) {
1458 if (!sdp || gl->gl_sbd == sdp) {
1459 gfs2_glock_hold(gl);
1460 read_unlock(gl_lock_addr(hash));
1461 if (prev)
1462 gfs2_glock_put(prev);
1463 prev = gl;
1464 examiner(gl); 1429 examiner(gl);
1465 has_entries = 1;
1466 read_lock(gl_lock_addr(hash));
1467 }
1468 if (gl->gl_list.next == NULL)
1469 break;
1470 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1471 } 1430 }
1472out: 1431 rcu_read_unlock();
1473 read_unlock(gl_lock_addr(hash));
1474 if (prev)
1475 gfs2_glock_put(prev);
1476 cond_resched(); 1432 cond_resched();
1477 return has_entries; 1433}
1434
1435static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1436{
1437 unsigned x;
1438
1439 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1440 examine_bucket(examiner, sdp, x);
1478} 1441}
1479 1442
1480 1443
@@ -1504,15 +1467,10 @@ static void thaw_glock(struct gfs2_glock *gl)
1504 1467
1505static void clear_glock(struct gfs2_glock *gl) 1468static void clear_glock(struct gfs2_glock *gl)
1506{ 1469{
1507 spin_lock(&lru_lock); 1470 gfs2_glock_remove_from_lru(gl);
1508 if (!list_empty(&gl->gl_lru)) {
1509 list_del_init(&gl->gl_lru);
1510 atomic_dec(&lru_count);
1511 }
1512 spin_unlock(&lru_lock);
1513 1471
1514 spin_lock(&gl->gl_spin); 1472 spin_lock(&gl->gl_spin);
1515 if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED) 1473 if (gl->gl_state != LM_ST_UNLOCKED)
1516 handle_callback(gl, LM_ST_UNLOCKED, 0); 1474 handle_callback(gl, LM_ST_UNLOCKED, 0);
1517 spin_unlock(&gl->gl_spin); 1475 spin_unlock(&gl->gl_spin);
1518 gfs2_glock_hold(gl); 1476 gfs2_glock_hold(gl);
@@ -1528,10 +1486,21 @@ static void clear_glock(struct gfs2_glock *gl)
1528 1486
1529void gfs2_glock_thaw(struct gfs2_sbd *sdp) 1487void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1530{ 1488{
1531 unsigned x; 1489 glock_hash_walk(thaw_glock, sdp);
1490}
1532 1491
1533 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) 1492static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1534 examine_bucket(thaw_glock, sdp, x); 1493{
1494 int ret;
1495 spin_lock(&gl->gl_spin);
1496 ret = __dump_glock(seq, gl);
1497 spin_unlock(&gl->gl_spin);
1498 return ret;
1499}
1500
1501static void dump_glock_func(struct gfs2_glock *gl)
1502{
1503 dump_glock(NULL, gl);
1535} 1504}
1536 1505
1537/** 1506/**
@@ -1544,13 +1513,10 @@ void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1544 1513
1545void gfs2_gl_hash_clear(struct gfs2_sbd *sdp) 1514void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
1546{ 1515{
1547 unsigned int x; 1516 glock_hash_walk(clear_glock, sdp);
1548
1549 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1550 examine_bucket(clear_glock, sdp, x);
1551 flush_workqueue(glock_workqueue); 1517 flush_workqueue(glock_workqueue);
1552 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0); 1518 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
1553 gfs2_dump_lockstate(sdp); 1519 glock_hash_walk(dump_glock_func, sdp);
1554} 1520}
1555 1521
1556void gfs2_glock_finish_truncate(struct gfs2_inode *ip) 1522void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
@@ -1622,24 +1588,25 @@ static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1622static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh) 1588static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
1623{ 1589{
1624 struct task_struct *gh_owner = NULL; 1590 struct task_struct *gh_owner = NULL;
1625 char buffer[KSYM_SYMBOL_LEN];
1626 char flags_buf[32]; 1591 char flags_buf[32];
1627 1592
1628 sprint_symbol(buffer, gh->gh_ip);
1629 if (gh->gh_owner_pid) 1593 if (gh->gh_owner_pid)
1630 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID); 1594 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
1631 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n", 1595 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1632 state2str(gh->gh_state), 1596 state2str(gh->gh_state),
1633 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags), 1597 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1634 gh->gh_error, 1598 gh->gh_error,
1635 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1, 1599 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1636 gh_owner ? gh_owner->comm : "(ended)", buffer); 1600 gh_owner ? gh_owner->comm : "(ended)",
1601 (void *)gh->gh_ip);
1637 return 0; 1602 return 0;
1638} 1603}
1639 1604
1640static const char *gflags2str(char *buf, const unsigned long *gflags) 1605static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
1641{ 1606{
1607 const unsigned long *gflags = &gl->gl_flags;
1642 char *p = buf; 1608 char *p = buf;
1609
1643 if (test_bit(GLF_LOCK, gflags)) 1610 if (test_bit(GLF_LOCK, gflags))
1644 *p++ = 'l'; 1611 *p++ = 'l';
1645 if (test_bit(GLF_DEMOTE, gflags)) 1612 if (test_bit(GLF_DEMOTE, gflags))
@@ -1660,6 +1627,12 @@ static const char *gflags2str(char *buf, const unsigned long *gflags)
1660 *p++ = 'I'; 1627 *p++ = 'I';
1661 if (test_bit(GLF_FROZEN, gflags)) 1628 if (test_bit(GLF_FROZEN, gflags))
1662 *p++ = 'F'; 1629 *p++ = 'F';
1630 if (test_bit(GLF_QUEUED, gflags))
1631 *p++ = 'q';
1632 if (test_bit(GLF_LRU, gflags))
1633 *p++ = 'L';
1634 if (gl->gl_object)
1635 *p++ = 'o';
1663 *p = 0; 1636 *p = 0;
1664 return buf; 1637 return buf;
1665} 1638}
@@ -1694,14 +1667,15 @@ static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
1694 dtime *= 1000000/HZ; /* demote time in uSec */ 1667 dtime *= 1000000/HZ; /* demote time in uSec */
1695 if (!test_bit(GLF_DEMOTE, &gl->gl_flags)) 1668 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1696 dtime = 0; 1669 dtime = 0;
1697 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d r:%d\n", 1670 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d\n",
1698 state2str(gl->gl_state), 1671 state2str(gl->gl_state),
1699 gl->gl_name.ln_type, 1672 gl->gl_name.ln_type,
1700 (unsigned long long)gl->gl_name.ln_number, 1673 (unsigned long long)gl->gl_name.ln_number,
1701 gflags2str(gflags_buf, &gl->gl_flags), 1674 gflags2str(gflags_buf, gl),
1702 state2str(gl->gl_target), 1675 state2str(gl->gl_target),
1703 state2str(gl->gl_demote_state), dtime, 1676 state2str(gl->gl_demote_state), dtime,
1704 atomic_read(&gl->gl_ail_count), 1677 atomic_read(&gl->gl_ail_count),
1678 atomic_read(&gl->gl_revokes),
1705 atomic_read(&gl->gl_ref)); 1679 atomic_read(&gl->gl_ref));
1706 1680
1707 list_for_each_entry(gh, &gl->gl_holders, gh_list) { 1681 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
@@ -1715,71 +1689,23 @@ out:
1715 return error; 1689 return error;
1716} 1690}
1717 1691
1718static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1719{
1720 int ret;
1721 spin_lock(&gl->gl_spin);
1722 ret = __dump_glock(seq, gl);
1723 spin_unlock(&gl->gl_spin);
1724 return ret;
1725}
1726 1692
1727/**
1728 * gfs2_dump_lockstate - print out the current lockstate
1729 * @sdp: the filesystem
1730 * @ub: the buffer to copy the information into
1731 *
1732 * If @ub is NULL, dump the lockstate to the console.
1733 *
1734 */
1735
1736static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
1737{
1738 struct gfs2_glock *gl;
1739 struct hlist_node *h;
1740 unsigned int x;
1741 int error = 0;
1742
1743 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1744
1745 read_lock(gl_lock_addr(x));
1746
1747 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
1748 if (gl->gl_sbd != sdp)
1749 continue;
1750
1751 error = dump_glock(NULL, gl);
1752 if (error)
1753 break;
1754 }
1755
1756 read_unlock(gl_lock_addr(x));
1757
1758 if (error)
1759 break;
1760 }
1761
1762
1763 return error;
1764}
1765 1693
1766 1694
1767int __init gfs2_glock_init(void) 1695int __init gfs2_glock_init(void)
1768{ 1696{
1769 unsigned i; 1697 unsigned i;
1770 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) { 1698 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
1771 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list); 1699 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
1772 } 1700 }
1773#ifdef GL_HASH_LOCK_SZ
1774 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
1775 rwlock_init(&gl_hash_locks[i]);
1776 }
1777#endif
1778 1701
1779 glock_workqueue = create_workqueue("glock_workqueue"); 1702 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
1703 WQ_HIGHPRI | WQ_FREEZABLE, 0);
1780 if (IS_ERR(glock_workqueue)) 1704 if (IS_ERR(glock_workqueue))
1781 return PTR_ERR(glock_workqueue); 1705 return PTR_ERR(glock_workqueue);
1782 gfs2_delete_workqueue = create_workqueue("delete_workqueue"); 1706 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
1707 WQ_MEM_RECLAIM | WQ_FREEZABLE,
1708 0);
1783 if (IS_ERR(gfs2_delete_workqueue)) { 1709 if (IS_ERR(gfs2_delete_workqueue)) {
1784 destroy_workqueue(glock_workqueue); 1710 destroy_workqueue(glock_workqueue);
1785 return PTR_ERR(gfs2_delete_workqueue); 1711 return PTR_ERR(gfs2_delete_workqueue);
@@ -1797,62 +1723,54 @@ void gfs2_glock_exit(void)
1797 destroy_workqueue(gfs2_delete_workqueue); 1723 destroy_workqueue(gfs2_delete_workqueue);
1798} 1724}
1799 1725
1726static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1727{
1728 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1729 struct gfs2_glock, gl_list);
1730}
1731
1732static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1733{
1734 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
1735 struct gfs2_glock, gl_list);
1736}
1737
1800static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi) 1738static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
1801{ 1739{
1802 struct gfs2_glock *gl; 1740 struct gfs2_glock *gl;
1803 1741
1804restart: 1742 do {
1805 read_lock(gl_lock_addr(gi->hash)); 1743 gl = gi->gl;
1806 gl = gi->gl; 1744 if (gl) {
1807 if (gl) { 1745 gi->gl = glock_hash_next(gl);
1808 gi->gl = hlist_entry(gl->gl_list.next, 1746 } else {
1809 struct gfs2_glock, gl_list); 1747 gi->gl = glock_hash_chain(gi->hash);
1810 } else { 1748 }
1811 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first, 1749 while (gi->gl == NULL) {
1812 struct gfs2_glock, gl_list); 1750 gi->hash++;
1813 } 1751 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1814 if (gi->gl) 1752 rcu_read_unlock();
1815 gfs2_glock_hold(gi->gl); 1753 return 1;
1816 read_unlock(gl_lock_addr(gi->hash)); 1754 }
1817 if (gl) 1755 gi->gl = glock_hash_chain(gi->hash);
1818 gfs2_glock_put(gl); 1756 }
1819 while (gi->gl == NULL) { 1757 /* Skip entries for other sb and dead entries */
1820 gi->hash++; 1758 } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
1821 if (gi->hash >= GFS2_GL_HASH_SIZE)
1822 return 1;
1823 read_lock(gl_lock_addr(gi->hash));
1824 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1825 struct gfs2_glock, gl_list);
1826 if (gi->gl)
1827 gfs2_glock_hold(gi->gl);
1828 read_unlock(gl_lock_addr(gi->hash));
1829 }
1830
1831 if (gi->sdp != gi->gl->gl_sbd)
1832 goto restart;
1833 1759
1834 return 0; 1760 return 0;
1835} 1761}
1836 1762
1837static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
1838{
1839 if (gi->gl)
1840 gfs2_glock_put(gi->gl);
1841 gi->gl = NULL;
1842}
1843
1844static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos) 1763static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
1845{ 1764{
1846 struct gfs2_glock_iter *gi = seq->private; 1765 struct gfs2_glock_iter *gi = seq->private;
1847 loff_t n = *pos; 1766 loff_t n = *pos;
1848 1767
1849 gi->hash = 0; 1768 gi->hash = 0;
1769 rcu_read_lock();
1850 1770
1851 do { 1771 do {
1852 if (gfs2_glock_iter_next(gi)) { 1772 if (gfs2_glock_iter_next(gi))
1853 gfs2_glock_iter_free(gi);
1854 return NULL; 1773 return NULL;
1855 }
1856 } while (n--); 1774 } while (n--);
1857 1775
1858 return gi->gl; 1776 return gi->gl;
@@ -1865,10 +1783,8 @@ static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
1865 1783
1866 (*pos)++; 1784 (*pos)++;
1867 1785
1868 if (gfs2_glock_iter_next(gi)) { 1786 if (gfs2_glock_iter_next(gi))
1869 gfs2_glock_iter_free(gi);
1870 return NULL; 1787 return NULL;
1871 }
1872 1788
1873 return gi->gl; 1789 return gi->gl;
1874} 1790}
@@ -1876,7 +1792,10 @@ static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
1876static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr) 1792static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
1877{ 1793{
1878 struct gfs2_glock_iter *gi = seq->private; 1794 struct gfs2_glock_iter *gi = seq->private;
1879 gfs2_glock_iter_free(gi); 1795
1796 if (gi->gl)
1797 rcu_read_unlock();
1798 gi->gl = NULL;
1880} 1799}
1881 1800
1882static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr) 1801static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
diff --git a/fs/gfs2/glock.h b/fs/gfs2/glock.h
index 2bda1911b156..6b2f757b9281 100644
--- a/fs/gfs2/glock.h
+++ b/fs/gfs2/glock.h
@@ -87,11 +87,10 @@ enum {
87#define GL_ASYNC 0x00000040 87#define GL_ASYNC 0x00000040
88#define GL_EXACT 0x00000080 88#define GL_EXACT 0x00000080
89#define GL_SKIP 0x00000100 89#define GL_SKIP 0x00000100
90#define GL_ATIME 0x00000200
91#define GL_NOCACHE 0x00000400 90#define GL_NOCACHE 0x00000400
92 91
93/* 92/*
94 * lm_lock() and lm_async_cb return flags 93 * lm_async_cb return flags
95 * 94 *
96 * LM_OUT_ST_MASK 95 * LM_OUT_ST_MASK
97 * Masks the lower two bits of lock state in the returned value. 96 * Masks the lower two bits of lock state in the returned value.
@@ -99,15 +98,11 @@ enum {
99 * LM_OUT_CANCELED 98 * LM_OUT_CANCELED
100 * The lock request was canceled. 99 * The lock request was canceled.
101 * 100 *
102 * LM_OUT_ASYNC
103 * The result of the request will be returned in an LM_CB_ASYNC callback.
104 *
105 */ 101 */
106 102
107#define LM_OUT_ST_MASK 0x00000003 103#define LM_OUT_ST_MASK 0x00000003
108#define LM_OUT_CANCELED 0x00000008 104#define LM_OUT_CANCELED 0x00000008
109#define LM_OUT_ASYNC 0x00000080 105#define LM_OUT_ERROR 0x00000004
110#define LM_OUT_ERROR 0x00000100
111 106
112/* 107/*
113 * lm_recovery_done() messages 108 * lm_recovery_done() messages
@@ -123,26 +118,13 @@ struct lm_lockops {
123 int (*lm_mount) (struct gfs2_sbd *sdp, const char *fsname); 118 int (*lm_mount) (struct gfs2_sbd *sdp, const char *fsname);
124 void (*lm_unmount) (struct gfs2_sbd *sdp); 119 void (*lm_unmount) (struct gfs2_sbd *sdp);
125 void (*lm_withdraw) (struct gfs2_sbd *sdp); 120 void (*lm_withdraw) (struct gfs2_sbd *sdp);
126 void (*lm_put_lock) (struct kmem_cache *cachep, struct gfs2_glock *gl); 121 void (*lm_put_lock) (struct gfs2_glock *gl);
127 unsigned int (*lm_lock) (struct gfs2_glock *gl, 122 int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
128 unsigned int req_state, unsigned int flags); 123 unsigned int flags);
129 void (*lm_cancel) (struct gfs2_glock *gl); 124 void (*lm_cancel) (struct gfs2_glock *gl);
130 const match_table_t *lm_tokens; 125 const match_table_t *lm_tokens;
131}; 126};
132 127
133#define LM_FLAG_TRY 0x00000001
134#define LM_FLAG_TRY_1CB 0x00000002
135#define LM_FLAG_NOEXP 0x00000004
136#define LM_FLAG_ANY 0x00000008
137#define LM_FLAG_PRIORITY 0x00000010
138
139#define GL_ASYNC 0x00000040
140#define GL_EXACT 0x00000080
141#define GL_SKIP 0x00000100
142#define GL_NOCACHE 0x00000400
143
144#define GLR_TRYFAILED 13
145
146extern struct workqueue_struct *gfs2_delete_workqueue; 128extern struct workqueue_struct *gfs2_delete_workqueue;
147static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl) 129static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
148{ 130{
@@ -192,7 +174,7 @@ int gfs2_glock_get(struct gfs2_sbd *sdp,
192 int create, struct gfs2_glock **glp); 174 int create, struct gfs2_glock **glp);
193void gfs2_glock_hold(struct gfs2_glock *gl); 175void gfs2_glock_hold(struct gfs2_glock *gl);
194void gfs2_glock_put_nolock(struct gfs2_glock *gl); 176void gfs2_glock_put_nolock(struct gfs2_glock *gl);
195int gfs2_glock_put(struct gfs2_glock *gl); 177void gfs2_glock_put(struct gfs2_glock *gl);
196void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags, 178void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
197 struct gfs2_holder *gh); 179 struct gfs2_holder *gh);
198void gfs2_holder_reinit(unsigned int state, unsigned flags, 180void gfs2_holder_reinit(unsigned int state, unsigned flags,
@@ -212,10 +194,12 @@ int gfs2_glock_nq_num(struct gfs2_sbd *sdp,
212int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs); 194int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
213void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs); 195void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
214void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs); 196void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs);
197
198__attribute__ ((format(printf, 2, 3)))
215void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...); 199void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
216 200
217/** 201/**
218 * gfs2_glock_nq_init - intialize a holder and enqueue it on a glock 202 * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
219 * @gl: the glock 203 * @gl: the glock
220 * @state: the state we're requesting 204 * @state: the state we're requesting
221 * @flags: the modifier flags 205 * @flags: the modifier flags
@@ -239,25 +223,21 @@ static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
239 return error; 223 return error;
240} 224}
241 225
242/* Lock Value Block functions */ 226extern void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
243 227extern void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
244int gfs2_lvb_hold(struct gfs2_glock *gl); 228extern void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
245void gfs2_lvb_unhold(struct gfs2_glock *gl); 229extern void gfs2_glock_finish_truncate(struct gfs2_inode *ip);
246 230extern void gfs2_glock_thaw(struct gfs2_sbd *sdp);
247void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state); 231extern void gfs2_glock_add_to_lru(struct gfs2_glock *gl);
248void gfs2_glock_complete(struct gfs2_glock *gl, int ret); 232extern void gfs2_glock_free(struct gfs2_glock *gl);
249void gfs2_reclaim_glock(struct gfs2_sbd *sdp); 233
250void gfs2_gl_hash_clear(struct gfs2_sbd *sdp); 234extern int __init gfs2_glock_init(void);
251void gfs2_glock_finish_truncate(struct gfs2_inode *ip); 235extern void gfs2_glock_exit(void);
252void gfs2_glock_thaw(struct gfs2_sbd *sdp); 236
253 237extern int gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
254int __init gfs2_glock_init(void); 238extern void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
255void gfs2_glock_exit(void); 239extern int gfs2_register_debugfs(void);
256 240extern void gfs2_unregister_debugfs(void);
257int gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
258void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
259int gfs2_register_debugfs(void);
260void gfs2_unregister_debugfs(void);
261 241
262extern const struct lm_lockops gfs2_dlm_ops; 242extern const struct lm_lockops gfs2_dlm_ops;
263 243
diff --git a/fs/gfs2/glops.c b/fs/gfs2/glops.c
index 49f97d3bb690..2cca29316bd6 100644
--- a/fs/gfs2/glops.c
+++ b/fs/gfs2/glops.c
@@ -28,18 +28,45 @@
28#include "trans.h" 28#include "trans.h"
29 29
30/** 30/**
31 * ail_empty_gl - remove all buffers for a given lock from the AIL 31 * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
32 * @gl: the glock 32 * @gl: the glock
33 * 33 *
34 * None of the buffers should be dirty, locked, or pinned. 34 * None of the buffers should be dirty, locked, or pinned.
35 */ 35 */
36 36
37static void gfs2_ail_empty_gl(struct gfs2_glock *gl) 37static void __gfs2_ail_flush(struct gfs2_glock *gl)
38{ 38{
39 struct gfs2_sbd *sdp = gl->gl_sbd; 39 struct gfs2_sbd *sdp = gl->gl_sbd;
40 struct list_head *head = &gl->gl_ail_list; 40 struct list_head *head = &gl->gl_ail_list;
41 struct gfs2_bufdata *bd; 41 struct gfs2_bufdata *bd;
42 struct buffer_head *bh; 42 struct buffer_head *bh;
43
44 spin_lock(&sdp->sd_ail_lock);
45 while (!list_empty(head)) {
46 bd = list_entry(head->next, struct gfs2_bufdata,
47 bd_ail_gl_list);
48 bh = bd->bd_bh;
49 gfs2_remove_from_ail(bd);
50 bd->bd_bh = NULL;
51 bh->b_private = NULL;
52 spin_unlock(&sdp->sd_ail_lock);
53
54 bd->bd_blkno = bh->b_blocknr;
55 gfs2_log_lock(sdp);
56 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
57 gfs2_trans_add_revoke(sdp, bd);
58 gfs2_log_unlock(sdp);
59
60 spin_lock(&sdp->sd_ail_lock);
61 }
62 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
63 spin_unlock(&sdp->sd_ail_lock);
64}
65
66
67static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
68{
69 struct gfs2_sbd *sdp = gl->gl_sbd;
43 struct gfs2_trans tr; 70 struct gfs2_trans tr;
44 71
45 memset(&tr, 0, sizeof(tr)); 72 memset(&tr, 0, sizeof(tr));
@@ -56,25 +83,29 @@ static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
56 BUG_ON(current->journal_info); 83 BUG_ON(current->journal_info);
57 current->journal_info = &tr; 84 current->journal_info = &tr;
58 85
59 gfs2_log_lock(sdp); 86 __gfs2_ail_flush(gl);
60 while (!list_empty(head)) {
61 bd = list_entry(head->next, struct gfs2_bufdata,
62 bd_ail_gl_list);
63 bh = bd->bd_bh;
64 gfs2_remove_from_ail(bd);
65 bd->bd_bh = NULL;
66 bh->b_private = NULL;
67 bd->bd_blkno = bh->b_blocknr;
68 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
69 gfs2_trans_add_revoke(sdp, bd);
70 }
71 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
72 gfs2_log_unlock(sdp);
73 87
74 gfs2_trans_end(sdp); 88 gfs2_trans_end(sdp);
75 gfs2_log_flush(sdp, NULL); 89 gfs2_log_flush(sdp, NULL);
76} 90}
77 91
92void gfs2_ail_flush(struct gfs2_glock *gl)
93{
94 struct gfs2_sbd *sdp = gl->gl_sbd;
95 unsigned int revokes = atomic_read(&gl->gl_ail_count);
96 int ret;
97
98 if (!revokes)
99 return;
100
101 ret = gfs2_trans_begin(sdp, 0, revokes);
102 if (ret)
103 return;
104 __gfs2_ail_flush(gl);
105 gfs2_trans_end(sdp);
106 gfs2_log_flush(sdp, NULL);
107}
108
78/** 109/**
79 * rgrp_go_sync - sync out the metadata for this glock 110 * rgrp_go_sync - sync out the metadata for this glock
80 * @gl: the glock 111 * @gl: the glock
@@ -190,8 +221,10 @@ static void inode_go_inval(struct gfs2_glock *gl, int flags)
190 } 221 }
191 } 222 }
192 223
193 if (ip == GFS2_I(gl->gl_sbd->sd_rindex)) 224 if (ip == GFS2_I(gl->gl_sbd->sd_rindex)) {
225 gfs2_log_flush(gl->gl_sbd, NULL);
194 gl->gl_sbd->sd_rindex_uptodate = 0; 226 gl->gl_sbd->sd_rindex_uptodate = 0;
227 }
195 if (ip && S_ISREG(ip->i_inode.i_mode)) 228 if (ip && S_ISREG(ip->i_inode.i_mode))
196 truncate_inode_pages(ip->i_inode.i_mapping, 0); 229 truncate_inode_pages(ip->i_inode.i_mapping, 0);
197} 230}
@@ -206,12 +239,134 @@ static void inode_go_inval(struct gfs2_glock *gl, int flags)
206static int inode_go_demote_ok(const struct gfs2_glock *gl) 239static int inode_go_demote_ok(const struct gfs2_glock *gl)
207{ 240{
208 struct gfs2_sbd *sdp = gl->gl_sbd; 241 struct gfs2_sbd *sdp = gl->gl_sbd;
242 struct gfs2_holder *gh;
243
209 if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object) 244 if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
210 return 0; 245 return 0;
246
247 if (!list_empty(&gl->gl_holders)) {
248 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
249 if (gh->gh_list.next != &gl->gl_holders)
250 return 0;
251 }
252
211 return 1; 253 return 1;
212} 254}
213 255
214/** 256/**
257 * gfs2_set_nlink - Set the inode's link count based on on-disk info
258 * @inode: The inode in question
259 * @nlink: The link count
260 *
261 * If the link count has hit zero, it must never be raised, whatever the
262 * on-disk inode might say. When new struct inodes are created the link
263 * count is set to 1, so that we can safely use this test even when reading
264 * in on disk information for the first time.
265 */
266
267static void gfs2_set_nlink(struct inode *inode, u32 nlink)
268{
269 /*
270 * We will need to review setting the nlink count here in the
271 * light of the forthcoming ro bind mount work. This is a reminder
272 * to do that.
273 */
274 if ((inode->i_nlink != nlink) && (inode->i_nlink != 0)) {
275 if (nlink == 0)
276 clear_nlink(inode);
277 else
278 inode->i_nlink = nlink;
279 }
280}
281
282static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
283{
284 const struct gfs2_dinode *str = buf;
285 struct timespec atime;
286 u16 height, depth;
287
288 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
289 goto corrupt;
290 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
291 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
292 ip->i_inode.i_rdev = 0;
293 switch (ip->i_inode.i_mode & S_IFMT) {
294 case S_IFBLK:
295 case S_IFCHR:
296 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
297 be32_to_cpu(str->di_minor));
298 break;
299 };
300
301 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
302 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
303 gfs2_set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
304 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
305 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
306 atime.tv_sec = be64_to_cpu(str->di_atime);
307 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
308 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
309 ip->i_inode.i_atime = atime;
310 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
311 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
312 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
313 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
314
315 ip->i_goal = be64_to_cpu(str->di_goal_meta);
316 ip->i_generation = be64_to_cpu(str->di_generation);
317
318 ip->i_diskflags = be32_to_cpu(str->di_flags);
319 gfs2_set_inode_flags(&ip->i_inode);
320 height = be16_to_cpu(str->di_height);
321 if (unlikely(height > GFS2_MAX_META_HEIGHT))
322 goto corrupt;
323 ip->i_height = (u8)height;
324
325 depth = be16_to_cpu(str->di_depth);
326 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
327 goto corrupt;
328 ip->i_depth = (u8)depth;
329 ip->i_entries = be32_to_cpu(str->di_entries);
330
331 ip->i_eattr = be64_to_cpu(str->di_eattr);
332 if (S_ISREG(ip->i_inode.i_mode))
333 gfs2_set_aops(&ip->i_inode);
334
335 return 0;
336corrupt:
337 gfs2_consist_inode(ip);
338 return -EIO;
339}
340
341/**
342 * gfs2_inode_refresh - Refresh the incore copy of the dinode
343 * @ip: The GFS2 inode
344 *
345 * Returns: errno
346 */
347
348int gfs2_inode_refresh(struct gfs2_inode *ip)
349{
350 struct buffer_head *dibh;
351 int error;
352
353 error = gfs2_meta_inode_buffer(ip, &dibh);
354 if (error)
355 return error;
356
357 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
358 brelse(dibh);
359 return -EIO;
360 }
361
362 error = gfs2_dinode_in(ip, dibh->b_data);
363 brelse(dibh);
364 clear_bit(GIF_INVALID, &ip->i_flags);
365
366 return error;
367}
368
369/**
215 * inode_go_lock - operation done after an inode lock is locked by a process 370 * inode_go_lock - operation done after an inode lock is locked by a process
216 * @gl: the glock 371 * @gl: the glock
217 * @flags: 372 * @flags:
@@ -262,30 +417,16 @@ static int inode_go_dump(struct seq_file *seq, const struct gfs2_glock *gl)
262 const struct gfs2_inode *ip = gl->gl_object; 417 const struct gfs2_inode *ip = gl->gl_object;
263 if (ip == NULL) 418 if (ip == NULL)
264 return 0; 419 return 0;
265 gfs2_print_dbg(seq, " I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu/%llu\n", 420 gfs2_print_dbg(seq, " I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu\n",
266 (unsigned long long)ip->i_no_formal_ino, 421 (unsigned long long)ip->i_no_formal_ino,
267 (unsigned long long)ip->i_no_addr, 422 (unsigned long long)ip->i_no_addr,
268 IF2DT(ip->i_inode.i_mode), ip->i_flags, 423 IF2DT(ip->i_inode.i_mode), ip->i_flags,
269 (unsigned int)ip->i_diskflags, 424 (unsigned int)ip->i_diskflags,
270 (unsigned long long)ip->i_inode.i_size, 425 (unsigned long long)i_size_read(&ip->i_inode));
271 (unsigned long long)ip->i_disksize);
272 return 0; 426 return 0;
273} 427}
274 428
275/** 429/**
276 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
277 * @gl: the glock
278 *
279 * Returns: 1 if it's ok
280 */
281
282static int rgrp_go_demote_ok(const struct gfs2_glock *gl)
283{
284 const struct address_space *mapping = (const struct address_space *)(gl + 1);
285 return !mapping->nrpages;
286}
287
288/**
289 * rgrp_go_lock - operation done after an rgrp lock is locked by 430 * rgrp_go_lock - operation done after an rgrp lock is locked by
290 * a first holder on this node. 431 * a first holder on this node.
291 * @gl: the glock 432 * @gl: the glock
@@ -326,7 +467,6 @@ static void trans_go_sync(struct gfs2_glock *gl)
326 467
327 if (gl->gl_state != LM_ST_UNLOCKED && 468 if (gl->gl_state != LM_ST_UNLOCKED &&
328 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) { 469 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
329 flush_workqueue(gfs2_delete_workqueue);
330 gfs2_meta_syncfs(sdp); 470 gfs2_meta_syncfs(sdp);
331 gfs2_log_shutdown(sdp); 471 gfs2_log_shutdown(sdp);
332 } 472 }
@@ -385,6 +525,10 @@ static int trans_go_demote_ok(const struct gfs2_glock *gl)
385static void iopen_go_callback(struct gfs2_glock *gl) 525static void iopen_go_callback(struct gfs2_glock *gl)
386{ 526{
387 struct gfs2_inode *ip = (struct gfs2_inode *)gl->gl_object; 527 struct gfs2_inode *ip = (struct gfs2_inode *)gl->gl_object;
528 struct gfs2_sbd *sdp = gl->gl_sbd;
529
530 if (sdp->sd_vfs->s_flags & MS_RDONLY)
531 return;
388 532
389 if (gl->gl_demote_state == LM_ST_UNLOCKED && 533 if (gl->gl_demote_state == LM_ST_UNLOCKED &&
390 gl->gl_state == LM_ST_SHARED && ip) { 534 gl->gl_state == LM_ST_SHARED && ip) {
@@ -412,7 +556,6 @@ const struct gfs2_glock_operations gfs2_inode_glops = {
412const struct gfs2_glock_operations gfs2_rgrp_glops = { 556const struct gfs2_glock_operations gfs2_rgrp_glops = {
413 .go_xmote_th = rgrp_go_sync, 557 .go_xmote_th = rgrp_go_sync,
414 .go_inval = rgrp_go_inval, 558 .go_inval = rgrp_go_inval,
415 .go_demote_ok = rgrp_go_demote_ok,
416 .go_lock = rgrp_go_lock, 559 .go_lock = rgrp_go_lock,
417 .go_unlock = rgrp_go_unlock, 560 .go_unlock = rgrp_go_unlock,
418 .go_dump = gfs2_rgrp_dump, 561 .go_dump = gfs2_rgrp_dump,
@@ -453,7 +596,6 @@ const struct gfs2_glock_operations *gfs2_glops_list[] = {
453 [LM_TYPE_META] = &gfs2_meta_glops, 596 [LM_TYPE_META] = &gfs2_meta_glops,
454 [LM_TYPE_INODE] = &gfs2_inode_glops, 597 [LM_TYPE_INODE] = &gfs2_inode_glops,
455 [LM_TYPE_RGRP] = &gfs2_rgrp_glops, 598 [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
456 [LM_TYPE_NONDISK] = &gfs2_trans_glops,
457 [LM_TYPE_IOPEN] = &gfs2_iopen_glops, 599 [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
458 [LM_TYPE_FLOCK] = &gfs2_flock_glops, 600 [LM_TYPE_FLOCK] = &gfs2_flock_glops,
459 [LM_TYPE_NONDISK] = &gfs2_nondisk_glops, 601 [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
diff --git a/fs/gfs2/glops.h b/fs/gfs2/glops.h
index b3aa2e3210fd..6fce409b5a50 100644
--- a/fs/gfs2/glops.h
+++ b/fs/gfs2/glops.h
@@ -23,4 +23,6 @@ extern const struct gfs2_glock_operations gfs2_quota_glops;
23extern const struct gfs2_glock_operations gfs2_journal_glops; 23extern const struct gfs2_glock_operations gfs2_journal_glops;
24extern const struct gfs2_glock_operations *gfs2_glops_list[]; 24extern const struct gfs2_glock_operations *gfs2_glops_list[];
25 25
26extern void gfs2_ail_flush(struct gfs2_glock *gl);
27
26#endif /* __GLOPS_DOT_H__ */ 28#endif /* __GLOPS_DOT_H__ */
diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h
index fdbf4b366fa5..81206e70cbf6 100644
--- a/fs/gfs2/incore.h
+++ b/fs/gfs2/incore.h
@@ -11,13 +11,16 @@
11#define __INCORE_DOT_H__ 11#define __INCORE_DOT_H__
12 12
13#include <linux/fs.h> 13#include <linux/fs.h>
14#include <linux/kobject.h>
14#include <linux/workqueue.h> 15#include <linux/workqueue.h>
15#include <linux/dlm.h> 16#include <linux/dlm.h>
16#include <linux/buffer_head.h> 17#include <linux/buffer_head.h>
18#include <linux/rcupdate.h>
19#include <linux/rculist_bl.h>
20#include <linux/completion.h>
17 21
18#define DIO_WAIT 0x00000010 22#define DIO_WAIT 0x00000010
19#define DIO_METADATA 0x00000020 23#define DIO_METADATA 0x00000020
20#define DIO_ALL 0x00000100
21 24
22struct gfs2_log_operations; 25struct gfs2_log_operations;
23struct gfs2_log_element; 26struct gfs2_log_element;
@@ -196,22 +199,27 @@ enum {
196 GLF_REPLY_PENDING = 9, 199 GLF_REPLY_PENDING = 9,
197 GLF_INITIAL = 10, 200 GLF_INITIAL = 10,
198 GLF_FROZEN = 11, 201 GLF_FROZEN = 11,
202 GLF_QUEUED = 12,
203 GLF_LRU = 13,
204 GLF_OBJECT = 14, /* Used only for tracing */
199}; 205};
200 206
201struct gfs2_glock { 207struct gfs2_glock {
202 struct hlist_node gl_list; 208 struct hlist_bl_node gl_list;
203 unsigned long gl_flags; /* GLF_... */ 209 unsigned long gl_flags; /* GLF_... */
204 struct lm_lockname gl_name; 210 struct lm_lockname gl_name;
205 atomic_t gl_ref; 211 atomic_t gl_ref;
206 212
207 spinlock_t gl_spin; 213 spinlock_t gl_spin;
208 214
209 unsigned int gl_state; 215 /* State fields protected by gl_spin */
210 unsigned int gl_target; 216 unsigned int gl_state:2, /* Current state */
211 unsigned int gl_reply; 217 gl_target:2, /* Target state */
218 gl_demote_state:2, /* State requested by remote node */
219 gl_req:2, /* State in last dlm request */
220 gl_reply:8; /* Last reply from the dlm */
221
212 unsigned int gl_hash; 222 unsigned int gl_hash;
213 unsigned int gl_req;
214 unsigned int gl_demote_state; /* state requested by remote node */
215 unsigned long gl_demote_time; /* time of first demote request */ 223 unsigned long gl_demote_time; /* time of first demote request */
216 struct list_head gl_holders; 224 struct list_head gl_holders;
217 225
@@ -228,8 +236,10 @@ struct gfs2_glock {
228 236
229 struct list_head gl_ail_list; 237 struct list_head gl_ail_list;
230 atomic_t gl_ail_count; 238 atomic_t gl_ail_count;
239 atomic_t gl_revokes;
231 struct delayed_work gl_work; 240 struct delayed_work gl_work;
232 struct work_struct gl_delete; 241 struct work_struct gl_delete;
242 struct rcu_head gl_rcu;
233}; 243};
234 244
235#define GFS2_MIN_LVB_SIZE 32 /* Min size of LVB that gfs2 supports */ 245#define GFS2_MIN_LVB_SIZE 32 /* Min size of LVB that gfs2 supports */
@@ -267,7 +277,6 @@ struct gfs2_inode {
267 u64 i_no_formal_ino; 277 u64 i_no_formal_ino;
268 u64 i_generation; 278 u64 i_generation;
269 u64 i_eattr; 279 u64 i_eattr;
270 loff_t i_disksize;
271 unsigned long i_flags; /* GIF_... */ 280 unsigned long i_flags; /* GIF_... */
272 struct gfs2_glock *i_gl; /* Move into i_gh? */ 281 struct gfs2_glock *i_gl; /* Move into i_gh? */
273 struct gfs2_holder i_iopen_gh; 282 struct gfs2_holder i_iopen_gh;
@@ -311,6 +320,7 @@ enum {
311 QDF_USER = 0, 320 QDF_USER = 0,
312 QDF_CHANGE = 1, 321 QDF_CHANGE = 1,
313 QDF_LOCKED = 2, 322 QDF_LOCKED = 2,
323 QDF_REFRESH = 3,
314}; 324};
315 325
316struct gfs2_quota_data { 326struct gfs2_quota_data {
@@ -367,8 +377,6 @@ struct gfs2_ail {
367 unsigned int ai_first; 377 unsigned int ai_first;
368 struct list_head ai_ail1_list; 378 struct list_head ai_ail1_list;
369 struct list_head ai_ail2_list; 379 struct list_head ai_ail2_list;
370
371 u64 ai_sync_gen;
372}; 380};
373 381
374struct gfs2_journal_extent { 382struct gfs2_journal_extent {
@@ -416,11 +424,8 @@ struct gfs2_args {
416 char ar_locktable[GFS2_LOCKNAME_LEN]; /* Name of the Lock Table */ 424 char ar_locktable[GFS2_LOCKNAME_LEN]; /* Name of the Lock Table */
417 char ar_hostdata[GFS2_LOCKNAME_LEN]; /* Host specific data */ 425 char ar_hostdata[GFS2_LOCKNAME_LEN]; /* Host specific data */
418 unsigned int ar_spectator:1; /* Don't get a journal */ 426 unsigned int ar_spectator:1; /* Don't get a journal */
419 unsigned int ar_ignore_local_fs:1; /* Ignore optimisations */
420 unsigned int ar_localflocks:1; /* Let the VFS do flock|fcntl */ 427 unsigned int ar_localflocks:1; /* Let the VFS do flock|fcntl */
421 unsigned int ar_localcaching:1; /* Local caching */
422 unsigned int ar_debug:1; /* Oops on errors */ 428 unsigned int ar_debug:1; /* Oops on errors */
423 unsigned int ar_upgrade:1; /* Upgrade ondisk format */
424 unsigned int ar_posix_acl:1; /* Enable posix acls */ 429 unsigned int ar_posix_acl:1; /* Enable posix acls */
425 unsigned int ar_quota:2; /* off/account/on */ 430 unsigned int ar_quota:2; /* off/account/on */
426 unsigned int ar_suiddir:1; /* suiddir support */ 431 unsigned int ar_suiddir:1; /* suiddir support */
@@ -484,7 +489,6 @@ struct gfs2_sb_host {
484 489
485 char sb_lockproto[GFS2_LOCKNAME_LEN]; 490 char sb_lockproto[GFS2_LOCKNAME_LEN];
486 char sb_locktable[GFS2_LOCKNAME_LEN]; 491 char sb_locktable[GFS2_LOCKNAME_LEN];
487 u8 sb_uuid[16];
488}; 492};
489 493
490/* 494/*
@@ -497,7 +501,7 @@ struct gfs2_sb_host {
497 */ 501 */
498 502
499struct lm_lockstruct { 503struct lm_lockstruct {
500 unsigned int ls_jid; 504 int ls_jid;
501 unsigned int ls_first; 505 unsigned int ls_first;
502 unsigned int ls_first_done; 506 unsigned int ls_first_done;
503 unsigned int ls_nodir; 507 unsigned int ls_nodir;
@@ -543,6 +547,7 @@ struct gfs2_sbd {
543 struct gfs2_glock *sd_trans_gl; 547 struct gfs2_glock *sd_trans_gl;
544 wait_queue_head_t sd_glock_wait; 548 wait_queue_head_t sd_glock_wait;
545 atomic_t sd_glock_disposal; 549 atomic_t sd_glock_disposal;
550 struct completion sd_locking_init;
546 551
547 /* Inode Stuff */ 552 /* Inode Stuff */
548 553
@@ -572,6 +577,7 @@ struct gfs2_sbd {
572 struct list_head sd_rindex_mru_list; 577 struct list_head sd_rindex_mru_list;
573 struct gfs2_rgrpd *sd_rindex_forward; 578 struct gfs2_rgrpd *sd_rindex_forward;
574 unsigned int sd_rgrps; 579 unsigned int sd_rgrps;
580 unsigned int sd_max_rg_data;
575 581
576 /* Journal index stuff */ 582 /* Journal index stuff */
577 583
@@ -646,9 +652,9 @@ struct gfs2_sbd {
646 unsigned int sd_log_flush_head; 652 unsigned int sd_log_flush_head;
647 u64 sd_log_flush_wrapped; 653 u64 sd_log_flush_wrapped;
648 654
655 spinlock_t sd_ail_lock;
649 struct list_head sd_ail1_list; 656 struct list_head sd_ail1_list;
650 struct list_head sd_ail2_list; 657 struct list_head sd_ail2_list;
651 u64 sd_ail_sync_gen;
652 658
653 /* Replay stuff */ 659 /* Replay stuff */
654 660
diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
index 08140f185a37..03e0c529063e 100644
--- a/fs/gfs2/inode.c
+++ b/fs/gfs2/inode.c
@@ -1,23 +1,25 @@
1/* 1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 3 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
4 * 4 *
5 * This copyrighted material is made available to anyone wishing to use, 5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions 6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2. 7 * of the GNU General Public License version 2.
8 */ 8 */
9 9
10#include <linux/sched.h>
11#include <linux/slab.h> 10#include <linux/slab.h>
12#include <linux/spinlock.h> 11#include <linux/spinlock.h>
13#include <linux/completion.h> 12#include <linux/completion.h>
14#include <linux/buffer_head.h> 13#include <linux/buffer_head.h>
14#include <linux/namei.h>
15#include <linux/mm.h>
16#include <linux/xattr.h>
15#include <linux/posix_acl.h> 17#include <linux/posix_acl.h>
16#include <linux/sort.h>
17#include <linux/gfs2_ondisk.h> 18#include <linux/gfs2_ondisk.h>
18#include <linux/crc32.h> 19#include <linux/crc32.h>
20#include <linux/fiemap.h>
19#include <linux/security.h> 21#include <linux/security.h>
20#include <linux/time.h> 22#include <asm/uaccess.h>
21 23
22#include "gfs2.h" 24#include "gfs2.h"
23#include "incore.h" 25#include "incore.h"
@@ -26,65 +28,29 @@
26#include "dir.h" 28#include "dir.h"
27#include "xattr.h" 29#include "xattr.h"
28#include "glock.h" 30#include "glock.h"
29#include "glops.h"
30#include "inode.h" 31#include "inode.h"
31#include "log.h"
32#include "meta_io.h" 32#include "meta_io.h"
33#include "quota.h" 33#include "quota.h"
34#include "rgrp.h" 34#include "rgrp.h"
35#include "trans.h" 35#include "trans.h"
36#include "util.h" 36#include "util.h"
37 37#include "super.h"
38struct gfs2_inum_range_host { 38#include "glops.h"
39 u64 ir_start;
40 u64 ir_length;
41};
42
43static int iget_test(struct inode *inode, void *opaque)
44{
45 struct gfs2_inode *ip = GFS2_I(inode);
46 u64 *no_addr = opaque;
47
48 if (ip->i_no_addr == *no_addr)
49 return 1;
50
51 return 0;
52}
53
54static int iget_set(struct inode *inode, void *opaque)
55{
56 struct gfs2_inode *ip = GFS2_I(inode);
57 u64 *no_addr = opaque;
58
59 inode->i_ino = (unsigned long)*no_addr;
60 ip->i_no_addr = *no_addr;
61 return 0;
62}
63
64struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
65{
66 unsigned long hash = (unsigned long)no_addr;
67 return ilookup5(sb, hash, iget_test, &no_addr);
68}
69
70static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
71{
72 unsigned long hash = (unsigned long)no_addr;
73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
74}
75 39
76struct gfs2_skip_data { 40struct gfs2_skip_data {
77 u64 no_addr; 41 u64 no_addr;
78 int skipped; 42 int skipped;
43 int non_block;
79}; 44};
80 45
81static int iget_skip_test(struct inode *inode, void *opaque) 46static int iget_test(struct inode *inode, void *opaque)
82{ 47{
83 struct gfs2_inode *ip = GFS2_I(inode); 48 struct gfs2_inode *ip = GFS2_I(inode);
84 struct gfs2_skip_data *data = opaque; 49 struct gfs2_skip_data *data = opaque;
85 50
86 if (ip->i_no_addr == data->no_addr) { 51 if (ip->i_no_addr == data->no_addr) {
87 if (inode->i_state & (I_FREEING|I_WILL_FREE)){ 52 if (data->non_block &&
53 inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
88 data->skipped = 1; 54 data->skipped = 1;
89 return 0; 55 return 0;
90 } 56 }
@@ -93,40 +59,50 @@ static int iget_skip_test(struct inode *inode, void *opaque)
93 return 0; 59 return 0;
94} 60}
95 61
96static int iget_skip_set(struct inode *inode, void *opaque) 62static int iget_set(struct inode *inode, void *opaque)
97{ 63{
98 struct gfs2_inode *ip = GFS2_I(inode); 64 struct gfs2_inode *ip = GFS2_I(inode);
99 struct gfs2_skip_data *data = opaque; 65 struct gfs2_skip_data *data = opaque;
100 66
101 if (data->skipped) 67 if (data->skipped)
102 return 1; 68 return -ENOENT;
103 inode->i_ino = (unsigned long)(data->no_addr); 69 inode->i_ino = (unsigned long)(data->no_addr);
104 ip->i_no_addr = data->no_addr; 70 ip->i_no_addr = data->no_addr;
105 return 0; 71 return 0;
106} 72}
107 73
108static struct inode *gfs2_iget_skip(struct super_block *sb, 74struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
109 u64 no_addr) 75{
76 unsigned long hash = (unsigned long)no_addr;
77 struct gfs2_skip_data data;
78
79 data.no_addr = no_addr;
80 data.skipped = 0;
81 data.non_block = non_block;
82 return ilookup5(sb, hash, iget_test, &data);
83}
84
85static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
86 int non_block)
110{ 87{
111 struct gfs2_skip_data data; 88 struct gfs2_skip_data data;
112 unsigned long hash = (unsigned long)no_addr; 89 unsigned long hash = (unsigned long)no_addr;
113 90
114 data.no_addr = no_addr; 91 data.no_addr = no_addr;
115 data.skipped = 0; 92 data.skipped = 0;
116 return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data); 93 data.non_block = non_block;
94 return iget5_locked(sb, hash, iget_test, iget_set, &data);
117} 95}
118 96
119/** 97/**
120 * GFS2 lookup code fills in vfs inode contents based on info obtained 98 * gfs2_set_iop - Sets inode operations
121 * from directory entry inside gfs2_inode_lookup(). This has caused issues 99 * @inode: The inode with correct i_mode filled in
122 * with NFS code path since its get_dentry routine doesn't have the relevant
123 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
124 * segment inside gfs2_inode_lookup code needs to get moved around.
125 * 100 *
126 * Clears I_NEW as well. 101 * GFS2 lookup code fills in vfs inode contents based on info obtained
127 **/ 102 * from directory entry inside gfs2_inode_lookup().
103 */
128 104
129void gfs2_set_iop(struct inode *inode) 105static void gfs2_set_iop(struct inode *inode)
130{ 106{
131 struct gfs2_sbd *sdp = GFS2_SB(inode); 107 struct gfs2_sbd *sdp = GFS2_SB(inode);
132 umode_t mode = inode->i_mode; 108 umode_t mode = inode->i_mode;
@@ -149,8 +125,6 @@ void gfs2_set_iop(struct inode *inode)
149 inode->i_op = &gfs2_file_iops; 125 inode->i_op = &gfs2_file_iops;
150 init_special_inode(inode, inode->i_mode, inode->i_rdev); 126 init_special_inode(inode, inode->i_mode, inode->i_rdev);
151 } 127 }
152
153 unlock_new_inode(inode);
154} 128}
155 129
156/** 130/**
@@ -158,21 +132,20 @@ void gfs2_set_iop(struct inode *inode)
158 * @sb: The super block 132 * @sb: The super block
159 * @no_addr: The inode number 133 * @no_addr: The inode number
160 * @type: The type of the inode 134 * @type: The type of the inode
135 * non_block: Can we block on inodes that are being freed?
161 * 136 *
162 * Returns: A VFS inode, or an error 137 * Returns: A VFS inode, or an error
163 */ 138 */
164 139
165struct inode *gfs2_inode_lookup(struct super_block *sb, 140struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
166 unsigned int type, 141 u64 no_addr, u64 no_formal_ino, int non_block)
167 u64 no_addr,
168 u64 no_formal_ino)
169{ 142{
170 struct inode *inode; 143 struct inode *inode;
171 struct gfs2_inode *ip; 144 struct gfs2_inode *ip;
172 struct gfs2_glock *io_gl = NULL; 145 struct gfs2_glock *io_gl = NULL;
173 int error; 146 int error;
174 147
175 inode = gfs2_iget(sb, no_addr); 148 inode = gfs2_iget(sb, no_addr, non_block);
176 ip = GFS2_I(inode); 149 ip = GFS2_I(inode);
177 150
178 if (!inode) 151 if (!inode)
@@ -195,132 +168,29 @@ struct inode *gfs2_inode_lookup(struct super_block *sb,
195 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); 168 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
196 if (unlikely(error)) 169 if (unlikely(error))
197 goto fail_iopen; 170 goto fail_iopen;
198 ip->i_iopen_gh.gh_gl->gl_object = ip;
199 171
172 ip->i_iopen_gh.gh_gl->gl_object = ip;
200 gfs2_glock_put(io_gl); 173 gfs2_glock_put(io_gl);
201 io_gl = NULL; 174 io_gl = NULL;
202 175
203 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
204 goto gfs2_nfsbypass;
205
206 inode->i_mode = DT2IF(type);
207
208 /*
209 * We must read the inode in order to work out its type in
210 * this case. Note that this doesn't happen often as we normally
211 * know the type beforehand. This code path only occurs during
212 * unlinked inode recovery (where it is safe to do this glock,
213 * which is not true in the general case).
214 */
215 if (type == DT_UNKNOWN) { 176 if (type == DT_UNKNOWN) {
216 struct gfs2_holder gh; 177 /* Inode glock must be locked already */
217 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 178 error = gfs2_inode_refresh(GFS2_I(inode));
218 if (unlikely(error)) 179 if (error)
219 goto fail_glock; 180 goto fail_refresh;
220 /* Inode is now uptodate */ 181 } else {
221 gfs2_glock_dq_uninit(&gh); 182 inode->i_mode = DT2IF(type);
222 } 183 }
223 184
224 gfs2_set_iop(inode); 185 gfs2_set_iop(inode);
186 unlock_new_inode(inode);
225 } 187 }
226 188
227gfs2_nfsbypass:
228 return inode; 189 return inode;
229fail_glock:
230 gfs2_glock_dq(&ip->i_iopen_gh);
231fail_iopen:
232 if (io_gl)
233 gfs2_glock_put(io_gl);
234fail_put:
235 if (inode->i_state & I_NEW)
236 ip->i_gl->gl_object = NULL;
237 gfs2_glock_put(ip->i_gl);
238fail:
239 if (inode->i_state & I_NEW)
240 iget_failed(inode);
241 else
242 iput(inode);
243 return ERR_PTR(error);
244}
245
246/**
247 * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation
248 * and try to reclaim it by doing iput.
249 *
250 * This function assumes no rgrp locks are currently held.
251 *
252 * @sb: The super block
253 * no_addr: The inode number
254 *
255 */
256
257void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr)
258{
259 struct gfs2_sbd *sdp;
260 struct gfs2_inode *ip;
261 struct gfs2_glock *io_gl = NULL;
262 int error;
263 struct gfs2_holder gh;
264 struct inode *inode;
265
266 inode = gfs2_iget_skip(sb, no_addr);
267
268 if (!inode)
269 return;
270
271 /* If it's not a new inode, someone's using it, so leave it alone. */
272 if (!(inode->i_state & I_NEW)) {
273 iput(inode);
274 return;
275 }
276
277 ip = GFS2_I(inode);
278 sdp = GFS2_SB(inode);
279 ip->i_no_formal_ino = -1;
280
281 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
282 if (unlikely(error))
283 goto fail;
284 ip->i_gl->gl_object = ip;
285
286 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
287 if (unlikely(error))
288 goto fail_put;
289
290 set_bit(GIF_INVALID, &ip->i_flags);
291 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, LM_FLAG_TRY | GL_EXACT,
292 &ip->i_iopen_gh);
293 if (unlikely(error))
294 goto fail_iopen;
295
296 ip->i_iopen_gh.gh_gl->gl_object = ip;
297 gfs2_glock_put(io_gl);
298 io_gl = NULL;
299
300 inode->i_mode = DT2IF(DT_UNKNOWN);
301
302 /*
303 * We must read the inode in order to work out its type in
304 * this case. Note that this doesn't happen often as we normally
305 * know the type beforehand. This code path only occurs during
306 * unlinked inode recovery (where it is safe to do this glock,
307 * which is not true in the general case).
308 */
309 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY,
310 &gh);
311 if (unlikely(error))
312 goto fail_glock;
313
314 /* Inode is now uptodate */
315 gfs2_glock_dq_uninit(&gh);
316 gfs2_set_iop(inode);
317
318 /* The iput will cause it to be deleted. */
319 iput(inode);
320 return;
321 190
322fail_glock: 191fail_refresh:
323 gfs2_glock_dq(&ip->i_iopen_gh); 192 ip->i_iopen_gh.gh_gl->gl_object = NULL;
193 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
324fail_iopen: 194fail_iopen:
325 if (io_gl) 195 if (io_gl)
326 gfs2_glock_put(io_gl); 196 gfs2_glock_put(io_gl);
@@ -329,208 +199,53 @@ fail_put:
329 gfs2_glock_put(ip->i_gl); 199 gfs2_glock_put(ip->i_gl);
330fail: 200fail:
331 iget_failed(inode); 201 iget_failed(inode);
332 return; 202 return ERR_PTR(error);
333}
334
335static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
336{
337 const struct gfs2_dinode *str = buf;
338 struct timespec atime;
339 u16 height, depth;
340
341 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
342 goto corrupt;
343 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
344 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
345 ip->i_inode.i_rdev = 0;
346 switch (ip->i_inode.i_mode & S_IFMT) {
347 case S_IFBLK:
348 case S_IFCHR:
349 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
350 be32_to_cpu(str->di_minor));
351 break;
352 };
353
354 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
355 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
356 /*
357 * We will need to review setting the nlink count here in the
358 * light of the forthcoming ro bind mount work. This is a reminder
359 * to do that.
360 */
361 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
362 ip->i_disksize = be64_to_cpu(str->di_size);
363 i_size_write(&ip->i_inode, ip->i_disksize);
364 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
365 atime.tv_sec = be64_to_cpu(str->di_atime);
366 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
367 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
368 ip->i_inode.i_atime = atime;
369 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
370 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
371 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
372 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
373
374 ip->i_goal = be64_to_cpu(str->di_goal_meta);
375 ip->i_generation = be64_to_cpu(str->di_generation);
376
377 ip->i_diskflags = be32_to_cpu(str->di_flags);
378 gfs2_set_inode_flags(&ip->i_inode);
379 height = be16_to_cpu(str->di_height);
380 if (unlikely(height > GFS2_MAX_META_HEIGHT))
381 goto corrupt;
382 ip->i_height = (u8)height;
383
384 depth = be16_to_cpu(str->di_depth);
385 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
386 goto corrupt;
387 ip->i_depth = (u8)depth;
388 ip->i_entries = be32_to_cpu(str->di_entries);
389
390 ip->i_eattr = be64_to_cpu(str->di_eattr);
391 if (S_ISREG(ip->i_inode.i_mode))
392 gfs2_set_aops(&ip->i_inode);
393
394 return 0;
395corrupt:
396 if (gfs2_consist_inode(ip))
397 gfs2_dinode_print(ip);
398 return -EIO;
399}
400
401/**
402 * gfs2_inode_refresh - Refresh the incore copy of the dinode
403 * @ip: The GFS2 inode
404 *
405 * Returns: errno
406 */
407
408int gfs2_inode_refresh(struct gfs2_inode *ip)
409{
410 struct buffer_head *dibh;
411 int error;
412
413 error = gfs2_meta_inode_buffer(ip, &dibh);
414 if (error)
415 return error;
416
417 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
418 brelse(dibh);
419 return -EIO;
420 }
421
422 error = gfs2_dinode_in(ip, dibh->b_data);
423 brelse(dibh);
424 clear_bit(GIF_INVALID, &ip->i_flags);
425
426 return error;
427} 203}
428 204
429int gfs2_dinode_dealloc(struct gfs2_inode *ip) 205struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
206 u64 *no_formal_ino, unsigned int blktype)
430{ 207{
431 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 208 struct super_block *sb = sdp->sd_vfs;
432 struct gfs2_alloc *al; 209 struct gfs2_holder i_gh;
433 struct gfs2_rgrpd *rgd; 210 struct inode *inode = NULL;
434 int error; 211 int error;
435 212
436 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { 213 /* Must not read in block until block type is verified */
437 if (gfs2_consist_inode(ip)) 214 error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
438 gfs2_dinode_print(ip); 215 LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
439 return -EIO;
440 }
441
442 al = gfs2_alloc_get(ip);
443 if (!al)
444 return -ENOMEM;
445
446 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
447 if (error)
448 goto out;
449
450 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
451 if (error)
452 goto out_qs;
453
454 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
455 if (!rgd) {
456 gfs2_consist_inode(ip);
457 error = -EIO;
458 goto out_rindex_relse;
459 }
460
461 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
462 &al->al_rgd_gh);
463 if (error) 216 if (error)
464 goto out_rindex_relse; 217 return ERR_PTR(error);
465 218
466 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); 219 error = gfs2_check_blk_type(sdp, no_addr, blktype);
467 if (error) 220 if (error)
468 goto out_rg_gunlock; 221 goto fail;
469
470 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
471 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
472
473 gfs2_free_di(rgd, ip);
474
475 gfs2_trans_end(sdp);
476 222
477out_rg_gunlock: 223 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
478 gfs2_glock_dq_uninit(&al->al_rgd_gh); 224 if (IS_ERR(inode))
479out_rindex_relse: 225 goto fail;
480 gfs2_glock_dq_uninit(&al->al_ri_gh);
481out_qs:
482 gfs2_quota_unhold(ip);
483out:
484 gfs2_alloc_put(ip);
485 return error;
486}
487 226
488/** 227 /* Two extra checks for NFS only */
489 * gfs2_change_nlink - Change nlink count on inode 228 if (no_formal_ino) {
490 * @ip: The GFS2 inode 229 error = -ESTALE;
491 * @diff: The change in the nlink count required 230 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
492 * 231 goto fail_iput;
493 * Returns: errno
494 */
495int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
496{
497 struct buffer_head *dibh;
498 u32 nlink;
499 int error;
500 232
501 BUG_ON(diff != 1 && diff != -1); 233 error = -EIO;
502 nlink = ip->i_inode.i_nlink + diff; 234 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
235 goto fail_iput;
503 236
504 /* If we are reducing the nlink count, but the new value ends up being 237 error = 0;
505 bigger than the old one, we must have underflowed. */
506 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
507 if (gfs2_consist_inode(ip))
508 gfs2_dinode_print(ip);
509 return -EIO;
510 } 238 }
511 239
512 error = gfs2_meta_inode_buffer(ip, &dibh); 240fail:
513 if (error) 241 gfs2_glock_dq_uninit(&i_gh);
514 return error; 242 return error ? ERR_PTR(error) : inode;
515 243fail_iput:
516 if (diff > 0) 244 iput(inode);
517 inc_nlink(&ip->i_inode); 245 goto fail;
518 else
519 drop_nlink(&ip->i_inode);
520
521 ip->i_inode.i_ctime = CURRENT_TIME;
522
523 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
524 gfs2_dinode_out(ip, dibh->b_data);
525 brelse(dibh);
526 mark_inode_dirty(&ip->i_inode);
527
528 if (ip->i_inode.i_nlink == 0)
529 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
530
531 return error;
532} 246}
533 247
248
534struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) 249struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
535{ 250{
536 struct qstr qstr; 251 struct qstr qstr;
@@ -592,7 +307,7 @@ struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
592 } 307 }
593 308
594 if (!is_root) { 309 if (!is_root) {
595 error = gfs2_permission(dir, MAY_EXEC); 310 error = gfs2_permission(dir, MAY_EXEC, 0);
596 if (error) 311 if (error)
597 goto out; 312 goto out;
598 } 313 }
@@ -622,13 +337,13 @@ static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
622{ 337{
623 int error; 338 int error;
624 339
625 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC); 340 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
626 if (error) 341 if (error)
627 return error; 342 return error;
628 343
629 /* Don't create entries in an unlinked directory */ 344 /* Don't create entries in an unlinked directory */
630 if (!dip->i_inode.i_nlink) 345 if (!dip->i_inode.i_nlink)
631 return -EPERM; 346 return -ENOENT;
632 347
633 error = gfs2_dir_check(&dip->i_inode, name, NULL); 348 error = gfs2_dir_check(&dip->i_inode, name, NULL);
634 switch (error) { 349 switch (error) {
@@ -698,21 +413,44 @@ out:
698 return error; 413 return error;
699} 414}
700 415
416static void gfs2_init_dir(struct buffer_head *dibh,
417 const struct gfs2_inode *parent)
418{
419 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
420 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
421
422 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
423 dent->de_inum = di->di_num; /* already GFS2 endian */
424 dent->de_type = cpu_to_be16(DT_DIR);
425
426 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
427 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
428 gfs2_inum_out(parent, dent);
429 dent->de_type = cpu_to_be16(DT_DIR);
430
431}
432
701/** 433/**
702 * init_dinode - Fill in a new dinode structure 434 * init_dinode - Fill in a new dinode structure
703 * @dip: the directory this inode is being created in 435 * @dip: The directory this inode is being created in
704 * @gl: The glock covering the new inode 436 * @gl: The glock covering the new inode
705 * @inum: the inode number 437 * @inum: The inode number
706 * @mode: the file permissions 438 * @mode: The file permissions
707 * @uid: 439 * @uid: The uid of the new inode
708 * @gid: 440 * @gid: The gid of the new inode
441 * @generation: The generation number of the new inode
442 * @dev: The device number (if a device node)
443 * @symname: The symlink destination (if a symlink)
444 * @size: The inode size (ignored for directories)
445 * @bhp: The buffer head (returned to caller)
709 * 446 *
710 */ 447 */
711 448
712static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 449static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
713 const struct gfs2_inum_host *inum, unsigned int mode, 450 const struct gfs2_inum_host *inum, unsigned int mode,
714 unsigned int uid, unsigned int gid, 451 unsigned int uid, unsigned int gid,
715 const u64 *generation, dev_t dev, struct buffer_head **bhp) 452 const u64 *generation, dev_t dev, const char *symname,
453 unsigned size, struct buffer_head **bhp)
716{ 454{
717 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 455 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
718 struct gfs2_dinode *di; 456 struct gfs2_dinode *di;
@@ -731,7 +469,7 @@ static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
731 di->di_uid = cpu_to_be32(uid); 469 di->di_uid = cpu_to_be32(uid);
732 di->di_gid = cpu_to_be32(gid); 470 di->di_gid = cpu_to_be32(gid);
733 di->di_nlink = 0; 471 di->di_nlink = 0;
734 di->di_size = 0; 472 di->di_size = cpu_to_be64(size);
735 di->di_blocks = cpu_to_be64(1); 473 di->di_blocks = cpu_to_be64(1);
736 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec); 474 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
737 di->di_major = cpu_to_be32(MAJOR(dev)); 475 di->di_major = cpu_to_be32(MAJOR(dev));
@@ -739,16 +477,6 @@ static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
739 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); 477 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
740 di->di_generation = cpu_to_be64(*generation); 478 di->di_generation = cpu_to_be64(*generation);
741 di->di_flags = 0; 479 di->di_flags = 0;
742
743 if (S_ISREG(mode)) {
744 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
745 gfs2_tune_get(sdp, gt_new_files_jdata))
746 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
747 } else if (S_ISDIR(mode)) {
748 di->di_flags |= cpu_to_be32(dip->i_diskflags &
749 GFS2_DIF_INHERIT_JDATA);
750 }
751
752 di->__pad1 = 0; 480 di->__pad1 = 0;
753 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0); 481 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
754 di->di_height = 0; 482 di->di_height = 0;
@@ -762,7 +490,26 @@ static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
762 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec); 490 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
763 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec); 491 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
764 memset(&di->di_reserved, 0, sizeof(di->di_reserved)); 492 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
765 493
494 switch(mode & S_IFMT) {
495 case S_IFREG:
496 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
497 gfs2_tune_get(sdp, gt_new_files_jdata))
498 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
499 break;
500 case S_IFDIR:
501 di->di_flags |= cpu_to_be32(dip->i_diskflags &
502 GFS2_DIF_INHERIT_JDATA);
503 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
504 di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
505 di->di_entries = cpu_to_be32(2);
506 gfs2_init_dir(dibh, dip);
507 break;
508 case S_IFLNK:
509 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
510 break;
511 }
512
766 set_buffer_uptodate(dibh); 513 set_buffer_uptodate(dibh);
767 514
768 *bhp = dibh; 515 *bhp = dibh;
@@ -770,7 +517,8 @@ static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
770 517
771static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 518static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
772 unsigned int mode, const struct gfs2_inum_host *inum, 519 unsigned int mode, const struct gfs2_inum_host *inum,
773 const u64 *generation, dev_t dev, struct buffer_head **bhp) 520 const u64 *generation, dev_t dev, const char *symname,
521 unsigned int size, struct buffer_head **bhp)
774{ 522{
775 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 523 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
776 unsigned int uid, gid; 524 unsigned int uid, gid;
@@ -792,7 +540,7 @@ static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
792 if (error) 540 if (error)
793 goto out_quota; 541 goto out_quota;
794 542
795 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp); 543 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
796 gfs2_quota_change(dip, +1, uid, gid); 544 gfs2_quota_change(dip, +1, uid, gid);
797 gfs2_trans_end(sdp); 545 gfs2_trans_end(sdp);
798 546
@@ -846,14 +594,16 @@ static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
846 goto fail_quota_locks; 594 goto fail_quota_locks;
847 } 595 }
848 596
849 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode)); 597 error = gfs2_dir_add(&dip->i_inode, name, ip);
850 if (error) 598 if (error)
851 goto fail_end_trans; 599 goto fail_end_trans;
852 600
853 error = gfs2_meta_inode_buffer(ip, &dibh); 601 error = gfs2_meta_inode_buffer(ip, &dibh);
854 if (error) 602 if (error)
855 goto fail_end_trans; 603 goto fail_end_trans;
856 ip->i_inode.i_nlink = 1; 604 inc_nlink(&ip->i_inode);
605 if (S_ISDIR(ip->i_inode.i_mode))
606 inc_nlink(&ip->i_inode);
857 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 607 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
858 gfs2_dinode_out(ip, dibh->b_data); 608 gfs2_dinode_out(ip, dibh->b_data);
859 brelse(dibh); 609 brelse(dibh);
@@ -874,14 +624,15 @@ fail:
874 return error; 624 return error;
875} 625}
876 626
877static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) 627static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
628 const struct qstr *qstr)
878{ 629{
879 int err; 630 int err;
880 size_t len; 631 size_t len;
881 void *value; 632 void *value;
882 char *name; 633 char *name;
883 634
884 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, 635 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
885 &name, &value, &len); 636 &name, &value, &len);
886 637
887 if (err) { 638 if (err) {
@@ -899,27 +650,25 @@ static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
899} 650}
900 651
901/** 652/**
902 * gfs2_createi - Create a new inode 653 * gfs2_create_inode - Create a new inode
903 * @ghs: An array of two holders 654 * @dir: The parent directory
904 * @name: The name of the new file 655 * @dentry: The new dentry
905 * @mode: the permissions on the new inode 656 * @mode: The permissions on the new inode
906 * 657 * @dev: For device nodes, this is the device number
907 * @ghs[0] is an initialized holder for the directory 658 * @symname: For symlinks, this is the link destination
908 * @ghs[1] is the holder for the inode lock 659 * @size: The initial size of the inode (ignored for directories)
909 *
910 * If the return value is not NULL, the glocks on both the directory and the new
911 * file are held. A transaction has been started and an inplace reservation
912 * is held, as well.
913 * 660 *
914 * Returns: An inode 661 * Returns: 0 on success, or error code
915 */ 662 */
916 663
917struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, 664static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
918 unsigned int mode, dev_t dev) 665 unsigned int mode, dev_t dev, const char *symname,
666 unsigned int size)
919{ 667{
668 const struct qstr *name = &dentry->d_name;
669 struct gfs2_holder ghs[2];
920 struct inode *inode = NULL; 670 struct inode *inode = NULL;
921 struct gfs2_inode *dip = ghs->gh_gl->gl_object; 671 struct gfs2_inode *dip = GFS2_I(dir);
922 struct inode *dir = &dip->i_inode;
923 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 672 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
924 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 }; 673 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
925 int error; 674 int error;
@@ -927,10 +676,9 @@ struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
927 struct buffer_head *bh = NULL; 676 struct buffer_head *bh = NULL;
928 677
929 if (!name->len || name->len > GFS2_FNAMESIZE) 678 if (!name->len || name->len > GFS2_FNAMESIZE)
930 return ERR_PTR(-ENAMETOOLONG); 679 return -ENAMETOOLONG;
931 680
932 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); 681 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
933 error = gfs2_glock_nq(ghs);
934 if (error) 682 if (error)
935 goto fail; 683 goto fail;
936 684
@@ -948,12 +696,12 @@ struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
948 if (error) 696 if (error)
949 goto fail_gunlock; 697 goto fail_gunlock;
950 698
951 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh); 699 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
952 if (error) 700 if (error)
953 goto fail_gunlock2; 701 goto fail_gunlock2;
954 702
955 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr, 703 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
956 inum.no_formal_ino); 704 inum.no_formal_ino, 0);
957 if (IS_ERR(inode)) 705 if (IS_ERR(inode))
958 goto fail_gunlock2; 706 goto fail_gunlock2;
959 707
@@ -965,7 +713,7 @@ struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
965 if (error) 713 if (error)
966 goto fail_gunlock2; 714 goto fail_gunlock2;
967 715
968 error = gfs2_security_init(dip, GFS2_I(inode)); 716 error = gfs2_security_init(dip, GFS2_I(inode), name);
969 if (error) 717 if (error)
970 goto fail_gunlock2; 718 goto fail_gunlock2;
971 719
@@ -975,41 +723,866 @@ struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
975 723
976 if (bh) 724 if (bh)
977 brelse(bh); 725 brelse(bh);
978 return inode; 726
727 gfs2_trans_end(sdp);
728 if (dip->i_alloc->al_rgd)
729 gfs2_inplace_release(dip);
730 gfs2_quota_unlock(dip);
731 gfs2_alloc_put(dip);
732 gfs2_glock_dq_uninit_m(2, ghs);
733 mark_inode_dirty(inode);
734 d_instantiate(dentry, inode);
735 return 0;
979 736
980fail_gunlock2: 737fail_gunlock2:
981 gfs2_glock_dq_uninit(ghs + 1); 738 gfs2_glock_dq_uninit(ghs + 1);
982 if (inode && !IS_ERR(inode)) 739 if (inode && !IS_ERR(inode))
983 iput(inode); 740 iput(inode);
984fail_gunlock: 741fail_gunlock:
985 gfs2_glock_dq(ghs); 742 gfs2_glock_dq_uninit(ghs);
986fail: 743fail:
987 if (bh) 744 if (bh)
988 brelse(bh); 745 brelse(bh);
989 return ERR_PTR(error); 746 return error;
990} 747}
991 748
992static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) 749/**
750 * gfs2_create - Create a file
751 * @dir: The directory in which to create the file
752 * @dentry: The dentry of the new file
753 * @mode: The mode of the new file
754 *
755 * Returns: errno
756 */
757
758static int gfs2_create(struct inode *dir, struct dentry *dentry,
759 int mode, struct nameidata *nd)
993{ 760{
994 struct inode *inode = &ip->i_inode; 761 struct inode *inode;
762 int ret;
763
764 for (;;) {
765 ret = gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0);
766 if (ret != -EEXIST || (nd && (nd->flags & LOOKUP_EXCL)))
767 return ret;
768
769 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
770 if (inode) {
771 if (!IS_ERR(inode))
772 break;
773 return PTR_ERR(inode);
774 }
775 }
776
777 d_instantiate(dentry, inode);
778 return 0;
779}
780
781/**
782 * gfs2_lookup - Look up a filename in a directory and return its inode
783 * @dir: The directory inode
784 * @dentry: The dentry of the new inode
785 * @nd: passed from Linux VFS, ignored by us
786 *
787 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
788 *
789 * Returns: errno
790 */
791
792static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
793 struct nameidata *nd)
794{
795 struct inode *inode = NULL;
796
797 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
798 if (inode && IS_ERR(inode))
799 return ERR_CAST(inode);
800
801 if (inode) {
802 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
803 struct gfs2_holder gh;
804 int error;
805 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
806 if (error) {
807 iput(inode);
808 return ERR_PTR(error);
809 }
810 gfs2_glock_dq_uninit(&gh);
811 return d_splice_alias(inode, dentry);
812 }
813 d_add(dentry, inode);
814
815 return NULL;
816}
817
818/**
819 * gfs2_link - Link to a file
820 * @old_dentry: The inode to link
821 * @dir: Add link to this directory
822 * @dentry: The name of the link
823 *
824 * Link the inode in "old_dentry" into the directory "dir" with the
825 * name in "dentry".
826 *
827 * Returns: errno
828 */
829
830static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
831 struct dentry *dentry)
832{
833 struct gfs2_inode *dip = GFS2_I(dir);
834 struct gfs2_sbd *sdp = GFS2_SB(dir);
835 struct inode *inode = old_dentry->d_inode;
836 struct gfs2_inode *ip = GFS2_I(inode);
837 struct gfs2_holder ghs[2];
995 struct buffer_head *dibh; 838 struct buffer_head *dibh;
839 int alloc_required;
996 int error; 840 int error;
997 841
842 if (S_ISDIR(inode->i_mode))
843 return -EPERM;
844
845 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
846 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
847
848 error = gfs2_glock_nq(ghs); /* parent */
849 if (error)
850 goto out_parent;
851
852 error = gfs2_glock_nq(ghs + 1); /* child */
853 if (error)
854 goto out_child;
855
856 error = -ENOENT;
857 if (inode->i_nlink == 0)
858 goto out_gunlock;
859
860 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
861 if (error)
862 goto out_gunlock;
863
864 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
865 switch (error) {
866 case -ENOENT:
867 break;
868 case 0:
869 error = -EEXIST;
870 default:
871 goto out_gunlock;
872 }
873
874 error = -EINVAL;
875 if (!dip->i_inode.i_nlink)
876 goto out_gunlock;
877 error = -EFBIG;
878 if (dip->i_entries == (u32)-1)
879 goto out_gunlock;
880 error = -EPERM;
881 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
882 goto out_gunlock;
883 error = -EINVAL;
884 if (!ip->i_inode.i_nlink)
885 goto out_gunlock;
886 error = -EMLINK;
887 if (ip->i_inode.i_nlink == (u32)-1)
888 goto out_gunlock;
889
890 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
891 if (error < 0)
892 goto out_gunlock;
893 error = 0;
894
895 if (alloc_required) {
896 struct gfs2_alloc *al = gfs2_alloc_get(dip);
897 if (!al) {
898 error = -ENOMEM;
899 goto out_gunlock;
900 }
901
902 error = gfs2_quota_lock_check(dip);
903 if (error)
904 goto out_alloc;
905
906 al->al_requested = sdp->sd_max_dirres;
907
908 error = gfs2_inplace_reserve(dip);
909 if (error)
910 goto out_gunlock_q;
911
912 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
913 gfs2_rg_blocks(al) +
914 2 * RES_DINODE + RES_STATFS +
915 RES_QUOTA, 0);
916 if (error)
917 goto out_ipres;
918 } else {
919 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
920 if (error)
921 goto out_ipres;
922 }
923
998 error = gfs2_meta_inode_buffer(ip, &dibh); 924 error = gfs2_meta_inode_buffer(ip, &dibh);
999 if (error) 925 if (error)
926 goto out_end_trans;
927
928 error = gfs2_dir_add(dir, &dentry->d_name, ip);
929 if (error)
930 goto out_brelse;
931
932 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
933 inc_nlink(&ip->i_inode);
934 ip->i_inode.i_ctime = CURRENT_TIME;
935 gfs2_dinode_out(ip, dibh->b_data);
936 mark_inode_dirty(&ip->i_inode);
937
938out_brelse:
939 brelse(dibh);
940out_end_trans:
941 gfs2_trans_end(sdp);
942out_ipres:
943 if (alloc_required)
944 gfs2_inplace_release(dip);
945out_gunlock_q:
946 if (alloc_required)
947 gfs2_quota_unlock(dip);
948out_alloc:
949 if (alloc_required)
950 gfs2_alloc_put(dip);
951out_gunlock:
952 gfs2_glock_dq(ghs + 1);
953out_child:
954 gfs2_glock_dq(ghs);
955out_parent:
956 gfs2_holder_uninit(ghs);
957 gfs2_holder_uninit(ghs + 1);
958 if (!error) {
959 ihold(inode);
960 d_instantiate(dentry, inode);
961 mark_inode_dirty(inode);
962 }
963 return error;
964}
965
966/*
967 * gfs2_unlink_ok - check to see that a inode is still in a directory
968 * @dip: the directory
969 * @name: the name of the file
970 * @ip: the inode
971 *
972 * Assumes that the lock on (at least) @dip is held.
973 *
974 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
975 */
976
977static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
978 const struct gfs2_inode *ip)
979{
980 int error;
981
982 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
983 return -EPERM;
984
985 if ((dip->i_inode.i_mode & S_ISVTX) &&
986 dip->i_inode.i_uid != current_fsuid() &&
987 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
988 return -EPERM;
989
990 if (IS_APPEND(&dip->i_inode))
991 return -EPERM;
992
993 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
994 if (error)
995 return error;
996
997 error = gfs2_dir_check(&dip->i_inode, name, ip);
998 if (error)
1000 return error; 999 return error;
1001 1000
1002 if ((attr->ia_valid & ATTR_SIZE) && 1001 return 0;
1003 attr->ia_size != i_size_read(inode)) { 1002}
1004 error = vmtruncate(inode, attr->ia_size); 1003
1004/**
1005 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1006 * @dip: The parent directory
1007 * @name: The name of the entry in the parent directory
1008 * @bh: The inode buffer for the inode to be removed
1009 * @inode: The inode to be removed
1010 *
1011 * Called with all the locks and in a transaction. This will only be
1012 * called for a directory after it has been checked to ensure it is empty.
1013 *
1014 * Returns: 0 on success, or an error
1015 */
1016
1017static int gfs2_unlink_inode(struct gfs2_inode *dip,
1018 const struct dentry *dentry,
1019 struct buffer_head *bh)
1020{
1021 struct inode *inode = dentry->d_inode;
1022 struct gfs2_inode *ip = GFS2_I(inode);
1023 int error;
1024
1025 error = gfs2_dir_del(dip, dentry);
1026 if (error)
1027 return error;
1028
1029 ip->i_entries = 0;
1030 inode->i_ctime = CURRENT_TIME;
1031 if (S_ISDIR(inode->i_mode))
1032 clear_nlink(inode);
1033 else
1034 drop_nlink(inode);
1035 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1036 gfs2_dinode_out(ip, bh->b_data);
1037 mark_inode_dirty(inode);
1038 if (inode->i_nlink == 0)
1039 gfs2_unlink_di(inode);
1040 return 0;
1041}
1042
1043
1044/**
1045 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1046 * @dir: The inode of the directory containing the inode to unlink
1047 * @dentry: The file itself
1048 *
1049 * This routine uses the type of the inode as a flag to figure out
1050 * whether this is an unlink or an rmdir.
1051 *
1052 * Returns: errno
1053 */
1054
1055static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1056{
1057 struct gfs2_inode *dip = GFS2_I(dir);
1058 struct gfs2_sbd *sdp = GFS2_SB(dir);
1059 struct inode *inode = dentry->d_inode;
1060 struct gfs2_inode *ip = GFS2_I(inode);
1061 struct buffer_head *bh;
1062 struct gfs2_holder ghs[3];
1063 struct gfs2_rgrpd *rgd;
1064 struct gfs2_holder ri_gh;
1065 int error;
1066
1067 error = gfs2_rindex_hold(sdp, &ri_gh);
1068 if (error)
1069 return error;
1070
1071 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1072 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1073
1074 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
1075 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1076
1077
1078 error = gfs2_glock_nq(ghs); /* parent */
1079 if (error)
1080 goto out_parent;
1081
1082 error = gfs2_glock_nq(ghs + 1); /* child */
1083 if (error)
1084 goto out_child;
1085
1086 error = -ENOENT;
1087 if (inode->i_nlink == 0)
1088 goto out_rgrp;
1089
1090 if (S_ISDIR(inode->i_mode)) {
1091 error = -ENOTEMPTY;
1092 if (ip->i_entries > 2 || inode->i_nlink > 2)
1093 goto out_rgrp;
1094 }
1095
1096 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1097 if (error)
1098 goto out_rgrp;
1099
1100 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1101 if (error)
1102 goto out_gunlock;
1103
1104 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1105 if (error)
1106 goto out_gunlock;
1107
1108 error = gfs2_meta_inode_buffer(ip, &bh);
1109 if (error)
1110 goto out_end_trans;
1111
1112 error = gfs2_unlink_inode(dip, dentry, bh);
1113 brelse(bh);
1114
1115out_end_trans:
1116 gfs2_trans_end(sdp);
1117out_gunlock:
1118 gfs2_glock_dq(ghs + 2);
1119out_rgrp:
1120 gfs2_holder_uninit(ghs + 2);
1121 gfs2_glock_dq(ghs + 1);
1122out_child:
1123 gfs2_holder_uninit(ghs + 1);
1124 gfs2_glock_dq(ghs);
1125out_parent:
1126 gfs2_holder_uninit(ghs);
1127 gfs2_glock_dq_uninit(&ri_gh);
1128 return error;
1129}
1130
1131/**
1132 * gfs2_symlink - Create a symlink
1133 * @dir: The directory to create the symlink in
1134 * @dentry: The dentry to put the symlink in
1135 * @symname: The thing which the link points to
1136 *
1137 * Returns: errno
1138 */
1139
1140static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
1141 const char *symname)
1142{
1143 struct gfs2_sbd *sdp = GFS2_SB(dir);
1144 unsigned int size;
1145
1146 size = strlen(symname);
1147 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
1148 return -ENAMETOOLONG;
1149
1150 return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size);
1151}
1152
1153/**
1154 * gfs2_mkdir - Make a directory
1155 * @dir: The parent directory of the new one
1156 * @dentry: The dentry of the new directory
1157 * @mode: The mode of the new directory
1158 *
1159 * Returns: errno
1160 */
1161
1162static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1163{
1164 return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0);
1165}
1166
1167/**
1168 * gfs2_mknod - Make a special file
1169 * @dir: The directory in which the special file will reside
1170 * @dentry: The dentry of the special file
1171 * @mode: The mode of the special file
1172 * @dev: The device specification of the special file
1173 *
1174 */
1175
1176static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
1177 dev_t dev)
1178{
1179 return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0);
1180}
1181
1182/*
1183 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1184 * @this: move this
1185 * @to: to here
1186 *
1187 * Follow @to back to the root and make sure we don't encounter @this
1188 * Assumes we already hold the rename lock.
1189 *
1190 * Returns: errno
1191 */
1192
1193static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1194{
1195 struct inode *dir = &to->i_inode;
1196 struct super_block *sb = dir->i_sb;
1197 struct inode *tmp;
1198 int error = 0;
1199
1200 igrab(dir);
1201
1202 for (;;) {
1203 if (dir == &this->i_inode) {
1204 error = -EINVAL;
1205 break;
1206 }
1207 if (dir == sb->s_root->d_inode) {
1208 error = 0;
1209 break;
1210 }
1211
1212 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1213 if (IS_ERR(tmp)) {
1214 error = PTR_ERR(tmp);
1215 break;
1216 }
1217
1218 iput(dir);
1219 dir = tmp;
1220 }
1221
1222 iput(dir);
1223
1224 return error;
1225}
1226
1227/**
1228 * gfs2_rename - Rename a file
1229 * @odir: Parent directory of old file name
1230 * @odentry: The old dentry of the file
1231 * @ndir: Parent directory of new file name
1232 * @ndentry: The new dentry of the file
1233 *
1234 * Returns: errno
1235 */
1236
1237static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1238 struct inode *ndir, struct dentry *ndentry)
1239{
1240 struct gfs2_inode *odip = GFS2_I(odir);
1241 struct gfs2_inode *ndip = GFS2_I(ndir);
1242 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
1243 struct gfs2_inode *nip = NULL;
1244 struct gfs2_sbd *sdp = GFS2_SB(odir);
1245 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
1246 struct gfs2_rgrpd *nrgd;
1247 unsigned int num_gh;
1248 int dir_rename = 0;
1249 int alloc_required = 0;
1250 unsigned int x;
1251 int error;
1252
1253 if (ndentry->d_inode) {
1254 nip = GFS2_I(ndentry->d_inode);
1255 if (ip == nip)
1256 return 0;
1257 }
1258
1259 error = gfs2_rindex_hold(sdp, &ri_gh);
1260 if (error)
1261 return error;
1262
1263 if (odip != ndip) {
1264 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1265 0, &r_gh);
1266 if (error)
1267 goto out;
1268
1269 if (S_ISDIR(ip->i_inode.i_mode)) {
1270 dir_rename = 1;
1271 /* don't move a dirctory into it's subdir */
1272 error = gfs2_ok_to_move(ip, ndip);
1273 if (error)
1274 goto out_gunlock_r;
1275 }
1276 }
1277
1278 num_gh = 1;
1279 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1280 if (odip != ndip) {
1281 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1282 num_gh++;
1283 }
1284 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1285 num_gh++;
1286
1287 if (nip) {
1288 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1289 num_gh++;
1290 /* grab the resource lock for unlink flag twiddling
1291 * this is the case of the target file already existing
1292 * so we unlink before doing the rename
1293 */
1294 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
1295 if (nrgd)
1296 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
1297 }
1298
1299 for (x = 0; x < num_gh; x++) {
1300 error = gfs2_glock_nq(ghs + x);
1301 if (error)
1302 goto out_gunlock;
1303 }
1304
1305 error = -ENOENT;
1306 if (ip->i_inode.i_nlink == 0)
1307 goto out_gunlock;
1308
1309 /* Check out the old directory */
1310
1311 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1312 if (error)
1313 goto out_gunlock;
1314
1315 /* Check out the new directory */
1316
1317 if (nip) {
1318 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1319 if (error)
1320 goto out_gunlock;
1321
1322 if (nip->i_inode.i_nlink == 0) {
1323 error = -EAGAIN;
1324 goto out_gunlock;
1325 }
1326
1327 if (S_ISDIR(nip->i_inode.i_mode)) {
1328 if (nip->i_entries < 2) {
1329 gfs2_consist_inode(nip);
1330 error = -EIO;
1331 goto out_gunlock;
1332 }
1333 if (nip->i_entries > 2) {
1334 error = -ENOTEMPTY;
1335 goto out_gunlock;
1336 }
1337 }
1338 } else {
1339 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
1340 if (error)
1341 goto out_gunlock;
1342
1343 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1344 switch (error) {
1345 case -ENOENT:
1346 error = 0;
1347 break;
1348 case 0:
1349 error = -EEXIST;
1350 default:
1351 goto out_gunlock;
1352 };
1353
1354 if (odip != ndip) {
1355 if (!ndip->i_inode.i_nlink) {
1356 error = -ENOENT;
1357 goto out_gunlock;
1358 }
1359 if (ndip->i_entries == (u32)-1) {
1360 error = -EFBIG;
1361 goto out_gunlock;
1362 }
1363 if (S_ISDIR(ip->i_inode.i_mode) &&
1364 ndip->i_inode.i_nlink == (u32)-1) {
1365 error = -EMLINK;
1366 goto out_gunlock;
1367 }
1368 }
1369 }
1370
1371 /* Check out the dir to be renamed */
1372
1373 if (dir_rename) {
1374 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
1375 if (error)
1376 goto out_gunlock;
1377 }
1378
1379 if (nip == NULL)
1380 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
1381 error = alloc_required;
1382 if (error < 0)
1383 goto out_gunlock;
1384 error = 0;
1385
1386 if (alloc_required) {
1387 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
1388 if (!al) {
1389 error = -ENOMEM;
1390 goto out_gunlock;
1391 }
1392
1393 error = gfs2_quota_lock_check(ndip);
1394 if (error)
1395 goto out_alloc;
1396
1397 al->al_requested = sdp->sd_max_dirres;
1398
1399 error = gfs2_inplace_reserve_ri(ndip);
1400 if (error)
1401 goto out_gunlock_q;
1402
1403 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
1404 gfs2_rg_blocks(al) +
1405 4 * RES_DINODE + 4 * RES_LEAF +
1406 RES_STATFS + RES_QUOTA + 4, 0);
1407 if (error)
1408 goto out_ipreserv;
1409 } else {
1410 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1411 5 * RES_LEAF + 4, 0);
1412 if (error)
1413 goto out_gunlock;
1414 }
1415
1416 /* Remove the target file, if it exists */
1417
1418 if (nip) {
1419 struct buffer_head *bh;
1420 error = gfs2_meta_inode_buffer(nip, &bh);
1421 if (error)
1422 goto out_end_trans;
1423 error = gfs2_unlink_inode(ndip, ndentry, bh);
1424 brelse(bh);
1425 }
1426
1427 if (dir_rename) {
1428 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1429 if (error)
1430 goto out_end_trans;
1431 } else {
1432 struct buffer_head *dibh;
1433 error = gfs2_meta_inode_buffer(ip, &dibh);
1434 if (error)
1435 goto out_end_trans;
1436 ip->i_inode.i_ctime = CURRENT_TIME;
1437 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1438 gfs2_dinode_out(ip, dibh->b_data);
1439 brelse(dibh);
1440 }
1441
1442 error = gfs2_dir_del(odip, odentry);
1443 if (error)
1444 goto out_end_trans;
1445
1446 error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
1447 if (error)
1448 goto out_end_trans;
1449
1450out_end_trans:
1451 gfs2_trans_end(sdp);
1452out_ipreserv:
1453 if (alloc_required)
1454 gfs2_inplace_release(ndip);
1455out_gunlock_q:
1456 if (alloc_required)
1457 gfs2_quota_unlock(ndip);
1458out_alloc:
1459 if (alloc_required)
1460 gfs2_alloc_put(ndip);
1461out_gunlock:
1462 while (x--) {
1463 gfs2_glock_dq(ghs + x);
1464 gfs2_holder_uninit(ghs + x);
1465 }
1466out_gunlock_r:
1467 if (r_gh.gh_gl)
1468 gfs2_glock_dq_uninit(&r_gh);
1469out:
1470 gfs2_glock_dq_uninit(&ri_gh);
1471 return error;
1472}
1473
1474/**
1475 * gfs2_follow_link - Follow a symbolic link
1476 * @dentry: The dentry of the link
1477 * @nd: Data that we pass to vfs_follow_link()
1478 *
1479 * This can handle symlinks of any size.
1480 *
1481 * Returns: 0 on success or error code
1482 */
1483
1484static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1485{
1486 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1487 struct gfs2_holder i_gh;
1488 struct buffer_head *dibh;
1489 unsigned int size;
1490 char *buf;
1491 int error;
1492
1493 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1494 error = gfs2_glock_nq(&i_gh);
1495 if (error) {
1496 gfs2_holder_uninit(&i_gh);
1497 nd_set_link(nd, ERR_PTR(error));
1498 return NULL;
1499 }
1500
1501 size = (unsigned int)i_size_read(&ip->i_inode);
1502 if (size == 0) {
1503 gfs2_consist_inode(ip);
1504 buf = ERR_PTR(-EIO);
1505 goto out;
1506 }
1507
1508 error = gfs2_meta_inode_buffer(ip, &dibh);
1509 if (error) {
1510 buf = ERR_PTR(error);
1511 goto out;
1512 }
1513
1514 buf = kzalloc(size + 1, GFP_NOFS);
1515 if (!buf)
1516 buf = ERR_PTR(-ENOMEM);
1517 else
1518 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1519 brelse(dibh);
1520out:
1521 gfs2_glock_dq_uninit(&i_gh);
1522 nd_set_link(nd, buf);
1523 return NULL;
1524}
1525
1526static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1527{
1528 char *s = nd_get_link(nd);
1529 if (!IS_ERR(s))
1530 kfree(s);
1531}
1532
1533/**
1534 * gfs2_permission -
1535 * @inode: The inode
1536 * @mask: The mask to be tested
1537 * @flags: Indicates whether this is an RCU path walk or not
1538 *
1539 * This may be called from the VFS directly, or from within GFS2 with the
1540 * inode locked, so we look to see if the glock is already locked and only
1541 * lock the glock if its not already been done.
1542 *
1543 * Returns: errno
1544 */
1545
1546int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
1547{
1548 struct gfs2_inode *ip;
1549 struct gfs2_holder i_gh;
1550 int error;
1551 int unlock = 0;
1552
1553
1554 ip = GFS2_I(inode);
1555 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1556 if (flags & IPERM_FLAG_RCU)
1557 return -ECHILD;
1558 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1005 if (error) 1559 if (error)
1006 return error; 1560 return error;
1561 unlock = 1;
1007 } 1562 }
1008 1563
1564 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1565 error = -EACCES;
1566 else
1567 error = generic_permission(inode, mask, flags, gfs2_check_acl);
1568 if (unlock)
1569 gfs2_glock_dq_uninit(&i_gh);
1570
1571 return error;
1572}
1573
1574static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1575{
1576 struct inode *inode = &ip->i_inode;
1577 struct buffer_head *dibh;
1578 int error;
1579
1580 error = gfs2_meta_inode_buffer(ip, &dibh);
1581 if (error)
1582 return error;
1583
1009 setattr_copy(inode, attr); 1584 setattr_copy(inode, attr);
1010 mark_inode_dirty(inode); 1585 mark_inode_dirty(inode);
1011
1012 gfs2_assert_warn(GFS2_SB(inode), !error);
1013 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 1586 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1014 gfs2_dinode_out(ip, dibh->b_data); 1587 gfs2_dinode_out(ip, dibh->b_data);
1015 brelse(dibh); 1588 brelse(dibh);
@@ -1021,8 +1594,6 @@ static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1021 * @ip: 1594 * @ip:
1022 * @attr: 1595 * @attr:
1023 * 1596 *
1024 * Called with a reference on the vnode.
1025 *
1026 * Returns: errno 1597 * Returns: errno
1027 */ 1598 */
1028 1599
@@ -1042,60 +1613,280 @@ int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1042 return error; 1613 return error;
1043} 1614}
1044 1615
1045void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf) 1616static int setattr_chown(struct inode *inode, struct iattr *attr)
1617{
1618 struct gfs2_inode *ip = GFS2_I(inode);
1619 struct gfs2_sbd *sdp = GFS2_SB(inode);
1620 u32 ouid, ogid, nuid, ngid;
1621 int error;
1622
1623 ouid = inode->i_uid;
1624 ogid = inode->i_gid;
1625 nuid = attr->ia_uid;
1626 ngid = attr->ia_gid;
1627
1628 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1629 ouid = nuid = NO_QUOTA_CHANGE;
1630 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1631 ogid = ngid = NO_QUOTA_CHANGE;
1632
1633 if (!gfs2_alloc_get(ip))
1634 return -ENOMEM;
1635
1636 error = gfs2_quota_lock(ip, nuid, ngid);
1637 if (error)
1638 goto out_alloc;
1639
1640 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1641 error = gfs2_quota_check(ip, nuid, ngid);
1642 if (error)
1643 goto out_gunlock_q;
1644 }
1645
1646 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1647 if (error)
1648 goto out_gunlock_q;
1649
1650 error = gfs2_setattr_simple(ip, attr);
1651 if (error)
1652 goto out_end_trans;
1653
1654 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1655 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1656 gfs2_quota_change(ip, -blocks, ouid, ogid);
1657 gfs2_quota_change(ip, blocks, nuid, ngid);
1658 }
1659
1660out_end_trans:
1661 gfs2_trans_end(sdp);
1662out_gunlock_q:
1663 gfs2_quota_unlock(ip);
1664out_alloc:
1665 gfs2_alloc_put(ip);
1666 return error;
1667}
1668
1669/**
1670 * gfs2_setattr - Change attributes on an inode
1671 * @dentry: The dentry which is changing
1672 * @attr: The structure describing the change
1673 *
1674 * The VFS layer wants to change one or more of an inodes attributes. Write
1675 * that change out to disk.
1676 *
1677 * Returns: errno
1678 */
1679
1680static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1681{
1682 struct inode *inode = dentry->d_inode;
1683 struct gfs2_inode *ip = GFS2_I(inode);
1684 struct gfs2_holder i_gh;
1685 int error;
1686
1687 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1688 if (error)
1689 return error;
1690
1691 error = -EPERM;
1692 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1693 goto out;
1694
1695 error = inode_change_ok(inode, attr);
1696 if (error)
1697 goto out;
1698
1699 if (attr->ia_valid & ATTR_SIZE)
1700 error = gfs2_setattr_size(inode, attr->ia_size);
1701 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1702 error = setattr_chown(inode, attr);
1703 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1704 error = gfs2_acl_chmod(ip, attr);
1705 else
1706 error = gfs2_setattr_simple(ip, attr);
1707
1708out:
1709 gfs2_glock_dq_uninit(&i_gh);
1710 if (!error)
1711 mark_inode_dirty(inode);
1712 return error;
1713}
1714
1715/**
1716 * gfs2_getattr - Read out an inode's attributes
1717 * @mnt: The vfsmount the inode is being accessed from
1718 * @dentry: The dentry to stat
1719 * @stat: The inode's stats
1720 *
1721 * This may be called from the VFS directly, or from within GFS2 with the
1722 * inode locked, so we look to see if the glock is already locked and only
1723 * lock the glock if its not already been done. Note that its the NFS
1724 * readdirplus operation which causes this to be called (from filldir)
1725 * with the glock already held.
1726 *
1727 * Returns: errno
1728 */
1729
1730static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1731 struct kstat *stat)
1732{
1733 struct inode *inode = dentry->d_inode;
1734 struct gfs2_inode *ip = GFS2_I(inode);
1735 struct gfs2_holder gh;
1736 int error;
1737 int unlock = 0;
1738
1739 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1740 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1741 if (error)
1742 return error;
1743 unlock = 1;
1744 }
1745
1746 generic_fillattr(inode, stat);
1747 if (unlock)
1748 gfs2_glock_dq_uninit(&gh);
1749
1750 return 0;
1751}
1752
1753static int gfs2_setxattr(struct dentry *dentry, const char *name,
1754 const void *data, size_t size, int flags)
1755{
1756 struct inode *inode = dentry->d_inode;
1757 struct gfs2_inode *ip = GFS2_I(inode);
1758 struct gfs2_holder gh;
1759 int ret;
1760
1761 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1762 ret = gfs2_glock_nq(&gh);
1763 if (ret == 0) {
1764 ret = generic_setxattr(dentry, name, data, size, flags);
1765 gfs2_glock_dq(&gh);
1766 }
1767 gfs2_holder_uninit(&gh);
1768 return ret;
1769}
1770
1771static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1772 void *data, size_t size)
1046{ 1773{
1047 struct gfs2_dinode *str = buf; 1774 struct inode *inode = dentry->d_inode;
1048 1775 struct gfs2_inode *ip = GFS2_I(inode);
1049 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 1776 struct gfs2_holder gh;
1050 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI); 1777 int ret;
1051 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI); 1778
1052 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr); 1779 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1053 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino); 1780 ret = gfs2_glock_nq(&gh);
1054 str->di_mode = cpu_to_be32(ip->i_inode.i_mode); 1781 if (ret == 0) {
1055 str->di_uid = cpu_to_be32(ip->i_inode.i_uid); 1782 ret = generic_getxattr(dentry, name, data, size);
1056 str->di_gid = cpu_to_be32(ip->i_inode.i_gid); 1783 gfs2_glock_dq(&gh);
1057 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink); 1784 }
1058 str->di_size = cpu_to_be64(ip->i_disksize); 1785 gfs2_holder_uninit(&gh);
1059 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode)); 1786 return ret;
1060 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1061 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1062 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1063
1064 str->di_goal_meta = cpu_to_be64(ip->i_goal);
1065 str->di_goal_data = cpu_to_be64(ip->i_goal);
1066 str->di_generation = cpu_to_be64(ip->i_generation);
1067
1068 str->di_flags = cpu_to_be32(ip->i_diskflags);
1069 str->di_height = cpu_to_be16(ip->i_height);
1070 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1071 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
1072 GFS2_FORMAT_DE : 0);
1073 str->di_depth = cpu_to_be16(ip->i_depth);
1074 str->di_entries = cpu_to_be32(ip->i_entries);
1075
1076 str->di_eattr = cpu_to_be64(ip->i_eattr);
1077 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1078 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1079 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
1080} 1787}
1081 1788
1082void gfs2_dinode_print(const struct gfs2_inode *ip) 1789static int gfs2_removexattr(struct dentry *dentry, const char *name)
1083{ 1790{
1084 printk(KERN_INFO " no_formal_ino = %llu\n", 1791 struct inode *inode = dentry->d_inode;
1085 (unsigned long long)ip->i_no_formal_ino); 1792 struct gfs2_inode *ip = GFS2_I(inode);
1086 printk(KERN_INFO " no_addr = %llu\n", 1793 struct gfs2_holder gh;
1087 (unsigned long long)ip->i_no_addr); 1794 int ret;
1088 printk(KERN_INFO " i_disksize = %llu\n", 1795
1089 (unsigned long long)ip->i_disksize); 1796 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1090 printk(KERN_INFO " blocks = %llu\n", 1797 ret = gfs2_glock_nq(&gh);
1091 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode)); 1798 if (ret == 0) {
1092 printk(KERN_INFO " i_goal = %llu\n", 1799 ret = generic_removexattr(dentry, name);
1093 (unsigned long long)ip->i_goal); 1800 gfs2_glock_dq(&gh);
1094 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags); 1801 }
1095 printk(KERN_INFO " i_height = %u\n", ip->i_height); 1802 gfs2_holder_uninit(&gh);
1096 printk(KERN_INFO " i_depth = %u\n", ip->i_depth); 1803 return ret;
1097 printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
1098 printk(KERN_INFO " i_eattr = %llu\n",
1099 (unsigned long long)ip->i_eattr);
1100} 1804}
1101 1805
1806static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1807 u64 start, u64 len)
1808{
1809 struct gfs2_inode *ip = GFS2_I(inode);
1810 struct gfs2_holder gh;
1811 int ret;
1812
1813 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1814 if (ret)
1815 return ret;
1816
1817 mutex_lock(&inode->i_mutex);
1818
1819 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1820 if (ret)
1821 goto out;
1822
1823 if (gfs2_is_stuffed(ip)) {
1824 u64 phys = ip->i_no_addr << inode->i_blkbits;
1825 u64 size = i_size_read(inode);
1826 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1827 FIEMAP_EXTENT_DATA_INLINE;
1828 phys += sizeof(struct gfs2_dinode);
1829 phys += start;
1830 if (start + len > size)
1831 len = size - start;
1832 if (start < size)
1833 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1834 len, flags);
1835 if (ret == 1)
1836 ret = 0;
1837 } else {
1838 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1839 gfs2_block_map);
1840 }
1841
1842 gfs2_glock_dq_uninit(&gh);
1843out:
1844 mutex_unlock(&inode->i_mutex);
1845 return ret;
1846}
1847
1848const struct inode_operations gfs2_file_iops = {
1849 .permission = gfs2_permission,
1850 .setattr = gfs2_setattr,
1851 .getattr = gfs2_getattr,
1852 .setxattr = gfs2_setxattr,
1853 .getxattr = gfs2_getxattr,
1854 .listxattr = gfs2_listxattr,
1855 .removexattr = gfs2_removexattr,
1856 .fiemap = gfs2_fiemap,
1857};
1858
1859const struct inode_operations gfs2_dir_iops = {
1860 .create = gfs2_create,
1861 .lookup = gfs2_lookup,
1862 .link = gfs2_link,
1863 .unlink = gfs2_unlink,
1864 .symlink = gfs2_symlink,
1865 .mkdir = gfs2_mkdir,
1866 .rmdir = gfs2_unlink,
1867 .mknod = gfs2_mknod,
1868 .rename = gfs2_rename,
1869 .permission = gfs2_permission,
1870 .setattr = gfs2_setattr,
1871 .getattr = gfs2_getattr,
1872 .setxattr = gfs2_setxattr,
1873 .getxattr = gfs2_getxattr,
1874 .listxattr = gfs2_listxattr,
1875 .removexattr = gfs2_removexattr,
1876 .fiemap = gfs2_fiemap,
1877};
1878
1879const struct inode_operations gfs2_symlink_iops = {
1880 .readlink = generic_readlink,
1881 .follow_link = gfs2_follow_link,
1882 .put_link = gfs2_put_link,
1883 .permission = gfs2_permission,
1884 .setattr = gfs2_setattr,
1885 .getattr = gfs2_getattr,
1886 .setxattr = gfs2_setxattr,
1887 .getxattr = gfs2_getxattr,
1888 .listxattr = gfs2_listxattr,
1889 .removexattr = gfs2_removexattr,
1890 .fiemap = gfs2_fiemap,
1891};
1892
diff --git a/fs/gfs2/inode.h b/fs/gfs2/inode.h
index 300ada3f21de..31606076f701 100644
--- a/fs/gfs2/inode.h
+++ b/fs/gfs2/inode.h
@@ -19,6 +19,8 @@ extern int gfs2_releasepage(struct page *page, gfp_t gfp_mask);
19extern int gfs2_internal_read(struct gfs2_inode *ip, 19extern int gfs2_internal_read(struct gfs2_inode *ip,
20 struct file_ra_state *ra_state, 20 struct file_ra_state *ra_state,
21 char *buf, loff_t *pos, unsigned size); 21 char *buf, loff_t *pos, unsigned size);
22extern void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
23 unsigned int from, unsigned int to);
22extern void gfs2_set_aops(struct inode *inode); 24extern void gfs2_set_aops(struct inode *inode);
23 25
24static inline int gfs2_is_stuffed(const struct gfs2_inode *ip) 26static inline int gfs2_is_stuffed(const struct gfs2_inode *ip)
@@ -80,27 +82,36 @@ static inline void gfs2_inum_out(const struct gfs2_inode *ip,
80 dent->de_inum.no_addr = cpu_to_be64(ip->i_no_addr); 82 dent->de_inum.no_addr = cpu_to_be64(ip->i_no_addr);
81} 83}
82 84
85static inline int gfs2_check_internal_file_size(struct inode *inode,
86 u64 minsize, u64 maxsize)
87{
88 u64 size = i_size_read(inode);
89 if (size < minsize || size > maxsize)
90 goto err;
91 if (size & ((1 << inode->i_blkbits) - 1))
92 goto err;
93 return 0;
94err:
95 gfs2_consist_inode(GFS2_I(inode));
96 return -EIO;
97}
83 98
84extern void gfs2_set_iop(struct inode *inode);
85extern struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned type, 99extern struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned type,
86 u64 no_addr, u64 no_formal_ino); 100 u64 no_addr, u64 no_formal_ino,
87extern void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr); 101 int non_block);
88extern struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr); 102extern struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
103 u64 *no_formal_ino,
104 unsigned int blktype);
105extern struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int nonblock);
89 106
90extern int gfs2_inode_refresh(struct gfs2_inode *ip); 107extern int gfs2_inode_refresh(struct gfs2_inode *ip);
91 108
92extern int gfs2_dinode_dealloc(struct gfs2_inode *inode);
93extern int gfs2_change_nlink(struct gfs2_inode *ip, int diff);
94extern struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, 109extern struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
95 int is_root); 110 int is_root);
96extern struct inode *gfs2_createi(struct gfs2_holder *ghs, 111extern int gfs2_permission(struct inode *inode, int mask, unsigned int flags);
97 const struct qstr *name,
98 unsigned int mode, dev_t dev);
99extern int gfs2_permission(struct inode *inode, int mask);
100extern int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr); 112extern int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr);
101extern struct inode *gfs2_lookup_simple(struct inode *dip, const char *name); 113extern struct inode *gfs2_lookup_simple(struct inode *dip, const char *name);
102extern void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf); 114extern void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf);
103extern void gfs2_dinode_print(const struct gfs2_inode *ip);
104 115
105extern const struct inode_operations gfs2_file_iops; 116extern const struct inode_operations gfs2_file_iops;
106extern const struct inode_operations gfs2_dir_iops; 117extern const struct inode_operations gfs2_dir_iops;
diff --git a/fs/gfs2/lock_dlm.c b/fs/gfs2/lock_dlm.c
index 0e0470ed34c2..98c80d8c2a62 100644
--- a/fs/gfs2/lock_dlm.c
+++ b/fs/gfs2/lock_dlm.c
@@ -22,7 +22,6 @@ static void gdlm_ast(void *arg)
22{ 22{
23 struct gfs2_glock *gl = arg; 23 struct gfs2_glock *gl = arg;
24 unsigned ret = gl->gl_state; 24 unsigned ret = gl->gl_state;
25 struct gfs2_sbd *sdp = gl->gl_sbd;
26 25
27 BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED); 26 BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED);
28 27
@@ -31,20 +30,15 @@ static void gdlm_ast(void *arg)
31 30
32 switch (gl->gl_lksb.sb_status) { 31 switch (gl->gl_lksb.sb_status) {
33 case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */ 32 case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */
34 if (gl->gl_ops->go_flags & GLOF_ASPACE) 33 gfs2_glock_free(gl);
35 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
36 else
37 kmem_cache_free(gfs2_glock_cachep, gl);
38 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
39 wake_up(&sdp->sd_glock_wait);
40 return; 34 return;
41 case -DLM_ECANCEL: /* Cancel while getting lock */ 35 case -DLM_ECANCEL: /* Cancel while getting lock */
42 ret |= LM_OUT_CANCELED; 36 ret |= LM_OUT_CANCELED;
43 goto out; 37 goto out;
44 case -EAGAIN: /* Try lock fails */ 38 case -EAGAIN: /* Try lock fails */
39 case -EDEADLK: /* Deadlock detected */
45 goto out; 40 goto out;
46 case -EINVAL: /* Invalid */ 41 case -ETIMEDOUT: /* Canceled due to timeout */
47 case -ENOMEM: /* Out of memory */
48 ret |= LM_OUT_ERROR; 42 ret |= LM_OUT_ERROR;
49 goto out; 43 goto out;
50 case 0: /* Success */ 44 case 0: /* Success */
@@ -146,15 +140,13 @@ static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
146 return lkf; 140 return lkf;
147} 141}
148 142
149static unsigned int gdlm_lock(struct gfs2_glock *gl, 143static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
150 unsigned int req_state, unsigned int flags) 144 unsigned int flags)
151{ 145{
152 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct; 146 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
153 int error;
154 int req; 147 int req;
155 u32 lkf; 148 u32 lkf;
156 149
157 gl->gl_req = req_state;
158 req = make_mode(req_state); 150 req = make_mode(req_state);
159 lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req); 151 lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
160 152
@@ -162,25 +154,18 @@ static unsigned int gdlm_lock(struct gfs2_glock *gl,
162 * Submit the actual lock request. 154 * Submit the actual lock request.
163 */ 155 */
164 156
165 error = dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname, 157 return dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname,
166 GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast); 158 GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
167 if (error == -EAGAIN)
168 return 0;
169 if (error)
170 return LM_OUT_ERROR;
171 return LM_OUT_ASYNC;
172} 159}
173 160
174static void gdlm_put_lock(struct kmem_cache *cachep, struct gfs2_glock *gl) 161static void gdlm_put_lock(struct gfs2_glock *gl)
175{ 162{
176 struct gfs2_sbd *sdp = gl->gl_sbd; 163 struct gfs2_sbd *sdp = gl->gl_sbd;
177 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 164 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
178 int error; 165 int error;
179 166
180 if (gl->gl_lksb.sb_lkid == 0) { 167 if (gl->gl_lksb.sb_lkid == 0) {
181 kmem_cache_free(cachep, gl); 168 gfs2_glock_free(gl);
182 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
183 wake_up(&sdp->sd_glock_wait);
184 return; 169 return;
185 } 170 }
186 171
diff --git a/fs/gfs2/log.c b/fs/gfs2/log.c
index ac750bd31a6f..85c62923ee29 100644
--- a/fs/gfs2/log.c
+++ b/fs/gfs2/log.c
@@ -18,6 +18,7 @@
18#include <linux/kthread.h> 18#include <linux/kthread.h>
19#include <linux/freezer.h> 19#include <linux/freezer.h>
20#include <linux/bio.h> 20#include <linux/bio.h>
21#include <linux/writeback.h>
21 22
22#include "gfs2.h" 23#include "gfs2.h"
23#include "incore.h" 24#include "incore.h"
@@ -67,7 +68,7 @@ unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
67 * @mapping: The associated mapping (maybe NULL) 68 * @mapping: The associated mapping (maybe NULL)
68 * @bd: The gfs2_bufdata to remove 69 * @bd: The gfs2_bufdata to remove
69 * 70 *
70 * The log lock _must_ be held when calling this function 71 * The ail lock _must_ be held when calling this function
71 * 72 *
72 */ 73 */
73 74
@@ -83,55 +84,97 @@ void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
83/** 84/**
84 * gfs2_ail1_start_one - Start I/O on a part of the AIL 85 * gfs2_ail1_start_one - Start I/O on a part of the AIL
85 * @sdp: the filesystem 86 * @sdp: the filesystem
86 * @tr: the part of the AIL 87 * @wbc: The writeback control structure
88 * @ai: The ail structure
87 * 89 *
88 */ 90 */
89 91
90static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) 92static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
91__releases(&sdp->sd_log_lock) 93 struct writeback_control *wbc,
92__acquires(&sdp->sd_log_lock) 94 struct gfs2_ail *ai)
95__releases(&sdp->sd_ail_lock)
96__acquires(&sdp->sd_ail_lock)
93{ 97{
98 struct gfs2_glock *gl = NULL;
99 struct address_space *mapping;
94 struct gfs2_bufdata *bd, *s; 100 struct gfs2_bufdata *bd, *s;
95 struct buffer_head *bh; 101 struct buffer_head *bh;
96 int retry;
97 102
98 do { 103 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, bd_ail_st_list) {
99 retry = 0; 104 bh = bd->bd_bh;
100 105
101 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, 106 gfs2_assert(sdp, bd->bd_ail == ai);
102 bd_ail_st_list) {
103 bh = bd->bd_bh;
104 107
105 gfs2_assert(sdp, bd->bd_ail == ai); 108 if (!buffer_busy(bh)) {
109 if (!buffer_uptodate(bh))
110 gfs2_io_error_bh(sdp, bh);
111 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
112 continue;
113 }
106 114
107 if (!buffer_busy(bh)) { 115 if (!buffer_dirty(bh))
108 if (!buffer_uptodate(bh)) 116 continue;
109 gfs2_io_error_bh(sdp, bh); 117 if (gl == bd->bd_gl)
110 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); 118 continue;
111 continue; 119 gl = bd->bd_gl;
112 } 120 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
121 mapping = bh->b_page->mapping;
122 if (!mapping)
123 continue;
124 spin_unlock(&sdp->sd_ail_lock);
125 generic_writepages(mapping, wbc);
126 spin_lock(&sdp->sd_ail_lock);
127 if (wbc->nr_to_write <= 0)
128 break;
129 return 1;
130 }
113 131
114 if (!buffer_dirty(bh)) 132 return 0;
115 continue; 133}
116 134
117 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
118 135
119 get_bh(bh); 136/**
120 gfs2_log_unlock(sdp); 137 * gfs2_ail1_flush - start writeback of some ail1 entries
121 lock_buffer(bh); 138 * @sdp: The super block
122 if (test_clear_buffer_dirty(bh)) { 139 * @wbc: The writeback control structure
123 bh->b_end_io = end_buffer_write_sync; 140 *
124 submit_bh(WRITE_SYNC_PLUG, bh); 141 * Writes back some ail1 entries, according to the limits in the
125 } else { 142 * writeback control structure
126 unlock_buffer(bh); 143 */
127 brelse(bh); 144
128 } 145void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
129 gfs2_log_lock(sdp); 146{
147 struct list_head *head = &sdp->sd_ail1_list;
148 struct gfs2_ail *ai;
130 149
131 retry = 1; 150 trace_gfs2_ail_flush(sdp, wbc, 1);
151 spin_lock(&sdp->sd_ail_lock);
152restart:
153 list_for_each_entry_reverse(ai, head, ai_list) {
154 if (wbc->nr_to_write <= 0)
132 break; 155 break;
133 } 156 if (gfs2_ail1_start_one(sdp, wbc, ai))
134 } while (retry); 157 goto restart;
158 }
159 spin_unlock(&sdp->sd_ail_lock);
160 trace_gfs2_ail_flush(sdp, wbc, 0);
161}
162
163/**
164 * gfs2_ail1_start - start writeback of all ail1 entries
165 * @sdp: The superblock
166 */
167
168static void gfs2_ail1_start(struct gfs2_sbd *sdp)
169{
170 struct writeback_control wbc = {
171 .sync_mode = WB_SYNC_NONE,
172 .nr_to_write = LONG_MAX,
173 .range_start = 0,
174 .range_end = LLONG_MAX,
175 };
176
177 return gfs2_ail1_flush(sdp, &wbc);
135} 178}
136 179
137/** 180/**
@@ -141,7 +184,7 @@ __acquires(&sdp->sd_log_lock)
141 * 184 *
142 */ 185 */
143 186
144static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags) 187static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
145{ 188{
146 struct gfs2_bufdata *bd, *s; 189 struct gfs2_bufdata *bd, *s;
147 struct buffer_head *bh; 190 struct buffer_head *bh;
@@ -149,76 +192,63 @@ static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int fl
149 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, 192 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
150 bd_ail_st_list) { 193 bd_ail_st_list) {
151 bh = bd->bd_bh; 194 bh = bd->bd_bh;
152
153 gfs2_assert(sdp, bd->bd_ail == ai); 195 gfs2_assert(sdp, bd->bd_ail == ai);
154 196 if (buffer_busy(bh))
155 if (buffer_busy(bh)) { 197 continue;
156 if (flags & DIO_ALL)
157 continue;
158 else
159 break;
160 }
161
162 if (!buffer_uptodate(bh)) 198 if (!buffer_uptodate(bh))
163 gfs2_io_error_bh(sdp, bh); 199 gfs2_io_error_bh(sdp, bh);
164
165 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); 200 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
166 } 201 }
167 202
168 return list_empty(&ai->ai_ail1_list);
169} 203}
170 204
171static void gfs2_ail1_start(struct gfs2_sbd *sdp) 205/**
172{ 206 * gfs2_ail1_empty - Try to empty the ail1 lists
173 struct list_head *head; 207 * @sdp: The superblock
174 u64 sync_gen; 208 *
175 struct gfs2_ail *ai; 209 * Tries to empty the ail1 lists, starting with the oldest first
176 int done = 0; 210 */
177
178 gfs2_log_lock(sdp);
179 head = &sdp->sd_ail1_list;
180 if (list_empty(head)) {
181 gfs2_log_unlock(sdp);
182 return;
183 }
184 sync_gen = sdp->sd_ail_sync_gen++;
185
186 while(!done) {
187 done = 1;
188 list_for_each_entry_reverse(ai, head, ai_list) {
189 if (ai->ai_sync_gen >= sync_gen)
190 continue;
191 ai->ai_sync_gen = sync_gen;
192 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
193 done = 0;
194 break;
195 }
196 }
197
198 gfs2_log_unlock(sdp);
199}
200 211
201static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags) 212static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
202{ 213{
203 struct gfs2_ail *ai, *s; 214 struct gfs2_ail *ai, *s;
204 int ret; 215 int ret;
205 216
206 gfs2_log_lock(sdp); 217 spin_lock(&sdp->sd_ail_lock);
207
208 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { 218 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
209 if (gfs2_ail1_empty_one(sdp, ai, flags)) 219 gfs2_ail1_empty_one(sdp, ai);
220 if (list_empty(&ai->ai_ail1_list))
210 list_move(&ai->ai_list, &sdp->sd_ail2_list); 221 list_move(&ai->ai_list, &sdp->sd_ail2_list);
211 else if (!(flags & DIO_ALL)) 222 else
212 break; 223 break;
213 } 224 }
214
215 ret = list_empty(&sdp->sd_ail1_list); 225 ret = list_empty(&sdp->sd_ail1_list);
216 226 spin_unlock(&sdp->sd_ail_lock);
217 gfs2_log_unlock(sdp);
218 227
219 return ret; 228 return ret;
220} 229}
221 230
231static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
232{
233 struct gfs2_ail *ai;
234 struct gfs2_bufdata *bd;
235 struct buffer_head *bh;
236
237 spin_lock(&sdp->sd_ail_lock);
238 list_for_each_entry_reverse(ai, &sdp->sd_ail1_list, ai_list) {
239 list_for_each_entry(bd, &ai->ai_ail1_list, bd_ail_st_list) {
240 bh = bd->bd_bh;
241 if (!buffer_locked(bh))
242 continue;
243 get_bh(bh);
244 spin_unlock(&sdp->sd_ail_lock);
245 wait_on_buffer(bh);
246 brelse(bh);
247 return;
248 }
249 }
250 spin_unlock(&sdp->sd_ail_lock);
251}
222 252
223/** 253/**
224 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced 254 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
@@ -247,7 +277,7 @@ static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
247 int wrap = (new_tail < old_tail); 277 int wrap = (new_tail < old_tail);
248 int a, b, rm; 278 int a, b, rm;
249 279
250 gfs2_log_lock(sdp); 280 spin_lock(&sdp->sd_ail_lock);
251 281
252 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { 282 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
253 a = (old_tail <= ai->ai_first); 283 a = (old_tail <= ai->ai_first);
@@ -263,7 +293,7 @@ static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
263 kfree(ai); 293 kfree(ai);
264 } 294 }
265 295
266 gfs2_log_unlock(sdp); 296 spin_unlock(&sdp->sd_ail_lock);
267} 297}
268 298
269/** 299/**
@@ -421,7 +451,7 @@ static unsigned int current_tail(struct gfs2_sbd *sdp)
421 struct gfs2_ail *ai; 451 struct gfs2_ail *ai;
422 unsigned int tail; 452 unsigned int tail;
423 453
424 gfs2_log_lock(sdp); 454 spin_lock(&sdp->sd_ail_lock);
425 455
426 if (list_empty(&sdp->sd_ail1_list)) { 456 if (list_empty(&sdp->sd_ail1_list)) {
427 tail = sdp->sd_log_head; 457 tail = sdp->sd_log_head;
@@ -430,7 +460,7 @@ static unsigned int current_tail(struct gfs2_sbd *sdp)
430 tail = ai->ai_first; 460 tail = ai->ai_first;
431 } 461 }
432 462
433 gfs2_log_unlock(sdp); 463 spin_unlock(&sdp->sd_ail_lock);
434 464
435 return tail; 465 return tail;
436} 466}
@@ -574,7 +604,7 @@ static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
574 set_buffer_uptodate(bh); 604 set_buffer_uptodate(bh);
575 clear_buffer_dirty(bh); 605 clear_buffer_dirty(bh);
576 606
577 gfs2_ail1_empty(sdp, 0); 607 gfs2_ail1_empty(sdp);
578 tail = current_tail(sdp); 608 tail = current_tail(sdp);
579 609
580 lh = (struct gfs2_log_header *)bh->b_data; 610 lh = (struct gfs2_log_header *)bh->b_data;
@@ -592,22 +622,13 @@ static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
592 lh->lh_hash = cpu_to_be32(hash); 622 lh->lh_hash = cpu_to_be32(hash);
593 623
594 bh->b_end_io = end_buffer_write_sync; 624 bh->b_end_io = end_buffer_write_sync;
595 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
596 goto skip_barrier;
597 get_bh(bh); 625 get_bh(bh);
598 submit_bh(WRITE_BARRIER | REQ_META, bh); 626 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
599 wait_on_buffer(bh);
600 if (buffer_eopnotsupp(bh)) {
601 clear_buffer_eopnotsupp(bh);
602 set_buffer_uptodate(bh);
603 fs_info(sdp, "barrier sync failed - disabling barriers\n");
604 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
605 lock_buffer(bh);
606skip_barrier:
607 get_bh(bh);
608 submit_bh(WRITE_SYNC | REQ_META, bh); 627 submit_bh(WRITE_SYNC | REQ_META, bh);
609 wait_on_buffer(bh); 628 else
610 } 629 submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
630 wait_on_buffer(bh);
631
611 if (!buffer_uptodate(bh)) 632 if (!buffer_uptodate(bh))
612 gfs2_io_error_bh(sdp, bh); 633 gfs2_io_error_bh(sdp, bh);
613 brelse(bh); 634 brelse(bh);
@@ -656,7 +677,7 @@ static void gfs2_ordered_write(struct gfs2_sbd *sdp)
656 lock_buffer(bh); 677 lock_buffer(bh);
657 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) { 678 if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
658 bh->b_end_io = end_buffer_write_sync; 679 bh->b_end_io = end_buffer_write_sync;
659 submit_bh(WRITE_SYNC_PLUG, bh); 680 submit_bh(WRITE_SYNC, bh);
660 } else { 681 } else {
661 unlock_buffer(bh); 682 unlock_buffer(bh);
662 brelse(bh); 683 brelse(bh);
@@ -752,10 +773,12 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
752 sdp->sd_log_commited_databuf = 0; 773 sdp->sd_log_commited_databuf = 0;
753 sdp->sd_log_commited_revoke = 0; 774 sdp->sd_log_commited_revoke = 0;
754 775
776 spin_lock(&sdp->sd_ail_lock);
755 if (!list_empty(&ai->ai_ail1_list)) { 777 if (!list_empty(&ai->ai_ail1_list)) {
756 list_add(&ai->ai_list, &sdp->sd_ail1_list); 778 list_add(&ai->ai_list, &sdp->sd_ail1_list);
757 ai = NULL; 779 ai = NULL;
758 } 780 }
781 spin_unlock(&sdp->sd_ail_lock);
759 gfs2_log_unlock(sdp); 782 gfs2_log_unlock(sdp);
760 trace_gfs2_log_flush(sdp, 0); 783 trace_gfs2_log_flush(sdp, 0);
761 up_write(&sdp->sd_log_flush_lock); 784 up_write(&sdp->sd_log_flush_lock);
@@ -876,10 +899,11 @@ void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
876 gfs2_log_flush(sdp, NULL); 899 gfs2_log_flush(sdp, NULL);
877 for (;;) { 900 for (;;) {
878 gfs2_ail1_start(sdp); 901 gfs2_ail1_start(sdp);
879 if (gfs2_ail1_empty(sdp, DIO_ALL)) 902 gfs2_ail1_wait(sdp);
903 if (gfs2_ail1_empty(sdp))
880 break; 904 break;
881 msleep(10);
882 } 905 }
906 gfs2_log_flush(sdp, NULL);
883} 907}
884 908
885static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp) 909static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
@@ -912,20 +936,20 @@ int gfs2_logd(void *data)
912 936
913 preflush = atomic_read(&sdp->sd_log_pinned); 937 preflush = atomic_read(&sdp->sd_log_pinned);
914 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) { 938 if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
915 gfs2_ail1_empty(sdp, DIO_ALL); 939 gfs2_ail1_empty(sdp);
916 gfs2_log_flush(sdp, NULL); 940 gfs2_log_flush(sdp, NULL);
917 gfs2_ail1_empty(sdp, DIO_ALL);
918 } 941 }
919 942
920 if (gfs2_ail_flush_reqd(sdp)) { 943 if (gfs2_ail_flush_reqd(sdp)) {
921 gfs2_ail1_start(sdp); 944 gfs2_ail1_start(sdp);
922 io_schedule(); 945 gfs2_ail1_wait(sdp);
923 gfs2_ail1_empty(sdp, 0); 946 gfs2_ail1_empty(sdp);
924 gfs2_log_flush(sdp, NULL); 947 gfs2_log_flush(sdp, NULL);
925 gfs2_ail1_empty(sdp, DIO_ALL);
926 } 948 }
927 949
928 wake_up(&sdp->sd_log_waitq); 950 if (!gfs2_ail_flush_reqd(sdp))
951 wake_up(&sdp->sd_log_waitq);
952
929 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ; 953 t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
930 if (freezing(current)) 954 if (freezing(current))
931 refrigerator(); 955 refrigerator();
diff --git a/fs/gfs2/log.h b/fs/gfs2/log.h
index 0d007f920234..ab0621698b73 100644
--- a/fs/gfs2/log.h
+++ b/fs/gfs2/log.h
@@ -12,6 +12,7 @@
12 12
13#include <linux/list.h> 13#include <linux/list.h>
14#include <linux/spinlock.h> 14#include <linux/spinlock.h>
15#include <linux/writeback.h>
15#include "incore.h" 16#include "incore.h"
16 17
17/** 18/**
@@ -59,6 +60,7 @@ extern struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
59extern void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl); 60extern void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl);
60extern void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *trans); 61extern void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *trans);
61extern void gfs2_remove_from_ail(struct gfs2_bufdata *bd); 62extern void gfs2_remove_from_ail(struct gfs2_bufdata *bd);
63extern void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc);
62 64
63extern void gfs2_log_shutdown(struct gfs2_sbd *sdp); 65extern void gfs2_log_shutdown(struct gfs2_sbd *sdp);
64extern void gfs2_meta_syncfs(struct gfs2_sbd *sdp); 66extern void gfs2_meta_syncfs(struct gfs2_sbd *sdp);
diff --git a/fs/gfs2/lops.c b/fs/gfs2/lops.c
index bf33f822058d..05bbb124699f 100644
--- a/fs/gfs2/lops.c
+++ b/fs/gfs2/lops.c
@@ -40,7 +40,7 @@ static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
40{ 40{
41 struct gfs2_bufdata *bd; 41 struct gfs2_bufdata *bd;
42 42
43 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); 43 BUG_ON(!current->journal_info);
44 44
45 clear_buffer_dirty(bh); 45 clear_buffer_dirty(bh);
46 if (test_set_buffer_pinned(bh)) 46 if (test_set_buffer_pinned(bh))
@@ -51,8 +51,10 @@ static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
51 /* If this buffer is in the AIL and it has already been written 51 /* If this buffer is in the AIL and it has already been written
52 * to in-place disk block, remove it from the AIL. 52 * to in-place disk block, remove it from the AIL.
53 */ 53 */
54 spin_lock(&sdp->sd_ail_lock);
54 if (bd->bd_ail) 55 if (bd->bd_ail)
55 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); 56 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
57 spin_unlock(&sdp->sd_ail_lock);
56 get_bh(bh); 58 get_bh(bh);
57 atomic_inc(&sdp->sd_log_pinned); 59 atomic_inc(&sdp->sd_log_pinned);
58 trace_gfs2_pin(bd, 1); 60 trace_gfs2_pin(bd, 1);
@@ -63,6 +65,7 @@ static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
63 * @sdp: the filesystem the buffer belongs to 65 * @sdp: the filesystem the buffer belongs to
64 * @bh: The buffer to unpin 66 * @bh: The buffer to unpin
65 * @ai: 67 * @ai:
68 * @flags: The inode dirty flags
66 * 69 *
67 */ 70 */
68 71
@@ -71,16 +74,14 @@ static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
71{ 74{
72 struct gfs2_bufdata *bd = bh->b_private; 75 struct gfs2_bufdata *bd = bh->b_private;
73 76
74 gfs2_assert_withdraw(sdp, buffer_uptodate(bh)); 77 BUG_ON(!buffer_uptodate(bh));
75 78 BUG_ON(!buffer_pinned(bh));
76 if (!buffer_pinned(bh))
77 gfs2_assert_withdraw(sdp, 0);
78 79
79 lock_buffer(bh); 80 lock_buffer(bh);
80 mark_buffer_dirty(bh); 81 mark_buffer_dirty(bh);
81 clear_buffer_pinned(bh); 82 clear_buffer_pinned(bh);
82 83
83 gfs2_log_lock(sdp); 84 spin_lock(&sdp->sd_ail_lock);
84 if (bd->bd_ail) { 85 if (bd->bd_ail) {
85 list_del(&bd->bd_ail_st_list); 86 list_del(&bd->bd_ail_st_list);
86 brelse(bh); 87 brelse(bh);
@@ -91,9 +92,10 @@ static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
91 } 92 }
92 bd->bd_ail = ai; 93 bd->bd_ail = ai;
93 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); 94 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
95 spin_unlock(&sdp->sd_ail_lock);
96
94 clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); 97 clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
95 trace_gfs2_pin(bd, 0); 98 trace_gfs2_pin(bd, 0);
96 gfs2_log_unlock(sdp);
97 unlock_buffer(bh); 99 unlock_buffer(bh);
98 atomic_dec(&sdp->sd_log_pinned); 100 atomic_dec(&sdp->sd_log_pinned);
99} 101}
@@ -200,7 +202,7 @@ static void buf_lo_before_commit(struct gfs2_sbd *sdp)
200 } 202 }
201 203
202 gfs2_log_unlock(sdp); 204 gfs2_log_unlock(sdp);
203 submit_bh(WRITE_SYNC_PLUG, bh); 205 submit_bh(WRITE_SYNC, bh);
204 gfs2_log_lock(sdp); 206 gfs2_log_lock(sdp);
205 207
206 n = 0; 208 n = 0;
@@ -210,7 +212,7 @@ static void buf_lo_before_commit(struct gfs2_sbd *sdp)
210 gfs2_log_unlock(sdp); 212 gfs2_log_unlock(sdp);
211 lock_buffer(bd2->bd_bh); 213 lock_buffer(bd2->bd_bh);
212 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh); 214 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
213 submit_bh(WRITE_SYNC_PLUG, bh); 215 submit_bh(WRITE_SYNC, bh);
214 gfs2_log_lock(sdp); 216 gfs2_log_lock(sdp);
215 if (++n >= num) 217 if (++n >= num)
216 break; 218 break;
@@ -318,12 +320,16 @@ static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
318 320
319static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) 321static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
320{ 322{
323 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
324 struct gfs2_glock *gl = bd->bd_gl;
321 struct gfs2_trans *tr; 325 struct gfs2_trans *tr;
322 326
323 tr = current->journal_info; 327 tr = current->journal_info;
324 tr->tr_touched = 1; 328 tr->tr_touched = 1;
325 tr->tr_num_revoke++; 329 tr->tr_num_revoke++;
326 sdp->sd_log_num_revoke++; 330 sdp->sd_log_num_revoke++;
331 atomic_inc(&gl->gl_revokes);
332 set_bit(GLF_LFLUSH, &gl->gl_flags);
327 list_add(&le->le_list, &sdp->sd_log_le_revoke); 333 list_add(&le->le_list, &sdp->sd_log_le_revoke);
328} 334}
329 335
@@ -346,13 +352,11 @@ static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
346 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke); 352 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
347 offset = sizeof(struct gfs2_log_descriptor); 353 offset = sizeof(struct gfs2_log_descriptor);
348 354
349 while (!list_empty(head)) { 355 list_for_each_entry(bd, head, bd_le.le_list) {
350 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
351 list_del_init(&bd->bd_le.le_list);
352 sdp->sd_log_num_revoke--; 356 sdp->sd_log_num_revoke--;
353 357
354 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) { 358 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
355 submit_bh(WRITE_SYNC_PLUG, bh); 359 submit_bh(WRITE_SYNC, bh);
356 360
357 bh = gfs2_log_get_buf(sdp); 361 bh = gfs2_log_get_buf(sdp);
358 mh = (struct gfs2_meta_header *)bh->b_data; 362 mh = (struct gfs2_meta_header *)bh->b_data;
@@ -363,13 +367,27 @@ static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
363 } 367 }
364 368
365 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno); 369 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno);
366 kmem_cache_free(gfs2_bufdata_cachep, bd);
367
368 offset += sizeof(u64); 370 offset += sizeof(u64);
369 } 371 }
370 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); 372 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
371 373
372 submit_bh(WRITE_SYNC_PLUG, bh); 374 submit_bh(WRITE_SYNC, bh);
375}
376
377static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
378{
379 struct list_head *head = &sdp->sd_log_le_revoke;
380 struct gfs2_bufdata *bd;
381 struct gfs2_glock *gl;
382
383 while (!list_empty(head)) {
384 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
385 list_del_init(&bd->bd_le.le_list);
386 gl = bd->bd_gl;
387 atomic_dec(&gl->gl_revokes);
388 clear_bit(GLF_LFLUSH, &gl->gl_flags);
389 kmem_cache_free(gfs2_bufdata_cachep, bd);
390 }
373} 391}
374 392
375static void revoke_lo_before_scan(struct gfs2_jdesc *jd, 393static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
@@ -571,7 +589,7 @@ static void gfs2_write_blocks(struct gfs2_sbd *sdp, struct buffer_head *bh,
571 ptr = bh_log_ptr(bh); 589 ptr = bh_log_ptr(bh);
572 590
573 get_bh(bh); 591 get_bh(bh);
574 submit_bh(WRITE_SYNC_PLUG, bh); 592 submit_bh(WRITE_SYNC, bh);
575 gfs2_log_lock(sdp); 593 gfs2_log_lock(sdp);
576 while(!list_empty(list)) { 594 while(!list_empty(list)) {
577 bd = list_entry(list->next, struct gfs2_bufdata, bd_le.le_list); 595 bd = list_entry(list->next, struct gfs2_bufdata, bd_le.le_list);
@@ -597,7 +615,7 @@ static void gfs2_write_blocks(struct gfs2_sbd *sdp, struct buffer_head *bh,
597 } else { 615 } else {
598 bh1 = gfs2_log_fake_buf(sdp, bd->bd_bh); 616 bh1 = gfs2_log_fake_buf(sdp, bd->bd_bh);
599 } 617 }
600 submit_bh(WRITE_SYNC_PLUG, bh1); 618 submit_bh(WRITE_SYNC, bh1);
601 gfs2_log_lock(sdp); 619 gfs2_log_lock(sdp);
602 ptr += 2; 620 ptr += 2;
603 } 621 }
@@ -745,6 +763,7 @@ const struct gfs2_log_operations gfs2_buf_lops = {
745const struct gfs2_log_operations gfs2_revoke_lops = { 763const struct gfs2_log_operations gfs2_revoke_lops = {
746 .lo_add = revoke_lo_add, 764 .lo_add = revoke_lo_add,
747 .lo_before_commit = revoke_lo_before_commit, 765 .lo_before_commit = revoke_lo_before_commit,
766 .lo_after_commit = revoke_lo_after_commit,
748 .lo_before_scan = revoke_lo_before_scan, 767 .lo_before_scan = revoke_lo_before_scan,
749 .lo_scan_elements = revoke_lo_scan_elements, 768 .lo_scan_elements = revoke_lo_scan_elements,
750 .lo_after_scan = revoke_lo_after_scan, 769 .lo_after_scan = revoke_lo_after_scan,
diff --git a/fs/gfs2/main.c b/fs/gfs2/main.c
index b1e9630eb46a..c2b34cd2abe0 100644
--- a/fs/gfs2/main.c
+++ b/fs/gfs2/main.c
@@ -14,6 +14,8 @@
14#include <linux/module.h> 14#include <linux/module.h>
15#include <linux/init.h> 15#include <linux/init.h>
16#include <linux/gfs2_ondisk.h> 16#include <linux/gfs2_ondisk.h>
17#include <linux/rcupdate.h>
18#include <linux/rculist_bl.h>
17#include <asm/atomic.h> 19#include <asm/atomic.h>
18 20
19#include "gfs2.h" 21#include "gfs2.h"
@@ -24,6 +26,7 @@
24#include "glock.h" 26#include "glock.h"
25#include "quota.h" 27#include "quota.h"
26#include "recovery.h" 28#include "recovery.h"
29#include "dir.h"
27 30
28static struct shrinker qd_shrinker = { 31static struct shrinker qd_shrinker = {
29 .shrink = gfs2_shrink_qd_memory, 32 .shrink = gfs2_shrink_qd_memory,
@@ -44,12 +47,13 @@ static void gfs2_init_glock_once(void *foo)
44{ 47{
45 struct gfs2_glock *gl = foo; 48 struct gfs2_glock *gl = foo;
46 49
47 INIT_HLIST_NODE(&gl->gl_list); 50 INIT_HLIST_BL_NODE(&gl->gl_list);
48 spin_lock_init(&gl->gl_spin); 51 spin_lock_init(&gl->gl_spin);
49 INIT_LIST_HEAD(&gl->gl_holders); 52 INIT_LIST_HEAD(&gl->gl_holders);
50 INIT_LIST_HEAD(&gl->gl_lru); 53 INIT_LIST_HEAD(&gl->gl_lru);
51 INIT_LIST_HEAD(&gl->gl_ail_list); 54 INIT_LIST_HEAD(&gl->gl_ail_list);
52 atomic_set(&gl->gl_ail_count, 0); 55 atomic_set(&gl->gl_ail_count, 0);
56 atomic_set(&gl->gl_revokes, 0);
53} 57}
54 58
55static void gfs2_init_gl_aspace_once(void *foo) 59static void gfs2_init_gl_aspace_once(void *foo)
@@ -58,14 +62,7 @@ static void gfs2_init_gl_aspace_once(void *foo)
58 struct address_space *mapping = (struct address_space *)(gl + 1); 62 struct address_space *mapping = (struct address_space *)(gl + 1);
59 63
60 gfs2_init_glock_once(gl); 64 gfs2_init_glock_once(gl);
61 memset(mapping, 0, sizeof(*mapping)); 65 address_space_init_once(mapping);
62 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
63 spin_lock_init(&mapping->tree_lock);
64 spin_lock_init(&mapping->i_mmap_lock);
65 INIT_LIST_HEAD(&mapping->private_list);
66 spin_lock_init(&mapping->private_lock);
67 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
68 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
69} 66}
70 67
71/** 68/**
@@ -78,6 +75,9 @@ static int __init init_gfs2_fs(void)
78{ 75{
79 int error; 76 int error;
80 77
78 gfs2_str2qstr(&gfs2_qdot, ".");
79 gfs2_str2qstr(&gfs2_qdotdot, "..");
80
81 error = gfs2_sys_init(); 81 error = gfs2_sys_init();
82 if (error) 82 if (error)
83 return error; 83 return error;
@@ -140,13 +140,13 @@ static int __init init_gfs2_fs(void)
140 140
141 error = -ENOMEM; 141 error = -ENOMEM;
142 gfs_recovery_wq = alloc_workqueue("gfs_recovery", 142 gfs_recovery_wq = alloc_workqueue("gfs_recovery",
143 WQ_NON_REENTRANT | WQ_RESCUER, 0); 143 WQ_MEM_RECLAIM | WQ_FREEZABLE, 0);
144 if (!gfs_recovery_wq) 144 if (!gfs_recovery_wq)
145 goto fail_wq; 145 goto fail_wq;
146 146
147 gfs2_register_debugfs(); 147 gfs2_register_debugfs();
148 148
149 printk("GFS2 (built %s %s) installed\n", __DATE__, __TIME__); 149 printk("GFS2 installed\n");
150 150
151 return 0; 151 return 0;
152 152
@@ -194,6 +194,8 @@ static void __exit exit_gfs2_fs(void)
194 unregister_filesystem(&gfs2meta_fs_type); 194 unregister_filesystem(&gfs2meta_fs_type);
195 destroy_workqueue(gfs_recovery_wq); 195 destroy_workqueue(gfs_recovery_wq);
196 196
197 rcu_barrier();
198
197 kmem_cache_destroy(gfs2_quotad_cachep); 199 kmem_cache_destroy(gfs2_quotad_cachep);
198 kmem_cache_destroy(gfs2_rgrpd_cachep); 200 kmem_cache_destroy(gfs2_rgrpd_cachep);
199 kmem_cache_destroy(gfs2_bufdata_cachep); 201 kmem_cache_destroy(gfs2_bufdata_cachep);
diff --git a/fs/gfs2/meta_io.c b/fs/gfs2/meta_io.c
index f3b071f921aa..747238cd9f96 100644
--- a/fs/gfs2/meta_io.c
+++ b/fs/gfs2/meta_io.c
@@ -31,13 +31,14 @@
31#include "rgrp.h" 31#include "rgrp.h"
32#include "trans.h" 32#include "trans.h"
33#include "util.h" 33#include "util.h"
34#include "trace_gfs2.h"
34 35
35static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc) 36static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
36{ 37{
37 struct buffer_head *bh, *head; 38 struct buffer_head *bh, *head;
38 int nr_underway = 0; 39 int nr_underway = 0;
39 int write_op = REQ_META | 40 int write_op = REQ_META |
40 (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC_PLUG : WRITE); 41 (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
41 42
42 BUG_ON(!PageLocked(page)); 43 BUG_ON(!PageLocked(page));
43 BUG_ON(!page_has_buffers(page)); 44 BUG_ON(!page_has_buffers(page));
@@ -55,7 +56,7 @@ static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wb
55 * activity, but those code paths have their own higher-level 56 * activity, but those code paths have their own higher-level
56 * throttling. 57 * throttling.
57 */ 58 */
58 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { 59 if (wbc->sync_mode != WB_SYNC_NONE) {
59 lock_buffer(bh); 60 lock_buffer(bh);
60 } else if (!trylock_buffer(bh)) { 61 } else if (!trylock_buffer(bh)) {
61 redirty_page_for_writepage(wbc, page); 62 redirty_page_for_writepage(wbc, page);
@@ -94,7 +95,6 @@ static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wb
94const struct address_space_operations gfs2_meta_aops = { 95const struct address_space_operations gfs2_meta_aops = {
95 .writepage = gfs2_aspace_writepage, 96 .writepage = gfs2_aspace_writepage,
96 .releasepage = gfs2_releasepage, 97 .releasepage = gfs2_releasepage,
97 .sync_page = block_sync_page,
98}; 98};
99 99
100/** 100/**
@@ -311,6 +311,7 @@ void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int
311 struct gfs2_bufdata *bd = bh->b_private; 311 struct gfs2_bufdata *bd = bh->b_private;
312 312
313 if (test_clear_buffer_pinned(bh)) { 313 if (test_clear_buffer_pinned(bh)) {
314 trace_gfs2_pin(bd, 0);
314 atomic_dec(&sdp->sd_log_pinned); 315 atomic_dec(&sdp->sd_log_pinned);
315 list_del_init(&bd->bd_le.le_list); 316 list_del_init(&bd->bd_le.le_list);
316 if (meta) { 317 if (meta) {
@@ -326,6 +327,7 @@ void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int
326 brelse(bh); 327 brelse(bh);
327 } 328 }
328 if (bd) { 329 if (bd) {
330 spin_lock(&sdp->sd_ail_lock);
329 if (bd->bd_ail) { 331 if (bd->bd_ail) {
330 gfs2_remove_from_ail(bd); 332 gfs2_remove_from_ail(bd);
331 bh->b_private = NULL; 333 bh->b_private = NULL;
@@ -333,6 +335,7 @@ void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int
333 bd->bd_blkno = bh->b_blocknr; 335 bd->bd_blkno = bh->b_blocknr;
334 gfs2_trans_add_revoke(sdp, bd); 336 gfs2_trans_add_revoke(sdp, bd);
335 } 337 }
338 spin_unlock(&sdp->sd_ail_lock);
336 } 339 }
337 clear_buffer_dirty(bh); 340 clear_buffer_dirty(bh);
338 clear_buffer_uptodate(bh); 341 clear_buffer_uptodate(bh);
diff --git a/fs/gfs2/meta_io.h b/fs/gfs2/meta_io.h
index 6a1d9ba16411..22c526593131 100644
--- a/fs/gfs2/meta_io.h
+++ b/fs/gfs2/meta_io.h
@@ -77,8 +77,6 @@ struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen);
77 77
78#define buffer_busy(bh) \ 78#define buffer_busy(bh) \
79((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned))) 79((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
80#define buffer_in_io(bh) \
81((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
82 80
83#endif /* __DIO_DOT_H__ */ 81#endif /* __DIO_DOT_H__ */
84 82
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 4d4b1e8ac64c..2a77071fb7b6 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -38,14 +38,6 @@
38#define DO 0 38#define DO 0
39#define UNDO 1 39#define UNDO 1
40 40
41static const u32 gfs2_old_fs_formats[] = {
42 0
43};
44
45static const u32 gfs2_old_multihost_formats[] = {
46 0
47};
48
49/** 41/**
50 * gfs2_tune_init - Fill a gfs2_tune structure with default values 42 * gfs2_tune_init - Fill a gfs2_tune structure with default values
51 * @gt: tune 43 * @gt: tune
@@ -80,6 +72,7 @@ static struct gfs2_sbd *init_sbd(struct super_block *sb)
80 72
81 init_waitqueue_head(&sdp->sd_glock_wait); 73 init_waitqueue_head(&sdp->sd_glock_wait);
82 atomic_set(&sdp->sd_glock_disposal, 0); 74 atomic_set(&sdp->sd_glock_disposal, 0);
75 init_completion(&sdp->sd_locking_init);
83 spin_lock_init(&sdp->sd_statfs_spin); 76 spin_lock_init(&sdp->sd_statfs_spin);
84 77
85 spin_lock_init(&sdp->sd_rindex_spin); 78 spin_lock_init(&sdp->sd_rindex_spin);
@@ -107,6 +100,7 @@ static struct gfs2_sbd *init_sbd(struct super_block *sb)
107 100
108 init_waitqueue_head(&sdp->sd_log_waitq); 101 init_waitqueue_head(&sdp->sd_log_waitq);
109 init_waitqueue_head(&sdp->sd_logd_waitq); 102 init_waitqueue_head(&sdp->sd_logd_waitq);
103 spin_lock_init(&sdp->sd_ail_lock);
110 INIT_LIST_HEAD(&sdp->sd_ail1_list); 104 INIT_LIST_HEAD(&sdp->sd_ail1_list);
111 INIT_LIST_HEAD(&sdp->sd_ail2_list); 105 INIT_LIST_HEAD(&sdp->sd_ail2_list);
112 106
@@ -133,9 +127,9 @@ static struct gfs2_sbd *init_sbd(struct super_block *sb)
133 * changed. 127 * changed.
134 */ 128 */
135 129
136static int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int silent) 130static int gfs2_check_sb(struct gfs2_sbd *sdp, int silent)
137{ 131{
138 unsigned int x; 132 struct gfs2_sb_host *sb = &sdp->sd_sb;
139 133
140 if (sb->sb_magic != GFS2_MAGIC || 134 if (sb->sb_magic != GFS2_MAGIC ||
141 sb->sb_type != GFS2_METATYPE_SB) { 135 sb->sb_type != GFS2_METATYPE_SB) {
@@ -150,55 +144,9 @@ static int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int sile
150 sb->sb_multihost_format == GFS2_FORMAT_MULTI) 144 sb->sb_multihost_format == GFS2_FORMAT_MULTI)
151 return 0; 145 return 0;
152 146
153 if (sb->sb_fs_format != GFS2_FORMAT_FS) { 147 fs_warn(sdp, "Unknown on-disk format, unable to mount\n");
154 for (x = 0; gfs2_old_fs_formats[x]; x++)
155 if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
156 break;
157
158 if (!gfs2_old_fs_formats[x]) {
159 printk(KERN_WARNING
160 "GFS2: code version (%u, %u) is incompatible "
161 "with ondisk format (%u, %u)\n",
162 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
163 sb->sb_fs_format, sb->sb_multihost_format);
164 printk(KERN_WARNING
165 "GFS2: I don't know how to upgrade this FS\n");
166 return -EINVAL;
167 }
168 }
169 148
170 if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) { 149 return -EINVAL;
171 for (x = 0; gfs2_old_multihost_formats[x]; x++)
172 if (gfs2_old_multihost_formats[x] ==
173 sb->sb_multihost_format)
174 break;
175
176 if (!gfs2_old_multihost_formats[x]) {
177 printk(KERN_WARNING
178 "GFS2: code version (%u, %u) is incompatible "
179 "with ondisk format (%u, %u)\n",
180 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
181 sb->sb_fs_format, sb->sb_multihost_format);
182 printk(KERN_WARNING
183 "GFS2: I don't know how to upgrade this FS\n");
184 return -EINVAL;
185 }
186 }
187
188 if (!sdp->sd_args.ar_upgrade) {
189 printk(KERN_WARNING
190 "GFS2: code version (%u, %u) is incompatible "
191 "with ondisk format (%u, %u)\n",
192 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
193 sb->sb_fs_format, sb->sb_multihost_format);
194 printk(KERN_INFO
195 "GFS2: Use the \"upgrade\" mount option to upgrade "
196 "the FS\n");
197 printk(KERN_INFO "GFS2: See the manual for more details\n");
198 return -EINVAL;
199 }
200
201 return 0;
202} 150}
203 151
204static void end_bio_io_page(struct bio *bio, int error) 152static void end_bio_io_page(struct bio *bio, int error)
@@ -212,8 +160,10 @@ static void end_bio_io_page(struct bio *bio, int error)
212 unlock_page(page); 160 unlock_page(page);
213} 161}
214 162
215static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf) 163static void gfs2_sb_in(struct gfs2_sbd *sdp, const void *buf)
216{ 164{
165 struct gfs2_sb_host *sb = &sdp->sd_sb;
166 struct super_block *s = sdp->sd_vfs;
217 const struct gfs2_sb *str = buf; 167 const struct gfs2_sb *str = buf;
218 168
219 sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic); 169 sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
@@ -230,7 +180,7 @@ static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
230 180
231 memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN); 181 memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
232 memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN); 182 memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
233 memcpy(sb->sb_uuid, str->sb_uuid, 16); 183 memcpy(s->s_uuid, str->sb_uuid, 16);
234} 184}
235 185
236/** 186/**
@@ -252,7 +202,7 @@ static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
252 * Returns: 0 on success or error 202 * Returns: 0 on success or error
253 */ 203 */
254 204
255static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector) 205static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector, int silent)
256{ 206{
257 struct super_block *sb = sdp->sd_vfs; 207 struct super_block *sb = sdp->sd_vfs;
258 struct gfs2_sb *p; 208 struct gfs2_sb *p;
@@ -282,10 +232,10 @@ static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector)
282 return -EIO; 232 return -EIO;
283 } 233 }
284 p = kmap(page); 234 p = kmap(page);
285 gfs2_sb_in(&sdp->sd_sb, p); 235 gfs2_sb_in(sdp, p);
286 kunmap(page); 236 kunmap(page);
287 __free_page(page); 237 __free_page(page);
288 return 0; 238 return gfs2_check_sb(sdp, silent);
289} 239}
290 240
291/** 241/**
@@ -302,17 +252,13 @@ static int gfs2_read_sb(struct gfs2_sbd *sdp, int silent)
302 unsigned int x; 252 unsigned int x;
303 int error; 253 int error;
304 254
305 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift); 255 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
306 if (error) { 256 if (error) {
307 if (!silent) 257 if (!silent)
308 fs_err(sdp, "can't read superblock\n"); 258 fs_err(sdp, "can't read superblock\n");
309 return error; 259 return error;
310 } 260 }
311 261
312 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
313 if (error)
314 return error;
315
316 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift - 262 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
317 GFS2_BASIC_BLOCK_SHIFT; 263 GFS2_BASIC_BLOCK_SHIFT;
318 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift; 264 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
@@ -395,14 +341,10 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
395 /* Try to autodetect */ 341 /* Try to autodetect */
396 342
397 if (!proto[0] || !table[0]) { 343 if (!proto[0] || !table[0]) {
398 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift); 344 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
399 if (error) 345 if (error)
400 return error; 346 return error;
401 347
402 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
403 if (error)
404 goto out;
405
406 if (!proto[0]) 348 if (!proto[0])
407 proto = sdp->sd_sb.sb_lockproto; 349 proto = sdp->sd_sb.sb_lockproto;
408 if (!table[0]) 350 if (!table[0])
@@ -419,7 +361,6 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
419 while ((table = strchr(table, '/'))) 361 while ((table = strchr(table, '/')))
420 *table = '_'; 362 *table = '_';
421 363
422out:
423 return error; 364 return error;
424} 365}
425 366
@@ -485,7 +426,7 @@ static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
485 struct dentry *dentry; 426 struct dentry *dentry;
486 struct inode *inode; 427 struct inode *inode;
487 428
488 inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0); 429 inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0, 0);
489 if (IS_ERR(inode)) { 430 if (IS_ERR(inode)) {
490 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode)); 431 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
491 return PTR_ERR(inode); 432 return PTR_ERR(inode);
@@ -496,7 +437,6 @@ static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
496 iput(inode); 437 iput(inode);
497 return -ENOMEM; 438 return -ENOMEM;
498 } 439 }
499 dentry->d_op = &gfs2_dops;
500 *dptr = dentry; 440 *dptr = dentry;
501 return 0; 441 return 0;
502} 442}
@@ -586,7 +526,7 @@ static int map_journal_extents(struct gfs2_sbd *sdp)
586 526
587 prev_db = 0; 527 prev_db = 0;
588 528
589 for (lb = 0; lb < ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; lb++) { 529 for (lb = 0; lb < i_size_read(jd->jd_inode) >> sdp->sd_sb.sb_bsize_shift; lb++) {
590 bh.b_state = 0; 530 bh.b_state = 0;
591 bh.b_blocknr = 0; 531 bh.b_blocknr = 0;
592 bh.b_size = 1 << ip->i_inode.i_blkbits; 532 bh.b_size = 1 << ip->i_inode.i_blkbits;
@@ -985,17 +925,9 @@ static const match_table_t nolock_tokens = {
985 { Opt_err, NULL }, 925 { Opt_err, NULL },
986}; 926};
987 927
988static void nolock_put_lock(struct kmem_cache *cachep, struct gfs2_glock *gl)
989{
990 struct gfs2_sbd *sdp = gl->gl_sbd;
991 kmem_cache_free(cachep, gl);
992 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
993 wake_up(&sdp->sd_glock_wait);
994}
995
996static const struct lm_lockops nolock_ops = { 928static const struct lm_lockops nolock_ops = {
997 .lm_proto_name = "lock_nolock", 929 .lm_proto_name = "lock_nolock",
998 .lm_put_lock = nolock_put_lock, 930 .lm_put_lock = gfs2_glock_free,
999 .lm_tokens = &nolock_tokens, 931 .lm_tokens = &nolock_tokens,
1000}; 932};
1001 933
@@ -1022,7 +954,6 @@ static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
1022 if (!strcmp("lock_nolock", proto)) { 954 if (!strcmp("lock_nolock", proto)) {
1023 lm = &nolock_ops; 955 lm = &nolock_ops;
1024 sdp->sd_args.ar_localflocks = 1; 956 sdp->sd_args.ar_localflocks = 1;
1025 sdp->sd_args.ar_localcaching = 1;
1026#ifdef CONFIG_GFS2_FS_LOCKING_DLM 957#ifdef CONFIG_GFS2_FS_LOCKING_DLM
1027 } else if (!strcmp("lock_dlm", proto)) { 958 } else if (!strcmp("lock_dlm", proto)) {
1028 lm = &gfs2_dlm_ops; 959 lm = &gfs2_dlm_ops;
@@ -1087,11 +1018,13 @@ hostdata_error:
1087 fsname++; 1018 fsname++;
1088 if (lm->lm_mount == NULL) { 1019 if (lm->lm_mount == NULL) {
1089 fs_info(sdp, "Now mounting FS...\n"); 1020 fs_info(sdp, "Now mounting FS...\n");
1021 complete(&sdp->sd_locking_init);
1090 return 0; 1022 return 0;
1091 } 1023 }
1092 ret = lm->lm_mount(sdp, fsname); 1024 ret = lm->lm_mount(sdp, fsname);
1093 if (ret == 0) 1025 if (ret == 0)
1094 fs_info(sdp, "Joined cluster. Now mounting FS...\n"); 1026 fs_info(sdp, "Joined cluster. Now mounting FS...\n");
1027 complete(&sdp->sd_locking_init);
1095 return ret; 1028 return ret;
1096} 1029}
1097 1030
@@ -1113,8 +1046,6 @@ static int gfs2_journalid_wait(void *word)
1113 1046
1114static int wait_on_journal(struct gfs2_sbd *sdp) 1047static int wait_on_journal(struct gfs2_sbd *sdp)
1115{ 1048{
1116 if (sdp->sd_args.ar_spectator)
1117 return 0;
1118 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL) 1049 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
1119 return 0; 1050 return 0;
1120 1051
@@ -1165,6 +1096,7 @@ static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent
1165 1096
1166 sb->s_magic = GFS2_MAGIC; 1097 sb->s_magic = GFS2_MAGIC;
1167 sb->s_op = &gfs2_super_ops; 1098 sb->s_op = &gfs2_super_ops;
1099 sb->s_d_op = &gfs2_dops;
1168 sb->s_export_op = &gfs2_export_ops; 1100 sb->s_export_op = &gfs2_export_ops;
1169 sb->s_xattr = gfs2_xattr_handlers; 1101 sb->s_xattr = gfs2_xattr_handlers;
1170 sb->s_qcop = &gfs2_quotactl_ops; 1102 sb->s_qcop = &gfs2_quotactl_ops;
@@ -1185,8 +1117,7 @@ static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent
1185 if (sdp->sd_args.ar_statfs_quantum) { 1117 if (sdp->sd_args.ar_statfs_quantum) {
1186 sdp->sd_tune.gt_statfs_slow = 0; 1118 sdp->sd_tune.gt_statfs_slow = 0;
1187 sdp->sd_tune.gt_statfs_quantum = sdp->sd_args.ar_statfs_quantum; 1119 sdp->sd_tune.gt_statfs_quantum = sdp->sd_args.ar_statfs_quantum;
1188 } 1120 } else {
1189 else {
1190 sdp->sd_tune.gt_statfs_slow = 1; 1121 sdp->sd_tune.gt_statfs_slow = 1;
1191 sdp->sd_tune.gt_statfs_quantum = 30; 1122 sdp->sd_tune.gt_statfs_quantum = 30;
1192 } 1123 }
@@ -1217,6 +1148,20 @@ static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent
1217 if (error) 1148 if (error)
1218 goto fail_sb; 1149 goto fail_sb;
1219 1150
1151 /*
1152 * If user space has failed to join the cluster or some similar
1153 * failure has occurred, then the journal id will contain a
1154 * negative (error) number. This will then be returned to the
1155 * caller (of the mount syscall). We do this even for spectator
1156 * mounts (which just write a jid of 0 to indicate "ok" even though
1157 * the jid is unused in the spectator case)
1158 */
1159 if (sdp->sd_lockstruct.ls_jid < 0) {
1160 error = sdp->sd_lockstruct.ls_jid;
1161 sdp->sd_lockstruct.ls_jid = 0;
1162 goto fail_sb;
1163 }
1164
1220 error = init_inodes(sdp, DO); 1165 error = init_inodes(sdp, DO);
1221 if (error) 1166 if (error)
1222 goto fail_sb; 1167 goto fail_sb;
@@ -1264,7 +1209,6 @@ fail_sb:
1264fail_locking: 1209fail_locking:
1265 init_locking(sdp, &mount_gh, UNDO); 1210 init_locking(sdp, &mount_gh, UNDO);
1266fail_lm: 1211fail_lm:
1267 invalidate_inodes(sb);
1268 gfs2_gl_hash_clear(sdp); 1212 gfs2_gl_hash_clear(sdp);
1269 gfs2_lm_unmount(sdp); 1213 gfs2_lm_unmount(sdp);
1270fail_sys: 1214fail_sys:
@@ -1296,12 +1240,11 @@ static int test_gfs2_super(struct super_block *s, void *ptr)
1296} 1240}
1297 1241
1298/** 1242/**
1299 * gfs2_get_sb - Get the GFS2 superblock 1243 * gfs2_mount - Get the GFS2 superblock
1300 * @fs_type: The GFS2 filesystem type 1244 * @fs_type: The GFS2 filesystem type
1301 * @flags: Mount flags 1245 * @flags: Mount flags
1302 * @dev_name: The name of the device 1246 * @dev_name: The name of the device
1303 * @data: The mount arguments 1247 * @data: The mount arguments
1304 * @mnt: The vfsmnt for this mount
1305 * 1248 *
1306 * Q. Why not use get_sb_bdev() ? 1249 * Q. Why not use get_sb_bdev() ?
1307 * A. We need to select one of two root directories to mount, independent 1250 * A. We need to select one of two root directories to mount, independent
@@ -1310,12 +1253,12 @@ static int test_gfs2_super(struct super_block *s, void *ptr)
1310 * Returns: 0 or -ve on error 1253 * Returns: 0 or -ve on error
1311 */ 1254 */
1312 1255
1313static int gfs2_get_sb(struct file_system_type *fs_type, int flags, 1256static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
1314 const char *dev_name, void *data, struct vfsmount *mnt) 1257 const char *dev_name, void *data)
1315{ 1258{
1316 struct block_device *bdev; 1259 struct block_device *bdev;
1317 struct super_block *s; 1260 struct super_block *s;
1318 fmode_t mode = FMODE_READ; 1261 fmode_t mode = FMODE_READ | FMODE_EXCL;
1319 int error; 1262 int error;
1320 struct gfs2_args args; 1263 struct gfs2_args args;
1321 struct gfs2_sbd *sdp; 1264 struct gfs2_sbd *sdp;
@@ -1323,9 +1266,9 @@ static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
1323 if (!(flags & MS_RDONLY)) 1266 if (!(flags & MS_RDONLY))
1324 mode |= FMODE_WRITE; 1267 mode |= FMODE_WRITE;
1325 1268
1326 bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1269 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1327 if (IS_ERR(bdev)) 1270 if (IS_ERR(bdev))
1328 return PTR_ERR(bdev); 1271 return ERR_CAST(bdev);
1329 1272
1330 /* 1273 /*
1331 * once the super is inserted into the list by sget, s_umount 1274 * once the super is inserted into the list by sget, s_umount
@@ -1344,6 +1287,9 @@ static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
1344 if (IS_ERR(s)) 1287 if (IS_ERR(s))
1345 goto error_bdev; 1288 goto error_bdev;
1346 1289
1290 if (s->s_root)
1291 blkdev_put(bdev, mode);
1292
1347 memset(&args, 0, sizeof(args)); 1293 memset(&args, 0, sizeof(args));
1348 args.ar_quota = GFS2_QUOTA_DEFAULT; 1294 args.ar_quota = GFS2_QUOTA_DEFAULT;
1349 args.ar_data = GFS2_DATA_DEFAULT; 1295 args.ar_data = GFS2_DATA_DEFAULT;
@@ -1355,17 +1301,13 @@ static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
1355 error = gfs2_mount_args(&args, data); 1301 error = gfs2_mount_args(&args, data);
1356 if (error) { 1302 if (error) {
1357 printk(KERN_WARNING "GFS2: can't parse mount arguments\n"); 1303 printk(KERN_WARNING "GFS2: can't parse mount arguments\n");
1358 if (s->s_root) 1304 goto error_super;
1359 goto error_super;
1360 deactivate_locked_super(s);
1361 return error;
1362 } 1305 }
1363 1306
1364 if (s->s_root) { 1307 if (s->s_root) {
1365 error = -EBUSY; 1308 error = -EBUSY;
1366 if ((flags ^ s->s_flags) & MS_RDONLY) 1309 if ((flags ^ s->s_flags) & MS_RDONLY)
1367 goto error_super; 1310 goto error_super;
1368 close_bdev_exclusive(bdev, mode);
1369 } else { 1311 } else {
1370 char b[BDEVNAME_SIZE]; 1312 char b[BDEVNAME_SIZE];
1371 1313
@@ -1374,27 +1316,24 @@ static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
1374 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1316 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1375 sb_set_blocksize(s, block_size(bdev)); 1317 sb_set_blocksize(s, block_size(bdev));
1376 error = fill_super(s, &args, flags & MS_SILENT ? 1 : 0); 1318 error = fill_super(s, &args, flags & MS_SILENT ? 1 : 0);
1377 if (error) { 1319 if (error)
1378 deactivate_locked_super(s); 1320 goto error_super;
1379 return error;
1380 }
1381 s->s_flags |= MS_ACTIVE; 1321 s->s_flags |= MS_ACTIVE;
1382 bdev->bd_super = s; 1322 bdev->bd_super = s;
1383 } 1323 }
1384 1324
1385 sdp = s->s_fs_info; 1325 sdp = s->s_fs_info;
1386 mnt->mnt_sb = s;
1387 if (args.ar_meta) 1326 if (args.ar_meta)
1388 mnt->mnt_root = dget(sdp->sd_master_dir); 1327 return dget(sdp->sd_master_dir);
1389 else 1328 else
1390 mnt->mnt_root = dget(sdp->sd_root_dir); 1329 return dget(sdp->sd_root_dir);
1391 return 0;
1392 1330
1393error_super: 1331error_super:
1394 deactivate_locked_super(s); 1332 deactivate_locked_super(s);
1333 return ERR_PTR(error);
1395error_bdev: 1334error_bdev:
1396 close_bdev_exclusive(bdev, mode); 1335 blkdev_put(bdev, mode);
1397 return error; 1336 return ERR_PTR(error);
1398} 1337}
1399 1338
1400static int set_meta_super(struct super_block *s, void *ptr) 1339static int set_meta_super(struct super_block *s, void *ptr)
@@ -1402,8 +1341,8 @@ static int set_meta_super(struct super_block *s, void *ptr)
1402 return -EINVAL; 1341 return -EINVAL;
1403} 1342}
1404 1343
1405static int gfs2_get_sb_meta(struct file_system_type *fs_type, int flags, 1344static struct dentry *gfs2_mount_meta(struct file_system_type *fs_type,
1406 const char *dev_name, void *data, struct vfsmount *mnt) 1345 int flags, const char *dev_name, void *data)
1407{ 1346{
1408 struct super_block *s; 1347 struct super_block *s;
1409 struct gfs2_sbd *sdp; 1348 struct gfs2_sbd *sdp;
@@ -1414,23 +1353,21 @@ static int gfs2_get_sb_meta(struct file_system_type *fs_type, int flags,
1414 if (error) { 1353 if (error) {
1415 printk(KERN_WARNING "GFS2: path_lookup on %s returned error %d\n", 1354 printk(KERN_WARNING "GFS2: path_lookup on %s returned error %d\n",
1416 dev_name, error); 1355 dev_name, error);
1417 return error; 1356 return ERR_PTR(error);
1418 } 1357 }
1419 s = sget(&gfs2_fs_type, test_gfs2_super, set_meta_super, 1358 s = sget(&gfs2_fs_type, test_gfs2_super, set_meta_super,
1420 path.dentry->d_inode->i_sb->s_bdev); 1359 path.dentry->d_inode->i_sb->s_bdev);
1421 path_put(&path); 1360 path_put(&path);
1422 if (IS_ERR(s)) { 1361 if (IS_ERR(s)) {
1423 printk(KERN_WARNING "GFS2: gfs2 mount does not exist\n"); 1362 printk(KERN_WARNING "GFS2: gfs2 mount does not exist\n");
1424 return PTR_ERR(s); 1363 return ERR_CAST(s);
1425 } 1364 }
1426 if ((flags ^ s->s_flags) & MS_RDONLY) { 1365 if ((flags ^ s->s_flags) & MS_RDONLY) {
1427 deactivate_locked_super(s); 1366 deactivate_locked_super(s);
1428 return -EBUSY; 1367 return ERR_PTR(-EBUSY);
1429 } 1368 }
1430 sdp = s->s_fs_info; 1369 sdp = s->s_fs_info;
1431 mnt->mnt_sb = s; 1370 return dget(sdp->sd_master_dir);
1432 mnt->mnt_root = dget(sdp->sd_master_dir);
1433 return 0;
1434} 1371}
1435 1372
1436static void gfs2_kill_sb(struct super_block *sb) 1373static void gfs2_kill_sb(struct super_block *sb)
@@ -1456,7 +1393,7 @@ static void gfs2_kill_sb(struct super_block *sb)
1456struct file_system_type gfs2_fs_type = { 1393struct file_system_type gfs2_fs_type = {
1457 .name = "gfs2", 1394 .name = "gfs2",
1458 .fs_flags = FS_REQUIRES_DEV, 1395 .fs_flags = FS_REQUIRES_DEV,
1459 .get_sb = gfs2_get_sb, 1396 .mount = gfs2_mount,
1460 .kill_sb = gfs2_kill_sb, 1397 .kill_sb = gfs2_kill_sb,
1461 .owner = THIS_MODULE, 1398 .owner = THIS_MODULE,
1462}; 1399};
@@ -1464,7 +1401,7 @@ struct file_system_type gfs2_fs_type = {
1464struct file_system_type gfs2meta_fs_type = { 1401struct file_system_type gfs2meta_fs_type = {
1465 .name = "gfs2meta", 1402 .name = "gfs2meta",
1466 .fs_flags = FS_REQUIRES_DEV, 1403 .fs_flags = FS_REQUIRES_DEV,
1467 .get_sb = gfs2_get_sb_meta, 1404 .mount = gfs2_mount_meta,
1468 .owner = THIS_MODULE, 1405 .owner = THIS_MODULE,
1469}; 1406};
1470 1407
diff --git a/fs/gfs2/ops_inode.c b/fs/gfs2/ops_inode.c
deleted file mode 100644
index 1009be2c9737..000000000000
--- a/fs/gfs2/ops_inode.c
+++ /dev/null
@@ -1,1390 +0,0 @@
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
9
10#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
14#include <linux/namei.h>
15#include <linux/mm.h>
16#include <linux/xattr.h>
17#include <linux/posix_acl.h>
18#include <linux/gfs2_ondisk.h>
19#include <linux/crc32.h>
20#include <linux/fiemap.h>
21#include <asm/uaccess.h>
22
23#include "gfs2.h"
24#include "incore.h"
25#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
28#include "xattr.h"
29#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
32#include "quota.h"
33#include "rgrp.h"
34#include "trans.h"
35#include "util.h"
36#include "super.h"
37
38/**
39 * gfs2_create - Create a file
40 * @dir: The directory in which to create the file
41 * @dentry: The dentry of the new file
42 * @mode: The mode of the new file
43 *
44 * Returns: errno
45 */
46
47static int gfs2_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
49{
50 struct gfs2_inode *dip = GFS2_I(dir);
51 struct gfs2_sbd *sdp = GFS2_SB(dir);
52 struct gfs2_holder ghs[2];
53 struct inode *inode;
54
55 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
56
57 for (;;) {
58 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
59 if (!IS_ERR(inode)) {
60 gfs2_trans_end(sdp);
61 if (dip->i_alloc->al_rgd)
62 gfs2_inplace_release(dip);
63 gfs2_quota_unlock(dip);
64 gfs2_alloc_put(dip);
65 gfs2_glock_dq_uninit_m(2, ghs);
66 mark_inode_dirty(inode);
67 break;
68 } else if (PTR_ERR(inode) != -EEXIST ||
69 (nd && nd->flags & LOOKUP_EXCL)) {
70 gfs2_holder_uninit(ghs);
71 return PTR_ERR(inode);
72 }
73
74 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
75 if (inode) {
76 if (!IS_ERR(inode)) {
77 gfs2_holder_uninit(ghs);
78 break;
79 } else {
80 gfs2_holder_uninit(ghs);
81 return PTR_ERR(inode);
82 }
83 }
84 }
85
86 d_instantiate(dentry, inode);
87
88 return 0;
89}
90
91/**
92 * gfs2_lookup - Look up a filename in a directory and return its inode
93 * @dir: The directory inode
94 * @dentry: The dentry of the new inode
95 * @nd: passed from Linux VFS, ignored by us
96 *
97 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
98 *
99 * Returns: errno
100 */
101
102static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
103 struct nameidata *nd)
104{
105 struct inode *inode = NULL;
106
107 dentry->d_op = &gfs2_dops;
108
109 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
110 if (inode && IS_ERR(inode))
111 return ERR_CAST(inode);
112
113 if (inode) {
114 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
115 struct gfs2_holder gh;
116 int error;
117 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
118 if (error) {
119 iput(inode);
120 return ERR_PTR(error);
121 }
122 gfs2_glock_dq_uninit(&gh);
123 return d_splice_alias(inode, dentry);
124 }
125 d_add(dentry, inode);
126
127 return NULL;
128}
129
130/**
131 * gfs2_link - Link to a file
132 * @old_dentry: The inode to link
133 * @dir: Add link to this directory
134 * @dentry: The name of the link
135 *
136 * Link the inode in "old_dentry" into the directory "dir" with the
137 * name in "dentry".
138 *
139 * Returns: errno
140 */
141
142static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
143 struct dentry *dentry)
144{
145 struct gfs2_inode *dip = GFS2_I(dir);
146 struct gfs2_sbd *sdp = GFS2_SB(dir);
147 struct inode *inode = old_dentry->d_inode;
148 struct gfs2_inode *ip = GFS2_I(inode);
149 struct gfs2_holder ghs[2];
150 int alloc_required;
151 int error;
152
153 if (S_ISDIR(inode->i_mode))
154 return -EPERM;
155
156 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
157 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
158
159 error = gfs2_glock_nq(ghs); /* parent */
160 if (error)
161 goto out_parent;
162
163 error = gfs2_glock_nq(ghs + 1); /* child */
164 if (error)
165 goto out_child;
166
167 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
168 if (error)
169 goto out_gunlock;
170
171 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
172 switch (error) {
173 case -ENOENT:
174 break;
175 case 0:
176 error = -EEXIST;
177 default:
178 goto out_gunlock;
179 }
180
181 error = -EINVAL;
182 if (!dip->i_inode.i_nlink)
183 goto out_gunlock;
184 error = -EFBIG;
185 if (dip->i_entries == (u32)-1)
186 goto out_gunlock;
187 error = -EPERM;
188 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
189 goto out_gunlock;
190 error = -EINVAL;
191 if (!ip->i_inode.i_nlink)
192 goto out_gunlock;
193 error = -EMLINK;
194 if (ip->i_inode.i_nlink == (u32)-1)
195 goto out_gunlock;
196
197 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
198 if (error < 0)
199 goto out_gunlock;
200 error = 0;
201
202 if (alloc_required) {
203 struct gfs2_alloc *al = gfs2_alloc_get(dip);
204 if (!al) {
205 error = -ENOMEM;
206 goto out_gunlock;
207 }
208
209 error = gfs2_quota_lock_check(dip);
210 if (error)
211 goto out_alloc;
212
213 al->al_requested = sdp->sd_max_dirres;
214
215 error = gfs2_inplace_reserve(dip);
216 if (error)
217 goto out_gunlock_q;
218
219 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
220 al->al_rgd->rd_length +
221 2 * RES_DINODE + RES_STATFS +
222 RES_QUOTA, 0);
223 if (error)
224 goto out_ipres;
225 } else {
226 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
227 if (error)
228 goto out_ipres;
229 }
230
231 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
232 if (error)
233 goto out_end_trans;
234
235 error = gfs2_change_nlink(ip, +1);
236
237out_end_trans:
238 gfs2_trans_end(sdp);
239out_ipres:
240 if (alloc_required)
241 gfs2_inplace_release(dip);
242out_gunlock_q:
243 if (alloc_required)
244 gfs2_quota_unlock(dip);
245out_alloc:
246 if (alloc_required)
247 gfs2_alloc_put(dip);
248out_gunlock:
249 gfs2_glock_dq(ghs + 1);
250out_child:
251 gfs2_glock_dq(ghs);
252out_parent:
253 gfs2_holder_uninit(ghs);
254 gfs2_holder_uninit(ghs + 1);
255 if (!error) {
256 atomic_inc(&inode->i_count);
257 d_instantiate(dentry, inode);
258 mark_inode_dirty(inode);
259 }
260 return error;
261}
262
263/*
264 * gfs2_unlink_ok - check to see that a inode is still in a directory
265 * @dip: the directory
266 * @name: the name of the file
267 * @ip: the inode
268 *
269 * Assumes that the lock on (at least) @dip is held.
270 *
271 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
272 */
273
274static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
275 const struct gfs2_inode *ip)
276{
277 int error;
278
279 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
280 return -EPERM;
281
282 if ((dip->i_inode.i_mode & S_ISVTX) &&
283 dip->i_inode.i_uid != current_fsuid() &&
284 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
285 return -EPERM;
286
287 if (IS_APPEND(&dip->i_inode))
288 return -EPERM;
289
290 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
291 if (error)
292 return error;
293
294 error = gfs2_dir_check(&dip->i_inode, name, ip);
295 if (error)
296 return error;
297
298 return 0;
299}
300
301/**
302 * gfs2_unlink - Unlink a file
303 * @dir: The inode of the directory containing the file to unlink
304 * @dentry: The file itself
305 *
306 * Unlink a file. Call gfs2_unlinki()
307 *
308 * Returns: errno
309 */
310
311static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
312{
313 struct gfs2_inode *dip = GFS2_I(dir);
314 struct gfs2_sbd *sdp = GFS2_SB(dir);
315 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
316 struct gfs2_holder ghs[3];
317 struct gfs2_rgrpd *rgd;
318 struct gfs2_holder ri_gh;
319 int error;
320
321 error = gfs2_rindex_hold(sdp, &ri_gh);
322 if (error)
323 return error;
324
325 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
326 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
327
328 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
329 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
330
331
332 error = gfs2_glock_nq(ghs); /* parent */
333 if (error)
334 goto out_parent;
335
336 error = gfs2_glock_nq(ghs + 1); /* child */
337 if (error)
338 goto out_child;
339
340 error = gfs2_glock_nq(ghs + 2); /* rgrp */
341 if (error)
342 goto out_rgrp;
343
344 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
345 if (error)
346 goto out_gunlock;
347
348 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
349 if (error)
350 goto out_gunlock;
351
352 error = gfs2_dir_del(dip, &dentry->d_name);
353 if (error)
354 goto out_end_trans;
355
356 error = gfs2_change_nlink(ip, -1);
357
358out_end_trans:
359 gfs2_trans_end(sdp);
360out_gunlock:
361 gfs2_glock_dq(ghs + 2);
362out_rgrp:
363 gfs2_holder_uninit(ghs + 2);
364 gfs2_glock_dq(ghs + 1);
365out_child:
366 gfs2_holder_uninit(ghs + 1);
367 gfs2_glock_dq(ghs);
368out_parent:
369 gfs2_holder_uninit(ghs);
370 gfs2_glock_dq_uninit(&ri_gh);
371 return error;
372}
373
374/**
375 * gfs2_symlink - Create a symlink
376 * @dir: The directory to create the symlink in
377 * @dentry: The dentry to put the symlink in
378 * @symname: The thing which the link points to
379 *
380 * Returns: errno
381 */
382
383static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
384 const char *symname)
385{
386 struct gfs2_inode *dip = GFS2_I(dir), *ip;
387 struct gfs2_sbd *sdp = GFS2_SB(dir);
388 struct gfs2_holder ghs[2];
389 struct inode *inode;
390 struct buffer_head *dibh;
391 int size;
392 int error;
393
394 /* Must be stuffed with a null terminator for gfs2_follow_link() */
395 size = strlen(symname);
396 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
397 return -ENAMETOOLONG;
398
399 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
400
401 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
402 if (IS_ERR(inode)) {
403 gfs2_holder_uninit(ghs);
404 return PTR_ERR(inode);
405 }
406
407 ip = ghs[1].gh_gl->gl_object;
408
409 ip->i_disksize = size;
410 i_size_write(inode, size);
411
412 error = gfs2_meta_inode_buffer(ip, &dibh);
413
414 if (!gfs2_assert_withdraw(sdp, !error)) {
415 gfs2_dinode_out(ip, dibh->b_data);
416 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
417 size);
418 brelse(dibh);
419 }
420
421 gfs2_trans_end(sdp);
422 if (dip->i_alloc->al_rgd)
423 gfs2_inplace_release(dip);
424 gfs2_quota_unlock(dip);
425 gfs2_alloc_put(dip);
426
427 gfs2_glock_dq_uninit_m(2, ghs);
428
429 d_instantiate(dentry, inode);
430 mark_inode_dirty(inode);
431
432 return 0;
433}
434
435/**
436 * gfs2_mkdir - Make a directory
437 * @dir: The parent directory of the new one
438 * @dentry: The dentry of the new directory
439 * @mode: The mode of the new directory
440 *
441 * Returns: errno
442 */
443
444static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
445{
446 struct gfs2_inode *dip = GFS2_I(dir), *ip;
447 struct gfs2_sbd *sdp = GFS2_SB(dir);
448 struct gfs2_holder ghs[2];
449 struct inode *inode;
450 struct buffer_head *dibh;
451 int error;
452
453 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
454
455 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
456 if (IS_ERR(inode)) {
457 gfs2_holder_uninit(ghs);
458 return PTR_ERR(inode);
459 }
460
461 ip = ghs[1].gh_gl->gl_object;
462
463 ip->i_inode.i_nlink = 2;
464 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
465 ip->i_diskflags |= GFS2_DIF_JDATA;
466 ip->i_entries = 2;
467
468 error = gfs2_meta_inode_buffer(ip, &dibh);
469
470 if (!gfs2_assert_withdraw(sdp, !error)) {
471 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
472 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
473 struct qstr str;
474
475 gfs2_str2qstr(&str, ".");
476 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
477 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
478 dent->de_inum = di->di_num; /* already GFS2 endian */
479 dent->de_type = cpu_to_be16(DT_DIR);
480 di->di_entries = cpu_to_be32(1);
481
482 gfs2_str2qstr(&str, "..");
483 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
484 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
485
486 gfs2_inum_out(dip, dent);
487 dent->de_type = cpu_to_be16(DT_DIR);
488
489 gfs2_dinode_out(ip, di);
490
491 brelse(dibh);
492 }
493
494 error = gfs2_change_nlink(dip, +1);
495 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
496
497 gfs2_trans_end(sdp);
498 if (dip->i_alloc->al_rgd)
499 gfs2_inplace_release(dip);
500 gfs2_quota_unlock(dip);
501 gfs2_alloc_put(dip);
502
503 gfs2_glock_dq_uninit_m(2, ghs);
504
505 d_instantiate(dentry, inode);
506 mark_inode_dirty(inode);
507
508 return 0;
509}
510
511/**
512 * gfs2_rmdiri - Remove a directory
513 * @dip: The parent directory of the directory to be removed
514 * @name: The name of the directory to be removed
515 * @ip: The GFS2 inode of the directory to be removed
516 *
517 * Assumes Glocks on dip and ip are held
518 *
519 * Returns: errno
520 */
521
522static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
523 struct gfs2_inode *ip)
524{
525 struct qstr dotname;
526 int error;
527
528 if (ip->i_entries != 2) {
529 if (gfs2_consist_inode(ip))
530 gfs2_dinode_print(ip);
531 return -EIO;
532 }
533
534 error = gfs2_dir_del(dip, name);
535 if (error)
536 return error;
537
538 error = gfs2_change_nlink(dip, -1);
539 if (error)
540 return error;
541
542 gfs2_str2qstr(&dotname, ".");
543 error = gfs2_dir_del(ip, &dotname);
544 if (error)
545 return error;
546
547 gfs2_str2qstr(&dotname, "..");
548 error = gfs2_dir_del(ip, &dotname);
549 if (error)
550 return error;
551
552 /* It looks odd, but it really should be done twice */
553 error = gfs2_change_nlink(ip, -1);
554 if (error)
555 return error;
556
557 error = gfs2_change_nlink(ip, -1);
558 if (error)
559 return error;
560
561 return error;
562}
563
564/**
565 * gfs2_rmdir - Remove a directory
566 * @dir: The parent directory of the directory to be removed
567 * @dentry: The dentry of the directory to remove
568 *
569 * Remove a directory. Call gfs2_rmdiri()
570 *
571 * Returns: errno
572 */
573
574static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
575{
576 struct gfs2_inode *dip = GFS2_I(dir);
577 struct gfs2_sbd *sdp = GFS2_SB(dir);
578 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
579 struct gfs2_holder ghs[3];
580 struct gfs2_rgrpd *rgd;
581 struct gfs2_holder ri_gh;
582 int error;
583
584 error = gfs2_rindex_hold(sdp, &ri_gh);
585 if (error)
586 return error;
587 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
588 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
589
590 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
591 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
592
593 error = gfs2_glock_nq(ghs); /* parent */
594 if (error)
595 goto out_parent;
596
597 error = gfs2_glock_nq(ghs + 1); /* child */
598 if (error)
599 goto out_child;
600
601 error = gfs2_glock_nq(ghs + 2); /* rgrp */
602 if (error)
603 goto out_rgrp;
604
605 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
606 if (error)
607 goto out_gunlock;
608
609 if (ip->i_entries < 2) {
610 if (gfs2_consist_inode(ip))
611 gfs2_dinode_print(ip);
612 error = -EIO;
613 goto out_gunlock;
614 }
615 if (ip->i_entries > 2) {
616 error = -ENOTEMPTY;
617 goto out_gunlock;
618 }
619
620 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
621 if (error)
622 goto out_gunlock;
623
624 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
625
626 gfs2_trans_end(sdp);
627
628out_gunlock:
629 gfs2_glock_dq(ghs + 2);
630out_rgrp:
631 gfs2_holder_uninit(ghs + 2);
632 gfs2_glock_dq(ghs + 1);
633out_child:
634 gfs2_holder_uninit(ghs + 1);
635 gfs2_glock_dq(ghs);
636out_parent:
637 gfs2_holder_uninit(ghs);
638 gfs2_glock_dq_uninit(&ri_gh);
639 return error;
640}
641
642/**
643 * gfs2_mknod - Make a special file
644 * @dir: The directory in which the special file will reside
645 * @dentry: The dentry of the special file
646 * @mode: The mode of the special file
647 * @rdev: The device specification of the special file
648 *
649 */
650
651static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
652 dev_t dev)
653{
654 struct gfs2_inode *dip = GFS2_I(dir);
655 struct gfs2_sbd *sdp = GFS2_SB(dir);
656 struct gfs2_holder ghs[2];
657 struct inode *inode;
658
659 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
660
661 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
662 if (IS_ERR(inode)) {
663 gfs2_holder_uninit(ghs);
664 return PTR_ERR(inode);
665 }
666
667 gfs2_trans_end(sdp);
668 if (dip->i_alloc->al_rgd)
669 gfs2_inplace_release(dip);
670 gfs2_quota_unlock(dip);
671 gfs2_alloc_put(dip);
672
673 gfs2_glock_dq_uninit_m(2, ghs);
674
675 d_instantiate(dentry, inode);
676 mark_inode_dirty(inode);
677
678 return 0;
679}
680
681/*
682 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
683 * @this: move this
684 * @to: to here
685 *
686 * Follow @to back to the root and make sure we don't encounter @this
687 * Assumes we already hold the rename lock.
688 *
689 * Returns: errno
690 */
691
692static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
693{
694 struct inode *dir = &to->i_inode;
695 struct super_block *sb = dir->i_sb;
696 struct inode *tmp;
697 struct qstr dotdot;
698 int error = 0;
699
700 gfs2_str2qstr(&dotdot, "..");
701
702 igrab(dir);
703
704 for (;;) {
705 if (dir == &this->i_inode) {
706 error = -EINVAL;
707 break;
708 }
709 if (dir == sb->s_root->d_inode) {
710 error = 0;
711 break;
712 }
713
714 tmp = gfs2_lookupi(dir, &dotdot, 1);
715 if (IS_ERR(tmp)) {
716 error = PTR_ERR(tmp);
717 break;
718 }
719
720 iput(dir);
721 dir = tmp;
722 }
723
724 iput(dir);
725
726 return error;
727}
728
729/**
730 * gfs2_rename - Rename a file
731 * @odir: Parent directory of old file name
732 * @odentry: The old dentry of the file
733 * @ndir: Parent directory of new file name
734 * @ndentry: The new dentry of the file
735 *
736 * Returns: errno
737 */
738
739static int gfs2_rename(struct inode *odir, struct dentry *odentry,
740 struct inode *ndir, struct dentry *ndentry)
741{
742 struct gfs2_inode *odip = GFS2_I(odir);
743 struct gfs2_inode *ndip = GFS2_I(ndir);
744 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
745 struct gfs2_inode *nip = NULL;
746 struct gfs2_sbd *sdp = GFS2_SB(odir);
747 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
748 struct gfs2_rgrpd *nrgd;
749 unsigned int num_gh;
750 int dir_rename = 0;
751 int alloc_required = 0;
752 unsigned int x;
753 int error;
754
755 if (ndentry->d_inode) {
756 nip = GFS2_I(ndentry->d_inode);
757 if (ip == nip)
758 return 0;
759 }
760
761
762 if (odip != ndip) {
763 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
764 0, &r_gh);
765 if (error)
766 goto out;
767
768 if (S_ISDIR(ip->i_inode.i_mode)) {
769 dir_rename = 1;
770 /* don't move a dirctory into it's subdir */
771 error = gfs2_ok_to_move(ip, ndip);
772 if (error)
773 goto out_gunlock_r;
774 }
775 }
776
777 num_gh = 1;
778 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
779 if (odip != ndip) {
780 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
781 num_gh++;
782 }
783 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 num_gh++;
785
786 if (nip) {
787 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
788 num_gh++;
789 /* grab the resource lock for unlink flag twiddling
790 * this is the case of the target file already existing
791 * so we unlink before doing the rename
792 */
793 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
794 if (nrgd)
795 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
796 }
797
798 for (x = 0; x < num_gh; x++) {
799 error = gfs2_glock_nq(ghs + x);
800 if (error)
801 goto out_gunlock;
802 }
803
804 /* Check out the old directory */
805
806 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
807 if (error)
808 goto out_gunlock;
809
810 /* Check out the new directory */
811
812 if (nip) {
813 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
814 if (error)
815 goto out_gunlock;
816
817 if (S_ISDIR(nip->i_inode.i_mode)) {
818 if (nip->i_entries < 2) {
819 if (gfs2_consist_inode(nip))
820 gfs2_dinode_print(nip);
821 error = -EIO;
822 goto out_gunlock;
823 }
824 if (nip->i_entries > 2) {
825 error = -ENOTEMPTY;
826 goto out_gunlock;
827 }
828 }
829 } else {
830 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
831 if (error)
832 goto out_gunlock;
833
834 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
835 switch (error) {
836 case -ENOENT:
837 error = 0;
838 break;
839 case 0:
840 error = -EEXIST;
841 default:
842 goto out_gunlock;
843 };
844
845 if (odip != ndip) {
846 if (!ndip->i_inode.i_nlink) {
847 error = -EINVAL;
848 goto out_gunlock;
849 }
850 if (ndip->i_entries == (u32)-1) {
851 error = -EFBIG;
852 goto out_gunlock;
853 }
854 if (S_ISDIR(ip->i_inode.i_mode) &&
855 ndip->i_inode.i_nlink == (u32)-1) {
856 error = -EMLINK;
857 goto out_gunlock;
858 }
859 }
860 }
861
862 /* Check out the dir to be renamed */
863
864 if (dir_rename) {
865 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
866 if (error)
867 goto out_gunlock;
868 }
869
870 if (nip == NULL)
871 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
872 error = alloc_required;
873 if (error < 0)
874 goto out_gunlock;
875 error = 0;
876
877 if (alloc_required) {
878 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
879 if (!al) {
880 error = -ENOMEM;
881 goto out_gunlock;
882 }
883
884 error = gfs2_quota_lock_check(ndip);
885 if (error)
886 goto out_alloc;
887
888 al->al_requested = sdp->sd_max_dirres;
889
890 error = gfs2_inplace_reserve(ndip);
891 if (error)
892 goto out_gunlock_q;
893
894 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
895 al->al_rgd->rd_length +
896 4 * RES_DINODE + 4 * RES_LEAF +
897 RES_STATFS + RES_QUOTA + 4, 0);
898 if (error)
899 goto out_ipreserv;
900 } else {
901 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
902 5 * RES_LEAF + 4, 0);
903 if (error)
904 goto out_gunlock;
905 }
906
907 /* Remove the target file, if it exists */
908
909 if (nip) {
910 if (S_ISDIR(nip->i_inode.i_mode))
911 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
912 else {
913 error = gfs2_dir_del(ndip, &ndentry->d_name);
914 if (error)
915 goto out_end_trans;
916 error = gfs2_change_nlink(nip, -1);
917 }
918 if (error)
919 goto out_end_trans;
920 }
921
922 if (dir_rename) {
923 struct qstr name;
924 gfs2_str2qstr(&name, "..");
925
926 error = gfs2_change_nlink(ndip, +1);
927 if (error)
928 goto out_end_trans;
929 error = gfs2_change_nlink(odip, -1);
930 if (error)
931 goto out_end_trans;
932
933 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
934 if (error)
935 goto out_end_trans;
936 } else {
937 struct buffer_head *dibh;
938 error = gfs2_meta_inode_buffer(ip, &dibh);
939 if (error)
940 goto out_end_trans;
941 ip->i_inode.i_ctime = CURRENT_TIME;
942 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
943 gfs2_dinode_out(ip, dibh->b_data);
944 brelse(dibh);
945 }
946
947 error = gfs2_dir_del(odip, &odentry->d_name);
948 if (error)
949 goto out_end_trans;
950
951 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
952 if (error)
953 goto out_end_trans;
954
955out_end_trans:
956 gfs2_trans_end(sdp);
957out_ipreserv:
958 if (alloc_required)
959 gfs2_inplace_release(ndip);
960out_gunlock_q:
961 if (alloc_required)
962 gfs2_quota_unlock(ndip);
963out_alloc:
964 if (alloc_required)
965 gfs2_alloc_put(ndip);
966out_gunlock:
967 while (x--) {
968 gfs2_glock_dq(ghs + x);
969 gfs2_holder_uninit(ghs + x);
970 }
971out_gunlock_r:
972 if (r_gh.gh_gl)
973 gfs2_glock_dq_uninit(&r_gh);
974out:
975 return error;
976}
977
978/**
979 * gfs2_follow_link - Follow a symbolic link
980 * @dentry: The dentry of the link
981 * @nd: Data that we pass to vfs_follow_link()
982 *
983 * This can handle symlinks of any size.
984 *
985 * Returns: 0 on success or error code
986 */
987
988static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
989{
990 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
991 struct gfs2_holder i_gh;
992 struct buffer_head *dibh;
993 unsigned int x;
994 char *buf;
995 int error;
996
997 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
998 error = gfs2_glock_nq(&i_gh);
999 if (error) {
1000 gfs2_holder_uninit(&i_gh);
1001 nd_set_link(nd, ERR_PTR(error));
1002 return NULL;
1003 }
1004
1005 if (!ip->i_disksize) {
1006 gfs2_consist_inode(ip);
1007 buf = ERR_PTR(-EIO);
1008 goto out;
1009 }
1010
1011 error = gfs2_meta_inode_buffer(ip, &dibh);
1012 if (error) {
1013 buf = ERR_PTR(error);
1014 goto out;
1015 }
1016
1017 x = ip->i_disksize + 1;
1018 buf = kmalloc(x, GFP_NOFS);
1019 if (!buf)
1020 buf = ERR_PTR(-ENOMEM);
1021 else
1022 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1023 brelse(dibh);
1024out:
1025 gfs2_glock_dq_uninit(&i_gh);
1026 nd_set_link(nd, buf);
1027 return NULL;
1028}
1029
1030static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1031{
1032 char *s = nd_get_link(nd);
1033 if (!IS_ERR(s))
1034 kfree(s);
1035}
1036
1037/**
1038 * gfs2_permission -
1039 * @inode:
1040 * @mask:
1041 * @nd: passed from Linux VFS, ignored by us
1042 *
1043 * This may be called from the VFS directly, or from within GFS2 with the
1044 * inode locked, so we look to see if the glock is already locked and only
1045 * lock the glock if its not already been done.
1046 *
1047 * Returns: errno
1048 */
1049
1050int gfs2_permission(struct inode *inode, int mask)
1051{
1052 struct gfs2_inode *ip = GFS2_I(inode);
1053 struct gfs2_holder i_gh;
1054 int error;
1055 int unlock = 0;
1056
1057 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1058 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1059 if (error)
1060 return error;
1061 unlock = 1;
1062 }
1063
1064 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1065 error = -EACCES;
1066 else
1067 error = generic_permission(inode, mask, gfs2_check_acl);
1068 if (unlock)
1069 gfs2_glock_dq_uninit(&i_gh);
1070
1071 return error;
1072}
1073
1074/*
1075 * XXX(truncate): the truncate_setsize calls should be moved to the end.
1076 */
1077static int setattr_size(struct inode *inode, struct iattr *attr)
1078{
1079 struct gfs2_inode *ip = GFS2_I(inode);
1080 struct gfs2_sbd *sdp = GFS2_SB(inode);
1081 int error;
1082
1083 if (attr->ia_size != ip->i_disksize) {
1084 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1085 if (error)
1086 return error;
1087 truncate_setsize(inode, attr->ia_size);
1088 gfs2_trans_end(sdp);
1089 }
1090
1091 error = gfs2_truncatei(ip, attr->ia_size);
1092 if (error && (inode->i_size != ip->i_disksize))
1093 i_size_write(inode, ip->i_disksize);
1094
1095 return error;
1096}
1097
1098static int setattr_chown(struct inode *inode, struct iattr *attr)
1099{
1100 struct gfs2_inode *ip = GFS2_I(inode);
1101 struct gfs2_sbd *sdp = GFS2_SB(inode);
1102 struct buffer_head *dibh;
1103 u32 ouid, ogid, nuid, ngid;
1104 int error;
1105
1106 ouid = inode->i_uid;
1107 ogid = inode->i_gid;
1108 nuid = attr->ia_uid;
1109 ngid = attr->ia_gid;
1110
1111 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1112 ouid = nuid = NO_QUOTA_CHANGE;
1113 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1114 ogid = ngid = NO_QUOTA_CHANGE;
1115
1116 if (!gfs2_alloc_get(ip))
1117 return -ENOMEM;
1118
1119 error = gfs2_quota_lock(ip, nuid, ngid);
1120 if (error)
1121 goto out_alloc;
1122
1123 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1124 error = gfs2_quota_check(ip, nuid, ngid);
1125 if (error)
1126 goto out_gunlock_q;
1127 }
1128
1129 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1130 if (error)
1131 goto out_gunlock_q;
1132
1133 error = gfs2_meta_inode_buffer(ip, &dibh);
1134 if (error)
1135 goto out_end_trans;
1136
1137 if ((attr->ia_valid & ATTR_SIZE) &&
1138 attr->ia_size != i_size_read(inode)) {
1139 int error;
1140
1141 error = vmtruncate(inode, attr->ia_size);
1142 gfs2_assert_warn(sdp, !error);
1143 }
1144
1145 setattr_copy(inode, attr);
1146 mark_inode_dirty(inode);
1147
1148 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1149 gfs2_dinode_out(ip, dibh->b_data);
1150 brelse(dibh);
1151
1152 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1153 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1154 gfs2_quota_change(ip, -blocks, ouid, ogid);
1155 gfs2_quota_change(ip, blocks, nuid, ngid);
1156 }
1157
1158out_end_trans:
1159 gfs2_trans_end(sdp);
1160out_gunlock_q:
1161 gfs2_quota_unlock(ip);
1162out_alloc:
1163 gfs2_alloc_put(ip);
1164 return error;
1165}
1166
1167/**
1168 * gfs2_setattr - Change attributes on an inode
1169 * @dentry: The dentry which is changing
1170 * @attr: The structure describing the change
1171 *
1172 * The VFS layer wants to change one or more of an inodes attributes. Write
1173 * that change out to disk.
1174 *
1175 * Returns: errno
1176 */
1177
1178static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1179{
1180 struct inode *inode = dentry->d_inode;
1181 struct gfs2_inode *ip = GFS2_I(inode);
1182 struct gfs2_holder i_gh;
1183 int error;
1184
1185 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1186 if (error)
1187 return error;
1188
1189 error = -EPERM;
1190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1191 goto out;
1192
1193 error = inode_change_ok(inode, attr);
1194 if (error)
1195 goto out;
1196
1197 if (attr->ia_valid & ATTR_SIZE)
1198 error = setattr_size(inode, attr);
1199 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1200 error = setattr_chown(inode, attr);
1201 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1202 error = gfs2_acl_chmod(ip, attr);
1203 else
1204 error = gfs2_setattr_simple(ip, attr);
1205
1206out:
1207 gfs2_glock_dq_uninit(&i_gh);
1208 if (!error)
1209 mark_inode_dirty(inode);
1210 return error;
1211}
1212
1213/**
1214 * gfs2_getattr - Read out an inode's attributes
1215 * @mnt: The vfsmount the inode is being accessed from
1216 * @dentry: The dentry to stat
1217 * @stat: The inode's stats
1218 *
1219 * This may be called from the VFS directly, or from within GFS2 with the
1220 * inode locked, so we look to see if the glock is already locked and only
1221 * lock the glock if its not already been done. Note that its the NFS
1222 * readdirplus operation which causes this to be called (from filldir)
1223 * with the glock already held.
1224 *
1225 * Returns: errno
1226 */
1227
1228static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1229 struct kstat *stat)
1230{
1231 struct inode *inode = dentry->d_inode;
1232 struct gfs2_inode *ip = GFS2_I(inode);
1233 struct gfs2_holder gh;
1234 int error;
1235 int unlock = 0;
1236
1237 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1238 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1239 if (error)
1240 return error;
1241 unlock = 1;
1242 }
1243
1244 generic_fillattr(inode, stat);
1245 if (unlock)
1246 gfs2_glock_dq_uninit(&gh);
1247
1248 return 0;
1249}
1250
1251static int gfs2_setxattr(struct dentry *dentry, const char *name,
1252 const void *data, size_t size, int flags)
1253{
1254 struct inode *inode = dentry->d_inode;
1255 struct gfs2_inode *ip = GFS2_I(inode);
1256 struct gfs2_holder gh;
1257 int ret;
1258
1259 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1260 ret = gfs2_glock_nq(&gh);
1261 if (ret == 0) {
1262 ret = generic_setxattr(dentry, name, data, size, flags);
1263 gfs2_glock_dq(&gh);
1264 }
1265 gfs2_holder_uninit(&gh);
1266 return ret;
1267}
1268
1269static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1270 void *data, size_t size)
1271{
1272 struct inode *inode = dentry->d_inode;
1273 struct gfs2_inode *ip = GFS2_I(inode);
1274 struct gfs2_holder gh;
1275 int ret;
1276
1277 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1278 ret = gfs2_glock_nq(&gh);
1279 if (ret == 0) {
1280 ret = generic_getxattr(dentry, name, data, size);
1281 gfs2_glock_dq(&gh);
1282 }
1283 gfs2_holder_uninit(&gh);
1284 return ret;
1285}
1286
1287static int gfs2_removexattr(struct dentry *dentry, const char *name)
1288{
1289 struct inode *inode = dentry->d_inode;
1290 struct gfs2_inode *ip = GFS2_I(inode);
1291 struct gfs2_holder gh;
1292 int ret;
1293
1294 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1295 ret = gfs2_glock_nq(&gh);
1296 if (ret == 0) {
1297 ret = generic_removexattr(dentry, name);
1298 gfs2_glock_dq(&gh);
1299 }
1300 gfs2_holder_uninit(&gh);
1301 return ret;
1302}
1303
1304static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1305 u64 start, u64 len)
1306{
1307 struct gfs2_inode *ip = GFS2_I(inode);
1308 struct gfs2_holder gh;
1309 int ret;
1310
1311 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1312 if (ret)
1313 return ret;
1314
1315 mutex_lock(&inode->i_mutex);
1316
1317 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1318 if (ret)
1319 goto out;
1320
1321 if (gfs2_is_stuffed(ip)) {
1322 u64 phys = ip->i_no_addr << inode->i_blkbits;
1323 u64 size = i_size_read(inode);
1324 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1325 FIEMAP_EXTENT_DATA_INLINE;
1326 phys += sizeof(struct gfs2_dinode);
1327 phys += start;
1328 if (start + len > size)
1329 len = size - start;
1330 if (start < size)
1331 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1332 len, flags);
1333 if (ret == 1)
1334 ret = 0;
1335 } else {
1336 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1337 gfs2_block_map);
1338 }
1339
1340 gfs2_glock_dq_uninit(&gh);
1341out:
1342 mutex_unlock(&inode->i_mutex);
1343 return ret;
1344}
1345
1346const struct inode_operations gfs2_file_iops = {
1347 .permission = gfs2_permission,
1348 .setattr = gfs2_setattr,
1349 .getattr = gfs2_getattr,
1350 .setxattr = gfs2_setxattr,
1351 .getxattr = gfs2_getxattr,
1352 .listxattr = gfs2_listxattr,
1353 .removexattr = gfs2_removexattr,
1354 .fiemap = gfs2_fiemap,
1355};
1356
1357const struct inode_operations gfs2_dir_iops = {
1358 .create = gfs2_create,
1359 .lookup = gfs2_lookup,
1360 .link = gfs2_link,
1361 .unlink = gfs2_unlink,
1362 .symlink = gfs2_symlink,
1363 .mkdir = gfs2_mkdir,
1364 .rmdir = gfs2_rmdir,
1365 .mknod = gfs2_mknod,
1366 .rename = gfs2_rename,
1367 .permission = gfs2_permission,
1368 .setattr = gfs2_setattr,
1369 .getattr = gfs2_getattr,
1370 .setxattr = gfs2_setxattr,
1371 .getxattr = gfs2_getxattr,
1372 .listxattr = gfs2_listxattr,
1373 .removexattr = gfs2_removexattr,
1374 .fiemap = gfs2_fiemap,
1375};
1376
1377const struct inode_operations gfs2_symlink_iops = {
1378 .readlink = generic_readlink,
1379 .follow_link = gfs2_follow_link,
1380 .put_link = gfs2_put_link,
1381 .permission = gfs2_permission,
1382 .setattr = gfs2_setattr,
1383 .getattr = gfs2_getattr,
1384 .setxattr = gfs2_setxattr,
1385 .getxattr = gfs2_getxattr,
1386 .listxattr = gfs2_listxattr,
1387 .removexattr = gfs2_removexattr,
1388 .fiemap = gfs2_fiemap,
1389};
1390
diff --git a/fs/gfs2/quota.c b/fs/gfs2/quota.c
index 1bc6b5695e6d..42e8d23bc047 100644
--- a/fs/gfs2/quota.c
+++ b/fs/gfs2/quota.c
@@ -38,6 +38,7 @@
38 38
39#include <linux/sched.h> 39#include <linux/sched.h>
40#include <linux/slab.h> 40#include <linux/slab.h>
41#include <linux/mm.h>
41#include <linux/spinlock.h> 42#include <linux/spinlock.h>
42#include <linux/completion.h> 43#include <linux/completion.h>
43#include <linux/buffer_head.h> 44#include <linux/buffer_head.h>
@@ -77,19 +78,20 @@ static LIST_HEAD(qd_lru_list);
77static atomic_t qd_lru_count = ATOMIC_INIT(0); 78static atomic_t qd_lru_count = ATOMIC_INIT(0);
78static DEFINE_SPINLOCK(qd_lru_lock); 79static DEFINE_SPINLOCK(qd_lru_lock);
79 80
80int gfs2_shrink_qd_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask) 81int gfs2_shrink_qd_memory(struct shrinker *shrink, struct shrink_control *sc)
81{ 82{
82 struct gfs2_quota_data *qd; 83 struct gfs2_quota_data *qd;
83 struct gfs2_sbd *sdp; 84 struct gfs2_sbd *sdp;
85 int nr_to_scan = sc->nr_to_scan;
84 86
85 if (nr == 0) 87 if (nr_to_scan == 0)
86 goto out; 88 goto out;
87 89
88 if (!(gfp_mask & __GFP_FS)) 90 if (!(sc->gfp_mask & __GFP_FS))
89 return -1; 91 return -1;
90 92
91 spin_lock(&qd_lru_lock); 93 spin_lock(&qd_lru_lock);
92 while (nr && !list_empty(&qd_lru_list)) { 94 while (nr_to_scan && !list_empty(&qd_lru_list)) {
93 qd = list_entry(qd_lru_list.next, 95 qd = list_entry(qd_lru_list.next,
94 struct gfs2_quota_data, qd_reclaim); 96 struct gfs2_quota_data, qd_reclaim);
95 sdp = qd->qd_gl->gl_sbd; 97 sdp = qd->qd_gl->gl_sbd;
@@ -110,7 +112,7 @@ int gfs2_shrink_qd_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
110 spin_unlock(&qd_lru_lock); 112 spin_unlock(&qd_lru_lock);
111 kmem_cache_free(gfs2_quotad_cachep, qd); 113 kmem_cache_free(gfs2_quotad_cachep, qd);
112 spin_lock(&qd_lru_lock); 114 spin_lock(&qd_lru_lock);
113 nr--; 115 nr_to_scan--;
114 } 116 }
115 spin_unlock(&qd_lru_lock); 117 spin_unlock(&qd_lru_lock);
116 118
@@ -631,6 +633,7 @@ static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
631 struct fs_disk_quota *fdq) 633 struct fs_disk_quota *fdq)
632{ 634{
633 struct inode *inode = &ip->i_inode; 635 struct inode *inode = &ip->i_inode;
636 struct gfs2_sbd *sdp = GFS2_SB(inode);
634 struct address_space *mapping = inode->i_mapping; 637 struct address_space *mapping = inode->i_mapping;
635 unsigned long index = loc >> PAGE_CACHE_SHIFT; 638 unsigned long index = loc >> PAGE_CACHE_SHIFT;
636 unsigned offset = loc & (PAGE_CACHE_SIZE - 1); 639 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
@@ -658,13 +661,17 @@ static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
658 qd->qd_qb.qb_value = qp->qu_value; 661 qd->qd_qb.qb_value = qp->qu_value;
659 if (fdq) { 662 if (fdq) {
660 if (fdq->d_fieldmask & FS_DQ_BSOFT) { 663 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
661 qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit); 664 qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
662 qd->qd_qb.qb_warn = qp->qu_warn; 665 qd->qd_qb.qb_warn = qp->qu_warn;
663 } 666 }
664 if (fdq->d_fieldmask & FS_DQ_BHARD) { 667 if (fdq->d_fieldmask & FS_DQ_BHARD) {
665 qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit); 668 qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
666 qd->qd_qb.qb_limit = qp->qu_limit; 669 qd->qd_qb.qb_limit = qp->qu_limit;
667 } 670 }
671 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
672 qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
673 qd->qd_qb.qb_value = qp->qu_value;
674 }
668 } 675 }
669 676
670 /* Write the quota into the quota file on disk */ 677 /* Write the quota into the quota file on disk */
@@ -735,10 +742,8 @@ get_a_page:
735 goto out; 742 goto out;
736 743
737 size = loc + sizeof(struct gfs2_quota); 744 size = loc + sizeof(struct gfs2_quota);
738 if (size > inode->i_size) { 745 if (size > inode->i_size)
739 ip->i_disksize = size;
740 i_size_write(inode, size); 746 i_size_write(inode, size);
741 }
742 inode->i_mtime = inode->i_atime = CURRENT_TIME; 747 inode->i_mtime = inode->i_atime = CURRENT_TIME;
743 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 748 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
744 gfs2_dinode_out(ip, dibh->b_data); 749 gfs2_dinode_out(ip, dibh->b_data);
@@ -817,7 +822,7 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
817 goto out_alloc; 822 goto out_alloc;
818 823
819 if (nalloc) 824 if (nalloc)
820 blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS; 825 blocks += gfs2_rg_blocks(al) + nalloc * ind_blocks + RES_STATFS;
821 826
822 error = gfs2_trans_begin(sdp, blocks, 0); 827 error = gfs2_trans_begin(sdp, blocks, 0);
823 if (error) 828 if (error)
@@ -831,6 +836,7 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
831 goto out_end_trans; 836 goto out_end_trans;
832 837
833 do_qc(qd, -qd->qd_change_sync); 838 do_qc(qd, -qd->qd_change_sync);
839 set_bit(QDF_REFRESH, &qd->qd_flags);
834 } 840 }
835 841
836 error = 0; 842 error = 0;
@@ -926,6 +932,7 @@ int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
926{ 932{
927 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 933 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
928 struct gfs2_alloc *al = ip->i_alloc; 934 struct gfs2_alloc *al = ip->i_alloc;
935 struct gfs2_quota_data *qd;
929 unsigned int x; 936 unsigned int x;
930 int error = 0; 937 int error = 0;
931 938
@@ -939,7 +946,11 @@ int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
939 sort_qd, NULL); 946 sort_qd, NULL);
940 947
941 for (x = 0; x < al->al_qd_num; x++) { 948 for (x = 0; x < al->al_qd_num; x++) {
942 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]); 949 int force = NO_FORCE;
950 qd = al->al_qd[x];
951 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
952 force = FORCE;
953 error = do_glock(qd, force, &al->al_qd_ghs[x]);
943 if (error) 954 if (error)
944 break; 955 break;
945 } 956 }
@@ -1190,18 +1201,17 @@ static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *
1190int gfs2_quota_init(struct gfs2_sbd *sdp) 1201int gfs2_quota_init(struct gfs2_sbd *sdp)
1191{ 1202{
1192 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); 1203 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1193 unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; 1204 u64 size = i_size_read(sdp->sd_qc_inode);
1205 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
1194 unsigned int x, slot = 0; 1206 unsigned int x, slot = 0;
1195 unsigned int found = 0; 1207 unsigned int found = 0;
1196 u64 dblock; 1208 u64 dblock;
1197 u32 extlen = 0; 1209 u32 extlen = 0;
1198 int error; 1210 int error;
1199 1211
1200 if (!ip->i_disksize || ip->i_disksize > (64 << 20) || 1212 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
1201 ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
1202 gfs2_consist_inode(ip);
1203 return -EIO; 1213 return -EIO;
1204 } 1214
1205 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; 1215 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1206 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE); 1216 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1207 1217
@@ -1500,9 +1510,9 @@ static int gfs2_get_dqblk(struct super_block *sb, int type, qid_t id,
1500 fdq->d_version = FS_DQUOT_VERSION; 1510 fdq->d_version = FS_DQUOT_VERSION;
1501 fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA; 1511 fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
1502 fdq->d_id = id; 1512 fdq->d_id = id;
1503 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit); 1513 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1504 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn); 1514 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1505 fdq->d_bcount = be64_to_cpu(qlvb->qb_value); 1515 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
1506 1516
1507 gfs2_glock_dq_uninit(&q_gh); 1517 gfs2_glock_dq_uninit(&q_gh);
1508out: 1518out:
@@ -1511,7 +1521,7 @@ out:
1511} 1521}
1512 1522
1513/* GFS2 only supports a subset of the XFS fields */ 1523/* GFS2 only supports a subset of the XFS fields */
1514#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD) 1524#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
1515 1525
1516static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id, 1526static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
1517 struct fs_disk_quota *fdq) 1527 struct fs_disk_quota *fdq)
@@ -1569,16 +1579,24 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
1569 1579
1570 /* If nothing has changed, this is a no-op */ 1580 /* If nothing has changed, this is a no-op */
1571 if ((fdq->d_fieldmask & FS_DQ_BSOFT) && 1581 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
1572 (fdq->d_blk_softlimit == be64_to_cpu(qd->qd_qb.qb_warn))) 1582 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
1573 fdq->d_fieldmask ^= FS_DQ_BSOFT; 1583 fdq->d_fieldmask ^= FS_DQ_BSOFT;
1584
1574 if ((fdq->d_fieldmask & FS_DQ_BHARD) && 1585 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
1575 (fdq->d_blk_hardlimit == be64_to_cpu(qd->qd_qb.qb_limit))) 1586 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
1576 fdq->d_fieldmask ^= FS_DQ_BHARD; 1587 fdq->d_fieldmask ^= FS_DQ_BHARD;
1588
1589 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1590 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1591 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1592
1577 if (fdq->d_fieldmask == 0) 1593 if (fdq->d_fieldmask == 0)
1578 goto out_i; 1594 goto out_i;
1579 1595
1580 offset = qd2offset(qd); 1596 offset = qd2offset(qd);
1581 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota)); 1597 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
1598 if (gfs2_is_stuffed(ip))
1599 alloc_required = 1;
1582 if (alloc_required) { 1600 if (alloc_required) {
1583 al = gfs2_alloc_get(ip); 1601 al = gfs2_alloc_get(ip);
1584 if (al == NULL) 1602 if (al == NULL)
@@ -1589,9 +1607,12 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
1589 error = gfs2_inplace_reserve(ip); 1607 error = gfs2_inplace_reserve(ip);
1590 if (error) 1608 if (error)
1591 goto out_alloc; 1609 goto out_alloc;
1610 blocks += gfs2_rg_blocks(al);
1592 } 1611 }
1593 1612
1594 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 1, 0); 1613 /* Some quotas span block boundaries and can update two blocks,
1614 adding an extra block to the transaction to handle such quotas */
1615 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
1595 if (error) 1616 if (error)
1596 goto out_release; 1617 goto out_release;
1597 1618
@@ -1621,4 +1642,3 @@ const struct quotactl_ops gfs2_quotactl_ops = {
1621 .get_dqblk = gfs2_get_dqblk, 1642 .get_dqblk = gfs2_get_dqblk,
1622 .set_dqblk = gfs2_set_dqblk, 1643 .set_dqblk = gfs2_set_dqblk,
1623}; 1644};
1624
diff --git a/fs/gfs2/quota.h b/fs/gfs2/quota.h
index e7d236ca48bd..90bf1c302a98 100644
--- a/fs/gfs2/quota.h
+++ b/fs/gfs2/quota.h
@@ -12,6 +12,7 @@
12 12
13struct gfs2_inode; 13struct gfs2_inode;
14struct gfs2_sbd; 14struct gfs2_sbd;
15struct shrink_control;
15 16
16#define NO_QUOTA_CHANGE ((u32)-1) 17#define NO_QUOTA_CHANGE ((u32)-1)
17 18
@@ -51,7 +52,8 @@ static inline int gfs2_quota_lock_check(struct gfs2_inode *ip)
51 return ret; 52 return ret;
52} 53}
53 54
54extern int gfs2_shrink_qd_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask); 55extern int gfs2_shrink_qd_memory(struct shrinker *shrink,
56 struct shrink_control *sc);
55extern const struct quotactl_ops gfs2_quotactl_ops; 57extern const struct quotactl_ops gfs2_quotactl_ops;
56 58
57#endif /* __QUOTA_DOT_H__ */ 59#endif /* __QUOTA_DOT_H__ */
diff --git a/fs/gfs2/recovery.c b/fs/gfs2/recovery.c
index f7f89a94a5a4..f2a02edcac8f 100644
--- a/fs/gfs2/recovery.c
+++ b/fs/gfs2/recovery.c
@@ -455,11 +455,13 @@ void gfs2_recover_func(struct work_struct *work)
455 int ro = 0; 455 int ro = 0;
456 unsigned int pass; 456 unsigned int pass;
457 int error; 457 int error;
458 int jlocked = 0;
458 459
459 if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) { 460 if (sdp->sd_args.ar_spectator ||
461 (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) {
460 fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n", 462 fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
461 jd->jd_jid); 463 jd->jd_jid);
462 464 jlocked = 1;
463 /* Acquire the journal lock so we can do recovery */ 465 /* Acquire the journal lock so we can do recovery */
464 466
465 error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops, 467 error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
@@ -554,13 +556,12 @@ void gfs2_recover_func(struct work_struct *work)
554 jd->jd_jid, t); 556 jd->jd_jid, t);
555 } 557 }
556 558
557 if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
558 gfs2_glock_dq_uninit(&ji_gh);
559
560 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS); 559 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
561 560
562 if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) 561 if (jlocked) {
562 gfs2_glock_dq_uninit(&ji_gh);
563 gfs2_glock_dq_uninit(&j_gh); 563 gfs2_glock_dq_uninit(&j_gh);
564 }
564 565
565 fs_info(sdp, "jid=%u: Done\n", jd->jd_jid); 566 fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
566 goto done; 567 goto done;
@@ -568,7 +569,7 @@ void gfs2_recover_func(struct work_struct *work)
568fail_gunlock_tr: 569fail_gunlock_tr:
569 gfs2_glock_dq_uninit(&t_gh); 570 gfs2_glock_dq_uninit(&t_gh);
570fail_gunlock_ji: 571fail_gunlock_ji:
571 if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) { 572 if (jlocked) {
572 gfs2_glock_dq_uninit(&ji_gh); 573 gfs2_glock_dq_uninit(&ji_gh);
573fail_gunlock_j: 574fail_gunlock_j:
574 gfs2_glock_dq_uninit(&j_gh); 575 gfs2_glock_dq_uninit(&j_gh);
diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
index 171a744f8e45..9b780df3fd54 100644
--- a/fs/gfs2/rgrp.c
+++ b/fs/gfs2/rgrp.c
@@ -78,10 +78,11 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
78 78
79static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1, 79static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
80 unsigned char *buf2, unsigned int offset, 80 unsigned char *buf2, unsigned int offset,
81 unsigned int buflen, u32 block, 81 struct gfs2_bitmap *bi, u32 block,
82 unsigned char new_state) 82 unsigned char new_state)
83{ 83{
84 unsigned char *byte1, *byte2, *end, cur_state; 84 unsigned char *byte1, *byte2, *end, cur_state;
85 unsigned int buflen = bi->bi_len;
85 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; 86 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
86 87
87 byte1 = buf1 + offset + (block / GFS2_NBBY); 88 byte1 = buf1 + offset + (block / GFS2_NBBY);
@@ -92,6 +93,16 @@ static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
92 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK; 93 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
93 94
94 if (unlikely(!valid_change[new_state * 4 + cur_state])) { 95 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
96 printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, "
97 "new_state=%d\n",
98 (unsigned long long)block, cur_state, new_state);
99 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n",
100 (unsigned long long)rgd->rd_addr,
101 (unsigned long)bi->bi_start);
102 printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n",
103 (unsigned long)bi->bi_offset,
104 (unsigned long)bi->bi_len);
105 dump_stack();
95 gfs2_consist_rgrpd(rgd); 106 gfs2_consist_rgrpd(rgd);
96 return; 107 return;
97 } 108 }
@@ -381,6 +392,7 @@ static void clear_rgrpdi(struct gfs2_sbd *sdp)
381 392
382 if (gl) { 393 if (gl) {
383 gl->gl_object = NULL; 394 gl->gl_object = NULL;
395 gfs2_glock_add_to_lru(gl);
384 gfs2_glock_put(gl); 396 gfs2_glock_put(gl);
385 } 397 }
386 398
@@ -500,7 +512,7 @@ u64 gfs2_ri_total(struct gfs2_sbd *sdp)
500 for (rgrps = 0;; rgrps++) { 512 for (rgrps = 0;; rgrps++) {
501 loff_t pos = rgrps * sizeof(struct gfs2_rindex); 513 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
502 514
503 if (pos + sizeof(struct gfs2_rindex) >= ip->i_disksize) 515 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
504 break; 516 break;
505 error = gfs2_internal_read(ip, &ra_state, buf, &pos, 517 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
506 sizeof(struct gfs2_rindex)); 518 sizeof(struct gfs2_rindex));
@@ -583,12 +595,14 @@ static int read_rindex_entry(struct gfs2_inode *ip,
583 * Returns: 0 on successful update, error code otherwise 595 * Returns: 0 on successful update, error code otherwise
584 */ 596 */
585 597
586static int gfs2_ri_update(struct gfs2_inode *ip) 598int gfs2_ri_update(struct gfs2_inode *ip)
587{ 599{
588 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 600 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
589 struct inode *inode = &ip->i_inode; 601 struct inode *inode = &ip->i_inode;
590 struct file_ra_state ra_state; 602 struct file_ra_state ra_state;
591 u64 rgrp_count = ip->i_disksize; 603 u64 rgrp_count = i_size_read(inode);
604 struct gfs2_rgrpd *rgd;
605 unsigned int max_data = 0;
592 int error; 606 int error;
593 607
594 do_div(rgrp_count, sizeof(struct gfs2_rindex)); 608 do_div(rgrp_count, sizeof(struct gfs2_rindex));
@@ -603,40 +617,10 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
603 } 617 }
604 } 618 }
605 619
606 sdp->sd_rindex_uptodate = 1; 620 list_for_each_entry(rgd, &sdp->sd_rindex_list, rd_list)
607 return 0; 621 if (rgd->rd_data > max_data)
608} 622 max_data = rgd->rd_data;
609 623 sdp->sd_max_rg_data = max_data;
610/**
611 * gfs2_ri_update_special - Pull in a new resource index from the disk
612 *
613 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
614 * In this case we know that we don't have any resource groups in memory yet.
615 *
616 * @ip: pointer to the rindex inode
617 *
618 * Returns: 0 on successful update, error code otherwise
619 */
620static int gfs2_ri_update_special(struct gfs2_inode *ip)
621{
622 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
623 struct inode *inode = &ip->i_inode;
624 struct file_ra_state ra_state;
625 int error;
626
627 file_ra_state_init(&ra_state, inode->i_mapping);
628 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
629 /* Ignore partials */
630 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
631 ip->i_disksize)
632 break;
633 error = read_rindex_entry(ip, &ra_state);
634 if (error) {
635 clear_rgrpdi(sdp);
636 return error;
637 }
638 }
639
640 sdp->sd_rindex_uptodate = 1; 624 sdp->sd_rindex_uptodate = 1;
641 return 0; 625 return 0;
642} 626}
@@ -854,8 +838,7 @@ static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
854 if ((start + nr_sects) != blk) { 838 if ((start + nr_sects) != blk) {
855 rv = blkdev_issue_discard(bdev, start, 839 rv = blkdev_issue_discard(bdev, start,
856 nr_sects, GFP_NOFS, 840 nr_sects, GFP_NOFS,
857 BLKDEV_IFL_WAIT | 841 0);
858 BLKDEV_IFL_BARRIER);
859 if (rv) 842 if (rv)
860 goto fail; 843 goto fail;
861 nr_sects = 0; 844 nr_sects = 0;
@@ -869,8 +852,7 @@ start_new_extent:
869 } 852 }
870 } 853 }
871 if (nr_sects) { 854 if (nr_sects) {
872 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 855 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
873 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
874 if (rv) 856 if (rv)
875 goto fail; 857 goto fail;
876 } 858 }
@@ -953,17 +935,18 @@ static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
953 * The inode, if one has been found, in inode. 935 * The inode, if one has been found, in inode.
954 */ 936 */
955 937
956static u64 try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, 938static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
957 u64 skip)
958{ 939{
959 u32 goal = 0, block; 940 u32 goal = 0, block;
960 u64 no_addr; 941 u64 no_addr;
961 struct gfs2_sbd *sdp = rgd->rd_sbd; 942 struct gfs2_sbd *sdp = rgd->rd_sbd;
962 unsigned int n; 943 unsigned int n;
944 struct gfs2_glock *gl;
945 struct gfs2_inode *ip;
946 int error;
947 int found = 0;
963 948
964 for(;;) { 949 while (goal < rgd->rd_data) {
965 if (goal >= rgd->rd_data)
966 break;
967 down_write(&sdp->sd_log_flush_lock); 950 down_write(&sdp->sd_log_flush_lock);
968 n = 1; 951 n = 1;
969 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, 952 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
@@ -974,17 +957,38 @@ static u64 try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked,
974 /* rgblk_search can return a block < goal, so we need to 957 /* rgblk_search can return a block < goal, so we need to
975 keep it marching forward. */ 958 keep it marching forward. */
976 no_addr = block + rgd->rd_data0; 959 no_addr = block + rgd->rd_data0;
977 goal++; 960 goal = max(block + 1, goal + 1);
978 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked) 961 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
979 continue; 962 continue;
980 if (no_addr == skip) 963 if (no_addr == skip)
981 continue; 964 continue;
982 *last_unlinked = no_addr; 965 *last_unlinked = no_addr;
983 return no_addr; 966
967 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
968 if (error)
969 continue;
970
971 /* If the inode is already in cache, we can ignore it here
972 * because the existing inode disposal code will deal with
973 * it when all refs have gone away. Accessing gl_object like
974 * this is not safe in general. Here it is ok because we do
975 * not dereference the pointer, and we only need an approx
976 * answer to whether it is NULL or not.
977 */
978 ip = gl->gl_object;
979
980 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
981 gfs2_glock_put(gl);
982 else
983 found++;
984
985 /* Limit reclaim to sensible number of tasks */
986 if (found > NR_CPUS)
987 return;
984 } 988 }
985 989
986 rgd->rd_flags &= ~GFS2_RDF_CHECK; 990 rgd->rd_flags &= ~GFS2_RDF_CHECK;
987 return 0; 991 return;
988} 992}
989 993
990/** 994/**
@@ -1065,11 +1069,9 @@ static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1065 * Try to acquire rgrp in way which avoids contending with others. 1069 * Try to acquire rgrp in way which avoids contending with others.
1066 * 1070 *
1067 * Returns: errno 1071 * Returns: errno
1068 * unlinked: the block address of an unlinked block to be reclaimed
1069 */ 1072 */
1070 1073
1071static int get_local_rgrp(struct gfs2_inode *ip, u64 *unlinked, 1074static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1072 u64 *last_unlinked)
1073{ 1075{
1074 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1076 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1075 struct gfs2_rgrpd *rgd, *begin = NULL; 1077 struct gfs2_rgrpd *rgd, *begin = NULL;
@@ -1079,7 +1081,6 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *unlinked,
1079 int loops = 0; 1081 int loops = 0;
1080 int error, rg_locked; 1082 int error, rg_locked;
1081 1083
1082 *unlinked = 0;
1083 rgd = gfs2_blk2rgrpd(sdp, ip->i_goal); 1084 rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
1084 1085
1085 while (rgd) { 1086 while (rgd) {
@@ -1096,17 +1097,10 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *unlinked,
1096 case 0: 1097 case 0:
1097 if (try_rgrp_fit(rgd, al)) 1098 if (try_rgrp_fit(rgd, al))
1098 goto out; 1099 goto out;
1099 /* If the rg came in already locked, there's no 1100 if (rgd->rd_flags & GFS2_RDF_CHECK)
1100 way we can recover from a failed try_rgrp_unlink 1101 try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
1101 because that would require an iput which can only
1102 happen after the rgrp is unlocked. */
1103 if (!rg_locked && rgd->rd_flags & GFS2_RDF_CHECK)
1104 *unlinked = try_rgrp_unlink(rgd, last_unlinked,
1105 ip->i_no_addr);
1106 if (!rg_locked) 1102 if (!rg_locked)
1107 gfs2_glock_dq_uninit(&al->al_rgd_gh); 1103 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1108 if (*unlinked)
1109 return -EAGAIN;
1110 /* fall through */ 1104 /* fall through */
1111 case GLR_TRYFAILED: 1105 case GLR_TRYFAILED:
1112 rgd = recent_rgrp_next(rgd); 1106 rgd = recent_rgrp_next(rgd);
@@ -1135,13 +1129,10 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *unlinked,
1135 case 0: 1129 case 0:
1136 if (try_rgrp_fit(rgd, al)) 1130 if (try_rgrp_fit(rgd, al))
1137 goto out; 1131 goto out;
1138 if (!rg_locked && rgd->rd_flags & GFS2_RDF_CHECK) 1132 if (rgd->rd_flags & GFS2_RDF_CHECK)
1139 *unlinked = try_rgrp_unlink(rgd, last_unlinked, 1133 try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
1140 ip->i_no_addr);
1141 if (!rg_locked) 1134 if (!rg_locked)
1142 gfs2_glock_dq_uninit(&al->al_rgd_gh); 1135 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1143 if (*unlinked)
1144 return -EAGAIN;
1145 break; 1136 break;
1146 1137
1147 case GLR_TRYFAILED: 1138 case GLR_TRYFAILED:
@@ -1188,47 +1179,52 @@ out:
1188 * Returns: errno 1179 * Returns: errno
1189 */ 1180 */
1190 1181
1191int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line) 1182int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
1183 char *file, unsigned int line)
1192{ 1184{
1193 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1185 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1194 struct gfs2_alloc *al = ip->i_alloc; 1186 struct gfs2_alloc *al = ip->i_alloc;
1195 int error = 0; 1187 int error = 0;
1196 u64 last_unlinked = NO_BLOCK, unlinked; 1188 u64 last_unlinked = NO_BLOCK;
1189 int tries = 0;
1197 1190
1198 if (gfs2_assert_warn(sdp, al->al_requested)) 1191 if (gfs2_assert_warn(sdp, al->al_requested))
1199 return -EINVAL; 1192 return -EINVAL;
1200 1193
1201try_again: 1194 if (hold_rindex) {
1202 /* We need to hold the rindex unless the inode we're using is 1195 /* We need to hold the rindex unless the inode we're using is
1203 the rindex itself, in which case it's already held. */ 1196 the rindex itself, in which case it's already held. */
1204 if (ip != GFS2_I(sdp->sd_rindex))
1205 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1206 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1207 error = gfs2_ri_update_special(ip);
1208
1209 if (error)
1210 return error;
1211
1212 /* Find an rgrp suitable for allocation. If it encounters any unlinked
1213 dinodes along the way, error will equal -EAGAIN and unlinked will
1214 contains it block address. We then need to look up that inode and
1215 try to free it, and try the allocation again. */
1216 error = get_local_rgrp(ip, &unlinked, &last_unlinked);
1217 if (error) {
1218 if (ip != GFS2_I(sdp->sd_rindex)) 1197 if (ip != GFS2_I(sdp->sd_rindex))
1219 gfs2_glock_dq_uninit(&al->al_ri_gh); 1198 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1220 if (error != -EAGAIN) 1199 else if (!sdp->sd_rgrps) /* We may not have the rindex read
1200 in, so: */
1201 error = gfs2_ri_update(ip);
1202 if (error)
1221 return error; 1203 return error;
1204 }
1222 1205
1223 gfs2_process_unlinked_inode(ip->i_inode.i_sb, unlinked); 1206try_again:
1224 /* regardless of whether or not gfs2_process_unlinked_inode 1207 do {
1225 was successful, we don't want to repeat it again. */ 1208 error = get_local_rgrp(ip, &last_unlinked);
1226 last_unlinked = unlinked; 1209 /* If there is no space, flushing the log may release some */
1227 gfs2_log_flush(sdp, NULL); 1210 if (error) {
1228 error = 0; 1211 if (ip == GFS2_I(sdp->sd_rindex) &&
1212 !sdp->sd_rindex_uptodate) {
1213 error = gfs2_ri_update(ip);
1214 if (error)
1215 return error;
1216 goto try_again;
1217 }
1218 gfs2_log_flush(sdp, NULL);
1219 }
1220 } while (error && tries++ < 3);
1229 1221
1230 goto try_again; 1222 if (error) {
1223 if (hold_rindex && ip != GFS2_I(sdp->sd_rindex))
1224 gfs2_glock_dq_uninit(&al->al_ri_gh);
1225 return error;
1231 } 1226 }
1227
1232 /* no error, so we have the rgrp set in the inode's allocation. */ 1228 /* no error, so we have the rgrp set in the inode's allocation. */
1233 al->al_file = file; 1229 al->al_file = file;
1234 al->al_line = line; 1230 al->al_line = line;
@@ -1257,7 +1253,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
1257 al->al_rgd = NULL; 1253 al->al_rgd = NULL;
1258 if (al->al_rgd_gh.gh_gl) 1254 if (al->al_rgd_gh.gh_gl)
1259 gfs2_glock_dq_uninit(&al->al_rgd_gh); 1255 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1260 if (ip != GFS2_I(sdp->sd_rindex)) 1256 if (ip != GFS2_I(sdp->sd_rindex) && al->al_ri_gh.gh_gl)
1261 gfs2_glock_dq_uninit(&al->al_ri_gh); 1257 gfs2_glock_dq_uninit(&al->al_ri_gh);
1262} 1258}
1263 1259
@@ -1381,7 +1377,7 @@ skip:
1381 1377
1382 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); 1378 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1383 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset, 1379 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1384 bi->bi_len, blk, new_state); 1380 bi, blk, new_state);
1385 goal = blk; 1381 goal = blk;
1386 while (*n < elen) { 1382 while (*n < elen) {
1387 goal++; 1383 goal++;
@@ -1391,7 +1387,7 @@ skip:
1391 GFS2_BLKST_FREE) 1387 GFS2_BLKST_FREE)
1392 break; 1388 break;
1393 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset, 1389 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1394 bi->bi_len, goal, new_state); 1390 bi, goal, new_state);
1395 (*n)++; 1391 (*n)++;
1396 } 1392 }
1397out: 1393out:
@@ -1448,7 +1444,7 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1448 } 1444 }
1449 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); 1445 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1450 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset, 1446 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
1451 bi->bi_len, buf_blk, new_state); 1447 bi, buf_blk, new_state);
1452 } 1448 }
1453 1449
1454 return rgd; 1450 return rgd;
@@ -1496,11 +1492,19 @@ int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
1496 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1492 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1497 struct buffer_head *dibh; 1493 struct buffer_head *dibh;
1498 struct gfs2_alloc *al = ip->i_alloc; 1494 struct gfs2_alloc *al = ip->i_alloc;
1499 struct gfs2_rgrpd *rgd = al->al_rgd; 1495 struct gfs2_rgrpd *rgd;
1500 u32 goal, blk; 1496 u32 goal, blk;
1501 u64 block; 1497 u64 block;
1502 int error; 1498 int error;
1503 1499
1500 /* Only happens if there is a bug in gfs2, return something distinctive
1501 * to ensure that it is noticed.
1502 */
1503 if (al == NULL)
1504 return -ECANCELED;
1505
1506 rgd = al->al_rgd;
1507
1504 if (rgrp_contains_block(rgd, ip->i_goal)) 1508 if (rgrp_contains_block(rgd, ip->i_goal))
1505 goal = ip->i_goal - rgd->rd_data0; 1509 goal = ip->i_goal - rgd->rd_data0;
1506 else 1510 else
@@ -1610,7 +1614,7 @@ rgrp_error:
1610 * 1614 *
1611 */ 1615 */
1612 1616
1613void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen) 1617void __gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1614{ 1618{
1615 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1619 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1616 struct gfs2_rgrpd *rgd; 1620 struct gfs2_rgrpd *rgd;
@@ -1626,6 +1630,24 @@ void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1626 1630
1627 gfs2_trans_add_rg(rgd); 1631 gfs2_trans_add_rg(rgd);
1628 1632
1633 /* Directories keep their data in the metadata address space */
1634 if (ip->i_depth)
1635 gfs2_meta_wipe(ip, bstart, blen);
1636}
1637
1638/**
1639 * gfs2_free_data - free a contiguous run of data block(s)
1640 * @ip: the inode these blocks are being freed from
1641 * @bstart: first block of a run of contiguous blocks
1642 * @blen: the length of the block run
1643 *
1644 */
1645
1646void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1647{
1648 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1649
1650 __gfs2_free_data(ip, bstart, blen);
1629 gfs2_statfs_change(sdp, 0, +blen, 0); 1651 gfs2_statfs_change(sdp, 0, +blen, 0);
1630 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); 1652 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1631} 1653}
@@ -1638,7 +1660,7 @@ void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1638 * 1660 *
1639 */ 1661 */
1640 1662
1641void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen) 1663void __gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1642{ 1664{
1643 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1665 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1644 struct gfs2_rgrpd *rgd; 1666 struct gfs2_rgrpd *rgd;
@@ -1653,10 +1675,24 @@ void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1653 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); 1675 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1654 1676
1655 gfs2_trans_add_rg(rgd); 1677 gfs2_trans_add_rg(rgd);
1678 gfs2_meta_wipe(ip, bstart, blen);
1679}
1656 1680
1681/**
1682 * gfs2_free_meta - free a contiguous run of data block(s)
1683 * @ip: the inode these blocks are being freed from
1684 * @bstart: first block of a run of contiguous blocks
1685 * @blen: the length of the block run
1686 *
1687 */
1688
1689void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1690{
1691 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1692
1693 __gfs2_free_meta(ip, bstart, blen);
1657 gfs2_statfs_change(sdp, 0, +blen, 0); 1694 gfs2_statfs_change(sdp, 0, +blen, 0);
1658 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); 1695 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1659 gfs2_meta_wipe(ip, bstart, blen);
1660} 1696}
1661 1697
1662void gfs2_unlink_di(struct inode *inode) 1698void gfs2_unlink_di(struct inode *inode)
diff --git a/fs/gfs2/rgrp.h b/fs/gfs2/rgrp.h
index f07119d89557..a80e3034ac47 100644
--- a/fs/gfs2/rgrp.h
+++ b/fs/gfs2/rgrp.h
@@ -39,17 +39,22 @@ static inline void gfs2_alloc_put(struct gfs2_inode *ip)
39 ip->i_alloc = NULL; 39 ip->i_alloc = NULL;
40} 40}
41 41
42extern int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, 42extern int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
43 unsigned int line); 43 char *file, unsigned int line);
44#define gfs2_inplace_reserve(ip) \ 44#define gfs2_inplace_reserve(ip) \
45gfs2_inplace_reserve_i((ip), __FILE__, __LINE__) 45 gfs2_inplace_reserve_i((ip), 1, __FILE__, __LINE__)
46#define gfs2_inplace_reserve_ri(ip) \
47 gfs2_inplace_reserve_i((ip), 0, __FILE__, __LINE__)
46 48
47extern void gfs2_inplace_release(struct gfs2_inode *ip); 49extern void gfs2_inplace_release(struct gfs2_inode *ip);
48 50
51extern int gfs2_ri_update(struct gfs2_inode *ip);
49extern int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n); 52extern int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n);
50extern int gfs2_alloc_di(struct gfs2_inode *ip, u64 *bn, u64 *generation); 53extern int gfs2_alloc_di(struct gfs2_inode *ip, u64 *bn, u64 *generation);
51 54
55extern void __gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen);
52extern void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen); 56extern void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen);
57extern void __gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);
53extern void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen); 58extern void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);
54extern void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip); 59extern void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip);
55extern void gfs2_unlink_di(struct inode *inode); 60extern void gfs2_unlink_di(struct inode *inode);
diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index 77cb9f830ee4..fb0edf735483 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -23,6 +23,7 @@
23#include <linux/time.h> 23#include <linux/time.h>
24#include <linux/wait.h> 24#include <linux/wait.h>
25#include <linux/writeback.h> 25#include <linux/writeback.h>
26#include <linux/backing-dev.h>
26 27
27#include "gfs2.h" 28#include "gfs2.h"
28#include "incore.h" 29#include "incore.h"
@@ -85,6 +86,7 @@ static const match_table_t tokens = {
85 {Opt_locktable, "locktable=%s"}, 86 {Opt_locktable, "locktable=%s"},
86 {Opt_hostdata, "hostdata=%s"}, 87 {Opt_hostdata, "hostdata=%s"},
87 {Opt_spectator, "spectator"}, 88 {Opt_spectator, "spectator"},
89 {Opt_spectator, "norecovery"},
88 {Opt_ignore_local_fs, "ignore_local_fs"}, 90 {Opt_ignore_local_fs, "ignore_local_fs"},
89 {Opt_localflocks, "localflocks"}, 91 {Opt_localflocks, "localflocks"},
90 {Opt_localcaching, "localcaching"}, 92 {Opt_localcaching, "localcaching"},
@@ -159,13 +161,13 @@ int gfs2_mount_args(struct gfs2_args *args, char *options)
159 args->ar_spectator = 1; 161 args->ar_spectator = 1;
160 break; 162 break;
161 case Opt_ignore_local_fs: 163 case Opt_ignore_local_fs:
162 args->ar_ignore_local_fs = 1; 164 /* Retained for backwards compat only */
163 break; 165 break;
164 case Opt_localflocks: 166 case Opt_localflocks:
165 args->ar_localflocks = 1; 167 args->ar_localflocks = 1;
166 break; 168 break;
167 case Opt_localcaching: 169 case Opt_localcaching:
168 args->ar_localcaching = 1; 170 /* Retained for backwards compat only */
169 break; 171 break;
170 case Opt_debug: 172 case Opt_debug:
171 if (args->ar_errors == GFS2_ERRORS_PANIC) { 173 if (args->ar_errors == GFS2_ERRORS_PANIC) {
@@ -179,7 +181,7 @@ int gfs2_mount_args(struct gfs2_args *args, char *options)
179 args->ar_debug = 0; 181 args->ar_debug = 0;
180 break; 182 break;
181 case Opt_upgrade: 183 case Opt_upgrade:
182 args->ar_upgrade = 1; 184 /* Retained for backwards compat only */
183 break; 185 break;
184 case Opt_acl: 186 case Opt_acl:
185 args->ar_posix_acl = 1; 187 args->ar_posix_acl = 1;
@@ -342,15 +344,14 @@ int gfs2_jdesc_check(struct gfs2_jdesc *jd)
342{ 344{
343 struct gfs2_inode *ip = GFS2_I(jd->jd_inode); 345 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
344 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); 346 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
347 u64 size = i_size_read(jd->jd_inode);
345 348
346 if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) || 349 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, 1 << 30))
347 (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
348 gfs2_consist_inode(ip);
349 return -EIO; 350 return -EIO;
350 }
351 jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
352 351
353 if (gfs2_write_alloc_required(ip, 0, ip->i_disksize)) { 352 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
353
354 if (gfs2_write_alloc_required(ip, 0, size)) {
354 gfs2_consist_inode(ip); 355 gfs2_consist_inode(ip);
355 return -EIO; 356 return -EIO;
356 } 357 }
@@ -657,7 +658,7 @@ out:
657 * @sdp: the file system 658 * @sdp: the file system
658 * 659 *
659 * This function flushes data and meta data for all machines by 660 * This function flushes data and meta data for all machines by
660 * aquiring the transaction log exclusively. All journals are 661 * acquiring the transaction log exclusively. All journals are
661 * ensured to be in a clean state as well. 662 * ensured to be in a clean state as well.
662 * 663 *
663 * Returns: errno 664 * Returns: errno
@@ -700,11 +701,47 @@ void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
700 mutex_unlock(&sdp->sd_freeze_lock); 701 mutex_unlock(&sdp->sd_freeze_lock);
701} 702}
702 703
704void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
705{
706 struct gfs2_dinode *str = buf;
707
708 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
709 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
710 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
711 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
712 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
713 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
714 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
715 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
716 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
717 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
718 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
719 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
720 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
721 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
722
723 str->di_goal_meta = cpu_to_be64(ip->i_goal);
724 str->di_goal_data = cpu_to_be64(ip->i_goal);
725 str->di_generation = cpu_to_be64(ip->i_generation);
726
727 str->di_flags = cpu_to_be32(ip->i_diskflags);
728 str->di_height = cpu_to_be16(ip->i_height);
729 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
730 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
731 GFS2_FORMAT_DE : 0);
732 str->di_depth = cpu_to_be16(ip->i_depth);
733 str->di_entries = cpu_to_be32(ip->i_entries);
734
735 str->di_eattr = cpu_to_be64(ip->i_eattr);
736 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
737 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
738 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
739}
703 740
704/** 741/**
705 * gfs2_write_inode - Make sure the inode is stable on the disk 742 * gfs2_write_inode - Make sure the inode is stable on the disk
706 * @inode: The inode 743 * @inode: The inode
707 * @sync: synchronous write flag 744 * @wbc: The writeback control structure
708 * 745 *
709 * Returns: errno 746 * Returns: errno
710 */ 747 */
@@ -713,18 +750,24 @@ static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
713{ 750{
714 struct gfs2_inode *ip = GFS2_I(inode); 751 struct gfs2_inode *ip = GFS2_I(inode);
715 struct gfs2_sbd *sdp = GFS2_SB(inode); 752 struct gfs2_sbd *sdp = GFS2_SB(inode);
753 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
754 struct backing_dev_info *bdi = metamapping->backing_dev_info;
716 struct gfs2_holder gh; 755 struct gfs2_holder gh;
717 struct buffer_head *bh; 756 struct buffer_head *bh;
718 struct timespec atime; 757 struct timespec atime;
719 struct gfs2_dinode *di; 758 struct gfs2_dinode *di;
720 int ret = 0; 759 int ret = -EAGAIN;
760 int unlock_required = 0;
721 761
722 /* Check this is a "normal" inode, etc */ 762 /* Skip timestamp update, if this is from a memalloc */
723 if (current->flags & PF_MEMALLOC) 763 if (current->flags & PF_MEMALLOC)
724 return 0;
725 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
726 if (ret)
727 goto do_flush; 764 goto do_flush;
765 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
766 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
767 if (ret)
768 goto do_flush;
769 unlock_required = 1;
770 }
728 ret = gfs2_trans_begin(sdp, RES_DINODE, 0); 771 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
729 if (ret) 772 if (ret)
730 goto do_unlock; 773 goto do_unlock;
@@ -741,10 +784,18 @@ static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
741 } 784 }
742 gfs2_trans_end(sdp); 785 gfs2_trans_end(sdp);
743do_unlock: 786do_unlock:
744 gfs2_glock_dq_uninit(&gh); 787 if (unlock_required)
788 gfs2_glock_dq_uninit(&gh);
745do_flush: 789do_flush:
746 if (wbc->sync_mode == WB_SYNC_ALL) 790 if (wbc->sync_mode == WB_SYNC_ALL)
747 gfs2_log_flush(GFS2_SB(inode), ip->i_gl); 791 gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
792 filemap_fdatawrite(metamapping);
793 if (bdi->dirty_exceeded)
794 gfs2_ail1_flush(sdp, wbc);
795 if (!ret && (wbc->sync_mode == WB_SYNC_ALL))
796 ret = filemap_fdatawait(metamapping);
797 if (ret)
798 mark_inode_dirty_sync(inode);
748 return ret; 799 return ret;
749} 800}
750 801
@@ -857,7 +908,6 @@ restart:
857 gfs2_clear_rgrpd(sdp); 908 gfs2_clear_rgrpd(sdp);
858 gfs2_jindex_free(sdp); 909 gfs2_jindex_free(sdp);
859 /* Take apart glock structures and buffer lists */ 910 /* Take apart glock structures and buffer lists */
860 invalidate_inodes(sdp->sd_vfs);
861 gfs2_gl_hash_clear(sdp); 911 gfs2_gl_hash_clear(sdp);
862 /* Unmount the locking protocol */ 912 /* Unmount the locking protocol */
863 gfs2_lm_unmount(sdp); 913 gfs2_lm_unmount(sdp);
@@ -875,8 +925,9 @@ restart:
875 925
876static int gfs2_sync_fs(struct super_block *sb, int wait) 926static int gfs2_sync_fs(struct super_block *sb, int wait)
877{ 927{
878 if (wait && sb->s_fs_info) 928 struct gfs2_sbd *sdp = sb->s_fs_info;
879 gfs2_log_flush(sb->s_fs_info, NULL); 929 if (wait && sdp)
930 gfs2_log_flush(sdp, NULL);
880 return 0; 931 return 0;
881} 932}
882 933
@@ -1129,9 +1180,7 @@ static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
1129 1180
1130 /* Some flags must not be changed */ 1181 /* Some flags must not be changed */
1131 if (args_neq(&args, &sdp->sd_args, spectator) || 1182 if (args_neq(&args, &sdp->sd_args, spectator) ||
1132 args_neq(&args, &sdp->sd_args, ignore_local_fs) ||
1133 args_neq(&args, &sdp->sd_args, localflocks) || 1183 args_neq(&args, &sdp->sd_args, localflocks) ||
1134 args_neq(&args, &sdp->sd_args, localcaching) ||
1135 args_neq(&args, &sdp->sd_args, meta)) 1184 args_neq(&args, &sdp->sd_args, meta))
1136 return -EINVAL; 1185 return -EINVAL;
1137 1186
@@ -1234,16 +1283,10 @@ static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1234 seq_printf(s, ",hostdata=%s", args->ar_hostdata); 1283 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
1235 if (args->ar_spectator) 1284 if (args->ar_spectator)
1236 seq_printf(s, ",spectator"); 1285 seq_printf(s, ",spectator");
1237 if (args->ar_ignore_local_fs)
1238 seq_printf(s, ",ignore_local_fs");
1239 if (args->ar_localflocks) 1286 if (args->ar_localflocks)
1240 seq_printf(s, ",localflocks"); 1287 seq_printf(s, ",localflocks");
1241 if (args->ar_localcaching)
1242 seq_printf(s, ",localcaching");
1243 if (args->ar_debug) 1288 if (args->ar_debug)
1244 seq_printf(s, ",debug"); 1289 seq_printf(s, ",debug");
1245 if (args->ar_upgrade)
1246 seq_printf(s, ",upgrade");
1247 if (args->ar_posix_acl) 1290 if (args->ar_posix_acl)
1248 seq_printf(s, ",acl"); 1291 seq_printf(s, ",acl");
1249 if (args->ar_quota != GFS2_QUOTA_DEFAULT) { 1292 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
@@ -1317,7 +1360,92 @@ static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1317 return 0; 1360 return 0;
1318} 1361}
1319 1362
1320/* 1363static void gfs2_final_release_pages(struct gfs2_inode *ip)
1364{
1365 struct inode *inode = &ip->i_inode;
1366 struct gfs2_glock *gl = ip->i_gl;
1367
1368 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1369 truncate_inode_pages(&inode->i_data, 0);
1370
1371 if (atomic_read(&gl->gl_revokes) == 0) {
1372 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1373 clear_bit(GLF_DIRTY, &gl->gl_flags);
1374 }
1375}
1376
1377static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1378{
1379 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1380 struct gfs2_alloc *al;
1381 struct gfs2_rgrpd *rgd;
1382 int error;
1383
1384 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
1385 gfs2_consist_inode(ip);
1386 return -EIO;
1387 }
1388
1389 al = gfs2_alloc_get(ip);
1390 if (!al)
1391 return -ENOMEM;
1392
1393 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1394 if (error)
1395 goto out;
1396
1397 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1398 if (error)
1399 goto out_qs;
1400
1401 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
1402 if (!rgd) {
1403 gfs2_consist_inode(ip);
1404 error = -EIO;
1405 goto out_rindex_relse;
1406 }
1407
1408 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1409 &al->al_rgd_gh);
1410 if (error)
1411 goto out_rindex_relse;
1412
1413 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1414 sdp->sd_jdesc->jd_blocks);
1415 if (error)
1416 goto out_rg_gunlock;
1417
1418 gfs2_free_di(rgd, ip);
1419
1420 gfs2_final_release_pages(ip);
1421
1422 gfs2_trans_end(sdp);
1423
1424out_rg_gunlock:
1425 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1426out_rindex_relse:
1427 gfs2_glock_dq_uninit(&al->al_ri_gh);
1428out_qs:
1429 gfs2_quota_unhold(ip);
1430out:
1431 gfs2_alloc_put(ip);
1432 return error;
1433}
1434
1435/**
1436 * gfs2_evict_inode - Remove an inode from cache
1437 * @inode: The inode to evict
1438 *
1439 * There are three cases to consider:
1440 * 1. i_nlink == 0, we are final opener (and must deallocate)
1441 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1442 * 3. i_nlink > 0
1443 *
1444 * If the fs is read only, then we have to treat all cases as per #3
1445 * since we are unable to do any deallocation. The inode will be
1446 * deallocated by the next read/write node to attempt an allocation
1447 * in the same resource group
1448 *
1321 * We have to (at the moment) hold the inodes main lock to cover 1449 * We have to (at the moment) hold the inodes main lock to cover
1322 * the gap between unlocking the shared lock on the iopen lock and 1450 * the gap between unlocking the shared lock on the iopen lock and
1323 * taking the exclusive lock. I'd rather do a shared -> exclusive 1451 * taking the exclusive lock. I'd rather do a shared -> exclusive
@@ -1327,15 +1455,17 @@ static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1327 1455
1328static void gfs2_evict_inode(struct inode *inode) 1456static void gfs2_evict_inode(struct inode *inode)
1329{ 1457{
1330 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; 1458 struct super_block *sb = inode->i_sb;
1459 struct gfs2_sbd *sdp = sb->s_fs_info;
1331 struct gfs2_inode *ip = GFS2_I(inode); 1460 struct gfs2_inode *ip = GFS2_I(inode);
1332 struct gfs2_holder gh; 1461 struct gfs2_holder gh;
1333 int error; 1462 int error;
1334 1463
1335 if (inode->i_nlink) 1464 if (inode->i_nlink || (sb->s_flags & MS_RDONLY))
1336 goto out; 1465 goto out;
1337 1466
1338 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 1467 /* Must not read inode block until block type has been verified */
1468 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
1339 if (unlikely(error)) { 1469 if (unlikely(error)) {
1340 gfs2_glock_dq_uninit(&ip->i_iopen_gh); 1470 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1341 goto out; 1471 goto out;
@@ -1345,12 +1475,21 @@ static void gfs2_evict_inode(struct inode *inode)
1345 if (error) 1475 if (error)
1346 goto out_truncate; 1476 goto out_truncate;
1347 1477
1478 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1479 error = gfs2_inode_refresh(ip);
1480 if (error)
1481 goto out_truncate;
1482 }
1483
1484 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1348 gfs2_glock_dq_wait(&ip->i_iopen_gh); 1485 gfs2_glock_dq_wait(&ip->i_iopen_gh);
1349 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh); 1486 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
1350 error = gfs2_glock_nq(&ip->i_iopen_gh); 1487 error = gfs2_glock_nq(&ip->i_iopen_gh);
1351 if (error) 1488 if (error)
1352 goto out_truncate; 1489 goto out_truncate;
1353 1490
1491 /* Case 1 starts here */
1492
1354 if (S_ISDIR(inode->i_mode) && 1493 if (S_ISDIR(inode->i_mode) &&
1355 (ip->i_diskflags & GFS2_DIF_EXHASH)) { 1494 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1356 error = gfs2_dir_exhash_dealloc(ip); 1495 error = gfs2_dir_exhash_dealloc(ip);
@@ -1371,10 +1510,10 @@ static void gfs2_evict_inode(struct inode *inode)
1371 } 1510 }
1372 1511
1373 error = gfs2_dinode_dealloc(ip); 1512 error = gfs2_dinode_dealloc(ip);
1374 if (error) 1513 goto out_unlock;
1375 goto out_unlock;
1376 1514
1377out_truncate: 1515out_truncate:
1516 /* Case 2 starts here */
1378 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks); 1517 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1379 if (error) 1518 if (error)
1380 goto out_unlock; 1519 goto out_unlock;
@@ -1383,6 +1522,7 @@ out_truncate:
1383 gfs2_trans_end(sdp); 1522 gfs2_trans_end(sdp);
1384 1523
1385out_unlock: 1524out_unlock:
1525 /* Error path for case 1 */
1386 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) 1526 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
1387 gfs2_glock_dq(&ip->i_iopen_gh); 1527 gfs2_glock_dq(&ip->i_iopen_gh);
1388 gfs2_holder_uninit(&ip->i_iopen_gh); 1528 gfs2_holder_uninit(&ip->i_iopen_gh);
@@ -1390,10 +1530,12 @@ out_unlock:
1390 if (error && error != GLR_TRYFAILED && error != -EROFS) 1530 if (error && error != GLR_TRYFAILED && error != -EROFS)
1391 fs_warn(sdp, "gfs2_evict_inode: %d\n", error); 1531 fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
1392out: 1532out:
1533 /* Case 3 starts here */
1393 truncate_inode_pages(&inode->i_data, 0); 1534 truncate_inode_pages(&inode->i_data, 0);
1394 end_writeback(inode); 1535 end_writeback(inode);
1395 1536
1396 ip->i_gl->gl_object = NULL; 1537 ip->i_gl->gl_object = NULL;
1538 gfs2_glock_add_to_lru(ip->i_gl);
1397 gfs2_glock_put(ip->i_gl); 1539 gfs2_glock_put(ip->i_gl);
1398 ip->i_gl = NULL; 1540 ip->i_gl = NULL;
1399 if (ip->i_iopen_gh.gh_gl) { 1541 if (ip->i_iopen_gh.gh_gl) {
@@ -1414,11 +1556,18 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
1414 return &ip->i_inode; 1556 return &ip->i_inode;
1415} 1557}
1416 1558
1417static void gfs2_destroy_inode(struct inode *inode) 1559static void gfs2_i_callback(struct rcu_head *head)
1418{ 1560{
1561 struct inode *inode = container_of(head, struct inode, i_rcu);
1562 INIT_LIST_HEAD(&inode->i_dentry);
1419 kmem_cache_free(gfs2_inode_cachep, inode); 1563 kmem_cache_free(gfs2_inode_cachep, inode);
1420} 1564}
1421 1565
1566static void gfs2_destroy_inode(struct inode *inode)
1567{
1568 call_rcu(&inode->i_rcu, gfs2_i_callback);
1569}
1570
1422const struct super_operations gfs2_super_ops = { 1571const struct super_operations gfs2_super_ops = {
1423 .alloc_inode = gfs2_alloc_inode, 1572 .alloc_inode = gfs2_alloc_inode,
1424 .destroy_inode = gfs2_destroy_inode, 1573 .destroy_inode = gfs2_destroy_inode,
diff --git a/fs/gfs2/sys.c b/fs/gfs2/sys.c
index ccacffd2faaa..443cabcfcd23 100644
--- a/fs/gfs2/sys.c
+++ b/fs/gfs2/sys.c
@@ -81,7 +81,8 @@ static int gfs2_uuid_valid(const u8 *uuid)
81 81
82static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf) 82static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
83{ 83{
84 const u8 *uuid = sdp->sd_sb.sb_uuid; 84 struct super_block *s = sdp->sd_vfs;
85 const u8 *uuid = s->s_uuid;
85 buf[0] = '\0'; 86 buf[0] = '\0';
86 if (!gfs2_uuid_valid(uuid)) 87 if (!gfs2_uuid_valid(uuid))
87 return 0; 88 return 0;
@@ -230,7 +231,10 @@ static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len
230 231
231 if (gltype > LM_TYPE_JOURNAL) 232 if (gltype > LM_TYPE_JOURNAL)
232 return -EINVAL; 233 return -EINVAL;
233 glops = gfs2_glops_list[gltype]; 234 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_TRANS_LOCK)
235 glops = &gfs2_trans_glops;
236 else
237 glops = gfs2_glops_list[gltype];
234 if (glops == NULL) 238 if (glops == NULL)
235 return -EINVAL; 239 return -EINVAL;
236 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags)) 240 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
@@ -334,6 +338,9 @@ static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
334 rv = sscanf(buf, "%u", &first); 338 rv = sscanf(buf, "%u", &first);
335 if (rv != 1 || first > 1) 339 if (rv != 1 || first > 1)
336 return -EINVAL; 340 return -EINVAL;
341 rv = wait_for_completion_killable(&sdp->sd_locking_init);
342 if (rv)
343 return rv;
337 spin_lock(&sdp->sd_jindex_spin); 344 spin_lock(&sdp->sd_jindex_spin);
338 rv = -EBUSY; 345 rv = -EBUSY;
339 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0) 346 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
@@ -399,31 +406,34 @@ static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
399 406
400static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf) 407static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
401{ 408{
402 return sprintf(buf, "%u\n", sdp->sd_lockstruct.ls_jid); 409 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
403} 410}
404 411
405static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len) 412static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
406{ 413{
407 unsigned jid; 414 int jid;
408 int rv; 415 int rv;
409 416
410 rv = sscanf(buf, "%u", &jid); 417 rv = sscanf(buf, "%d", &jid);
411 if (rv != 1) 418 if (rv != 1)
412 return -EINVAL; 419 return -EINVAL;
413 420 rv = wait_for_completion_killable(&sdp->sd_locking_init);
421 if (rv)
422 return rv;
414 spin_lock(&sdp->sd_jindex_spin); 423 spin_lock(&sdp->sd_jindex_spin);
415 rv = -EINVAL; 424 rv = -EINVAL;
416 if (sdp->sd_args.ar_spectator)
417 goto out;
418 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL) 425 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
419 goto out; 426 goto out;
420 rv = -EBUSY; 427 rv = -EBUSY;
421 if (test_and_clear_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0) 428 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
422 goto out; 429 goto out;
430 rv = 0;
431 if (sdp->sd_args.ar_spectator && jid > 0)
432 rv = jid = -EINVAL;
423 sdp->sd_lockstruct.ls_jid = jid; 433 sdp->sd_lockstruct.ls_jid = jid;
434 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
424 smp_mb__after_clear_bit(); 435 smp_mb__after_clear_bit();
425 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID); 436 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
426 rv = 0;
427out: 437out:
428 spin_unlock(&sdp->sd_jindex_spin); 438 spin_unlock(&sdp->sd_jindex_spin);
429 return rv ? rv : len; 439 return rv ? rv : len;
@@ -612,12 +622,13 @@ static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
612 struct kobj_uevent_env *env) 622 struct kobj_uevent_env *env)
613{ 623{
614 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj); 624 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
615 const u8 *uuid = sdp->sd_sb.sb_uuid; 625 struct super_block *s = sdp->sd_vfs;
626 const u8 *uuid = s->s_uuid;
616 627
617 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name); 628 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
618 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name); 629 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
619 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags)) 630 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
620 add_uevent_var(env, "JOURNALID=%u", sdp->sd_lockstruct.ls_jid); 631 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
621 if (gfs2_uuid_valid(uuid)) 632 if (gfs2_uuid_valid(uuid))
622 add_uevent_var(env, "UUID=%pUB", uuid); 633 add_uevent_var(env, "UUID=%pUB", uuid);
623 return 0; 634 return 0;
diff --git a/fs/gfs2/trace_gfs2.h b/fs/gfs2/trace_gfs2.h
index 148d55c14171..5d07609ec57d 100644
--- a/fs/gfs2/trace_gfs2.h
+++ b/fs/gfs2/trace_gfs2.h
@@ -10,6 +10,7 @@
10#include <linux/buffer_head.h> 10#include <linux/buffer_head.h>
11#include <linux/dlmconstants.h> 11#include <linux/dlmconstants.h>
12#include <linux/gfs2_ondisk.h> 12#include <linux/gfs2_ondisk.h>
13#include <linux/writeback.h>
13#include "incore.h" 14#include "incore.h"
14#include "glock.h" 15#include "glock.h"
15 16
@@ -39,7 +40,10 @@
39 {(1UL << GLF_INVALIDATE_IN_PROGRESS), "i" }, \ 40 {(1UL << GLF_INVALIDATE_IN_PROGRESS), "i" }, \
40 {(1UL << GLF_REPLY_PENDING), "r" }, \ 41 {(1UL << GLF_REPLY_PENDING), "r" }, \
41 {(1UL << GLF_INITIAL), "I" }, \ 42 {(1UL << GLF_INITIAL), "I" }, \
42 {(1UL << GLF_FROZEN), "F" }) 43 {(1UL << GLF_FROZEN), "F" }, \
44 {(1UL << GLF_QUEUED), "q" }, \
45 {(1UL << GLF_LRU), "L" }, \
46 {(1UL << GLF_OBJECT), "o" })
43 47
44#ifndef NUMPTY 48#ifndef NUMPTY
45#define NUMPTY 49#define NUMPTY
@@ -93,7 +97,7 @@ TRACE_EVENT(gfs2_glock_state_change,
93 __entry->new_state = glock_trace_state(new_state); 97 __entry->new_state = glock_trace_state(new_state);
94 __entry->tgt_state = glock_trace_state(gl->gl_target); 98 __entry->tgt_state = glock_trace_state(gl->gl_target);
95 __entry->dmt_state = glock_trace_state(gl->gl_demote_state); 99 __entry->dmt_state = glock_trace_state(gl->gl_demote_state);
96 __entry->flags = gl->gl_flags; 100 __entry->flags = gl->gl_flags | (gl->gl_object ? (1UL<<GLF_OBJECT) : 0);
97 ), 101 ),
98 102
99 TP_printk("%u,%u glock %d:%lld state %s to %s tgt:%s dmt:%s flags:%s", 103 TP_printk("%u,%u glock %d:%lld state %s to %s tgt:%s dmt:%s flags:%s",
@@ -126,7 +130,7 @@ TRACE_EVENT(gfs2_glock_put,
126 __entry->gltype = gl->gl_name.ln_type; 130 __entry->gltype = gl->gl_name.ln_type;
127 __entry->glnum = gl->gl_name.ln_number; 131 __entry->glnum = gl->gl_name.ln_number;
128 __entry->cur_state = glock_trace_state(gl->gl_state); 132 __entry->cur_state = glock_trace_state(gl->gl_state);
129 __entry->flags = gl->gl_flags; 133 __entry->flags = gl->gl_flags | (gl->gl_object ? (1UL<<GLF_OBJECT) : 0);
130 ), 134 ),
131 135
132 TP_printk("%u,%u glock %d:%lld state %s => %s flags:%s", 136 TP_printk("%u,%u glock %d:%lld state %s => %s flags:%s",
@@ -160,7 +164,7 @@ TRACE_EVENT(gfs2_demote_rq,
160 __entry->glnum = gl->gl_name.ln_number; 164 __entry->glnum = gl->gl_name.ln_number;
161 __entry->cur_state = glock_trace_state(gl->gl_state); 165 __entry->cur_state = glock_trace_state(gl->gl_state);
162 __entry->dmt_state = glock_trace_state(gl->gl_demote_state); 166 __entry->dmt_state = glock_trace_state(gl->gl_demote_state);
163 __entry->flags = gl->gl_flags; 167 __entry->flags = gl->gl_flags | (gl->gl_object ? (1UL<<GLF_OBJECT) : 0);
164 ), 168 ),
165 169
166 TP_printk("%u,%u glock %d:%lld demote %s to %s flags:%s", 170 TP_printk("%u,%u glock %d:%lld demote %s to %s flags:%s",
@@ -317,6 +321,33 @@ TRACE_EVENT(gfs2_log_blocks,
317 MINOR(__entry->dev), __entry->blocks) 321 MINOR(__entry->dev), __entry->blocks)
318); 322);
319 323
324/* Writing back the AIL */
325TRACE_EVENT(gfs2_ail_flush,
326
327 TP_PROTO(const struct gfs2_sbd *sdp, const struct writeback_control *wbc, int start),
328
329 TP_ARGS(sdp, wbc, start),
330
331 TP_STRUCT__entry(
332 __field( dev_t, dev )
333 __field( int, start )
334 __field( int, sync_mode )
335 __field( long, nr_to_write )
336 ),
337
338 TP_fast_assign(
339 __entry->dev = sdp->sd_vfs->s_dev;
340 __entry->start = start;
341 __entry->sync_mode = wbc->sync_mode;
342 __entry->nr_to_write = wbc->nr_to_write;
343 ),
344
345 TP_printk("%u,%u ail flush %s %s %ld", MAJOR(__entry->dev),
346 MINOR(__entry->dev), __entry->start ? "start" : "end",
347 __entry->sync_mode == WB_SYNC_ALL ? "all" : "none",
348 __entry->nr_to_write)
349);
350
320/* Section 3 - bmap 351/* Section 3 - bmap
321 * 352 *
322 * Objectives: 353 * Objectives:
diff --git a/fs/gfs2/trans.h b/fs/gfs2/trans.h
index edf9d4bd908e..fb56b783e028 100644
--- a/fs/gfs2/trans.h
+++ b/fs/gfs2/trans.h
@@ -20,11 +20,20 @@ struct gfs2_glock;
20#define RES_JDATA 1 20#define RES_JDATA 1
21#define RES_DATA 1 21#define RES_DATA 1
22#define RES_LEAF 1 22#define RES_LEAF 1
23#define RES_RG_HDR 1
23#define RES_RG_BIT 2 24#define RES_RG_BIT 2
24#define RES_EATTR 1 25#define RES_EATTR 1
25#define RES_STATFS 1 26#define RES_STATFS 1
26#define RES_QUOTA 2 27#define RES_QUOTA 2
27 28
29/* reserve either the number of blocks to be allocated plus the rg header
30 * block, or all of the blocks in the rg, whichever is smaller */
31static inline unsigned int gfs2_rg_blocks(const struct gfs2_alloc *al)
32{
33 return (al->al_requested < al->al_rgd->rd_length)?
34 al->al_requested + 1 : al->al_rgd->rd_length;
35}
36
28int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks, 37int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
29 unsigned int revokes); 38 unsigned int revokes);
30 39
diff --git a/fs/gfs2/xattr.c b/fs/gfs2/xattr.c
index 776af6eb4bcb..439b61c03262 100644
--- a/fs/gfs2/xattr.c
+++ b/fs/gfs2/xattr.c
@@ -734,7 +734,7 @@ static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
734 goto out_gunlock_q; 734 goto out_gunlock_q;
735 735
736 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), 736 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
737 blks + al->al_rgd->rd_length + 737 blks + gfs2_rg_blocks(al) +
738 RES_DINODE + RES_STATFS + RES_QUOTA, 0); 738 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
739 if (error) 739 if (error)
740 goto out_ipres; 740 goto out_ipres;
@@ -1296,10 +1296,8 @@ fail:
1296 1296
1297int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data) 1297int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
1298{ 1298{
1299 struct inode *inode = &ip->i_inode;
1300 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1299 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1301 struct gfs2_ea_location el; 1300 struct gfs2_ea_location el;
1302 struct buffer_head *dibh;
1303 int error; 1301 int error;
1304 1302
1305 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el); 1303 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
@@ -1321,26 +1319,7 @@ int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
1321 if (error) 1319 if (error)
1322 return error; 1320 return error;
1323 1321
1324 error = gfs2_meta_inode_buffer(ip, &dibh); 1322 error = gfs2_setattr_simple(ip, attr);
1325 if (error)
1326 goto out_trans_end;
1327
1328 if ((attr->ia_valid & ATTR_SIZE) &&
1329 attr->ia_size != i_size_read(inode)) {
1330 int error;
1331
1332 error = vmtruncate(inode, attr->ia_size);
1333 gfs2_assert_warn(GFS2_SB(inode), !error);
1334 }
1335
1336 setattr_copy(inode, attr);
1337 mark_inode_dirty(inode);
1338
1339 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1340 gfs2_dinode_out(ip, dibh->b_data);
1341 brelse(dibh);
1342
1343out_trans_end:
1344 gfs2_trans_end(sdp); 1323 gfs2_trans_end(sdp);
1345 return error; 1324 return error;
1346} 1325}