[GFS2] Patch to remove stats gathering from GFS2
[linux-2.6] / fs / gfs2 / ops_export.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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 v.2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <asm/semaphore.h>
16
17 #include "gfs2.h"
18 #include "dir.h"
19 #include "glock.h"
20 #include "glops.h"
21 #include "inode.h"
22 #include "ops_export.h"
23 #include "rgrp.h"
24
25 static struct dentry *gfs2_decode_fh(struct super_block *sb,
26                                      __u32 *fh,
27                                      int fh_len,
28                                      int fh_type,
29                                      int (*acceptable)(void *context,
30                                                        struct dentry *dentry),
31                                      void *context)
32 {
33         struct gfs2_inum this, parent;
34
35         if (fh_type != fh_len)
36                 return NULL;
37
38         memset(&parent, 0, sizeof(struct gfs2_inum));
39
40         switch (fh_type) {
41         case 8:
42                 parent.no_formal_ino = ((uint64_t)be32_to_cpu(fh[4])) << 32;
43                 parent.no_formal_ino |= be32_to_cpu(fh[5]);
44                 parent.no_addr = ((uint64_t)be32_to_cpu(fh[6])) << 32;
45                 parent.no_addr |= be32_to_cpu(fh[7]);
46         case 4:
47                 this.no_formal_ino = ((uint64_t)be32_to_cpu(fh[0])) << 32;
48                 this.no_formal_ino |= be32_to_cpu(fh[1]);
49                 this.no_addr = ((uint64_t)be32_to_cpu(fh[2])) << 32;
50                 this.no_addr |= be32_to_cpu(fh[3]);
51                 break;
52         default:
53                 return NULL;
54         }
55
56         return gfs2_export_ops.find_exported_dentry(sb, &this, &parent,
57                                                     acceptable, context);
58 }
59
60 static int gfs2_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
61                           int connectable)
62 {
63         struct inode *inode = dentry->d_inode;
64         struct gfs2_inode *ip = get_v2ip(inode);
65         struct gfs2_sbd *sdp = ip->i_sbd;
66
67         if (*len < 4 || (connectable && *len < 8))
68                 return 255;
69
70         fh[0] = ip->i_num.no_formal_ino >> 32;
71         fh[0] = cpu_to_be32(fh[0]);
72         fh[1] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
73         fh[1] = cpu_to_be32(fh[1]);
74         fh[2] = ip->i_num.no_addr >> 32;
75         fh[2] = cpu_to_be32(fh[2]);
76         fh[3] = ip->i_num.no_addr & 0xFFFFFFFF;
77         fh[3] = cpu_to_be32(fh[3]);
78         *len = 4;
79
80         if (!connectable || ip == get_v2ip(sdp->sd_root_dir))
81                 return *len;
82
83         spin_lock(&dentry->d_lock);
84         inode = dentry->d_parent->d_inode;
85         ip = get_v2ip(inode);
86         gfs2_inode_hold(ip);
87         spin_unlock(&dentry->d_lock);
88
89         fh[4] = ip->i_num.no_formal_ino >> 32;
90         fh[4] = cpu_to_be32(fh[4]);
91         fh[5] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
92         fh[5] = cpu_to_be32(fh[5]);
93         fh[6] = ip->i_num.no_addr >> 32;
94         fh[6] = cpu_to_be32(fh[6]);
95         fh[7] = ip->i_num.no_addr & 0xFFFFFFFF;
96         fh[7] = cpu_to_be32(fh[7]);
97         *len = 8;
98
99         gfs2_inode_put(ip);
100
101         return *len;
102 }
103
104 struct get_name_filldir {
105         struct gfs2_inum inum;
106         char *name;
107 };
108
109 static int get_name_filldir(void *opaque, const char *name, unsigned int length,
110                             uint64_t offset, struct gfs2_inum *inum,
111                             unsigned int type)
112 {
113         struct get_name_filldir *gnfd = (struct get_name_filldir *)opaque;
114
115         if (!gfs2_inum_equal(inum, &gnfd->inum))
116                 return 0;
117
118         memcpy(gnfd->name, name, length);
119         gnfd->name[length] = 0;
120
121         return 1;
122 }
123
124 static int gfs2_get_name(struct dentry *parent, char *name,
125                          struct dentry *child)
126 {
127         struct inode *dir = parent->d_inode;
128         struct inode *inode = child->d_inode;
129         struct gfs2_inode *dip, *ip;
130         struct get_name_filldir gnfd;
131         struct gfs2_holder gh;
132         uint64_t offset = 0;
133         int error;
134
135         if (!dir)
136                 return -EINVAL;
137
138         if (!S_ISDIR(dir->i_mode) || !inode)
139                 return -EINVAL;
140
141         dip = get_v2ip(dir);
142         ip = get_v2ip(inode);
143
144         *name = 0;
145         gnfd.inum = ip->i_num;
146         gnfd.name = name;
147
148         error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
149         if (error)
150                 return error;
151
152         error = gfs2_dir_read(dip, &offset, &gnfd, get_name_filldir);
153
154         gfs2_glock_dq_uninit(&gh);
155
156         if (!error && !*name)
157                 error = -ENOENT;
158
159         return error;
160 }
161
162 static struct dentry *gfs2_get_parent(struct dentry *child)
163 {
164         struct gfs2_inode *dip = get_v2ip(child->d_inode);
165         struct qstr dotdot = { .name = "..", .len = 2 };
166         struct inode *inode;
167         struct dentry *dentry;
168         int error;
169
170         error = gfs2_lookupi(child->d_inode, &dotdot, 1, &inode);
171         if (error)
172                 return ERR_PTR(error);
173
174         dentry = d_alloc_anon(inode);
175         if (!dentry) {
176                 iput(inode);
177                 return ERR_PTR(-ENOMEM);
178         }
179
180         return dentry;
181 }
182
183 static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_p)
184 {
185         struct gfs2_sbd *sdp = get_v2sdp(sb);
186         struct gfs2_inum *inum = (struct gfs2_inum *)inum_p;
187         struct gfs2_holder i_gh, ri_gh, rgd_gh;
188         struct gfs2_rgrpd *rgd;
189         struct gfs2_inode *ip;
190         struct inode *inode;
191         struct dentry *dentry;
192         int error;
193
194         /* System files? */
195
196         inode = gfs2_iget(sb, inum);
197         if (inode) {
198                 ip = get_v2ip(inode);
199                 if (ip->i_num.no_formal_ino != inum->no_formal_ino) {
200                         iput(inode);
201                         return ERR_PTR(-ESTALE);
202                 }
203                 goto out_inode;
204         }
205
206         error = gfs2_glock_nq_num(sdp,
207                                   inum->no_addr, &gfs2_inode_glops,
208                                   LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
209                                   &i_gh);
210         if (error)
211                 return ERR_PTR(error);
212
213         error = gfs2_inode_get(i_gh.gh_gl, inum, NO_CREATE, &ip);
214         if (error)
215                 goto fail;
216         if (ip)
217                 goto out_ip;
218
219         error = gfs2_rindex_hold(sdp, &ri_gh);
220         if (error)
221                 goto fail;
222
223         error = -EINVAL;
224         rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
225         if (!rgd)
226                 goto fail_rindex;
227
228         error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
229         if (error)
230                 goto fail_rindex;
231
232         error = -ESTALE;
233         if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
234                 goto fail_rgd;
235
236         gfs2_glock_dq_uninit(&rgd_gh);
237         gfs2_glock_dq_uninit(&ri_gh);
238
239         error = gfs2_inode_get(i_gh.gh_gl, inum, CREATE, &ip);
240         if (error)
241                 goto fail;
242
243         error = gfs2_inode_refresh(ip);
244         if (error) {
245                 gfs2_inode_put(ip);
246                 goto fail;
247         }
248
249  out_ip:
250         error = -EIO;
251         if (ip->i_di.di_flags & GFS2_DIF_SYSTEM) {
252                 gfs2_inode_put(ip);
253                 goto fail;
254         }
255
256         gfs2_glock_dq_uninit(&i_gh);
257
258         inode = gfs2_ip2v(ip);
259         gfs2_inode_put(ip);
260
261         if (!inode)
262                 return ERR_PTR(-ENOMEM);
263
264  out_inode:
265         dentry = d_alloc_anon(inode);
266         if (!dentry) {
267                 iput(inode);
268                 return ERR_PTR(-ENOMEM);
269         }
270
271         return dentry;
272
273  fail_rgd:
274         gfs2_glock_dq_uninit(&rgd_gh);
275
276  fail_rindex:
277         gfs2_glock_dq_uninit(&ri_gh);
278
279  fail:
280         gfs2_glock_dq_uninit(&i_gh);
281         return ERR_PTR(error);
282 }
283
284 struct export_operations gfs2_export_ops = {
285         .decode_fh = gfs2_decode_fh,
286         .encode_fh = gfs2_encode_fh,
287         .get_name = gfs2_get_name,
288         .get_parent = gfs2_get_parent,
289         .get_dentry = gfs2_get_dentry,
290 };
291