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"
12 #include "dns_resolve.h"
16 #define NFSDBG_FACILITY NFSDBG_VFS
18 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
19 static void nfs4_evict_inode(struct inode *inode);
20 static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
21 int flags, const char *dev_name, void *raw_data);
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);
27 static struct file_system_type nfs4_remote_fs_type = {
30 .mount = nfs4_remote_mount,
31 .kill_sb = nfs_kill_super,
32 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
35 static struct file_system_type nfs4_remote_referral_fs_type = {
38 .mount = nfs4_remote_referral_mount,
39 .kill_sb = nfs_kill_super,
40 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
43 struct file_system_type nfs4_referral_fs_type = {
46 .mount = nfs4_referral_mount,
47 .kill_sb = nfs_kill_super,
48 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
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,
55 .drop_inode = nfs_drop_inode,
56 .put_super = nfs_put_super,
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,
67 struct nfs_subversion nfs_v4 = {
69 .nfs_fs = &nfs4_fs_type,
70 .rpc_vers = &nfs_version4,
71 .rpc_ops = &nfs_v4_clientops,
73 .xattr = nfs4_xattr_handlers,
76 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
78 int ret = nfs_write_inode(inode, wbc);
80 if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
84 if (wbc->sync_mode == WB_SYNC_NONE)
87 status = pnfs_layoutcommit_inode(inode, sync);
95 * Clean out any remaining NFSv4 state that might be left over due
96 * to open() calls that passed nfs_atomic_lookup, but failed to call
99 static void nfs4_evict_inode(struct inode *inode)
101 truncate_inode_pages(&inode->i_data, 0);
103 pnfs_return_layout(inode);
104 pnfs_destroy_layout(NFS_I(inode));
105 /* If we are holding a delegation, return it! */
106 nfs_inode_return_delegation_noreclaim(inode);
107 /* First call standard NFS clear_inode() code */
108 nfs_clear_inode(inode);
112 * Get the superblock for the NFS4 root partition
114 static struct dentry *
115 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
116 const char *dev_name, void *info)
118 struct nfs_mount_info *mount_info = info;
119 struct nfs_server *server;
120 struct dentry *mntroot = ERR_PTR(-ENOMEM);
122 mount_info->set_security = nfs_set_sb_security;
124 /* Get a volume representation */
125 server = nfs4_create_server(mount_info, &nfs_v4);
126 if (IS_ERR(server)) {
127 mntroot = ERR_CAST(server);
131 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
137 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
138 int flags, void *data, const char *hostname)
140 struct vfsmount *root_mnt;
144 len = strlen(hostname) + 5;
145 root_devname = kmalloc(len, GFP_KERNEL);
146 if (root_devname == NULL)
147 return ERR_PTR(-ENOMEM);
148 /* Does hostname needs to be enclosed in brackets? */
149 if (strchr(hostname, ':'))
150 snprintf(root_devname, len, "[%s]:/", hostname);
152 snprintf(root_devname, len, "%s:/", hostname);
153 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
158 struct nfs_referral_count {
159 struct list_head list;
160 const struct task_struct *task;
161 unsigned int referral_count;
164 static LIST_HEAD(nfs_referral_count_list);
165 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
167 static struct nfs_referral_count *nfs_find_referral_count(void)
169 struct nfs_referral_count *p;
171 list_for_each_entry(p, &nfs_referral_count_list, list) {
172 if (p->task == current)
178 #define NFS_MAX_NESTED_REFERRALS 2
180 static int nfs_referral_loop_protect(void)
182 struct nfs_referral_count *p, *new;
185 new = kmalloc(sizeof(*new), GFP_KERNEL);
189 new->referral_count = 1;
192 spin_lock(&nfs_referral_count_list_lock);
193 p = nfs_find_referral_count();
195 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
200 list_add(&new->list, &nfs_referral_count_list);
203 spin_unlock(&nfs_referral_count_list_lock);
209 static void nfs_referral_loop_unprotect(void)
211 struct nfs_referral_count *p;
213 spin_lock(&nfs_referral_count_list_lock);
214 p = nfs_find_referral_count();
216 if (p->referral_count == 0)
220 spin_unlock(&nfs_referral_count_list_lock);
224 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
225 const char *export_path)
227 struct dentry *dentry;
230 if (IS_ERR(root_mnt))
231 return ERR_CAST(root_mnt);
233 err = nfs_referral_loop_protect();
239 dentry = mount_subtree(root_mnt, export_path);
240 nfs_referral_loop_unprotect();
245 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
246 struct nfs_mount_info *mount_info,
247 struct nfs_subversion *nfs_mod)
250 struct vfsmount *root_mnt;
252 struct nfs_parsed_mount_data *data = mount_info->parsed;
254 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
256 if (data->auth_flavors[0] == RPC_AUTH_MAXFLAVOR)
257 data->auth_flavors[0] = RPC_AUTH_UNIX;
258 export_path = data->nfs_server.export_path;
259 data->nfs_server.export_path = "/";
260 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
261 data->nfs_server.hostname);
262 data->nfs_server.export_path = export_path;
264 res = nfs_follow_remote_path(root_mnt, export_path);
266 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
267 IS_ERR(res) ? PTR_ERR(res) : 0,
268 IS_ERR(res) ? " [error]" : "");
272 static struct dentry *
273 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
274 const char *dev_name, void *raw_data)
276 struct nfs_mount_info mount_info = {
277 .fill_super = nfs_fill_super,
278 .set_security = nfs_clone_sb_security,
281 struct nfs_server *server;
282 struct dentry *mntroot = ERR_PTR(-ENOMEM);
284 dprintk("--> nfs4_referral_get_sb()\n");
286 mount_info.mntfh = nfs_alloc_fhandle();
287 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
290 /* create a new volume representation */
291 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
292 if (IS_ERR(server)) {
293 mntroot = ERR_CAST(server);
297 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
299 nfs_free_fhandle(mount_info.mntfh);
304 * Create an NFS4 server record on referral traversal
306 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
307 int flags, const char *dev_name, void *raw_data)
309 struct nfs_clone_mount *data = raw_data;
311 struct vfsmount *root_mnt;
314 dprintk("--> nfs4_referral_mount()\n");
316 export_path = data->mnt_path;
317 data->mnt_path = "/";
319 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
320 flags, data, data->hostname);
321 data->mnt_path = export_path;
323 res = nfs_follow_remote_path(root_mnt, export_path);
324 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
325 IS_ERR(res) ? PTR_ERR(res) : 0,
326 IS_ERR(res) ? " [error]" : "");
331 static int __init init_nfs_v4(void)
335 err = nfs_dns_resolver_init();
339 err = nfs_idmap_init();
343 err = nfs4_register_sysctl();
347 register_nfs_version(&nfs_v4);
352 nfs_dns_resolver_destroy();
357 static void __exit exit_nfs_v4(void)
359 unregister_nfs_version(&nfs_v4);
360 nfs4_unregister_sysctl();
362 nfs_dns_resolver_destroy();
365 MODULE_LICENSE("GPL");
367 module_init(init_nfs_v4);
368 module_exit(exit_nfs_v4);