namespace.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/module.h>
  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>
  19. #include "internal.h"
  20. #define NFSDBG_FACILITY NFSDBG_VFS
  21. static void nfs_expire_automounts(struct work_struct *work);
  22. static LIST_HEAD(nfs_automount_list);
  23. static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
  24. int nfs_mountpoint_expiry_timeout = 500 * HZ;
  25. /*
  26. * nfs_path - reconstruct the path given an arbitrary dentry
  27. * @base - used to return pointer to the end of devname part of path
  28. * @dentry - pointer to dentry
  29. * @buffer - result buffer
  30. * @buflen - length of buffer
  31. * @flags - options (see below)
  32. *
  33. * Helper function for constructing the server pathname
  34. * by arbitrary hashed dentry.
  35. *
  36. * This is mainly for use in figuring out the path on the
  37. * server side when automounting on top of an existing partition
  38. * and in generating /proc/mounts and friends.
  39. *
  40. * Supported flags:
  41. * NFS_PATH_CANONICAL: ensure there is exactly one slash after
  42. * the original device (export) name
  43. * (if unset, the original name is returned verbatim)
  44. */
  45. char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
  46. unsigned flags)
  47. {
  48. char *end;
  49. int namelen;
  50. unsigned seq;
  51. const char *base;
  52. rename_retry:
  53. end = buffer+buflen;
  54. *--end = '\0';
  55. buflen--;
  56. seq = read_seqbegin(&rename_lock);
  57. rcu_read_lock();
  58. while (1) {
  59. spin_lock(&dentry->d_lock);
  60. if (IS_ROOT(dentry))
  61. break;
  62. namelen = dentry->d_name.len;
  63. buflen -= namelen + 1;
  64. if (buflen < 0)
  65. goto Elong_unlock;
  66. end -= namelen;
  67. memcpy(end, dentry->d_name.name, namelen);
  68. *--end = '/';
  69. spin_unlock(&dentry->d_lock);
  70. dentry = dentry->d_parent;
  71. }
  72. if (read_seqretry(&rename_lock, seq)) {
  73. spin_unlock(&dentry->d_lock);
  74. rcu_read_unlock();
  75. goto rename_retry;
  76. }
  77. if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
  78. if (--buflen < 0) {
  79. spin_unlock(&dentry->d_lock);
  80. rcu_read_unlock();
  81. goto Elong;
  82. }
  83. *--end = '/';
  84. }
  85. *p = end;
  86. base = dentry->d_fsdata;
  87. if (!base) {
  88. spin_unlock(&dentry->d_lock);
  89. rcu_read_unlock();
  90. WARN_ON(1);
  91. return end;
  92. }
  93. namelen = strlen(base);
  94. if (*end == '/') {
  95. /* Strip off excess slashes in base string */
  96. while (namelen > 0 && base[namelen - 1] == '/')
  97. namelen--;
  98. }
  99. buflen -= namelen;
  100. if (buflen < 0) {
  101. spin_unlock(&dentry->d_lock);
  102. rcu_read_unlock();
  103. goto Elong;
  104. }
  105. end -= namelen;
  106. memcpy(end, base, namelen);
  107. spin_unlock(&dentry->d_lock);
  108. rcu_read_unlock();
  109. return end;
  110. Elong_unlock:
  111. spin_unlock(&dentry->d_lock);
  112. rcu_read_unlock();
  113. if (read_seqretry(&rename_lock, seq))
  114. goto rename_retry;
  115. Elong:
  116. return ERR_PTR(-ENAMETOOLONG);
  117. }
  118. EXPORT_SYMBOL_GPL(nfs_path);
  119. /*
  120. * nfs_d_automount - Handle crossing a mountpoint on the server
  121. * @path - The mountpoint
  122. *
  123. * When we encounter a mountpoint on the server, we want to set up
  124. * a mountpoint on the client too, to prevent inode numbers from
  125. * colliding, and to allow "df" to work properly.
  126. * On NFSv4, we also want to allow for the fact that different
  127. * filesystems may be migrated to different servers in a failover
  128. * situation, and that different filesystems may want to use
  129. * different security flavours.
  130. */
  131. struct vfsmount *nfs_d_automount(struct path *path)
  132. {
  133. struct vfsmount *mnt;
  134. struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
  135. struct nfs_fh *fh = NULL;
  136. struct nfs_fattr *fattr = NULL;
  137. if (IS_ROOT(path->dentry))
  138. return ERR_PTR(-ESTALE);
  139. mnt = ERR_PTR(-ENOMEM);
  140. fh = nfs_alloc_fhandle();
  141. fattr = nfs_alloc_fattr();
  142. if (fh == NULL || fattr == NULL)
  143. goto out;
  144. mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
  145. if (IS_ERR(mnt))
  146. goto out;
  147. mntget(mnt); /* prevent immediate expiration */
  148. mnt_set_expiry(mnt, &nfs_automount_list);
  149. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  150. out:
  151. nfs_free_fattr(fattr);
  152. nfs_free_fhandle(fh);
  153. return mnt;
  154. }
  155. static int
  156. nfs_namespace_getattr(const struct path *path, struct kstat *stat,
  157. u32 request_mask, unsigned int query_flags)
  158. {
  159. if (NFS_FH(d_inode(path->dentry))->size != 0)
  160. return nfs_getattr(path, stat, request_mask, query_flags);
  161. generic_fillattr(d_inode(path->dentry), stat);
  162. return 0;
  163. }
  164. static int
  165. nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
  166. {
  167. if (NFS_FH(d_inode(dentry))->size != 0)
  168. return nfs_setattr(dentry, attr);
  169. return -EACCES;
  170. }
  171. const struct inode_operations nfs_mountpoint_inode_operations = {
  172. .getattr = nfs_getattr,
  173. .setattr = nfs_setattr,
  174. };
  175. const struct inode_operations nfs_referral_inode_operations = {
  176. .getattr = nfs_namespace_getattr,
  177. .setattr = nfs_namespace_setattr,
  178. };
  179. static void nfs_expire_automounts(struct work_struct *work)
  180. {
  181. struct list_head *list = &nfs_automount_list;
  182. mark_mounts_for_expiry(list);
  183. if (!list_empty(list))
  184. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  185. }
  186. void nfs_release_automount_timer(void)
  187. {
  188. if (list_empty(&nfs_automount_list))
  189. cancel_delayed_work(&nfs_automount_task);
  190. }
  191. /*
  192. * Clone a mountpoint of the appropriate type
  193. */
  194. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  195. const char *devname,
  196. struct nfs_clone_mount *mountdata)
  197. {
  198. return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
  199. }
  200. /**
  201. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  202. * @dentry - parent directory
  203. * @fh - filehandle for new root dentry
  204. * @fattr - attributes for new root inode
  205. * @authflavor - security flavor to use when performing the mount
  206. *
  207. */
  208. struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
  209. struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
  210. {
  211. struct nfs_clone_mount mountdata = {
  212. .sb = dentry->d_sb,
  213. .dentry = dentry,
  214. .fh = fh,
  215. .fattr = fattr,
  216. .authflavor = authflavor,
  217. };
  218. struct vfsmount *mnt;
  219. char *page = (char *) __get_free_page(GFP_USER);
  220. char *devname;
  221. if (page == NULL)
  222. return ERR_PTR(-ENOMEM);
  223. devname = nfs_devname(dentry, page, PAGE_SIZE);
  224. if (IS_ERR(devname))
  225. mnt = ERR_CAST(devname);
  226. else
  227. mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
  228. free_page((unsigned long)page);
  229. return mnt;
  230. }
  231. EXPORT_SYMBOL_GPL(nfs_do_submount);
  232. struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
  233. struct nfs_fh *fh, struct nfs_fattr *fattr)
  234. {
  235. int err;
  236. struct dentry *parent = dget_parent(dentry);
  237. /* Look it up again to get its attributes */
  238. err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
  239. dput(parent);
  240. if (err != 0)
  241. return ERR_PTR(err);
  242. return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
  243. }
  244. EXPORT_SYMBOL_GPL(nfs_submount);