2 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/nfs_idmap.h>
7 #include <linux/nfs4_mount.h>
8 #include <linux/nfs_fs.h>
9 #include "delegation.h"
15 #define NFSDBG_FACILITY NFSDBG_VFS
17 static int nfs4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
);
18 static void nfs4_evict_inode(struct inode
*inode
);
19 static struct dentry
*nfs4_remote_mount(struct file_system_type
*fs_type
,
20 int flags
, const char *dev_name
, void *raw_data
);
21 static struct dentry
*nfs4_referral_mount(struct file_system_type
*fs_type
,
22 int flags
, const char *dev_name
, void *raw_data
);
23 static struct dentry
*nfs4_remote_referral_mount(struct file_system_type
*fs_type
,
24 int flags
, const char *dev_name
, void *raw_data
);
26 static struct file_system_type nfs4_remote_fs_type
= {
29 .mount
= nfs4_remote_mount
,
30 .kill_sb
= nfs_kill_super
,
31 .fs_flags
= FS_RENAME_DOES_D_MOVE
|FS_REVAL_DOT
|FS_BINARY_MOUNTDATA
,
34 static struct file_system_type nfs4_remote_referral_fs_type
= {
37 .mount
= nfs4_remote_referral_mount
,
38 .kill_sb
= nfs_kill_super
,
39 .fs_flags
= FS_RENAME_DOES_D_MOVE
|FS_REVAL_DOT
|FS_BINARY_MOUNTDATA
,
42 struct file_system_type nfs4_referral_fs_type
= {
45 .mount
= nfs4_referral_mount
,
46 .kill_sb
= nfs_kill_super
,
47 .fs_flags
= FS_RENAME_DOES_D_MOVE
|FS_REVAL_DOT
|FS_BINARY_MOUNTDATA
,
50 static const struct super_operations nfs4_sops
= {
51 .alloc_inode
= nfs_alloc_inode
,
52 .destroy_inode
= nfs_destroy_inode
,
53 .write_inode
= nfs4_write_inode
,
54 .put_super
= nfs_put_super
,
56 .evict_inode
= nfs4_evict_inode
,
57 .umount_begin
= nfs_umount_begin
,
58 .show_options
= nfs_show_options
,
59 .show_devname
= nfs_show_devname
,
60 .show_path
= nfs_show_path
,
61 .show_stats
= nfs_show_stats
,
62 .remount_fs
= nfs_remount
,
65 struct nfs_subversion nfs_v4
= {
67 .nfs_fs
= &nfs4_fs_type
,
68 .rpc_vers
= &nfs_version4
,
69 .rpc_ops
= &nfs_v4_clientops
,
71 .xattr
= nfs4_xattr_handlers
,
74 static int nfs4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
76 int ret
= nfs_write_inode(inode
, wbc
);
78 if (ret
>= 0 && test_bit(NFS_INO_LAYOUTCOMMIT
, &NFS_I(inode
)->flags
)) {
82 if (wbc
->sync_mode
== WB_SYNC_NONE
)
85 status
= pnfs_layoutcommit_inode(inode
, sync
);
93 * Clean out any remaining NFSv4 state that might be left over due
94 * to open() calls that passed nfs_atomic_lookup, but failed to call
97 static void nfs4_evict_inode(struct inode
*inode
)
99 truncate_inode_pages(&inode
->i_data
, 0);
101 pnfs_return_layout(inode
);
102 pnfs_destroy_layout(NFS_I(inode
));
103 /* If we are holding a delegation, return it! */
104 nfs_inode_return_delegation_noreclaim(inode
);
105 /* First call standard NFS clear_inode() code */
106 nfs_clear_inode(inode
);
110 * Get the superblock for the NFS4 root partition
112 static struct dentry
*
113 nfs4_remote_mount(struct file_system_type
*fs_type
, int flags
,
114 const char *dev_name
, void *info
)
116 struct nfs_mount_info
*mount_info
= info
;
117 struct nfs_server
*server
;
118 struct dentry
*mntroot
= ERR_PTR(-ENOMEM
);
120 mount_info
->set_security
= nfs_set_sb_security
;
122 /* Get a volume representation */
123 server
= nfs4_create_server(mount_info
, &nfs_v4
);
124 if (IS_ERR(server
)) {
125 mntroot
= ERR_CAST(server
);
129 mntroot
= nfs_fs_mount_common(server
, flags
, dev_name
, mount_info
, &nfs_v4
);
135 static struct vfsmount
*nfs_do_root_mount(struct file_system_type
*fs_type
,
136 int flags
, void *data
, const char *hostname
)
138 struct vfsmount
*root_mnt
;
142 len
= strlen(hostname
) + 5;
143 root_devname
= kmalloc(len
, GFP_KERNEL
);
144 if (root_devname
== NULL
)
145 return ERR_PTR(-ENOMEM
);
146 /* Does hostname needs to be enclosed in brackets? */
147 if (strchr(hostname
, ':'))
148 snprintf(root_devname
, len
, "[%s]:/", hostname
);
150 snprintf(root_devname
, len
, "%s:/", hostname
);
151 root_mnt
= vfs_kern_mount(fs_type
, flags
, root_devname
, data
);
156 struct nfs_referral_count
{
157 struct list_head list
;
158 const struct task_struct
*task
;
159 unsigned int referral_count
;
162 static LIST_HEAD(nfs_referral_count_list
);
163 static DEFINE_SPINLOCK(nfs_referral_count_list_lock
);
165 static struct nfs_referral_count
*nfs_find_referral_count(void)
167 struct nfs_referral_count
*p
;
169 list_for_each_entry(p
, &nfs_referral_count_list
, list
) {
170 if (p
->task
== current
)
176 #define NFS_MAX_NESTED_REFERRALS 2
178 static int nfs_referral_loop_protect(void)
180 struct nfs_referral_count
*p
, *new;
183 new = kmalloc(sizeof(*new), GFP_KERNEL
);
187 new->referral_count
= 1;
190 spin_lock(&nfs_referral_count_list_lock
);
191 p
= nfs_find_referral_count();
193 if (p
->referral_count
>= NFS_MAX_NESTED_REFERRALS
)
198 list_add(&new->list
, &nfs_referral_count_list
);
201 spin_unlock(&nfs_referral_count_list_lock
);
207 static void nfs_referral_loop_unprotect(void)
209 struct nfs_referral_count
*p
;
211 spin_lock(&nfs_referral_count_list_lock
);
212 p
= nfs_find_referral_count();
214 if (p
->referral_count
== 0)
218 spin_unlock(&nfs_referral_count_list_lock
);
222 static struct dentry
*nfs_follow_remote_path(struct vfsmount
*root_mnt
,
223 const char *export_path
)
225 struct dentry
*dentry
;
228 if (IS_ERR(root_mnt
))
229 return ERR_CAST(root_mnt
);
231 err
= nfs_referral_loop_protect();
237 dentry
= mount_subtree(root_mnt
, export_path
);
238 nfs_referral_loop_unprotect();
243 struct dentry
*nfs4_try_mount(int flags
, const char *dev_name
,
244 struct nfs_mount_info
*mount_info
,
245 struct nfs_subversion
*nfs_mod
)
248 struct vfsmount
*root_mnt
;
250 struct nfs_parsed_mount_data
*data
= mount_info
->parsed
;
252 dfprintk(MOUNT
, "--> nfs4_try_mount()\n");
254 export_path
= data
->nfs_server
.export_path
;
255 data
->nfs_server
.export_path
= "/";
256 root_mnt
= nfs_do_root_mount(&nfs4_remote_fs_type
, flags
, mount_info
,
257 data
->nfs_server
.hostname
);
258 data
->nfs_server
.export_path
= export_path
;
260 res
= nfs_follow_remote_path(root_mnt
, export_path
);
262 dfprintk(MOUNT
, "<-- nfs4_try_mount() = %ld%s\n",
263 IS_ERR(res
) ? PTR_ERR(res
) : 0,
264 IS_ERR(res
) ? " [error]" : "");
268 static struct dentry
*
269 nfs4_remote_referral_mount(struct file_system_type
*fs_type
, int flags
,
270 const char *dev_name
, void *raw_data
)
272 struct nfs_mount_info mount_info
= {
273 .fill_super
= nfs_fill_super
,
274 .set_security
= nfs_clone_sb_security
,
277 struct nfs_server
*server
;
278 struct dentry
*mntroot
= ERR_PTR(-ENOMEM
);
280 dprintk("--> nfs4_referral_get_sb()\n");
282 mount_info
.mntfh
= nfs_alloc_fhandle();
283 if (mount_info
.cloned
== NULL
|| mount_info
.mntfh
== NULL
)
286 /* create a new volume representation */
287 server
= nfs4_create_referral_server(mount_info
.cloned
, mount_info
.mntfh
);
288 if (IS_ERR(server
)) {
289 mntroot
= ERR_CAST(server
);
293 mntroot
= nfs_fs_mount_common(server
, flags
, dev_name
, &mount_info
, &nfs_v4
);
295 nfs_free_fhandle(mount_info
.mntfh
);
300 * Create an NFS4 server record on referral traversal
302 static struct dentry
*nfs4_referral_mount(struct file_system_type
*fs_type
,
303 int flags
, const char *dev_name
, void *raw_data
)
305 struct nfs_clone_mount
*data
= raw_data
;
307 struct vfsmount
*root_mnt
;
310 dprintk("--> nfs4_referral_mount()\n");
312 export_path
= data
->mnt_path
;
313 data
->mnt_path
= "/";
315 root_mnt
= nfs_do_root_mount(&nfs4_remote_referral_fs_type
,
316 flags
, data
, data
->hostname
);
317 data
->mnt_path
= export_path
;
319 res
= nfs_follow_remote_path(root_mnt
, export_path
);
320 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
321 IS_ERR(res
) ? PTR_ERR(res
) : 0,
322 IS_ERR(res
) ? " [error]" : "");
327 static int __init
init_nfs_v4(void)
331 err
= nfs_idmap_init();
335 err
= nfs4_register_sysctl();
339 register_nfs_version(&nfs_v4
);
347 static void __exit
exit_nfs_v4(void)
349 unregister_nfs_version(&nfs_v4
);
350 nfs4_unregister_sysctl();
354 MODULE_LICENSE("GPL");
356 module_init(init_nfs_v4
);
357 module_exit(exit_nfs_v4
);