1 /* AFS dynamic root handling
2  *
3  * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11 
12 #include <linux/fs.h>
13 #include <linux/namei.h>
14 #include <linux/dns_resolver.h>
15 #include "internal.h"
16 
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,
22 };
23 
24 /*
25  * Probe to see if a cell may exist.  This prevents positive dentries from
26  * being created unnecessarily.
27  */
afs_probe_cell_name(struct dentry * dentry)28 static int afs_probe_cell_name(struct dentry *dentry)
29 {
30 	struct afs_cell *cell;
31 	const char *name = dentry->d_name.name;
32 	size_t len = dentry->d_name.len;
33 	int ret;
34 
35 	/* Names prefixed with a dot are R/W mounts. */
36 	if (name[0] == '.') {
37 		if (len == 1)
38 			return -EINVAL;
39 		name++;
40 		len--;
41 	}
42 
43 	cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len);
44 	if (!IS_ERR(cell)) {
45 		afs_put_cell(afs_d2net(dentry), cell);
46 		return 0;
47 	}
48 
49 	ret = dns_query("afsdb", name, len, "", NULL, NULL);
50 	if (ret == -ENODATA)
51 		ret = -EDESTADDRREQ;
52 	return ret;
53 }
54 
55 /*
56  * Try to auto mount the mountpoint with pseudo directory, if the autocell
57  * operation is setted.
58  */
afs_try_auto_mntpt(struct dentry * dentry,struct inode * dir)59 struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
60 {
61 	struct afs_vnode *vnode = AFS_FS_I(dir);
62 	struct inode *inode;
63 	int ret = -ENOENT;
64 
65 	_enter("%p{%pd}, {%x:%u}",
66 	       dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
67 
68 	if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
69 		goto out;
70 
71 	ret = afs_probe_cell_name(dentry);
72 	if (ret < 0)
73 		goto out;
74 
75 	inode = afs_iget_pseudo_dir(dir->i_sb, false);
76 	if (IS_ERR(inode)) {
77 		ret = PTR_ERR(inode);
78 		goto out;
79 	}
80 
81 	_leave("= %p", inode);
82 	return inode;
83 
84 out:
85 	_leave("= %d", ret);
86 	return ret == -ENOENT ? NULL : ERR_PTR(ret);
87 }
88 
89 /*
90  * Look up @cell in a dynroot directory.  This is a substitution for the
91  * local cell name for the net namespace.
92  */
afs_lookup_atcell(struct dentry * dentry)93 static struct dentry *afs_lookup_atcell(struct dentry *dentry)
94 {
95 	struct afs_cell *cell;
96 	struct afs_net *net = afs_d2net(dentry);
97 	struct dentry *ret;
98 	unsigned int seq = 0;
99 	char *name;
100 	int len;
101 
102 	if (!net->ws_cell)
103 		return ERR_PTR(-ENOENT);
104 
105 	ret = ERR_PTR(-ENOMEM);
106 	name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
107 	if (!name)
108 		goto out_p;
109 
110 	rcu_read_lock();
111 	do {
112 		read_seqbegin_or_lock(&net->cells_lock, &seq);
113 		cell = rcu_dereference_raw(net->ws_cell);
114 		if (cell) {
115 			len = cell->name_len;
116 			memcpy(name, cell->name, len + 1);
117 		}
118 	} while (need_seqretry(&net->cells_lock, seq));
119 	done_seqretry(&net->cells_lock, seq);
120 	rcu_read_unlock();
121 
122 	ret = ERR_PTR(-ENOENT);
123 	if (!cell)
124 		goto out_n;
125 
126 	ret = lookup_one_len(name, dentry->d_parent, len);
127 
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.
130 	 */
131 
132 out_n:
133 	kfree(name);
134 out_p:
135 	return ret;
136 }
137 
138 /*
139  * Look up an entry in a dynroot directory.
140  */
afs_dynroot_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)141 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
142 					 unsigned int flags)
143 {
144 	_enter("%pd", dentry);
145 
146 	ASSERTCMP(d_inode(dentry), ==, NULL);
147 
148 	if (flags & LOOKUP_CREATE)
149 		return ERR_PTR(-EOPNOTSUPP);
150 
151 	if (dentry->d_name.len >= AFSNAMEMAX) {
152 		_leave(" = -ENAMETOOLONG");
153 		return ERR_PTR(-ENAMETOOLONG);
154 	}
155 
156 	if (dentry->d_name.len == 5 &&
157 	    memcmp(dentry->d_name.name, "@cell", 5) == 0)
158 		return afs_lookup_atcell(dentry);
159 
160 	return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
161 }
162 
163 const struct inode_operations afs_dynroot_inode_operations = {
164 	.lookup		= afs_dynroot_lookup,
165 };
166 
167 /*
168  * Dirs in the dynamic root don't need revalidation.
169  */
afs_dynroot_d_revalidate(struct dentry * dentry,unsigned int flags)170 static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
171 {
172 	return 1;
173 }
174 
175 /*
176  * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
177  * sleep)
178  * - called from dput() when d_count is going to 0.
179  * - return 1 to request dentry be unhashed, 0 otherwise
180  */
afs_dynroot_d_delete(const struct dentry * dentry)181 static int afs_dynroot_d_delete(const struct dentry *dentry)
182 {
183 	return d_really_is_positive(dentry);
184 }
185 
186 const struct dentry_operations afs_dynroot_dentry_operations = {
187 	.d_revalidate	= afs_dynroot_d_revalidate,
188 	.d_delete	= afs_dynroot_d_delete,
189 	.d_release	= afs_d_release,
190 	.d_automount	= afs_d_automount,
191 };
192 
193 /*
194  * Create a manually added cell mount directory.
195  * - The caller must hold net->proc_cells_lock
196  */
afs_dynroot_mkdir(struct afs_net * net,struct afs_cell * cell)197 int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
198 {
199 	struct super_block *sb = net->dynroot_sb;
200 	struct dentry *root, *subdir;
201 	int ret;
202 
203 	if (!sb || atomic_read(&sb->s_active) == 0)
204 		return 0;
205 
206 	/* Let the ->lookup op do the creation */
207 	root = sb->s_root;
208 	inode_lock(root->d_inode);
209 	subdir = lookup_one_len(cell->name, root, cell->name_len);
210 	if (IS_ERR(subdir)) {
211 		ret = PTR_ERR(subdir);
212 		goto unlock;
213 	}
214 
215 	/* Note that we're retaining an extra ref on the dentry */
216 	subdir->d_fsdata = (void *)1UL;
217 	ret = 0;
218 unlock:
219 	inode_unlock(root->d_inode);
220 	return ret;
221 }
222 
223 /*
224  * Remove a manually added cell mount directory.
225  * - The caller must hold net->proc_cells_lock
226  */
afs_dynroot_rmdir(struct afs_net * net,struct afs_cell * cell)227 void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
228 {
229 	struct super_block *sb = net->dynroot_sb;
230 	struct dentry *root, *subdir;
231 
232 	if (!sb || atomic_read(&sb->s_active) == 0)
233 		return;
234 
235 	root = sb->s_root;
236 	inode_lock(root->d_inode);
237 
238 	/* Don't want to trigger a lookup call, which will re-add the cell */
239 	subdir = try_lookup_one_len(cell->name, root, cell->name_len);
240 	if (IS_ERR_OR_NULL(subdir)) {
241 		_debug("lookup %ld", PTR_ERR(subdir));
242 		goto no_dentry;
243 	}
244 
245 	_debug("rmdir %pd %u", subdir, d_count(subdir));
246 
247 	if (subdir->d_fsdata) {
248 		_debug("unpin %u", d_count(subdir));
249 		subdir->d_fsdata = NULL;
250 		dput(subdir);
251 	}
252 	dput(subdir);
253 no_dentry:
254 	inode_unlock(root->d_inode);
255 	_leave("");
256 }
257 
258 /*
259  * Populate a newly created dynamic root with cell names.
260  */
afs_dynroot_populate(struct super_block * sb)261 int afs_dynroot_populate(struct super_block *sb)
262 {
263 	struct afs_cell *cell;
264 	struct afs_net *net = afs_sb2net(sb);
265 	int ret;
266 
267 	if (mutex_lock_interruptible(&net->proc_cells_lock) < 0)
268 		return -ERESTARTSYS;
269 
270 	net->dynroot_sb = sb;
271 	hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
272 		ret = afs_dynroot_mkdir(net, cell);
273 		if (ret < 0)
274 			goto error;
275 	}
276 
277 	ret = 0;
278 out:
279 	mutex_unlock(&net->proc_cells_lock);
280 	return ret;
281 
282 error:
283 	net->dynroot_sb = NULL;
284 	goto out;
285 }
286 
287 /*
288  * When a dynamic root that's in the process of being destroyed, depopulate it
289  * of pinned directories.
290  */
afs_dynroot_depopulate(struct super_block * sb)291 void afs_dynroot_depopulate(struct super_block *sb)
292 {
293 	struct afs_net *net = afs_sb2net(sb);
294 	struct dentry *root = sb->s_root, *subdir, *tmp;
295 
296 	/* Prevent more subdirs from being created */
297 	mutex_lock(&net->proc_cells_lock);
298 	if (net->dynroot_sb == sb)
299 		net->dynroot_sb = NULL;
300 	mutex_unlock(&net->proc_cells_lock);
301 
302 	if (root) {
303 		inode_lock(root->d_inode);
304 
305 		/* Remove all the pins for dirs created for manually added cells */
306 		list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
307 			if (subdir->d_fsdata) {
308 				subdir->d_fsdata = NULL;
309 				dput(subdir);
310 			}
311 		}
312 
313 		inode_unlock(root->d_inode);
314 	}
315 }
316