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