Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> |
| 3 | */ |
| 4 | #include <linux/init.h> |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 5 | #include <linux/module.h> |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 6 | #include <linux/nfs_idmap.h> |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 7 | #include <linux/nfs4_mount.h> |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 8 | #include <linux/nfs_fs.h> |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 9 | #include "delegation.h" |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 10 | #include "internal.h" |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 11 | #include "nfs4_fs.h" |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 12 | #include "dns_resolve.h" |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 13 | #include "pnfs.h" |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 14 | #include "nfs.h" |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 15 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 16 | #define NFSDBG_FACILITY NFSDBG_VFS |
| 17 | |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 18 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); |
| 19 | static void nfs4_evict_inode(struct inode *inode); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 20 | static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type, |
| 21 | int flags, const char *dev_name, void *raw_data); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 22 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, |
| 23 | int flags, const char *dev_name, void *raw_data); |
| 24 | static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type, |
| 25 | int flags, const char *dev_name, void *raw_data); |
| 26 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 27 | static struct file_system_type nfs4_remote_fs_type = { |
| 28 | .owner = THIS_MODULE, |
| 29 | .name = "nfs4", |
| 30 | .mount = nfs4_remote_mount, |
| 31 | .kill_sb = nfs_kill_super, |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 32 | .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 35 | static struct file_system_type nfs4_remote_referral_fs_type = { |
| 36 | .owner = THIS_MODULE, |
| 37 | .name = "nfs4", |
| 38 | .mount = nfs4_remote_referral_mount, |
| 39 | .kill_sb = nfs_kill_super, |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 40 | .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct file_system_type nfs4_referral_fs_type = { |
| 44 | .owner = THIS_MODULE, |
| 45 | .name = "nfs4", |
| 46 | .mount = nfs4_referral_mount, |
| 47 | .kill_sb = nfs_kill_super, |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 48 | .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static const struct super_operations nfs4_sops = { |
| 52 | .alloc_inode = nfs_alloc_inode, |
| 53 | .destroy_inode = nfs_destroy_inode, |
| 54 | .write_inode = nfs4_write_inode, |
Trond Myklebust | eed9935 | 2012-12-14 14:36:36 -0500 | [diff] [blame] | 55 | .drop_inode = nfs_drop_inode, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 56 | .put_super = nfs_put_super, |
| 57 | .statfs = nfs_statfs, |
| 58 | .evict_inode = nfs4_evict_inode, |
| 59 | .umount_begin = nfs_umount_begin, |
| 60 | .show_options = nfs_show_options, |
| 61 | .show_devname = nfs_show_devname, |
| 62 | .show_path = nfs_show_path, |
| 63 | .show_stats = nfs_show_stats, |
| 64 | .remount_fs = nfs_remount, |
| 65 | }; |
| 66 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 67 | struct nfs_subversion nfs_v4 = { |
| 68 | .owner = THIS_MODULE, |
| 69 | .nfs_fs = &nfs4_fs_type, |
| 70 | .rpc_vers = &nfs_version4, |
| 71 | .rpc_ops = &nfs_v4_clientops, |
Bryan Schumaker | 6a74490 | 2012-07-30 16:05:20 -0400 | [diff] [blame] | 72 | .sops = &nfs4_sops, |
| 73 | .xattr = nfs4_xattr_handlers, |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 74 | }; |
| 75 | |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 76 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 77 | { |
| 78 | int ret = nfs_write_inode(inode, wbc); |
| 79 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 80 | if (ret == 0) |
| 81 | ret = pnfs_layoutcommit_inode(inode, |
| 82 | wbc->sync_mode == WB_SYNC_ALL); |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Clean out any remaining NFSv4 state that might be left over due |
| 88 | * to open() calls that passed nfs_atomic_lookup, but failed to call |
| 89 | * nfs_open(). |
| 90 | */ |
| 91 | static void nfs4_evict_inode(struct inode *inode) |
| 92 | { |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 93 | truncate_inode_pages_final(&inode->i_data); |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 94 | clear_inode(inode); |
| 95 | pnfs_return_layout(inode); |
| 96 | pnfs_destroy_layout(NFS_I(inode)); |
| 97 | /* If we are holding a delegation, return it! */ |
| 98 | nfs_inode_return_delegation_noreclaim(inode); |
| 99 | /* First call standard NFS clear_inode() code */ |
| 100 | nfs_clear_inode(inode); |
| 101 | } |
| 102 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 103 | /* |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 104 | * Get the superblock for the NFS4 root partition |
| 105 | */ |
| 106 | static struct dentry * |
| 107 | nfs4_remote_mount(struct file_system_type *fs_type, int flags, |
| 108 | const char *dev_name, void *info) |
| 109 | { |
| 110 | struct nfs_mount_info *mount_info = info; |
| 111 | struct nfs_server *server; |
| 112 | struct dentry *mntroot = ERR_PTR(-ENOMEM); |
| 113 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 114 | mount_info->set_security = nfs_set_sb_security; |
| 115 | |
| 116 | /* Get a volume representation */ |
Bryan Schumaker | 1179acc | 2012-07-30 16:05:19 -0400 | [diff] [blame] | 117 | server = nfs4_create_server(mount_info, &nfs_v4); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 118 | if (IS_ERR(server)) { |
| 119 | mntroot = ERR_CAST(server); |
| 120 | goto out; |
| 121 | } |
| 122 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 123 | mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 124 | |
| 125 | out: |
| 126 | return mntroot; |
| 127 | } |
| 128 | |
| 129 | static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type, |
| 130 | int flags, void *data, const char *hostname) |
| 131 | { |
| 132 | struct vfsmount *root_mnt; |
| 133 | char *root_devname; |
| 134 | size_t len; |
| 135 | |
| 136 | len = strlen(hostname) + 5; |
| 137 | root_devname = kmalloc(len, GFP_KERNEL); |
| 138 | if (root_devname == NULL) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | /* Does hostname needs to be enclosed in brackets? */ |
| 141 | if (strchr(hostname, ':')) |
| 142 | snprintf(root_devname, len, "[%s]:/", hostname); |
| 143 | else |
| 144 | snprintf(root_devname, len, "%s:/", hostname); |
| 145 | root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data); |
| 146 | kfree(root_devname); |
| 147 | return root_mnt; |
| 148 | } |
| 149 | |
| 150 | struct nfs_referral_count { |
| 151 | struct list_head list; |
| 152 | const struct task_struct *task; |
| 153 | unsigned int referral_count; |
| 154 | }; |
| 155 | |
| 156 | static LIST_HEAD(nfs_referral_count_list); |
| 157 | static DEFINE_SPINLOCK(nfs_referral_count_list_lock); |
| 158 | |
| 159 | static struct nfs_referral_count *nfs_find_referral_count(void) |
| 160 | { |
| 161 | struct nfs_referral_count *p; |
| 162 | |
| 163 | list_for_each_entry(p, &nfs_referral_count_list, list) { |
| 164 | if (p->task == current) |
| 165 | return p; |
| 166 | } |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | #define NFS_MAX_NESTED_REFERRALS 2 |
| 171 | |
| 172 | static int nfs_referral_loop_protect(void) |
| 173 | { |
| 174 | struct nfs_referral_count *p, *new; |
| 175 | int ret = -ENOMEM; |
| 176 | |
| 177 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 178 | if (!new) |
| 179 | goto out; |
| 180 | new->task = current; |
| 181 | new->referral_count = 1; |
| 182 | |
| 183 | ret = 0; |
| 184 | spin_lock(&nfs_referral_count_list_lock); |
| 185 | p = nfs_find_referral_count(); |
| 186 | if (p != NULL) { |
| 187 | if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) |
| 188 | ret = -ELOOP; |
| 189 | else |
| 190 | p->referral_count++; |
| 191 | } else { |
| 192 | list_add(&new->list, &nfs_referral_count_list); |
| 193 | new = NULL; |
| 194 | } |
| 195 | spin_unlock(&nfs_referral_count_list_lock); |
| 196 | kfree(new); |
| 197 | out: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static void nfs_referral_loop_unprotect(void) |
| 202 | { |
| 203 | struct nfs_referral_count *p; |
| 204 | |
| 205 | spin_lock(&nfs_referral_count_list_lock); |
| 206 | p = nfs_find_referral_count(); |
| 207 | p->referral_count--; |
| 208 | if (p->referral_count == 0) |
| 209 | list_del(&p->list); |
| 210 | else |
| 211 | p = NULL; |
| 212 | spin_unlock(&nfs_referral_count_list_lock); |
| 213 | kfree(p); |
| 214 | } |
| 215 | |
| 216 | static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt, |
| 217 | const char *export_path) |
| 218 | { |
| 219 | struct dentry *dentry; |
| 220 | int err; |
| 221 | |
| 222 | if (IS_ERR(root_mnt)) |
| 223 | return ERR_CAST(root_mnt); |
| 224 | |
| 225 | err = nfs_referral_loop_protect(); |
| 226 | if (err) { |
| 227 | mntput(root_mnt); |
| 228 | return ERR_PTR(err); |
| 229 | } |
| 230 | |
| 231 | dentry = mount_subtree(root_mnt, export_path); |
| 232 | nfs_referral_loop_unprotect(); |
| 233 | |
| 234 | return dentry; |
| 235 | } |
| 236 | |
| 237 | struct dentry *nfs4_try_mount(int flags, const char *dev_name, |
Bryan Schumaker | ff9099f2 | 2012-07-30 16:05:18 -0400 | [diff] [blame] | 238 | struct nfs_mount_info *mount_info, |
| 239 | struct nfs_subversion *nfs_mod) |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 240 | { |
| 241 | char *export_path; |
| 242 | struct vfsmount *root_mnt; |
| 243 | struct dentry *res; |
| 244 | struct nfs_parsed_mount_data *data = mount_info->parsed; |
| 245 | |
| 246 | dfprintk(MOUNT, "--> nfs4_try_mount()\n"); |
| 247 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 248 | export_path = data->nfs_server.export_path; |
| 249 | data->nfs_server.export_path = "/"; |
| 250 | root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info, |
| 251 | data->nfs_server.hostname); |
| 252 | data->nfs_server.export_path = export_path; |
| 253 | |
| 254 | res = nfs_follow_remote_path(root_mnt, export_path); |
| 255 | |
Geyslan G. Bem | 6706246 | 2013-10-14 17:24:16 -0300 | [diff] [blame] | 256 | dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", |
| 257 | PTR_ERR_OR_ZERO(res), |
| 258 | IS_ERR(res) ? " [error]" : ""); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 259 | return res; |
| 260 | } |
| 261 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 262 | static struct dentry * |
| 263 | nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags, |
| 264 | const char *dev_name, void *raw_data) |
| 265 | { |
| 266 | struct nfs_mount_info mount_info = { |
Bryan Schumaker | 6a74490 | 2012-07-30 16:05:20 -0400 | [diff] [blame] | 267 | .fill_super = nfs_fill_super, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 268 | .set_security = nfs_clone_sb_security, |
| 269 | .cloned = raw_data, |
| 270 | }; |
| 271 | struct nfs_server *server; |
| 272 | struct dentry *mntroot = ERR_PTR(-ENOMEM); |
| 273 | |
| 274 | dprintk("--> nfs4_referral_get_sb()\n"); |
| 275 | |
| 276 | mount_info.mntfh = nfs_alloc_fhandle(); |
| 277 | if (mount_info.cloned == NULL || mount_info.mntfh == NULL) |
| 278 | goto out; |
| 279 | |
| 280 | /* create a new volume representation */ |
| 281 | server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh); |
| 282 | if (IS_ERR(server)) { |
| 283 | mntroot = ERR_CAST(server); |
| 284 | goto out; |
| 285 | } |
| 286 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 287 | mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 288 | out: |
| 289 | nfs_free_fhandle(mount_info.mntfh); |
| 290 | return mntroot; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Create an NFS4 server record on referral traversal |
| 295 | */ |
| 296 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, |
| 297 | int flags, const char *dev_name, void *raw_data) |
| 298 | { |
| 299 | struct nfs_clone_mount *data = raw_data; |
| 300 | char *export_path; |
| 301 | struct vfsmount *root_mnt; |
| 302 | struct dentry *res; |
| 303 | |
| 304 | dprintk("--> nfs4_referral_mount()\n"); |
| 305 | |
| 306 | export_path = data->mnt_path; |
| 307 | data->mnt_path = "/"; |
| 308 | |
| 309 | root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type, |
| 310 | flags, data, data->hostname); |
| 311 | data->mnt_path = export_path; |
| 312 | |
| 313 | res = nfs_follow_remote_path(root_mnt, export_path); |
Geyslan G. Bem | 6706246 | 2013-10-14 17:24:16 -0300 | [diff] [blame] | 314 | dprintk("<-- nfs4_referral_mount() = %d%s\n", |
| 315 | PTR_ERR_OR_ZERO(res), |
| 316 | IS_ERR(res) ? " [error]" : ""); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 317 | return res; |
| 318 | } |
| 319 | |
| 320 | |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 321 | static int __init init_nfs_v4(void) |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 322 | { |
| 323 | int err; |
| 324 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 325 | err = nfs_dns_resolver_init(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 326 | if (err) |
| 327 | goto out; |
| 328 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 329 | err = nfs_idmap_init(); |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 330 | if (err) |
| 331 | goto out1; |
| 332 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 333 | err = nfs4_register_sysctl(); |
| 334 | if (err) |
| 335 | goto out2; |
| 336 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 337 | register_nfs_version(&nfs_v4); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 338 | return 0; |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 339 | out2: |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 340 | nfs_idmap_quit(); |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 341 | out1: |
| 342 | nfs_dns_resolver_destroy(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 343 | out: |
| 344 | return err; |
| 345 | } |
| 346 | |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 347 | static void __exit exit_nfs_v4(void) |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 348 | { |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame^] | 349 | /* Not called in the _init(), conditionally loaded */ |
| 350 | nfs4_pnfs_v3_ds_connect_unload(); |
| 351 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 352 | unregister_nfs_version(&nfs_v4); |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 353 | nfs4_unregister_sysctl(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 354 | nfs_idmap_quit(); |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 355 | nfs_dns_resolver_destroy(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 356 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 357 | |
| 358 | MODULE_LICENSE("GPL"); |
| 359 | |
| 360 | module_init(init_nfs_v4); |
| 361 | module_exit(exit_nfs_v4); |