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