mntpt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_getattr,
  35. };
  36. static LIST_HEAD(afs_vfsmounts);
  37. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  38. static 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, struct key *key)
  44. {
  45. struct file file = {
  46. .private_data = key,
  47. };
  48. struct page *page;
  49. size_t size;
  50. char *buf;
  51. int ret;
  52. _enter("{%x:%u,%u}",
  53. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  54. /* read the contents of the symlink into the pagecache */
  55. page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
  56. if (IS_ERR(page)) {
  57. ret = PTR_ERR(page);
  58. goto out;
  59. }
  60. ret = -EIO;
  61. if (PageError(page))
  62. goto out_free;
  63. buf = kmap(page);
  64. /* examine the symlink's contents */
  65. size = vnode->status.size;
  66. _debug("symlink to %*.*s", (int) size, (int) size, buf);
  67. if (size > 2 &&
  68. (buf[0] == '%' || buf[0] == '#') &&
  69. buf[size - 1] == '.'
  70. ) {
  71. _debug("symlink is a mountpoint");
  72. spin_lock(&vnode->lock);
  73. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  74. spin_unlock(&vnode->lock);
  75. }
  76. ret = 0;
  77. kunmap(page);
  78. out_free:
  79. page_cache_release(page);
  80. out:
  81. _leave(" = %d", ret);
  82. return ret;
  83. }
  84. /*
  85. * no valid lookup procedure on this sort of dir
  86. */
  87. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  88. struct dentry *dentry,
  89. struct nameidata *nd)
  90. {
  91. _enter("%p,%p{%p{%s},%s}",
  92. dir,
  93. dentry,
  94. dentry->d_parent,
  95. dentry->d_parent ?
  96. dentry->d_parent->d_name.name : (const unsigned char *) "",
  97. dentry->d_name.name);
  98. return ERR_PTR(-EREMOTE);
  99. }
  100. /*
  101. * no valid open procedure on this sort of dir
  102. */
  103. static int afs_mntpt_open(struct inode *inode, struct file *file)
  104. {
  105. _enter("%p,%p{%p{%s},%s}",
  106. inode, file,
  107. file->f_path.dentry->d_parent,
  108. file->f_path.dentry->d_parent ?
  109. file->f_path.dentry->d_parent->d_name.name :
  110. (const unsigned char *) "",
  111. file->f_path.dentry->d_name.name);
  112. return -EREMOTE;
  113. }
  114. /*
  115. * create a vfsmount to be automounted
  116. */
  117. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  118. {
  119. struct afs_super_info *super;
  120. struct vfsmount *mnt;
  121. struct page *page = NULL;
  122. size_t size;
  123. char *buf, *devname = NULL, *options = NULL;
  124. int ret;
  125. _enter("{%s}", mntpt->d_name.name);
  126. BUG_ON(!mntpt->d_inode);
  127. ret = -EINVAL;
  128. size = mntpt->d_inode->i_size;
  129. if (size > PAGE_SIZE - 1)
  130. goto error;
  131. ret = -ENOMEM;
  132. devname = (char *) get_zeroed_page(GFP_KERNEL);
  133. if (!devname)
  134. goto error;
  135. options = (char *) get_zeroed_page(GFP_KERNEL);
  136. if (!options)
  137. goto error;
  138. /* read the contents of the AFS special symlink */
  139. page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
  140. if (IS_ERR(page)) {
  141. ret = PTR_ERR(page);
  142. goto error;
  143. }
  144. ret = -EIO;
  145. if (PageError(page))
  146. goto error;
  147. buf = kmap_atomic(page, KM_USER0);
  148. memcpy(devname, buf, size);
  149. kunmap_atomic(buf, KM_USER0);
  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->path.mnt->mnt_devname,
  187. dentry,
  188. nd->path.dentry->d_name.name);
  189. dput(nd->path.dentry);
  190. nd->path.dentry = dget(dentry);
  191. newmnt = afs_mntpt_do_automount(nd->path.dentry);
  192. if (IS_ERR(newmnt)) {
  193. path_put(&nd->path);
  194. return (void *)newmnt;
  195. }
  196. mntget(newmnt);
  197. err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
  198. switch (err) {
  199. case 0:
  200. path_put(&nd->path);
  201. nd->path.mnt = newmnt;
  202. nd->path.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->path.dentry) &&
  209. follow_down(&nd->path.mnt, &nd->path.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. }