1 /* AFS dynamic root handling
3 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
13 #include <linux/namei.h>
14 #include <linux/dns_resolver.h>
17 const struct file_operations afs_dynroot_file_operations = {
18 .open = dcache_dir_open,
19 .release = dcache_dir_close,
20 .iterate_shared = dcache_readdir,
21 .llseek = dcache_dir_lseek,
25 * Probe to see if a cell may exist. This prevents positive dentries from
26 * being created unnecessarily.
28 static int afs_probe_cell_name(struct dentry *dentry)
30 struct afs_cell *cell;
31 const char *name = dentry->d_name.name;
32 size_t len = dentry->d_name.len;
35 /* Names prefixed with a dot are R/W mounts. */
43 cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len);
45 afs_put_cell(afs_d2net(dentry), cell);
49 ret = dns_query("afsdb", name, len, "", NULL, NULL);
56 * Try to auto mount the mountpoint with pseudo directory, if the autocell
57 * operation is setted.
59 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
61 struct afs_vnode *vnode = AFS_FS_I(dir);
65 _enter("%p{%pd}, {%x:%u}",
66 dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
68 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
71 ret = afs_probe_cell_name(dentry);
75 inode = afs_iget_pseudo_dir(dir->i_sb, false);
81 _leave("= %p", inode);
86 return ret == -ENOENT ? NULL : ERR_PTR(ret);
90 * Look up @cell in a dynroot directory. This is a substitution for the
91 * local cell name for the net namespace.
93 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
95 struct afs_cell *cell;
96 struct afs_net *net = afs_d2net(dentry);
103 return ERR_PTR(-ENOENT);
105 ret = ERR_PTR(-ENOMEM);
106 name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
112 read_seqbegin_or_lock(&net->cells_lock, &seq);
113 cell = rcu_dereference_raw(net->ws_cell);
115 len = cell->name_len;
116 memcpy(name, cell->name, len + 1);
118 } while (need_seqretry(&net->cells_lock, seq));
119 done_seqretry(&net->cells_lock, seq);
122 ret = ERR_PTR(-ENOENT);
126 ret = lookup_one_len(name, dentry->d_parent, len);
128 /* We don't want to d_add() the @cell dentry here as we don't want to
129 * the cached dentry to hide changes to the local cell name.
139 * Look up an entry in a dynroot directory.
141 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
144 _enter("%pd", dentry);
146 ASSERTCMP(d_inode(dentry), ==, NULL);
148 if (dentry->d_name.len >= AFSNAMEMAX) {
149 _leave(" = -ENAMETOOLONG");
150 return ERR_PTR(-ENAMETOOLONG);
153 if (dentry->d_name.len == 5 &&
154 memcmp(dentry->d_name.name, "@cell", 5) == 0)
155 return afs_lookup_atcell(dentry);
157 return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
160 const struct inode_operations afs_dynroot_inode_operations = {
161 .lookup = afs_dynroot_lookup,
165 * Dirs in the dynamic root don't need revalidation.
167 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
173 * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
175 * - called from dput() when d_count is going to 0.
176 * - return 1 to request dentry be unhashed, 0 otherwise
178 static int afs_dynroot_d_delete(const struct dentry *dentry)
180 return d_really_is_positive(dentry);
183 const struct dentry_operations afs_dynroot_dentry_operations = {
184 .d_revalidate = afs_dynroot_d_revalidate,
185 .d_delete = afs_dynroot_d_delete,
186 .d_release = afs_d_release,
187 .d_automount = afs_d_automount,
191 * Create a manually added cell mount directory.
192 * - The caller must hold net->proc_cells_lock
194 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
196 struct super_block *sb = net->dynroot_sb;
197 struct dentry *root, *subdir;
200 if (!sb || atomic_read(&sb->s_active) == 0)
203 /* Let the ->lookup op do the creation */
205 inode_lock(root->d_inode);
206 subdir = lookup_one_len(cell->name, root, cell->name_len);
207 if (IS_ERR(subdir)) {
208 ret = PTR_ERR(subdir);
212 /* Note that we're retaining an extra ref on the dentry */
213 subdir->d_fsdata = (void *)1UL;
216 inode_unlock(root->d_inode);
221 * Remove a manually added cell mount directory.
222 * - The caller must hold net->proc_cells_lock
224 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
226 struct super_block *sb = net->dynroot_sb;
227 struct dentry *root, *subdir;
229 if (!sb || atomic_read(&sb->s_active) == 0)
233 inode_lock(root->d_inode);
235 /* Don't want to trigger a lookup call, which will re-add the cell */
236 subdir = try_lookup_one_len(cell->name, root, cell->name_len);
237 if (IS_ERR_OR_NULL(subdir)) {
238 _debug("lookup %ld", PTR_ERR(subdir));
242 _debug("rmdir %pd %u", subdir, d_count(subdir));
244 if (subdir->d_fsdata) {
245 _debug("unpin %u", d_count(subdir));
246 subdir->d_fsdata = NULL;
251 inode_unlock(root->d_inode);
256 * Populate a newly created dynamic root with cell names.
258 int afs_dynroot_populate(struct super_block *sb)
260 struct afs_cell *cell;
261 struct afs_net *net = afs_sb2net(sb);
264 if (mutex_lock_interruptible(&net->proc_cells_lock) < 0)
267 net->dynroot_sb = sb;
268 hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
269 ret = afs_dynroot_mkdir(net, cell);
276 mutex_unlock(&net->proc_cells_lock);
280 net->dynroot_sb = NULL;
285 * When a dynamic root that's in the process of being destroyed, depopulate it
286 * of pinned directories.
288 void afs_dynroot_depopulate(struct super_block *sb)
290 struct afs_net *net = afs_sb2net(sb);
291 struct dentry *root = sb->s_root, *subdir, *tmp;
293 /* Prevent more subdirs from being created */
294 mutex_lock(&net->proc_cells_lock);
295 if (net->dynroot_sb == sb)
296 net->dynroot_sb = NULL;
297 mutex_unlock(&net->proc_cells_lock);
299 inode_lock(root->d_inode);
301 /* Remove all the pins for dirs created for manually added cells */
302 list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
303 if (subdir->d_fsdata) {
304 subdir->d_fsdata = NULL;
309 inode_unlock(root->d_inode);