Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* mountpoint management
  3 *
  4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  5 * Written by David Howells (dhowells@redhat.com)
 
 
 
 
 
  6 */
  7
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/init.h>
 11#include <linux/fs.h>
 12#include <linux/pagemap.h>
 13#include <linux/mount.h>
 14#include <linux/namei.h>
 15#include <linux/gfp.h>
 16#include <linux/fs_context.h>
 17#include "internal.h"
 18
 19
 20static struct dentry *afs_mntpt_lookup(struct inode *dir,
 21				       struct dentry *dentry,
 22				       unsigned int flags);
 23static int afs_mntpt_open(struct inode *inode, struct file *file);
 24static void afs_mntpt_expiry_timed_out(struct work_struct *work);
 25
 26const struct file_operations afs_mntpt_file_operations = {
 27	.open		= afs_mntpt_open,
 28	.llseek		= noop_llseek,
 29};
 30
 31const struct inode_operations afs_mntpt_inode_operations = {
 32	.lookup		= afs_mntpt_lookup,
 33	.readlink	= page_readlink,
 34	.getattr	= afs_getattr,
 35};
 36
 37const struct inode_operations afs_autocell_inode_operations = {
 38	.getattr	= afs_getattr,
 39};
 40
 41static LIST_HEAD(afs_vfsmounts);
 42static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
 43
 44static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
 45
 46static const char afs_root_volume[] = "root.cell";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 47
 48/*
 49 * no valid lookup procedure on this sort of dir
 50 */
 51static struct dentry *afs_mntpt_lookup(struct inode *dir,
 52				       struct dentry *dentry,
 53				       unsigned int flags)
 54{
 55	_enter("%p,%p{%pd2}", dir, dentry, dentry);
 56	return ERR_PTR(-EREMOTE);
 57}
 58
 59/*
 60 * no valid open procedure on this sort of dir
 61 */
 62static int afs_mntpt_open(struct inode *inode, struct file *file)
 63{
 64	_enter("%p,%p{%pD2}", inode, file, file);
 65	return -EREMOTE;
 66}
 67
 68/*
 69 * Set the parameters for the proposed superblock.
 70 */
 71static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
 72{
 73	struct afs_fs_context *ctx = fc->fs_private;
 74	struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
 75	struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
 76	struct afs_cell *cell;
 77	const char *p;
 
 78	int ret;
 79
 80	if (fc->net_ns != src_as->net_ns) {
 81		put_net(fc->net_ns);
 82		fc->net_ns = get_net(src_as->net_ns);
 83	}
 84
 85	if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
 86		ctx->type = AFSVL_RWVOL;
 87		ctx->force = true;
 88	}
 89	if (ctx->cell) {
 90		afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt);
 91		ctx->cell = NULL;
 92	}
 
 
 
 
 93	if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
 94		/* if the directory is a pseudo directory, use the d_name */
 
 95		unsigned size = mntpt->d_name.len;
 96
 97		if (size < 2)
 98			return -ENOENT;
 
 99
100		p = mntpt->d_name.name;
101		if (mntpt->d_name.name[0] == '.') {
102			size--;
103			p++;
104			ctx->type = AFSVL_RWVOL;
105			ctx->force = true;
 
 
 
 
 
 
106		}
107		if (size > AFS_MAXCELLNAME)
108			return -ENAMETOOLONG;
109
110		cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
111		if (IS_ERR(cell)) {
112			pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
113			return PTR_ERR(cell);
114		}
115		ctx->cell = cell;
116
117		ctx->volname = afs_root_volume;
118		ctx->volnamesz = sizeof(afs_root_volume) - 1;
119	} else {
120		/* read the contents of the AFS special symlink */
121		struct page *page;
122		loff_t size = i_size_read(d_inode(mntpt));
123		char *buf;
124
125		if (src_as->cell)
126			ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
127
128		if (size < 2 || size > PAGE_SIZE - 1)
129			return -EINVAL;
130
131		page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
132		if (IS_ERR(page))
133			return PTR_ERR(page);
 
 
134
135		buf = kmap(page);
136		ret = -EINVAL;
137		if (buf[size - 1] == '.')
138			ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
139		kunmap(page);
 
 
140		put_page(page);
141		if (ret < 0)
142			return ret;
143	}
144
145	return 0;
146}
147
148/*
149 * create a vfsmount to be automounted
150 */
151static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
152{
153	struct fs_context *fc;
154	struct vfsmount *mnt;
155	int ret;
156
157	BUG_ON(!d_inode(mntpt));
158
159	fc = fs_context_for_submount(&afs_fs_type, mntpt);
160	if (IS_ERR(fc))
161		return ERR_CAST(fc);
162
163	ret = afs_mntpt_set_params(fc, mntpt);
164	if (!ret)
165		mnt = fc_mount(fc);
166	else
167		mnt = ERR_PTR(ret);
168
169	put_fs_context(fc);
170	return mnt;
 
 
 
 
 
 
 
 
 
 
171}
172
173/*
174 * handle an automount point
175 */
176struct vfsmount *afs_d_automount(struct path *path)
177{
178	struct vfsmount *newmnt;
179
180	_enter("{%pd}", path->dentry);
181
182	newmnt = afs_mntpt_do_automount(path->dentry);
183	if (IS_ERR(newmnt))
184		return newmnt;
185
186	mntget(newmnt); /* prevent immediate expiration */
187	mnt_set_expiry(newmnt, &afs_vfsmounts);
188	queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
189			   afs_mntpt_expiry_timeout * HZ);
190	_leave(" = %p", newmnt);
191	return newmnt;
192}
193
194/*
195 * handle mountpoint expiry timer going off
196 */
197static void afs_mntpt_expiry_timed_out(struct work_struct *work)
198{
199	_enter("");
200
201	if (!list_empty(&afs_vfsmounts)) {
202		mark_mounts_for_expiry(&afs_vfsmounts);
203		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
204				   afs_mntpt_expiry_timeout * HZ);
205	}
206
207	_leave("");
208}
209
210/*
211 * kill the AFS mountpoint timer if it's still running
212 */
213void afs_mntpt_kill_timer(void)
214{
215	_enter("");
216
217	ASSERT(list_empty(&afs_vfsmounts));
218	cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
219}
v4.6
 
  1/* mountpoint management
  2 *
  3 * Copyright (C) 2002 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 License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/init.h>
 15#include <linux/fs.h>
 16#include <linux/pagemap.h>
 17#include <linux/mount.h>
 18#include <linux/namei.h>
 19#include <linux/gfp.h>
 
 20#include "internal.h"
 21
 22
 23static struct dentry *afs_mntpt_lookup(struct inode *dir,
 24				       struct dentry *dentry,
 25				       unsigned int flags);
 26static int afs_mntpt_open(struct inode *inode, struct file *file);
 27static void afs_mntpt_expiry_timed_out(struct work_struct *work);
 28
 29const struct file_operations afs_mntpt_file_operations = {
 30	.open		= afs_mntpt_open,
 31	.llseek		= noop_llseek,
 32};
 33
 34const struct inode_operations afs_mntpt_inode_operations = {
 35	.lookup		= afs_mntpt_lookup,
 36	.readlink	= page_readlink,
 37	.getattr	= afs_getattr,
 38};
 39
 40const struct inode_operations afs_autocell_inode_operations = {
 41	.getattr	= afs_getattr,
 42};
 43
 44static LIST_HEAD(afs_vfsmounts);
 45static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
 46
 47static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
 48
 49/*
 50 * check a symbolic link to see whether it actually encodes a mountpoint
 51 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
 52 */
 53int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
 54{
 55	struct page *page;
 56	size_t size;
 57	char *buf;
 58	int ret;
 59
 60	_enter("{%x:%u,%u}",
 61	       vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
 62
 63	/* read the contents of the symlink into the pagecache */
 64	page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
 65			       afs_page_filler, key);
 66	if (IS_ERR(page)) {
 67		ret = PTR_ERR(page);
 68		goto out;
 69	}
 70
 71	ret = -EIO;
 72	if (PageError(page))
 73		goto out_free;
 74
 75	buf = kmap(page);
 76
 77	/* examine the symlink's contents */
 78	size = vnode->status.size;
 79	_debug("symlink to %*.*s", (int) size, (int) size, buf);
 80
 81	if (size > 2 &&
 82	    (buf[0] == '%' || buf[0] == '#') &&
 83	    buf[size - 1] == '.'
 84	    ) {
 85		_debug("symlink is a mountpoint");
 86		spin_lock(&vnode->lock);
 87		set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
 88		vnode->vfs_inode.i_flags |= S_AUTOMOUNT;
 89		spin_unlock(&vnode->lock);
 90	}
 91
 92	ret = 0;
 93
 94	kunmap(page);
 95out_free:
 96	put_page(page);
 97out:
 98	_leave(" = %d", ret);
 99	return ret;
100}
101
102/*
103 * no valid lookup procedure on this sort of dir
104 */
105static struct dentry *afs_mntpt_lookup(struct inode *dir,
106				       struct dentry *dentry,
107				       unsigned int flags)
108{
109	_enter("%p,%p{%pd2}", dir, dentry, dentry);
110	return ERR_PTR(-EREMOTE);
111}
112
113/*
114 * no valid open procedure on this sort of dir
115 */
116static int afs_mntpt_open(struct inode *inode, struct file *file)
117{
118	_enter("%p,%p{%pD2}", inode, file, file);
119	return -EREMOTE;
120}
121
122/*
123 * create a vfsmount to be automounted
124 */
125static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
126{
127	struct afs_super_info *super;
128	struct vfsmount *mnt;
129	struct afs_vnode *vnode;
130	struct page *page;
131	char *devname, *options;
132	bool rwpath = false;
133	int ret;
134
135	_enter("{%pd}", mntpt);
 
 
 
136
137	BUG_ON(!d_inode(mntpt));
138
139	ret = -ENOMEM;
140	devname = (char *) get_zeroed_page(GFP_KERNEL);
141	if (!devname)
142		goto error_no_devname;
143
144	options = (char *) get_zeroed_page(GFP_KERNEL);
145	if (!options)
146		goto error_no_options;
147
148	vnode = AFS_FS_I(d_inode(mntpt));
149	if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
150		/* if the directory is a pseudo directory, use the d_name */
151		static const char afs_root_cell[] = ":root.cell.";
152		unsigned size = mntpt->d_name.len;
153
154		ret = -ENOENT;
155		if (size < 2 || size > AFS_MAXCELLNAME)
156			goto error_no_page;
157
 
158		if (mntpt->d_name.name[0] == '.') {
159			devname[0] = '#';
160			memcpy(devname + 1, mntpt->d_name.name, size - 1);
161			memcpy(devname + size, afs_root_cell,
162			       sizeof(afs_root_cell));
163			rwpath = true;
164		} else {
165			devname[0] = '%';
166			memcpy(devname + 1, mntpt->d_name.name, size);
167			memcpy(devname + size + 1, afs_root_cell,
168			       sizeof(afs_root_cell));
169		}
 
 
 
 
 
 
 
 
 
 
 
 
170	} else {
171		/* read the contents of the AFS special symlink */
 
172		loff_t size = i_size_read(d_inode(mntpt));
173		char *buf;
174
175		ret = -EINVAL;
176		if (size > PAGE_SIZE - 1)
177			goto error_no_page;
 
 
178
179		page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
180		if (IS_ERR(page)) {
181			ret = PTR_ERR(page);
182			goto error_no_page;
183		}
184
185		ret = -EIO;
186		if (PageError(page))
187			goto error;
188
189		buf = kmap_atomic(page);
190		memcpy(devname, buf, size);
191		kunmap_atomic(buf);
192		put_page(page);
193		page = NULL;
 
194	}
195
196	/* work out what options we want */
197	super = AFS_FS_S(mntpt->d_sb);
198	memcpy(options, "cell=", 5);
199	strcpy(options + 5, super->volume->cell->name);
200	if (super->volume->type == AFSVL_RWVOL || rwpath)
201		strcat(options, ",rwpath");
202
203	/* try and do the mount */
204	_debug("--- attempting mount %s -o %s ---", devname, options);
205	mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
206	_debug("--- mount result %p ---", mnt);
207
208	free_page((unsigned long) devname);
209	free_page((unsigned long) options);
210	_leave(" = %p", mnt);
 
 
 
 
 
 
 
 
 
 
211	return mnt;
212
213error:
214	put_page(page);
215error_no_page:
216	free_page((unsigned long) options);
217error_no_options:
218	free_page((unsigned long) devname);
219error_no_devname:
220	_leave(" = %d", ret);
221	return ERR_PTR(ret);
222}
223
224/*
225 * handle an automount point
226 */
227struct vfsmount *afs_d_automount(struct path *path)
228{
229	struct vfsmount *newmnt;
230
231	_enter("{%pd}", path->dentry);
232
233	newmnt = afs_mntpt_do_automount(path->dentry);
234	if (IS_ERR(newmnt))
235		return newmnt;
236
237	mntget(newmnt); /* prevent immediate expiration */
238	mnt_set_expiry(newmnt, &afs_vfsmounts);
239	queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
240			   afs_mntpt_expiry_timeout * HZ);
241	_leave(" = %p", newmnt);
242	return newmnt;
243}
244
245/*
246 * handle mountpoint expiry timer going off
247 */
248static void afs_mntpt_expiry_timed_out(struct work_struct *work)
249{
250	_enter("");
251
252	if (!list_empty(&afs_vfsmounts)) {
253		mark_mounts_for_expiry(&afs_vfsmounts);
254		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
255				   afs_mntpt_expiry_timeout * HZ);
256	}
257
258	_leave("");
259}
260
261/*
262 * kill the AFS mountpoint timer if it's still running
263 */
264void afs_mntpt_kill_timer(void)
265{
266	_enter("");
267
268	ASSERT(list_empty(&afs_vfsmounts));
269	cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
270}