xref: /openbmc/linux/fs/afs/mntpt.c (revision 2c64e9cb)
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 <linux/fs_context.h>
21 #include "internal.h"
22 
23 
24 static struct dentry *afs_mntpt_lookup(struct inode *dir,
25 				       struct dentry *dentry,
26 				       unsigned int flags);
27 static int afs_mntpt_open(struct inode *inode, struct file *file);
28 static void afs_mntpt_expiry_timed_out(struct work_struct *work);
29 
30 const struct file_operations afs_mntpt_file_operations = {
31 	.open		= afs_mntpt_open,
32 	.llseek		= noop_llseek,
33 };
34 
35 const struct inode_operations afs_mntpt_inode_operations = {
36 	.lookup		= afs_mntpt_lookup,
37 	.readlink	= page_readlink,
38 	.getattr	= afs_getattr,
39 	.listxattr	= afs_listxattr,
40 };
41 
42 const struct inode_operations afs_autocell_inode_operations = {
43 	.getattr	= afs_getattr,
44 };
45 
46 static LIST_HEAD(afs_vfsmounts);
47 static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
48 
49 static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
50 
51 static const char afs_root_volume[] = "root.cell";
52 
53 /*
54  * no valid lookup procedure on this sort of dir
55  */
56 static struct dentry *afs_mntpt_lookup(struct inode *dir,
57 				       struct dentry *dentry,
58 				       unsigned int flags)
59 {
60 	_enter("%p,%p{%pd2}", dir, dentry, dentry);
61 	return ERR_PTR(-EREMOTE);
62 }
63 
64 /*
65  * no valid open procedure on this sort of dir
66  */
67 static int afs_mntpt_open(struct inode *inode, struct file *file)
68 {
69 	_enter("%p,%p{%pD2}", inode, file, file);
70 	return -EREMOTE;
71 }
72 
73 /*
74  * Set the parameters for the proposed superblock.
75  */
76 static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
77 {
78 	struct afs_fs_context *ctx = fc->fs_private;
79 	struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
80 	struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
81 	struct afs_cell *cell;
82 	const char *p;
83 	int ret;
84 
85 	if (fc->net_ns != src_as->net_ns) {
86 		put_net(fc->net_ns);
87 		fc->net_ns = get_net(src_as->net_ns);
88 	}
89 
90 	if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
91 		ctx->type = AFSVL_RWVOL;
92 		ctx->force = true;
93 	}
94 	if (ctx->cell) {
95 		afs_put_cell(ctx->net, ctx->cell);
96 		ctx->cell = NULL;
97 	}
98 	if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
99 		/* if the directory is a pseudo directory, use the d_name */
100 		unsigned size = mntpt->d_name.len;
101 
102 		if (size < 2)
103 			return -ENOENT;
104 
105 		p = mntpt->d_name.name;
106 		if (mntpt->d_name.name[0] == '.') {
107 			size--;
108 			p++;
109 			ctx->type = AFSVL_RWVOL;
110 			ctx->force = true;
111 		}
112 		if (size > AFS_MAXCELLNAME)
113 			return -ENAMETOOLONG;
114 
115 		cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
116 		if (IS_ERR(cell)) {
117 			pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
118 			return PTR_ERR(cell);
119 		}
120 		ctx->cell = cell;
121 
122 		ctx->volname = afs_root_volume;
123 		ctx->volnamesz = sizeof(afs_root_volume) - 1;
124 	} else {
125 		/* read the contents of the AFS special symlink */
126 		struct page *page;
127 		loff_t size = i_size_read(d_inode(mntpt));
128 		char *buf;
129 
130 		if (src_as->cell)
131 			ctx->cell = afs_get_cell(src_as->cell);
132 
133 		if (size > PAGE_SIZE - 1)
134 			return -EINVAL;
135 
136 		page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
137 		if (IS_ERR(page))
138 			return PTR_ERR(page);
139 
140 		if (PageError(page)) {
141 			ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
142 			put_page(page);
143 			return ret;
144 		}
145 
146 		buf = kmap(page);
147 		ret = vfs_parse_fs_string(fc, "source", buf, size);
148 		kunmap(page);
149 		put_page(page);
150 		if (ret < 0)
151 			return ret;
152 	}
153 
154 	return 0;
155 }
156 
157 /*
158  * create a vfsmount to be automounted
159  */
160 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
161 {
162 	struct fs_context *fc;
163 	struct vfsmount *mnt;
164 	int ret;
165 
166 	BUG_ON(!d_inode(mntpt));
167 
168 	fc = fs_context_for_submount(&afs_fs_type, mntpt);
169 	if (IS_ERR(fc))
170 		return ERR_CAST(fc);
171 
172 	ret = afs_mntpt_set_params(fc, mntpt);
173 	if (!ret)
174 		mnt = fc_mount(fc);
175 	else
176 		mnt = ERR_PTR(ret);
177 
178 	put_fs_context(fc);
179 	return mnt;
180 }
181 
182 /*
183  * handle an automount point
184  */
185 struct vfsmount *afs_d_automount(struct path *path)
186 {
187 	struct vfsmount *newmnt;
188 
189 	_enter("{%pd}", path->dentry);
190 
191 	newmnt = afs_mntpt_do_automount(path->dentry);
192 	if (IS_ERR(newmnt))
193 		return newmnt;
194 
195 	mntget(newmnt); /* prevent immediate expiration */
196 	mnt_set_expiry(newmnt, &afs_vfsmounts);
197 	queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
198 			   afs_mntpt_expiry_timeout * HZ);
199 	_leave(" = %p", newmnt);
200 	return newmnt;
201 }
202 
203 /*
204  * handle mountpoint expiry timer going off
205  */
206 static void afs_mntpt_expiry_timed_out(struct work_struct *work)
207 {
208 	_enter("");
209 
210 	if (!list_empty(&afs_vfsmounts)) {
211 		mark_mounts_for_expiry(&afs_vfsmounts);
212 		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
213 				   afs_mntpt_expiry_timeout * HZ);
214 	}
215 
216 	_leave("");
217 }
218 
219 /*
220  * kill the AFS mountpoint timer if it's still running
221  */
222 void afs_mntpt_kill_timer(void)
223 {
224 	_enter("");
225 
226 	ASSERT(list_empty(&afs_vfsmounts));
227 	cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
228 }
229