mntpt.c 7.5 KB

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