1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS dynamic root handling
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/namei.h>
10 #include <linux/dns_resolver.h>
13 static atomic_t afs_autocell_ino;
16 * iget5() comparator for inode created by autocell operations
18 * These pseudo inodes don't match anything.
20 static int afs_iget5_pseudo_test(struct inode *inode, void *opaque)
26 * iget5() inode initialiser
28 static int afs_iget5_pseudo_set(struct inode *inode, void *opaque)
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;
34 vnode->volume = as->volume;
36 inode->i_ino = fid->vnode;
37 inode->i_generation = fid->unique;
42 * Create an inode for a dynamic root directory or an autocell dynamic
45 struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
47 struct afs_super_info *as = AFS_FS_S(sb);
48 struct afs_vnode *vnode;
50 struct afs_fid fid = {};
55 fid.vid = as->volume->vid;
60 fid.vnode = atomic_inc_return(&afs_autocell_ino);
64 inode = iget5_locked(sb, fid.vnode,
65 afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
68 return ERR_PTR(-ENOMEM);
71 _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
72 inode, inode->i_ino, fid.vid, fid.vnode, fid.unique);
74 vnode = AFS_FS_I(inode);
76 /* there shouldn't be an existing inode */
77 BUG_ON(!(inode->i_state & I_NEW));
80 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
82 inode->i_op = &afs_dynroot_inode_operations;
83 inode->i_fop = &simple_dir_operations;
85 inode->i_op = &afs_autocell_inode_operations;
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);
92 inode->i_generation = 0;
94 set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
96 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
97 inode->i_flags |= S_AUTOMOUNT;
100 inode->i_flags |= S_NOATIME;
101 unlock_new_inode(inode);
102 _leave(" = %p", inode);
107 * Probe to see if a cell may exist. This prevents positive dentries from
108 * being created unnecessarily.
110 static int afs_probe_cell_name(struct dentry *dentry)
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;
119 /* Names prefixed with a dot are R/W mounts. */
120 if (name[0] == '.') {
127 cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe);
129 afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe);
133 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
134 &result, NULL, false);
135 if (ret == -ENODATA || ret == -ENOKEY || ret == 0)
137 if (ret > 0 && ret >= sizeof(struct dns_server_list_v1_header)) {
138 struct dns_server_list_v1_header *v1 = (void *)result;
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))
154 * Try to auto mount the mountpoint with pseudo directory, if the autocell
155 * operation is setted.
157 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
159 struct afs_vnode *vnode = AFS_FS_I(dir);
163 _enter("%p{%pd}, {%llx:%llu}",
164 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
166 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
169 ret = afs_probe_cell_name(dentry);
173 inode = afs_iget_pseudo_dir(dir->i_sb, false);
175 ret = PTR_ERR(inode);
179 _leave("= %p", inode);
184 return ret == -ENOENT ? NULL : ERR_PTR(ret);
188 * Look up @cell in a dynroot directory. This is a substitution for the
189 * local cell name for the net namespace.
191 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
193 struct afs_cell *cell;
194 struct afs_net *net = afs_d2net(dentry);
200 return ERR_PTR(-ENOENT);
202 ret = ERR_PTR(-ENOMEM);
203 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
207 down_read(&net->cells_lock);
210 len = cell->name_len;
211 memcpy(name, cell->name, len + 1);
213 up_read(&net->cells_lock);
215 ret = ERR_PTR(-ENOENT);
219 ret = lookup_one_len(name, dentry->d_parent, len);
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.
232 * Look up an entry in a dynroot directory.
234 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
237 _enter("%pd", dentry);
239 ASSERTCMP(d_inode(dentry), ==, NULL);
241 if (flags & LOOKUP_CREATE)
242 return ERR_PTR(-EOPNOTSUPP);
244 if (dentry->d_name.len >= AFSNAMEMAX) {
245 _leave(" = -ENAMETOOLONG");
246 return ERR_PTR(-ENAMETOOLONG);
249 if (dentry->d_name.len == 5 &&
250 memcmp(dentry->d_name.name, "@cell", 5) == 0)
251 return afs_lookup_atcell(dentry);
253 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
256 const struct inode_operations afs_dynroot_inode_operations = {
257 .lookup = afs_dynroot_lookup,
261 * Dirs in the dynamic root don't need revalidation.
263 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
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,
276 * Create a manually added cell mount directory.
277 * - The caller must hold net->proc_cells_lock
279 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
281 struct super_block *sb = net->dynroot_sb;
282 struct dentry *root, *subdir;
285 if (!sb || atomic_read(&sb->s_active) == 0)
288 /* Let the ->lookup op do the creation */
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);
297 /* Note that we're retaining an extra ref on the dentry */
298 subdir->d_fsdata = (void *)1UL;
301 inode_unlock(root->d_inode);
306 * Remove a manually added cell mount directory.
307 * - The caller must hold net->proc_cells_lock
309 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
311 struct super_block *sb = net->dynroot_sb;
312 struct dentry *root, *subdir;
314 if (!sb || atomic_read(&sb->s_active) == 0)
318 inode_lock(root->d_inode);
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));
327 _debug("rmdir %pd %u", subdir, d_count(subdir));
329 if (subdir->d_fsdata) {
330 _debug("unpin %u", d_count(subdir));
331 subdir->d_fsdata = NULL;
336 inode_unlock(root->d_inode);
341 * Populate a newly created dynamic root with cell names.
343 int afs_dynroot_populate(struct super_block *sb)
345 struct afs_cell *cell;
346 struct afs_net *net = afs_sb2net(sb);
349 mutex_lock(&net->proc_cells_lock);
351 net->dynroot_sb = sb;
352 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
353 ret = afs_dynroot_mkdir(net, cell);
360 mutex_unlock(&net->proc_cells_lock);
364 net->dynroot_sb = NULL;
369 * When a dynamic root that's in the process of being destroyed, depopulate it
370 * of pinned directories.
372 void afs_dynroot_depopulate(struct super_block *sb)
374 struct afs_net *net = afs_sb2net(sb);
375 struct dentry *root = sb->s_root, *subdir, *tmp;
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);
384 inode_lock(root->d_inode);
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;
394 inode_unlock(root->d_inode);