mntpt.c 6.7 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/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_expiry_timed_out(struct work_struct *work);
  25. const struct file_operations afs_mntpt_file_operations = {
  26. .open = afs_mntpt_open,
  27. .llseek = noop_llseek,
  28. };
  29. const struct inode_operations afs_mntpt_inode_operations = {
  30. .lookup = afs_mntpt_lookup,
  31. .readlink = page_readlink,
  32. .getattr = afs_getattr,
  33. };
  34. const struct inode_operations afs_autocell_inode_operations = {
  35. .getattr = afs_getattr,
  36. };
  37. static LIST_HEAD(afs_vfsmounts);
  38. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  39. static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  40. /*
  41. * check a symbolic link to see whether it actually encodes a mountpoint
  42. * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
  43. */
  44. int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
  45. {
  46. struct page *page;
  47. size_t size;
  48. char *buf;
  49. int ret;
  50. _enter("{%x:%u,%u}",
  51. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  52. /* read the contents of the symlink into the pagecache */
  53. page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
  54. afs_page_filler, key);
  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. vnode->vfs_inode.i_flags |= S_AUTOMOUNT;
  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 afs_vnode *vnode;
  122. struct page *page;
  123. char *devname, *options;
  124. bool rwpath = false;
  125. int ret;
  126. _enter("{%s}", mntpt->d_name.name);
  127. BUG_ON(!mntpt->d_inode);
  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. vnode = AFS_FS_I(mntpt->d_inode);
  136. if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
  137. /* if the directory is a pseudo directory, use the d_name */
  138. static const char afs_root_cell[] = ":root.cell.";
  139. unsigned size = mntpt->d_name.len;
  140. ret = -ENOENT;
  141. if (size < 2 || size > AFS_MAXCELLNAME)
  142. goto error_no_page;
  143. if (mntpt->d_name.name[0] == '.') {
  144. devname[0] = '#';
  145. memcpy(devname + 1, mntpt->d_name.name, size - 1);
  146. memcpy(devname + size, afs_root_cell,
  147. sizeof(afs_root_cell));
  148. rwpath = true;
  149. } else {
  150. devname[0] = '%';
  151. memcpy(devname + 1, mntpt->d_name.name, size);
  152. memcpy(devname + size + 1, afs_root_cell,
  153. sizeof(afs_root_cell));
  154. }
  155. } else {
  156. /* read the contents of the AFS special symlink */
  157. loff_t size = i_size_read(mntpt->d_inode);
  158. char *buf;
  159. ret = -EINVAL;
  160. if (size > PAGE_SIZE - 1)
  161. goto error_no_page;
  162. page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
  163. if (IS_ERR(page)) {
  164. ret = PTR_ERR(page);
  165. goto error_no_page;
  166. }
  167. ret = -EIO;
  168. if (PageError(page))
  169. goto error;
  170. buf = kmap_atomic(page);
  171. memcpy(devname, buf, size);
  172. kunmap_atomic(buf);
  173. page_cache_release(page);
  174. page = NULL;
  175. }
  176. /* work out what options we want */
  177. super = AFS_FS_S(mntpt->d_sb);
  178. memcpy(options, "cell=", 5);
  179. strcpy(options + 5, super->volume->cell->name);
  180. if (super->volume->type == AFSVL_RWVOL || rwpath)
  181. strcat(options, ",rwpath");
  182. /* try and do the mount */
  183. _debug("--- attempting mount %s -o %s ---", devname, options);
  184. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  185. _debug("--- mount result %p ---", mnt);
  186. free_page((unsigned long) devname);
  187. free_page((unsigned long) options);
  188. _leave(" = %p", mnt);
  189. return mnt;
  190. error:
  191. page_cache_release(page);
  192. error_no_page:
  193. free_page((unsigned long) options);
  194. error_no_options:
  195. free_page((unsigned long) devname);
  196. error_no_devname:
  197. _leave(" = %d", ret);
  198. return ERR_PTR(ret);
  199. }
  200. /*
  201. * handle an automount point
  202. */
  203. struct vfsmount *afs_d_automount(struct path *path)
  204. {
  205. struct vfsmount *newmnt;
  206. _enter("{%s}", path->dentry->d_name.name);
  207. newmnt = afs_mntpt_do_automount(path->dentry);
  208. if (IS_ERR(newmnt))
  209. return newmnt;
  210. mntget(newmnt); /* prevent immediate expiration */
  211. mnt_set_expiry(newmnt, &afs_vfsmounts);
  212. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  213. afs_mntpt_expiry_timeout * HZ);
  214. _leave(" = %p", newmnt);
  215. return newmnt;
  216. }
  217. /*
  218. * handle mountpoint expiry timer going off
  219. */
  220. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  221. {
  222. _enter("");
  223. if (!list_empty(&afs_vfsmounts)) {
  224. mark_mounts_for_expiry(&afs_vfsmounts);
  225. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  226. afs_mntpt_expiry_timeout * HZ);
  227. }
  228. _leave("");
  229. }
  230. /*
  231. * kill the AFS mountpoint timer if it's still running
  232. */
  233. void afs_mntpt_kill_timer(void)
  234. {
  235. _enter("");
  236. ASSERT(list_empty(&afs_vfsmounts));
  237. cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
  238. }