mntpt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "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 file file = {
  45. .private_data = key,
  46. };
  47. struct page *page;
  48. size_t size;
  49. char *buf;
  50. int ret;
  51. _enter("{%x:%u,%u}",
  52. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  53. /* read the contents of the symlink into the pagecache */
  54. page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
  55. if (IS_ERR(page)) {
  56. ret = PTR_ERR(page);
  57. goto out;
  58. }
  59. ret = -EIO;
  60. if (PageError(page))
  61. goto out_free;
  62. buf = kmap(page);
  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. kunmap(page);
  77. out_free:
  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. if (PageError(page))
  145. goto error;
  146. buf = kmap_atomic(page, KM_USER0);
  147. memcpy(devname, buf, size);
  148. kunmap_atomic(buf, KM_USER0);
  149. page_cache_release(page);
  150. page = NULL;
  151. /* work out what options we want */
  152. super = AFS_FS_S(mntpt->d_sb);
  153. memcpy(options, "cell=", 5);
  154. strcpy(options + 5, super->volume->cell->name);
  155. if (super->volume->type == AFSVL_RWVOL)
  156. strcat(options, ",rwpath");
  157. /* try and do the mount */
  158. _debug("--- attempting mount %s -o %s ---", devname, options);
  159. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  160. _debug("--- mount result %p ---", mnt);
  161. free_page((unsigned long) devname);
  162. free_page((unsigned long) options);
  163. _leave(" = %p", mnt);
  164. return mnt;
  165. error:
  166. if (page)
  167. page_cache_release(page);
  168. if (devname)
  169. free_page((unsigned long) devname);
  170. if (options)
  171. free_page((unsigned long) options);
  172. _leave(" = %d", ret);
  173. return ERR_PTR(ret);
  174. }
  175. /*
  176. * follow a link from a mountpoint directory, thus causing it to be mounted
  177. */
  178. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
  179. {
  180. struct vfsmount *newmnt;
  181. int err;
  182. _enter("%p{%s},{%s:%p{%s},}",
  183. dentry,
  184. dentry->d_name.name,
  185. nd->path.mnt->mnt_devname,
  186. dentry,
  187. nd->path.dentry->d_name.name);
  188. dput(nd->path.dentry);
  189. nd->path.dentry = dget(dentry);
  190. newmnt = afs_mntpt_do_automount(nd->path.dentry);
  191. if (IS_ERR(newmnt)) {
  192. path_put(&nd->path);
  193. return (void *)newmnt;
  194. }
  195. mntget(newmnt);
  196. err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
  197. switch (err) {
  198. case 0:
  199. path_put(&nd->path);
  200. nd->path.mnt = newmnt;
  201. nd->path.dentry = dget(newmnt->mnt_root);
  202. schedule_delayed_work(&afs_mntpt_expiry_timer,
  203. afs_mntpt_expiry_timeout * HZ);
  204. break;
  205. case -EBUSY:
  206. /* someone else made a mount here whilst we were busy */
  207. while (d_mountpoint(nd->path.dentry) &&
  208. follow_down(&nd->path))
  209. ;
  210. err = 0;
  211. default:
  212. mntput(newmnt);
  213. break;
  214. }
  215. _leave(" = %d", err);
  216. return ERR_PTR(err);
  217. }
  218. /*
  219. * handle mountpoint expiry timer going off
  220. */
  221. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  222. {
  223. _enter("");
  224. if (!list_empty(&afs_vfsmounts)) {
  225. mark_mounts_for_expiry(&afs_vfsmounts);
  226. schedule_delayed_work(&afs_mntpt_expiry_timer,
  227. afs_mntpt_expiry_timeout * HZ);
  228. }
  229. _leave("");
  230. }
  231. /*
  232. * kill the AFS mountpoint timer if it's still running
  233. */
  234. void afs_mntpt_kill_timer(void)
  235. {
  236. _enter("");
  237. ASSERT(list_empty(&afs_vfsmounts));
  238. cancel_delayed_work(&afs_mntpt_expiry_timer);
  239. flush_scheduled_work();
  240. }