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/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/namei.h>
20 #include <linux/mnt_namespace.h>
28 static struct dentry *afs_mntpt_lookup(struct inode *dir,
29 struct dentry *dentry,
30 struct nameidata *nd);
31 static int afs_mntpt_open(struct inode *inode, struct file *file);
32 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
34 const struct file_operations afs_mntpt_file_operations = {
35 .open = afs_mntpt_open,
38 const struct inode_operations afs_mntpt_inode_operations = {
39 .lookup = afs_mntpt_lookup,
40 .follow_link = afs_mntpt_follow_link,
41 .readlink = page_readlink,
42 .getattr = afs_inode_getattr,
45 static LIST_HEAD(afs_vfsmounts);
47 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
49 struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
50 .timed_out = afs_mntpt_expiry_timed_out,
53 struct afs_timer afs_mntpt_expiry_timer;
55 unsigned long afs_mntpt_expiry_timeout = 20;
57 /*****************************************************************************/
59 * check a symbolic link to see whether it actually encodes a mountpoint
60 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
62 int afs_mntpt_check_symlink(struct afs_vnode *vnode)
69 _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
71 /* read the contents of the symlink into the pagecache */
72 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
79 wait_on_page_locked(page);
81 if (!PageUptodate(page))
86 /* examine the symlink's contents */
87 size = vnode->status.size;
88 _debug("symlink to %*.*s", size, (int) size, buf);
91 (buf[0] == '%' || buf[0] == '#') &&
94 _debug("symlink is a mountpoint");
95 spin_lock(&vnode->lock);
96 vnode->flags |= AFS_VNODE_MOUNTPOINT;
97 spin_unlock(&vnode->lock);
104 page_cache_release(page);
106 _leave(" = %d", ret);
109 } /* end afs_mntpt_check_symlink() */
111 /*****************************************************************************/
113 * no valid lookup procedure on this sort of dir
115 static struct dentry *afs_mntpt_lookup(struct inode *dir,
116 struct dentry *dentry,
117 struct nameidata *nd)
119 kenter("%p,%p{%p{%s},%s}",
124 dentry->d_parent->d_name.name : (const unsigned char *) "",
125 dentry->d_name.name);
127 return ERR_PTR(-EREMOTE);
128 } /* end afs_mntpt_lookup() */
130 /*****************************************************************************/
132 * no valid open procedure on this sort of dir
134 static int afs_mntpt_open(struct inode *inode, struct file *file)
136 kenter("%p,%p{%p{%s},%s}",
138 file->f_path.dentry->d_parent,
139 file->f_path.dentry->d_parent ?
140 file->f_path.dentry->d_parent->d_name.name :
141 (const unsigned char *) "",
142 file->f_path.dentry->d_name.name);
145 } /* end afs_mntpt_open() */
147 /*****************************************************************************/
149 * create a vfsmount to be automounted
151 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
153 struct afs_super_info *super;
154 struct vfsmount *mnt;
155 struct page *page = NULL;
157 char *buf, *devname = NULL, *options = NULL;
160 kenter("{%s}", mntpt->d_name.name);
162 BUG_ON(!mntpt->d_inode);
165 size = mntpt->d_inode->i_size;
166 if (size > PAGE_SIZE - 1)
170 devname = (char *) get_zeroed_page(GFP_KERNEL);
174 options = (char *) get_zeroed_page(GFP_KERNEL);
178 /* read the contents of the AFS special symlink */
179 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
186 wait_on_page_locked(page);
187 if (!PageUptodate(page) || PageError(page))
191 memcpy(devname, buf, size);
193 page_cache_release(page);
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)
201 strcat(options, ",rwpath");
203 /* try and do the mount */
204 kdebug("--- attempting mount %s -o %s ---", devname, options);
205 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
206 kdebug("--- mount result %p ---", mnt);
208 free_page((unsigned long) devname);
209 free_page((unsigned long) options);
210 kleave(" = %p", mnt);
215 page_cache_release(page);
217 free_page((unsigned long) devname);
219 free_page((unsigned long) options);
220 kleave(" = %d", ret);
222 } /* end afs_mntpt_do_automount() */
224 /*****************************************************************************/
226 * follow a link from a mountpoint directory, thus causing it to be mounted
228 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
230 struct vfsmount *newmnt;
231 struct dentry *old_dentry;
234 kenter("%p{%s},{%s:%p{%s}}",
237 nd->mnt->mnt_devname,
239 nd->dentry->d_name.name);
241 newmnt = afs_mntpt_do_automount(dentry);
242 if (IS_ERR(newmnt)) {
244 return (void *)newmnt;
247 old_dentry = nd->dentry;
249 err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
250 nd->dentry = old_dentry;
257 dget(newmnt->mnt_root);
258 nd->dentry = newmnt->mnt_root;
261 kleave(" = %d", err);
263 } /* end afs_mntpt_follow_link() */
265 /*****************************************************************************/
267 * handle mountpoint expiry timer going off
269 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
273 mark_mounts_for_expiry(&afs_vfsmounts);
275 afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
276 afs_mntpt_expiry_timeout * HZ);
279 } /* end afs_mntpt_expiry_timed_out() */