CIFS: Add oplock break support for SMB2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / cifs / cifsfs.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifsfs.c
3 *
2b280fab 4 * Copyright (C) International Business Machines Corp., 2002,2008
1da177e4
LT
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>
6ab16d24 35#include <linux/delay.h>
45af7a0f 36#include <linux/kthread.h>
7dfb7103 37#include <linux/freezer.h>
fec11dd9 38#include <linux/namei.h>
3eb9a889 39#include <net/ipv6.h>
1da177e4
LT
40#include "cifsfs.h"
41#include "cifspdu.h"
42#define DECLARE_GLOBALS_HERE
43#include "cifsglob.h"
44#include "cifsproto.h"
45#include "cifs_debug.h"
46#include "cifs_fs_sb.h"
47#include <linux/mm.h>
84a15b93 48#include <linux/key-type.h>
e545937a 49#include "cifs_spnego.h"
f579cf3c 50#include "fscache.h"
3792c173
PS
51#ifdef CONFIG_CIFS_SMB2
52#include "smb2pdu.h"
53#endif
1da177e4
LT
54#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
55
1da177e4
LT
56int cifsFYI = 0;
57int cifsERROR = 1;
58int traceSMB = 0;
e7504734 59bool enable_oplocks = true;
1da177e4
LT
60unsigned int linuxExtEnabled = 1;
61unsigned int lookupCacheEnabled = 1;
04912d6a 62unsigned int global_secflags = CIFSSEC_DEF;
3979877e 63/* unsigned int ntlmv2_support = 0; */
1da177e4 64unsigned int sign_CIFS_PDUs = 1;
ee9b6d61 65static const struct super_operations cifs_super_ops;
1da177e4
LT
66unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
67module_param(CIFSMaxBufSize, int, 0);
63135e08
SF
68MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
69 "Default: 16384 Range: 8192 to 130048");
1da177e4
LT
70unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
71module_param(cifs_min_rcv, int, 0);
63135e08
SF
72MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
73 "1 to 64");
1da177e4
LT
74unsigned int cifs_min_small = 30;
75module_param(cifs_min_small, int, 0);
63135e08
SF
76MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
77 "Range: 2 to 256");
1da177e4 78unsigned int cifs_max_pending = CIFS_MAX_REQ;
fef33df8 79module_param(cifs_max_pending, int, 0444);
63135e08 80MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
10b9b98e 81 "Default: 32767 Range: 2 to 32767.");
e7504734
SF
82module_param(enable_oplocks, bool, 0644);
83MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks (bool). Default:"
84 "y/Y/1");
85
1da177e4
LT
86extern mempool_t *cifs_sm_req_poolp;
87extern mempool_t *cifs_req_poolp;
88extern mempool_t *cifs_mid_poolp;
89
da472fc8
JL
90struct workqueue_struct *cifsiod_wq;
91
33319141
PS
92#ifdef CONFIG_HIGHMEM
93DEFINE_MUTEX(cifs_kmap_mutex);
94#endif
95
1da177e4 96static int
97d1152a 97cifs_read_super(struct super_block *sb)
1da177e4
LT
98{
99 struct inode *inode;
b2e5cd33 100 struct cifs_sb_info *cifs_sb;
1da177e4 101 int rc = 0;
50c2f753 102
b2e5cd33 103 cifs_sb = CIFS_SB(sb);
1da177e4 104
2c6292ae
AV
105 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
106 sb->s_flags |= MS_POSIXACL;
107
108 if (cifs_sb_master_tcon(cifs_sb)->ses->capabilities & CAP_LARGE_FILES)
109 sb->s_maxbytes = MAX_LFS_FILESIZE;
110 else
111 sb->s_maxbytes = MAX_NON_LFS;
112
113 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
114 sb->s_time_gran = 100;
115
1da177e4
LT
116 sb->s_magic = CIFS_MAGIC_NUMBER;
117 sb->s_op = &cifs_super_ops;
8044f7f4 118 sb->s_bdi = &cifs_sb->bdi;
1da177e4
LT
119 sb->s_blocksize = CIFS_MAX_MSGSIZE;
120 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
9b6763e0 121 inode = cifs_root_iget(sb);
1da177e4 122
ce634ab2
DH
123 if (IS_ERR(inode)) {
124 rc = PTR_ERR(inode);
1da177e4
LT
125 goto out_no_root;
126 }
127
48fde701 128 sb->s_root = d_make_root(inode);
1da177e4
LT
129 if (!sb->s_root) {
130 rc = -ENOMEM;
131 goto out_no_root;
132 }
50c2f753 133
ea4b5740 134 /* do that *after* d_make_root() - we want NULL ->d_op for root here */
1c929cfe
AV
135 if (cifs_sb_master_tcon(cifs_sb)->nocase)
136 sb->s_d_op = &cifs_ci_dentry_ops;
137 else
138 sb->s_d_op = &cifs_dentry_ops;
139
f3a6a60e 140#ifdef CONFIG_CIFS_NFSD_EXPORT
7521a3c5 141 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
b6b38f70 142 cFYI(1, "export ops supported");
7521a3c5
SF
143 sb->s_export_op = &cifs_export_ops;
144 }
f3a6a60e 145#endif /* CONFIG_CIFS_NFSD_EXPORT */
1da177e4
LT
146
147 return 0;
148
149out_no_root:
b6b38f70 150 cERROR(1, "cifs_read_super: get root inode failed");
1da177e4
LT
151 return rc;
152}
153
6d686175
AV
154static void cifs_kill_sb(struct super_block *sb)
155{
156 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
157 kill_anon_super(sb);
98ab494d 158 cifs_umount(cifs_sb);
1da177e4
LT
159}
160
161static int
726c3342 162cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 163{
726c3342 164 struct super_block *sb = dentry->d_sb;
39da9847 165 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
96daf2b0 166 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
c81156dd 167 int rc = -EOPNOTSUPP;
6d5786a3 168 unsigned int xid;
1da177e4 169
6d5786a3 170 xid = get_xid();
1da177e4 171
1da177e4
LT
172 buf->f_type = CIFS_MAGIC_NUMBER;
173
39da9847
SF
174 /*
175 * PATH_MAX may be too long - it would presumably be total path,
176 * but note that some servers (includinng Samba 3) have a shorter
177 * maximum path.
178 *
179 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
180 */
181 buf->f_namelen = PATH_MAX;
1da177e4
LT
182 buf->f_files = 0; /* undefined */
183 buf->f_ffree = 0; /* unlimited */
184
39da9847
SF
185 /*
186 * We could add a second check for a QFS Unix capability bit
187 */
188 if ((tcon->ses->capabilities & CAP_UNIX) &&
189 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
190 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
191
192 /*
193 * Only need to call the old QFSInfo if failed on newer one,
194 * e.g. by OS/2.
195 **/
196 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
197 rc = CIFSSMBQFSInfo(xid, tcon, buf);
198
199 /*
200 * Some old Windows servers also do not support level 103, retry with
201 * older level one if old server failed the previous call or we
202 * bypassed it because we detected that this was an older LANMAN sess
203 */
4523cc30 204 if (rc)
39da9847
SF
205 rc = SMBOldQFSInfo(xid, tcon, buf);
206
6d5786a3 207 free_xid(xid);
39da9847 208 return 0;
1da177e4
LT
209}
210
10556cb2 211static int cifs_permission(struct inode *inode, int mask)
1da177e4
LT
212{
213 struct cifs_sb_info *cifs_sb;
214
215 cifs_sb = CIFS_SB(inode->i_sb);
216
f696a365
MS
217 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
218 if ((mask & MAY_EXEC) && !execute_ok(inode))
219 return -EACCES;
220 else
221 return 0;
222 } else /* file mode might have been restricted at mount time
50c2f753 223 on the client (above and beyond ACL on servers) for
1da177e4 224 servers which do not support setting and viewing mode bits,
50c2f753 225 so allowing client to check permissions is useful */
2830ba7f 226 return generic_permission(inode, mask);
1da177e4
LT
227}
228
e18b890b
CL
229static struct kmem_cache *cifs_inode_cachep;
230static struct kmem_cache *cifs_req_cachep;
231static struct kmem_cache *cifs_mid_cachep;
e18b890b 232static struct kmem_cache *cifs_sm_req_cachep;
1da177e4
LT
233mempool_t *cifs_sm_req_poolp;
234mempool_t *cifs_req_poolp;
235mempool_t *cifs_mid_poolp;
236
237static struct inode *
238cifs_alloc_inode(struct super_block *sb)
239{
240 struct cifsInodeInfo *cifs_inode;
e94b1766 241 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
1da177e4
LT
242 if (!cifs_inode)
243 return NULL;
244 cifs_inode->cifsAttrs = 0x20; /* default */
1da177e4
LT
245 cifs_inode->time = 0;
246 /* Until the file is open and we have gotten oplock
247 info back from the server, can not assume caching of
248 file data or metadata */
c6723628 249 cifs_set_oplock_level(cifs_inode, 0);
9a8165fc 250 cifs_inode->delete_pending = false;
df2cf170 251 cifs_inode->invalid_mapping = false;
1da177e4 252 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
fbec9ab9 253 cifs_inode->server_eof = 0;
20054bd6
JL
254 cifs_inode->uniqueid = 0;
255 cifs_inode->createtime = 0;
50c2f753 256
1b2b2126
SF
257 /* Can not set i_flags here - they get immediately overwritten
258 to zero by the VFS */
259/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
1da177e4
LT
260 INIT_LIST_HEAD(&cifs_inode->openFileList);
261 return &cifs_inode->vfs_inode;
262}
263
fa0d7e3d
NP
264static void cifs_i_callback(struct rcu_head *head)
265{
266 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
267 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
268}
269
1da177e4
LT
270static void
271cifs_destroy_inode(struct inode *inode)
272{
fa0d7e3d 273 call_rcu(&inode->i_rcu, cifs_i_callback);
1da177e4
LT
274}
275
9451a9a5 276static void
b57922d9 277cifs_evict_inode(struct inode *inode)
9451a9a5 278{
b57922d9 279 truncate_inode_pages(&inode->i_data, 0);
dbd5768f 280 clear_inode(inode);
9451a9a5
SJ
281 cifs_fscache_release_inode_cookie(inode);
282}
283
61f98ffd
JL
284static void
285cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
286{
a9f1b85e
PS
287 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
288 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
289
61f98ffd
JL
290 seq_printf(s, ",addr=");
291
a9f1b85e 292 switch (server->dstaddr.ss_family) {
61f98ffd 293 case AF_INET:
a9f1b85e 294 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
61f98ffd
JL
295 break;
296 case AF_INET6:
a9f1b85e
PS
297 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
298 if (sa6->sin6_scope_id)
299 seq_printf(s, "%%%u", sa6->sin6_scope_id);
61f98ffd
JL
300 break;
301 default:
302 seq_printf(s, "(unknown)");
303 }
304}
305
3e715513
JL
306static void
307cifs_show_security(struct seq_file *s, struct TCP_Server_Info *server)
308{
309 seq_printf(s, ",sec=");
310
311 switch (server->secType) {
312 case LANMAN:
313 seq_printf(s, "lanman");
314 break;
315 case NTLMv2:
316 seq_printf(s, "ntlmv2");
317 break;
318 case NTLM:
319 seq_printf(s, "ntlm");
320 break;
321 case Kerberos:
322 seq_printf(s, "krb5");
323 break;
324 case RawNTLMSSP:
325 seq_printf(s, "ntlmssp");
326 break;
327 default:
328 /* shouldn't ever happen */
329 seq_printf(s, "unknown");
330 break;
331 }
332
333 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
334 seq_printf(s, "i");
335}
336
d06b5056
JL
337static void
338cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
339{
340 seq_printf(s, ",cache=");
341
342 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
343 seq_printf(s, "strict");
344 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
345 seq_printf(s, "none");
346 else
347 seq_printf(s, "loose");
348}
349
1da177e4
LT
350/*
351 * cifs_show_options() is for displaying mount options in /proc/mounts.
352 * Not all settable options are displayed but most of the important
353 * ones are.
354 */
355static int
34c80b1d 356cifs_show_options(struct seq_file *s, struct dentry *root)
1da177e4 357{
34c80b1d 358 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
96daf2b0 359 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
3eb9a889
BG
360 struct sockaddr *srcaddr;
361 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
8616e0fc 362
23db65f5 363 seq_printf(s, ",vers=%s", tcon->ses->server->vals->version_string);
3e715513 364 cifs_show_security(s, tcon->ses->server);
d06b5056 365 cifs_show_cache_flavor(s, cifs_sb);
3e715513 366
8e047d09 367 seq_printf(s, ",unc=%s", tcon->treeName);
29e07c82
JL
368
369 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
370 seq_printf(s, ",multiuser");
8727c8a8
SF
371 else if (tcon->ses->user_name)
372 seq_printf(s, ",username=%s", tcon->ses->user_name);
29e07c82 373
8616e0fc
JL
374 if (tcon->ses->domainName)
375 seq_printf(s, ",domain=%s", tcon->ses->domainName);
376
3eb9a889
BG
377 if (srcaddr->sa_family != AF_UNSPEC) {
378 struct sockaddr_in *saddr4;
379 struct sockaddr_in6 *saddr6;
380 saddr4 = (struct sockaddr_in *)srcaddr;
381 saddr6 = (struct sockaddr_in6 *)srcaddr;
382 if (srcaddr->sa_family == AF_INET6)
383 seq_printf(s, ",srcaddr=%pI6c",
384 &saddr6->sin6_addr);
385 else if (srcaddr->sa_family == AF_INET)
386 seq_printf(s, ",srcaddr=%pI4",
387 &saddr4->sin_addr.s_addr);
388 else
389 seq_printf(s, ",srcaddr=BAD-AF:%i",
390 (int)(srcaddr->sa_family));
391 }
392
28f88810 393 seq_printf(s, ",uid=%u", cifs_sb->mnt_uid);
340481a3
JL
394 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
395 seq_printf(s, ",forceuid");
4486d6ed
JL
396 else
397 seq_printf(s, ",noforceuid");
340481a3 398
28f88810 399 seq_printf(s, ",gid=%u", cifs_sb->mnt_gid);
340481a3
JL
400 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
401 seq_printf(s, ",forcegid");
4486d6ed
JL
402 else
403 seq_printf(s, ",noforcegid");
8616e0fc 404
61f98ffd 405 cifs_show_address(s, tcon->ses->server);
1da177e4 406
8616e0fc 407 if (!tcon->unix_ext)
5206efd6 408 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
2b280fab
SF
409 cifs_sb->mnt_file_mode,
410 cifs_sb->mnt_dir_mode);
8616e0fc
JL
411 if (tcon->seal)
412 seq_printf(s, ",seal");
413 if (tcon->nocase)
414 seq_printf(s, ",nocase");
415 if (tcon->retry)
416 seq_printf(s, ",hard");
d4ffff1f
PS
417 if (tcon->unix_ext)
418 seq_printf(s, ",unix");
419 else
420 seq_printf(s, ",nounix");
8616e0fc
JL
421 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
422 seq_printf(s, ",posixpaths");
423 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
424 seq_printf(s, ",setuids");
425 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
426 seq_printf(s, ",serverino");
d4ffff1f
PS
427 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
428 seq_printf(s, ",rwpidforward");
429 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
430 seq_printf(s, ",forcemand");
8616e0fc
JL
431 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
432 seq_printf(s, ",nouser_xattr");
433 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
434 seq_printf(s, ",mapchars");
435 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
436 seq_printf(s, ",sfu");
437 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
438 seq_printf(s, ",nobrl");
439 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
440 seq_printf(s, ",cifsacl");
441 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
442 seq_printf(s, ",dynperm");
34c80b1d 443 if (root->d_sb->s_flags & MS_POSIXACL)
8616e0fc 444 seq_printf(s, ",acl");
736a3320
SM
445 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
446 seq_printf(s, ",mfsymlinks");
476428f8
SJ
447 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
448 seq_printf(s, ",fsc");
71c424ba
SF
449 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
450 seq_printf(s, ",nostrictsync");
451 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
452 seq_printf(s, ",noperm");
3c7c87fd
SP
453 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
454 seq_printf(s, ",backupuid=%u", cifs_sb->mnt_backupuid);
455 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
456 seq_printf(s, ",backupgid=%u", cifs_sb->mnt_backupgid);
8616e0fc 457
28f88810
SP
458 seq_printf(s, ",rsize=%u", cifs_sb->rsize);
459 seq_printf(s, ",wsize=%u", cifs_sb->wsize);
6d20e840 460 /* convert actimeo and display it in seconds */
156d1790 461 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
8616e0fc 462
1da177e4
LT
463 return 0;
464}
465
42faad99 466static void cifs_umount_begin(struct super_block *sb)
68058e75 467{
42faad99 468 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
96daf2b0 469 struct cifs_tcon *tcon;
68058e75 470
4523cc30 471 if (cifs_sb == NULL)
9e2e85f8
SF
472 return;
473
0d424ad0 474 tcon = cifs_sb_master_tcon(cifs_sb);
f1987b44 475
3f9bcca7 476 spin_lock(&cifs_tcp_ses_lock);
ad8034f1
SF
477 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
478 /* we have other mounts to same share or we have
479 already tried to force umount this and woken up
480 all waiting network requests, nothing to do */
3f9bcca7 481 spin_unlock(&cifs_tcp_ses_lock);
ad8034f1
SF
482 return;
483 } else if (tcon->tc_count == 1)
5e1253b5 484 tcon->tidStatus = CifsExiting;
3f9bcca7 485 spin_unlock(&cifs_tcp_ses_lock);
5e1253b5 486
3a5ff61c 487 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
7b7abfe3 488 /* cancel_notify_requests(tcon); */
50c2f753 489 if (tcon->ses && tcon->ses->server) {
b6b38f70 490 cFYI(1, "wake up tasks now - umount begin not complete");
9e2e85f8 491 wake_up_all(&tcon->ses->server->request_q);
6ab16d24
SF
492 wake_up_all(&tcon->ses->server->response_q);
493 msleep(1); /* yield */
494 /* we have to kick the requests once more */
495 wake_up_all(&tcon->ses->server->response_q);
496 msleep(1);
5e1253b5 497 }
68058e75
SF
498
499 return;
500}
68058e75 501
bf97d287 502#ifdef CONFIG_CIFS_STATS2
64132379 503static int cifs_show_stats(struct seq_file *s, struct dentry *root)
bf97d287
SF
504{
505 /* BB FIXME */
506 return 0;
507}
508#endif
509
1da177e4
LT
510static int cifs_remount(struct super_block *sb, int *flags, char *data)
511{
512 *flags |= MS_NODIRATIME;
513 return 0;
514}
515
45321ac5 516static int cifs_drop_inode(struct inode *inode)
12420ac3
JL
517{
518 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
519
45321ac5
AV
520 /* no serverino => unconditional eviction */
521 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
522 generic_drop_inode(inode);
12420ac3
JL
523}
524
ee9b6d61 525static const struct super_operations cifs_super_ops = {
1da177e4
LT
526 .statfs = cifs_statfs,
527 .alloc_inode = cifs_alloc_inode,
528 .destroy_inode = cifs_destroy_inode,
12420ac3 529 .drop_inode = cifs_drop_inode,
b57922d9 530 .evict_inode = cifs_evict_inode,
12420ac3
JL
531/* .delete_inode = cifs_delete_inode, */ /* Do not need above
532 function unless later we add lazy close of inodes or unless the
50c2f753
SF
533 kernel forgets to call us with the same number of releases (closes)
534 as opens */
1da177e4 535 .show_options = cifs_show_options,
7b7abfe3 536 .umount_begin = cifs_umount_begin,
1da177e4 537 .remount_fs = cifs_remount,
bf97d287 538#ifdef CONFIG_CIFS_STATS2
f46d3e11 539 .show_stats = cifs_show_stats,
bf97d287 540#endif
1da177e4
LT
541};
542
f87d39d9
SF
543/*
544 * Get root dentry from superblock according to prefix path mount option.
545 * Return dentry with refcount + 1 on success and NULL otherwise.
546 */
547static struct dentry *
548cifs_get_root(struct smb_vol *vol, struct super_block *sb)
549{
fec11dd9 550 struct dentry *dentry;
f87d39d9 551 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
fec11dd9
AV
552 char *full_path = NULL;
553 char *s, *p;
f87d39d9
SF
554 char sep;
555
9224dfc2
PS
556 full_path = build_path_to_root(vol, cifs_sb,
557 cifs_sb_master_tcon(cifs_sb));
f87d39d9 558 if (full_path == NULL)
9403c9c5 559 return ERR_PTR(-ENOMEM);
f87d39d9
SF
560
561 cFYI(1, "Get root dentry for %s", full_path);
562
f87d39d9 563 sep = CIFS_DIR_SEP(cifs_sb);
fec11dd9
AV
564 dentry = dget(sb->s_root);
565 p = s = full_path;
566
567 do {
568 struct inode *dir = dentry->d_inode;
569 struct dentry *child;
570
5b980b01
PS
571 if (!dir) {
572 dput(dentry);
573 dentry = ERR_PTR(-ENOENT);
574 break;
575 }
576
fec11dd9
AV
577 /* skip separators */
578 while (*s == sep)
579 s++;
580 if (!*s)
581 break;
582 p = s++;
583 /* next separator */
584 while (*s && *s != sep)
585 s++;
586
587 mutex_lock(&dir->i_mutex);
588 child = lookup_one_len(p, dentry, s - p);
589 mutex_unlock(&dir->i_mutex);
590 dput(dentry);
591 dentry = child;
592 } while (!IS_ERR(dentry));
f87d39d9 593 kfree(full_path);
fec11dd9 594 return dentry;
f87d39d9
SF
595}
596
ee01a14d
AV
597static int cifs_set_super(struct super_block *sb, void *data)
598{
599 struct cifs_mnt_data *mnt_data = data;
600 sb->s_fs_info = mnt_data->cifs_sb;
601 return set_anon_super(sb, NULL);
602}
603
d753ed97
AV
604static struct dentry *
605cifs_do_mount(struct file_system_type *fs_type,
724d9f1c 606 int flags, const char *dev_name, void *data)
1da177e4
LT
607{
608 int rc;
db719222 609 struct super_block *sb;
724d9f1c
PS
610 struct cifs_sb_info *cifs_sb;
611 struct smb_vol *volume_info;
25c7f41e 612 struct cifs_mnt_data mnt_data;
724d9f1c 613 struct dentry *root;
1da177e4 614
b6b38f70 615 cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
1da177e4 616
04db79b0
JL
617 volume_info = cifs_get_volume_info((char *)data, dev_name);
618 if (IS_ERR(volume_info))
619 return ERR_CAST(volume_info);
724d9f1c
PS
620
621 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
622 if (cifs_sb == NULL) {
623 root = ERR_PTR(-ENOMEM);
5c4f1ad7 624 goto out_nls;
724d9f1c
PS
625 }
626
5d3bc605
AV
627 cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
628 if (cifs_sb->mountdata == NULL) {
629 root = ERR_PTR(-ENOMEM);
5c4f1ad7 630 goto out_cifs_sb;
5d3bc605
AV
631 }
632
724d9f1c
PS
633 cifs_setup_cifs_sb(volume_info, cifs_sb);
634
97d1152a
AV
635 rc = cifs_mount(cifs_sb, volume_info);
636 if (rc) {
637 if (!(flags & MS_SILENT))
638 cERROR(1, "cifs_mount failed w/return code = %d", rc);
639 root = ERR_PTR(rc);
5c4f1ad7 640 goto out_mountdata;
97d1152a
AV
641 }
642
25c7f41e
PS
643 mnt_data.vol = volume_info;
644 mnt_data.cifs_sb = cifs_sb;
645 mnt_data.flags = flags;
646
9249e17f
DH
647 /* BB should we make this contingent on mount parm? */
648 flags |= MS_NODIRATIME | MS_NOATIME;
649
650 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
724d9f1c 651 if (IS_ERR(sb)) {
724d9f1c 652 root = ERR_CAST(sb);
97d1152a 653 cifs_umount(cifs_sb);
d757d71b 654 goto out;
724d9f1c 655 }
1da177e4 656
ee01a14d 657 if (sb->s_root) {
25c7f41e 658 cFYI(1, "Use existing superblock");
97d1152a 659 cifs_umount(cifs_sb);
5c4f1ad7 660 } else {
5c4f1ad7
AV
661 rc = cifs_read_super(sb);
662 if (rc) {
663 root = ERR_PTR(rc);
664 goto out_super;
665 }
b2e5cd33 666
5c4f1ad7 667 sb->s_flags |= MS_ACTIVE;
1da177e4 668 }
724d9f1c 669
f87d39d9 670 root = cifs_get_root(volume_info, sb);
9403c9c5 671 if (IS_ERR(root))
f87d39d9 672 goto out_super;
25c7f41e 673
f87d39d9 674 cFYI(1, "dentry root is: %p", root);
641a58d6 675 goto out;
724d9f1c 676
641a58d6 677out_super:
641a58d6 678 deactivate_locked_super(sb);
641a58d6 679out:
f9e59bcb 680 cifs_cleanup_volume_info(volume_info);
724d9f1c 681 return root;
5c4f1ad7
AV
682
683out_mountdata:
684 kfree(cifs_sb->mountdata);
685out_cifs_sb:
686 kfree(cifs_sb);
687out_nls:
688 unload_nls(volume_info->local_nls);
689 goto out;
1da177e4
LT
690}
691
027445c3
BP
692static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
693 unsigned long nr_segs, loff_t pos)
1da177e4 694{
e6a00296 695 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
1da177e4 696 ssize_t written;
72432ffc 697 int rc;
1da177e4 698
027445c3 699 written = generic_file_aio_write(iocb, iov, nr_segs, pos);
72432ffc
PS
700
701 if (CIFS_I(inode)->clientCanCacheAll)
702 return written;
703
704 rc = filemap_fdatawrite(inode->i_mapping);
705 if (rc)
706 cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
707
1da177e4
LT
708 return written;
709}
710
c32a0b68
SF
711static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
712{
06222e49
JB
713 /*
714 * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
715 * the cached file length
716 */
48a5730e 717 if (origin != SEEK_SET && origin != SEEK_CUR) {
6feb9891
PS
718 int rc;
719 struct inode *inode = file->f_path.dentry->d_inode;
720
721 /*
722 * We need to be sure that all dirty pages are written and the
723 * server has the newest file length.
724 */
725 if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
726 inode->i_mapping->nrpages != 0) {
727 rc = filemap_fdatawait(inode->i_mapping);
156ecb2d
SF
728 if (rc) {
729 mapping_set_error(inode->i_mapping, rc);
730 return rc;
731 }
6feb9891
PS
732 }
733 /*
734 * Some applications poll for the file length in this strange
735 * way so we must seek to end on non-oplocked files by
736 * setting the revalidate time to zero.
737 */
738 CIFS_I(inode)->time = 0;
739
740 rc = cifs_revalidate_file_attr(file);
741 if (rc < 0)
742 return (loff_t)rc;
c32a0b68 743 }
ef3d0fd2 744 return generic_file_llseek(file, offset, origin);
c32a0b68
SF
745}
746
84210e91
SF
747static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
748{
b89f4321
AB
749 /* note that this is called by vfs setlease with lock_flocks held
750 to protect *lease from going away */
84210e91 751 struct inode *inode = file->f_path.dentry->d_inode;
ba00ba64 752 struct cifsFileInfo *cfile = file->private_data;
84210e91
SF
753
754 if (!(S_ISREG(inode->i_mode)))
755 return -EINVAL;
756
757 /* check if file is oplocked */
758 if (((arg == F_RDLCK) &&
759 (CIFS_I(inode)->clientCanCacheRead)) ||
760 ((arg == F_WRLCK) &&
761 (CIFS_I(inode)->clientCanCacheAll)))
762 return generic_setlease(file, arg, lease);
13cfb733
JL
763 else if (tlink_tcon(cfile->tlink)->local_lease &&
764 !CIFS_I(inode)->clientCanCacheRead)
84210e91
SF
765 /* If the server claims to support oplock on this
766 file, then we still need to check oplock even
767 if the local_lease mount option is set, but there
768 are servers which do not support oplock for which
769 this mount option may be useful if the user
770 knows that the file won't be changed on the server
771 by anyone else */
772 return generic_setlease(file, arg, lease);
51ee4b84 773 else
84210e91
SF
774 return -EAGAIN;
775}
84210e91 776
e6ab1582 777struct file_system_type cifs_fs_type = {
1da177e4
LT
778 .owner = THIS_MODULE,
779 .name = "cifs",
d753ed97 780 .mount = cifs_do_mount,
6d686175 781 .kill_sb = cifs_kill_sb,
1da177e4
LT
782 /* .fs_flags */
783};
754661f1 784const struct inode_operations cifs_dir_inode_ops = {
1da177e4 785 .create = cifs_create,
d2c12719 786 .atomic_open = cifs_atomic_open,
1da177e4
LT
787 .lookup = cifs_lookup,
788 .getattr = cifs_getattr,
789 .unlink = cifs_unlink,
790 .link = cifs_hardlink,
791 .mkdir = cifs_mkdir,
792 .rmdir = cifs_rmdir,
793 .rename = cifs_rename,
794 .permission = cifs_permission,
795/* revalidate:cifs_revalidate, */
796 .setattr = cifs_setattr,
797 .symlink = cifs_symlink,
798 .mknod = cifs_mknod,
799#ifdef CONFIG_CIFS_XATTR
800 .setxattr = cifs_setxattr,
801 .getxattr = cifs_getxattr,
802 .listxattr = cifs_listxattr,
803 .removexattr = cifs_removexattr,
804#endif
805};
806
754661f1 807const struct inode_operations cifs_file_inode_ops = {
1da177e4
LT
808/* revalidate:cifs_revalidate, */
809 .setattr = cifs_setattr,
810 .getattr = cifs_getattr, /* do we need this anymore? */
811 .rename = cifs_rename,
812 .permission = cifs_permission,
813#ifdef CONFIG_CIFS_XATTR
814 .setxattr = cifs_setxattr,
815 .getxattr = cifs_getxattr,
816 .listxattr = cifs_listxattr,
817 .removexattr = cifs_removexattr,
50c2f753 818#endif
1da177e4
LT
819};
820
754661f1 821const struct inode_operations cifs_symlink_inode_ops = {
50c2f753 822 .readlink = generic_readlink,
1da177e4
LT
823 .follow_link = cifs_follow_link,
824 .put_link = cifs_put_link,
825 .permission = cifs_permission,
826 /* BB add the following two eventually */
827 /* revalidate: cifs_revalidate,
828 setattr: cifs_notify_change, *//* BB do we need notify change */
829#ifdef CONFIG_CIFS_XATTR
830 .setxattr = cifs_setxattr,
831 .getxattr = cifs_getxattr,
832 .listxattr = cifs_listxattr,
833 .removexattr = cifs_removexattr,
50c2f753 834#endif
1da177e4
LT
835};
836
4b6f5d20 837const struct file_operations cifs_file_ops = {
87c89dd7
SF
838 .read = do_sync_read,
839 .write = do_sync_write,
87c89dd7
SF
840 .aio_read = generic_file_aio_read,
841 .aio_write = cifs_file_aio_write,
1da177e4
LT
842 .open = cifs_open,
843 .release = cifs_close,
844 .lock = cifs_lock,
845 .fsync = cifs_fsync,
846 .flush = cifs_flush,
847 .mmap = cifs_file_mmap,
5ffc4ef4 848 .splice_read = generic_file_splice_read,
c32a0b68 849 .llseek = cifs_llseek,
c67593a0 850#ifdef CONFIG_CIFS_POSIX
f9ddcca4 851 .unlocked_ioctl = cifs_ioctl,
c67593a0 852#endif /* CONFIG_CIFS_POSIX */
84210e91 853 .setlease = cifs_setlease,
1da177e4
LT
854};
855
8be7e6ba
PS
856const struct file_operations cifs_file_strict_ops = {
857 .read = do_sync_read,
858 .write = do_sync_write,
a70307ee 859 .aio_read = cifs_strict_readv,
72432ffc 860 .aio_write = cifs_strict_writev,
8be7e6ba
PS
861 .open = cifs_open,
862 .release = cifs_close,
863 .lock = cifs_lock,
864 .fsync = cifs_strict_fsync,
865 .flush = cifs_flush,
7a6a19b1 866 .mmap = cifs_file_strict_mmap,
8be7e6ba
PS
867 .splice_read = generic_file_splice_read,
868 .llseek = cifs_llseek,
869#ifdef CONFIG_CIFS_POSIX
870 .unlocked_ioctl = cifs_ioctl,
871#endif /* CONFIG_CIFS_POSIX */
872 .setlease = cifs_setlease,
873};
874
4b6f5d20 875const struct file_operations cifs_file_direct_ops = {
0b81c1c4
PS
876 /* BB reevaluate whether they can be done with directio, no cache */
877 .read = do_sync_read,
878 .write = do_sync_write,
879 .aio_read = cifs_user_readv,
880 .aio_write = cifs_user_writev,
1da177e4
LT
881 .open = cifs_open,
882 .release = cifs_close,
883 .lock = cifs_lock,
884 .fsync = cifs_fsync,
885 .flush = cifs_flush,
a994b8fa 886 .mmap = cifs_file_mmap,
5ffc4ef4 887 .splice_read = generic_file_splice_read,
c67593a0 888#ifdef CONFIG_CIFS_POSIX
f9ddcca4 889 .unlocked_ioctl = cifs_ioctl,
c67593a0 890#endif /* CONFIG_CIFS_POSIX */
c32a0b68 891 .llseek = cifs_llseek,
84210e91 892 .setlease = cifs_setlease,
1da177e4 893};
8be7e6ba 894
4b6f5d20 895const struct file_operations cifs_file_nobrl_ops = {
87c89dd7
SF
896 .read = do_sync_read,
897 .write = do_sync_write,
87c89dd7
SF
898 .aio_read = generic_file_aio_read,
899 .aio_write = cifs_file_aio_write,
900 .open = cifs_open,
901 .release = cifs_close,
902 .fsync = cifs_fsync,
903 .flush = cifs_flush,
904 .mmap = cifs_file_mmap,
5ffc4ef4 905 .splice_read = generic_file_splice_read,
c32a0b68 906 .llseek = cifs_llseek,
8b94bcb9 907#ifdef CONFIG_CIFS_POSIX
f9ddcca4 908 .unlocked_ioctl = cifs_ioctl,
8b94bcb9 909#endif /* CONFIG_CIFS_POSIX */
84210e91 910 .setlease = cifs_setlease,
8b94bcb9
SF
911};
912
8be7e6ba
PS
913const struct file_operations cifs_file_strict_nobrl_ops = {
914 .read = do_sync_read,
915 .write = do_sync_write,
a70307ee 916 .aio_read = cifs_strict_readv,
72432ffc 917 .aio_write = cifs_strict_writev,
8be7e6ba
PS
918 .open = cifs_open,
919 .release = cifs_close,
920 .fsync = cifs_strict_fsync,
921 .flush = cifs_flush,
7a6a19b1 922 .mmap = cifs_file_strict_mmap,
8be7e6ba
PS
923 .splice_read = generic_file_splice_read,
924 .llseek = cifs_llseek,
925#ifdef CONFIG_CIFS_POSIX
926 .unlocked_ioctl = cifs_ioctl,
927#endif /* CONFIG_CIFS_POSIX */
928 .setlease = cifs_setlease,
929};
930
4b6f5d20 931const struct file_operations cifs_file_direct_nobrl_ops = {
0b81c1c4
PS
932 /* BB reevaluate whether they can be done with directio, no cache */
933 .read = do_sync_read,
934 .write = do_sync_write,
935 .aio_read = cifs_user_readv,
936 .aio_write = cifs_user_writev,
87c89dd7
SF
937 .open = cifs_open,
938 .release = cifs_close,
939 .fsync = cifs_fsync,
940 .flush = cifs_flush,
810627a0 941 .mmap = cifs_file_mmap,
5ffc4ef4 942 .splice_read = generic_file_splice_read,
8b94bcb9 943#ifdef CONFIG_CIFS_POSIX
f9ddcca4 944 .unlocked_ioctl = cifs_ioctl,
8b94bcb9 945#endif /* CONFIG_CIFS_POSIX */
c32a0b68 946 .llseek = cifs_llseek,
84210e91 947 .setlease = cifs_setlease,
8b94bcb9 948};
1da177e4 949
4b6f5d20 950const struct file_operations cifs_dir_ops = {
1da177e4
LT
951 .readdir = cifs_readdir,
952 .release = cifs_closedir,
953 .read = generic_read_dir,
f9ddcca4 954 .unlocked_ioctl = cifs_ioctl,
3222a3e5 955 .llseek = generic_file_llseek,
1da177e4
LT
956};
957
958static void
51cc5068 959cifs_init_once(void *inode)
1da177e4
LT
960{
961 struct cifsInodeInfo *cifsi = inode;
962
a35afb83 963 inode_init_once(&cifsi->vfs_inode);
d59dad2b 964 mutex_init(&cifsi->lock_mutex);
1da177e4
LT
965}
966
967static int
968cifs_init_inodecache(void)
969{
970 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
26f57364 971 sizeof(struct cifsInodeInfo),
fffb60f9
PJ
972 0, (SLAB_RECLAIM_ACCOUNT|
973 SLAB_MEM_SPREAD),
20c2df83 974 cifs_init_once);
1da177e4
LT
975 if (cifs_inode_cachep == NULL)
976 return -ENOMEM;
977
978 return 0;
979}
980
981static void
982cifs_destroy_inodecache(void)
983{
1a1d92c1 984 kmem_cache_destroy(cifs_inode_cachep);
1da177e4
LT
985}
986
987static int
988cifs_init_request_bufs(void)
989{
3792c173
PS
990 size_t max_hdr_size = MAX_CIFS_HDR_SIZE;
991#ifdef CONFIG_CIFS_SMB2
992 /*
993 * SMB2 maximum header size is bigger than CIFS one - no problems to
994 * allocate some more bytes for CIFS.
995 */
996 max_hdr_size = MAX_SMB2_HDR_SIZE;
997#endif
4523cc30 998 if (CIFSMaxBufSize < 8192) {
1da177e4
LT
999 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1000 Unicode path name has to fit in any SMB/CIFS path based frames */
1001 CIFSMaxBufSize = 8192;
1002 } else if (CIFSMaxBufSize > 1024*127) {
1003 CIFSMaxBufSize = 1024 * 127;
1004 } else {
1005 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1006 }
b6b38f70 1007/* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
1da177e4 1008 cifs_req_cachep = kmem_cache_create("cifs_request",
3792c173 1009 CIFSMaxBufSize + max_hdr_size, 0,
20c2df83 1010 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1011 if (cifs_req_cachep == NULL)
1012 return -ENOMEM;
1013
4523cc30 1014 if (cifs_min_rcv < 1)
1da177e4
LT
1015 cifs_min_rcv = 1;
1016 else if (cifs_min_rcv > 64) {
1017 cifs_min_rcv = 64;
b6b38f70 1018 cERROR(1, "cifs_min_rcv set to maximum (64)");
1da177e4
LT
1019 }
1020
93d2341c
MD
1021 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1022 cifs_req_cachep);
1da177e4 1023
4523cc30 1024 if (cifs_req_poolp == NULL) {
1da177e4
LT
1025 kmem_cache_destroy(cifs_req_cachep);
1026 return -ENOMEM;
1027 }
ec637e3f 1028 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1da177e4
LT
1029 almost all handle based requests (but not write response, nor is it
1030 sufficient for path based requests). A smaller size would have
50c2f753 1031 been more efficient (compacting multiple slab items on one 4k page)
1da177e4
LT
1032 for the case in which debug was on, but this larger size allows
1033 more SMBs to use small buffer alloc and is still much more
6dc0f87e 1034 efficient to alloc 1 per page off the slab compared to 17K (5page)
1da177e4
LT
1035 alloc of large cifs buffers even when page debugging is on */
1036 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
6dc0f87e 1037 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
20c2df83 1038 NULL);
1da177e4
LT
1039 if (cifs_sm_req_cachep == NULL) {
1040 mempool_destroy(cifs_req_poolp);
1041 kmem_cache_destroy(cifs_req_cachep);
6dc0f87e 1042 return -ENOMEM;
1da177e4
LT
1043 }
1044
4523cc30 1045 if (cifs_min_small < 2)
1da177e4
LT
1046 cifs_min_small = 2;
1047 else if (cifs_min_small > 256) {
1048 cifs_min_small = 256;
b6b38f70 1049 cFYI(1, "cifs_min_small set to maximum (256)");
1da177e4
LT
1050 }
1051
93d2341c
MD
1052 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1053 cifs_sm_req_cachep);
1da177e4 1054
4523cc30 1055 if (cifs_sm_req_poolp == NULL) {
1da177e4
LT
1056 mempool_destroy(cifs_req_poolp);
1057 kmem_cache_destroy(cifs_req_cachep);
1058 kmem_cache_destroy(cifs_sm_req_cachep);
1059 return -ENOMEM;
1060 }
1061
1062 return 0;
1063}
1064
1065static void
1066cifs_destroy_request_bufs(void)
1067{
1068 mempool_destroy(cifs_req_poolp);
1a1d92c1 1069 kmem_cache_destroy(cifs_req_cachep);
1da177e4 1070 mempool_destroy(cifs_sm_req_poolp);
1a1d92c1 1071 kmem_cache_destroy(cifs_sm_req_cachep);
1da177e4
LT
1072}
1073
1074static int
1075cifs_init_mids(void)
1076{
1077 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
26f57364
SF
1078 sizeof(struct mid_q_entry), 0,
1079 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1080 if (cifs_mid_cachep == NULL)
1081 return -ENOMEM;
1082
93d2341c
MD
1083 /* 3 is a reasonable minimum number of simultaneous operations */
1084 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
4523cc30 1085 if (cifs_mid_poolp == NULL) {
1da177e4
LT
1086 kmem_cache_destroy(cifs_mid_cachep);
1087 return -ENOMEM;
1088 }
1089
1da177e4
LT
1090 return 0;
1091}
1092
1093static void
1094cifs_destroy_mids(void)
1095{
1096 mempool_destroy(cifs_mid_poolp);
1a1d92c1 1097 kmem_cache_destroy(cifs_mid_cachep);
1da177e4
LT
1098}
1099
1da177e4
LT
1100static int __init
1101init_cifs(void)
1102{
1103 int rc = 0;
1da177e4 1104 cifs_proc_init();
e7ddee90 1105 INIT_LIST_HEAD(&cifs_tcp_ses_list);
0eff0e26 1106#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
4ca9c190
SF
1107 INIT_LIST_HEAD(&GlobalDnotifyReqList);
1108 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
0eff0e26 1109#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1da177e4
LT
1110/*
1111 * Initialize Global counters
1112 */
1113 atomic_set(&sesInfoAllocCount, 0);
1114 atomic_set(&tconInfoAllocCount, 0);
6dc0f87e 1115 atomic_set(&tcpSesAllocCount, 0);
1da177e4
LT
1116 atomic_set(&tcpSesReconnectCount, 0);
1117 atomic_set(&tconInfoReconnectCount, 0);
1118
1119 atomic_set(&bufAllocCount, 0);
4498eed5
SF
1120 atomic_set(&smBufAllocCount, 0);
1121#ifdef CONFIG_CIFS_STATS2
1122 atomic_set(&totBufAllocCount, 0);
1123 atomic_set(&totSmBufAllocCount, 0);
1124#endif /* CONFIG_CIFS_STATS2 */
1125
1da177e4
LT
1126 atomic_set(&midCount, 0);
1127 GlobalCurrentXid = 0;
1128 GlobalTotalActiveXid = 0;
1129 GlobalMaxActiveXid = 0;
3f9bcca7 1130 spin_lock_init(&cifs_tcp_ses_lock);
4477288a 1131 spin_lock_init(&cifs_file_list_lock);
1da177e4
LT
1132 spin_lock_init(&GlobalMid_Lock);
1133
4523cc30 1134 if (cifs_max_pending < 2) {
1da177e4 1135 cifs_max_pending = 2;
b6b38f70 1136 cFYI(1, "cifs_max_pending set to min of 2");
10b9b98e
PS
1137 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1138 cifs_max_pending = CIFS_MAX_REQ;
1139 cFYI(1, "cifs_max_pending set to max of %u", CIFS_MAX_REQ);
1da177e4
LT
1140 }
1141
da472fc8
JL
1142 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1143 if (!cifsiod_wq) {
1144 rc = -ENOMEM;
1145 goto out_clean_proc;
1146 }
1147
f579cf3c
SJ
1148 rc = cifs_fscache_register();
1149 if (rc)
da472fc8 1150 goto out_destroy_wq;
f579cf3c 1151
1da177e4 1152 rc = cifs_init_inodecache();
45af7a0f 1153 if (rc)
d3bf5221 1154 goto out_unreg_fscache;
45af7a0f
SF
1155
1156 rc = cifs_init_mids();
1157 if (rc)
1158 goto out_destroy_inodecache;
1159
1160 rc = cifs_init_request_bufs();
1161 if (rc)
1162 goto out_destroy_mids;
1163
84a15b93
JL
1164#ifdef CONFIG_CIFS_UPCALL
1165 rc = register_key_type(&cifs_spnego_key_type);
1166 if (rc)
4d79dba0
SP
1167 goto out_destroy_request_bufs;
1168#endif /* CONFIG_CIFS_UPCALL */
1169
1170#ifdef CONFIG_CIFS_ACL
1171 rc = init_cifs_idmap();
1172 if (rc)
c4aca0c0 1173 goto out_register_key_type;
4d79dba0
SP
1174#endif /* CONFIG_CIFS_ACL */
1175
1176 rc = register_filesystem(&cifs_fs_type);
1177 if (rc)
c4aca0c0 1178 goto out_init_cifs_idmap;
45af7a0f 1179
45af7a0f
SF
1180 return 0;
1181
c4aca0c0 1182out_init_cifs_idmap:
4d79dba0
SP
1183#ifdef CONFIG_CIFS_ACL
1184 exit_cifs_idmap();
c4aca0c0 1185out_register_key_type:
4d79dba0 1186#endif
84a15b93 1187#ifdef CONFIG_CIFS_UPCALL
4d79dba0 1188 unregister_key_type(&cifs_spnego_key_type);
c4aca0c0 1189out_destroy_request_bufs:
1fc7995d 1190#endif
45af7a0f 1191 cifs_destroy_request_bufs();
d3bf5221 1192out_destroy_mids:
45af7a0f 1193 cifs_destroy_mids();
d3bf5221 1194out_destroy_inodecache:
45af7a0f 1195 cifs_destroy_inodecache();
d3bf5221 1196out_unreg_fscache:
f579cf3c 1197 cifs_fscache_unregister();
da472fc8
JL
1198out_destroy_wq:
1199 destroy_workqueue(cifsiod_wq);
d3bf5221
SF
1200out_clean_proc:
1201 cifs_proc_clean();
1da177e4
LT
1202 return rc;
1203}
1204
1205static void __exit
1206exit_cifs(void)
1207{
b6b38f70 1208 cFYI(DBG2, "exit_cifs");
3dd93306 1209 unregister_filesystem(&cifs_fs_type);
78d31a3a 1210 cifs_dfs_release_automount_timer();
4d79dba0
SP
1211#ifdef CONFIG_CIFS_ACL
1212 cifs_destroy_idmaptrees();
1213 exit_cifs_idmap();
1214#endif
84a15b93
JL
1215#ifdef CONFIG_CIFS_UPCALL
1216 unregister_key_type(&cifs_spnego_key_type);
1da177e4 1217#endif
1da177e4 1218 cifs_destroy_request_bufs();
3dd93306
JL
1219 cifs_destroy_mids();
1220 cifs_destroy_inodecache();
1221 cifs_fscache_unregister();
da472fc8 1222 destroy_workqueue(cifsiod_wq);
3dd93306 1223 cifs_proc_clean();
1da177e4
LT
1224}
1225
1226MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
6dc0f87e 1227MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1da177e4 1228MODULE_DESCRIPTION
63135e08
SF
1229 ("VFS to access servers complying with the SNIA CIFS Specification "
1230 "e.g. Samba and Windows");
1da177e4
LT
1231MODULE_VERSION(CIFS_VERSION);
1232module_init(init_cifs)
1233module_exit(exit_cifs)