mntpt.c 6.6 KB

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