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/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);
34 #ifdef AFS_AUTOMOUNT_SUPPORT
35 static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
38 struct file_operations afs_mntpt_file_operations = {
39 .open = afs_mntpt_open,
42 struct inode_operations afs_mntpt_inode_operations = {
43 .lookup = afs_mntpt_lookup,
44 #ifdef AFS_AUTOMOUNT_SUPPORT
45 .follow_link = afs_mntpt_follow_link,
47 .readlink = page_readlink,
48 .getattr = afs_inode_getattr,
51 #ifdef AFS_AUTOMOUNT_SUPPORT
52 static LIST_HEAD(afs_vfsmounts);
54 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
56 struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
57 .timed_out = afs_mntpt_expiry_timed_out,
60 struct afs_timer afs_mntpt_expiry_timer;
62 unsigned long afs_mntpt_expiry_timeout = 20;
65 /*****************************************************************************/
67 * check a symbolic link to see whether it actually encodes a mountpoint
68 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
70 int afs_mntpt_check_symlink(struct afs_vnode *vnode)
78 _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
80 /* read the contents of the symlink into the pagecache */
81 filler = (filler_t *) AFS_VNODE_TO_I(vnode)->i_mapping->a_ops->readpage;
83 page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
91 wait_on_page_locked(page);
93 if (!PageUptodate(page))
98 /* examine the symlink's contents */
99 size = vnode->status.size;
100 _debug("symlink to %*.*s", size, (int) size, buf);
103 (buf[0] == '%' || buf[0] == '#') &&
106 _debug("symlink is a mountpoint");
107 spin_lock(&vnode->lock);
108 vnode->flags |= AFS_VNODE_MOUNTPOINT;
109 spin_unlock(&vnode->lock);
116 page_cache_release(page);
118 _leave(" = %d", ret);
121 } /* end afs_mntpt_check_symlink() */
123 /*****************************************************************************/
125 * no valid lookup procedure on this sort of dir
127 static struct dentry *afs_mntpt_lookup(struct inode *dir,
128 struct dentry *dentry,
129 struct nameidata *nd)
131 kenter("%p,%p{%p{%s},%s}",
136 dentry->d_parent->d_name.name : (const unsigned char *) "",
137 dentry->d_name.name);
139 return ERR_PTR(-EREMOTE);
140 } /* end afs_mntpt_lookup() */
142 /*****************************************************************************/
144 * no valid open procedure on this sort of dir
146 static int afs_mntpt_open(struct inode *inode, struct file *file)
148 kenter("%p,%p{%p{%s},%s}",
150 file->f_dentry->d_parent,
151 file->f_dentry->d_parent ?
152 file->f_dentry->d_parent->d_name.name :
153 (const unsigned char *) "",
154 file->f_dentry->d_name.name);
157 } /* end afs_mntpt_open() */
159 #ifdef AFS_AUTOMOUNT_SUPPORT
160 /*****************************************************************************/
162 * create a vfsmount to be automounted
164 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
166 struct afs_super_info *super;
167 struct vfsmount *mnt;
168 struct page *page = NULL;
170 char *buf, *devname = NULL, *options = NULL;
173 kenter("{%s}", mntpt->d_name.name);
175 BUG_ON(!mntpt->d_inode);
178 size = mntpt->d_inode->i_size;
179 if (size > PAGE_SIZE - 1)
183 devname = (char *) get_zeroed_page(GFP_KERNEL);
187 options = (char *) get_zeroed_page(GFP_KERNEL);
191 /* read the contents of the AFS special symlink */
192 filler_t *filler = mntpt->d_inode->i_mapping->a_ops->readpage;
194 page = read_cache_page(mntpt->d_inode->i_mapping, 0, filler, NULL);
201 wait_on_page_locked(page);
202 if (!PageUptodate(page) || PageError(page))
206 memcpy(devname, buf, size);
208 page_cache_release(page);
211 /* work out what options we want */
212 super = AFS_FS_S(mntpt->d_sb);
213 memcpy(options, "cell=", 5);
214 strcpy(options + 5, super->volume->cell->name);
215 if (super->volume->type == AFSVL_RWVOL)
216 strcat(options, ",rwpath");
218 /* try and do the mount */
219 kdebug("--- attempting mount %s -o %s ---", devname, options);
220 mnt = do_kern_mount("afs", 0, devname, options);
221 kdebug("--- mount result %p ---", mnt);
223 free_page((unsigned long) devname);
224 free_page((unsigned long) options);
225 kleave(" = %p", mnt);
230 page_cache_release(page);
232 free_page((unsigned long) devname);
234 free_page((unsigned long) options);
235 kleave(" = %d", ret);
237 } /* end afs_mntpt_do_automount() */
239 /*****************************************************************************/
241 * follow a link from a mountpoint directory, thus causing it to be mounted
243 static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
245 struct nameidata newnd;
246 struct vfsmount *newmnt;
249 kenter("%p{%s},{%s:%p{%s}}",
252 nd->mnt->mnt_devname,
254 nd->dentry->d_name.name);
256 newmnt = afs_mntpt_do_automount(dentry);
258 return PTR_ERR(newmnt);
260 struct_cpy(&newnd, nd);
261 newnd.dentry = dentry;
262 err = do_add_mount(newmnt, &newnd, 0, &afs_vfsmounts);
268 dget(newmnt->mnt_root);
269 nd->dentry = newmnt->mnt_root;
272 kleave(" = %d", err);
274 } /* end afs_mntpt_follow_link() */
276 /*****************************************************************************/
278 * handle mountpoint expiry timer going off
280 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
284 mark_mounts_for_expiry(&afs_vfsmounts);
286 afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
287 afs_mntpt_expiry_timeout * HZ);
290 } /* end afs_mntpt_expiry_timed_out() */