]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/nfs/nfs4super.c
saner calling conventions for nfs_fs_mount_common()
[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_remote_mount(struct file_system_type *fs_type,
22         int flags, const char *dev_name, void *raw_data);
23 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
24         int flags, const char *dev_name, void *raw_data);
25 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
26         int flags, const char *dev_name, void *raw_data);
27
28 static struct file_system_type nfs4_remote_fs_type = {
29         .owner          = THIS_MODULE,
30         .name           = "nfs4",
31         .mount          = nfs4_remote_mount,
32         .kill_sb        = nfs_kill_super,
33         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
34 };
35
36 static struct file_system_type nfs4_remote_referral_fs_type = {
37         .owner          = THIS_MODULE,
38         .name           = "nfs4",
39         .mount          = nfs4_remote_referral_mount,
40         .kill_sb        = nfs_kill_super,
41         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
42 };
43
44 struct file_system_type nfs4_referral_fs_type = {
45         .owner          = THIS_MODULE,
46         .name           = "nfs4",
47         .mount          = nfs4_referral_mount,
48         .kill_sb        = nfs_kill_super,
49         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
50 };
51
52 static const struct super_operations nfs4_sops = {
53         .alloc_inode    = nfs_alloc_inode,
54         .free_inode     = nfs_free_inode,
55         .write_inode    = nfs4_write_inode,
56         .drop_inode     = nfs_drop_inode,
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
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,
72         .sops     = &nfs4_sops,
73         .xattr    = nfs4_xattr_handlers,
74 };
75
76 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
77 {
78         int ret = nfs_write_inode(inode, wbc);
79
80         if (ret == 0)
81                 ret = pnfs_layoutcommit_inode(inode,
82                                 wbc->sync_mode == WB_SYNC_ALL);
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 {
93         truncate_inode_pages_final(&inode->i_data);
94         clear_inode(inode);
95         /* If we are holding a delegation, return and free it */
96         nfs_inode_evict_delegation(inode);
97         /* Note that above delegreturn would trigger pnfs return-on-close */
98         pnfs_return_layout(inode);
99         pnfs_destroy_layout(NFS_I(inode));
100         /* First call standard NFS clear_inode() code */
101         nfs_clear_inode(inode);
102 }
103
104 /*
105  * Get the superblock for the NFS4 root partition
106  */
107 static struct dentry *
108 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
109                   const char *dev_name, void *info)
110 {
111         struct nfs_mount_info *mount_info = info;
112         struct nfs_server *server;
113
114         mount_info->set_security = nfs_set_sb_security;
115
116         /* Get a volume representation */
117         server = nfs4_create_server(mount_info, &nfs_v4);
118         return nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
119 }
120
121 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
122                 int flags, void *data, const char *hostname)
123 {
124         struct vfsmount *root_mnt;
125         char *root_devname;
126         size_t len;
127
128         len = strlen(hostname) + 5;
129         root_devname = kmalloc(len, GFP_KERNEL);
130         if (root_devname == NULL)
131                 return ERR_PTR(-ENOMEM);
132         /* Does hostname needs to be enclosed in brackets? */
133         if (strchr(hostname, ':'))
134                 snprintf(root_devname, len, "[%s]:/", hostname);
135         else
136                 snprintf(root_devname, len, "%s:/", hostname);
137         root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
138         kfree(root_devname);
139         return root_mnt;
140 }
141
142 struct nfs_referral_count {
143         struct list_head list;
144         const struct task_struct *task;
145         unsigned int referral_count;
146 };
147
148 static LIST_HEAD(nfs_referral_count_list);
149 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
150
151 static struct nfs_referral_count *nfs_find_referral_count(void)
152 {
153         struct nfs_referral_count *p;
154
155         list_for_each_entry(p, &nfs_referral_count_list, list) {
156                 if (p->task == current)
157                         return p;
158         }
159         return NULL;
160 }
161
162 #define NFS_MAX_NESTED_REFERRALS 2
163
164 static int nfs_referral_loop_protect(void)
165 {
166         struct nfs_referral_count *p, *new;
167         int ret = -ENOMEM;
168
169         new = kmalloc(sizeof(*new), GFP_KERNEL);
170         if (!new)
171                 goto out;
172         new->task = current;
173         new->referral_count = 1;
174
175         ret = 0;
176         spin_lock(&nfs_referral_count_list_lock);
177         p = nfs_find_referral_count();
178         if (p != NULL) {
179                 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
180                         ret = -ELOOP;
181                 else
182                         p->referral_count++;
183         } else {
184                 list_add(&new->list, &nfs_referral_count_list);
185                 new = NULL;
186         }
187         spin_unlock(&nfs_referral_count_list_lock);
188         kfree(new);
189 out:
190         return ret;
191 }
192
193 static void nfs_referral_loop_unprotect(void)
194 {
195         struct nfs_referral_count *p;
196
197         spin_lock(&nfs_referral_count_list_lock);
198         p = nfs_find_referral_count();
199         p->referral_count--;
200         if (p->referral_count == 0)
201                 list_del(&p->list);
202         else
203                 p = NULL;
204         spin_unlock(&nfs_referral_count_list_lock);
205         kfree(p);
206 }
207
208 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
209                 const char *export_path)
210 {
211         struct dentry *dentry;
212         int err;
213
214         if (IS_ERR(root_mnt))
215                 return ERR_CAST(root_mnt);
216
217         err = nfs_referral_loop_protect();
218         if (err) {
219                 mntput(root_mnt);
220                 return ERR_PTR(err);
221         }
222
223         dentry = mount_subtree(root_mnt, export_path);
224         nfs_referral_loop_unprotect();
225
226         return dentry;
227 }
228
229 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
230                               struct nfs_mount_info *mount_info,
231                               struct nfs_subversion *nfs_mod)
232 {
233         char *export_path;
234         struct vfsmount *root_mnt;
235         struct dentry *res;
236         struct nfs_parsed_mount_data *data = mount_info->parsed;
237
238         dfprintk(MOUNT, "--> nfs4_try_mount()\n");
239
240         export_path = data->nfs_server.export_path;
241         data->nfs_server.export_path = "/";
242         root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
243                         data->nfs_server.hostname);
244         data->nfs_server.export_path = export_path;
245
246         res = nfs_follow_remote_path(root_mnt, export_path);
247
248         dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
249                  PTR_ERR_OR_ZERO(res),
250                  IS_ERR(res) ? " [error]" : "");
251         return res;
252 }
253
254 static struct dentry *
255 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
256                            const char *dev_name, void *raw_data)
257 {
258         struct nfs_mount_info mount_info = {
259                 .fill_super = nfs_fill_super,
260                 .set_security = nfs_clone_sb_security,
261                 .cloned = raw_data,
262         };
263         struct nfs_server *server;
264         struct dentry *mntroot = ERR_PTR(-ENOMEM);
265
266         dprintk("--> nfs4_referral_get_sb()\n");
267
268         mount_info.mntfh = nfs_alloc_fhandle();
269         if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
270                 goto out;
271
272         /* create a new volume representation */
273         server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
274         mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
275 out:
276         nfs_free_fhandle(mount_info.mntfh);
277         return mntroot;
278 }
279
280 /*
281  * Create an NFS4 server record on referral traversal
282  */
283 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
284                 int flags, const char *dev_name, void *raw_data)
285 {
286         struct nfs_clone_mount *data = raw_data;
287         char *export_path;
288         struct vfsmount *root_mnt;
289         struct dentry *res;
290
291         dprintk("--> nfs4_referral_mount()\n");
292
293         export_path = data->mnt_path;
294         data->mnt_path = "/";
295
296         root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
297                         flags, data, data->hostname);
298         data->mnt_path = export_path;
299
300         res = nfs_follow_remote_path(root_mnt, export_path);
301         dprintk("<-- nfs4_referral_mount() = %d%s\n",
302                 PTR_ERR_OR_ZERO(res),
303                 IS_ERR(res) ? " [error]" : "");
304         return res;
305 }
306
307
308 static int __init init_nfs_v4(void)
309 {
310         int err;
311
312         err = nfs_dns_resolver_init();
313         if (err)
314                 goto out;
315
316         err = nfs_idmap_init();
317         if (err)
318                 goto out1;
319
320         err = nfs4_register_sysctl();
321         if (err)
322                 goto out2;
323
324         register_nfs_version(&nfs_v4);
325         return 0;
326 out2:
327         nfs_idmap_quit();
328 out1:
329         nfs_dns_resolver_destroy();
330 out:
331         return err;
332 }
333
334 static void __exit exit_nfs_v4(void)
335 {
336         /* Not called in the _init(), conditionally loaded */
337         nfs4_pnfs_v3_ds_connect_unload();
338
339         unregister_nfs_version(&nfs_v4);
340         nfs4_unregister_sysctl();
341         nfs_idmap_quit();
342         nfs_dns_resolver_destroy();
343 }
344
345 MODULE_LICENSE("GPL");
346
347 module_init(init_nfs_v4);
348 module_exit(exit_nfs_v4);