namespace.c 9.2 KB

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