namespace.c 10.0 KB

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