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