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