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