]> Git Repo - linux.git/blame - fs/afs/dynroot.c
Linux 6.14-rc3
[linux.git] / fs / afs / dynroot.c
CommitLineData
b4d0d230 1// SPDX-License-Identifier: GPL-2.0-or-later
0da0b7fd 2/* AFS dynamic root handling
66c7e1d3
DH
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells ([email protected])
66c7e1d3
DH
6 */
7
8#include <linux/fs.h>
9#include <linux/namei.h>
10#include <linux/dns_resolver.h>
11#include "internal.h"
12
e49c7b2f
DH
13static 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 */
20static int afs_iget5_pseudo_test(struct inode *inode, void *opaque)
21{
22 return 0;
23}
24
25/*
26 * iget5() inode initialiser
27 */
28static 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 */
45struct 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
100ccd18 79 netfs_inode_init(&vnode->netfs, NULL, false);
e49c7b2f
DH
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;
562ce1f7 91 simple_inode_init_ts(inode);
e49c7b2f
DH
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
66c7e1d3
DH
107/*
108 * Probe to see if a cell may exist. This prevents positive dentries from
109 * being created unnecessarily.
110 */
111static int afs_probe_cell_name(struct dentry *dentry)
112{
113 struct afs_cell *cell;
a58946c1 114 struct afs_net *net = afs_d2net(dentry);
66c7e1d3
DH
115 const char *name = dentry->d_name.name;
116 size_t len = dentry->d_name.len;
74cef687 117 char *result = NULL;
66c7e1d3
DH
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
dca54a7b 128 cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe);
66c7e1d3 129 if (!IS_ERR(cell)) {
dca54a7b 130 afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe);
66c7e1d3
DH
131 return 0;
132 }
133
a58946c1 134 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
74cef687
DH
135 &result, NULL, false);
136 if (ret == -ENODATA || ret == -ENOKEY || ret == 0)
2a4ca1b4 137 ret = -ENOENT;
74cef687
DH
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);
66c7e1d3
DH
151 return ret;
152}
153
154/*
155 * Try to auto mount the mountpoint with pseudo directory, if the autocell
156 * operation is setted.
157 */
158struct 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
3b6492df 164 _enter("%p{%pd}, {%llx:%llu}",
66c7e1d3
DH
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
183out:
184 _leave("= %d", ret);
1401a0fc 185 return ret == -ENOENT ? NULL : ERR_PTR(ret);
66c7e1d3
DH
186}
187
66c7e1d3
DH
188/*
189 * Look up an entry in a dynroot directory.
190 */
191static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
192 unsigned int flags)
193{
66c7e1d3
DH
194 _enter("%pd", dentry);
195
196 ASSERTCMP(d_inode(dentry), ==, NULL);
197
1da4bd9f
DH
198 if (flags & LOOKUP_CREATE)
199 return ERR_PTR(-EOPNOTSUPP);
200
66c7e1d3
DH
201 if (dentry->d_name.len >= AFSNAMEMAX) {
202 _leave(" = -ENAMETOOLONG");
203 return ERR_PTR(-ENAMETOOLONG);
204 }
205
1401a0fc 206 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
66c7e1d3
DH
207}
208
209const struct inode_operations afs_dynroot_inode_operations = {
210 .lookup = afs_dynroot_lookup,
211};
212
66c7e1d3 213const struct dentry_operations afs_dynroot_dentry_operations = {
71f8b55b 214 .d_delete = always_delete_dentry,
66c7e1d3
DH
215 .d_release = afs_d_release,
216 .d_automount = afs_d_automount,
217};
0da0b7fd
DH
218
219/*
220 * Create a manually added cell mount directory.
221 * - The caller must hold net->proc_cells_lock
222 */
223int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
224{
225 struct super_block *sb = net->dynroot_sb;
92f08e9d
DH
226 struct dentry *root, *subdir, *dsubdir;
227 char *dotname = cell->name - 1;
0da0b7fd
DH
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
92f08e9d
DH
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. */
0da0b7fd 250 subdir->d_fsdata = (void *)1UL;
92f08e9d 251 dsubdir->d_fsdata = (void *)1UL;
0da0b7fd
DH
252 ret = 0;
253unlock:
254 inode_unlock(root->d_inode);
255 return ret;
256}
257
92f08e9d 258static void afs_dynroot_rm_one_dir(struct dentry *root, const char *name, size_t name_len)
0da0b7fd 259{
92f08e9d 260 struct dentry *subdir;
0da0b7fd
DH
261
262 /* Don't want to trigger a lookup call, which will re-add the cell */
92f08e9d 263 subdir = try_lookup_one_len(name, root, name_len);
0da0b7fd
DH
264 if (IS_ERR_OR_NULL(subdir)) {
265 _debug("lookup %ld", PTR_ERR(subdir));
92f08e9d 266 return;
0da0b7fd
DH
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);
92f08e9d
DH
277}
278
279/*
280 * Remove a manually added cell mount directory.
281 * - The caller must hold net->proc_cells_lock
282 */
283void 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);
0da0b7fd
DH
295 _leave("");
296}
297
30bca65b
DH
298static 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 */
308static 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 (!net->ws_cell)
318 return ERR_PTR(-ENOENT);
319
320 down_read(&net->cells_lock);
321
322 cell = net->ws_cell;
323 if (dotted)
324 name = cell->name - 1;
325 else
326 name = cell->name;
327 afs_get_cell(cell, afs_cell_trace_get_atcell);
328 set_delayed_call(done, afs_atcell_delayed_put_cell, cell);
329
330 up_read(&net->cells_lock);
331 return name;
332}
333
334static const struct inode_operations afs_atcell_inode_operations = {
335 .get_link = afs_atcell_get_link,
336};
337
338/*
339 * Look up @cell or .@cell in a dynroot directory. This is a substitution for
340 * the local cell name for the net namespace.
341 */
342static struct dentry *afs_dynroot_create_symlink(struct dentry *root, const char *name)
343{
344 struct afs_vnode *vnode;
345 struct afs_fid fid = { .vnode = 2, .unique = 1, };
346 struct dentry *dentry;
347 struct inode *inode;
348
349 if (name[0] == '.')
350 fid.vnode = 3;
351
352 dentry = d_alloc_name(root, name);
353 if (!dentry)
354 return ERR_PTR(-ENOMEM);
355
356 inode = iget5_locked(dentry->d_sb, fid.vnode,
357 afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
358 if (!inode) {
359 dput(dentry);
360 return ERR_PTR(-ENOMEM);
361 }
362
363 vnode = AFS_FS_I(inode);
364
365 /* there shouldn't be an existing inode */
366 if (WARN_ON_ONCE(!(inode->i_state & I_NEW))) {
367 iput(inode);
368 dput(dentry);
369 return ERR_PTR(-EIO);
370 }
371
372 netfs_inode_init(&vnode->netfs, NULL, false);
373 simple_inode_init_ts(inode);
374 set_nlink(inode, 1);
375 inode->i_size = 0;
376 inode->i_mode = S_IFLNK | 0555;
377 inode->i_op = &afs_atcell_inode_operations;
378 inode->i_uid = GLOBAL_ROOT_UID;
379 inode->i_gid = GLOBAL_ROOT_GID;
380 inode->i_blocks = 0;
381 inode->i_generation = 0;
382 inode->i_flags |= S_NOATIME;
383
384 unlock_new_inode(inode);
385 d_splice_alias(inode, dentry);
386 return dentry;
387}
388
389/*
390 * Create @cell and .@cell symlinks.
391 */
392static int afs_dynroot_symlink(struct afs_net *net)
393{
394 struct super_block *sb = net->dynroot_sb;
395 struct dentry *root, *symlink, *dsymlink;
396 int ret;
397
398 /* Let the ->lookup op do the creation */
399 root = sb->s_root;
400 inode_lock(root->d_inode);
401 symlink = afs_dynroot_create_symlink(root, "@cell");
402 if (IS_ERR(symlink)) {
403 ret = PTR_ERR(symlink);
404 goto unlock;
405 }
406
407 dsymlink = afs_dynroot_create_symlink(root, ".@cell");
408 if (IS_ERR(dsymlink)) {
409 ret = PTR_ERR(dsymlink);
410 dput(symlink);
411 goto unlock;
412 }
413
414 /* Note that we're retaining extra refs on the dentries. */
415 symlink->d_fsdata = (void *)1UL;
416 dsymlink->d_fsdata = (void *)1UL;
417 ret = 0;
418unlock:
419 inode_unlock(root->d_inode);
420 return ret;
421}
422
0da0b7fd
DH
423/*
424 * Populate a newly created dynamic root with cell names.
425 */
426int afs_dynroot_populate(struct super_block *sb)
427{
428 struct afs_cell *cell;
429 struct afs_net *net = afs_sb2net(sb);
430 int ret;
431
3b05e528 432 mutex_lock(&net->proc_cells_lock);
0da0b7fd
DH
433
434 net->dynroot_sb = sb;
30bca65b
DH
435 ret = afs_dynroot_symlink(net);
436 if (ret < 0)
437 goto error;
438
6b3944e4 439 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
0da0b7fd
DH
440 ret = afs_dynroot_mkdir(net, cell);
441 if (ret < 0)
442 goto error;
443 }
444
445 ret = 0;
446out:
447 mutex_unlock(&net->proc_cells_lock);
448 return ret;
449
450error:
451 net->dynroot_sb = NULL;
452 goto out;
453}
454
455/*
456 * When a dynamic root that's in the process of being destroyed, depopulate it
457 * of pinned directories.
458 */
459void afs_dynroot_depopulate(struct super_block *sb)
460{
461 struct afs_net *net = afs_sb2net(sb);
da549bdd 462 struct dentry *root = sb->s_root, *subdir;
0da0b7fd
DH
463
464 /* Prevent more subdirs from being created */
465 mutex_lock(&net->proc_cells_lock);
466 if (net->dynroot_sb == sb)
467 net->dynroot_sb = NULL;
468 mutex_unlock(&net->proc_cells_lock);
469
5e0b17b0 470 if (root) {
da549bdd 471 struct hlist_node *n;
5e0b17b0
DH
472 inode_lock(root->d_inode);
473
474 /* Remove all the pins for dirs created for manually added cells */
da549bdd 475 hlist_for_each_entry_safe(subdir, n, &root->d_children, d_sib) {
5e0b17b0
DH
476 if (subdir->d_fsdata) {
477 subdir->d_fsdata = NULL;
478 dput(subdir);
479 }
0da0b7fd 480 }
0da0b7fd 481
5e0b17b0
DH
482 inode_unlock(root->d_inode);
483 }
0da0b7fd 484}
This page took 0.497595 seconds and 5 git commands to generate.