]>
Commit | Line | Data |
---|---|---|
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 (dhowells@redhat.com) | |
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 |
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 | */ | |
20 | static int afs_iget5_pseudo_test(struct inode *inode, void *opaque) | |
21 | { | |
22 | return 0; | |
23 | } | |
24 | ||
25 | /* | |
26 | * iget5() inode initialiser | |
27 | */ | |
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 | */ | |
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 | ||
e81fb419 | 79 | netfs_inode_init(&vnode->netfs, NULL); |
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 | */ | |
111 | static 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; | |
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 | ||
dca54a7b | 127 | cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe); |
66c7e1d3 | 128 | if (!IS_ERR(cell)) { |
dca54a7b | 129 | afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe); |
66c7e1d3 DH |
130 | return 0; |
131 | } | |
132 | ||
a58946c1 DH |
133 | ret = dns_query(net->net, "afsdb", name, len, "srv=1", |
134 | NULL, NULL, false); | |
2a4ca1b4 DH |
135 | if (ret == -ENODATA || ret == -ENOKEY) |
136 | ret = -ENOENT; | |
66c7e1d3 DH |
137 | return ret; |
138 | } | |
139 | ||
140 | /* | |
141 | * Try to auto mount the mountpoint with pseudo directory, if the autocell | |
142 | * operation is setted. | |
143 | */ | |
144 | struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir) | |
145 | { | |
146 | struct afs_vnode *vnode = AFS_FS_I(dir); | |
147 | struct inode *inode; | |
148 | int ret = -ENOENT; | |
149 | ||
3b6492df | 150 | _enter("%p{%pd}, {%llx:%llu}", |
66c7e1d3 DH |
151 | dentry, dentry, vnode->fid.vid, vnode->fid.vnode); |
152 | ||
153 | if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags)) | |
154 | goto out; | |
155 | ||
156 | ret = afs_probe_cell_name(dentry); | |
157 | if (ret < 0) | |
158 | goto out; | |
159 | ||
160 | inode = afs_iget_pseudo_dir(dir->i_sb, false); | |
161 | if (IS_ERR(inode)) { | |
162 | ret = PTR_ERR(inode); | |
163 | goto out; | |
164 | } | |
165 | ||
166 | _leave("= %p", inode); | |
167 | return inode; | |
168 | ||
169 | out: | |
170 | _leave("= %d", ret); | |
1401a0fc | 171 | return ret == -ENOENT ? NULL : ERR_PTR(ret); |
66c7e1d3 DH |
172 | } |
173 | ||
174 | /* | |
175 | * Look up @cell in a dynroot directory. This is a substitution for the | |
176 | * local cell name for the net namespace. | |
177 | */ | |
178 | static struct dentry *afs_lookup_atcell(struct dentry *dentry) | |
179 | { | |
180 | struct afs_cell *cell; | |
181 | struct afs_net *net = afs_d2net(dentry); | |
182 | struct dentry *ret; | |
66c7e1d3 DH |
183 | char *name; |
184 | int len; | |
185 | ||
186 | if (!net->ws_cell) | |
187 | return ERR_PTR(-ENOENT); | |
188 | ||
189 | ret = ERR_PTR(-ENOMEM); | |
190 | name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL); | |
191 | if (!name) | |
192 | goto out_p; | |
193 | ||
92e3cc91 DH |
194 | down_read(&net->cells_lock); |
195 | cell = net->ws_cell; | |
196 | if (cell) { | |
197 | len = cell->name_len; | |
198 | memcpy(name, cell->name, len + 1); | |
199 | } | |
200 | up_read(&net->cells_lock); | |
66c7e1d3 DH |
201 | |
202 | ret = ERR_PTR(-ENOENT); | |
203 | if (!cell) | |
204 | goto out_n; | |
205 | ||
206 | ret = lookup_one_len(name, dentry->d_parent, len); | |
207 | ||
208 | /* We don't want to d_add() the @cell dentry here as we don't want to | |
209 | * the cached dentry to hide changes to the local cell name. | |
210 | */ | |
211 | ||
212 | out_n: | |
213 | kfree(name); | |
214 | out_p: | |
215 | return ret; | |
216 | } | |
217 | ||
218 | /* | |
219 | * Look up an entry in a dynroot directory. | |
220 | */ | |
221 | static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry, | |
222 | unsigned int flags) | |
223 | { | |
66c7e1d3 DH |
224 | _enter("%pd", dentry); |
225 | ||
226 | ASSERTCMP(d_inode(dentry), ==, NULL); | |
227 | ||
1da4bd9f DH |
228 | if (flags & LOOKUP_CREATE) |
229 | return ERR_PTR(-EOPNOTSUPP); | |
230 | ||
66c7e1d3 DH |
231 | if (dentry->d_name.len >= AFSNAMEMAX) { |
232 | _leave(" = -ENAMETOOLONG"); | |
233 | return ERR_PTR(-ENAMETOOLONG); | |
234 | } | |
235 | ||
236 | if (dentry->d_name.len == 5 && | |
237 | memcmp(dentry->d_name.name, "@cell", 5) == 0) | |
238 | return afs_lookup_atcell(dentry); | |
239 | ||
1401a0fc | 240 | return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry); |
66c7e1d3 DH |
241 | } |
242 | ||
243 | const struct inode_operations afs_dynroot_inode_operations = { | |
244 | .lookup = afs_dynroot_lookup, | |
245 | }; | |
246 | ||
247 | /* | |
248 | * Dirs in the dynamic root don't need revalidation. | |
249 | */ | |
250 | static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags) | |
251 | { | |
252 | return 1; | |
253 | } | |
254 | ||
66c7e1d3 DH |
255 | const struct dentry_operations afs_dynroot_dentry_operations = { |
256 | .d_revalidate = afs_dynroot_d_revalidate, | |
71f8b55b | 257 | .d_delete = always_delete_dentry, |
66c7e1d3 DH |
258 | .d_release = afs_d_release, |
259 | .d_automount = afs_d_automount, | |
260 | }; | |
0da0b7fd DH |
261 | |
262 | /* | |
263 | * Create a manually added cell mount directory. | |
264 | * - The caller must hold net->proc_cells_lock | |
265 | */ | |
266 | int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell) | |
267 | { | |
268 | struct super_block *sb = net->dynroot_sb; | |
269 | struct dentry *root, *subdir; | |
270 | int ret; | |
271 | ||
272 | if (!sb || atomic_read(&sb->s_active) == 0) | |
273 | return 0; | |
274 | ||
275 | /* Let the ->lookup op do the creation */ | |
276 | root = sb->s_root; | |
277 | inode_lock(root->d_inode); | |
278 | subdir = lookup_one_len(cell->name, root, cell->name_len); | |
279 | if (IS_ERR(subdir)) { | |
280 | ret = PTR_ERR(subdir); | |
281 | goto unlock; | |
282 | } | |
283 | ||
284 | /* Note that we're retaining an extra ref on the dentry */ | |
285 | subdir->d_fsdata = (void *)1UL; | |
286 | ret = 0; | |
287 | unlock: | |
288 | inode_unlock(root->d_inode); | |
289 | return ret; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Remove a manually added cell mount directory. | |
294 | * - The caller must hold net->proc_cells_lock | |
295 | */ | |
296 | void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell) | |
297 | { | |
298 | struct super_block *sb = net->dynroot_sb; | |
299 | struct dentry *root, *subdir; | |
300 | ||
301 | if (!sb || atomic_read(&sb->s_active) == 0) | |
302 | return; | |
303 | ||
304 | root = sb->s_root; | |
305 | inode_lock(root->d_inode); | |
306 | ||
307 | /* Don't want to trigger a lookup call, which will re-add the cell */ | |
308 | subdir = try_lookup_one_len(cell->name, root, cell->name_len); | |
309 | if (IS_ERR_OR_NULL(subdir)) { | |
310 | _debug("lookup %ld", PTR_ERR(subdir)); | |
311 | goto no_dentry; | |
312 | } | |
313 | ||
314 | _debug("rmdir %pd %u", subdir, d_count(subdir)); | |
315 | ||
316 | if (subdir->d_fsdata) { | |
317 | _debug("unpin %u", d_count(subdir)); | |
318 | subdir->d_fsdata = NULL; | |
319 | dput(subdir); | |
320 | } | |
321 | dput(subdir); | |
322 | no_dentry: | |
323 | inode_unlock(root->d_inode); | |
324 | _leave(""); | |
325 | } | |
326 | ||
327 | /* | |
328 | * Populate a newly created dynamic root with cell names. | |
329 | */ | |
330 | int afs_dynroot_populate(struct super_block *sb) | |
331 | { | |
332 | struct afs_cell *cell; | |
333 | struct afs_net *net = afs_sb2net(sb); | |
334 | int ret; | |
335 | ||
3b05e528 | 336 | mutex_lock(&net->proc_cells_lock); |
0da0b7fd DH |
337 | |
338 | net->dynroot_sb = sb; | |
6b3944e4 | 339 | hlist_for_each_entry(cell, &net->proc_cells, proc_link) { |
0da0b7fd DH |
340 | ret = afs_dynroot_mkdir(net, cell); |
341 | if (ret < 0) | |
342 | goto error; | |
343 | } | |
344 | ||
345 | ret = 0; | |
346 | out: | |
347 | mutex_unlock(&net->proc_cells_lock); | |
348 | return ret; | |
349 | ||
350 | error: | |
351 | net->dynroot_sb = NULL; | |
352 | goto out; | |
353 | } | |
354 | ||
355 | /* | |
356 | * When a dynamic root that's in the process of being destroyed, depopulate it | |
357 | * of pinned directories. | |
358 | */ | |
359 | void afs_dynroot_depopulate(struct super_block *sb) | |
360 | { | |
361 | struct afs_net *net = afs_sb2net(sb); | |
362 | struct dentry *root = sb->s_root, *subdir, *tmp; | |
363 | ||
364 | /* Prevent more subdirs from being created */ | |
365 | mutex_lock(&net->proc_cells_lock); | |
366 | if (net->dynroot_sb == sb) | |
367 | net->dynroot_sb = NULL; | |
368 | mutex_unlock(&net->proc_cells_lock); | |
369 | ||
5e0b17b0 DH |
370 | if (root) { |
371 | inode_lock(root->d_inode); | |
372 | ||
373 | /* Remove all the pins for dirs created for manually added cells */ | |
374 | list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) { | |
375 | if (subdir->d_fsdata) { | |
376 | subdir->d_fsdata = NULL; | |
377 | dput(subdir); | |
378 | } | |
0da0b7fd | 379 | } |
0da0b7fd | 380 | |
5e0b17b0 DH |
381 | inode_unlock(root->d_inode); |
382 | } | |
0da0b7fd | 383 | } |