2 * linux/fs/nfs/namespace.c
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5 * - Modified by David Howells <dhowells@redhat.com>
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>
21 #define NFSDBG_FACILITY NFSDBG_VFS
23 static void nfs_expire_automounts(struct work_struct *work);
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;
29 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
31 struct nfs_fattr *fattr,
32 rpc_authflavor_t authflavor);
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)
42 * Helper function for constructing the server pathname
43 * by arbitrary hashed dentry.
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.
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)
54 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
67 seq = read_seqbegin(&rename_lock);
70 spin_lock(&dentry->d_lock);
73 namelen = dentry->d_name.len;
74 buflen -= namelen + 1;
78 memcpy(end, dentry->d_name.name, namelen);
80 spin_unlock(&dentry->d_lock);
81 dentry = dentry->d_parent;
83 if (read_seqretry(&rename_lock, seq)) {
84 spin_unlock(&dentry->d_lock);
88 if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
90 spin_unlock(&dentry->d_lock);
97 base = dentry->d_fsdata;
99 spin_unlock(&dentry->d_lock);
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] == '/')
112 spin_unlock(&dentry->d_lock);
117 memcpy(end, base, namelen);
118 spin_unlock(&dentry->d_lock);
122 spin_unlock(&dentry->d_lock);
124 if (read_seqretry(&rename_lock, seq))
127 return ERR_PTR(-ENAMETOOLONG);
131 rpc_authflavor_t nfs_find_best_sec(struct nfs4_secinfo_flavors *flavors)
133 struct gss_api_mech *mech;
134 struct xdr_netobj oid;
136 rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;
138 for (i = 0; i < flavors->num_flavors; i++) {
139 struct nfs4_secinfo_flavor *flavor;
140 flavor = &flavors->flavors[i];
142 if (flavor->flavor == RPC_AUTH_NULL || flavor->flavor == RPC_AUTH_UNIX) {
143 pseudoflavor = flavor->flavor;
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);
151 pseudoflavor = gss_svc_to_pseudoflavor(mech, flavor->gss.service);
160 static int nfs_negotiate_security(const struct dentry *parent,
161 const struct dentry *dentry,
162 rpc_authflavor_t *flavor)
165 struct nfs4_secinfo_flavors *flavors;
166 int (*secinfo)(struct inode *, const struct qstr *, struct nfs4_secinfo_flavors *);
169 secinfo = NFS_PROTO(parent->d_inode)->secinfo;
170 if (secinfo != NULL) {
171 page = alloc_page(GFP_KERNEL);
176 flavors = page_address(page);
177 ret = secinfo(parent->d_inode, &dentry->d_name, flavors);
178 *flavor = nfs_find_best_sec(flavors);
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)
191 struct rpc_clnt *clone;
192 struct rpc_auth *auth;
195 err = nfs_negotiate_security(parent, path->dentry, flavor);
198 clone = rpc_clone_client(server->client);
199 auth = rpcauth_create(*flavor, clone);
204 err = server->nfs_client->rpc_ops->lookup(clone, parent->d_inode,
205 &path->dentry->d_name,
208 rpc_shutdown_client(clone);
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)
221 #endif /* CONFIG_NFS_V4 */
224 * nfs_d_automount - Handle crossing a mountpoint on the server
225 * @path - The mountpoint
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.
235 struct vfsmount *nfs_d_automount(struct path *path)
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;
243 rpc_authflavor_t flavor = RPC_AUTH_UNIX;
245 dprintk("--> nfs_d_automount()\n");
247 mnt = ERR_PTR(-ESTALE);
248 if (IS_ROOT(path->dentry))
251 mnt = ERR_PTR(-ENOMEM);
252 fh = nfs_alloc_fhandle();
253 fattr = nfs_alloc_fattr();
254 if (fh == NULL || fattr == NULL)
257 dprintk("%s: enter\n", __func__);
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,
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);
272 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
273 mnt = nfs_do_refmount(path->dentry);
275 mnt = nfs_do_submount(path->dentry, fh, fattr, flavor);
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);
285 nfs_free_fattr(fattr);
286 nfs_free_fhandle(fh);
288 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
293 nfs_namespace_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
295 if (NFS_FH(dentry->d_inode)->size != 0)
296 return nfs_getattr(mnt, dentry, stat);
297 generic_fillattr(dentry->d_inode, stat);
302 nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
304 if (NFS_FH(dentry->d_inode)->size != 0)
305 return nfs_setattr(dentry, attr);
309 const struct inode_operations nfs_mountpoint_inode_operations = {
310 .getattr = nfs_getattr,
311 .setattr = nfs_setattr,
314 const struct inode_operations nfs_referral_inode_operations = {
315 .getattr = nfs_namespace_getattr,
316 .setattr = nfs_namespace_setattr,
319 static void nfs_expire_automounts(struct work_struct *work)
321 struct list_head *list = &nfs_automount_list;
323 mark_mounts_for_expiry(list);
324 if (!list_empty(list))
325 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
328 void nfs_release_automount_timer(void)
330 if (list_empty(&nfs_automount_list))
331 cancel_delayed_work(&nfs_automount_task);
335 * Clone a mountpoint of the appropriate type
337 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
339 struct nfs_clone_mount *mountdata)
342 struct vfsmount *mnt = ERR_PTR(-EINVAL);
343 switch (server->nfs_client->rpc_ops->version) {
346 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
349 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
353 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
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
365 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
367 struct nfs_fattr *fattr,
368 rpc_authflavor_t authflavor)
370 struct nfs_clone_mount mountdata = {
375 .authflavor = authflavor,
377 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
378 char *page = (char *) __get_free_page(GFP_USER);
381 dprintk("--> nfs_do_submount()\n");
383 dprintk("%s: submounting on %s/%s\n", __func__,
384 dentry->d_parent->d_name.name,
385 dentry->d_name.name);
388 devname = nfs_devname(dentry, page, PAGE_SIZE);
389 mnt = (struct vfsmount *)devname;
392 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
394 free_page((unsigned long)page);
396 dprintk("%s: done\n", __func__);
398 dprintk("<-- nfs_do_submount() = %p\n", mnt);