]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/nfs/nfs4super.c
nfs: merge xdev and remote file_system_type
[linux.git] / fs / nfs / nfs4super.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4  */
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/nfs4_mount.h>
8 #include <linux/nfs_fs.h>
9 #include "delegation.h"
10 #include "internal.h"
11 #include "nfs4_fs.h"
12 #include "nfs4idmap.h"
13 #include "dns_resolve.h"
14 #include "pnfs.h"
15 #include "nfs.h"
16
17 #define NFSDBG_FACILITY         NFSDBG_VFS
18
19 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
20 static void nfs4_evict_inode(struct inode *inode);
21 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
22         int flags, const char *dev_name, void *raw_data);
23
24 struct file_system_type nfs4_referral_fs_type = {
25         .owner          = THIS_MODULE,
26         .name           = "nfs4",
27         .mount          = nfs4_referral_mount,
28         .kill_sb        = nfs_kill_super,
29         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
30 };
31
32 static const struct super_operations nfs4_sops = {
33         .alloc_inode    = nfs_alloc_inode,
34         .free_inode     = nfs_free_inode,
35         .write_inode    = nfs4_write_inode,
36         .drop_inode     = nfs_drop_inode,
37         .statfs         = nfs_statfs,
38         .evict_inode    = nfs4_evict_inode,
39         .umount_begin   = nfs_umount_begin,
40         .show_options   = nfs_show_options,
41         .show_devname   = nfs_show_devname,
42         .show_path      = nfs_show_path,
43         .show_stats     = nfs_show_stats,
44         .remount_fs     = nfs_remount,
45 };
46
47 struct nfs_subversion nfs_v4 = {
48         .owner = THIS_MODULE,
49         .nfs_fs   = &nfs4_fs_type,
50         .rpc_vers = &nfs_version4,
51         .rpc_ops  = &nfs_v4_clientops,
52         .sops     = &nfs4_sops,
53         .xattr    = nfs4_xattr_handlers,
54 };
55
56 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
57 {
58         int ret = nfs_write_inode(inode, wbc);
59
60         if (ret == 0)
61                 ret = pnfs_layoutcommit_inode(inode,
62                                 wbc->sync_mode == WB_SYNC_ALL);
63         return ret;
64 }
65
66 /*
67  * Clean out any remaining NFSv4 state that might be left over due
68  * to open() calls that passed nfs_atomic_lookup, but failed to call
69  * nfs_open().
70  */
71 static void nfs4_evict_inode(struct inode *inode)
72 {
73         truncate_inode_pages_final(&inode->i_data);
74         clear_inode(inode);
75         /* If we are holding a delegation, return and free it */
76         nfs_inode_evict_delegation(inode);
77         /* Note that above delegreturn would trigger pnfs return-on-close */
78         pnfs_return_layout(inode);
79         pnfs_destroy_layout(NFS_I(inode));
80         /* First call standard NFS clear_inode() code */
81         nfs_clear_inode(inode);
82 }
83
84 struct nfs_referral_count {
85         struct list_head list;
86         const struct task_struct *task;
87         unsigned int referral_count;
88 };
89
90 static LIST_HEAD(nfs_referral_count_list);
91 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
92
93 static struct nfs_referral_count *nfs_find_referral_count(void)
94 {
95         struct nfs_referral_count *p;
96
97         list_for_each_entry(p, &nfs_referral_count_list, list) {
98                 if (p->task == current)
99                         return p;
100         }
101         return NULL;
102 }
103
104 #define NFS_MAX_NESTED_REFERRALS 2
105
106 static int nfs_referral_loop_protect(void)
107 {
108         struct nfs_referral_count *p, *new;
109         int ret = -ENOMEM;
110
111         new = kmalloc(sizeof(*new), GFP_KERNEL);
112         if (!new)
113                 goto out;
114         new->task = current;
115         new->referral_count = 1;
116
117         ret = 0;
118         spin_lock(&nfs_referral_count_list_lock);
119         p = nfs_find_referral_count();
120         if (p != NULL) {
121                 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
122                         ret = -ELOOP;
123                 else
124                         p->referral_count++;
125         } else {
126                 list_add(&new->list, &nfs_referral_count_list);
127                 new = NULL;
128         }
129         spin_unlock(&nfs_referral_count_list_lock);
130         kfree(new);
131 out:
132         return ret;
133 }
134
135 static void nfs_referral_loop_unprotect(void)
136 {
137         struct nfs_referral_count *p;
138
139         spin_lock(&nfs_referral_count_list_lock);
140         p = nfs_find_referral_count();
141         p->referral_count--;
142         if (p->referral_count == 0)
143                 list_del(&p->list);
144         else
145                 p = NULL;
146         spin_unlock(&nfs_referral_count_list_lock);
147         kfree(p);
148 }
149
150 static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags,
151                                     struct nfs_mount_info *info,
152                                     const char *hostname,
153                                     const char *export_path)
154 {
155         struct vfsmount *root_mnt;
156         struct dentry *dentry;
157         char *root_devname;
158         int err;
159         size_t len;
160
161         if (IS_ERR(server))
162                 return ERR_CAST(server);
163
164         len = strlen(hostname) + 5;
165         root_devname = kmalloc(len, GFP_KERNEL);
166         if (root_devname == NULL) {
167                 nfs_free_server(server);
168                 return ERR_PTR(-ENOMEM);
169         }
170
171         /* Does hostname needs to be enclosed in brackets? */
172         if (strchr(hostname, ':'))
173                 snprintf(root_devname, len, "[%s]:/", hostname);
174         else
175                 snprintf(root_devname, len, "%s:/", hostname);
176         info->server = server;
177         root_mnt = vfs_kern_mount(&nfs_prepared_fs_type, flags, root_devname, info);
178         if (info->server)
179                 nfs_free_server(info->server);
180         info->server = NULL;
181         kfree(root_devname);
182
183         if (IS_ERR(root_mnt))
184                 return ERR_CAST(root_mnt);
185
186         err = nfs_referral_loop_protect();
187         if (err) {
188                 mntput(root_mnt);
189                 return ERR_PTR(err);
190         }
191
192         dentry = mount_subtree(root_mnt, export_path);
193         nfs_referral_loop_unprotect();
194
195         return dentry;
196 }
197
198 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
199                               struct nfs_mount_info *mount_info)
200 {
201         struct nfs_parsed_mount_data *data = mount_info->parsed;
202         struct dentry *res;
203
204         mount_info->set_security = nfs_set_sb_security;
205
206         dfprintk(MOUNT, "--> nfs4_try_mount()\n");
207
208         res = do_nfs4_mount(nfs4_create_server(mount_info, &nfs_v4),
209                             flags, mount_info,
210                             data->nfs_server.hostname,
211                             data->nfs_server.export_path);
212
213         dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
214                  PTR_ERR_OR_ZERO(res),
215                  IS_ERR(res) ? " [error]" : "");
216         return res;
217 }
218
219 /*
220  * Create an NFS4 server record on referral traversal
221  */
222 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
223                 int flags, const char *dev_name, void *raw_data)
224 {
225         struct nfs_clone_mount *data = raw_data;
226         struct nfs_mount_info mount_info = {
227                 .fill_super = nfs_fill_super,
228                 .set_security = nfs_clone_sb_security,
229                 .cloned = data,
230                 .nfs_mod = &nfs_v4,
231         };
232         struct dentry *res;
233
234         dprintk("--> nfs4_referral_mount()\n");
235
236         mount_info.mntfh = nfs_alloc_fhandle();
237         if (!mount_info.mntfh)
238                 return ERR_PTR(-ENOMEM);
239
240         res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned,
241                                                         mount_info.mntfh),
242                             flags, &mount_info, data->hostname, data->mnt_path);
243
244         dprintk("<-- nfs4_referral_mount() = %d%s\n",
245                 PTR_ERR_OR_ZERO(res),
246                 IS_ERR(res) ? " [error]" : "");
247
248         nfs_free_fhandle(mount_info.mntfh);
249         return res;
250 }
251
252
253 static int __init init_nfs_v4(void)
254 {
255         int err;
256
257         err = nfs_dns_resolver_init();
258         if (err)
259                 goto out;
260
261         err = nfs_idmap_init();
262         if (err)
263                 goto out1;
264
265         err = nfs4_register_sysctl();
266         if (err)
267                 goto out2;
268
269         register_nfs_version(&nfs_v4);
270         return 0;
271 out2:
272         nfs_idmap_quit();
273 out1:
274         nfs_dns_resolver_destroy();
275 out:
276         return err;
277 }
278
279 static void __exit exit_nfs_v4(void)
280 {
281         /* Not called in the _init(), conditionally loaded */
282         nfs4_pnfs_v3_ds_connect_unload();
283
284         unregister_nfs_version(&nfs_v4);
285         nfs4_unregister_sysctl();
286         nfs_idmap_quit();
287         nfs_dns_resolver_destroy();
288 }
289
290 MODULE_LICENSE("GPL");
291
292 module_init(init_nfs_v4);
293 module_exit(exit_nfs_v4);