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>
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>
24 static struct dentry
*afs_mntpt_lookup(struct inode
*dir
,
25 struct dentry
*dentry
,
26 struct nameidata
*nd
);
27 static int afs_mntpt_open(struct inode
*inode
, struct file
*file
);
28 static void *afs_mntpt_follow_link(struct dentry
*dentry
, struct nameidata
*nd
);
29 static void afs_mntpt_expiry_timed_out(struct work_struct
*work
);
31 const struct file_operations afs_mntpt_file_operations
= {
32 .open
= afs_mntpt_open
,
35 const struct inode_operations afs_mntpt_inode_operations
= {
36 .lookup
= afs_mntpt_lookup
,
37 .follow_link
= afs_mntpt_follow_link
,
38 .readlink
= page_readlink
,
39 .getattr
= afs_getattr
,
42 static LIST_HEAD(afs_vfsmounts
);
43 static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer
, afs_mntpt_expiry_timed_out
);
45 static unsigned long afs_mntpt_expiry_timeout
= 10 * 60;
48 * check a symbolic link to see whether it actually encodes a mountpoint
49 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
51 int afs_mntpt_check_symlink(struct afs_vnode
*vnode
, struct key
*key
)
62 vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->fid
.unique
);
64 /* read the contents of the symlink into the pagecache */
65 page
= read_mapping_page(AFS_VNODE_TO_I(vnode
)->i_mapping
, 0, &file
);
77 /* examine the symlink's contents */
78 size
= vnode
->status
.size
;
79 _debug("symlink to %*.*s", (int) size
, (int) size
, buf
);
82 (buf
[0] == '%' || buf
[0] == '#') &&
85 _debug("symlink is a mountpoint");
86 spin_lock(&vnode
->lock
);
87 set_bit(AFS_VNODE_MOUNTPOINT
, &vnode
->flags
);
88 spin_unlock(&vnode
->lock
);
95 page_cache_release(page
);
102 * no valid lookup procedure on this sort of dir
104 static struct dentry
*afs_mntpt_lookup(struct inode
*dir
,
105 struct dentry
*dentry
,
106 struct nameidata
*nd
)
108 _enter("%p,%p{%p{%s},%s}",
113 dentry
->d_parent
->d_name
.name
: (const unsigned char *) "",
114 dentry
->d_name
.name
);
116 return ERR_PTR(-EREMOTE
);
120 * no valid open procedure on this sort of dir
122 static int afs_mntpt_open(struct inode
*inode
, struct file
*file
)
124 _enter("%p,%p{%p{%s},%s}",
126 file
->f_path
.dentry
->d_parent
,
127 file
->f_path
.dentry
->d_parent
?
128 file
->f_path
.dentry
->d_parent
->d_name
.name
:
129 (const unsigned char *) "",
130 file
->f_path
.dentry
->d_name
.name
);
136 * create a vfsmount to be automounted
138 static struct vfsmount
*afs_mntpt_do_automount(struct dentry
*mntpt
)
140 struct afs_super_info
*super
;
141 struct vfsmount
*mnt
;
142 struct page
*page
= NULL
;
144 char *buf
, *devname
= NULL
, *options
= NULL
;
147 _enter("{%s}", mntpt
->d_name
.name
);
149 BUG_ON(!mntpt
->d_inode
);
152 size
= mntpt
->d_inode
->i_size
;
153 if (size
> PAGE_SIZE
- 1)
157 devname
= (char *) get_zeroed_page(GFP_KERNEL
);
161 options
= (char *) get_zeroed_page(GFP_KERNEL
);
165 /* read the contents of the AFS special symlink */
166 page
= read_mapping_page(mntpt
->d_inode
->i_mapping
, 0, NULL
);
177 memcpy(devname
, buf
, size
);
179 page_cache_release(page
);
182 /* work out what options we want */
183 super
= AFS_FS_S(mntpt
->d_sb
);
184 memcpy(options
, "cell=", 5);
185 strcpy(options
+ 5, super
->volume
->cell
->name
);
186 if (super
->volume
->type
== AFSVL_RWVOL
)
187 strcat(options
, ",rwpath");
189 /* try and do the mount */
190 _debug("--- attempting mount %s -o %s ---", devname
, options
);
191 mnt
= vfs_kern_mount(&afs_fs_type
, 0, devname
, options
);
192 _debug("--- mount result %p ---", mnt
);
194 free_page((unsigned long) devname
);
195 free_page((unsigned long) options
);
196 _leave(" = %p", mnt
);
201 page_cache_release(page
);
203 free_page((unsigned long) devname
);
205 free_page((unsigned long) options
);
206 _leave(" = %d", ret
);
211 * follow a link from a mountpoint directory, thus causing it to be mounted
213 static void *afs_mntpt_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
215 struct vfsmount
*newmnt
;
218 _enter("%p{%s},{%s:%p{%s},}",
221 nd
->mnt
->mnt_devname
,
223 nd
->dentry
->d_name
.name
);
226 nd
->dentry
= dget(dentry
);
228 newmnt
= afs_mntpt_do_automount(nd
->dentry
);
229 if (IS_ERR(newmnt
)) {
231 return (void *)newmnt
;
235 err
= do_add_mount(newmnt
, nd
, MNT_SHRINKABLE
, &afs_vfsmounts
);
241 nd
->dentry
= dget(newmnt
->mnt_root
);
242 schedule_delayed_work(&afs_mntpt_expiry_timer
,
243 afs_mntpt_expiry_timeout
* HZ
);
246 /* someone else made a mount here whilst we were busy */
247 while (d_mountpoint(nd
->dentry
) &&
248 follow_down(&nd
->mnt
, &nd
->dentry
))
256 _leave(" = %d", err
);
261 * handle mountpoint expiry timer going off
263 static void afs_mntpt_expiry_timed_out(struct work_struct
*work
)
267 if (!list_empty(&afs_vfsmounts
)) {
268 mark_mounts_for_expiry(&afs_vfsmounts
);
269 schedule_delayed_work(&afs_mntpt_expiry_timer
,
270 afs_mntpt_expiry_timeout
* HZ
);
277 * kill the AFS mountpoint timer if it's still running
279 void afs_mntpt_kill_timer(void)
283 ASSERT(list_empty(&afs_vfsmounts
));
284 cancel_delayed_work(&afs_mntpt_expiry_timer
);
285 flush_scheduled_work();
289 * begin unmount by attempting to remove all automounted mountpoints we added
291 void afs_umount_begin(struct vfsmount
*vfsmnt
, int flags
)
293 shrink_submounts(vfsmnt
, &afs_vfsmounts
);