Linux Audio

Check our new training course

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