dynroot.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* AFS dynamic root handling
  2. *
  3. * Copyright (C) 2018 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/namei.h>
  13. #include <linux/dns_resolver.h>
  14. #include "internal.h"
  15. const struct file_operations afs_dynroot_file_operations = {
  16. .open = dcache_dir_open,
  17. .release = dcache_dir_close,
  18. .iterate_shared = dcache_readdir,
  19. .llseek = dcache_dir_lseek,
  20. };
  21. /*
  22. * Probe to see if a cell may exist. This prevents positive dentries from
  23. * being created unnecessarily.
  24. */
  25. static int afs_probe_cell_name(struct dentry *dentry)
  26. {
  27. struct afs_cell *cell;
  28. const char *name = dentry->d_name.name;
  29. size_t len = dentry->d_name.len;
  30. int ret;
  31. /* Names prefixed with a dot are R/W mounts. */
  32. if (name[0] == '.') {
  33. if (len == 1)
  34. return -EINVAL;
  35. name++;
  36. len--;
  37. }
  38. cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len);
  39. if (!IS_ERR(cell)) {
  40. afs_put_cell(afs_d2net(dentry), cell);
  41. return 0;
  42. }
  43. ret = dns_query("afsdb", name, len, "", NULL, NULL);
  44. if (ret == -ENODATA)
  45. ret = -EDESTADDRREQ;
  46. return ret;
  47. }
  48. /*
  49. * Try to auto mount the mountpoint with pseudo directory, if the autocell
  50. * operation is setted.
  51. */
  52. struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
  53. {
  54. struct afs_vnode *vnode = AFS_FS_I(dir);
  55. struct inode *inode;
  56. int ret = -ENOENT;
  57. _enter("%p{%pd}, {%x:%u}",
  58. dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
  59. if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
  60. goto out;
  61. ret = afs_probe_cell_name(dentry);
  62. if (ret < 0)
  63. goto out;
  64. inode = afs_iget_pseudo_dir(dir->i_sb, false);
  65. if (IS_ERR(inode)) {
  66. ret = PTR_ERR(inode);
  67. goto out;
  68. }
  69. _leave("= %p", inode);
  70. return inode;
  71. out:
  72. _leave("= %d", ret);
  73. return ret == -ENOENT ? NULL : ERR_PTR(ret);
  74. }
  75. /*
  76. * Look up @cell in a dynroot directory. This is a substitution for the
  77. * local cell name for the net namespace.
  78. */
  79. static struct dentry *afs_lookup_atcell(struct dentry *dentry)
  80. {
  81. struct afs_cell *cell;
  82. struct afs_net *net = afs_d2net(dentry);
  83. struct dentry *ret;
  84. unsigned int seq = 0;
  85. char *name;
  86. int len;
  87. if (!net->ws_cell)
  88. return ERR_PTR(-ENOENT);
  89. ret = ERR_PTR(-ENOMEM);
  90. name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
  91. if (!name)
  92. goto out_p;
  93. rcu_read_lock();
  94. do {
  95. read_seqbegin_or_lock(&net->cells_lock, &seq);
  96. cell = rcu_dereference_raw(net->ws_cell);
  97. if (cell) {
  98. len = cell->name_len;
  99. memcpy(name, cell->name, len + 1);
  100. }
  101. } while (need_seqretry(&net->cells_lock, seq));
  102. done_seqretry(&net->cells_lock, seq);
  103. rcu_read_unlock();
  104. ret = ERR_PTR(-ENOENT);
  105. if (!cell)
  106. goto out_n;
  107. ret = lookup_one_len(name, dentry->d_parent, len);
  108. /* We don't want to d_add() the @cell dentry here as we don't want to
  109. * the cached dentry to hide changes to the local cell name.
  110. */
  111. out_n:
  112. kfree(name);
  113. out_p:
  114. return ret;
  115. }
  116. /*
  117. * Look up an entry in a dynroot directory.
  118. */
  119. static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
  120. unsigned int flags)
  121. {
  122. _enter("%pd", dentry);
  123. ASSERTCMP(d_inode(dentry), ==, NULL);
  124. if (flags & LOOKUP_CREATE)
  125. return ERR_PTR(-EOPNOTSUPP);
  126. if (dentry->d_name.len >= AFSNAMEMAX) {
  127. _leave(" = -ENAMETOOLONG");
  128. return ERR_PTR(-ENAMETOOLONG);
  129. }
  130. if (dentry->d_name.len == 5 &&
  131. memcmp(dentry->d_name.name, "@cell", 5) == 0)
  132. return afs_lookup_atcell(dentry);
  133. return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
  134. }
  135. const struct inode_operations afs_dynroot_inode_operations = {
  136. .lookup = afs_dynroot_lookup,
  137. };
  138. /*
  139. * Dirs in the dynamic root don't need revalidation.
  140. */
  141. static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
  142. {
  143. return 1;
  144. }
  145. /*
  146. * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  147. * sleep)
  148. * - called from dput() when d_count is going to 0.
  149. * - return 1 to request dentry be unhashed, 0 otherwise
  150. */
  151. static int afs_dynroot_d_delete(const struct dentry *dentry)
  152. {
  153. return d_really_is_positive(dentry);
  154. }
  155. const struct dentry_operations afs_dynroot_dentry_operations = {
  156. .d_revalidate = afs_dynroot_d_revalidate,
  157. .d_delete = afs_dynroot_d_delete,
  158. .d_release = afs_d_release,
  159. .d_automount = afs_d_automount,
  160. };
  161. /*
  162. * Create a manually added cell mount directory.
  163. * - The caller must hold net->proc_cells_lock
  164. */
  165. int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
  166. {
  167. struct super_block *sb = net->dynroot_sb;
  168. struct dentry *root, *subdir;
  169. int ret;
  170. if (!sb || atomic_read(&sb->s_active) == 0)
  171. return 0;
  172. /* Let the ->lookup op do the creation */
  173. root = sb->s_root;
  174. inode_lock(root->d_inode);
  175. subdir = lookup_one_len(cell->name, root, cell->name_len);
  176. if (IS_ERR(subdir)) {
  177. ret = PTR_ERR(subdir);
  178. goto unlock;
  179. }
  180. /* Note that we're retaining an extra ref on the dentry */
  181. subdir->d_fsdata = (void *)1UL;
  182. ret = 0;
  183. unlock:
  184. inode_unlock(root->d_inode);
  185. return ret;
  186. }
  187. /*
  188. * Remove a manually added cell mount directory.
  189. * - The caller must hold net->proc_cells_lock
  190. */
  191. void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
  192. {
  193. struct super_block *sb = net->dynroot_sb;
  194. struct dentry *root, *subdir;
  195. if (!sb || atomic_read(&sb->s_active) == 0)
  196. return;
  197. root = sb->s_root;
  198. inode_lock(root->d_inode);
  199. /* Don't want to trigger a lookup call, which will re-add the cell */
  200. subdir = try_lookup_one_len(cell->name, root, cell->name_len);
  201. if (IS_ERR_OR_NULL(subdir)) {
  202. _debug("lookup %ld", PTR_ERR(subdir));
  203. goto no_dentry;
  204. }
  205. _debug("rmdir %pd %u", subdir, d_count(subdir));
  206. if (subdir->d_fsdata) {
  207. _debug("unpin %u", d_count(subdir));
  208. subdir->d_fsdata = NULL;
  209. dput(subdir);
  210. }
  211. dput(subdir);
  212. no_dentry:
  213. inode_unlock(root->d_inode);
  214. _leave("");
  215. }
  216. /*
  217. * Populate a newly created dynamic root with cell names.
  218. */
  219. int afs_dynroot_populate(struct super_block *sb)
  220. {
  221. struct afs_cell *cell;
  222. struct afs_net *net = afs_sb2net(sb);
  223. int ret;
  224. if (mutex_lock_interruptible(&net->proc_cells_lock) < 0)
  225. return -ERESTARTSYS;
  226. net->dynroot_sb = sb;
  227. hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
  228. ret = afs_dynroot_mkdir(net, cell);
  229. if (ret < 0)
  230. goto error;
  231. }
  232. ret = 0;
  233. out:
  234. mutex_unlock(&net->proc_cells_lock);
  235. return ret;
  236. error:
  237. net->dynroot_sb = NULL;
  238. goto out;
  239. }
  240. /*
  241. * When a dynamic root that's in the process of being destroyed, depopulate it
  242. * of pinned directories.
  243. */
  244. void afs_dynroot_depopulate(struct super_block *sb)
  245. {
  246. struct afs_net *net = afs_sb2net(sb);
  247. struct dentry *root = sb->s_root, *subdir, *tmp;
  248. /* Prevent more subdirs from being created */
  249. mutex_lock(&net->proc_cells_lock);
  250. if (net->dynroot_sb == sb)
  251. net->dynroot_sb = NULL;
  252. mutex_unlock(&net->proc_cells_lock);
  253. inode_lock(root->d_inode);
  254. /* Remove all the pins for dirs created for manually added cells */
  255. list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
  256. if (subdir->d_fsdata) {
  257. subdir->d_fsdata = NULL;
  258. dput(subdir);
  259. }
  260. }
  261. inode_unlock(root->d_inode);
  262. }