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));
79 netfs_i_context_init(inode, NULL);
81 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
83 inode->i_op = &afs_dynroot_inode_operations;
84 inode->i_fop = &simple_dir_operations;
86 inode->i_op = &afs_autocell_inode_operations;
89 inode->i_uid = GLOBAL_ROOT_UID;
90 inode->i_gid = GLOBAL_ROOT_GID;
91 inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode);
93 inode->i_generation = 0;
95 set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
97 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
98 inode->i_flags |= S_AUTOMOUNT;
101 inode->i_flags |= S_NOATIME;
102 unlock_new_inode(inode);
103 _leave(" = %p", inode);
108 * Probe to see if a cell may exist. This prevents positive dentries from
109 * being created unnecessarily.
111 static int afs_probe_cell_name(struct dentry *dentry)
113 struct afs_cell *cell;
114 struct afs_net *net = afs_d2net(dentry);
115 const char *name = dentry->d_name.name;
116 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",
141 * Try to auto mount the mountpoint with pseudo directory, if the autocell
142 * operation is setted.
144 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
146 struct afs_vnode *vnode = AFS_FS_I(dir);
150 _enter("%p{%pd}, {%llx:%llu}",
151 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
153 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
156 ret = afs_probe_cell_name(dentry);
160 inode = afs_iget_pseudo_dir(dir->i_sb, false);
162 ret = PTR_ERR(inode);
166 _leave("= %p", inode);
171 return ret == -ENOENT ? NULL : ERR_PTR(ret);
175 * Look up @cell in a dynroot directory. This is a substitution for the
176 * local cell name for the net namespace.
178 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
180 struct afs_cell *cell;
181 struct afs_net *net = afs_d2net(dentry);
187 return ERR_PTR(-ENOENT);
189 ret = ERR_PTR(-ENOMEM);
190 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
194 down_read(&net->cells_lock);
197 len = cell->name_len;
198 memcpy(name, cell->name, len + 1);
200 up_read(&net->cells_lock);
202 ret = ERR_PTR(-ENOENT);
206 ret = lookup_one_len(name, dentry->d_parent, len);
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.
219 * Look up an entry in a dynroot directory.
221 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
224 _enter("%pd", dentry);
226 ASSERTCMP(d_inode(dentry), ==, NULL);
228 if (flags & LOOKUP_CREATE)
229 return ERR_PTR(-EOPNOTSUPP);
231 if (dentry->d_name.len >= AFSNAMEMAX) {
232 _leave(" = -ENAMETOOLONG");
233 return ERR_PTR(-ENAMETOOLONG);
236 if (dentry->d_name.len == 5 &&
237 memcmp(dentry->d_name.name, "@cell", 5) == 0)
238 return afs_lookup_atcell(dentry);
240 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
243 const struct inode_operations afs_dynroot_inode_operations = {
244 .lookup = afs_dynroot_lookup,
248 * Dirs in the dynamic root don't need revalidation.
250 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
256 * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
258 * - called from dput() when d_count is going to 0.
259 * - return 1 to request dentry be unhashed, 0 otherwise
261 static int afs_dynroot_d_delete(const struct dentry *dentry)
263 return d_really_is_positive(dentry);
266 const struct dentry_operations afs_dynroot_dentry_operations = {
267 .d_revalidate = afs_dynroot_d_revalidate,
268 .d_delete = afs_dynroot_d_delete,
269 .d_release = afs_d_release,
270 .d_automount = afs_d_automount,
274 * Create a manually added cell mount directory.
275 * - The caller must hold net->proc_cells_lock
277 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
279 struct super_block *sb = net->dynroot_sb;
280 struct dentry *root, *subdir;
283 if (!sb || atomic_read(&sb->s_active) == 0)
286 /* Let the ->lookup op do the creation */
288 inode_lock(root->d_inode);
289 subdir = lookup_one_len(cell->name, root, cell->name_len);
290 if (IS_ERR(subdir)) {
291 ret = PTR_ERR(subdir);
295 /* Note that we're retaining an extra ref on the dentry */
296 subdir->d_fsdata = (void *)1UL;
299 inode_unlock(root->d_inode);
304 * Remove a manually added cell mount directory.
305 * - The caller must hold net->proc_cells_lock
307 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
309 struct super_block *sb = net->dynroot_sb;
310 struct dentry *root, *subdir;
312 if (!sb || atomic_read(&sb->s_active) == 0)
316 inode_lock(root->d_inode);
318 /* Don't want to trigger a lookup call, which will re-add the cell */
319 subdir = try_lookup_one_len(cell->name, root, cell->name_len);
320 if (IS_ERR_OR_NULL(subdir)) {
321 _debug("lookup %ld", PTR_ERR(subdir));
325 _debug("rmdir %pd %u", subdir, d_count(subdir));
327 if (subdir->d_fsdata) {
328 _debug("unpin %u", d_count(subdir));
329 subdir->d_fsdata = NULL;
334 inode_unlock(root->d_inode);
339 * Populate a newly created dynamic root with cell names.
341 int afs_dynroot_populate(struct super_block *sb)
343 struct afs_cell *cell;
344 struct afs_net *net = afs_sb2net(sb);
347 mutex_lock(&net->proc_cells_lock);
349 net->dynroot_sb = sb;
350 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
351 ret = afs_dynroot_mkdir(net, cell);
358 mutex_unlock(&net->proc_cells_lock);
362 net->dynroot_sb = NULL;
367 * When a dynamic root that's in the process of being destroyed, depopulate it
368 * of pinned directories.
370 void afs_dynroot_depopulate(struct super_block *sb)
372 struct afs_net *net = afs_sb2net(sb);
373 struct dentry *root = sb->s_root, *subdir, *tmp;
375 /* Prevent more subdirs from being created */
376 mutex_lock(&net->proc_cells_lock);
377 if (net->dynroot_sb == sb)
378 net->dynroot_sb = NULL;
379 mutex_unlock(&net->proc_cells_lock);
382 inode_lock(root->d_inode);
384 /* Remove all the pins for dirs created for manually added cells */
385 list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
386 if (subdir->d_fsdata) {
387 subdir->d_fsdata = NULL;
392 inode_unlock(root->d_inode);