1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS dynamic root handling
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/dns_resolver.h>
11 #include "internal.h"
12
13 static atomic_t afs_autocell_ino;
14
15 /*
16 * iget5() comparator for inode created by autocell operations
17 *
18 * These pseudo inodes don't match anything.
19 */
afs_iget5_pseudo_test(struct inode * inode,void * opaque)20 static int afs_iget5_pseudo_test(struct inode *inode, void *opaque)
21 {
22 return 0;
23 }
24
25 /*
26 * iget5() inode initialiser
27 */
afs_iget5_pseudo_set(struct inode * inode,void * opaque)28 static int afs_iget5_pseudo_set(struct inode *inode, void *opaque)
29 {
30 struct afs_super_info *as = AFS_FS_S(inode->i_sb);
31 struct afs_vnode *vnode = AFS_FS_I(inode);
32 struct afs_fid *fid = opaque;
33
34 vnode->volume = as->volume;
35 vnode->fid = *fid;
36 inode->i_ino = fid->vnode;
37 inode->i_generation = fid->unique;
38 return 0;
39 }
40
41 /*
42 * Create an inode for a dynamic root directory or an autocell dynamic
43 * automount dir.
44 */
afs_iget_pseudo_dir(struct super_block * sb,bool root)45 struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
46 {
47 struct afs_super_info *as = AFS_FS_S(sb);
48 struct afs_vnode *vnode;
49 struct inode *inode;
50 struct afs_fid fid = {};
51
52 _enter("");
53
54 if (as->volume)
55 fid.vid = as->volume->vid;
56 if (root) {
57 fid.vnode = 1;
58 fid.unique = 1;
59 } else {
60 fid.vnode = atomic_inc_return(&afs_autocell_ino);
61 fid.unique = 0;
62 }
63
64 inode = iget5_locked(sb, fid.vnode,
65 afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
66 if (!inode) {
67 _leave(" = -ENOMEM");
68 return ERR_PTR(-ENOMEM);
69 }
70
71 _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
72 inode, inode->i_ino, fid.vid, fid.vnode, fid.unique);
73
74 vnode = AFS_FS_I(inode);
75
76 /* there shouldn't be an existing inode */
77 BUG_ON(!(inode->i_state & I_NEW));
78
79 netfs_inode_init(&vnode->netfs, NULL, false);
80 inode->i_size = 0;
81 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
82 if (root) {
83 inode->i_op = &afs_dynroot_inode_operations;
84 inode->i_fop = &simple_dir_operations;
85 } else {
86 inode->i_op = &afs_autocell_inode_operations;
87 }
88 set_nlink(inode, 2);
89 inode->i_uid = GLOBAL_ROOT_UID;
90 inode->i_gid = GLOBAL_ROOT_GID;
91 simple_inode_init_ts(inode);
92 inode->i_blocks = 0;
93 inode->i_generation = 0;
94
95 set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
96 if (!root) {
97 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
98 inode->i_flags |= S_AUTOMOUNT;
99 }
100
101 inode->i_flags |= S_NOATIME;
102 unlock_new_inode(inode);
103 _leave(" = %p", inode);
104 return inode;
105 }
106
107 /*
108 * Probe to see if a cell may exist. This prevents positive dentries from
109 * being created unnecessarily.
110 */
afs_probe_cell_name(struct dentry * dentry)111 static int afs_probe_cell_name(struct dentry *dentry)
112 {
113 struct afs_cell *cell;
114 struct afs_net *net = afs_d2net(dentry);
115 const char *name = dentry->d_name.name;
116 size_t len = dentry->d_name.len;
117 char *result = NULL;
118 int ret;
119
120 /* Names prefixed with a dot are R/W mounts. */
121 if (name[0] == '.') {
122 if (len == 1)
123 return -EINVAL;
124 name++;
125 len--;
126 }
127
128 cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe);
129 if (!IS_ERR(cell)) {
130 afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe);
131 return 0;
132 }
133
134 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
135 &result, NULL, false);
136 if (ret == -ENODATA || ret == -ENOKEY || ret == 0)
137 ret = -ENOENT;
138 if (ret > 0 && ret >= sizeof(struct dns_server_list_v1_header)) {
139 struct dns_server_list_v1_header *v1 = (void *)result;
140
141 if (v1->hdr.zero == 0 &&
142 v1->hdr.content == DNS_PAYLOAD_IS_SERVER_LIST &&
143 v1->hdr.version == 1 &&
144 (v1->status != DNS_LOOKUP_GOOD &&
145 v1->status != DNS_LOOKUP_GOOD_WITH_BAD))
146 return -ENOENT;
147
148 }
149
150 kfree(result);
151 return ret;
152 }
153
154 /*
155 * Try to auto mount the mountpoint with pseudo directory, if the autocell
156 * operation is setted.
157 */
afs_try_auto_mntpt(struct dentry * dentry,struct inode * dir)158 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
159 {
160 struct afs_vnode *vnode = AFS_FS_I(dir);
161 struct inode *inode;
162 int ret = -ENOENT;
163
164 _enter("%p{%pd}, {%llx:%llu}",
165 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
166
167 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
168 goto out;
169
170 ret = afs_probe_cell_name(dentry);
171 if (ret < 0)
172 goto out;
173
174 inode = afs_iget_pseudo_dir(dir->i_sb, false);
175 if (IS_ERR(inode)) {
176 ret = PTR_ERR(inode);
177 goto out;
178 }
179
180 _leave("= %p", inode);
181 return inode;
182
183 out:
184 _leave("= %d", ret);
185 return ret == -ENOENT ? NULL : ERR_PTR(ret);
186 }
187
188 /*
189 * Look up an entry in a dynroot directory.
190 */
afs_dynroot_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)191 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
192 unsigned int flags)
193 {
194 _enter("%pd", dentry);
195
196 ASSERTCMP(d_inode(dentry), ==, NULL);
197
198 if (flags & LOOKUP_CREATE)
199 return ERR_PTR(-EOPNOTSUPP);
200
201 if (dentry->d_name.len >= AFSNAMEMAX) {
202 _leave(" = -ENAMETOOLONG");
203 return ERR_PTR(-ENAMETOOLONG);
204 }
205
206 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
207 }
208
209 const struct inode_operations afs_dynroot_inode_operations = {
210 .lookup = afs_dynroot_lookup,
211 };
212
213 const struct dentry_operations afs_dynroot_dentry_operations = {
214 .d_delete = always_delete_dentry,
215 .d_release = afs_d_release,
216 .d_automount = afs_d_automount,
217 };
218
219 /*
220 * Create a manually added cell mount directory.
221 * - The caller must hold net->proc_cells_lock
222 */
afs_dynroot_mkdir(struct afs_net * net,struct afs_cell * cell)223 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
224 {
225 struct super_block *sb = net->dynroot_sb;
226 struct dentry *root, *subdir, *dsubdir;
227 char *dotname = cell->name - 1;
228 int ret;
229
230 if (!sb || atomic_read(&sb->s_active) == 0)
231 return 0;
232
233 /* Let the ->lookup op do the creation */
234 root = sb->s_root;
235 inode_lock(root->d_inode);
236 subdir = lookup_one_len(cell->name, root, cell->name_len);
237 if (IS_ERR(subdir)) {
238 ret = PTR_ERR(subdir);
239 goto unlock;
240 }
241
242 dsubdir = lookup_one_len(dotname, root, cell->name_len + 1);
243 if (IS_ERR(dsubdir)) {
244 ret = PTR_ERR(dsubdir);
245 dput(subdir);
246 goto unlock;
247 }
248
249 /* Note that we're retaining extra refs on the dentries. */
250 subdir->d_fsdata = (void *)1UL;
251 dsubdir->d_fsdata = (void *)1UL;
252 ret = 0;
253 unlock:
254 inode_unlock(root->d_inode);
255 return ret;
256 }
257
afs_dynroot_rm_one_dir(struct dentry * root,const char * name,size_t name_len)258 static void afs_dynroot_rm_one_dir(struct dentry *root, const char *name, size_t name_len)
259 {
260 struct dentry *subdir;
261
262 /* Don't want to trigger a lookup call, which will re-add the cell */
263 subdir = try_lookup_one_len(name, root, name_len);
264 if (IS_ERR_OR_NULL(subdir)) {
265 _debug("lookup %ld", PTR_ERR(subdir));
266 return;
267 }
268
269 _debug("rmdir %pd %u", subdir, d_count(subdir));
270
271 if (subdir->d_fsdata) {
272 _debug("unpin %u", d_count(subdir));
273 subdir->d_fsdata = NULL;
274 dput(subdir);
275 }
276 dput(subdir);
277 }
278
279 /*
280 * Remove a manually added cell mount directory.
281 * - The caller must hold net->proc_cells_lock
282 */
afs_dynroot_rmdir(struct afs_net * net,struct afs_cell * cell)283 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
284 {
285 struct super_block *sb = net->dynroot_sb;
286 char *dotname = cell->name - 1;
287
288 if (!sb || atomic_read(&sb->s_active) == 0)
289 return;
290
291 inode_lock(sb->s_root->d_inode);
292 afs_dynroot_rm_one_dir(sb->s_root, cell->name, cell->name_len);
293 afs_dynroot_rm_one_dir(sb->s_root, dotname, cell->name_len + 1);
294 inode_unlock(sb->s_root->d_inode);
295 _leave("");
296 }
297
afs_atcell_delayed_put_cell(void * arg)298 static void afs_atcell_delayed_put_cell(void *arg)
299 {
300 struct afs_cell *cell = arg;
301
302 afs_put_cell(cell, afs_cell_trace_put_atcell);
303 }
304
305 /*
306 * Read @cell or .@cell symlinks.
307 */
afs_atcell_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)308 static const char *afs_atcell_get_link(struct dentry *dentry, struct inode *inode,
309 struct delayed_call *done)
310 {
311 struct afs_vnode *vnode = AFS_FS_I(inode);
312 struct afs_cell *cell;
313 struct afs_net *net = afs_i2net(inode);
314 const char *name;
315 bool dotted = vnode->fid.vnode == 3;
316
317 if (!dentry) {
318 /* We're in RCU-pathwalk. */
319 cell = rcu_dereference(net->ws_cell);
320 if (dotted)
321 name = cell->name - 1;
322 else
323 name = cell->name;
324 /* Shouldn't need to set a delayed call. */
325 return name;
326 }
327
328 if (!rcu_access_pointer(net->ws_cell))
329 return ERR_PTR(-ENOENT);
330
331 down_read(&net->cells_lock);
332
333 cell = rcu_dereference_protected(net->ws_cell, lockdep_is_held(&net->cells_lock));
334 if (dotted)
335 name = cell->name - 1;
336 else
337 name = cell->name;
338 afs_get_cell(cell, afs_cell_trace_get_atcell);
339 set_delayed_call(done, afs_atcell_delayed_put_cell, cell);
340
341 up_read(&net->cells_lock);
342 return name;
343 }
344
345 static const struct inode_operations afs_atcell_inode_operations = {
346 .get_link = afs_atcell_get_link,
347 };
348
349 /*
350 * Look up @cell or .@cell in a dynroot directory. This is a substitution for
351 * the local cell name for the net namespace.
352 */
afs_dynroot_create_symlink(struct dentry * root,const char * name)353 static struct dentry *afs_dynroot_create_symlink(struct dentry *root, const char *name)
354 {
355 struct afs_vnode *vnode;
356 struct afs_fid fid = { .vnode = 2, .unique = 1, };
357 struct dentry *dentry;
358 struct inode *inode;
359
360 if (name[0] == '.')
361 fid.vnode = 3;
362
363 dentry = d_alloc_name(root, name);
364 if (!dentry)
365 return ERR_PTR(-ENOMEM);
366
367 inode = iget5_locked(dentry->d_sb, fid.vnode,
368 afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
369 if (!inode) {
370 dput(dentry);
371 return ERR_PTR(-ENOMEM);
372 }
373
374 vnode = AFS_FS_I(inode);
375
376 /* there shouldn't be an existing inode */
377 if (WARN_ON_ONCE(!(inode->i_state & I_NEW))) {
378 iput(inode);
379 dput(dentry);
380 return ERR_PTR(-EIO);
381 }
382
383 netfs_inode_init(&vnode->netfs, NULL, false);
384 simple_inode_init_ts(inode);
385 set_nlink(inode, 1);
386 inode->i_size = 0;
387 inode->i_mode = S_IFLNK | 0555;
388 inode->i_op = &afs_atcell_inode_operations;
389 inode->i_uid = GLOBAL_ROOT_UID;
390 inode->i_gid = GLOBAL_ROOT_GID;
391 inode->i_blocks = 0;
392 inode->i_generation = 0;
393 inode->i_flags |= S_NOATIME;
394
395 unlock_new_inode(inode);
396 d_splice_alias(inode, dentry);
397 return dentry;
398 }
399
400 /*
401 * Create @cell and .@cell symlinks.
402 */
afs_dynroot_symlink(struct afs_net * net)403 static int afs_dynroot_symlink(struct afs_net *net)
404 {
405 struct super_block *sb = net->dynroot_sb;
406 struct dentry *root, *symlink, *dsymlink;
407 int ret;
408
409 /* Let the ->lookup op do the creation */
410 root = sb->s_root;
411 inode_lock(root->d_inode);
412 symlink = afs_dynroot_create_symlink(root, "@cell");
413 if (IS_ERR(symlink)) {
414 ret = PTR_ERR(symlink);
415 goto unlock;
416 }
417
418 dsymlink = afs_dynroot_create_symlink(root, ".@cell");
419 if (IS_ERR(dsymlink)) {
420 ret = PTR_ERR(dsymlink);
421 dput(symlink);
422 goto unlock;
423 }
424
425 /* Note that we're retaining extra refs on the dentries. */
426 symlink->d_fsdata = (void *)1UL;
427 dsymlink->d_fsdata = (void *)1UL;
428 ret = 0;
429 unlock:
430 inode_unlock(root->d_inode);
431 return ret;
432 }
433
434 /*
435 * Populate a newly created dynamic root with cell names.
436 */
afs_dynroot_populate(struct super_block * sb)437 int afs_dynroot_populate(struct super_block *sb)
438 {
439 struct afs_cell *cell;
440 struct afs_net *net = afs_sb2net(sb);
441 int ret;
442
443 mutex_lock(&net->proc_cells_lock);
444
445 net->dynroot_sb = sb;
446 ret = afs_dynroot_symlink(net);
447 if (ret < 0)
448 goto error;
449
450 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
451 ret = afs_dynroot_mkdir(net, cell);
452 if (ret < 0)
453 goto error;
454 }
455
456 ret = 0;
457 out:
458 mutex_unlock(&net->proc_cells_lock);
459 return ret;
460
461 error:
462 net->dynroot_sb = NULL;
463 goto out;
464 }
465
466 /*
467 * When a dynamic root that's in the process of being destroyed, depopulate it
468 * of pinned directories.
469 */
afs_dynroot_depopulate(struct super_block * sb)470 void afs_dynroot_depopulate(struct super_block *sb)
471 {
472 struct afs_net *net = afs_sb2net(sb);
473 struct dentry *root = sb->s_root, *subdir;
474
475 /* Prevent more subdirs from being created */
476 mutex_lock(&net->proc_cells_lock);
477 if (net->dynroot_sb == sb)
478 net->dynroot_sb = NULL;
479 mutex_unlock(&net->proc_cells_lock);
480
481 if (root) {
482 struct hlist_node *n;
483 inode_lock(root->d_inode);
484
485 /* Remove all the pins for dirs created for manually added cells */
486 hlist_for_each_entry_safe(subdir, n, &root->d_children, d_sib) {
487 if (subdir->d_fsdata) {
488 subdir->d_fsdata = NULL;
489 dput(subdir);
490 }
491 }
492
493 inode_unlock(root->d_inode);
494 }
495 }
496