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