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