xref: /openbmc/linux/fs/afs/mntpt.c (revision 5e8d780d)
1 /* mntpt.c: 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/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/namespace.h>
22 #include "super.h"
23 #include "cell.h"
24 #include "volume.h"
25 #include "vnode.h"
26 #include "internal.h"
27 
28 
29 static struct dentry *afs_mntpt_lookup(struct inode *dir,
30 				       struct dentry *dentry,
31 				       struct nameidata *nd);
32 static int afs_mntpt_open(struct inode *inode, struct file *file);
33 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
34 
35 const struct file_operations afs_mntpt_file_operations = {
36 	.open		= afs_mntpt_open,
37 };
38 
39 struct inode_operations afs_mntpt_inode_operations = {
40 	.lookup		= afs_mntpt_lookup,
41 	.follow_link	= afs_mntpt_follow_link,
42 	.readlink	= page_readlink,
43 	.getattr	= afs_inode_getattr,
44 };
45 
46 static LIST_HEAD(afs_vfsmounts);
47 
48 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
49 
50 struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
51 	.timed_out	= afs_mntpt_expiry_timed_out,
52 };
53 
54 struct afs_timer afs_mntpt_expiry_timer;
55 
56 unsigned long afs_mntpt_expiry_timeout = 20;
57 
58 /*****************************************************************************/
59 /*
60  * check a symbolic link to see whether it actually encodes a mountpoint
61  * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
62  */
63 int afs_mntpt_check_symlink(struct afs_vnode *vnode)
64 {
65 	struct page *page;
66 	size_t size;
67 	char *buf;
68 	int ret;
69 
70 	_enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
71 
72 	/* read the contents of the symlink into the pagecache */
73 	page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
74 	if (IS_ERR(page)) {
75 		ret = PTR_ERR(page);
76 		goto out;
77 	}
78 
79 	ret = -EIO;
80 	wait_on_page_locked(page);
81 	buf = kmap(page);
82 	if (!PageUptodate(page))
83 		goto out_free;
84 	if (PageError(page))
85 		goto out_free;
86 
87 	/* examine the symlink's contents */
88 	size = vnode->status.size;
89 	_debug("symlink to %*.*s", size, (int) size, buf);
90 
91 	if (size > 2 &&
92 	    (buf[0] == '%' || buf[0] == '#') &&
93 	    buf[size - 1] == '.'
94 	    ) {
95 		_debug("symlink is a mountpoint");
96 		spin_lock(&vnode->lock);
97 		vnode->flags |= AFS_VNODE_MOUNTPOINT;
98 		spin_unlock(&vnode->lock);
99 	}
100 
101 	ret = 0;
102 
103  out_free:
104 	kunmap(page);
105 	page_cache_release(page);
106  out:
107 	_leave(" = %d", ret);
108 	return ret;
109 
110 } /* end afs_mntpt_check_symlink() */
111 
112 /*****************************************************************************/
113 /*
114  * no valid lookup procedure on this sort of dir
115  */
116 static struct dentry *afs_mntpt_lookup(struct inode *dir,
117 				       struct dentry *dentry,
118 				       struct nameidata *nd)
119 {
120 	kenter("%p,%p{%p{%s},%s}",
121 	       dir,
122 	       dentry,
123 	       dentry->d_parent,
124 	       dentry->d_parent ?
125 	       dentry->d_parent->d_name.name : (const unsigned char *) "",
126 	       dentry->d_name.name);
127 
128 	return ERR_PTR(-EREMOTE);
129 } /* end afs_mntpt_lookup() */
130 
131 /*****************************************************************************/
132 /*
133  * no valid open procedure on this sort of dir
134  */
135 static int afs_mntpt_open(struct inode *inode, struct file *file)
136 {
137 	kenter("%p,%p{%p{%s},%s}",
138 	       inode, file,
139 	       file->f_dentry->d_parent,
140 	       file->f_dentry->d_parent ?
141 	       file->f_dentry->d_parent->d_name.name :
142 	       (const unsigned char *) "",
143 	       file->f_dentry->d_name.name);
144 
145 	return -EREMOTE;
146 } /* end afs_mntpt_open() */
147 
148 /*****************************************************************************/
149 /*
150  * create a vfsmount to be automounted
151  */
152 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
153 {
154 	struct afs_super_info *super;
155 	struct vfsmount *mnt;
156 	struct page *page = NULL;
157 	size_t size;
158 	char *buf, *devname = NULL, *options = NULL;
159 	int ret;
160 
161 	kenter("{%s}", mntpt->d_name.name);
162 
163 	BUG_ON(!mntpt->d_inode);
164 
165 	ret = -EINVAL;
166 	size = mntpt->d_inode->i_size;
167 	if (size > PAGE_SIZE - 1)
168 		goto error;
169 
170 	ret = -ENOMEM;
171 	devname = (char *) get_zeroed_page(GFP_KERNEL);
172 	if (!devname)
173 		goto error;
174 
175 	options = (char *) get_zeroed_page(GFP_KERNEL);
176 	if (!options)
177 		goto error;
178 
179 	/* read the contents of the AFS special symlink */
180 	page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
181 	if (IS_ERR(page)) {
182 		ret = PTR_ERR(page);
183 		goto error;
184 	}
185 
186 	ret = -EIO;
187 	wait_on_page_locked(page);
188 	if (!PageUptodate(page) || PageError(page))
189 		goto error;
190 
191 	buf = kmap(page);
192 	memcpy(devname, buf, size);
193 	kunmap(page);
194 	page_cache_release(page);
195 	page = NULL;
196 
197 	/* work out what options we want */
198 	super = AFS_FS_S(mntpt->d_sb);
199 	memcpy(options, "cell=", 5);
200 	strcpy(options + 5, super->volume->cell->name);
201 	if (super->volume->type == AFSVL_RWVOL)
202 		strcat(options, ",rwpath");
203 
204 	/* try and do the mount */
205 	kdebug("--- attempting mount %s -o %s ---", devname, options);
206 	mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
207 	kdebug("--- mount result %p ---", mnt);
208 
209 	free_page((unsigned long) devname);
210 	free_page((unsigned long) options);
211 	kleave(" = %p", mnt);
212 	return mnt;
213 
214  error:
215 	if (page)
216 		page_cache_release(page);
217 	if (devname)
218 		free_page((unsigned long) devname);
219 	if (options)
220 		free_page((unsigned long) options);
221 	kleave(" = %d", ret);
222 	return ERR_PTR(ret);
223 } /* end afs_mntpt_do_automount() */
224 
225 /*****************************************************************************/
226 /*
227  * follow a link from a mountpoint directory, thus causing it to be mounted
228  */
229 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
230 {
231 	struct vfsmount *newmnt;
232 	struct dentry *old_dentry;
233 	int err;
234 
235 	kenter("%p{%s},{%s:%p{%s}}",
236 	       dentry,
237 	       dentry->d_name.name,
238 	       nd->mnt->mnt_devname,
239 	       dentry,
240 	       nd->dentry->d_name.name);
241 
242 	newmnt = afs_mntpt_do_automount(dentry);
243 	if (IS_ERR(newmnt)) {
244 		path_release(nd);
245 		return (void *)newmnt;
246 	}
247 
248 	old_dentry = nd->dentry;
249 	nd->dentry = dentry;
250 	err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
251 	nd->dentry = old_dentry;
252 
253 	path_release(nd);
254 
255 	if (!err) {
256 		mntget(newmnt);
257 		nd->mnt = newmnt;
258 		dget(newmnt->mnt_root);
259 		nd->dentry = newmnt->mnt_root;
260 	}
261 
262 	kleave(" = %d", err);
263 	return ERR_PTR(err);
264 } /* end afs_mntpt_follow_link() */
265 
266 /*****************************************************************************/
267 /*
268  * handle mountpoint expiry timer going off
269  */
270 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
271 {
272 	kenter("");
273 
274 	mark_mounts_for_expiry(&afs_vfsmounts);
275 
276 	afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
277 				afs_mntpt_expiry_timeout * HZ);
278 
279 	kleave("");
280 } /* end afs_mntpt_expiry_timed_out() */
281