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