2 * linux/fs/nfs/nfs4namespace.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/mount.h>
12 #include <linux/namei.h>
13 #include <linux/nfs_fs.h>
14 #include <linux/string.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/vfs.h>
17 #include <linux/inet.h>
21 #define NFSDBG_FACILITY NFSDBG_VFS
24 * Check if fs_root is valid
26 static inline char *nfs4_pathname_string(const struct nfs4_pathname
*pathname
,
27 char *buffer
, ssize_t buflen
)
29 char *end
= buffer
+ buflen
;
35 n
= pathname
->ncomponents
;
37 const 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
);
51 * Determine the mount path as a string
53 static char *nfs4_path(const struct vfsmount
*mnt_parent
,
54 const struct dentry
*dentry
,
55 char *buffer
, ssize_t buflen
)
59 srvpath
= strchr(mnt_parent
->mnt_devname
, ':');
63 srvpath
= mnt_parent
->mnt_devname
;
65 return nfs_path(srvpath
, mnt_parent
->mnt_root
, dentry
, buffer
, buflen
);
69 * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
70 * believe to be the server path to this dentry
72 static int nfs4_validate_fspath(const struct vfsmount
*mnt_parent
,
73 const struct dentry
*dentry
,
74 const struct nfs4_fs_locations
*locations
,
75 char *page
, char *page2
)
77 const char *path
, *fs_path
;
79 path
= nfs4_path(mnt_parent
, dentry
, page
, PAGE_SIZE
);
83 fs_path
= nfs4_pathname_string(&locations
->fs_path
, page2
, PAGE_SIZE
);
85 return PTR_ERR(fs_path
);
87 if (strncmp(path
, fs_path
, strlen(fs_path
)) != 0) {
88 dprintk("%s: path %s does not begin with fsroot %s\n",
89 __func__
, path
, fs_path
);
96 static struct vfsmount
*try_location(struct nfs_clone_mount
*mountdata
,
97 char *page
, char *page2
,
98 const struct nfs4_fs_location
*location
)
100 struct vfsmount
*mnt
= ERR_PTR(-ENOENT
);
105 mnt_path
= nfs4_pathname_string(&location
->rootpath
, page2
, PAGE_SIZE
);
106 if (IS_ERR(mnt_path
))
108 mountdata
->mnt_path
= mnt_path
;
109 page2
+= strlen(mnt_path
) + 1;
110 page2len
= PAGE_SIZE
- strlen(mnt_path
) - 1;
112 for (s
= 0; s
< location
->nservers
; s
++) {
113 const struct nfs4_string
*buf
= &location
->servers
[s
];
114 struct sockaddr_storage addr
;
116 if (buf
->len
<= 0 || buf
->len
>= PAGE_SIZE
)
119 mountdata
->addr
= (struct sockaddr
*)&addr
;
121 if (memchr(buf
->data
, IPV6_SCOPE_DELIMITER
, buf
->len
))
123 nfs_parse_ip_address(buf
->data
, buf
->len
,
124 mountdata
->addr
, &mountdata
->addrlen
);
125 if (mountdata
->addr
->sa_family
== AF_UNSPEC
)
127 nfs_set_port(mountdata
->addr
, NFS_PORT
);
129 strncpy(page2
, buf
->data
, page2len
);
130 page2
[page2len
] = '\0';
131 mountdata
->hostname
= page2
;
133 snprintf(page
, PAGE_SIZE
, "%s:%s",
135 mountdata
->mnt_path
);
137 mnt
= vfs_kern_mount(&nfs4_referral_fs_type
, 0, page
, mountdata
);
145 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
146 * @mnt_parent - mountpoint of parent directory
147 * @dentry - parent directory
148 * @locations - array of NFSv4 server location information
151 static struct vfsmount
*nfs_follow_referral(const struct vfsmount
*mnt_parent
,
152 const struct dentry
*dentry
,
153 const struct nfs4_fs_locations
*locations
)
155 struct vfsmount
*mnt
= ERR_PTR(-ENOENT
);
156 struct nfs_clone_mount mountdata
= {
157 .sb
= mnt_parent
->mnt_sb
,
159 .authflavor
= NFS_SB(mnt_parent
->mnt_sb
)->client
->cl_auth
->au_flavor
,
161 char *page
= NULL
, *page2
= NULL
;
164 if (locations
== NULL
|| locations
->nlocations
<= 0)
167 dprintk("%s: referral at %s/%s\n", __func__
,
168 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
170 page
= (char *) __get_free_page(GFP_USER
);
174 page2
= (char *) __get_free_page(GFP_USER
);
178 /* Ensure fs path is a prefix of current dentry path */
179 error
= nfs4_validate_fspath(mnt_parent
, dentry
, locations
, page
, page2
);
181 mnt
= ERR_PTR(error
);
185 for (loc
= 0; loc
< locations
->nlocations
; loc
++) {
186 const struct nfs4_fs_location
*location
= &locations
->locations
[loc
];
188 if (location
== NULL
|| location
->nservers
<= 0 ||
189 location
->rootpath
.ncomponents
== 0)
192 mnt
= try_location(&mountdata
, page
, page2
, location
);
198 free_page((unsigned long) page
);
199 free_page((unsigned long) page2
);
200 dprintk("%s: done\n", __func__
);
205 * nfs_do_refmount - handle crossing a referral on server
206 * @dentry - dentry of referral
207 * @nd - nameidata info
210 struct vfsmount
*nfs_do_refmount(const struct vfsmount
*mnt_parent
, struct dentry
*dentry
)
212 struct vfsmount
*mnt
= ERR_PTR(-ENOMEM
);
213 struct dentry
*parent
;
214 struct nfs4_fs_locations
*fs_locations
= NULL
;
218 /* BUG_ON(IS_ROOT(dentry)); */
219 dprintk("%s: enter\n", __func__
);
221 page
= alloc_page(GFP_KERNEL
);
225 fs_locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
226 if (fs_locations
== NULL
)
230 mnt
= ERR_PTR(-ENOENT
);
232 parent
= dget_parent(dentry
);
233 dprintk("%s: getting locations for %s/%s\n",
234 __func__
, parent
->d_name
.name
, dentry
->d_name
.name
);
236 err
= nfs4_proc_fs_locations(parent
->d_inode
, &dentry
->d_name
, fs_locations
, page
);
239 fs_locations
->nlocations
<= 0 ||
240 fs_locations
->fs_path
.ncomponents
<= 0)
243 mnt
= nfs_follow_referral(mnt_parent
, dentry
, fs_locations
);
248 dprintk("%s: done\n", __func__
);