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>
14 * Probe to see if a cell may exist. This prevents positive dentries from
15 * being created unnecessarily.
17 static int afs_probe_cell_name(struct dentry *dentry)
19 struct afs_cell *cell;
20 struct afs_net *net = afs_d2net(dentry);
21 const char *name = dentry->d_name.name;
22 size_t len = dentry->d_name.len;
25 /* Names prefixed with a dot are R/W mounts. */
33 cell = afs_lookup_cell_rcu(net, name, len);
35 afs_put_cell(net, cell);
39 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
47 * Try to auto mount the mountpoint with pseudo directory, if the autocell
48 * operation is setted.
50 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
52 struct afs_vnode *vnode = AFS_FS_I(dir);
56 _enter("%p{%pd}, {%llx:%llu}",
57 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
59 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
62 ret = afs_probe_cell_name(dentry);
66 inode = afs_iget_pseudo_dir(dir->i_sb, false);
72 _leave("= %p", inode);
77 return ret == -ENOENT ? NULL : ERR_PTR(ret);
81 * Look up @cell in a dynroot directory. This is a substitution for the
82 * local cell name for the net namespace.
84 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
86 struct afs_cell *cell;
87 struct afs_net *net = afs_d2net(dentry);
94 return ERR_PTR(-ENOENT);
96 ret = ERR_PTR(-ENOMEM);
97 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
103 read_seqbegin_or_lock(&net->cells_lock, &seq);
104 cell = rcu_dereference_raw(net->ws_cell);
106 len = cell->name_len;
107 memcpy(name, cell->name, len + 1);
109 } while (need_seqretry(&net->cells_lock, seq));
110 done_seqretry(&net->cells_lock, seq);
113 ret = ERR_PTR(-ENOENT);
117 ret = lookup_one_len(name, dentry->d_parent, len);
119 /* We don't want to d_add() the @cell dentry here as we don't want to
120 * the cached dentry to hide changes to the local cell name.
130 * Look up an entry in a dynroot directory.
132 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
135 _enter("%pd", dentry);
137 ASSERTCMP(d_inode(dentry), ==, NULL);
139 if (dentry->d_name.len >= AFSNAMEMAX) {
140 _leave(" = -ENAMETOOLONG");
141 return ERR_PTR(-ENAMETOOLONG);
144 if (dentry->d_name.len == 5 &&
145 memcmp(dentry->d_name.name, "@cell", 5) == 0)
146 return afs_lookup_atcell(dentry);
148 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
151 const struct inode_operations afs_dynroot_inode_operations = {
152 .lookup = afs_dynroot_lookup,
156 * Dirs in the dynamic root don't need revalidation.
158 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
164 * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
166 * - called from dput() when d_count is going to 0.
167 * - return 1 to request dentry be unhashed, 0 otherwise
169 static int afs_dynroot_d_delete(const struct dentry *dentry)
171 return d_really_is_positive(dentry);
174 const struct dentry_operations afs_dynroot_dentry_operations = {
175 .d_revalidate = afs_dynroot_d_revalidate,
176 .d_delete = afs_dynroot_d_delete,
177 .d_release = afs_d_release,
178 .d_automount = afs_d_automount,
182 * Create a manually added cell mount directory.
183 * - The caller must hold net->proc_cells_lock
185 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
187 struct super_block *sb = net->dynroot_sb;
188 struct dentry *root, *subdir;
191 if (!sb || atomic_read(&sb->s_active) == 0)
194 /* Let the ->lookup op do the creation */
196 inode_lock(root->d_inode);
197 subdir = lookup_one_len(cell->name, root, cell->name_len);
198 if (IS_ERR(subdir)) {
199 ret = PTR_ERR(subdir);
203 /* Note that we're retaining an extra ref on the dentry */
204 subdir->d_fsdata = (void *)1UL;
207 inode_unlock(root->d_inode);
212 * Remove a manually added cell mount directory.
213 * - The caller must hold net->proc_cells_lock
215 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
217 struct super_block *sb = net->dynroot_sb;
218 struct dentry *root, *subdir;
220 if (!sb || atomic_read(&sb->s_active) == 0)
224 inode_lock(root->d_inode);
226 /* Don't want to trigger a lookup call, which will re-add the cell */
227 subdir = try_lookup_one_len(cell->name, root, cell->name_len);
228 if (IS_ERR_OR_NULL(subdir)) {
229 _debug("lookup %ld", PTR_ERR(subdir));
233 _debug("rmdir %pd %u", subdir, d_count(subdir));
235 if (subdir->d_fsdata) {
236 _debug("unpin %u", d_count(subdir));
237 subdir->d_fsdata = NULL;
242 inode_unlock(root->d_inode);
247 * Populate a newly created dynamic root with cell names.
249 int afs_dynroot_populate(struct super_block *sb)
251 struct afs_cell *cell;
252 struct afs_net *net = afs_sb2net(sb);
255 mutex_lock(&net->proc_cells_lock);
257 net->dynroot_sb = sb;
258 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
259 ret = afs_dynroot_mkdir(net, cell);
266 mutex_unlock(&net->proc_cells_lock);
270 net->dynroot_sb = NULL;
275 * When a dynamic root that's in the process of being destroyed, depopulate it
276 * of pinned directories.
278 void afs_dynroot_depopulate(struct super_block *sb)
280 struct afs_net *net = afs_sb2net(sb);
281 struct dentry *root = sb->s_root, *subdir, *tmp;
283 /* Prevent more subdirs from being created */
284 mutex_lock(&net->proc_cells_lock);
285 if (net->dynroot_sb == sb)
286 net->dynroot_sb = NULL;
287 mutex_unlock(&net->proc_cells_lock);
289 inode_lock(root->d_inode);
291 /* Remove all the pins for dirs created for manually added cells */
292 list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
293 if (subdir->d_fsdata) {
294 subdir->d_fsdata = NULL;
299 inode_unlock(root->d_inode);