1 /* 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>
16 #include <linux/pagemap.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/gfp.h>
23 static struct dentry *afs_mntpt_lookup(struct inode *dir,
24 struct dentry *dentry,
25 struct nameidata *nd);
26 static int afs_mntpt_open(struct inode *inode, struct file *file);
27 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
28 static void afs_mntpt_expiry_timed_out(struct work_struct *work);
30 const struct file_operations afs_mntpt_file_operations = {
31 .open = afs_mntpt_open,
34 const struct inode_operations afs_mntpt_inode_operations = {
35 .lookup = afs_mntpt_lookup,
36 .follow_link = afs_mntpt_follow_link,
37 .readlink = page_readlink,
38 .getattr = afs_getattr,
41 static LIST_HEAD(afs_vfsmounts);
42 static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
44 static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
47 * check a symbolic link to see whether it actually encodes a mountpoint
48 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
50 int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
58 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
60 /* read the contents of the symlink into the pagecache */
61 page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
62 afs_page_filler, key);
74 /* examine the symlink's contents */
75 size = vnode->status.size;
76 _debug("symlink to %*.*s", (int) size, (int) size, buf);
79 (buf[0] == '%' || buf[0] == '#') &&
82 _debug("symlink is a mountpoint");
83 spin_lock(&vnode->lock);
84 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
85 spin_unlock(&vnode->lock);
92 page_cache_release(page);
99 * no valid lookup procedure on this sort of dir
101 static struct dentry *afs_mntpt_lookup(struct inode *dir,
102 struct dentry *dentry,
103 struct nameidata *nd)
105 _enter("%p,%p{%p{%s},%s}",
110 dentry->d_parent->d_name.name : (const unsigned char *) "",
111 dentry->d_name.name);
113 return ERR_PTR(-EREMOTE);
117 * no valid open procedure on this sort of dir
119 static int afs_mntpt_open(struct inode *inode, struct file *file)
121 _enter("%p,%p{%p{%s},%s}",
123 file->f_path.dentry->d_parent,
124 file->f_path.dentry->d_parent ?
125 file->f_path.dentry->d_parent->d_name.name :
126 (const unsigned char *) "",
127 file->f_path.dentry->d_name.name);
133 * create a vfsmount to be automounted
135 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
137 struct afs_super_info *super;
138 struct vfsmount *mnt;
141 char *buf, *devname, *options;
144 _enter("{%s}", mntpt->d_name.name);
146 BUG_ON(!mntpt->d_inode);
149 size = mntpt->d_inode->i_size;
150 if (size > PAGE_SIZE - 1)
151 goto error_no_devname;
154 devname = (char *) get_zeroed_page(GFP_KERNEL);
156 goto error_no_devname;
158 options = (char *) get_zeroed_page(GFP_KERNEL);
160 goto error_no_options;
162 /* read the contents of the AFS special symlink */
163 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
173 buf = kmap_atomic(page, KM_USER0);
174 memcpy(devname, buf, size);
175 kunmap_atomic(buf, KM_USER0);
176 page_cache_release(page);
179 /* work out what options we want */
180 super = AFS_FS_S(mntpt->d_sb);
181 memcpy(options, "cell=", 5);
182 strcpy(options + 5, super->volume->cell->name);
183 if (super->volume->type == AFSVL_RWVOL)
184 strcat(options, ",rwpath");
186 /* try and do the mount */
187 _debug("--- attempting mount %s -o %s ---", devname, options);
188 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
189 _debug("--- mount result %p ---", mnt);
191 free_page((unsigned long) devname);
192 free_page((unsigned long) options);
193 _leave(" = %p", mnt);
197 page_cache_release(page);
199 free_page((unsigned long) options);
201 free_page((unsigned long) devname);
203 _leave(" = %d", ret);
208 * follow a link from a mountpoint directory, thus causing it to be mounted
210 static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
212 struct vfsmount *newmnt;
215 _enter("%p{%s},{%s:%p{%s},}",
218 nd->path.mnt->mnt_devname,
220 nd->path.dentry->d_name.name);
222 dput(nd->path.dentry);
223 nd->path.dentry = dget(dentry);
225 newmnt = afs_mntpt_do_automount(nd->path.dentry);
226 if (IS_ERR(newmnt)) {
228 return (void *)newmnt;
232 err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
236 nd->path.mnt = newmnt;
237 nd->path.dentry = dget(newmnt->mnt_root);
238 schedule_delayed_work(&afs_mntpt_expiry_timer,
239 afs_mntpt_expiry_timeout * HZ);
242 /* someone else made a mount here whilst we were busy */
243 while (d_mountpoint(nd->path.dentry) &&
244 follow_down(&nd->path))
252 _leave(" = %d", err);
257 * handle mountpoint expiry timer going off
259 static void afs_mntpt_expiry_timed_out(struct work_struct *work)
263 if (!list_empty(&afs_vfsmounts)) {
264 mark_mounts_for_expiry(&afs_vfsmounts);
265 schedule_delayed_work(&afs_mntpt_expiry_timer,
266 afs_mntpt_expiry_timeout * HZ);
273 * kill the AFS mountpoint timer if it's still running
275 void afs_mntpt_kill_timer(void)
279 ASSERT(list_empty(&afs_vfsmounts));
280 cancel_delayed_work(&afs_mntpt_expiry_timer);
281 flush_scheduled_work();