mntpt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 = afs_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->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,
  94. AFS_LOOKUP_CELL_MOUNTPOINT,
  95. afs_cell_trace_use_lookup_mntpt);
  96. if (IS_ERR(cell)) {
  97. pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
  98. return PTR_ERR(cell);
  99. }
  100. ctx->cell = cell;
  101. ctx->volname = afs_root_volume;
  102. ctx->volnamesz = sizeof(afs_root_volume) - 1;
  103. } else {
  104. /* read the contents of the AFS special symlink */
  105. DEFINE_DELAYED_CALL(cleanup);
  106. const char *content;
  107. loff_t size = i_size_read(d_inode(mntpt));
  108. if (src_as->cell)
  109. ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
  110. if (size < 2 || size > PAGE_SIZE - 1)
  111. return -EINVAL;
  112. content = afs_get_link(mntpt, d_inode(mntpt), &cleanup);
  113. if (IS_ERR(content)) {
  114. do_delayed_call(&cleanup);
  115. return PTR_ERR(content);
  116. }
  117. ret = -EINVAL;
  118. if (content[size - 1] == '.')
  119. ret = vfs_parse_fs_qstr(fc, "source",
  120. &QSTR_LEN(content, size - 1));
  121. do_delayed_call(&cleanup);
  122. if (ret < 0)
  123. return ret;
  124. /* Don't cross a backup volume mountpoint from a backup volume */
  125. if (src_as->volume && src_as->volume->type == AFSVL_BACKVOL &&
  126. ctx->type == AFSVL_BACKVOL)
  127. return -ENODEV;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * create a vfsmount to be automounted
  133. */
  134. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  135. {
  136. struct fs_context *fc;
  137. struct vfsmount *mnt;
  138. int ret;
  139. BUG_ON(!d_inode(mntpt));
  140. fc = fs_context_for_submount(&afs_fs_type, mntpt);
  141. if (IS_ERR(fc))
  142. return ERR_CAST(fc);
  143. ret = afs_mntpt_set_params(fc, mntpt);
  144. if (!ret)
  145. mnt = fc_mount(fc);
  146. else
  147. mnt = ERR_PTR(ret);
  148. put_fs_context(fc);
  149. return mnt;
  150. }
  151. /*
  152. * handle an automount point
  153. */
  154. struct vfsmount *afs_d_automount(struct path *path)
  155. {
  156. struct vfsmount *newmnt;
  157. _enter("{%pd}", path->dentry);
  158. newmnt = afs_mntpt_do_automount(path->dentry);
  159. if (IS_ERR(newmnt))
  160. return newmnt;
  161. mnt_set_expiry(newmnt, &afs_vfsmounts);
  162. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  163. afs_mntpt_expiry_timeout * HZ);
  164. _leave(" = %p", newmnt);
  165. return newmnt;
  166. }
  167. /*
  168. * handle mountpoint expiry timer going off
  169. */
  170. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  171. {
  172. _enter("");
  173. if (!list_empty(&afs_vfsmounts)) {
  174. mark_mounts_for_expiry(&afs_vfsmounts);
  175. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  176. afs_mntpt_expiry_timeout * HZ);
  177. }
  178. _leave("");
  179. }
  180. /*
  181. * kill the AFS mountpoint timer if it's still running
  182. */
  183. void afs_mntpt_kill_timer(void)
  184. {
  185. _enter("");
  186. ASSERT(list_empty(&afs_vfsmounts));
  187. cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
  188. }