2 * linux/fs/nfs/nfs4namespace.c
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
9 #include <linux/config.h>
11 #include <linux/dcache.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/inet.h>
21 #define NFSDBG_FACILITY NFSDBG_VFS
24 * Check if fs_root is valid
26 static inline char *nfs4_pathname_string(struct nfs4_pathname *pathname,
27 char *buffer, ssize_t buflen)
29 char *end = buffer + buflen;
35 n = pathname->ncomponents;
37 struct nfs4_string *component = &pathname->components[n];
38 buflen -= component->len + 1;
41 end -= component->len;
42 memcpy(end, component->data, component->len);
47 return ERR_PTR(-ENAMETOOLONG);
52 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
53 * @mnt_parent - mountpoint of parent directory
54 * @dentry - parent directory
55 * @fspath - fs path returned in fs_locations
56 * @mntpath - mount path to new server
57 * @hostname - hostname of new server
58 * @addr - host addr of new server
61 static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
62 const struct dentry *dentry,
63 struct nfs4_fs_locations *locations)
65 struct vfsmount *mnt = ERR_PTR(-ENOENT);
66 struct nfs_clone_mount mountdata = {
67 .sb = mnt_parent->mnt_sb,
69 .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
76 if (locations == NULL || locations->nlocations <= 0)
79 dprintk("%s: referral at %s/%s\n", __FUNCTION__,
80 dentry->d_parent->d_name.name, dentry->d_name.name);
82 /* Ensure fs path is a prefix of current dentry path */
83 page = (char *) __get_free_page(GFP_USER);
86 page2 = (char *) __get_free_page(GFP_USER);
90 path = nfs4_path(dentry, page, PAGE_SIZE);
94 fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
98 if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
99 dprintk("%s: path %s does not begin with fsroot %s\n", __FUNCTION__, path, fs_path);
103 devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
104 if (IS_ERR(devname)) {
105 mnt = (struct vfsmount *)devname;
110 while (loc < locations->nlocations && IS_ERR(mnt)) {
111 struct nfs4_fs_location *location = &locations->locations[loc];
114 if (location == NULL || location->nservers <= 0 ||
115 location->rootpath.ncomponents == 0) {
120 mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
121 if (IS_ERR(mnt_path)) {
125 mountdata.mnt_path = mnt_path;
128 while (s < location->nservers) {
129 struct sockaddr_in addr = {};
131 if (location->servers[s].len <= 0 ||
132 valid_ipaddr4(location->servers[s].data) < 0) {
137 mountdata.hostname = location->servers[s].data;
138 addr.sin_addr.s_addr = in_aton(mountdata.hostname);
139 addr.sin_family = AF_INET;
140 addr.sin_port = htons(NFS_PORT);
141 mountdata.addr = &addr;
143 mnt = vfs_kern_mount(&nfs_referral_nfs4_fs_type, 0, devname, &mountdata);
153 free_page((unsigned long)page);
154 free_page((unsigned long)page2);
156 dprintk("%s: done\n", __FUNCTION__);
161 * nfs_do_refmount - handle crossing a referral on server
162 * @dentry - dentry of referral
163 * @nd - nameidata info
166 struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
168 struct vfsmount *mnt = ERR_PTR(-ENOENT);
169 struct dentry *parent;
170 struct nfs4_fs_locations *fs_locations = NULL;
174 /* BUG_ON(IS_ROOT(dentry)); */
175 dprintk("%s: enter\n", __FUNCTION__);
177 page = alloc_page(GFP_KERNEL);
181 fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
182 if (fs_locations == NULL)
186 parent = dget_parent(dentry);
187 dprintk("%s: getting locations for %s/%s\n", __FUNCTION__, parent->d_name.name, dentry->d_name.name);
188 err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
190 if (err != 0 || fs_locations->nlocations <= 0 ||
191 fs_locations->fs_path.ncomponents <= 0)
194 mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
199 dprintk("%s: done\n", __FUNCTION__);