mntpt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* mountpoint management
  3. *
  4. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/gfp.h>
  15. #include <linux/fs_context.h>
  16. #include "internal.h"
  17. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  18. struct dentry *dentry,
  19. unsigned int flags);
  20. static int afs_mntpt_open(struct inode *inode, struct file *file);
  21. static void afs_mntpt_expiry_timed_out(struct work_struct *work);
  22. const struct file_operations afs_mntpt_file_operations = {
  23. .open = afs_mntpt_open,
  24. .llseek = noop_llseek,
  25. };
  26. const struct inode_operations afs_mntpt_inode_operations = {
  27. .lookup = afs_mntpt_lookup,
  28. .readlink = page_readlink,
  29. .getattr = afs_getattr,
  30. };
  31. const struct inode_operations afs_autocell_inode_operations = {
  32. .getattr = afs_getattr,
  33. };
  34. static LIST_HEAD(afs_vfsmounts);
  35. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  36. static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  37. static const char afs_root_volume[] = "root.cell";
  38. /*
  39. * no valid lookup procedure on this sort of dir
  40. */
  41. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  42. struct dentry *dentry,
  43. unsigned int flags)
  44. {
  45. _enter("%p,%p{%pd2}", dir, dentry, dentry);
  46. return ERR_PTR(-EREMOTE);
  47. }
  48. /*
  49. * no valid open procedure on this sort of dir
  50. */
  51. static int afs_mntpt_open(struct inode *inode, struct file *file)
  52. {
  53. _enter("%p,%p{%pD2}", inode, file, file);
  54. return -EREMOTE;
  55. }
  56. /*
  57. * Set the parameters for the proposed superblock.
  58. */
  59. static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
  60. {
  61. struct afs_fs_context *ctx = fc->fs_private;
  62. struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
  63. struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
  64. struct afs_cell *cell;
  65. const char *p;
  66. int ret;
  67. if (fc->net_ns != src_as->net_ns) {
  68. put_net(fc->net_ns);
  69. fc->net_ns = get_net(src_as->net_ns);
  70. }
  71. if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
  72. ctx->type = AFSVL_RWVOL;
  73. ctx->force = true;
  74. }
  75. if (ctx->cell) {
  76. afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt);
  77. ctx->cell = NULL;
  78. }
  79. if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
  80. /* if the directory is a pseudo directory, use the d_name */
  81. unsigned size = mntpt->d_name.len;
  82. if (size < 2)
  83. return -ENOENT;
  84. p = mntpt->d_name.name;
  85. if (mntpt->d_name.name[0] == '.') {
  86. size--;
  87. p++;
  88. ctx->type = AFSVL_RWVOL;
  89. ctx->force = true;
  90. }
  91. if (size > AFS_MAXCELLNAME)
  92. return -ENAMETOOLONG;
  93. cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
  94. if (IS_ERR(cell)) {
  95. pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
  96. return PTR_ERR(cell);
  97. }
  98. ctx->cell = cell;
  99. ctx->volname = afs_root_volume;
  100. ctx->volnamesz = sizeof(afs_root_volume) - 1;
  101. } else {
  102. /* read the contents of the AFS special symlink */
  103. struct page *page;
  104. loff_t size = i_size_read(d_inode(mntpt));
  105. char *buf;
  106. if (src_as->cell)
  107. ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
  108. if (size < 2 || size > PAGE_SIZE - 1)
  109. return -EINVAL;
  110. page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
  111. if (IS_ERR(page))
  112. return PTR_ERR(page);
  113. if (PageError(page)) {
  114. ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
  115. put_page(page);
  116. return ret;
  117. }
  118. buf = kmap(page);
  119. ret = -EINVAL;
  120. if (buf[size - 1] == '.')
  121. ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
  122. kunmap(page);
  123. put_page(page);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * create a vfsmount to be automounted
  131. */
  132. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  133. {
  134. struct fs_context *fc;
  135. struct vfsmount *mnt;
  136. int ret;
  137. BUG_ON(!d_inode(mntpt));
  138. fc = fs_context_for_submount(&afs_fs_type, mntpt);
  139. if (IS_ERR(fc))
  140. return ERR_CAST(fc);
  141. ret = afs_mntpt_set_params(fc, mntpt);
  142. if (!ret)
  143. mnt = fc_mount(fc);
  144. else
  145. mnt = ERR_PTR(ret);
  146. put_fs_context(fc);
  147. return mnt;
  148. }
  149. /*
  150. * handle an automount point
  151. */
  152. struct vfsmount *afs_d_automount(struct path *path)
  153. {
  154. struct vfsmount *newmnt;
  155. _enter("{%pd}", path->dentry);
  156. newmnt = afs_mntpt_do_automount(path->dentry);
  157. if (IS_ERR(newmnt))
  158. return newmnt;
  159. mntget(newmnt); /* prevent immediate expiration */
  160. mnt_set_expiry(newmnt, &afs_vfsmounts);
  161. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  162. afs_mntpt_expiry_timeout * HZ);
  163. _leave(" = %p", newmnt);
  164. return newmnt;
  165. }
  166. /*
  167. * handle mountpoint expiry timer going off
  168. */
  169. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  170. {
  171. _enter("");
  172. if (!list_empty(&afs_vfsmounts)) {
  173. mark_mounts_for_expiry(&afs_vfsmounts);
  174. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  175. afs_mntpt_expiry_timeout * HZ);
  176. }
  177. _leave("");
  178. }
  179. /*
  180. * kill the AFS mountpoint timer if it's still running
  181. */
  182. void afs_mntpt_kill_timer(void)
  183. {
  184. _enter("");
  185. ASSERT(list_empty(&afs_vfsmounts));
  186. cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
  187. }