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