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 inode->i_size = 0;
80 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
81 if (root) {
82 inode->i_op = &afs_dynroot_inode_operations;
83 inode->i_fop = &simple_dir_operations;
84 } else {
85 inode->i_op = &afs_autocell_inode_operations;
86 }
87 set_nlink(inode, 2);
88 inode->i_uid = GLOBAL_ROOT_UID;
89 inode->i_gid = GLOBAL_ROOT_GID;
90 inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode);
91 inode->i_blocks = 0;
92 inode->i_generation = 0;
93
94 set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
95 if (!root) {
96 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
97 inode->i_flags |= S_AUTOMOUNT;
98 }
99
100 inode->i_flags |= S_NOATIME;
101 unlock_new_inode(inode);
102 _leave(" = %p", inode);
103 return inode;
104 }
105
106 /*
107 * Probe to see if a cell may exist. This prevents positive dentries from
108 * being created unnecessarily.
109 */
afs_probe_cell_name(struct dentry * dentry)110 static int afs_probe_cell_name(struct dentry *dentry)
111 {
112 struct afs_cell *cell;
113 struct afs_net *net = afs_d2net(dentry);
114 const char *name = dentry->d_name.name;
115 size_t len = dentry->d_name.len;
116 char *result = NULL;
117 int ret;
118
119 /* Names prefixed with a dot are R/W mounts. */
120 if (name[0] == '.') {
121 if (len == 1)
122 return -EINVAL;
123 name++;
124 len--;
125 }
126
127 cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe);
128 if (!IS_ERR(cell)) {
129 afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe);
130 return 0;
131 }
132
133 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
134 &result, NULL, false);
135 if (ret == -ENODATA || ret == -ENOKEY || ret == 0)
136 ret = -ENOENT;
137 if (ret > 0 && ret >= sizeof(struct dns_server_list_v1_header)) {
138 struct dns_server_list_v1_header *v1 = (void *)result;
139
140 if (v1->hdr.zero == 0 &&
141 v1->hdr.content == DNS_PAYLOAD_IS_SERVER_LIST &&
142 v1->hdr.version == 1 &&
143 (v1->status != DNS_LOOKUP_GOOD &&
144 v1->status != DNS_LOOKUP_GOOD_WITH_BAD))
145 return -ENOENT;
146
147 }
148
149 kfree(result);
150 return ret;
151 }
152
153 /*
154 * Try to auto mount the mountpoint with pseudo directory, if the autocell
155 * operation is setted.
156 */
afs_try_auto_mntpt(struct dentry * dentry,struct inode * dir)157 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
158 {
159 struct afs_vnode *vnode = AFS_FS_I(dir);
160 struct inode *inode;
161 int ret = -ENOENT;
162
163 _enter("%p{%pd}, {%llx:%llu}",
164 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
165
166 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
167 goto out;
168
169 ret = afs_probe_cell_name(dentry);
170 if (ret < 0)
171 goto out;
172
173 inode = afs_iget_pseudo_dir(dir->i_sb, false);
174 if (IS_ERR(inode)) {
175 ret = PTR_ERR(inode);
176 goto out;
177 }
178
179 _leave("= %p", inode);
180 return inode;
181
182 out:
183 _leave("= %d", ret);
184 return ret == -ENOENT ? NULL : ERR_PTR(ret);
185 }
186
187 /*
188 * Look up @cell in a dynroot directory. This is a substitution for the
189 * local cell name for the net namespace.
190 */
afs_lookup_atcell(struct dentry * dentry)191 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
192 {
193 struct afs_cell *cell;
194 struct afs_net *net = afs_d2net(dentry);
195 struct dentry *ret;
196 char *name;
197 int len;
198
199 if (!net->ws_cell)
200 return ERR_PTR(-ENOENT);
201
202 ret = ERR_PTR(-ENOMEM);
203 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
204 if (!name)
205 goto out_p;
206
207 down_read(&net->cells_lock);
208 cell = net->ws_cell;
209 if (cell) {
210 len = cell->name_len;
211 memcpy(name, cell->name, len + 1);
212 }
213 up_read(&net->cells_lock);
214
215 ret = ERR_PTR(-ENOENT);
216 if (!cell)
217 goto out_n;
218
219 ret = lookup_one_len(name, dentry->d_parent, len);
220
221 /* We don't want to d_add() the @cell dentry here as we don't want to
222 * the cached dentry to hide changes to the local cell name.
223 */
224
225 out_n:
226 kfree(name);
227 out_p:
228 return ret;
229 }
230
231 /*
232 * Look up an entry in a dynroot directory.
233 */
afs_dynroot_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)234 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
235 unsigned int flags)
236 {
237 _enter("%pd", dentry);
238
239 ASSERTCMP(d_inode(dentry), ==, NULL);
240
241 if (flags & LOOKUP_CREATE)
242 return ERR_PTR(-EOPNOTSUPP);
243
244 if (dentry->d_name.len >= AFSNAMEMAX) {
245 _leave(" = -ENAMETOOLONG");
246 return ERR_PTR(-ENAMETOOLONG);
247 }
248
249 if (dentry->d_name.len == 5 &&
250 memcmp(dentry->d_name.name, "@cell", 5) == 0)
251 return afs_lookup_atcell(dentry);
252
253 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
254 }
255
256 const struct inode_operations afs_dynroot_inode_operations = {
257 .lookup = afs_dynroot_lookup,
258 };
259
260 /*
261 * Dirs in the dynamic root don't need revalidation.
262 */
afs_dynroot_d_revalidate(struct dentry * dentry,unsigned int flags)263 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
264 {
265 return 1;
266 }
267
268 const struct dentry_operations afs_dynroot_dentry_operations = {
269 .d_revalidate = afs_dynroot_d_revalidate,
270 .d_delete = always_delete_dentry,
271 .d_release = afs_d_release,
272 .d_automount = afs_d_automount,
273 };
274
275 /*
276 * Create a manually added cell mount directory.
277 * - The caller must hold net->proc_cells_lock
278 */
afs_dynroot_mkdir(struct afs_net * net,struct afs_cell * cell)279 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
280 {
281 struct super_block *sb = net->dynroot_sb;
282 struct dentry *root, *subdir;
283 int ret;
284
285 if (!sb || atomic_read(&sb->s_active) == 0)
286 return 0;
287
288 /* Let the ->lookup op do the creation */
289 root = sb->s_root;
290 inode_lock(root->d_inode);
291 subdir = lookup_one_len(cell->name, root, cell->name_len);
292 if (IS_ERR(subdir)) {
293 ret = PTR_ERR(subdir);
294 goto unlock;
295 }
296
297 /* Note that we're retaining an extra ref on the dentry */
298 subdir->d_fsdata = (void *)1UL;
299 ret = 0;
300 unlock:
301 inode_unlock(root->d_inode);
302 return ret;
303 }
304
305 /*
306 * Remove a manually added cell mount directory.
307 * - The caller must hold net->proc_cells_lock
308 */
afs_dynroot_rmdir(struct afs_net * net,struct afs_cell * cell)309 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
310 {
311 struct super_block *sb = net->dynroot_sb;
312 struct dentry *root, *subdir;
313
314 if (!sb || atomic_read(&sb->s_active) == 0)
315 return;
316
317 root = sb->s_root;
318 inode_lock(root->d_inode);
319
320 /* Don't want to trigger a lookup call, which will re-add the cell */
321 subdir = try_lookup_one_len(cell->name, root, cell->name_len);
322 if (IS_ERR_OR_NULL(subdir)) {
323 _debug("lookup %ld", PTR_ERR(subdir));
324 goto no_dentry;
325 }
326
327 _debug("rmdir %pd %u", subdir, d_count(subdir));
328
329 if (subdir->d_fsdata) {
330 _debug("unpin %u", d_count(subdir));
331 subdir->d_fsdata = NULL;
332 dput(subdir);
333 }
334 dput(subdir);
335 no_dentry:
336 inode_unlock(root->d_inode);
337 _leave("");
338 }
339
340 /*
341 * Populate a newly created dynamic root with cell names.
342 */
afs_dynroot_populate(struct super_block * sb)343 int afs_dynroot_populate(struct super_block *sb)
344 {
345 struct afs_cell *cell;
346 struct afs_net *net = afs_sb2net(sb);
347 int ret;
348
349 mutex_lock(&net->proc_cells_lock);
350
351 net->dynroot_sb = sb;
352 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
353 ret = afs_dynroot_mkdir(net, cell);
354 if (ret < 0)
355 goto error;
356 }
357
358 ret = 0;
359 out:
360 mutex_unlock(&net->proc_cells_lock);
361 return ret;
362
363 error:
364 net->dynroot_sb = NULL;
365 goto out;
366 }
367
368 /*
369 * When a dynamic root that's in the process of being destroyed, depopulate it
370 * of pinned directories.
371 */
afs_dynroot_depopulate(struct super_block * sb)372 void afs_dynroot_depopulate(struct super_block *sb)
373 {
374 struct afs_net *net = afs_sb2net(sb);
375 struct dentry *root = sb->s_root, *subdir, *tmp;
376
377 /* Prevent more subdirs from being created */
378 mutex_lock(&net->proc_cells_lock);
379 if (net->dynroot_sb == sb)
380 net->dynroot_sb = NULL;
381 mutex_unlock(&net->proc_cells_lock);
382
383 if (root) {
384 inode_lock(root->d_inode);
385
386 /* Remove all the pins for dirs created for manually added cells */
387 list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
388 if (subdir->d_fsdata) {
389 subdir->d_fsdata = NULL;
390 dput(subdir);
391 }
392 }
393
394 inode_unlock(root->d_inode);
395 }
396 }
397