mntpt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* mountpoint management
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/mount.h>
  18. #include <linux/namei.h>
  19. #include <linux/mnt_namespace.h>
  20. #include "internal.h"
  21. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  22. struct dentry *dentry,
  23. struct nameidata *nd);
  24. static int afs_mntpt_open(struct inode *inode, struct file *file);
  25. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
  26. static void afs_mntpt_expiry_timed_out(struct work_struct *work);
  27. const struct file_operations afs_mntpt_file_operations = {
  28. .open = afs_mntpt_open,
  29. };
  30. const struct inode_operations afs_mntpt_inode_operations = {
  31. .lookup = afs_mntpt_lookup,
  32. .follow_link = afs_mntpt_follow_link,
  33. .readlink = page_readlink,
  34. .getattr = afs_inode_getattr,
  35. };
  36. static LIST_HEAD(afs_vfsmounts);
  37. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  38. unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  39. /*
  40. * check a symbolic link to see whether it actually encodes a mountpoint
  41. * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
  42. */
  43. int afs_mntpt_check_symlink(struct afs_vnode *vnode)
  44. {
  45. struct page *page;
  46. size_t size;
  47. char *buf;
  48. int ret;
  49. _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
  50. /* read the contents of the symlink into the pagecache */
  51. page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
  52. if (IS_ERR(page)) {
  53. ret = PTR_ERR(page);
  54. goto out;
  55. }
  56. ret = -EIO;
  57. wait_on_page_locked(page);
  58. buf = kmap(page);
  59. if (!PageUptodate(page))
  60. goto out_free;
  61. if (PageError(page))
  62. goto out_free;
  63. /* examine the symlink's contents */
  64. size = vnode->status.size;
  65. _debug("symlink to %*.*s", (int) size, (int) size, buf);
  66. if (size > 2 &&
  67. (buf[0] == '%' || buf[0] == '#') &&
  68. buf[size - 1] == '.'
  69. ) {
  70. _debug("symlink is a mountpoint");
  71. spin_lock(&vnode->lock);
  72. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  73. spin_unlock(&vnode->lock);
  74. }
  75. ret = 0;
  76. out_free:
  77. kunmap(page);
  78. page_cache_release(page);
  79. out:
  80. _leave(" = %d", ret);
  81. return ret;
  82. }
  83. /*
  84. * no valid lookup procedure on this sort of dir
  85. */
  86. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  87. struct dentry *dentry,
  88. struct nameidata *nd)
  89. {
  90. _enter("%p,%p{%p{%s},%s}",
  91. dir,
  92. dentry,
  93. dentry->d_parent,
  94. dentry->d_parent ?
  95. dentry->d_parent->d_name.name : (const unsigned char *) "",
  96. dentry->d_name.name);
  97. return ERR_PTR(-EREMOTE);
  98. }
  99. /*
  100. * no valid open procedure on this sort of dir
  101. */
  102. static int afs_mntpt_open(struct inode *inode, struct file *file)
  103. {
  104. _enter("%p,%p{%p{%s},%s}",
  105. inode, file,
  106. file->f_path.dentry->d_parent,
  107. file->f_path.dentry->d_parent ?
  108. file->f_path.dentry->d_parent->d_name.name :
  109. (const unsigned char *) "",
  110. file->f_path.dentry->d_name.name);
  111. return -EREMOTE;
  112. }
  113. /*
  114. * create a vfsmount to be automounted
  115. */
  116. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  117. {
  118. struct afs_super_info *super;
  119. struct vfsmount *mnt;
  120. struct page *page = NULL;
  121. size_t size;
  122. char *buf, *devname = NULL, *options = NULL;
  123. int ret;
  124. _enter("{%s}", mntpt->d_name.name);
  125. BUG_ON(!mntpt->d_inode);
  126. ret = -EINVAL;
  127. size = mntpt->d_inode->i_size;
  128. if (size > PAGE_SIZE - 1)
  129. goto error;
  130. ret = -ENOMEM;
  131. devname = (char *) get_zeroed_page(GFP_KERNEL);
  132. if (!devname)
  133. goto error;
  134. options = (char *) get_zeroed_page(GFP_KERNEL);
  135. if (!options)
  136. goto error;
  137. /* read the contents of the AFS special symlink */
  138. page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
  139. if (IS_ERR(page)) {
  140. ret = PTR_ERR(page);
  141. goto error;
  142. }
  143. ret = -EIO;
  144. wait_on_page_locked(page);
  145. if (!PageUptodate(page) || PageError(page))
  146. goto error;
  147. buf = kmap(page);
  148. memcpy(devname, buf, size);
  149. kunmap(page);
  150. page_cache_release(page);
  151. page = NULL;
  152. /* work out what options we want */
  153. super = AFS_FS_S(mntpt->d_sb);
  154. memcpy(options, "cell=", 5);
  155. strcpy(options + 5, super->volume->cell->name);
  156. if (super->volume->type == AFSVL_RWVOL)
  157. strcat(options, ",rwpath");
  158. /* try and do the mount */
  159. _debug("--- attempting mount %s -o %s ---", devname, options);
  160. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  161. _debug("--- mount result %p ---", mnt);
  162. free_page((unsigned long) devname);
  163. free_page((unsigned long) options);
  164. _leave(" = %p", mnt);
  165. return mnt;
  166. error:
  167. if (page)
  168. page_cache_release(page);
  169. if (devname)
  170. free_page((unsigned long) devname);
  171. if (options)
  172. free_page((unsigned long) options);
  173. _leave(" = %d", ret);
  174. return ERR_PTR(ret);
  175. }
  176. /*
  177. * follow a link from a mountpoint directory, thus causing it to be mounted
  178. */
  179. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
  180. {
  181. struct vfsmount *newmnt;
  182. int err;
  183. _enter("%p{%s},{%s:%p{%s}}",
  184. dentry,
  185. dentry->d_name.name,
  186. nd->mnt->mnt_devname,
  187. dentry,
  188. nd->dentry->d_name.name);
  189. dput(nd->dentry);
  190. nd->dentry = dget(dentry);
  191. newmnt = afs_mntpt_do_automount(nd->dentry);
  192. if (IS_ERR(newmnt)) {
  193. path_release(nd);
  194. return (void *)newmnt;
  195. }
  196. mntget(newmnt);
  197. err = do_add_mount(newmnt, nd, MNT_SHRINKABLE, &afs_vfsmounts);
  198. switch (err) {
  199. case 0:
  200. path_release(nd);
  201. nd->mnt = newmnt;
  202. nd->dentry = dget(newmnt->mnt_root);
  203. schedule_delayed_work(&afs_mntpt_expiry_timer,
  204. afs_mntpt_expiry_timeout * HZ);
  205. break;
  206. case -EBUSY:
  207. /* someone else made a mount here whilst we were busy */
  208. while (d_mountpoint(nd->dentry) &&
  209. follow_down(&nd->mnt, &nd->dentry))
  210. ;
  211. err = 0;
  212. default:
  213. mntput(newmnt);
  214. break;
  215. }
  216. _leave(" = %d", err);
  217. return ERR_PTR(err);
  218. }
  219. /*
  220. * handle mountpoint expiry timer going off
  221. */
  222. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  223. {
  224. _enter("");
  225. if (!list_empty(&afs_vfsmounts)) {
  226. mark_mounts_for_expiry(&afs_vfsmounts);
  227. schedule_delayed_work(&afs_mntpt_expiry_timer,
  228. afs_mntpt_expiry_timeout * HZ);
  229. }
  230. _leave("");
  231. }
  232. /*
  233. * kill the AFS mountpoint timer if it's still running
  234. */
  235. void afs_mntpt_kill_timer(void)
  236. {
  237. _enter("");
  238. ASSERT(list_empty(&afs_vfsmounts));
  239. cancel_delayed_work(&afs_mntpt_expiry_timer);
  240. flush_scheduled_work();
  241. }
  242. /*
  243. * begin unmount by attempting to remove all automounted mountpoints we added
  244. */
  245. void afs_umount_begin(struct vfsmount *vfsmnt, int flags)
  246. {
  247. shrink_submounts(vfsmnt, &afs_vfsmounts);
  248. }