56c1826fcc2ff7c2c13c37ad78e56dc49d6ee615
[platform/kernel/linux-starfive.git] / fs / smb / client / cifsfs.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   Common Internet FileSystem (CIFS) client
8  *
9  */
10
11 /* Note that BB means BUGBUG (ie something to fix eventually) */
12
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/filelock.h>
16 #include <linux/mount.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/seq_file.h>
21 #include <linux/vfs.h>
22 #include <linux/mempool.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/freezer.h>
26 #include <linux/namei.h>
27 #include <linux/random.h>
28 #include <linux/uuid.h>
29 #include <linux/xattr.h>
30 #include <uapi/linux/magic.h>
31 #include <net/ipv6.h>
32 #include "cifsfs.h"
33 #include "cifspdu.h"
34 #define DECLARE_GLOBALS_HERE
35 #include "cifsglob.h"
36 #include "cifsproto.h"
37 #include "cifs_debug.h"
38 #include "cifs_fs_sb.h"
39 #include <linux/mm.h>
40 #include <linux/key-type.h>
41 #include "cifs_spnego.h"
42 #include "fscache.h"
43 #ifdef CONFIG_CIFS_DFS_UPCALL
44 #include "dfs_cache.h"
45 #endif
46 #ifdef CONFIG_CIFS_SWN_UPCALL
47 #include "netlink.h"
48 #endif
49 #include "fs_context.h"
50 #include "cached_dir.h"
51
52 /*
53  * DOS dates from 1980/1/1 through 2107/12/31
54  * Protocol specifications indicate the range should be to 119, which
55  * limits maximum year to 2099. But this range has not been checked.
56  */
57 #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
58 #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
59 #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
60
61 int cifsFYI = 0;
62 bool traceSMB;
63 bool enable_oplocks = true;
64 bool linuxExtEnabled = true;
65 bool lookupCacheEnabled = true;
66 bool disable_legacy_dialects; /* false by default */
67 bool enable_gcm_256 = true;
68 bool require_gcm_256; /* false by default */
69 bool enable_negotiate_signing; /* false by default */
70 unsigned int global_secflags = CIFSSEC_DEF;
71 /* unsigned int ntlmv2_support = 0; */
72 unsigned int sign_CIFS_PDUs = 1;
73
74 /*
75  * Global transaction id (XID) information
76  */
77 unsigned int GlobalCurrentXid;  /* protected by GlobalMid_Sem */
78 unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
79 unsigned int GlobalMaxActiveXid;        /* prot by GlobalMid_Sem */
80 spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
81
82 /*
83  *  Global counters, updated atomically
84  */
85 atomic_t sesInfoAllocCount;
86 atomic_t tconInfoAllocCount;
87 atomic_t tcpSesNextId;
88 atomic_t tcpSesAllocCount;
89 atomic_t tcpSesReconnectCount;
90 atomic_t tconInfoReconnectCount;
91
92 atomic_t mid_count;
93 atomic_t buf_alloc_count;
94 atomic_t small_buf_alloc_count;
95 #ifdef CONFIG_CIFS_STATS2
96 atomic_t total_buf_alloc_count;
97 atomic_t total_small_buf_alloc_count;
98 #endif/* STATS2 */
99 struct list_head        cifs_tcp_ses_list;
100 spinlock_t              cifs_tcp_ses_lock;
101 static const struct super_operations cifs_super_ops;
102 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
103 module_param(CIFSMaxBufSize, uint, 0444);
104 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
105                                  "for CIFS requests. "
106                                  "Default: 16384 Range: 8192 to 130048");
107 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
108 module_param(cifs_min_rcv, uint, 0444);
109 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
110                                 "1 to 64");
111 unsigned int cifs_min_small = 30;
112 module_param(cifs_min_small, uint, 0444);
113 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
114                                  "Range: 2 to 256");
115 unsigned int cifs_max_pending = CIFS_MAX_REQ;
116 module_param(cifs_max_pending, uint, 0444);
117 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
118                                    "CIFS/SMB1 dialect (N/A for SMB3) "
119                                    "Default: 32767 Range: 2 to 32767.");
120 unsigned int dir_cache_timeout = 30;
121 module_param(dir_cache_timeout, uint, 0644);
122 MODULE_PARM_DESC(dir_cache_timeout, "Number of seconds to cache directory contents for which we have a lease. Default: 30 "
123                                  "Range: 1 to 65000 seconds, 0 to disable caching dir contents");
124 #ifdef CONFIG_CIFS_STATS2
125 unsigned int slow_rsp_threshold = 1;
126 module_param(slow_rsp_threshold, uint, 0644);
127 MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
128                                    "before logging that a response is delayed. "
129                                    "Default: 1 (if set to 0 disables msg).");
130 #endif /* STATS2 */
131
132 module_param(enable_oplocks, bool, 0644);
133 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
134
135 module_param(enable_gcm_256, bool, 0644);
136 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
137
138 module_param(require_gcm_256, bool, 0644);
139 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
140
141 module_param(enable_negotiate_signing, bool, 0644);
142 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
143
144 module_param(disable_legacy_dialects, bool, 0644);
145 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
146                                   "helpful to restrict the ability to "
147                                   "override the default dialects (SMB2.1, "
148                                   "SMB3 and SMB3.02) on mount with old "
149                                   "dialects (CIFS/SMB1 and SMB2) since "
150                                   "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
151                                   " and less secure. Default: n/N/0");
152
153 extern mempool_t *cifs_sm_req_poolp;
154 extern mempool_t *cifs_req_poolp;
155 extern mempool_t *cifs_mid_poolp;
156
157 struct workqueue_struct *cifsiod_wq;
158 struct workqueue_struct *decrypt_wq;
159 struct workqueue_struct *fileinfo_put_wq;
160 struct workqueue_struct *cifsoplockd_wq;
161 struct workqueue_struct *deferredclose_wq;
162 __u32 cifs_lock_secret;
163
164 /*
165  * Bumps refcount for cifs super block.
166  * Note that it should be only called if a referece to VFS super block is
167  * already held, e.g. in open-type syscalls context. Otherwise it can race with
168  * atomic_dec_and_test in deactivate_locked_super.
169  */
170 void
171 cifs_sb_active(struct super_block *sb)
172 {
173         struct cifs_sb_info *server = CIFS_SB(sb);
174
175         if (atomic_inc_return(&server->active) == 1)
176                 atomic_inc(&sb->s_active);
177 }
178
179 void
180 cifs_sb_deactive(struct super_block *sb)
181 {
182         struct cifs_sb_info *server = CIFS_SB(sb);
183
184         if (atomic_dec_and_test(&server->active))
185                 deactivate_super(sb);
186 }
187
188 static int
189 cifs_read_super(struct super_block *sb)
190 {
191         struct inode *inode;
192         struct cifs_sb_info *cifs_sb;
193         struct cifs_tcon *tcon;
194         struct timespec64 ts;
195         int rc = 0;
196
197         cifs_sb = CIFS_SB(sb);
198         tcon = cifs_sb_master_tcon(cifs_sb);
199
200         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
201                 sb->s_flags |= SB_POSIXACL;
202
203         if (tcon->snapshot_time)
204                 sb->s_flags |= SB_RDONLY;
205
206         if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
207                 sb->s_maxbytes = MAX_LFS_FILESIZE;
208         else
209                 sb->s_maxbytes = MAX_NON_LFS;
210
211         /*
212          * Some very old servers like DOS and OS/2 used 2 second granularity
213          * (while all current servers use 100ns granularity - see MS-DTYP)
214          * but 1 second is the maximum allowed granularity for the VFS
215          * so for old servers set time granularity to 1 second while for
216          * everything else (current servers) set it to 100ns.
217          */
218         if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
219             ((tcon->ses->capabilities &
220               tcon->ses->server->vals->cap_nt_find) == 0) &&
221             !tcon->unix_ext) {
222                 sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
223                 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
224                 sb->s_time_min = ts.tv_sec;
225                 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
226                                     cpu_to_le16(SMB_TIME_MAX), 0);
227                 sb->s_time_max = ts.tv_sec;
228         } else {
229                 /*
230                  * Almost every server, including all SMB2+, uses DCE TIME
231                  * ie 100 nanosecond units, since 1601.  See MS-DTYP and MS-FSCC
232                  */
233                 sb->s_time_gran = 100;
234                 ts = cifs_NTtimeToUnix(0);
235                 sb->s_time_min = ts.tv_sec;
236                 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
237                 sb->s_time_max = ts.tv_sec;
238         }
239
240         sb->s_magic = CIFS_SUPER_MAGIC;
241         sb->s_op = &cifs_super_ops;
242         sb->s_xattr = cifs_xattr_handlers;
243         rc = super_setup_bdi(sb);
244         if (rc)
245                 goto out_no_root;
246         /* tune readahead according to rsize if readahead size not set on mount */
247         if (cifs_sb->ctx->rsize == 0)
248                 cifs_sb->ctx->rsize =
249                         tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
250         if (cifs_sb->ctx->rasize)
251                 sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
252         else
253                 sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE);
254
255         sb->s_blocksize = CIFS_MAX_MSGSIZE;
256         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
257         inode = cifs_root_iget(sb);
258
259         if (IS_ERR(inode)) {
260                 rc = PTR_ERR(inode);
261                 goto out_no_root;
262         }
263
264         if (tcon->nocase)
265                 sb->s_d_op = &cifs_ci_dentry_ops;
266         else
267                 sb->s_d_op = &cifs_dentry_ops;
268
269         sb->s_root = d_make_root(inode);
270         if (!sb->s_root) {
271                 rc = -ENOMEM;
272                 goto out_no_root;
273         }
274
275 #ifdef CONFIG_CIFS_NFSD_EXPORT
276         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
277                 cifs_dbg(FYI, "export ops supported\n");
278                 sb->s_export_op = &cifs_export_ops;
279         }
280 #endif /* CONFIG_CIFS_NFSD_EXPORT */
281
282         return 0;
283
284 out_no_root:
285         cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
286         return rc;
287 }
288
289 static void cifs_kill_sb(struct super_block *sb)
290 {
291         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
292
293         /*
294          * We ned to release all dentries for the cached directories
295          * before we kill the sb.
296          */
297         if (cifs_sb->root) {
298                 close_all_cached_dirs(cifs_sb);
299
300                 /* finally release root dentry */
301                 dput(cifs_sb->root);
302                 cifs_sb->root = NULL;
303         }
304
305         kill_anon_super(sb);
306         cifs_umount(cifs_sb);
307 }
308
309 static int
310 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
311 {
312         struct super_block *sb = dentry->d_sb;
313         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
314         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
315         struct TCP_Server_Info *server = tcon->ses->server;
316         unsigned int xid;
317         int rc = 0;
318
319         xid = get_xid();
320
321         if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
322                 buf->f_namelen =
323                        le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
324         else
325                 buf->f_namelen = PATH_MAX;
326
327         buf->f_fsid.val[0] = tcon->vol_serial_number;
328         /* are using part of create time for more randomness, see man statfs */
329         buf->f_fsid.val[1] =  (int)le64_to_cpu(tcon->vol_create_time);
330
331         buf->f_files = 0;       /* undefined */
332         buf->f_ffree = 0;       /* unlimited */
333
334         if (server->ops->queryfs)
335                 rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
336
337         free_xid(xid);
338         return rc;
339 }
340
341 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
342 {
343         struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
344         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
345         struct TCP_Server_Info *server = tcon->ses->server;
346
347         if (server->ops->fallocate)
348                 return server->ops->fallocate(file, tcon, mode, off, len);
349
350         return -EOPNOTSUPP;
351 }
352
353 static int cifs_permission(struct mnt_idmap *idmap,
354                            struct inode *inode, int mask)
355 {
356         struct cifs_sb_info *cifs_sb;
357
358         cifs_sb = CIFS_SB(inode->i_sb);
359
360         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
361                 if ((mask & MAY_EXEC) && !execute_ok(inode))
362                         return -EACCES;
363                 else
364                         return 0;
365         } else /* file mode might have been restricted at mount time
366                 on the client (above and beyond ACL on servers) for
367                 servers which do not support setting and viewing mode bits,
368                 so allowing client to check permissions is useful */
369                 return generic_permission(&nop_mnt_idmap, inode, mask);
370 }
371
372 static struct kmem_cache *cifs_inode_cachep;
373 static struct kmem_cache *cifs_req_cachep;
374 static struct kmem_cache *cifs_mid_cachep;
375 static struct kmem_cache *cifs_sm_req_cachep;
376 mempool_t *cifs_sm_req_poolp;
377 mempool_t *cifs_req_poolp;
378 mempool_t *cifs_mid_poolp;
379
380 static struct inode *
381 cifs_alloc_inode(struct super_block *sb)
382 {
383         struct cifsInodeInfo *cifs_inode;
384         cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
385         if (!cifs_inode)
386                 return NULL;
387         cifs_inode->cifsAttrs = 0x20;   /* default */
388         cifs_inode->time = 0;
389         /*
390          * Until the file is open and we have gotten oplock info back from the
391          * server, can not assume caching of file data or metadata.
392          */
393         cifs_set_oplock_level(cifs_inode, 0);
394         cifs_inode->flags = 0;
395         spin_lock_init(&cifs_inode->writers_lock);
396         cifs_inode->writers = 0;
397         cifs_inode->netfs.inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
398         cifs_inode->server_eof = 0;
399         cifs_inode->uniqueid = 0;
400         cifs_inode->createtime = 0;
401         cifs_inode->epoch = 0;
402         spin_lock_init(&cifs_inode->open_file_lock);
403         generate_random_uuid(cifs_inode->lease_key);
404         cifs_inode->symlink_target = NULL;
405
406         /*
407          * Can not set i_flags here - they get immediately overwritten to zero
408          * by the VFS.
409          */
410         /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
411         INIT_LIST_HEAD(&cifs_inode->openFileList);
412         INIT_LIST_HEAD(&cifs_inode->llist);
413         INIT_LIST_HEAD(&cifs_inode->deferred_closes);
414         spin_lock_init(&cifs_inode->deferred_lock);
415         return &cifs_inode->netfs.inode;
416 }
417
418 static void
419 cifs_free_inode(struct inode *inode)
420 {
421         struct cifsInodeInfo *cinode = CIFS_I(inode);
422
423         if (S_ISLNK(inode->i_mode))
424                 kfree(cinode->symlink_target);
425         kmem_cache_free(cifs_inode_cachep, cinode);
426 }
427
428 static void
429 cifs_evict_inode(struct inode *inode)
430 {
431         truncate_inode_pages_final(&inode->i_data);
432         if (inode->i_state & I_PINNING_FSCACHE_WB)
433                 cifs_fscache_unuse_inode_cookie(inode, true);
434         cifs_fscache_release_inode_cookie(inode);
435         clear_inode(inode);
436 }
437
438 static void
439 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
440 {
441         struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
442         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
443
444         seq_puts(s, ",addr=");
445
446         switch (server->dstaddr.ss_family) {
447         case AF_INET:
448                 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
449                 break;
450         case AF_INET6:
451                 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
452                 if (sa6->sin6_scope_id)
453                         seq_printf(s, "%%%u", sa6->sin6_scope_id);
454                 break;
455         default:
456                 seq_puts(s, "(unknown)");
457         }
458         if (server->rdma)
459                 seq_puts(s, ",rdma");
460 }
461
462 static void
463 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
464 {
465         if (ses->sectype == Unspecified) {
466                 if (ses->user_name == NULL)
467                         seq_puts(s, ",sec=none");
468                 return;
469         }
470
471         seq_puts(s, ",sec=");
472
473         switch (ses->sectype) {
474         case NTLMv2:
475                 seq_puts(s, "ntlmv2");
476                 break;
477         case Kerberos:
478                 seq_puts(s, "krb5");
479                 break;
480         case RawNTLMSSP:
481                 seq_puts(s, "ntlmssp");
482                 break;
483         default:
484                 /* shouldn't ever happen */
485                 seq_puts(s, "unknown");
486                 break;
487         }
488
489         if (ses->sign)
490                 seq_puts(s, "i");
491
492         if (ses->sectype == Kerberos)
493                 seq_printf(s, ",cruid=%u",
494                            from_kuid_munged(&init_user_ns, ses->cred_uid));
495 }
496
497 static void
498 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
499 {
500         seq_puts(s, ",cache=");
501
502         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
503                 seq_puts(s, "strict");
504         else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
505                 seq_puts(s, "none");
506         else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
507                 seq_puts(s, "singleclient"); /* assume only one client access */
508         else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
509                 seq_puts(s, "ro"); /* read only caching assumed */
510         else
511                 seq_puts(s, "loose");
512 }
513
514 /*
515  * cifs_show_devname() is used so we show the mount device name with correct
516  * format (e.g. forward slashes vs. back slashes) in /proc/mounts
517  */
518 static int cifs_show_devname(struct seq_file *m, struct dentry *root)
519 {
520         struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
521         char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
522
523         if (devname == NULL)
524                 seq_puts(m, "none");
525         else {
526                 convert_delimiter(devname, '/');
527                 /* escape all spaces in share names */
528                 seq_escape(m, devname, " \t");
529                 kfree(devname);
530         }
531         return 0;
532 }
533
534 /*
535  * cifs_show_options() is for displaying mount options in /proc/mounts.
536  * Not all settable options are displayed but most of the important
537  * ones are.
538  */
539 static int
540 cifs_show_options(struct seq_file *s, struct dentry *root)
541 {
542         struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
543         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
544         struct sockaddr *srcaddr;
545         srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
546
547         seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
548         cifs_show_security(s, tcon->ses);
549         cifs_show_cache_flavor(s, cifs_sb);
550
551         if (tcon->no_lease)
552                 seq_puts(s, ",nolease");
553         if (cifs_sb->ctx->multiuser)
554                 seq_puts(s, ",multiuser");
555         else if (tcon->ses->user_name)
556                 seq_show_option(s, "username", tcon->ses->user_name);
557
558         if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
559                 seq_show_option(s, "domain", tcon->ses->domainName);
560
561         if (srcaddr->sa_family != AF_UNSPEC) {
562                 struct sockaddr_in *saddr4;
563                 struct sockaddr_in6 *saddr6;
564                 saddr4 = (struct sockaddr_in *)srcaddr;
565                 saddr6 = (struct sockaddr_in6 *)srcaddr;
566                 if (srcaddr->sa_family == AF_INET6)
567                         seq_printf(s, ",srcaddr=%pI6c",
568                                    &saddr6->sin6_addr);
569                 else if (srcaddr->sa_family == AF_INET)
570                         seq_printf(s, ",srcaddr=%pI4",
571                                    &saddr4->sin_addr.s_addr);
572                 else
573                         seq_printf(s, ",srcaddr=BAD-AF:%i",
574                                    (int)(srcaddr->sa_family));
575         }
576
577         seq_printf(s, ",uid=%u",
578                    from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
579         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
580                 seq_puts(s, ",forceuid");
581         else
582                 seq_puts(s, ",noforceuid");
583
584         seq_printf(s, ",gid=%u",
585                    from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
586         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
587                 seq_puts(s, ",forcegid");
588         else
589                 seq_puts(s, ",noforcegid");
590
591         cifs_show_address(s, tcon->ses->server);
592
593         if (!tcon->unix_ext)
594                 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
595                                            cifs_sb->ctx->file_mode,
596                                            cifs_sb->ctx->dir_mode);
597         if (cifs_sb->ctx->iocharset)
598                 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
599         if (tcon->seal)
600                 seq_puts(s, ",seal");
601         else if (tcon->ses->server->ignore_signature)
602                 seq_puts(s, ",signloosely");
603         if (tcon->nocase)
604                 seq_puts(s, ",nocase");
605         if (tcon->nodelete)
606                 seq_puts(s, ",nodelete");
607         if (cifs_sb->ctx->no_sparse)
608                 seq_puts(s, ",nosparse");
609         if (tcon->local_lease)
610                 seq_puts(s, ",locallease");
611         if (tcon->retry)
612                 seq_puts(s, ",hard");
613         else
614                 seq_puts(s, ",soft");
615         if (tcon->use_persistent)
616                 seq_puts(s, ",persistenthandles");
617         else if (tcon->use_resilient)
618                 seq_puts(s, ",resilienthandles");
619         if (tcon->posix_extensions)
620                 seq_puts(s, ",posix");
621         else if (tcon->unix_ext)
622                 seq_puts(s, ",unix");
623         else
624                 seq_puts(s, ",nounix");
625         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
626                 seq_puts(s, ",nodfs");
627         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
628                 seq_puts(s, ",posixpaths");
629         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
630                 seq_puts(s, ",setuids");
631         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
632                 seq_puts(s, ",idsfromsid");
633         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
634                 seq_puts(s, ",serverino");
635         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
636                 seq_puts(s, ",rwpidforward");
637         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
638                 seq_puts(s, ",forcemand");
639         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
640                 seq_puts(s, ",nouser_xattr");
641         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
642                 seq_puts(s, ",mapchars");
643         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
644                 seq_puts(s, ",mapposix");
645         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
646                 seq_puts(s, ",sfu");
647         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
648                 seq_puts(s, ",nobrl");
649         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
650                 seq_puts(s, ",nohandlecache");
651         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
652                 seq_puts(s, ",modefromsid");
653         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
654                 seq_puts(s, ",cifsacl");
655         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
656                 seq_puts(s, ",dynperm");
657         if (root->d_sb->s_flags & SB_POSIXACL)
658                 seq_puts(s, ",acl");
659         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
660                 seq_puts(s, ",mfsymlinks");
661         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
662                 seq_puts(s, ",fsc");
663         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
664                 seq_puts(s, ",nostrictsync");
665         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
666                 seq_puts(s, ",noperm");
667         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
668                 seq_printf(s, ",backupuid=%u",
669                            from_kuid_munged(&init_user_ns,
670                                             cifs_sb->ctx->backupuid));
671         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
672                 seq_printf(s, ",backupgid=%u",
673                            from_kgid_munged(&init_user_ns,
674                                             cifs_sb->ctx->backupgid));
675
676         seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
677         seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
678         seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
679         if (cifs_sb->ctx->rasize)
680                 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
681         if (tcon->ses->server->min_offload)
682                 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
683         seq_printf(s, ",echo_interval=%lu",
684                         tcon->ses->server->echo_interval / HZ);
685
686         /* Only display the following if overridden on mount */
687         if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
688                 seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
689         if (tcon->ses->server->tcp_nodelay)
690                 seq_puts(s, ",tcpnodelay");
691         if (tcon->ses->server->noautotune)
692                 seq_puts(s, ",noautotune");
693         if (tcon->ses->server->noblocksnd)
694                 seq_puts(s, ",noblocksend");
695         if (tcon->ses->server->nosharesock)
696                 seq_puts(s, ",nosharesock");
697
698         if (tcon->snapshot_time)
699                 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
700         if (tcon->handle_timeout)
701                 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
702         if (tcon->max_cached_dirs != MAX_CACHED_FIDS)
703                 seq_printf(s, ",max_cached_dirs=%u", tcon->max_cached_dirs);
704
705         /*
706          * Display file and directory attribute timeout in seconds.
707          * If file and directory attribute timeout the same then actimeo
708          * was likely specified on mount
709          */
710         if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
711                 seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
712         else {
713                 seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
714                 seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
715         }
716         seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ);
717
718         if (tcon->ses->chan_max > 1)
719                 seq_printf(s, ",multichannel,max_channels=%zu",
720                            tcon->ses->chan_max);
721
722         if (tcon->use_witness)
723                 seq_puts(s, ",witness");
724
725         return 0;
726 }
727
728 static void cifs_umount_begin(struct super_block *sb)
729 {
730         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
731         struct cifs_tcon *tcon;
732
733         if (cifs_sb == NULL)
734                 return;
735
736         tcon = cifs_sb_master_tcon(cifs_sb);
737
738         spin_lock(&cifs_tcp_ses_lock);
739         spin_lock(&tcon->tc_lock);
740         if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
741                 /* we have other mounts to same share or we have
742                    already tried to umount this and woken up
743                    all waiting network requests, nothing to do */
744                 spin_unlock(&tcon->tc_lock);
745                 spin_unlock(&cifs_tcp_ses_lock);
746                 return;
747         }
748         /*
749          * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will
750          * fail later (e.g. due to open files).  TID_EXITING will be set just before tdis req sent
751          */
752         spin_unlock(&tcon->tc_lock);
753         spin_unlock(&cifs_tcp_ses_lock);
754
755         cifs_close_all_deferred_files(tcon);
756         /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
757         /* cancel_notify_requests(tcon); */
758         if (tcon->ses && tcon->ses->server) {
759                 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
760                 wake_up_all(&tcon->ses->server->request_q);
761                 wake_up_all(&tcon->ses->server->response_q);
762                 msleep(1); /* yield */
763                 /* we have to kick the requests once more */
764                 wake_up_all(&tcon->ses->server->response_q);
765                 msleep(1);
766         }
767
768         return;
769 }
770
771 static int cifs_freeze(struct super_block *sb)
772 {
773         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
774         struct cifs_tcon *tcon;
775
776         if (cifs_sb == NULL)
777                 return 0;
778
779         tcon = cifs_sb_master_tcon(cifs_sb);
780
781         cifs_close_all_deferred_files(tcon);
782         return 0;
783 }
784
785 #ifdef CONFIG_CIFS_STATS2
786 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
787 {
788         /* BB FIXME */
789         return 0;
790 }
791 #endif
792
793 static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
794 {
795         fscache_unpin_writeback(wbc, cifs_inode_cookie(inode));
796         return 0;
797 }
798
799 static int cifs_drop_inode(struct inode *inode)
800 {
801         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
802
803         /* no serverino => unconditional eviction */
804         return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
805                 generic_drop_inode(inode);
806 }
807
808 static const struct super_operations cifs_super_ops = {
809         .statfs = cifs_statfs,
810         .alloc_inode = cifs_alloc_inode,
811         .write_inode    = cifs_write_inode,
812         .free_inode = cifs_free_inode,
813         .drop_inode     = cifs_drop_inode,
814         .evict_inode    = cifs_evict_inode,
815 /*      .show_path      = cifs_show_path, */ /* Would we ever need show path? */
816         .show_devname   = cifs_show_devname,
817 /*      .delete_inode   = cifs_delete_inode,  */  /* Do not need above
818         function unless later we add lazy close of inodes or unless the
819         kernel forgets to call us with the same number of releases (closes)
820         as opens */
821         .show_options = cifs_show_options,
822         .umount_begin   = cifs_umount_begin,
823         .freeze_fs      = cifs_freeze,
824 #ifdef CONFIG_CIFS_STATS2
825         .show_stats = cifs_show_stats,
826 #endif
827 };
828
829 /*
830  * Get root dentry from superblock according to prefix path mount option.
831  * Return dentry with refcount + 1 on success and NULL otherwise.
832  */
833 static struct dentry *
834 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
835 {
836         struct dentry *dentry;
837         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
838         char *full_path = NULL;
839         char *s, *p;
840         char sep;
841
842         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
843                 return dget(sb->s_root);
844
845         full_path = cifs_build_path_to_root(ctx, cifs_sb,
846                                 cifs_sb_master_tcon(cifs_sb), 0);
847         if (full_path == NULL)
848                 return ERR_PTR(-ENOMEM);
849
850         cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
851
852         sep = CIFS_DIR_SEP(cifs_sb);
853         dentry = dget(sb->s_root);
854         s = full_path;
855
856         do {
857                 struct inode *dir = d_inode(dentry);
858                 struct dentry *child;
859
860                 if (!S_ISDIR(dir->i_mode)) {
861                         dput(dentry);
862                         dentry = ERR_PTR(-ENOTDIR);
863                         break;
864                 }
865
866                 /* skip separators */
867                 while (*s == sep)
868                         s++;
869                 if (!*s)
870                         break;
871                 p = s++;
872                 /* next separator */
873                 while (*s && *s != sep)
874                         s++;
875
876                 child = lookup_positive_unlocked(p, dentry, s - p);
877                 dput(dentry);
878                 dentry = child;
879         } while (!IS_ERR(dentry));
880         kfree(full_path);
881         return dentry;
882 }
883
884 static int cifs_set_super(struct super_block *sb, void *data)
885 {
886         struct cifs_mnt_data *mnt_data = data;
887         sb->s_fs_info = mnt_data->cifs_sb;
888         return set_anon_super(sb, NULL);
889 }
890
891 struct dentry *
892 cifs_smb3_do_mount(struct file_system_type *fs_type,
893               int flags, struct smb3_fs_context *old_ctx)
894 {
895         struct cifs_mnt_data mnt_data;
896         struct cifs_sb_info *cifs_sb;
897         struct super_block *sb;
898         struct dentry *root;
899         int rc;
900
901         if (cifsFYI) {
902                 cifs_dbg(FYI, "%s: devname=%s flags=0x%x\n", __func__,
903                          old_ctx->source, flags);
904         } else {
905                 cifs_info("Attempting to mount %s\n", old_ctx->source);
906         }
907
908         cifs_sb = kzalloc(sizeof(*cifs_sb), GFP_KERNEL);
909         if (!cifs_sb)
910                 return ERR_PTR(-ENOMEM);
911
912         cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
913         if (!cifs_sb->ctx) {
914                 root = ERR_PTR(-ENOMEM);
915                 goto out;
916         }
917         rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
918         if (rc) {
919                 root = ERR_PTR(rc);
920                 goto out;
921         }
922
923         rc = cifs_setup_cifs_sb(cifs_sb);
924         if (rc) {
925                 root = ERR_PTR(rc);
926                 goto out;
927         }
928
929         rc = cifs_mount(cifs_sb, cifs_sb->ctx);
930         if (rc) {
931                 if (!(flags & SB_SILENT))
932                         cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
933                                  rc);
934                 root = ERR_PTR(rc);
935                 goto out;
936         }
937
938         mnt_data.ctx = cifs_sb->ctx;
939         mnt_data.cifs_sb = cifs_sb;
940         mnt_data.flags = flags;
941
942         /* BB should we make this contingent on mount parm? */
943         flags |= SB_NODIRATIME | SB_NOATIME;
944
945         sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
946         if (IS_ERR(sb)) {
947                 cifs_umount(cifs_sb);
948                 return ERR_CAST(sb);
949         }
950
951         if (sb->s_root) {
952                 cifs_dbg(FYI, "Use existing superblock\n");
953                 cifs_umount(cifs_sb);
954                 cifs_sb = NULL;
955         } else {
956                 rc = cifs_read_super(sb);
957                 if (rc) {
958                         root = ERR_PTR(rc);
959                         goto out_super;
960                 }
961
962                 sb->s_flags |= SB_ACTIVE;
963         }
964
965         root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
966         if (IS_ERR(root))
967                 goto out_super;
968
969         if (cifs_sb)
970                 cifs_sb->root = dget(root);
971
972         cifs_dbg(FYI, "dentry root is: %p\n", root);
973         return root;
974
975 out_super:
976         deactivate_locked_super(sb);
977         return root;
978 out:
979         kfree(cifs_sb->prepath);
980         smb3_cleanup_fs_context(cifs_sb->ctx);
981         kfree(cifs_sb);
982         return root;
983 }
984
985
986 static ssize_t
987 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
988 {
989         ssize_t rc;
990         struct inode *inode = file_inode(iocb->ki_filp);
991
992         if (iocb->ki_flags & IOCB_DIRECT)
993                 return cifs_user_readv(iocb, iter);
994
995         rc = cifs_revalidate_mapping(inode);
996         if (rc)
997                 return rc;
998
999         return generic_file_read_iter(iocb, iter);
1000 }
1001
1002 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1003 {
1004         struct inode *inode = file_inode(iocb->ki_filp);
1005         struct cifsInodeInfo *cinode = CIFS_I(inode);
1006         ssize_t written;
1007         int rc;
1008
1009         if (iocb->ki_filp->f_flags & O_DIRECT) {
1010                 written = cifs_user_writev(iocb, from);
1011                 if (written > 0 && CIFS_CACHE_READ(cinode)) {
1012                         cifs_zap_mapping(inode);
1013                         cifs_dbg(FYI,
1014                                  "Set no oplock for inode=%p after a write operation\n",
1015                                  inode);
1016                         cinode->oplock = 0;
1017                 }
1018                 return written;
1019         }
1020
1021         written = cifs_get_writer(cinode);
1022         if (written)
1023                 return written;
1024
1025         written = generic_file_write_iter(iocb, from);
1026
1027         if (CIFS_CACHE_WRITE(CIFS_I(inode)))
1028                 goto out;
1029
1030         rc = filemap_fdatawrite(inode->i_mapping);
1031         if (rc)
1032                 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
1033                          rc, inode);
1034
1035 out:
1036         cifs_put_writer(cinode);
1037         return written;
1038 }
1039
1040 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
1041 {
1042         struct cifsFileInfo *cfile = file->private_data;
1043         struct cifs_tcon *tcon;
1044
1045         /*
1046          * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
1047          * the cached file length
1048          */
1049         if (whence != SEEK_SET && whence != SEEK_CUR) {
1050                 int rc;
1051                 struct inode *inode = file_inode(file);
1052
1053                 /*
1054                  * We need to be sure that all dirty pages are written and the
1055                  * server has the newest file length.
1056                  */
1057                 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
1058                     inode->i_mapping->nrpages != 0) {
1059                         rc = filemap_fdatawait(inode->i_mapping);
1060                         if (rc) {
1061                                 mapping_set_error(inode->i_mapping, rc);
1062                                 return rc;
1063                         }
1064                 }
1065                 /*
1066                  * Some applications poll for the file length in this strange
1067                  * way so we must seek to end on non-oplocked files by
1068                  * setting the revalidate time to zero.
1069                  */
1070                 CIFS_I(inode)->time = 0;
1071
1072                 rc = cifs_revalidate_file_attr(file);
1073                 if (rc < 0)
1074                         return (loff_t)rc;
1075         }
1076         if (cfile && cfile->tlink) {
1077                 tcon = tlink_tcon(cfile->tlink);
1078                 if (tcon->ses->server->ops->llseek)
1079                         return tcon->ses->server->ops->llseek(file, tcon,
1080                                                               offset, whence);
1081         }
1082         return generic_file_llseek(file, offset, whence);
1083 }
1084
1085 static int
1086 cifs_setlease(struct file *file, int arg, struct file_lock **lease, void **priv)
1087 {
1088         /*
1089          * Note that this is called by vfs setlease with i_lock held to
1090          * protect *lease from going away.
1091          */
1092         struct inode *inode = file_inode(file);
1093         struct cifsFileInfo *cfile = file->private_data;
1094
1095         if (!(S_ISREG(inode->i_mode)))
1096                 return -EINVAL;
1097
1098         /* Check if file is oplocked if this is request for new lease */
1099         if (arg == F_UNLCK ||
1100             ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1101             ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1102                 return generic_setlease(file, arg, lease, priv);
1103         else if (tlink_tcon(cfile->tlink)->local_lease &&
1104                  !CIFS_CACHE_READ(CIFS_I(inode)))
1105                 /*
1106                  * If the server claims to support oplock on this file, then we
1107                  * still need to check oplock even if the local_lease mount
1108                  * option is set, but there are servers which do not support
1109                  * oplock for which this mount option may be useful if the user
1110                  * knows that the file won't be changed on the server by anyone
1111                  * else.
1112                  */
1113                 return generic_setlease(file, arg, lease, priv);
1114         else
1115                 return -EAGAIN;
1116 }
1117
1118 struct file_system_type cifs_fs_type = {
1119         .owner = THIS_MODULE,
1120         .name = "cifs",
1121         .init_fs_context = smb3_init_fs_context,
1122         .parameters = smb3_fs_parameters,
1123         .kill_sb = cifs_kill_sb,
1124         .fs_flags = FS_RENAME_DOES_D_MOVE,
1125 };
1126 MODULE_ALIAS_FS("cifs");
1127
1128 struct file_system_type smb3_fs_type = {
1129         .owner = THIS_MODULE,
1130         .name = "smb3",
1131         .init_fs_context = smb3_init_fs_context,
1132         .parameters = smb3_fs_parameters,
1133         .kill_sb = cifs_kill_sb,
1134         .fs_flags = FS_RENAME_DOES_D_MOVE,
1135 };
1136 MODULE_ALIAS_FS("smb3");
1137 MODULE_ALIAS("smb3");
1138
1139 const struct inode_operations cifs_dir_inode_ops = {
1140         .create = cifs_create,
1141         .atomic_open = cifs_atomic_open,
1142         .lookup = cifs_lookup,
1143         .getattr = cifs_getattr,
1144         .unlink = cifs_unlink,
1145         .link = cifs_hardlink,
1146         .mkdir = cifs_mkdir,
1147         .rmdir = cifs_rmdir,
1148         .rename = cifs_rename2,
1149         .permission = cifs_permission,
1150         .setattr = cifs_setattr,
1151         .symlink = cifs_symlink,
1152         .mknod   = cifs_mknod,
1153         .listxattr = cifs_listxattr,
1154         .get_acl = cifs_get_acl,
1155         .set_acl = cifs_set_acl,
1156 };
1157
1158 const struct inode_operations cifs_file_inode_ops = {
1159         .setattr = cifs_setattr,
1160         .getattr = cifs_getattr,
1161         .permission = cifs_permission,
1162         .listxattr = cifs_listxattr,
1163         .fiemap = cifs_fiemap,
1164         .get_acl = cifs_get_acl,
1165         .set_acl = cifs_set_acl,
1166 };
1167
1168 const char *cifs_get_link(struct dentry *dentry, struct inode *inode,
1169                             struct delayed_call *done)
1170 {
1171         char *target_path;
1172
1173         target_path = kmalloc(PATH_MAX, GFP_KERNEL);
1174         if (!target_path)
1175                 return ERR_PTR(-ENOMEM);
1176
1177         spin_lock(&inode->i_lock);
1178         if (likely(CIFS_I(inode)->symlink_target)) {
1179                 strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX);
1180         } else {
1181                 kfree(target_path);
1182                 target_path = ERR_PTR(-EOPNOTSUPP);
1183         }
1184         spin_unlock(&inode->i_lock);
1185
1186         if (!IS_ERR(target_path))
1187                 set_delayed_call(done, kfree_link, target_path);
1188
1189         return target_path;
1190 }
1191
1192 const struct inode_operations cifs_symlink_inode_ops = {
1193         .get_link = cifs_get_link,
1194         .setattr = cifs_setattr,
1195         .permission = cifs_permission,
1196         .listxattr = cifs_listxattr,
1197 };
1198
1199 /*
1200  * Advance the EOF marker to after the source range.
1201  */
1202 static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi,
1203                                 struct cifs_tcon *src_tcon,
1204                                 unsigned int xid, loff_t src_end)
1205 {
1206         struct cifsFileInfo *writeable_srcfile;
1207         int rc = -EINVAL;
1208
1209         writeable_srcfile = find_writable_file(src_cifsi, FIND_WR_FSUID_ONLY);
1210         if (writeable_srcfile) {
1211                 if (src_tcon->ses->server->ops->set_file_size)
1212                         rc = src_tcon->ses->server->ops->set_file_size(
1213                                 xid, src_tcon, writeable_srcfile,
1214                                 src_inode->i_size, true /* no need to set sparse */);
1215                 else
1216                         rc = -ENOSYS;
1217                 cifsFileInfo_put(writeable_srcfile);
1218                 cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc);
1219         }
1220
1221         if (rc < 0)
1222                 goto set_failed;
1223
1224         netfs_resize_file(&src_cifsi->netfs, src_end);
1225         fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
1226         return 0;
1227
1228 set_failed:
1229         return filemap_write_and_wait(src_inode->i_mapping);
1230 }
1231
1232 /*
1233  * Flush out either the folio that overlaps the beginning of a range in which
1234  * pos resides or the folio that overlaps the end of a range unless that folio
1235  * is entirely within the range we're going to invalidate.  We extend the flush
1236  * bounds to encompass the folio.
1237  */
1238 static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend,
1239                             bool first)
1240 {
1241         struct folio *folio;
1242         unsigned long long fpos, fend;
1243         pgoff_t index = pos / PAGE_SIZE;
1244         size_t size;
1245         int rc = 0;
1246
1247         folio = filemap_get_folio(inode->i_mapping, index);
1248         if (IS_ERR(folio))
1249                 return 0;
1250
1251         size = folio_size(folio);
1252         fpos = folio_pos(folio);
1253         fend = fpos + size - 1;
1254         *_fstart = min_t(unsigned long long, *_fstart, fpos);
1255         *_fend   = max_t(unsigned long long, *_fend, fend);
1256         if ((first && pos == fpos) || (!first && pos == fend))
1257                 goto out;
1258
1259         rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend);
1260 out:
1261         folio_put(folio);
1262         return rc;
1263 }
1264
1265 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1266                 struct file *dst_file, loff_t destoff, loff_t len,
1267                 unsigned int remap_flags)
1268 {
1269         struct inode *src_inode = file_inode(src_file);
1270         struct inode *target_inode = file_inode(dst_file);
1271         struct cifsFileInfo *smb_file_src = src_file->private_data;
1272         struct cifsFileInfo *smb_file_target;
1273         struct cifs_tcon *target_tcon;
1274         unsigned int xid;
1275         int rc;
1276
1277         if (remap_flags & REMAP_FILE_DEDUP)
1278                 return -EOPNOTSUPP;
1279         if (remap_flags & ~REMAP_FILE_ADVISORY)
1280                 return -EINVAL;
1281
1282         cifs_dbg(FYI, "clone range\n");
1283
1284         xid = get_xid();
1285
1286         if (!src_file->private_data || !dst_file->private_data) {
1287                 rc = -EBADF;
1288                 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1289                 goto out;
1290         }
1291
1292         smb_file_target = dst_file->private_data;
1293         target_tcon = tlink_tcon(smb_file_target->tlink);
1294
1295         /*
1296          * Note: cifs case is easier than btrfs since server responsible for
1297          * checks for proper open modes and file type and if it wants
1298          * server could even support copy of range where source = target
1299          */
1300         lock_two_nondirectories(target_inode, src_inode);
1301
1302         if (len == 0)
1303                 len = src_inode->i_size - off;
1304
1305         cifs_dbg(FYI, "about to flush pages\n");
1306         /* should we flush first and last page first */
1307         truncate_inode_pages_range(&target_inode->i_data, destoff,
1308                                    PAGE_ALIGN(destoff + len)-1);
1309
1310         if (target_tcon->ses->server->ops->duplicate_extents)
1311                 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1312                         smb_file_src, smb_file_target, off, len, destoff);
1313         else
1314                 rc = -EOPNOTSUPP;
1315
1316         /* force revalidate of size and timestamps of target file now
1317            that target is updated on the server */
1318         CIFS_I(target_inode)->time = 0;
1319         /* although unlocking in the reverse order from locking is not
1320            strictly necessary here it is a little cleaner to be consistent */
1321         unlock_two_nondirectories(src_inode, target_inode);
1322 out:
1323         free_xid(xid);
1324         return rc < 0 ? rc : len;
1325 }
1326
1327 ssize_t cifs_file_copychunk_range(unsigned int xid,
1328                                 struct file *src_file, loff_t off,
1329                                 struct file *dst_file, loff_t destoff,
1330                                 size_t len, unsigned int flags)
1331 {
1332         struct inode *src_inode = file_inode(src_file);
1333         struct inode *target_inode = file_inode(dst_file);
1334         struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
1335         struct cifsFileInfo *smb_file_src;
1336         struct cifsFileInfo *smb_file_target;
1337         struct cifs_tcon *src_tcon;
1338         struct cifs_tcon *target_tcon;
1339         unsigned long long destend, fstart, fend;
1340         ssize_t rc;
1341
1342         cifs_dbg(FYI, "copychunk range\n");
1343
1344         if (!src_file->private_data || !dst_file->private_data) {
1345                 rc = -EBADF;
1346                 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1347                 goto out;
1348         }
1349
1350         rc = -EXDEV;
1351         smb_file_target = dst_file->private_data;
1352         smb_file_src = src_file->private_data;
1353         src_tcon = tlink_tcon(smb_file_src->tlink);
1354         target_tcon = tlink_tcon(smb_file_target->tlink);
1355
1356         if (src_tcon->ses != target_tcon->ses) {
1357                 cifs_dbg(VFS, "source and target of copy not on same server\n");
1358                 goto out;
1359         }
1360
1361         rc = -EOPNOTSUPP;
1362         if (!target_tcon->ses->server->ops->copychunk_range)
1363                 goto out;
1364
1365         /*
1366          * Note: cifs case is easier than btrfs since server responsible for
1367          * checks for proper open modes and file type and if it wants
1368          * server could even support copy of range where source = target
1369          */
1370         lock_two_nondirectories(target_inode, src_inode);
1371
1372         cifs_dbg(FYI, "about to flush pages\n");
1373
1374         rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
1375                                           off + len - 1);
1376         if (rc)
1377                 goto unlock;
1378
1379         /* The server-side copy will fail if the source crosses the EOF marker.
1380          * Advance the EOF marker after the flush above to the end of the range
1381          * if it's short of that.
1382          */
1383         if (src_cifsi->server_eof < off + len) {
1384                 rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
1385                 if (rc < 0)
1386                         goto unlock;
1387         }
1388
1389         destend = destoff + len - 1;
1390
1391         /* Flush the folios at either end of the destination range to prevent
1392          * accidental loss of dirty data outside of the range.
1393          */
1394         fstart = destoff;
1395         fend = destend;
1396
1397         rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
1398         if (rc)
1399                 goto unlock;
1400         rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
1401         if (rc)
1402                 goto unlock;
1403
1404         /* Discard all the folios that overlap the destination region. */
1405         truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
1406
1407         rc = file_modified(dst_file);
1408         if (!rc) {
1409                 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1410                         smb_file_src, smb_file_target, off, len, destoff);
1411                 if (rc > 0 && destoff + rc > i_size_read(target_inode))
1412                         truncate_setsize(target_inode, destoff + rc);
1413         }
1414
1415         file_accessed(src_file);
1416
1417         /* force revalidate of size and timestamps of target file now
1418          * that target is updated on the server
1419          */
1420         CIFS_I(target_inode)->time = 0;
1421
1422 unlock:
1423         /* although unlocking in the reverse order from locking is not
1424          * strictly necessary here it is a little cleaner to be consistent
1425          */
1426         unlock_two_nondirectories(src_inode, target_inode);
1427
1428 out:
1429         return rc;
1430 }
1431
1432 /*
1433  * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1434  * is a dummy operation.
1435  */
1436 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1437 {
1438         cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1439                  file, datasync);
1440
1441         return 0;
1442 }
1443
1444 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1445                                 struct file *dst_file, loff_t destoff,
1446                                 size_t len, unsigned int flags)
1447 {
1448         unsigned int xid = get_xid();
1449         ssize_t rc;
1450         struct cifsFileInfo *cfile = dst_file->private_data;
1451
1452         if (cfile->swapfile) {
1453                 rc = -EOPNOTSUPP;
1454                 free_xid(xid);
1455                 return rc;
1456         }
1457
1458         rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1459                                         len, flags);
1460         free_xid(xid);
1461
1462         if (rc == -EOPNOTSUPP || rc == -EXDEV)
1463                 rc = generic_copy_file_range(src_file, off, dst_file,
1464                                              destoff, len, flags);
1465         return rc;
1466 }
1467
1468 const struct file_operations cifs_file_ops = {
1469         .read_iter = cifs_loose_read_iter,
1470         .write_iter = cifs_file_write_iter,
1471         .open = cifs_open,
1472         .release = cifs_close,
1473         .lock = cifs_lock,
1474         .flock = cifs_flock,
1475         .fsync = cifs_fsync,
1476         .flush = cifs_flush,
1477         .mmap  = cifs_file_mmap,
1478         .splice_read = filemap_splice_read,
1479         .splice_write = iter_file_splice_write,
1480         .llseek = cifs_llseek,
1481         .unlocked_ioctl = cifs_ioctl,
1482         .copy_file_range = cifs_copy_file_range,
1483         .remap_file_range = cifs_remap_file_range,
1484         .setlease = cifs_setlease,
1485         .fallocate = cifs_fallocate,
1486 };
1487
1488 const struct file_operations cifs_file_strict_ops = {
1489         .read_iter = cifs_strict_readv,
1490         .write_iter = cifs_strict_writev,
1491         .open = cifs_open,
1492         .release = cifs_close,
1493         .lock = cifs_lock,
1494         .flock = cifs_flock,
1495         .fsync = cifs_strict_fsync,
1496         .flush = cifs_flush,
1497         .mmap = cifs_file_strict_mmap,
1498         .splice_read = filemap_splice_read,
1499         .splice_write = iter_file_splice_write,
1500         .llseek = cifs_llseek,
1501         .unlocked_ioctl = cifs_ioctl,
1502         .copy_file_range = cifs_copy_file_range,
1503         .remap_file_range = cifs_remap_file_range,
1504         .setlease = cifs_setlease,
1505         .fallocate = cifs_fallocate,
1506 };
1507
1508 const struct file_operations cifs_file_direct_ops = {
1509         .read_iter = cifs_direct_readv,
1510         .write_iter = cifs_direct_writev,
1511         .open = cifs_open,
1512         .release = cifs_close,
1513         .lock = cifs_lock,
1514         .flock = cifs_flock,
1515         .fsync = cifs_fsync,
1516         .flush = cifs_flush,
1517         .mmap = cifs_file_mmap,
1518         .splice_read = copy_splice_read,
1519         .splice_write = iter_file_splice_write,
1520         .unlocked_ioctl  = cifs_ioctl,
1521         .copy_file_range = cifs_copy_file_range,
1522         .remap_file_range = cifs_remap_file_range,
1523         .llseek = cifs_llseek,
1524         .setlease = cifs_setlease,
1525         .fallocate = cifs_fallocate,
1526 };
1527
1528 const struct file_operations cifs_file_nobrl_ops = {
1529         .read_iter = cifs_loose_read_iter,
1530         .write_iter = cifs_file_write_iter,
1531         .open = cifs_open,
1532         .release = cifs_close,
1533         .fsync = cifs_fsync,
1534         .flush = cifs_flush,
1535         .mmap  = cifs_file_mmap,
1536         .splice_read = filemap_splice_read,
1537         .splice_write = iter_file_splice_write,
1538         .llseek = cifs_llseek,
1539         .unlocked_ioctl = cifs_ioctl,
1540         .copy_file_range = cifs_copy_file_range,
1541         .remap_file_range = cifs_remap_file_range,
1542         .setlease = cifs_setlease,
1543         .fallocate = cifs_fallocate,
1544 };
1545
1546 const struct file_operations cifs_file_strict_nobrl_ops = {
1547         .read_iter = cifs_strict_readv,
1548         .write_iter = cifs_strict_writev,
1549         .open = cifs_open,
1550         .release = cifs_close,
1551         .fsync = cifs_strict_fsync,
1552         .flush = cifs_flush,
1553         .mmap = cifs_file_strict_mmap,
1554         .splice_read = filemap_splice_read,
1555         .splice_write = iter_file_splice_write,
1556         .llseek = cifs_llseek,
1557         .unlocked_ioctl = cifs_ioctl,
1558         .copy_file_range = cifs_copy_file_range,
1559         .remap_file_range = cifs_remap_file_range,
1560         .setlease = cifs_setlease,
1561         .fallocate = cifs_fallocate,
1562 };
1563
1564 const struct file_operations cifs_file_direct_nobrl_ops = {
1565         .read_iter = cifs_direct_readv,
1566         .write_iter = cifs_direct_writev,
1567         .open = cifs_open,
1568         .release = cifs_close,
1569         .fsync = cifs_fsync,
1570         .flush = cifs_flush,
1571         .mmap = cifs_file_mmap,
1572         .splice_read = copy_splice_read,
1573         .splice_write = iter_file_splice_write,
1574         .unlocked_ioctl  = cifs_ioctl,
1575         .copy_file_range = cifs_copy_file_range,
1576         .remap_file_range = cifs_remap_file_range,
1577         .llseek = cifs_llseek,
1578         .setlease = cifs_setlease,
1579         .fallocate = cifs_fallocate,
1580 };
1581
1582 const struct file_operations cifs_dir_ops = {
1583         .iterate_shared = cifs_readdir,
1584         .release = cifs_closedir,
1585         .read    = generic_read_dir,
1586         .unlocked_ioctl  = cifs_ioctl,
1587         .copy_file_range = cifs_copy_file_range,
1588         .remap_file_range = cifs_remap_file_range,
1589         .llseek = generic_file_llseek,
1590         .fsync = cifs_dir_fsync,
1591 };
1592
1593 static void
1594 cifs_init_once(void *inode)
1595 {
1596         struct cifsInodeInfo *cifsi = inode;
1597
1598         inode_init_once(&cifsi->netfs.inode);
1599         init_rwsem(&cifsi->lock_sem);
1600 }
1601
1602 static int __init
1603 cifs_init_inodecache(void)
1604 {
1605         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1606                                               sizeof(struct cifsInodeInfo),
1607                                               0, (SLAB_RECLAIM_ACCOUNT|
1608                                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1609                                               cifs_init_once);
1610         if (cifs_inode_cachep == NULL)
1611                 return -ENOMEM;
1612
1613         return 0;
1614 }
1615
1616 static void
1617 cifs_destroy_inodecache(void)
1618 {
1619         /*
1620          * Make sure all delayed rcu free inodes are flushed before we
1621          * destroy cache.
1622          */
1623         rcu_barrier();
1624         kmem_cache_destroy(cifs_inode_cachep);
1625 }
1626
1627 static int
1628 cifs_init_request_bufs(void)
1629 {
1630         /*
1631          * SMB2 maximum header size is bigger than CIFS one - no problems to
1632          * allocate some more bytes for CIFS.
1633          */
1634         size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1635
1636         if (CIFSMaxBufSize < 8192) {
1637         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1638         Unicode path name has to fit in any SMB/CIFS path based frames */
1639                 CIFSMaxBufSize = 8192;
1640         } else if (CIFSMaxBufSize > 1024*127) {
1641                 CIFSMaxBufSize = 1024 * 127;
1642         } else {
1643                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1644         }
1645 /*
1646         cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1647                  CIFSMaxBufSize, CIFSMaxBufSize);
1648 */
1649         cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1650                                             CIFSMaxBufSize + max_hdr_size, 0,
1651                                             SLAB_HWCACHE_ALIGN, 0,
1652                                             CIFSMaxBufSize + max_hdr_size,
1653                                             NULL);
1654         if (cifs_req_cachep == NULL)
1655                 return -ENOMEM;
1656
1657         if (cifs_min_rcv < 1)
1658                 cifs_min_rcv = 1;
1659         else if (cifs_min_rcv > 64) {
1660                 cifs_min_rcv = 64;
1661                 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1662         }
1663
1664         cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1665                                                   cifs_req_cachep);
1666
1667         if (cifs_req_poolp == NULL) {
1668                 kmem_cache_destroy(cifs_req_cachep);
1669                 return -ENOMEM;
1670         }
1671         /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1672         almost all handle based requests (but not write response, nor is it
1673         sufficient for path based requests).  A smaller size would have
1674         been more efficient (compacting multiple slab items on one 4k page)
1675         for the case in which debug was on, but this larger size allows
1676         more SMBs to use small buffer alloc and is still much more
1677         efficient to alloc 1 per page off the slab compared to 17K (5page)
1678         alloc of large cifs buffers even when page debugging is on */
1679         cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1680                         MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1681                         0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1682         if (cifs_sm_req_cachep == NULL) {
1683                 mempool_destroy(cifs_req_poolp);
1684                 kmem_cache_destroy(cifs_req_cachep);
1685                 return -ENOMEM;
1686         }
1687
1688         if (cifs_min_small < 2)
1689                 cifs_min_small = 2;
1690         else if (cifs_min_small > 256) {
1691                 cifs_min_small = 256;
1692                 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1693         }
1694
1695         cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1696                                                      cifs_sm_req_cachep);
1697
1698         if (cifs_sm_req_poolp == NULL) {
1699                 mempool_destroy(cifs_req_poolp);
1700                 kmem_cache_destroy(cifs_req_cachep);
1701                 kmem_cache_destroy(cifs_sm_req_cachep);
1702                 return -ENOMEM;
1703         }
1704
1705         return 0;
1706 }
1707
1708 static void
1709 cifs_destroy_request_bufs(void)
1710 {
1711         mempool_destroy(cifs_req_poolp);
1712         kmem_cache_destroy(cifs_req_cachep);
1713         mempool_destroy(cifs_sm_req_poolp);
1714         kmem_cache_destroy(cifs_sm_req_cachep);
1715 }
1716
1717 static int init_mids(void)
1718 {
1719         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1720                                             sizeof(struct mid_q_entry), 0,
1721                                             SLAB_HWCACHE_ALIGN, NULL);
1722         if (cifs_mid_cachep == NULL)
1723                 return -ENOMEM;
1724
1725         /* 3 is a reasonable minimum number of simultaneous operations */
1726         cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1727         if (cifs_mid_poolp == NULL) {
1728                 kmem_cache_destroy(cifs_mid_cachep);
1729                 return -ENOMEM;
1730         }
1731
1732         return 0;
1733 }
1734
1735 static void destroy_mids(void)
1736 {
1737         mempool_destroy(cifs_mid_poolp);
1738         kmem_cache_destroy(cifs_mid_cachep);
1739 }
1740
1741 static int __init
1742 init_cifs(void)
1743 {
1744         int rc = 0;
1745         cifs_proc_init();
1746         INIT_LIST_HEAD(&cifs_tcp_ses_list);
1747 /*
1748  *  Initialize Global counters
1749  */
1750         atomic_set(&sesInfoAllocCount, 0);
1751         atomic_set(&tconInfoAllocCount, 0);
1752         atomic_set(&tcpSesNextId, 0);
1753         atomic_set(&tcpSesAllocCount, 0);
1754         atomic_set(&tcpSesReconnectCount, 0);
1755         atomic_set(&tconInfoReconnectCount, 0);
1756
1757         atomic_set(&buf_alloc_count, 0);
1758         atomic_set(&small_buf_alloc_count, 0);
1759 #ifdef CONFIG_CIFS_STATS2
1760         atomic_set(&total_buf_alloc_count, 0);
1761         atomic_set(&total_small_buf_alloc_count, 0);
1762         if (slow_rsp_threshold < 1)
1763                 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1764         else if (slow_rsp_threshold > 32767)
1765                 cifs_dbg(VFS,
1766                        "slow response threshold set higher than recommended (0 to 32767)\n");
1767 #endif /* CONFIG_CIFS_STATS2 */
1768
1769         atomic_set(&mid_count, 0);
1770         GlobalCurrentXid = 0;
1771         GlobalTotalActiveXid = 0;
1772         GlobalMaxActiveXid = 0;
1773         spin_lock_init(&cifs_tcp_ses_lock);
1774         spin_lock_init(&GlobalMid_Lock);
1775
1776         cifs_lock_secret = get_random_u32();
1777
1778         if (cifs_max_pending < 2) {
1779                 cifs_max_pending = 2;
1780                 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1781         } else if (cifs_max_pending > CIFS_MAX_REQ) {
1782                 cifs_max_pending = CIFS_MAX_REQ;
1783                 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1784                          CIFS_MAX_REQ);
1785         }
1786
1787         /* Limit max to about 18 hours, and setting to zero disables directory entry caching */
1788         if (dir_cache_timeout > 65000) {
1789                 dir_cache_timeout = 65000;
1790                 cifs_dbg(VFS, "dir_cache_timeout set to max of 65000 seconds\n");
1791         }
1792
1793         cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1794         if (!cifsiod_wq) {
1795                 rc = -ENOMEM;
1796                 goto out_clean_proc;
1797         }
1798
1799         /*
1800          * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1801          * so that we don't launch too many worker threads but
1802          * Documentation/core-api/workqueue.rst recommends setting it to 0
1803          */
1804
1805         /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1806         decrypt_wq = alloc_workqueue("smb3decryptd",
1807                                      WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1808         if (!decrypt_wq) {
1809                 rc = -ENOMEM;
1810                 goto out_destroy_cifsiod_wq;
1811         }
1812
1813         fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1814                                      WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1815         if (!fileinfo_put_wq) {
1816                 rc = -ENOMEM;
1817                 goto out_destroy_decrypt_wq;
1818         }
1819
1820         cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1821                                          WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1822         if (!cifsoplockd_wq) {
1823                 rc = -ENOMEM;
1824                 goto out_destroy_fileinfo_put_wq;
1825         }
1826
1827         deferredclose_wq = alloc_workqueue("deferredclose",
1828                                            WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1829         if (!deferredclose_wq) {
1830                 rc = -ENOMEM;
1831                 goto out_destroy_cifsoplockd_wq;
1832         }
1833
1834         rc = cifs_init_inodecache();
1835         if (rc)
1836                 goto out_destroy_deferredclose_wq;
1837
1838         rc = init_mids();
1839         if (rc)
1840                 goto out_destroy_inodecache;
1841
1842         rc = cifs_init_request_bufs();
1843         if (rc)
1844                 goto out_destroy_mids;
1845
1846 #ifdef CONFIG_CIFS_DFS_UPCALL
1847         rc = dfs_cache_init();
1848         if (rc)
1849                 goto out_destroy_request_bufs;
1850 #endif /* CONFIG_CIFS_DFS_UPCALL */
1851 #ifdef CONFIG_CIFS_UPCALL
1852         rc = init_cifs_spnego();
1853         if (rc)
1854                 goto out_destroy_dfs_cache;
1855 #endif /* CONFIG_CIFS_UPCALL */
1856 #ifdef CONFIG_CIFS_SWN_UPCALL
1857         rc = cifs_genl_init();
1858         if (rc)
1859                 goto out_register_key_type;
1860 #endif /* CONFIG_CIFS_SWN_UPCALL */
1861
1862         rc = init_cifs_idmap();
1863         if (rc)
1864                 goto out_cifs_swn_init;
1865
1866         rc = register_filesystem(&cifs_fs_type);
1867         if (rc)
1868                 goto out_init_cifs_idmap;
1869
1870         rc = register_filesystem(&smb3_fs_type);
1871         if (rc) {
1872                 unregister_filesystem(&cifs_fs_type);
1873                 goto out_init_cifs_idmap;
1874         }
1875
1876         return 0;
1877
1878 out_init_cifs_idmap:
1879         exit_cifs_idmap();
1880 out_cifs_swn_init:
1881 #ifdef CONFIG_CIFS_SWN_UPCALL
1882         cifs_genl_exit();
1883 out_register_key_type:
1884 #endif
1885 #ifdef CONFIG_CIFS_UPCALL
1886         exit_cifs_spnego();
1887 out_destroy_dfs_cache:
1888 #endif
1889 #ifdef CONFIG_CIFS_DFS_UPCALL
1890         dfs_cache_destroy();
1891 out_destroy_request_bufs:
1892 #endif
1893         cifs_destroy_request_bufs();
1894 out_destroy_mids:
1895         destroy_mids();
1896 out_destroy_inodecache:
1897         cifs_destroy_inodecache();
1898 out_destroy_deferredclose_wq:
1899         destroy_workqueue(deferredclose_wq);
1900 out_destroy_cifsoplockd_wq:
1901         destroy_workqueue(cifsoplockd_wq);
1902 out_destroy_fileinfo_put_wq:
1903         destroy_workqueue(fileinfo_put_wq);
1904 out_destroy_decrypt_wq:
1905         destroy_workqueue(decrypt_wq);
1906 out_destroy_cifsiod_wq:
1907         destroy_workqueue(cifsiod_wq);
1908 out_clean_proc:
1909         cifs_proc_clean();
1910         return rc;
1911 }
1912
1913 static void __exit
1914 exit_cifs(void)
1915 {
1916         cifs_dbg(NOISY, "exit_smb3\n");
1917         unregister_filesystem(&cifs_fs_type);
1918         unregister_filesystem(&smb3_fs_type);
1919         cifs_release_automount_timer();
1920         exit_cifs_idmap();
1921 #ifdef CONFIG_CIFS_SWN_UPCALL
1922         cifs_genl_exit();
1923 #endif
1924 #ifdef CONFIG_CIFS_UPCALL
1925         exit_cifs_spnego();
1926 #endif
1927 #ifdef CONFIG_CIFS_DFS_UPCALL
1928         dfs_cache_destroy();
1929 #endif
1930         cifs_destroy_request_bufs();
1931         destroy_mids();
1932         cifs_destroy_inodecache();
1933         destroy_workqueue(deferredclose_wq);
1934         destroy_workqueue(cifsoplockd_wq);
1935         destroy_workqueue(decrypt_wq);
1936         destroy_workqueue(fileinfo_put_wq);
1937         destroy_workqueue(cifsiod_wq);
1938         cifs_proc_clean();
1939 }
1940
1941 MODULE_AUTHOR("Steve French");
1942 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1943 MODULE_DESCRIPTION
1944         ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1945         "also older servers complying with the SNIA CIFS Specification)");
1946 MODULE_VERSION(CIFS_VERSION);
1947 MODULE_SOFTDEP("ecb");
1948 MODULE_SOFTDEP("hmac");
1949 MODULE_SOFTDEP("md5");
1950 MODULE_SOFTDEP("nls");
1951 MODULE_SOFTDEP("aes");
1952 MODULE_SOFTDEP("cmac");
1953 MODULE_SOFTDEP("sha256");
1954 MODULE_SOFTDEP("sha512");
1955 MODULE_SOFTDEP("aead2");
1956 MODULE_SOFTDEP("ccm");
1957 MODULE_SOFTDEP("gcm");
1958 module_init(init_cifs)
1959 module_exit(exit_cifs)