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