NFS: Don't silently fail setattr() requests on mountpoints
[pandora-kernel.git] / fs / nfs / namespace.c
1 /*
2  * linux/fs/nfs/namespace.c
3  *
4  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5  * - Modified by David Howells <dhowells@redhat.com>
6  *
7  * NFS namespace
8  */
9
10 #include <linux/dcache.h>
11 #include <linux/gfp.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/string.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/vfs.h>
18 #include <linux/sunrpc/gss_api.h>
19 #include "internal.h"
20
21 #define NFSDBG_FACILITY         NFSDBG_VFS
22
23 static void nfs_expire_automounts(struct work_struct *work);
24
25 static LIST_HEAD(nfs_automount_list);
26 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
27 int nfs_mountpoint_expiry_timeout = 500 * HZ;
28
29 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
30                                         struct nfs_fh *fh,
31                                         struct nfs_fattr *fattr,
32                                         rpc_authflavor_t authflavor);
33
34 /*
35  * nfs_path - reconstruct the path given an arbitrary dentry
36  * @base - used to return pointer to the end of devname part of path
37  * @dentry - pointer to dentry
38  * @buffer - result buffer
39  * @buflen - length of buffer
40  * @flags - options (see below)
41  *
42  * Helper function for constructing the server pathname
43  * by arbitrary hashed dentry.
44  *
45  * This is mainly for use in figuring out the path on the
46  * server side when automounting on top of an existing partition
47  * and in generating /proc/mounts and friends.
48  *
49  * Supported flags:
50  * NFS_PATH_CANONICAL: ensure there is exactly one slash after
51  *                     the original device (export) name
52  *                     (if unset, the original name is returned verbatim)
53  */
54 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
55                unsigned flags)
56 {
57         char *end;
58         int namelen;
59         unsigned seq;
60         const char *base;
61
62 rename_retry:
63         end = buffer+buflen;
64         *--end = '\0';
65         buflen--;
66
67         seq = read_seqbegin(&rename_lock);
68         rcu_read_lock();
69         while (1) {
70                 spin_lock(&dentry->d_lock);
71                 if (IS_ROOT(dentry))
72                         break;
73                 namelen = dentry->d_name.len;
74                 buflen -= namelen + 1;
75                 if (buflen < 0)
76                         goto Elong_unlock;
77                 end -= namelen;
78                 memcpy(end, dentry->d_name.name, namelen);
79                 *--end = '/';
80                 spin_unlock(&dentry->d_lock);
81                 dentry = dentry->d_parent;
82         }
83         if (read_seqretry(&rename_lock, seq)) {
84                 spin_unlock(&dentry->d_lock);
85                 rcu_read_unlock();
86                 goto rename_retry;
87         }
88         if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
89                 if (--buflen < 0) {
90                         spin_unlock(&dentry->d_lock);
91                         rcu_read_unlock();
92                         goto Elong;
93                 }
94                 *--end = '/';
95         }
96         *p = end;
97         base = dentry->d_fsdata;
98         if (!base) {
99                 spin_unlock(&dentry->d_lock);
100                 rcu_read_unlock();
101                 WARN_ON(1);
102                 return end;
103         }
104         namelen = strlen(base);
105         if (flags & NFS_PATH_CANONICAL) {
106                 /* Strip off excess slashes in base string */
107                 while (namelen > 0 && base[namelen - 1] == '/')
108                         namelen--;
109         }
110         buflen -= namelen;
111         if (buflen < 0) {
112                 spin_unlock(&dentry->d_lock);
113                 rcu_read_unlock();
114                 goto Elong;
115         }
116         end -= namelen;
117         memcpy(end, base, namelen);
118         spin_unlock(&dentry->d_lock);
119         rcu_read_unlock();
120         return end;
121 Elong_unlock:
122         spin_unlock(&dentry->d_lock);
123         rcu_read_unlock();
124         if (read_seqretry(&rename_lock, seq))
125                 goto rename_retry;
126 Elong:
127         return ERR_PTR(-ENAMETOOLONG);
128 }
129
130 #ifdef CONFIG_NFS_V4
131 rpc_authflavor_t nfs_find_best_sec(struct nfs4_secinfo_flavors *flavors)
132 {
133         struct gss_api_mech *mech;
134         struct xdr_netobj oid;
135         int i;
136         rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;
137
138         for (i = 0; i < flavors->num_flavors; i++) {
139                 struct nfs4_secinfo_flavor *flavor;
140                 flavor = &flavors->flavors[i];
141
142                 if (flavor->flavor == RPC_AUTH_NULL || flavor->flavor == RPC_AUTH_UNIX) {
143                         pseudoflavor = flavor->flavor;
144                         break;
145                 } else if (flavor->flavor == RPC_AUTH_GSS) {
146                         oid.len  = flavor->gss.sec_oid4.len;
147                         oid.data = flavor->gss.sec_oid4.data;
148                         mech = gss_mech_get_by_OID(&oid);
149                         if (!mech)
150                                 continue;
151                         pseudoflavor = gss_svc_to_pseudoflavor(mech, flavor->gss.service);
152                         gss_mech_put(mech);
153                         break;
154                 }
155         }
156
157         return pseudoflavor;
158 }
159
160 static int nfs_negotiate_security(const struct dentry *parent,
161                                   const struct dentry *dentry,
162                                   rpc_authflavor_t *flavor)
163 {
164         struct page *page;
165         struct nfs4_secinfo_flavors *flavors;
166         int (*secinfo)(struct inode *, const struct qstr *, struct nfs4_secinfo_flavors *);
167         int ret = -EPERM;
168
169         secinfo = NFS_PROTO(parent->d_inode)->secinfo;
170         if (secinfo != NULL) {
171                 page = alloc_page(GFP_KERNEL);
172                 if (!page) {
173                         ret = -ENOMEM;
174                         goto out;
175                 }
176                 flavors = page_address(page);
177                 ret = secinfo(parent->d_inode, &dentry->d_name, flavors);
178                 *flavor = nfs_find_best_sec(flavors);
179                 put_page(page);
180         }
181
182 out:
183         return ret;
184 }
185
186 static int nfs_lookup_with_sec(struct nfs_server *server, struct dentry *parent,
187                                struct dentry *dentry, struct path *path,
188                                struct nfs_fh *fh, struct nfs_fattr *fattr,
189                                rpc_authflavor_t *flavor)
190 {
191         struct rpc_clnt *clone;
192         struct rpc_auth *auth;
193         int err;
194
195         err = nfs_negotiate_security(parent, path->dentry, flavor);
196         if (err < 0)
197                 goto out;
198         clone  = rpc_clone_client(server->client);
199         auth   = rpcauth_create(*flavor, clone);
200         if (!auth) {
201                 err = -EIO;
202                 goto out_shutdown;
203         }
204         err = server->nfs_client->rpc_ops->lookup(clone, parent->d_inode,
205                                                   &path->dentry->d_name,
206                                                   fh, fattr);
207 out_shutdown:
208         rpc_shutdown_client(clone);
209 out:
210         return err;
211 }
212 #else /* CONFIG_NFS_V4 */
213 static inline int nfs_lookup_with_sec(struct nfs_server *server,
214                                       struct dentry *parent, struct dentry *dentry,
215                                       struct path *path, struct nfs_fh *fh,
216                                       struct nfs_fattr *fattr,
217                                       rpc_authflavor_t *flavor)
218 {
219         return -EPERM;
220 }
221 #endif /* CONFIG_NFS_V4 */
222
223 /*
224  * nfs_d_automount - Handle crossing a mountpoint on the server
225  * @path - The mountpoint
226  *
227  * When we encounter a mountpoint on the server, we want to set up
228  * a mountpoint on the client too, to prevent inode numbers from
229  * colliding, and to allow "df" to work properly.
230  * On NFSv4, we also want to allow for the fact that different
231  * filesystems may be migrated to different servers in a failover
232  * situation, and that different filesystems may want to use
233  * different security flavours.
234  */
235 struct vfsmount *nfs_d_automount(struct path *path)
236 {
237         struct vfsmount *mnt;
238         struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
239         struct dentry *parent;
240         struct nfs_fh *fh = NULL;
241         struct nfs_fattr *fattr = NULL;
242         int err;
243         rpc_authflavor_t flavor = RPC_AUTH_UNIX;
244
245         dprintk("--> nfs_d_automount()\n");
246
247         mnt = ERR_PTR(-ESTALE);
248         if (IS_ROOT(path->dentry))
249                 goto out_nofree;
250
251         mnt = ERR_PTR(-ENOMEM);
252         fh = nfs_alloc_fhandle();
253         fattr = nfs_alloc_fattr();
254         if (fh == NULL || fattr == NULL)
255                 goto out;
256
257         dprintk("%s: enter\n", __func__);
258
259         /* Look it up again to get its attributes */
260         parent = dget_parent(path->dentry);
261         err = server->nfs_client->rpc_ops->lookup(server->client, parent->d_inode,
262                                                   &path->dentry->d_name,
263                                                   fh, fattr);
264         if (err == -EPERM && NFS_PROTO(parent->d_inode)->secinfo != NULL)
265                 err = nfs_lookup_with_sec(server, parent, path->dentry, path, fh, fattr, &flavor);
266         dput(parent);
267         if (err != 0) {
268                 mnt = ERR_PTR(err);
269                 goto out;
270         }
271
272         if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
273                 mnt = nfs_do_refmount(path->dentry);
274         else
275                 mnt = nfs_do_submount(path->dentry, fh, fattr, flavor);
276         if (IS_ERR(mnt))
277                 goto out;
278
279         dprintk("%s: done, success\n", __func__);
280         mntget(mnt); /* prevent immediate expiration */
281         mnt_set_expiry(mnt, &nfs_automount_list);
282         schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
283
284 out:
285         nfs_free_fattr(fattr);
286         nfs_free_fhandle(fh);
287 out_nofree:
288         dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
289         return mnt;
290 }
291
292 static int
293 nfs_namespace_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
294 {
295         if (NFS_FH(dentry->d_inode)->size != 0)
296                 return nfs_getattr(mnt, dentry, stat);
297         generic_fillattr(dentry->d_inode, stat);
298         return 0;
299 }
300
301 static int
302 nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
303 {
304         if (NFS_FH(dentry->d_inode)->size != 0)
305                 return nfs_setattr(dentry, attr);
306         return -EACCES;
307 }
308
309 const struct inode_operations nfs_mountpoint_inode_operations = {
310         .getattr        = nfs_getattr,
311         .setattr        = nfs_setattr,
312 };
313
314 const struct inode_operations nfs_referral_inode_operations = {
315         .getattr        = nfs_namespace_getattr,
316         .setattr        = nfs_namespace_setattr,
317 };
318
319 static void nfs_expire_automounts(struct work_struct *work)
320 {
321         struct list_head *list = &nfs_automount_list;
322
323         mark_mounts_for_expiry(list);
324         if (!list_empty(list))
325                 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
326 }
327
328 void nfs_release_automount_timer(void)
329 {
330         if (list_empty(&nfs_automount_list))
331                 cancel_delayed_work(&nfs_automount_task);
332 }
333
334 /*
335  * Clone a mountpoint of the appropriate type
336  */
337 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
338                                            const char *devname,
339                                            struct nfs_clone_mount *mountdata)
340 {
341 #ifdef CONFIG_NFS_V4
342         struct vfsmount *mnt = ERR_PTR(-EINVAL);
343         switch (server->nfs_client->rpc_ops->version) {
344                 case 2:
345                 case 3:
346                         mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
347                         break;
348                 case 4:
349                         mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
350         }
351         return mnt;
352 #else
353         return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
354 #endif
355 }
356
357 /**
358  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
359  * @dentry - parent directory
360  * @fh - filehandle for new root dentry
361  * @fattr - attributes for new root inode
362  * @authflavor - security flavor to use when performing the mount
363  *
364  */
365 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
366                                         struct nfs_fh *fh,
367                                         struct nfs_fattr *fattr,
368                                         rpc_authflavor_t authflavor)
369 {
370         struct nfs_clone_mount mountdata = {
371                 .sb = dentry->d_sb,
372                 .dentry = dentry,
373                 .fh = fh,
374                 .fattr = fattr,
375                 .authflavor = authflavor,
376         };
377         struct vfsmount *mnt = ERR_PTR(-ENOMEM);
378         char *page = (char *) __get_free_page(GFP_USER);
379         char *devname;
380
381         dprintk("--> nfs_do_submount()\n");
382
383         dprintk("%s: submounting on %s/%s\n", __func__,
384                         dentry->d_parent->d_name.name,
385                         dentry->d_name.name);
386         if (page == NULL)
387                 goto out;
388         devname = nfs_devname(dentry, page, PAGE_SIZE);
389         mnt = (struct vfsmount *)devname;
390         if (IS_ERR(devname))
391                 goto free_page;
392         mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
393 free_page:
394         free_page((unsigned long)page);
395 out:
396         dprintk("%s: done\n", __func__);
397
398         dprintk("<-- nfs_do_submount() = %p\n", mnt);
399         return mnt;
400 }