1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Common Internet FileSystem (CIFS) client
8 *
9 */
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>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
24 #include <linux/freezer.h>
25 #include <linux/namei.h>
26 #include <linux/random.h>
27 #include <linux/uuid.h>
28 #include <linux/xattr.h>
29 #include <net/ipv6.h>
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>
38 #include <linux/key-type.h>
39 #include "cifs_spnego.h"
40 #include "fscache.h"
41 #include "smb2pdu.h"
42 #ifdef CONFIG_CIFS_DFS_UPCALL
43 #include "dfs_cache.h"
44 #endif
45 #ifdef CONFIG_CIFS_SWN_UPCALL
46 #include "netlink.h"
47 #endif
48 #include "fs_context.h"
49
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
59 int cifsFYI = 0;
60 bool traceSMB;
61 bool enable_oplocks = true;
62 bool linuxExtEnabled = true;
63 bool lookupCacheEnabled = true;
64 bool disable_legacy_dialects; /* false by default */
65 bool enable_gcm_256 = true;
66 bool require_gcm_256; /* false by default */
67 bool enable_negotiate_signing; /* false by default */
68 unsigned int global_secflags = CIFSSEC_DEF;
69 /* unsigned int ntlmv2_support = 0; */
70 unsigned int sign_CIFS_PDUs = 1;
71 static const struct super_operations cifs_super_ops;
72 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
73 module_param(CIFSMaxBufSize, uint, 0444);
74 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
75 "for CIFS requests. "
76 "Default: 16384 Range: 8192 to 130048");
77 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
78 module_param(cifs_min_rcv, uint, 0444);
79 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
80 "1 to 64");
81 unsigned int cifs_min_small = 30;
82 module_param(cifs_min_small, uint, 0444);
83 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
84 "Range: 2 to 256");
85 unsigned int cifs_max_pending = CIFS_MAX_REQ;
86 module_param(cifs_max_pending, uint, 0444);
87 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
88 "CIFS/SMB1 dialect (N/A for SMB3) "
89 "Default: 32767 Range: 2 to 32767.");
90 #ifdef CONFIG_CIFS_STATS2
91 unsigned int slow_rsp_threshold = 1;
92 module_param(slow_rsp_threshold, uint, 0644);
93 MODULE_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
98 module_param(enable_oplocks, bool, 0644);
99 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
100
101 module_param(enable_gcm_256, bool, 0644);
102 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
103
104 module_param(require_gcm_256, bool, 0644);
105 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
106
107 module_param(enable_negotiate_signing, bool, 0644);
108 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
109
110 module_param(disable_legacy_dialects, bool, 0644);
111 MODULE_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
119 extern mempool_t *cifs_sm_req_poolp;
120 extern mempool_t *cifs_req_poolp;
121 extern mempool_t *cifs_mid_poolp;
122
123 struct workqueue_struct *cifsiod_wq;
124 struct workqueue_struct *decrypt_wq;
125 struct workqueue_struct *fileinfo_put_wq;
126 struct workqueue_struct *cifsoplockd_wq;
127 struct workqueue_struct *deferredclose_wq;
128 __u32 cifs_lock_secret;
129
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 */
136 void
cifs_sb_active(struct super_block * sb)137 cifs_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
145 void
cifs_sb_deactive(struct super_block * sb)146 cifs_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
154 static int
cifs_read_super(struct super_block * sb)155 cifs_read_super(struct super_block *sb)
156 {
157 struct inode *inode;
158 struct cifs_sb_info *cifs_sb;
159 struct cifs_tcon *tcon;
160 struct timespec64 ts;
161 int rc = 0;
162
163 cifs_sb = CIFS_SB(sb);
164 tcon = cifs_sb_master_tcon(cifs_sb);
165
166 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
167 sb->s_flags |= SB_POSIXACL;
168
169 if (tcon->snapshot_time)
170 sb->s_flags |= SB_RDONLY;
171
172 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
173 sb->s_maxbytes = MAX_LFS_FILESIZE;
174 else
175 sb->s_maxbytes = MAX_NON_LFS;
176
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 */
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);
190 sb->s_time_min = ts.tv_sec;
191 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
192 cpu_to_le16(SMB_TIME_MAX), 0);
193 sb->s_time_max = ts.tv_sec;
194 } else {
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);
201 sb->s_time_min = ts.tv_sec;
202 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
203 sb->s_time_max = ts.tv_sec;
204 }
205
206 sb->s_magic = CIFS_MAGIC_NUMBER;
207 sb->s_op = &cifs_super_ops;
208 sb->s_xattr = cifs_xattr_handlers;
209 rc = super_setup_bdi(sb);
210 if (rc)
211 goto out_no_root;
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;
217
218 sb->s_blocksize = CIFS_MAX_MSGSIZE;
219 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
220 inode = cifs_root_iget(sb);
221
222 if (IS_ERR(inode)) {
223 rc = PTR_ERR(inode);
224 goto out_no_root;
225 }
226
227 if (tcon->nocase)
228 sb->s_d_op = &cifs_ci_dentry_ops;
229 else
230 sb->s_d_op = &cifs_dentry_ops;
231
232 sb->s_root = d_make_root(inode);
233 if (!sb->s_root) {
234 rc = -ENOMEM;
235 goto out_no_root;
236 }
237
238 #ifdef CONFIG_CIFS_NFSD_EXPORT
239 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
240 cifs_dbg(FYI, "export ops supported\n");
241 sb->s_export_op = &cifs_export_ops;
242 }
243 #endif /* CONFIG_CIFS_NFSD_EXPORT */
244
245 return 0;
246
247 out_no_root:
248 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
249 return rc;
250 }
251
cifs_kill_sb(struct super_block * sb)252 static void cifs_kill_sb(struct super_block *sb)
253 {
254 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
255 struct cifs_tcon *tcon;
256 struct cached_fid *cfid;
257
258 /*
259 * We ned to release all dentries for the cached directories
260 * before we kill the sb.
261 */
262 if (cifs_sb->root) {
263 dput(cifs_sb->root);
264 cifs_sb->root = NULL;
265 }
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 }
277
278 kill_anon_super(sb);
279 cifs_umount(cifs_sb);
280 }
281
282 static int
cifs_statfs(struct dentry * dentry,struct kstatfs * buf)283 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
284 {
285 struct super_block *sb = dentry->d_sb;
286 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
287 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
288 struct TCP_Server_Info *server = tcon->ses->server;
289 unsigned int xid;
290 int rc = 0;
291
292 xid = get_xid();
293
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
304 buf->f_files = 0; /* undefined */
305 buf->f_ffree = 0; /* unlimited */
306
307 if (server->ops->queryfs)
308 rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
309
310 free_xid(xid);
311 return rc;
312 }
313
cifs_fallocate(struct file * file,int mode,loff_t off,loff_t len)314 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
315 {
316 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
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
cifs_permission(struct user_namespace * mnt_userns,struct inode * inode,int mask)326 static int cifs_permission(struct user_namespace *mnt_userns,
327 struct inode *inode, int mask)
328 {
329 struct cifs_sb_info *cifs_sb;
330
331 cifs_sb = CIFS_SB(inode->i_sb);
332
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
339 on the client (above and beyond ACL on servers) for
340 servers which do not support setting and viewing mode bits,
341 so allowing client to check permissions is useful */
342 return generic_permission(&init_user_ns, inode, mask);
343 }
344
345 static struct kmem_cache *cifs_inode_cachep;
346 static struct kmem_cache *cifs_req_cachep;
347 static struct kmem_cache *cifs_mid_cachep;
348 static struct kmem_cache *cifs_sm_req_cachep;
349 mempool_t *cifs_sm_req_poolp;
350 mempool_t *cifs_req_poolp;
351 mempool_t *cifs_mid_poolp;
352
353 static struct inode *
cifs_alloc_inode(struct super_block * sb)354 cifs_alloc_inode(struct super_block *sb)
355 {
356 struct cifsInodeInfo *cifs_inode;
357 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
358 if (!cifs_inode)
359 return NULL;
360 cifs_inode->cifsAttrs = 0x20; /* default */
361 cifs_inode->time = 0;
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 */
366 cifs_set_oplock_level(cifs_inode, 0);
367 cifs_inode->flags = 0;
368 spin_lock_init(&cifs_inode->writers_lock);
369 cifs_inode->writers = 0;
370 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
371 cifs_inode->server_eof = 0;
372 cifs_inode->uniqueid = 0;
373 cifs_inode->createtime = 0;
374 cifs_inode->epoch = 0;
375 spin_lock_init(&cifs_inode->open_file_lock);
376 generate_random_uuid(cifs_inode->lease_key);
377
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; */
383 INIT_LIST_HEAD(&cifs_inode->openFileList);
384 INIT_LIST_HEAD(&cifs_inode->llist);
385 INIT_LIST_HEAD(&cifs_inode->deferred_closes);
386 spin_lock_init(&cifs_inode->deferred_lock);
387 return &cifs_inode->vfs_inode;
388 }
389
390 static void
cifs_free_inode(struct inode * inode)391 cifs_free_inode(struct inode *inode)
392 {
393 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
394 }
395
396 static void
cifs_evict_inode(struct inode * inode)397 cifs_evict_inode(struct inode *inode)
398 {
399 truncate_inode_pages_final(&inode->i_data);
400 clear_inode(inode);
401 }
402
403 static void
cifs_show_address(struct seq_file * s,struct TCP_Server_Info * server)404 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
405 {
406 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
407 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
408
409 seq_puts(s, ",addr=");
410
411 switch (server->dstaddr.ss_family) {
412 case AF_INET:
413 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
414 break;
415 case AF_INET6:
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);
419 break;
420 default:
421 seq_puts(s, "(unknown)");
422 }
423 if (server->rdma)
424 seq_puts(s, ",rdma");
425 }
426
427 static void
cifs_show_security(struct seq_file * s,struct cifs_ses * ses)428 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
429 {
430 if (ses->sectype == Unspecified) {
431 if (ses->user_name == NULL)
432 seq_puts(s, ",sec=none");
433 return;
434 }
435
436 seq_puts(s, ",sec=");
437
438 switch (ses->sectype) {
439 case NTLMv2:
440 seq_puts(s, "ntlmv2");
441 break;
442 case Kerberos:
443 seq_puts(s, "krb5");
444 break;
445 case RawNTLMSSP:
446 seq_puts(s, "ntlmssp");
447 break;
448 default:
449 /* shouldn't ever happen */
450 seq_puts(s, "unknown");
451 break;
452 }
453
454 if (ses->sign)
455 seq_puts(s, "i");
456
457 if (ses->sectype == Kerberos)
458 seq_printf(s, ",cruid=%u",
459 from_kuid_munged(&init_user_ns, ses->cred_uid));
460 }
461
462 static void
cifs_show_cache_flavor(struct seq_file * s,struct cifs_sb_info * cifs_sb)463 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
464 {
465 seq_puts(s, ",cache=");
466
467 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
468 seq_puts(s, "strict");
469 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
470 seq_puts(s, "none");
471 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
472 seq_puts(s, "singleclient"); /* assume only one client access */
473 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
474 seq_puts(s, "ro"); /* read only caching assumed */
475 else
476 seq_puts(s, "loose");
477 }
478
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 */
cifs_show_devname(struct seq_file * m,struct dentry * root)483 static int cifs_show_devname(struct seq_file *m, struct dentry *root)
484 {
485 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
486 char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
487
488 if (devname == NULL)
489 seq_puts(m, "none");
490 else {
491 convert_delimiter(devname, '/');
492 /* escape all spaces in share names */
493 seq_escape(m, devname, " \t");
494 kfree(devname);
495 }
496 return 0;
497 }
498
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 */
504 static int
cifs_show_options(struct seq_file * s,struct dentry * root)505 cifs_show_options(struct seq_file *s, struct dentry *root)
506 {
507 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
508 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
509 struct sockaddr *srcaddr;
510 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
511
512 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
513 cifs_show_security(s, tcon->ses);
514 cifs_show_cache_flavor(s, cifs_sb);
515
516 if (tcon->no_lease)
517 seq_puts(s, ",nolease");
518 if (cifs_sb->ctx->multiuser)
519 seq_puts(s, ",multiuser");
520 else if (tcon->ses->user_name)
521 seq_show_option(s, "username", tcon->ses->user_name);
522
523 if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
524 seq_show_option(s, "domain", tcon->ses->domainName);
525
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
542 seq_printf(s, ",uid=%u",
543 from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
544 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
545 seq_puts(s, ",forceuid");
546 else
547 seq_puts(s, ",noforceuid");
548
549 seq_printf(s, ",gid=%u",
550 from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
551 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
552 seq_puts(s, ",forcegid");
553 else
554 seq_puts(s, ",noforcegid");
555
556 cifs_show_address(s, tcon->ses->server);
557
558 if (!tcon->unix_ext)
559 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
560 cifs_sb->ctx->file_mode,
561 cifs_sb->ctx->dir_mode);
562 if (cifs_sb->ctx->iocharset)
563 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
564 if (tcon->seal)
565 seq_puts(s, ",seal");
566 else if (tcon->ses->server->ignore_signature)
567 seq_puts(s, ",signloosely");
568 if (tcon->nocase)
569 seq_puts(s, ",nocase");
570 if (tcon->nodelete)
571 seq_puts(s, ",nodelete");
572 if (tcon->local_lease)
573 seq_puts(s, ",locallease");
574 if (tcon->retry)
575 seq_puts(s, ",hard");
576 else
577 seq_puts(s, ",soft");
578 if (tcon->use_persistent)
579 seq_puts(s, ",persistenthandles");
580 else if (tcon->use_resilient)
581 seq_puts(s, ",resilienthandles");
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");
588 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
589 seq_puts(s, ",nodfs");
590 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
591 seq_puts(s, ",posixpaths");
592 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
593 seq_puts(s, ",setuids");
594 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
595 seq_puts(s, ",idsfromsid");
596 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
597 seq_puts(s, ",serverino");
598 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
599 seq_puts(s, ",rwpidforward");
600 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
601 seq_puts(s, ",forcemand");
602 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
603 seq_puts(s, ",nouser_xattr");
604 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
605 seq_puts(s, ",mapchars");
606 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
607 seq_puts(s, ",mapposix");
608 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
609 seq_puts(s, ",sfu");
610 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
611 seq_puts(s, ",nobrl");
612 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
613 seq_puts(s, ",nohandlecache");
614 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
615 seq_puts(s, ",modefromsid");
616 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
617 seq_puts(s, ",cifsacl");
618 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
619 seq_puts(s, ",dynperm");
620 if (root->d_sb->s_flags & SB_POSIXACL)
621 seq_puts(s, ",acl");
622 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
623 seq_puts(s, ",mfsymlinks");
624 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
625 seq_puts(s, ",fsc");
626 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
627 seq_puts(s, ",nostrictsync");
628 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
629 seq_puts(s, ",noperm");
630 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
631 seq_printf(s, ",backupuid=%u",
632 from_kuid_munged(&init_user_ns,
633 cifs_sb->ctx->backupuid));
634 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
635 seq_printf(s, ",backupgid=%u",
636 from_kgid_munged(&init_user_ns,
637 cifs_sb->ctx->backupgid));
638
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);
642 if (cifs_sb->ctx->rasize)
643 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
644 if (tcon->ses->server->min_offload)
645 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
646 seq_printf(s, ",echo_interval=%lu",
647 tcon->ses->server->echo_interval / HZ);
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
653 if (tcon->snapshot_time)
654 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
655 if (tcon->handle_timeout)
656 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
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 }
669
670 if (tcon->ses->chan_max > 1)
671 seq_printf(s, ",multichannel,max_channels=%zu",
672 tcon->ses->chan_max);
673
674 if (tcon->use_witness)
675 seq_puts(s, ",witness");
676
677 return 0;
678 }
679
cifs_umount_begin(struct super_block * sb)680 static void cifs_umount_begin(struct super_block *sb)
681 {
682 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
683 struct cifs_tcon *tcon;
684
685 if (cifs_sb == NULL)
686 return;
687
688 tcon = cifs_sb_master_tcon(cifs_sb);
689
690 spin_lock(&cifs_tcp_ses_lock);
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 */
695 spin_unlock(&cifs_tcp_ses_lock);
696 return;
697 } else if (tcon->tc_count == 1)
698 tcon->tidStatus = CifsExiting;
699 spin_unlock(&cifs_tcp_ses_lock);
700
701 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
702 /* cancel_notify_requests(tcon); */
703 if (tcon->ses && tcon->ses->server) {
704 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
705 wake_up_all(&tcon->ses->server->request_q);
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);
711 }
712
713 return;
714 }
715
716 #ifdef CONFIG_CIFS_STATS2
cifs_show_stats(struct seq_file * s,struct dentry * root)717 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
718 {
719 /* BB FIXME */
720 return 0;
721 }
722 #endif
723
cifs_drop_inode(struct inode * inode)724 static int cifs_drop_inode(struct inode *inode)
725 {
726 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
727
728 /* no serverino => unconditional eviction */
729 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
730 generic_drop_inode(inode);
731 }
732
733 static const struct super_operations cifs_super_ops = {
734 .statfs = cifs_statfs,
735 .alloc_inode = cifs_alloc_inode,
736 .free_inode = cifs_free_inode,
737 .drop_inode = cifs_drop_inode,
738 .evict_inode = cifs_evict_inode,
739 /* .show_path = cifs_show_path, */ /* Would we ever need show path? */
740 .show_devname = cifs_show_devname,
741 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
742 function unless later we add lazy close of inodes or unless the
743 kernel forgets to call us with the same number of releases (closes)
744 as opens */
745 .show_options = cifs_show_options,
746 .umount_begin = cifs_umount_begin,
747 #ifdef CONFIG_CIFS_STATS2
748 .show_stats = cifs_show_stats,
749 #endif
750 };
751
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 */
756 static struct dentry *
cifs_get_root(struct smb3_fs_context * ctx,struct super_block * sb)757 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
758 {
759 struct dentry *dentry;
760 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
761 char *full_path = NULL;
762 char *s, *p;
763 char sep;
764
765 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
766 return dget(sb->s_root);
767
768 full_path = cifs_build_path_to_root(ctx, cifs_sb,
769 cifs_sb_master_tcon(cifs_sb), 0);
770 if (full_path == NULL)
771 return ERR_PTR(-ENOMEM);
772
773 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
774
775 sep = CIFS_DIR_SEP(cifs_sb);
776 dentry = dget(sb->s_root);
777 p = s = full_path;
778
779 do {
780 struct inode *dir = d_inode(dentry);
781 struct dentry *child;
782
783 if (!S_ISDIR(dir->i_mode)) {
784 dput(dentry);
785 dentry = ERR_PTR(-ENOTDIR);
786 break;
787 }
788
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
799 child = lookup_positive_unlocked(p, dentry, s - p);
800 dput(dentry);
801 dentry = child;
802 } while (!IS_ERR(dentry));
803 kfree(full_path);
804 return dentry;
805 }
806
cifs_set_super(struct super_block * sb,void * data)807 static 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
814 struct dentry *
cifs_smb3_do_mount(struct file_system_type * fs_type,int flags,struct smb3_fs_context * old_ctx)815 cifs_smb3_do_mount(struct file_system_type *fs_type,
816 int flags, struct smb3_fs_context *old_ctx)
817 {
818 int rc;
819 struct super_block *sb;
820 struct cifs_sb_info *cifs_sb = NULL;
821 struct cifs_mnt_data mnt_data;
822 struct dentry *root;
823
824 /*
825 * Prints in Kernel / CIFS log the attempted mount operation
826 * If CIFS_DEBUG && cifs_FYI
827 */
828 if (cifsFYI)
829 cifs_dbg(FYI, "Devname: %s flags: %d\n", old_ctx->UNC, flags);
830 else
831 cifs_info("Attempting to mount %s\n", old_ctx->UNC);
832
833 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
834 if (cifs_sb == NULL) {
835 root = ERR_PTR(-ENOMEM);
836 goto out;
837 }
838
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);
845 if (rc) {
846 root = ERR_PTR(rc);
847 goto out;
848 }
849
850 rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
851 if (rc) {
852 root = ERR_PTR(rc);
853 goto out;
854 }
855
856 rc = cifs_setup_cifs_sb(cifs_sb);
857 if (rc) {
858 root = ERR_PTR(rc);
859 goto out;
860 }
861
862 rc = cifs_mount(cifs_sb, cifs_sb->ctx);
863 if (rc) {
864 if (!(flags & SB_SILENT))
865 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
866 rc);
867 root = ERR_PTR(rc);
868 goto out;
869 }
870
871 mnt_data.ctx = cifs_sb->ctx;
872 mnt_data.cifs_sb = cifs_sb;
873 mnt_data.flags = flags;
874
875 /* BB should we make this contingent on mount parm? */
876 flags |= SB_NODIRATIME | SB_NOATIME;
877
878 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
879 if (IS_ERR(sb)) {
880 root = ERR_CAST(sb);
881 cifs_umount(cifs_sb);
882 cifs_sb = NULL;
883 goto out;
884 }
885
886 if (sb->s_root) {
887 cifs_dbg(FYI, "Use existing superblock\n");
888 cifs_umount(cifs_sb);
889 cifs_sb = NULL;
890 } else {
891 rc = cifs_read_super(sb);
892 if (rc) {
893 root = ERR_PTR(rc);
894 goto out_super;
895 }
896
897 sb->s_flags |= SB_ACTIVE;
898 }
899
900 root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
901 if (IS_ERR(root))
902 goto out_super;
903
904 if (cifs_sb)
905 cifs_sb->root = dget(root);
906
907 cifs_dbg(FYI, "dentry root is: %p\n", root);
908 return root;
909
910 out_super:
911 deactivate_locked_super(sb);
912 out:
913 if (cifs_sb) {
914 kfree(cifs_sb->prepath);
915 smb3_cleanup_fs_context(cifs_sb->ctx);
916 kfree(cifs_sb);
917 }
918 return root;
919 }
920
921
922 static ssize_t
cifs_loose_read_iter(struct kiocb * iocb,struct iov_iter * iter)923 cifs_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
928 if (iocb->ki_filp->f_flags & O_DIRECT)
929 return cifs_user_readv(iocb, iter);
930
931 rc = cifs_revalidate_mapping(inode);
932 if (rc)
933 return rc;
934
935 return generic_file_read_iter(iocb, iter);
936 }
937
cifs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)938 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
939 {
940 struct inode *inode = file_inode(iocb->ki_filp);
941 struct cifsInodeInfo *cinode = CIFS_I(inode);
942 ssize_t written;
943 int rc;
944
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
957 written = cifs_get_writer(cinode);
958 if (written)
959 return written;
960
961 written = generic_file_write_iter(iocb, from);
962
963 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
964 goto out;
965
966 rc = filemap_fdatawrite(inode->i_mapping);
967 if (rc)
968 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
969 rc, inode);
970
971 out:
972 cifs_put_writer(cinode);
973 return written;
974 }
975
cifs_llseek(struct file * file,loff_t offset,int whence)976 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
977 {
978 struct cifsFileInfo *cfile = file->private_data;
979 struct cifs_tcon *tcon;
980
981 /*
982 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
983 * the cached file length
984 */
985 if (whence != SEEK_SET && whence != SEEK_CUR) {
986 int rc;
987 struct inode *inode = file_inode(file);
988
989 /*
990 * We need to be sure that all dirty pages are written and the
991 * server has the newest file length.
992 */
993 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
994 inode->i_mapping->nrpages != 0) {
995 rc = filemap_fdatawait(inode->i_mapping);
996 if (rc) {
997 mapping_set_error(inode->i_mapping, rc);
998 return rc;
999 }
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;
1011 }
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 }
1018 return generic_file_llseek(file, offset, whence);
1019 }
1020
1021 static int
cifs_setlease(struct file * file,long arg,struct file_lock ** lease,void ** priv)1022 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
1023 {
1024 /*
1025 * Note that this is called by vfs setlease with i_lock held to
1026 * protect *lease from going away.
1027 */
1028 struct inode *inode = file_inode(file);
1029 struct cifsFileInfo *cfile = file->private_data;
1030
1031 if (!(S_ISREG(inode->i_mode)))
1032 return -EINVAL;
1033
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))) ||
1037 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1038 return generic_setlease(file, arg, lease, priv);
1039 else if (tlink_tcon(cfile->tlink)->local_lease &&
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 */
1049 return generic_setlease(file, arg, lease, priv);
1050 else
1051 return -EAGAIN;
1052 }
1053
1054 struct file_system_type cifs_fs_type = {
1055 .owner = THIS_MODULE,
1056 .name = "cifs",
1057 .init_fs_context = smb3_init_fs_context,
1058 .parameters = smb3_fs_parameters,
1059 .kill_sb = cifs_kill_sb,
1060 .fs_flags = FS_RENAME_DOES_D_MOVE,
1061 };
1062 MODULE_ALIAS_FS("cifs");
1063
1064 static struct file_system_type smb3_fs_type = {
1065 .owner = THIS_MODULE,
1066 .name = "smb3",
1067 .init_fs_context = smb3_init_fs_context,
1068 .parameters = smb3_fs_parameters,
1069 .kill_sb = cifs_kill_sb,
1070 .fs_flags = FS_RENAME_DOES_D_MOVE,
1071 };
1072 MODULE_ALIAS_FS("smb3");
1073 MODULE_ALIAS("smb3");
1074
1075 const struct inode_operations cifs_dir_inode_ops = {
1076 .create = cifs_create,
1077 .atomic_open = cifs_atomic_open,
1078 .lookup = cifs_lookup,
1079 .getattr = cifs_getattr,
1080 .unlink = cifs_unlink,
1081 .link = cifs_hardlink,
1082 .mkdir = cifs_mkdir,
1083 .rmdir = cifs_rmdir,
1084 .rename = cifs_rename2,
1085 .permission = cifs_permission,
1086 .setattr = cifs_setattr,
1087 .symlink = cifs_symlink,
1088 .mknod = cifs_mknod,
1089 .listxattr = cifs_listxattr,
1090 };
1091
1092 const struct inode_operations cifs_file_inode_ops = {
1093 .setattr = cifs_setattr,
1094 .getattr = cifs_getattr,
1095 .permission = cifs_permission,
1096 .listxattr = cifs_listxattr,
1097 .fiemap = cifs_fiemap,
1098 };
1099
1100 const struct inode_operations cifs_symlink_inode_ops = {
1101 .get_link = cifs_get_link,
1102 .permission = cifs_permission,
1103 .listxattr = cifs_listxattr,
1104 };
1105
cifs_remap_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,loff_t len,unsigned int remap_flags)1106 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1107 struct file *dst_file, loff_t destoff, loff_t len,
1108 unsigned int remap_flags)
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;
1113 struct cifsFileInfo *smb_file_target;
1114 struct cifs_tcon *target_tcon;
1115 unsigned int xid;
1116 int rc;
1117
1118 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1119 return -EINVAL;
1120
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
1131 smb_file_target = dst_file->private_data;
1132 target_tcon = tlink_tcon(smb_file_target->tlink);
1133
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,
1147 PAGE_ALIGN(destoff + len)-1);
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;
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);
1161 out:
1162 free_xid(xid);
1163 return rc < 0 ? rc : len;
1164 }
1165
cifs_file_copychunk_range(unsigned int xid,struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,size_t len,unsigned int flags)1166 ssize_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
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
1198 rc = -EOPNOTSUPP;
1199 if (!target_tcon->ses->server->ops->copychunk_range)
1200 goto out;
1201
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
1213 rc = file_modified(dst_file);
1214 if (!rc)
1215 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1216 smb_file_src, smb_file_target, off, len, destoff);
1217
1218 file_accessed(src_file);
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
1229 out:
1230 return rc;
1231 }
1232
1233 /*
1234 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1235 * is a dummy operation.
1236 */
cifs_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1237 static 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
cifs_copy_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,size_t len,unsigned int flags)1245 static 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;
1251 struct cifsFileInfo *cfile = dst_file->private_data;
1252
1253 if (cfile->swapfile)
1254 return -EOPNOTSUPP;
1255
1256 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1257 len, flags);
1258 free_xid(xid);
1259
1260 if (rc == -EOPNOTSUPP || rc == -EXDEV)
1261 rc = generic_copy_file_range(src_file, off, dst_file,
1262 destoff, len, flags);
1263 return rc;
1264 }
1265
1266 const struct file_operations cifs_file_ops = {
1267 .read_iter = cifs_loose_read_iter,
1268 .write_iter = cifs_file_write_iter,
1269 .open = cifs_open,
1270 .release = cifs_close,
1271 .lock = cifs_lock,
1272 .flock = cifs_flock,
1273 .fsync = cifs_fsync,
1274 .flush = cifs_flush,
1275 .mmap = cifs_file_mmap,
1276 .splice_read = generic_file_splice_read,
1277 .splice_write = iter_file_splice_write,
1278 .llseek = cifs_llseek,
1279 .unlocked_ioctl = cifs_ioctl,
1280 .copy_file_range = cifs_copy_file_range,
1281 .remap_file_range = cifs_remap_file_range,
1282 .setlease = cifs_setlease,
1283 .fallocate = cifs_fallocate,
1284 };
1285
1286 const struct file_operations cifs_file_strict_ops = {
1287 .read_iter = cifs_strict_readv,
1288 .write_iter = cifs_strict_writev,
1289 .open = cifs_open,
1290 .release = cifs_close,
1291 .lock = cifs_lock,
1292 .flock = cifs_flock,
1293 .fsync = cifs_strict_fsync,
1294 .flush = cifs_flush,
1295 .mmap = cifs_file_strict_mmap,
1296 .splice_read = generic_file_splice_read,
1297 .splice_write = iter_file_splice_write,
1298 .llseek = cifs_llseek,
1299 .unlocked_ioctl = cifs_ioctl,
1300 .copy_file_range = cifs_copy_file_range,
1301 .remap_file_range = cifs_remap_file_range,
1302 .setlease = cifs_setlease,
1303 .fallocate = cifs_fallocate,
1304 };
1305
1306 const struct file_operations cifs_file_direct_ops = {
1307 .read_iter = cifs_direct_readv,
1308 .write_iter = cifs_direct_writev,
1309 .open = cifs_open,
1310 .release = cifs_close,
1311 .lock = cifs_lock,
1312 .flock = cifs_flock,
1313 .fsync = cifs_fsync,
1314 .flush = cifs_flush,
1315 .mmap = cifs_file_mmap,
1316 .splice_read = generic_file_splice_read,
1317 .splice_write = iter_file_splice_write,
1318 .unlocked_ioctl = cifs_ioctl,
1319 .copy_file_range = cifs_copy_file_range,
1320 .remap_file_range = cifs_remap_file_range,
1321 .llseek = cifs_llseek,
1322 .setlease = cifs_setlease,
1323 .fallocate = cifs_fallocate,
1324 };
1325
1326 const struct file_operations cifs_file_nobrl_ops = {
1327 .read_iter = cifs_loose_read_iter,
1328 .write_iter = cifs_file_write_iter,
1329 .open = cifs_open,
1330 .release = cifs_close,
1331 .fsync = cifs_fsync,
1332 .flush = cifs_flush,
1333 .mmap = cifs_file_mmap,
1334 .splice_read = generic_file_splice_read,
1335 .splice_write = iter_file_splice_write,
1336 .llseek = cifs_llseek,
1337 .unlocked_ioctl = cifs_ioctl,
1338 .copy_file_range = cifs_copy_file_range,
1339 .remap_file_range = cifs_remap_file_range,
1340 .setlease = cifs_setlease,
1341 .fallocate = cifs_fallocate,
1342 };
1343
1344 const struct file_operations cifs_file_strict_nobrl_ops = {
1345 .read_iter = cifs_strict_readv,
1346 .write_iter = cifs_strict_writev,
1347 .open = cifs_open,
1348 .release = cifs_close,
1349 .fsync = cifs_strict_fsync,
1350 .flush = cifs_flush,
1351 .mmap = cifs_file_strict_mmap,
1352 .splice_read = generic_file_splice_read,
1353 .splice_write = iter_file_splice_write,
1354 .llseek = cifs_llseek,
1355 .unlocked_ioctl = cifs_ioctl,
1356 .copy_file_range = cifs_copy_file_range,
1357 .remap_file_range = cifs_remap_file_range,
1358 .setlease = cifs_setlease,
1359 .fallocate = cifs_fallocate,
1360 };
1361
1362 const struct file_operations cifs_file_direct_nobrl_ops = {
1363 .read_iter = cifs_direct_readv,
1364 .write_iter = cifs_direct_writev,
1365 .open = cifs_open,
1366 .release = cifs_close,
1367 .fsync = cifs_fsync,
1368 .flush = cifs_flush,
1369 .mmap = cifs_file_mmap,
1370 .splice_read = generic_file_splice_read,
1371 .splice_write = iter_file_splice_write,
1372 .unlocked_ioctl = cifs_ioctl,
1373 .copy_file_range = cifs_copy_file_range,
1374 .remap_file_range = cifs_remap_file_range,
1375 .llseek = cifs_llseek,
1376 .setlease = cifs_setlease,
1377 .fallocate = cifs_fallocate,
1378 };
1379
1380 const struct file_operations cifs_dir_ops = {
1381 .iterate_shared = cifs_readdir,
1382 .release = cifs_closedir,
1383 .read = generic_read_dir,
1384 .unlocked_ioctl = cifs_ioctl,
1385 .copy_file_range = cifs_copy_file_range,
1386 .remap_file_range = cifs_remap_file_range,
1387 .llseek = generic_file_llseek,
1388 .fsync = cifs_dir_fsync,
1389 };
1390
1391 static void
cifs_init_once(void * inode)1392 cifs_init_once(void *inode)
1393 {
1394 struct cifsInodeInfo *cifsi = inode;
1395
1396 inode_init_once(&cifsi->vfs_inode);
1397 init_rwsem(&cifsi->lock_sem);
1398 }
1399
1400 static int __init
cifs_init_inodecache(void)1401 cifs_init_inodecache(void)
1402 {
1403 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1404 sizeof(struct cifsInodeInfo),
1405 0, (SLAB_RECLAIM_ACCOUNT|
1406 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1407 cifs_init_once);
1408 if (cifs_inode_cachep == NULL)
1409 return -ENOMEM;
1410
1411 return 0;
1412 }
1413
1414 static void
cifs_destroy_inodecache(void)1415 cifs_destroy_inodecache(void)
1416 {
1417 /*
1418 * Make sure all delayed rcu free inodes are flushed before we
1419 * destroy cache.
1420 */
1421 rcu_barrier();
1422 kmem_cache_destroy(cifs_inode_cachep);
1423 }
1424
1425 static int
cifs_init_request_bufs(void)1426 cifs_init_request_bufs(void)
1427 {
1428 /*
1429 * SMB2 maximum header size is bigger than CIFS one - no problems to
1430 * allocate some more bytes for CIFS.
1431 */
1432 size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1433
1434 if (CIFSMaxBufSize < 8192) {
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 }
1443 /*
1444 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1445 CIFSMaxBufSize, CIFSMaxBufSize);
1446 */
1447 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1448 CIFSMaxBufSize + max_hdr_size, 0,
1449 SLAB_HWCACHE_ALIGN, 0,
1450 CIFSMaxBufSize + max_hdr_size,
1451 NULL);
1452 if (cifs_req_cachep == NULL)
1453 return -ENOMEM;
1454
1455 if (cifs_min_rcv < 1)
1456 cifs_min_rcv = 1;
1457 else if (cifs_min_rcv > 64) {
1458 cifs_min_rcv = 64;
1459 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1460 }
1461
1462 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1463 cifs_req_cachep);
1464
1465 if (cifs_req_poolp == NULL) {
1466 kmem_cache_destroy(cifs_req_cachep);
1467 return -ENOMEM;
1468 }
1469 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1470 almost all handle based requests (but not write response, nor is it
1471 sufficient for path based requests). A smaller size would have
1472 been more efficient (compacting multiple slab items on one 4k page)
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
1475 efficient to alloc 1 per page off the slab compared to 17K (5page)
1476 alloc of large cifs buffers even when page debugging is on */
1477 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1478 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1479 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1480 if (cifs_sm_req_cachep == NULL) {
1481 mempool_destroy(cifs_req_poolp);
1482 kmem_cache_destroy(cifs_req_cachep);
1483 return -ENOMEM;
1484 }
1485
1486 if (cifs_min_small < 2)
1487 cifs_min_small = 2;
1488 else if (cifs_min_small > 256) {
1489 cifs_min_small = 256;
1490 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1491 }
1492
1493 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1494 cifs_sm_req_cachep);
1495
1496 if (cifs_sm_req_poolp == NULL) {
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
1506 static void
cifs_destroy_request_bufs(void)1507 cifs_destroy_request_bufs(void)
1508 {
1509 mempool_destroy(cifs_req_poolp);
1510 kmem_cache_destroy(cifs_req_cachep);
1511 mempool_destroy(cifs_sm_req_poolp);
1512 kmem_cache_destroy(cifs_sm_req_cachep);
1513 }
1514
1515 static int
cifs_init_mids(void)1516 cifs_init_mids(void)
1517 {
1518 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1519 sizeof(struct mid_q_entry), 0,
1520 SLAB_HWCACHE_ALIGN, NULL);
1521 if (cifs_mid_cachep == NULL)
1522 return -ENOMEM;
1523
1524 /* 3 is a reasonable minimum number of simultaneous operations */
1525 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1526 if (cifs_mid_poolp == NULL) {
1527 kmem_cache_destroy(cifs_mid_cachep);
1528 return -ENOMEM;
1529 }
1530
1531 return 0;
1532 }
1533
1534 static void
cifs_destroy_mids(void)1535 cifs_destroy_mids(void)
1536 {
1537 mempool_destroy(cifs_mid_poolp);
1538 kmem_cache_destroy(cifs_mid_cachep);
1539 }
1540
1541 static int __init
init_cifs(void)1542 init_cifs(void)
1543 {
1544 int rc = 0;
1545 cifs_proc_init();
1546 INIT_LIST_HEAD(&cifs_tcp_ses_list);
1547 /*
1548 * Initialize Global counters
1549 */
1550 atomic_set(&sesInfoAllocCount, 0);
1551 atomic_set(&tconInfoAllocCount, 0);
1552 atomic_set(&tcpSesNextId, 0);
1553 atomic_set(&tcpSesAllocCount, 0);
1554 atomic_set(&tcpSesReconnectCount, 0);
1555 atomic_set(&tconInfoReconnectCount, 0);
1556
1557 atomic_set(&bufAllocCount, 0);
1558 atomic_set(&smBufAllocCount, 0);
1559 #ifdef CONFIG_CIFS_STATS2
1560 atomic_set(&totBufAllocCount, 0);
1561 atomic_set(&totSmBufAllocCount, 0);
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");
1567 #endif /* CONFIG_CIFS_STATS2 */
1568
1569 atomic_set(&midCount, 0);
1570 GlobalCurrentXid = 0;
1571 GlobalTotalActiveXid = 0;
1572 GlobalMaxActiveXid = 0;
1573 spin_lock_init(&cifs_tcp_ses_lock);
1574 spin_lock_init(&GlobalMid_Lock);
1575
1576 cifs_lock_secret = get_random_u32();
1577
1578 if (cifs_max_pending < 2) {
1579 cifs_max_pending = 2;
1580 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1581 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1582 cifs_max_pending = CIFS_MAX_REQ;
1583 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1584 CIFS_MAX_REQ);
1585 }
1586
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
1593 /*
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
1596 * Documentation/core-api/workqueue.rst recommends setting it to 0
1597 */
1598
1599 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1600 decrypt_wq = alloc_workqueue("smb3decryptd",
1601 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1602 if (!decrypt_wq) {
1603 rc = -ENOMEM;
1604 goto out_destroy_cifsiod_wq;
1605 }
1606
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
1614 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1615 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1616 if (!cifsoplockd_wq) {
1617 rc = -ENOMEM;
1618 goto out_destroy_fileinfo_put_wq;
1619 }
1620
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
1628 rc = cifs_fscache_register();
1629 if (rc)
1630 goto out_destroy_deferredclose_wq;
1631
1632 rc = cifs_init_inodecache();
1633 if (rc)
1634 goto out_unreg_fscache;
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
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 */
1649 #ifdef CONFIG_CIFS_UPCALL
1650 rc = init_cifs_spnego();
1651 if (rc)
1652 goto out_destroy_dfs_cache;
1653 #endif /* CONFIG_CIFS_UPCALL */
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 */
1659
1660 rc = init_cifs_idmap();
1661 if (rc)
1662 goto out_cifs_swn_init;
1663
1664 rc = register_filesystem(&cifs_fs_type);
1665 if (rc)
1666 goto out_init_cifs_idmap;
1667
1668 rc = register_filesystem(&smb3_fs_type);
1669 if (rc) {
1670 unregister_filesystem(&cifs_fs_type);
1671 goto out_init_cifs_idmap;
1672 }
1673
1674 return 0;
1675
1676 out_init_cifs_idmap:
1677 exit_cifs_idmap();
1678 out_cifs_swn_init:
1679 #ifdef CONFIG_CIFS_SWN_UPCALL
1680 cifs_genl_exit();
1681 out_register_key_type:
1682 #endif
1683 #ifdef CONFIG_CIFS_UPCALL
1684 exit_cifs_spnego();
1685 out_destroy_dfs_cache:
1686 #endif
1687 #ifdef CONFIG_CIFS_DFS_UPCALL
1688 dfs_cache_destroy();
1689 out_destroy_request_bufs:
1690 #endif
1691 cifs_destroy_request_bufs();
1692 out_destroy_mids:
1693 cifs_destroy_mids();
1694 out_destroy_inodecache:
1695 cifs_destroy_inodecache();
1696 out_unreg_fscache:
1697 cifs_fscache_unregister();
1698 out_destroy_deferredclose_wq:
1699 destroy_workqueue(deferredclose_wq);
1700 out_destroy_cifsoplockd_wq:
1701 destroy_workqueue(cifsoplockd_wq);
1702 out_destroy_fileinfo_put_wq:
1703 destroy_workqueue(fileinfo_put_wq);
1704 out_destroy_decrypt_wq:
1705 destroy_workqueue(decrypt_wq);
1706 out_destroy_cifsiod_wq:
1707 destroy_workqueue(cifsiod_wq);
1708 out_clean_proc:
1709 cifs_proc_clean();
1710 return rc;
1711 }
1712
1713 static void __exit
exit_cifs(void)1714 exit_cifs(void)
1715 {
1716 cifs_dbg(NOISY, "exit_smb3\n");
1717 unregister_filesystem(&cifs_fs_type);
1718 unregister_filesystem(&smb3_fs_type);
1719 cifs_dfs_release_automount_timer();
1720 exit_cifs_idmap();
1721 #ifdef CONFIG_CIFS_SWN_UPCALL
1722 cifs_genl_exit();
1723 #endif
1724 #ifdef CONFIG_CIFS_UPCALL
1725 exit_cifs_spnego();
1726 #endif
1727 #ifdef CONFIG_CIFS_DFS_UPCALL
1728 dfs_cache_destroy();
1729 #endif
1730 cifs_destroy_request_bufs();
1731 cifs_destroy_mids();
1732 cifs_destroy_inodecache();
1733 cifs_fscache_unregister();
1734 destroy_workqueue(deferredclose_wq);
1735 destroy_workqueue(cifsoplockd_wq);
1736 destroy_workqueue(decrypt_wq);
1737 destroy_workqueue(fileinfo_put_wq);
1738 destroy_workqueue(cifsiod_wq);
1739 cifs_proc_clean();
1740 }
1741
1742 MODULE_AUTHOR("Steve French");
1743 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1744 MODULE_DESCRIPTION
1745 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1746 "also older servers complying with the SNIA CIFS Specification)");
1747 MODULE_VERSION(CIFS_VERSION);
1748 MODULE_SOFTDEP("ecb");
1749 MODULE_SOFTDEP("hmac");
1750 MODULE_SOFTDEP("md5");
1751 MODULE_SOFTDEP("nls");
1752 MODULE_SOFTDEP("aes");
1753 MODULE_SOFTDEP("cmac");
1754 MODULE_SOFTDEP("sha256");
1755 MODULE_SOFTDEP("sha512");
1756 MODULE_SOFTDEP("aead2");
1757 MODULE_SOFTDEP("ccm");
1758 MODULE_SOFTDEP("gcm");
1759 module_init(init_cifs)
1760 module_exit(exit_cifs)
1761