| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * linux/fs/nfs/namespace.c | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com> | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 5 | * - Modified by David Howells <dhowells@redhat.com> | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 6 | * | 
|  | 7 | * NFS namespace | 
|  | 8 | */ | 
|  | 9 |  | 
|  | 10 | #include <linux/config.h> | 
|  | 11 |  | 
|  | 12 | #include <linux/dcache.h> | 
|  | 13 | #include <linux/mount.h> | 
|  | 14 | #include <linux/namei.h> | 
|  | 15 | #include <linux/nfs_fs.h> | 
|  | 16 | #include <linux/string.h> | 
|  | 17 | #include <linux/sunrpc/clnt.h> | 
|  | 18 | #include <linux/vfs.h> | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 19 | #include "internal.h" | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 20 |  | 
|  | 21 | #define NFSDBG_FACILITY		NFSDBG_VFS | 
|  | 22 |  | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 23 | static void nfs_expire_automounts(void *list); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 24 |  | 
|  | 25 | LIST_HEAD(nfs_automount_list); | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 26 | static DECLARE_WORK(nfs_automount_task, nfs_expire_automounts, &nfs_automount_list); | 
|  | 27 | int nfs_mountpoint_expiry_timeout = 500 * HZ; | 
|  | 28 |  | 
| Adrian Bunk | 66f3750 | 2006-09-27 01:51:13 -0700 | [diff] [blame] | 29 | static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent, | 
|  | 30 | const struct dentry *dentry, | 
|  | 31 | struct nfs_fh *fh, | 
|  | 32 | struct nfs_fattr *fattr); | 
|  | 33 |  | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 34 | /* | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 35 | * nfs_path - reconstruct the path given an arbitrary dentry | 
|  | 36 | * @base - arbitrary string to prepend to the path | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 37 | * @droot - pointer to root dentry for mountpoint | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 38 | * @dentry - pointer to dentry | 
|  | 39 | * @buffer - result buffer | 
|  | 40 | * @buflen - length of buffer | 
|  | 41 | * | 
|  | 42 | * Helper function for constructing the path from the | 
|  | 43 | * root dentry to an arbitrary hashed dentry. | 
|  | 44 | * | 
|  | 45 | * This is mainly for use in figuring out the path on the | 
|  | 46 | * server side when automounting on top of an existing partition. | 
|  | 47 | */ | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 48 | char *nfs_path(const char *base, | 
|  | 49 | const struct dentry *droot, | 
|  | 50 | const struct dentry *dentry, | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 51 | char *buffer, ssize_t buflen) | 
|  | 52 | { | 
|  | 53 | char *end = buffer+buflen; | 
|  | 54 | int namelen; | 
|  | 55 |  | 
|  | 56 | *--end = '\0'; | 
|  | 57 | buflen--; | 
|  | 58 | spin_lock(&dcache_lock); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 59 | while (!IS_ROOT(dentry) && dentry != droot) { | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 60 | namelen = dentry->d_name.len; | 
|  | 61 | buflen -= namelen + 1; | 
|  | 62 | if (buflen < 0) | 
| Josh Triplett | ce51019 | 2006-07-24 16:30:00 -0700 | [diff] [blame] | 63 | goto Elong_unlock; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 64 | end -= namelen; | 
|  | 65 | memcpy(end, dentry->d_name.name, namelen); | 
|  | 66 | *--end = '/'; | 
|  | 67 | dentry = dentry->d_parent; | 
|  | 68 | } | 
|  | 69 | spin_unlock(&dcache_lock); | 
|  | 70 | namelen = strlen(base); | 
|  | 71 | /* Strip off excess slashes in base string */ | 
|  | 72 | while (namelen > 0 && base[namelen - 1] == '/') | 
|  | 73 | namelen--; | 
|  | 74 | buflen -= namelen; | 
|  | 75 | if (buflen < 0) | 
|  | 76 | goto Elong; | 
|  | 77 | end -= namelen; | 
|  | 78 | memcpy(end, base, namelen); | 
|  | 79 | return end; | 
| Josh Triplett | ce51019 | 2006-07-24 16:30:00 -0700 | [diff] [blame] | 80 | Elong_unlock: | 
|  | 81 | spin_unlock(&dcache_lock); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 82 | Elong: | 
|  | 83 | return ERR_PTR(-ENAMETOOLONG); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | /* | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 87 | * nfs_follow_mountpoint - handle crossing a mountpoint on the server | 
|  | 88 | * @dentry - dentry of mountpoint | 
|  | 89 | * @nd - nameidata info | 
|  | 90 | * | 
|  | 91 | * When we encounter a mountpoint on the server, we want to set up | 
|  | 92 | * a mountpoint on the client too, to prevent inode numbers from | 
|  | 93 | * colliding, and to allow "df" to work properly. | 
|  | 94 | * On NFSv4, we also want to allow for the fact that different | 
|  | 95 | * filesystems may be migrated to different servers in a failover | 
|  | 96 | * situation, and that different filesystems may want to use | 
|  | 97 | * different security flavours. | 
|  | 98 | */ | 
|  | 99 | static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd) | 
|  | 100 | { | 
|  | 101 | struct vfsmount *mnt; | 
|  | 102 | struct nfs_server *server = NFS_SERVER(dentry->d_inode); | 
|  | 103 | struct dentry *parent; | 
|  | 104 | struct nfs_fh fh; | 
|  | 105 | struct nfs_fattr fattr; | 
|  | 106 | int err; | 
|  | 107 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 108 | dprintk("--> nfs_follow_mountpoint()\n"); | 
|  | 109 |  | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 110 | BUG_ON(IS_ROOT(dentry)); | 
|  | 111 | dprintk("%s: enter\n", __FUNCTION__); | 
|  | 112 | dput(nd->dentry); | 
|  | 113 | nd->dentry = dget(dentry); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 114 |  | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 115 | /* Look it up again */ | 
|  | 116 | parent = dget_parent(nd->dentry); | 
| David Howells | 8fa5c00 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 117 | err = server->nfs_client->rpc_ops->lookup(parent->d_inode, | 
|  | 118 | &nd->dentry->d_name, | 
|  | 119 | &fh, &fattr); | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 120 | dput(parent); | 
|  | 121 | if (err != 0) | 
|  | 122 | goto out_err; | 
|  | 123 |  | 
| Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 124 | if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) | 
|  | 125 | mnt = nfs_do_refmount(nd->mnt, nd->dentry); | 
|  | 126 | else | 
|  | 127 | mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr); | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 128 | err = PTR_ERR(mnt); | 
|  | 129 | if (IS_ERR(mnt)) | 
|  | 130 | goto out_err; | 
|  | 131 |  | 
|  | 132 | mntget(mnt); | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 133 | err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list); | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 134 | if (err < 0) { | 
|  | 135 | mntput(mnt); | 
|  | 136 | if (err == -EBUSY) | 
|  | 137 | goto out_follow; | 
|  | 138 | goto out_err; | 
|  | 139 | } | 
|  | 140 | mntput(nd->mnt); | 
|  | 141 | dput(nd->dentry); | 
|  | 142 | nd->mnt = mnt; | 
|  | 143 | nd->dentry = dget(mnt->mnt_root); | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 144 | schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout); | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 145 | out: | 
|  | 146 | dprintk("%s: done, returned %d\n", __FUNCTION__, err); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 147 |  | 
|  | 148 | dprintk("<-- nfs_follow_mountpoint() = %d\n", err); | 
| Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 149 | return ERR_PTR(err); | 
|  | 150 | out_err: | 
|  | 151 | path_release(nd); | 
|  | 152 | goto out; | 
|  | 153 | out_follow: | 
|  | 154 | while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry)) | 
|  | 155 | ; | 
|  | 156 | err = 0; | 
|  | 157 | goto out; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | struct inode_operations nfs_mountpoint_inode_operations = { | 
|  | 161 | .follow_link	= nfs_follow_mountpoint, | 
|  | 162 | .getattr	= nfs_getattr, | 
|  | 163 | }; | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 164 |  | 
| Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 165 | struct inode_operations nfs_referral_inode_operations = { | 
|  | 166 | .follow_link	= nfs_follow_mountpoint, | 
|  | 167 | }; | 
|  | 168 |  | 
| Trond Myklebust | 51d8fa6 | 2006-06-09 09:34:20 -0400 | [diff] [blame] | 169 | static void nfs_expire_automounts(void *data) | 
|  | 170 | { | 
|  | 171 | struct list_head *list = (struct list_head *)data; | 
|  | 172 |  | 
|  | 173 | mark_mounts_for_expiry(list); | 
|  | 174 | if (!list_empty(list)) | 
|  | 175 | schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | void nfs_release_automount_timer(void) | 
|  | 179 | { | 
|  | 180 | if (list_empty(&nfs_automount_list)) { | 
|  | 181 | cancel_delayed_work(&nfs_automount_task); | 
|  | 182 | flush_scheduled_work(); | 
|  | 183 | } | 
|  | 184 | } | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 185 |  | 
|  | 186 | /* | 
|  | 187 | * Clone a mountpoint of the appropriate type | 
|  | 188 | */ | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 189 | static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, | 
|  | 190 | const char *devname, | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 191 | struct nfs_clone_mount *mountdata) | 
|  | 192 | { | 
|  | 193 | #ifdef CONFIG_NFS_V4 | 
|  | 194 | struct vfsmount *mnt = NULL; | 
| David Howells | 8fa5c00 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 195 | switch (server->nfs_client->cl_nfsversion) { | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 196 | case 2: | 
|  | 197 | case 3: | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 198 | mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 199 | break; | 
|  | 200 | case 4: | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 201 | mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 202 | } | 
|  | 203 | return mnt; | 
|  | 204 | #else | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 205 | return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 206 | #endif | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | /** | 
|  | 210 | * nfs_do_submount - set up mountpoint when crossing a filesystem boundary | 
|  | 211 | * @mnt_parent - mountpoint of parent directory | 
|  | 212 | * @dentry - parent directory | 
|  | 213 | * @fh - filehandle for new root dentry | 
|  | 214 | * @fattr - attributes for new root inode | 
|  | 215 | * | 
|  | 216 | */ | 
| Adrian Bunk | 66f3750 | 2006-09-27 01:51:13 -0700 | [diff] [blame] | 217 | static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent, | 
|  | 218 | const struct dentry *dentry, | 
|  | 219 | struct nfs_fh *fh, | 
|  | 220 | struct nfs_fattr *fattr) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 221 | { | 
|  | 222 | struct nfs_clone_mount mountdata = { | 
|  | 223 | .sb = mnt_parent->mnt_sb, | 
|  | 224 | .dentry = dentry, | 
|  | 225 | .fh = fh, | 
|  | 226 | .fattr = fattr, | 
|  | 227 | }; | 
|  | 228 | struct vfsmount *mnt = ERR_PTR(-ENOMEM); | 
|  | 229 | char *page = (char *) __get_free_page(GFP_USER); | 
|  | 230 | char *devname; | 
|  | 231 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 232 | dprintk("--> nfs_do_submount()\n"); | 
|  | 233 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 234 | dprintk("%s: submounting on %s/%s\n", __FUNCTION__, | 
|  | 235 | dentry->d_parent->d_name.name, | 
|  | 236 | dentry->d_name.name); | 
|  | 237 | if (page == NULL) | 
|  | 238 | goto out; | 
|  | 239 | devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE); | 
|  | 240 | mnt = (struct vfsmount *)devname; | 
|  | 241 | if (IS_ERR(devname)) | 
|  | 242 | goto free_page; | 
|  | 243 | mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata); | 
|  | 244 | free_page: | 
|  | 245 | free_page((unsigned long)page); | 
|  | 246 | out: | 
|  | 247 | dprintk("%s: done\n", __FUNCTION__); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 248 |  | 
|  | 249 | dprintk("<-- nfs_do_submount() = %p\n", mnt); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 250 | return mnt; | 
|  | 251 | } |