1 /* mntpt.c: mountpoint management
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
18 #include <linux/pagemap.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/mnt_namespace.h>
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);
35 const struct file_operations afs_mntpt_file_operations = {
36 .open = afs_mntpt_open,
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,
46 static LIST_HEAD(afs_vfsmounts);
48 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
50 struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
51 .timed_out = afs_mntpt_expiry_timed_out,
54 struct afs_timer afs_mntpt_expiry_timer;
56 unsigned long afs_mntpt_expiry_timeout = 20;
58 /*****************************************************************************/
60 * check a symbolic link to see whether it actually encodes a mountpoint
61 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
63 int afs_mntpt_check_symlink(struct afs_vnode *vnode)
70 _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
72 /* read the contents of the symlink into the pagecache */
73 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
80 wait_on_page_locked(page);
82 if (!PageUptodate(page))
87 /* examine the symlink's contents */
88 size = vnode->status.size;
89 _debug("symlink to %*.*s", size, (int) size, buf);
92 (buf[0] == '%' || buf[0] == '#') &&
95 _debug("symlink is a mountpoint");
96 spin_lock(&vnode->lock);
97 vnode->flags |= AFS_VNODE_MOUNTPOINT;
98 spin_unlock(&vnode->lock);
105 page_cache_release(page);
107 _leave(" = %d", ret);
110 } /* end afs_mntpt_check_symlink() */
112 /*****************************************************************************/
114 * no valid lookup procedure on this sort of dir
116 static struct dentry *afs_mntpt_lookup(struct inode *dir,
117 struct dentry *dentry,
118 struct nameidata *nd)
120 kenter("%p,%p{%p{%s},%s}",
125 dentry->d_parent->d_name.name : (const unsigned char *) "",
126 dentry->d_name.name);
128 return ERR_PTR(-EREMOTE);
129 } /* end afs_mntpt_lookup() */
131 /*****************************************************************************/
133 * no valid open procedure on this sort of dir
135 static int afs_mntpt_open(struct inode *inode, struct file *file)
137 kenter("%p,%p{%p{%s},%s}",
139 file->f_path.dentry->d_parent,
140 file->f_path.dentry->d_parent ?
141 file->f_path.dentry->d_parent->d_name.name :
142 (const unsigned char *) "",
143 file->f_path.dentry->d_name.name);
146 } /* end afs_mntpt_open() */
148 /*****************************************************************************/
150 * create a vfsmount to be automounted
152 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
154 struct afs_super_info *super;
155 struct vfsmount *mnt;
156 struct page *page = NULL;
158 char *buf, *devname = NULL, *options = NULL;
161 kenter("{%s}", mntpt->d_name.name);
163 BUG_ON(!mntpt->d_inode);
166 size = mntpt->d_inode->i_size;
167 if (size > PAGE_SIZE - 1)
171 devname = (char *) get_zeroed_page(GFP_KERNEL);
175 options = (char *) get_zeroed_page(GFP_KERNEL);
179 /* read the contents of the AFS special symlink */
180 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
187 wait_on_page_locked(page);
188 if (!PageUptodate(page) || PageError(page))
192 memcpy(devname, buf, size);
194 page_cache_release(page);
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");
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);
209 free_page((unsigned long) devname);
210 free_page((unsigned long) options);
211 kleave(" = %p", mnt);
216 page_cache_release(page);
218 free_page((unsigned long) devname);
220 free_page((unsigned long) options);
221 kleave(" = %d", ret);
223 } /* end afs_mntpt_do_automount() */
225 /*****************************************************************************/
227 * follow a link from a mountpoint directory, thus causing it to be mounted
229 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
231 struct vfsmount *newmnt;
232 struct dentry *old_dentry;
235 kenter("%p{%s},{%s:%p{%s}}",
238 nd->mnt->mnt_devname,
240 nd->dentry->d_name.name);
242 newmnt = afs_mntpt_do_automount(dentry);
243 if (IS_ERR(newmnt)) {
245 return (void *)newmnt;
248 old_dentry = nd->dentry;
250 err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
251 nd->dentry = old_dentry;
258 dget(newmnt->mnt_root);
259 nd->dentry = newmnt->mnt_root;
262 kleave(" = %d", err);
264 } /* end afs_mntpt_follow_link() */
266 /*****************************************************************************/
268 * handle mountpoint expiry timer going off
270 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
274 mark_mounts_for_expiry(&afs_vfsmounts);
276 afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
277 afs_mntpt_expiry_timeout * HZ);
280 } /* end afs_mntpt_expiry_timed_out() */