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 const struct file_operations afs_dynroot_file_operations = {
14 .open = dcache_dir_open,
15 .release = dcache_dir_close,
16 .iterate_shared = dcache_readdir,
17 .llseek = dcache_dir_lseek,
21 * Probe to see if a cell may exist. This prevents positive dentries from
22 * being created unnecessarily.
24 static int afs_probe_cell_name(struct dentry *dentry)
26 struct afs_cell *cell;
27 struct afs_net *net = afs_d2net(dentry);
28 const char *name = dentry->d_name.name;
29 size_t len = dentry->d_name.len;
32 /* Names prefixed with a dot are R/W mounts. */
40 cell = afs_lookup_cell_rcu(net, name, len);
42 afs_put_cell(net, cell);
46 ret = dns_query(net->net, "afsdb", name, len, "srv=1",
54 * Try to auto mount the mountpoint with pseudo directory, if the autocell
55 * operation is setted.
57 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
59 struct afs_vnode *vnode = AFS_FS_I(dir);
63 _enter("%p{%pd}, {%llx:%llu}",
64 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
66 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
69 ret = afs_probe_cell_name(dentry);
73 inode = afs_iget_pseudo_dir(dir->i_sb, false);
79 _leave("= %p", inode);
84 return ret == -ENOENT ? NULL : ERR_PTR(ret);
88 * Look up @cell in a dynroot directory. This is a substitution for the
89 * local cell name for the net namespace.
91 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
93 struct afs_cell *cell;
94 struct afs_net *net = afs_d2net(dentry);
101 return ERR_PTR(-ENOENT);
103 ret = ERR_PTR(-ENOMEM);
104 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
110 read_seqbegin_or_lock(&net->cells_lock, &seq);
111 cell = rcu_dereference_raw(net->ws_cell);
113 len = cell->name_len;
114 memcpy(name, cell->name, len + 1);
116 } while (need_seqretry(&net->cells_lock, seq));
117 done_seqretry(&net->cells_lock, seq);
120 ret = ERR_PTR(-ENOENT);
124 ret = lookup_one_len(name, dentry->d_parent, len);
126 /* We don't want to d_add() the @cell dentry here as we don't want to
127 * the cached dentry to hide changes to the local cell name.
137 * Look up an entry in a dynroot directory.
139 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
142 _enter("%pd", dentry);
144 ASSERTCMP(d_inode(dentry), ==, NULL);
146 if (dentry->d_name.len >= AFSNAMEMAX) {
147 _leave(" = -ENAMETOOLONG");
148 return ERR_PTR(-ENAMETOOLONG);
151 if (dentry->d_name.len == 5 &&
152 memcmp(dentry->d_name.name, "@cell", 5) == 0)
153 return afs_lookup_atcell(dentry);
155 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
158 const struct inode_operations afs_dynroot_inode_operations = {
159 .lookup = afs_dynroot_lookup,
163 * Dirs in the dynamic root don't need revalidation.
165 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
171 * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
173 * - called from dput() when d_count is going to 0.
174 * - return 1 to request dentry be unhashed, 0 otherwise
176 static int afs_dynroot_d_delete(const struct dentry *dentry)
178 return d_really_is_positive(dentry);
181 const struct dentry_operations afs_dynroot_dentry_operations = {
182 .d_revalidate = afs_dynroot_d_revalidate,
183 .d_delete = afs_dynroot_d_delete,
184 .d_release = afs_d_release,
185 .d_automount = afs_d_automount,
189 * Create a manually added cell mount directory.
190 * - The caller must hold net->proc_cells_lock
192 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
194 struct super_block *sb = net->dynroot_sb;
195 struct dentry *root, *subdir;
198 if (!sb || atomic_read(&sb->s_active) == 0)
201 /* Let the ->lookup op do the creation */
203 inode_lock(root->d_inode);
204 subdir = lookup_one_len(cell->name, root, cell->name_len);
205 if (IS_ERR(subdir)) {
206 ret = PTR_ERR(subdir);
210 /* Note that we're retaining an extra ref on the dentry */
211 subdir->d_fsdata = (void *)1UL;
214 inode_unlock(root->d_inode);
219 * Remove a manually added cell mount directory.
220 * - The caller must hold net->proc_cells_lock
222 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
224 struct super_block *sb = net->dynroot_sb;
225 struct dentry *root, *subdir;
227 if (!sb || atomic_read(&sb->s_active) == 0)
231 inode_lock(root->d_inode);
233 /* Don't want to trigger a lookup call, which will re-add the cell */
234 subdir = try_lookup_one_len(cell->name, root, cell->name_len);
235 if (IS_ERR_OR_NULL(subdir)) {
236 _debug("lookup %ld", PTR_ERR(subdir));
240 _debug("rmdir %pd %u", subdir, d_count(subdir));
242 if (subdir->d_fsdata) {
243 _debug("unpin %u", d_count(subdir));
244 subdir->d_fsdata = NULL;
249 inode_unlock(root->d_inode);
254 * Populate a newly created dynamic root with cell names.
256 int afs_dynroot_populate(struct super_block *sb)
258 struct afs_cell *cell;
259 struct afs_net *net = afs_sb2net(sb);
262 mutex_lock(&net->proc_cells_lock);
264 net->dynroot_sb = sb;
265 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
266 ret = afs_dynroot_mkdir(net, cell);
273 mutex_unlock(&net->proc_cells_lock);
277 net->dynroot_sb = NULL;
282 * When a dynamic root that's in the process of being destroyed, depopulate it
283 * of pinned directories.
285 void afs_dynroot_depopulate(struct super_block *sb)
287 struct afs_net *net = afs_sb2net(sb);
288 struct dentry *root = sb->s_root, *subdir, *tmp;
290 /* Prevent more subdirs from being created */
291 mutex_lock(&net->proc_cells_lock);
292 if (net->dynroot_sb == sb)
293 net->dynroot_sb = NULL;
294 mutex_unlock(&net->proc_cells_lock);
296 inode_lock(root->d_inode);
298 /* Remove all the pins for dirs created for manually added cells */
299 list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
300 if (subdir->d_fsdata) {
301 subdir->d_fsdata = NULL;
306 inode_unlock(root->d_inode);