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/filelock.h> 16 #include <linux/mount.h> 17 #include <linux/slab.h> 18 #include <linux/init.h> 19 #include <linux/list.h> 20 #include <linux/seq_file.h> 21 #include <linux/vfs.h> 22 #include <linux/mempool.h> 23 #include <linux/delay.h> 24 #include <linux/kthread.h> 25 #include <linux/freezer.h> 26 #include <linux/namei.h> 27 #include <linux/random.h> 28 #include <linux/splice.h> 29 #include <linux/uuid.h> 30 #include <linux/xattr.h> 31 #include <linux/mm.h> 32 #include <linux/key-type.h> 33 #include <uapi/linux/magic.h> 34 #include <net/ipv6.h> 35 #include "cifsfs.h" 36 #define DECLARE_GLOBALS_HERE 37 #include "cifsglob.h" 38 #include "cifsproto.h" 39 #include "smb2proto.h" 40 #include "cifs_debug.h" 41 #include "cifs_fs_sb.h" 42 #include "cifs_spnego.h" 43 #include "fscache.h" 44 #ifdef CONFIG_CIFS_DFS_UPCALL 45 #include "dfs_cache.h" 46 #endif 47 #ifdef CONFIG_CIFS_SWN_UPCALL 48 #include "netlink.h" 49 #endif 50 #include "fs_context.h" 51 #include "cached_dir.h" 52 53 /* 54 * DOS dates from 1980/1/1 through 2107/12/31 55 * Protocol specifications indicate the range should be to 119, which 56 * limits maximum year to 2099. But this range has not been checked. 57 */ 58 #define SMB_DATE_MAX (127<<9 | 12<<5 | 31) 59 #define SMB_DATE_MIN (0<<9 | 1<<5 | 1) 60 #define SMB_TIME_MAX (23<<11 | 59<<5 | 29) 61 62 int cifsFYI = 0; 63 bool traceSMB; 64 bool enable_oplocks = true; 65 bool linuxExtEnabled = true; 66 bool lookupCacheEnabled = true; 67 bool disable_legacy_dialects; /* false by default */ 68 bool enable_gcm_256 = true; 69 bool require_gcm_256; /* false by default */ 70 bool enable_negotiate_signing; /* false by default */ 71 unsigned int global_secflags = CIFSSEC_DEF; 72 /* unsigned int ntlmv2_support = 0; */ 73 74 /* 75 * Global transaction id (XID) information 76 */ 77 unsigned int GlobalCurrentXid; /* protected by GlobalMid_Lock */ 78 unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Lock */ 79 unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Lock */ 80 DEFINE_SPINLOCK(GlobalMid_Lock); /* protects above & list operations on midQ entries */ 81 82 /* 83 * Global counters, updated atomically 84 */ 85 atomic_t sesInfoAllocCount; 86 atomic_t tconInfoAllocCount; 87 atomic_t tcpSesNextId; 88 atomic_t tcpSesAllocCount; 89 atomic_t tcpSesReconnectCount; 90 atomic_t tconInfoReconnectCount; 91 92 atomic_t mid_count; 93 atomic_t buf_alloc_count; 94 atomic_t small_buf_alloc_count; 95 #ifdef CONFIG_CIFS_STATS2 96 atomic_t total_buf_alloc_count; 97 atomic_t total_small_buf_alloc_count; 98 #endif/* STATS2 */ 99 struct list_head cifs_tcp_ses_list; 100 DEFINE_SPINLOCK(cifs_tcp_ses_lock); 101 static const struct super_operations cifs_super_ops; 102 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE; 103 module_param(CIFSMaxBufSize, uint, 0444); 104 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) " 105 "for CIFS requests. " 106 "Default: 16384 Range: 8192 to 130048"); 107 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL; 108 module_param(cifs_min_rcv, uint, 0444); 109 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: " 110 "1 to 64"); 111 unsigned int cifs_min_small = 30; 112 module_param(cifs_min_small, uint, 0444); 113 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 " 114 "Range: 2 to 256"); 115 unsigned int cifs_max_pending = CIFS_MAX_REQ; 116 module_param(cifs_max_pending, uint, 0444); 117 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for " 118 "CIFS/SMB1 dialect (N/A for SMB3) " 119 "Default: 32767 Range: 2 to 32767."); 120 unsigned int dir_cache_timeout = 30; 121 module_param(dir_cache_timeout, uint, 0644); 122 MODULE_PARM_DESC(dir_cache_timeout, "Number of seconds to cache directory contents for which we have a lease. Default: 30 " 123 "Range: 1 to 65000 seconds, 0 to disable caching dir contents"); 124 /* Module-wide total cached dirents (in bytes) across all tcons */ 125 atomic64_t cifs_dircache_bytes_used = ATOMIC64_INIT(0); 126 127 atomic_t cifs_sillycounter; 128 atomic_t cifs_tmpcounter; 129 130 #ifdef CONFIG_CIFS_STATS2 131 unsigned int slow_rsp_threshold = 1; 132 module_param(slow_rsp_threshold, uint, 0644); 133 MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait " 134 "before logging that a response is delayed. " 135 "Default: 1 (if set to 0 disables msg)."); 136 #endif /* STATS2 */ 137 138 module_param(enable_oplocks, bool, 0644); 139 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1"); 140 141 module_param(enable_gcm_256, bool, 0644); 142 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: y/Y/1"); 143 144 module_param(require_gcm_256, bool, 0644); 145 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0"); 146 147 module_param(enable_negotiate_signing, bool, 0644); 148 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0"); 149 150 module_param(disable_legacy_dialects, bool, 0644); 151 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be " 152 "helpful to restrict the ability to " 153 "override the default dialects (SMB2.1, " 154 "SMB3 and SMB3.02) on mount with old " 155 "dialects (CIFS/SMB1 and SMB2) since " 156 "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker" 157 " and less secure. Default: n/N/0"); 158 159 struct workqueue_struct *cifsiod_wq; 160 struct workqueue_struct *decrypt_wq; 161 struct workqueue_struct *fileinfo_put_wq; 162 struct workqueue_struct *cifsoplockd_wq; 163 struct workqueue_struct *deferredclose_wq; 164 struct workqueue_struct *serverclose_wq; 165 struct workqueue_struct *cfid_put_wq; 166 __u32 cifs_lock_secret; 167 168 /* 169 * Bumps refcount for cifs super block. 170 * Note that it should be only called if a reference to VFS super block is 171 * already held, e.g. in open-type syscalls context. Otherwise it can race with 172 * atomic_dec_and_test in deactivate_locked_super. 173 */ 174 void 175 cifs_sb_active(struct super_block *sb) 176 { 177 struct cifs_sb_info *server = CIFS_SB(sb); 178 179 if (atomic_inc_return(&server->active) == 1) 180 atomic_inc(&sb->s_active); 181 } 182 183 void 184 cifs_sb_deactive(struct super_block *sb) 185 { 186 struct cifs_sb_info *server = CIFS_SB(sb); 187 188 if (atomic_dec_and_test(&server->active)) 189 deactivate_super(sb); 190 } 191 192 static int 193 cifs_read_super(struct super_block *sb) 194 { 195 struct cifs_sb_info *cifs_sb; 196 struct cifs_tcon *tcon; 197 unsigned int sbflags; 198 struct timespec64 ts; 199 struct inode *inode; 200 int rc = 0; 201 202 cifs_sb = CIFS_SB(sb); 203 tcon = cifs_sb_master_tcon(cifs_sb); 204 sbflags = cifs_sb_flags(cifs_sb); 205 206 if (sbflags & CIFS_MOUNT_POSIXACL) 207 sb->s_flags |= SB_POSIXACL; 208 209 if (tcon->snapshot_time) 210 sb->s_flags |= SB_RDONLY; 211 212 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files) 213 sb->s_maxbytes = MAX_LFS_FILESIZE; 214 else 215 sb->s_maxbytes = MAX_NON_LFS; 216 217 /* 218 * Some very old servers like DOS and OS/2 used 2 second granularity 219 * (while all current servers use 100ns granularity - see MS-DTYP) 220 * but 1 second is the maximum allowed granularity for the VFS 221 * so for old servers set time granularity to 1 second while for 222 * everything else (current servers) set it to 100ns. 223 */ 224 if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) && 225 ((tcon->ses->capabilities & 226 tcon->ses->server->vals->cap_nt_find) == 0) && 227 !tcon->unix_ext) { 228 sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */ 229 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0); 230 sb->s_time_min = ts.tv_sec; 231 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX), 232 cpu_to_le16(SMB_TIME_MAX), 0); 233 sb->s_time_max = ts.tv_sec; 234 } else { 235 /* 236 * Almost every server, including all SMB2+, uses DCE TIME 237 * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC 238 */ 239 sb->s_time_gran = 100; 240 ts = cifs_NTtimeToUnix(0); 241 sb->s_time_min = ts.tv_sec; 242 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX)); 243 sb->s_time_max = ts.tv_sec; 244 } 245 246 sb->s_magic = CIFS_SUPER_MAGIC; 247 sb->s_op = &cifs_super_ops; 248 sb->s_xattr = cifs_xattr_handlers; 249 rc = super_setup_bdi(sb); 250 if (rc) 251 goto out_no_root; 252 /* tune readahead according to rsize if readahead size not set on mount */ 253 if (cifs_sb->ctx->rsize == 0) 254 cifs_sb->ctx->rsize = 255 tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx); 256 if (cifs_sb->ctx->rasize) 257 sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE; 258 else 259 sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE); 260 261 sb->s_blocksize = CIFS_MAX_MSGSIZE; 262 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */ 263 inode = cifs_root_iget(sb); 264 265 if (IS_ERR(inode)) { 266 rc = PTR_ERR(inode); 267 goto out_no_root; 268 } 269 270 if (tcon->nocase) 271 set_default_d_op(sb, &cifs_ci_dentry_ops); 272 else 273 set_default_d_op(sb, &cifs_dentry_ops); 274 275 sb->s_root = d_make_root(inode); 276 if (!sb->s_root) { 277 rc = -ENOMEM; 278 goto out_no_root; 279 } 280 281 #ifdef CONFIG_CIFS_NFSD_EXPORT 282 if (sbflags & CIFS_MOUNT_SERVER_INUM) { 283 cifs_dbg(FYI, "export ops supported\n"); 284 sb->s_export_op = &cifs_export_ops; 285 } 286 #endif /* CONFIG_CIFS_NFSD_EXPORT */ 287 288 return 0; 289 290 out_no_root: 291 cifs_dbg(VFS, "%s: get root inode failed\n", __func__); 292 return rc; 293 } 294 295 static void cifs_kill_sb(struct super_block *sb) 296 { 297 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 298 299 /* 300 * We need to release all dentries for the cached directories 301 * and close all deferred file handles before we kill the sb. 302 */ 303 if (cifs_sb->root) { 304 close_all_cached_dirs(cifs_sb); 305 cifs_close_all_deferred_files_sb(cifs_sb); 306 307 /* Wait for all pending oplock breaks to complete */ 308 flush_workqueue(cifsoplockd_wq); 309 310 /* finally release root dentry */ 311 dput(cifs_sb->root); 312 cifs_sb->root = NULL; 313 } 314 315 kill_anon_super(sb); 316 cifs_umount(cifs_sb); 317 } 318 319 static int 320 cifs_statfs(struct dentry *dentry, struct kstatfs *buf) 321 { 322 struct super_block *sb = dentry->d_sb; 323 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 324 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 325 struct TCP_Server_Info *server = tcon->ses->server; 326 unsigned int xid; 327 int rc = 0; 328 const char *full_path; 329 void *page; 330 331 xid = get_xid(); 332 page = alloc_dentry_path(); 333 334 full_path = build_path_from_dentry(dentry, page); 335 if (IS_ERR(full_path)) { 336 rc = PTR_ERR(full_path); 337 goto statfs_out; 338 } 339 340 if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0) 341 buf->f_namelen = 342 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength); 343 else 344 buf->f_namelen = PATH_MAX; 345 346 buf->f_fsid.val[0] = tcon->vol_serial_number; 347 /* are using part of create time for more randomness, see man statfs */ 348 buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time); 349 350 buf->f_files = 0; /* undefined */ 351 buf->f_ffree = 0; /* unlimited */ 352 353 if (server->ops->queryfs) 354 rc = server->ops->queryfs(xid, tcon, full_path, cifs_sb, buf); 355 356 statfs_out: 357 free_dentry_path(page); 358 free_xid(xid); 359 return rc; 360 } 361 362 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len) 363 { 364 struct cifs_tcon *tcon = cifs_sb_master_tcon(CIFS_SB(file)); 365 struct TCP_Server_Info *server = tcon->ses->server; 366 struct inode *inode = file_inode(file); 367 int rc; 368 369 if (!server->ops->fallocate) 370 return -EOPNOTSUPP; 371 372 rc = inode_lock_killable(inode); 373 if (rc) 374 return rc; 375 376 netfs_wait_for_outstanding_io(inode); 377 378 rc = file_modified(file); 379 if (rc) 380 goto out_unlock; 381 382 rc = server->ops->fallocate(file, tcon, mode, off, len); 383 384 out_unlock: 385 inode_unlock(inode); 386 return rc; 387 } 388 389 static int cifs_permission(struct mnt_idmap *idmap, 390 struct inode *inode, int mask) 391 { 392 unsigned int sbflags = cifs_sb_flags(CIFS_SB(inode)); 393 394 if (sbflags & CIFS_MOUNT_NO_PERM) { 395 if ((mask & MAY_EXEC) && !execute_ok(inode)) 396 return -EACCES; 397 else 398 return 0; 399 } else /* file mode might have been restricted at mount time 400 on the client (above and beyond ACL on servers) for 401 servers which do not support setting and viewing mode bits, 402 so allowing client to check permissions is useful */ 403 return generic_permission(&nop_mnt_idmap, inode, mask); 404 } 405 406 static struct kmem_cache *cifs_inode_cachep; 407 static struct kmem_cache *cifs_req_cachep; 408 static struct kmem_cache *cifs_mid_cachep; 409 static struct kmem_cache *cifs_sm_req_cachep; 410 static struct kmem_cache *cifs_io_request_cachep; 411 static struct kmem_cache *cifs_io_subrequest_cachep; 412 mempool_t *cifs_sm_req_poolp; 413 mempool_t *cifs_req_poolp; 414 mempool_t cifs_mid_pool; 415 mempool_t cifs_io_request_pool; 416 mempool_t cifs_io_subrequest_pool; 417 418 static struct inode * 419 cifs_alloc_inode(struct super_block *sb) 420 { 421 struct cifsInodeInfo *cifs_inode; 422 cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL); 423 if (!cifs_inode) 424 return NULL; 425 cifs_inode->cifsAttrs = ATTR_ARCHIVE; /* default */ 426 cifs_inode->time = 0; 427 /* 428 * Until the file is open and we have gotten oplock info back from the 429 * server, can not assume caching of file data or metadata. 430 */ 431 cifs_set_oplock_level(cifs_inode, 0); 432 cifs_inode->lease_granted = false; 433 cifs_inode->flags = 0; 434 spin_lock_init(&cifs_inode->writers_lock); 435 cifs_inode->writers = 0; 436 cifs_inode->netfs.inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */ 437 cifs_inode->netfs._remote_i_size = 0; 438 cifs_inode->netfs._zero_point = 0; 439 cifs_inode->uniqueid = 0; 440 cifs_inode->createtime = 0; 441 cifs_inode->epoch = 0; 442 spin_lock_init(&cifs_inode->open_file_lock); 443 generate_random_uuid(cifs_inode->lease_key); 444 cifs_inode->symlink_target = NULL; 445 446 /* 447 * Can not set i_flags here - they get immediately overwritten to zero 448 * by the VFS. 449 */ 450 /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */ 451 INIT_LIST_HEAD(&cifs_inode->openFileList); 452 INIT_LIST_HEAD(&cifs_inode->llist); 453 INIT_LIST_HEAD(&cifs_inode->deferred_closes); 454 spin_lock_init(&cifs_inode->deferred_lock); 455 return &cifs_inode->netfs.inode; 456 } 457 458 static void 459 cifs_free_inode(struct inode *inode) 460 { 461 struct cifsInodeInfo *cinode = CIFS_I(inode); 462 463 if (S_ISLNK(inode->i_mode)) 464 kfree(cinode->symlink_target); 465 kmem_cache_free(cifs_inode_cachep, cinode); 466 } 467 468 static void 469 cifs_evict_inode(struct inode *inode) 470 { 471 netfs_wait_for_outstanding_io(inode); 472 truncate_inode_pages_final(&inode->i_data); 473 if (inode_state_read_once(inode) & I_PINNING_NETFS_WB) 474 cifs_fscache_unuse_inode_cookie(inode, true); 475 cifs_fscache_release_inode_cookie(inode); 476 clear_inode(inode); 477 } 478 479 static void 480 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server) 481 { 482 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr; 483 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr; 484 485 seq_puts(s, ",addr="); 486 487 switch (server->dstaddr.ss_family) { 488 case AF_INET: 489 seq_printf(s, "%pI4", &sa->sin_addr.s_addr); 490 break; 491 case AF_INET6: 492 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr); 493 if (sa6->sin6_scope_id) 494 seq_printf(s, "%%%u", sa6->sin6_scope_id); 495 break; 496 default: 497 seq_puts(s, "(unknown)"); 498 } 499 if (server->rdma) 500 seq_puts(s, ",rdma"); 501 } 502 503 static void 504 cifs_show_security(struct seq_file *s, struct cifs_ses *ses) 505 { 506 if (ses->sectype == Unspecified) { 507 if (ses->user_name == NULL) 508 seq_puts(s, ",sec=none"); 509 return; 510 } 511 512 seq_puts(s, ",sec="); 513 514 switch (ses->sectype) { 515 case NTLMv2: 516 seq_puts(s, "ntlmv2"); 517 break; 518 case Kerberos: 519 seq_puts(s, "krb5"); 520 break; 521 case RawNTLMSSP: 522 seq_puts(s, "ntlmssp"); 523 break; 524 default: 525 /* shouldn't ever happen */ 526 seq_puts(s, "unknown"); 527 break; 528 } 529 530 if (ses->sign) 531 seq_puts(s, "i"); 532 533 if (ses->sectype == Kerberos) 534 seq_printf(s, ",cruid=%u", 535 from_kuid_munged(&init_user_ns, ses->cred_uid)); 536 } 537 538 static void 539 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb) 540 { 541 unsigned int sbflags = cifs_sb_flags(cifs_sb); 542 543 seq_puts(s, ",cache="); 544 545 if (sbflags & CIFS_MOUNT_STRICT_IO) 546 seq_puts(s, "strict"); 547 else if (sbflags & CIFS_MOUNT_DIRECT_IO) 548 seq_puts(s, "none"); 549 else if (sbflags & CIFS_MOUNT_RW_CACHE) 550 seq_puts(s, "singleclient"); /* assume only one client access */ 551 else if (sbflags & CIFS_MOUNT_RO_CACHE) 552 seq_puts(s, "ro"); /* read only caching assumed */ 553 else 554 seq_puts(s, "loose"); 555 } 556 557 /* 558 * cifs_show_devname() is used so we show the mount device name with correct 559 * format (e.g. forward slashes vs. back slashes) in /proc/mounts 560 */ 561 static int cifs_show_devname(struct seq_file *m, struct dentry *root) 562 { 563 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb); 564 char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL); 565 566 if (devname == NULL) 567 seq_puts(m, "none"); 568 else { 569 convert_delimiter(devname, '/'); 570 /* escape all spaces in share names */ 571 seq_escape(m, devname, " \t"); 572 kfree(devname); 573 } 574 return 0; 575 } 576 577 static void 578 cifs_show_upcall_target(struct seq_file *s, struct cifs_sb_info *cifs_sb) 579 { 580 if (cifs_sb->ctx->upcall_target == UPTARGET_UNSPECIFIED) { 581 seq_puts(s, ",upcall_target=app"); 582 return; 583 } 584 585 seq_puts(s, ",upcall_target="); 586 587 switch (cifs_sb->ctx->upcall_target) { 588 case UPTARGET_APP: 589 seq_puts(s, "app"); 590 break; 591 case UPTARGET_MOUNT: 592 seq_puts(s, "mount"); 593 break; 594 default: 595 /* shouldn't ever happen */ 596 seq_puts(s, "unknown"); 597 break; 598 } 599 } 600 601 /* 602 * cifs_show_options() is for displaying mount options in /proc/mounts. 603 * Not all settable options are displayed but most of the important 604 * ones are. 605 */ 606 static int 607 cifs_show_options(struct seq_file *s, struct dentry *root) 608 { 609 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb); 610 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 611 struct sockaddr *srcaddr; 612 unsigned int sbflags; 613 614 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr; 615 616 seq_show_option(s, "vers", tcon->ses->server->vals->version_string); 617 cifs_show_security(s, tcon->ses); 618 cifs_show_cache_flavor(s, cifs_sb); 619 cifs_show_upcall_target(s, cifs_sb); 620 621 if (tcon->no_lease) 622 seq_puts(s, ",nolease"); 623 if (cifs_sb->ctx->multiuser) 624 seq_puts(s, ",multiuser"); 625 else if (tcon->ses->user_name) 626 seq_show_option(s, "username", tcon->ses->user_name); 627 628 if (tcon->ses->domainName && tcon->ses->domainName[0] != 0) 629 seq_show_option(s, "domain", tcon->ses->domainName); 630 631 if (srcaddr->sa_family != AF_UNSPEC) { 632 struct sockaddr_in *saddr4; 633 struct sockaddr_in6 *saddr6; 634 saddr4 = (struct sockaddr_in *)srcaddr; 635 saddr6 = (struct sockaddr_in6 *)srcaddr; 636 if (srcaddr->sa_family == AF_INET6) 637 seq_printf(s, ",srcaddr=%pI6c", 638 &saddr6->sin6_addr); 639 else if (srcaddr->sa_family == AF_INET) 640 seq_printf(s, ",srcaddr=%pI4", 641 &saddr4->sin_addr.s_addr); 642 else 643 seq_printf(s, ",srcaddr=BAD-AF:%i", 644 (int)(srcaddr->sa_family)); 645 } 646 647 sbflags = cifs_sb_flags(cifs_sb); 648 seq_printf(s, ",uid=%u", 649 from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid)); 650 if (sbflags & CIFS_MOUNT_OVERR_UID) 651 seq_puts(s, ",forceuid"); 652 else 653 seq_puts(s, ",noforceuid"); 654 655 seq_printf(s, ",gid=%u", 656 from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid)); 657 if (sbflags & CIFS_MOUNT_OVERR_GID) 658 seq_puts(s, ",forcegid"); 659 else 660 seq_puts(s, ",noforcegid"); 661 662 cifs_show_address(s, tcon->ses->server); 663 664 if (!tcon->unix_ext) 665 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho", 666 cifs_sb->ctx->file_mode, 667 cifs_sb->ctx->dir_mode); 668 if (cifs_sb->ctx->iocharset) 669 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset); 670 if (tcon->ses->unicode == 0) 671 seq_puts(s, ",nounicode"); 672 else if (tcon->ses->unicode == 1) 673 seq_puts(s, ",unicode"); 674 if (tcon->seal) 675 seq_puts(s, ",seal"); 676 else if (tcon->ses->server->ignore_signature) 677 seq_puts(s, ",signloosely"); 678 if (tcon->nocase) 679 seq_puts(s, ",nocase"); 680 if (tcon->nodelete) 681 seq_puts(s, ",nodelete"); 682 if (cifs_sb->ctx->no_sparse) 683 seq_puts(s, ",nosparse"); 684 if (tcon->local_lease) 685 seq_puts(s, ",locallease"); 686 if (tcon->retry) 687 seq_puts(s, ",hard"); 688 else 689 seq_puts(s, ",soft"); 690 if (tcon->use_persistent) 691 seq_puts(s, ",persistenthandles"); 692 else if (tcon->use_resilient) 693 seq_puts(s, ",resilienthandles"); 694 if (tcon->posix_extensions) 695 seq_puts(s, ",posix"); 696 else if (tcon->unix_ext) 697 seq_puts(s, ",unix"); 698 else 699 seq_puts(s, ",nounix"); 700 if (sbflags & CIFS_MOUNT_NO_DFS) 701 seq_puts(s, ",nodfs"); 702 if (sbflags & CIFS_MOUNT_POSIX_PATHS) 703 seq_puts(s, ",posixpaths"); 704 if (sbflags & CIFS_MOUNT_SET_UID) 705 seq_puts(s, ",setuids"); 706 if (sbflags & CIFS_MOUNT_UID_FROM_ACL) 707 seq_puts(s, ",idsfromsid"); 708 if (sbflags & CIFS_MOUNT_SERVER_INUM) 709 seq_puts(s, ",serverino"); 710 if (sbflags & CIFS_MOUNT_RWPIDFORWARD) 711 seq_puts(s, ",rwpidforward"); 712 if (sbflags & CIFS_MOUNT_NOPOSIXBRL) 713 seq_puts(s, ",forcemand"); 714 if (sbflags & CIFS_MOUNT_NO_XATTR) 715 seq_puts(s, ",nouser_xattr"); 716 if (sbflags & CIFS_MOUNT_MAP_SPECIAL_CHR) 717 seq_puts(s, ",mapchars"); 718 if (sbflags & CIFS_MOUNT_MAP_SFM_CHR) 719 seq_puts(s, ",mapposix"); 720 if (sbflags & CIFS_MOUNT_UNX_EMUL) 721 seq_puts(s, ",sfu"); 722 if (sbflags & CIFS_MOUNT_NO_BRL) 723 seq_puts(s, ",nobrl"); 724 if (sbflags & CIFS_MOUNT_NO_HANDLE_CACHE) 725 seq_puts(s, ",nohandlecache"); 726 if (sbflags & CIFS_MOUNT_MODE_FROM_SID) 727 seq_puts(s, ",modefromsid"); 728 if (sbflags & CIFS_MOUNT_CIFS_ACL) 729 seq_puts(s, ",cifsacl"); 730 if (sbflags & CIFS_MOUNT_DYNPERM) 731 seq_puts(s, ",dynperm"); 732 if (root->d_sb->s_flags & SB_POSIXACL) 733 seq_puts(s, ",acl"); 734 if (sbflags & CIFS_MOUNT_MF_SYMLINKS) 735 seq_puts(s, ",mfsymlinks"); 736 if (sbflags & CIFS_MOUNT_FSCACHE) 737 seq_puts(s, ",fsc"); 738 if (sbflags & CIFS_MOUNT_NOSSYNC) 739 seq_puts(s, ",nostrictsync"); 740 if (sbflags & CIFS_MOUNT_NO_PERM) 741 seq_puts(s, ",noperm"); 742 if (sbflags & CIFS_MOUNT_CIFS_BACKUPUID) 743 seq_printf(s, ",backupuid=%u", 744 from_kuid_munged(&init_user_ns, 745 cifs_sb->ctx->backupuid)); 746 if (sbflags & CIFS_MOUNT_CIFS_BACKUPGID) 747 seq_printf(s, ",backupgid=%u", 748 from_kgid_munged(&init_user_ns, 749 cifs_sb->ctx->backupgid)); 750 seq_show_option(s, "reparse", 751 cifs_reparse_type_str(cifs_sb->ctx->reparse_type)); 752 if (cifs_sb->ctx->nonativesocket) 753 seq_puts(s, ",nonativesocket"); 754 else 755 seq_puts(s, ",nativesocket"); 756 seq_show_option(s, "symlink", 757 cifs_symlink_type_str(cifs_symlink_type(cifs_sb))); 758 759 seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize); 760 seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize); 761 seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize); 762 if (cifs_sb->ctx->rasize) 763 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize); 764 if (tcon->ses->server->min_offload) 765 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload); 766 if (tcon->ses->server->retrans) 767 seq_printf(s, ",retrans=%u", tcon->ses->server->retrans); 768 seq_printf(s, ",echo_interval=%lu", 769 tcon->ses->server->echo_interval / HZ); 770 771 /* Only display the following if overridden on mount */ 772 if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE) 773 seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits); 774 if (tcon->ses->server->tcp_nodelay) 775 seq_puts(s, ",tcpnodelay"); 776 if (tcon->ses->server->noautotune) 777 seq_puts(s, ",noautotune"); 778 if (tcon->ses->server->noblocksnd) 779 seq_puts(s, ",noblocksend"); 780 if (tcon->ses->server->nosharesock) 781 seq_puts(s, ",nosharesock"); 782 783 if (tcon->snapshot_time) 784 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time); 785 if (tcon->handle_timeout) 786 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout); 787 if (tcon->max_cached_dirs != MAX_CACHED_FIDS) 788 seq_printf(s, ",max_cached_dirs=%u", tcon->max_cached_dirs); 789 790 /* 791 * Display file and directory attribute timeout in seconds. 792 * If file and directory attribute timeout the same then actimeo 793 * was likely specified on mount 794 */ 795 if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax) 796 seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ); 797 else { 798 seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ); 799 seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ); 800 } 801 seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ); 802 803 if (tcon->ses->chan_max > 1) 804 seq_printf(s, ",multichannel,max_channels=%zu", 805 tcon->ses->chan_max); 806 807 if (tcon->use_witness) 808 seq_puts(s, ",witness"); 809 810 return 0; 811 } 812 813 static void cifs_umount_begin(struct super_block *sb) 814 { 815 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 816 struct cifs_tcon *tcon; 817 818 if (cifs_sb == NULL) 819 return; 820 821 tcon = cifs_sb_master_tcon(cifs_sb); 822 823 spin_lock(&cifs_tcp_ses_lock); 824 spin_lock(&tcon->tc_lock); 825 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 826 netfs_trace_tcon_ref_see_umount); 827 if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) { 828 /* we have other mounts to same share or we have 829 already tried to umount this and woken up 830 all waiting network requests, nothing to do */ 831 spin_unlock(&tcon->tc_lock); 832 spin_unlock(&cifs_tcp_ses_lock); 833 return; 834 } 835 /* 836 * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will 837 * fail later (e.g. due to open files). TID_EXITING will be set just before tdis req sent 838 */ 839 spin_unlock(&tcon->tc_lock); 840 spin_unlock(&cifs_tcp_ses_lock); 841 842 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */ 843 /* cancel_notify_requests(tcon); */ 844 if (tcon->ses && tcon->ses->server) { 845 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n"); 846 wake_up_all(&tcon->ses->server->request_q); 847 wake_up_all(&tcon->ses->server->response_q); 848 msleep(1); /* yield */ 849 /* we have to kick the requests once more */ 850 wake_up_all(&tcon->ses->server->response_q); 851 msleep(1); 852 } 853 854 return; 855 } 856 857 static int cifs_freeze(struct super_block *sb) 858 { 859 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 860 struct cifs_tcon *tcon; 861 862 if (cifs_sb == NULL) 863 return 0; 864 865 tcon = cifs_sb_master_tcon(cifs_sb); 866 867 cifs_close_all_deferred_files(tcon); 868 return 0; 869 } 870 871 #ifdef CONFIG_CIFS_STATS2 872 static int cifs_show_stats(struct seq_file *s, struct dentry *root) 873 { 874 /* BB FIXME */ 875 return 0; 876 } 877 #endif 878 879 static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc) 880 { 881 return netfs_unpin_writeback(inode, wbc); 882 } 883 884 static int cifs_drop_inode(struct inode *inode) 885 { 886 unsigned int sbflags = cifs_sb_flags(CIFS_SB(inode)); 887 888 /* no serverino => unconditional eviction */ 889 return !(sbflags & CIFS_MOUNT_SERVER_INUM) || 890 inode_generic_drop(inode); 891 } 892 893 static const struct super_operations cifs_super_ops = { 894 .statfs = cifs_statfs, 895 .alloc_inode = cifs_alloc_inode, 896 .write_inode = cifs_write_inode, 897 .free_inode = cifs_free_inode, 898 .drop_inode = cifs_drop_inode, 899 .evict_inode = cifs_evict_inode, 900 /* .show_path = cifs_show_path, */ /* Would we ever need show path? */ 901 .show_devname = cifs_show_devname, 902 /* .delete_inode = cifs_delete_inode, */ /* Do not need above 903 function unless later we add lazy close of inodes or unless the 904 kernel forgets to call us with the same number of releases (closes) 905 as opens */ 906 .show_options = cifs_show_options, 907 .umount_begin = cifs_umount_begin, 908 .freeze_fs = cifs_freeze, 909 #ifdef CONFIG_CIFS_STATS2 910 .show_stats = cifs_show_stats, 911 #endif 912 }; 913 914 /* 915 * Get root dentry from superblock according to prefix path mount option. 916 * Return dentry with refcount + 1 on success and NULL otherwise. 917 */ 918 static struct dentry * 919 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb) 920 { 921 struct dentry *dentry; 922 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 923 char *full_path = NULL; 924 char *s, *p; 925 char sep; 926 927 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_USE_PREFIX_PATH) 928 return dget(sb->s_root); 929 930 full_path = cifs_build_path_to_root(ctx, cifs_sb, 931 cifs_sb_master_tcon(cifs_sb), 0); 932 if (full_path == NULL) 933 return ERR_PTR(-ENOMEM); 934 935 cifs_dbg(FYI, "Get root dentry for %s\n", full_path); 936 937 sep = CIFS_DIR_SEP(cifs_sb); 938 dentry = dget(sb->s_root); 939 s = full_path; 940 941 do { 942 struct inode *dir = d_inode(dentry); 943 struct dentry *child; 944 945 if (!S_ISDIR(dir->i_mode)) { 946 dput(dentry); 947 dentry = ERR_PTR(-ENOTDIR); 948 break; 949 } 950 951 /* skip separators */ 952 while (*s == sep) 953 s++; 954 if (!*s) 955 break; 956 p = s++; 957 /* next separator */ 958 while (*s && *s != sep) 959 s++; 960 961 child = lookup_noperm_positive_unlocked(&QSTR_LEN(p, s - p), 962 dentry); 963 dput(dentry); 964 dentry = child; 965 } while (!IS_ERR(dentry)); 966 kfree(full_path); 967 return dentry; 968 } 969 970 static int cifs_set_super(struct super_block *sb, void *data) 971 { 972 struct cifs_mnt_data *mnt_data = data; 973 sb->s_fs_info = mnt_data->cifs_sb; 974 return set_anon_super(sb, NULL); 975 } 976 977 struct dentry * 978 cifs_smb3_do_mount(struct file_system_type *fs_type, 979 int flags, struct smb3_fs_context *old_ctx) 980 { 981 struct cifs_mnt_data mnt_data; 982 struct cifs_sb_info *cifs_sb; 983 struct super_block *sb; 984 struct dentry *root; 985 int rc; 986 987 if (cifsFYI) { 988 cifs_dbg(FYI, "%s: devname=%s flags=0x%x\n", __func__, 989 old_ctx->source, flags); 990 } else { 991 cifs_info("Attempting to mount %s\n", old_ctx->source); 992 } 993 cifs_sb = kzalloc_obj(*cifs_sb); 994 if (!cifs_sb) 995 return ERR_PTR(-ENOMEM); 996 997 cifs_sb->ctx = kzalloc_obj(struct smb3_fs_context); 998 if (!cifs_sb->ctx) { 999 root = ERR_PTR(-ENOMEM); 1000 goto out; 1001 } 1002 rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx); 1003 if (rc) { 1004 root = ERR_PTR(rc); 1005 goto out; 1006 } 1007 1008 rc = cifs_setup_cifs_sb(cifs_sb); 1009 if (rc) { 1010 root = ERR_PTR(rc); 1011 goto out; 1012 } 1013 1014 rc = cifs_mount(cifs_sb, cifs_sb->ctx); 1015 if (rc) { 1016 if (!(flags & SB_SILENT)) 1017 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n", 1018 rc); 1019 root = ERR_PTR(rc); 1020 goto out; 1021 } 1022 1023 mnt_data.ctx = cifs_sb->ctx; 1024 mnt_data.cifs_sb = cifs_sb; 1025 mnt_data.flags = flags; 1026 1027 /* BB should we make this contingent on mount parm? */ 1028 flags |= SB_NODIRATIME | SB_NOATIME; 1029 1030 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data); 1031 if (IS_ERR(sb)) { 1032 cifs_umount(cifs_sb); 1033 return ERR_CAST(sb); 1034 } 1035 1036 if (sb->s_root) { 1037 cifs_dbg(FYI, "Use existing superblock\n"); 1038 cifs_umount(cifs_sb); 1039 cifs_sb = NULL; 1040 } else { 1041 rc = cifs_read_super(sb); 1042 if (rc) { 1043 root = ERR_PTR(rc); 1044 goto out_super; 1045 } 1046 1047 sb->s_flags |= SB_ACTIVE; 1048 } 1049 1050 root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb); 1051 if (IS_ERR(root)) 1052 goto out_super; 1053 1054 if (cifs_sb) 1055 cifs_sb->root = dget(root); 1056 1057 cifs_dbg(FYI, "dentry root is: %p\n", root); 1058 return root; 1059 1060 out_super: 1061 deactivate_locked_super(sb); 1062 return root; 1063 out: 1064 kfree(cifs_sb->prepath); 1065 smb3_cleanup_fs_context(cifs_sb->ctx); 1066 kfree(cifs_sb); 1067 return root; 1068 } 1069 1070 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence) 1071 { 1072 struct cifsFileInfo *cfile = file->private_data; 1073 struct cifs_tcon *tcon; 1074 1075 /* 1076 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 1077 * the cached file length 1078 */ 1079 if (whence != SEEK_SET && whence != SEEK_CUR) { 1080 int rc; 1081 struct inode *inode = file_inode(file); 1082 1083 /* 1084 * We need to be sure that all dirty pages are written and the 1085 * server has the newest file length. 1086 */ 1087 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping && 1088 inode->i_mapping->nrpages != 0) { 1089 rc = filemap_fdatawait(inode->i_mapping); 1090 if (rc) { 1091 mapping_set_error(inode->i_mapping, rc); 1092 return rc; 1093 } 1094 } 1095 /* 1096 * Some applications poll for the file length in this strange 1097 * way so we must seek to end on non-oplocked files by 1098 * setting the revalidate time to zero. 1099 */ 1100 CIFS_I(inode)->time = 0; 1101 1102 rc = cifs_revalidate_file_attr(file); 1103 if (rc < 0) 1104 return (loff_t)rc; 1105 } 1106 if (cfile && cfile->tlink) { 1107 tcon = tlink_tcon(cfile->tlink); 1108 if (tcon->ses->server->ops->llseek) 1109 return tcon->ses->server->ops->llseek(file, tcon, 1110 offset, whence); 1111 } 1112 return generic_file_llseek(file, offset, whence); 1113 } 1114 1115 static int 1116 cifs_setlease(struct file *file, int arg, struct file_lease **lease, void **priv) 1117 { 1118 /* 1119 * Note that this is called by vfs setlease with i_lock held to 1120 * protect *lease from going away. 1121 */ 1122 struct inode *inode = file_inode(file); 1123 struct cifsFileInfo *cfile = file->private_data; 1124 1125 /* Check if file is oplocked if this is request for new lease */ 1126 if (arg == F_UNLCK || 1127 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) || 1128 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode)))) 1129 return generic_setlease(file, arg, lease, priv); 1130 else if (tlink_tcon(cfile->tlink)->local_lease && 1131 !CIFS_CACHE_READ(CIFS_I(inode))) 1132 /* 1133 * If the server claims to support oplock on this file, then we 1134 * still need to check oplock even if the local_lease mount 1135 * option is set, but there are servers which do not support 1136 * oplock for which this mount option may be useful if the user 1137 * knows that the file won't be changed on the server by anyone 1138 * else. 1139 */ 1140 return generic_setlease(file, arg, lease, priv); 1141 else 1142 return -EAGAIN; 1143 } 1144 1145 struct file_system_type cifs_fs_type = { 1146 .owner = THIS_MODULE, 1147 .name = "cifs", 1148 .init_fs_context = smb3_init_fs_context, 1149 .parameters = smb3_fs_parameters, 1150 .kill_sb = cifs_kill_sb, 1151 .fs_flags = FS_RENAME_DOES_D_MOVE, 1152 }; 1153 MODULE_ALIAS_FS("cifs"); 1154 1155 struct file_system_type smb3_fs_type = { 1156 .owner = THIS_MODULE, 1157 .name = "smb3", 1158 .init_fs_context = smb3_init_fs_context, 1159 .parameters = smb3_fs_parameters, 1160 .kill_sb = cifs_kill_sb, 1161 .fs_flags = FS_RENAME_DOES_D_MOVE, 1162 }; 1163 MODULE_ALIAS_FS("smb3"); 1164 MODULE_ALIAS("smb3"); 1165 1166 const struct inode_operations cifs_dir_inode_ops = { 1167 .create = cifs_create, 1168 .atomic_open = cifs_atomic_open, 1169 .tmpfile = cifs_tmpfile, 1170 .lookup = cifs_lookup, 1171 .getattr = cifs_getattr, 1172 .unlink = cifs_unlink, 1173 .link = cifs_hardlink, 1174 .mkdir = cifs_mkdir, 1175 .rmdir = cifs_rmdir, 1176 .rename = cifs_rename2, 1177 .permission = cifs_permission, 1178 .setattr = cifs_setattr, 1179 .symlink = cifs_symlink, 1180 .mknod = cifs_mknod, 1181 .listxattr = cifs_listxattr, 1182 .get_acl = cifs_get_acl, 1183 .set_acl = cifs_set_acl, 1184 }; 1185 1186 const struct inode_operations cifs_file_inode_ops = { 1187 .setattr = cifs_setattr, 1188 .getattr = cifs_getattr, 1189 .permission = cifs_permission, 1190 .listxattr = cifs_listxattr, 1191 .fiemap = cifs_fiemap, 1192 .get_acl = cifs_get_acl, 1193 .set_acl = cifs_set_acl, 1194 }; 1195 1196 const char *cifs_get_link(struct dentry *dentry, struct inode *inode, 1197 struct delayed_call *done) 1198 { 1199 char *target_path; 1200 1201 if (!dentry) 1202 return ERR_PTR(-ECHILD); 1203 1204 target_path = kmalloc(PATH_MAX, GFP_KERNEL); 1205 if (!target_path) 1206 return ERR_PTR(-ENOMEM); 1207 1208 spin_lock(&inode->i_lock); 1209 if (likely(CIFS_I(inode)->symlink_target)) { 1210 strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX); 1211 } else { 1212 kfree(target_path); 1213 target_path = ERR_PTR(-EOPNOTSUPP); 1214 } 1215 spin_unlock(&inode->i_lock); 1216 1217 if (!IS_ERR(target_path)) 1218 set_delayed_call(done, kfree_link, target_path); 1219 1220 return target_path; 1221 } 1222 1223 const struct inode_operations cifs_symlink_inode_ops = { 1224 .get_link = cifs_get_link, 1225 .setattr = cifs_setattr, 1226 .permission = cifs_permission, 1227 .listxattr = cifs_listxattr, 1228 }; 1229 1230 /* 1231 * Advance the EOF marker to after the source range. 1232 */ 1233 static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi, 1234 struct cifs_tcon *src_tcon, 1235 unsigned int xid, loff_t src_end) 1236 { 1237 struct cifsFileInfo *writeable_srcfile; 1238 int rc = -EINVAL; 1239 1240 writeable_srcfile = find_writable_file(src_cifsi, FIND_FSUID_ONLY); 1241 if (writeable_srcfile) { 1242 if (src_tcon->ses->server->ops->set_file_size) 1243 rc = src_tcon->ses->server->ops->set_file_size( 1244 xid, src_tcon, writeable_srcfile, 1245 src_inode->i_size, true /* no need to set sparse */); 1246 else 1247 rc = -ENOSYS; 1248 cifsFileInfo_put(writeable_srcfile); 1249 cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc); 1250 } 1251 1252 if (rc < 0) 1253 goto set_failed; 1254 1255 netfs_resize_file(&src_cifsi->netfs, src_end, true); 1256 fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end); 1257 return 0; 1258 1259 set_failed: 1260 return filemap_write_and_wait(src_inode->i_mapping); 1261 } 1262 1263 /* 1264 * Flush out either the folio that overlaps the beginning of a range in which 1265 * pos resides or the folio that overlaps the end of a range unless that folio 1266 * is entirely within the range we're going to invalidate. We extend the flush 1267 * bounds to encompass the folio. 1268 */ 1269 static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend, 1270 bool first) 1271 { 1272 struct folio *folio; 1273 unsigned long long fpos, fend; 1274 pgoff_t index = pos / PAGE_SIZE; 1275 size_t size; 1276 int rc = 0; 1277 1278 folio = filemap_get_folio(inode->i_mapping, index); 1279 if (IS_ERR(folio)) 1280 return 0; 1281 1282 size = folio_size(folio); 1283 fpos = folio_pos(folio); 1284 fend = fpos + size - 1; 1285 *_fstart = min_t(unsigned long long, *_fstart, fpos); 1286 *_fend = max_t(unsigned long long, *_fend, fend); 1287 if ((first && pos == fpos) || (!first && pos == fend)) 1288 goto out; 1289 1290 rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend); 1291 out: 1292 folio_put(folio); 1293 return rc; 1294 } 1295 1296 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off, 1297 struct file *dst_file, loff_t destoff, loff_t len, 1298 unsigned int remap_flags) 1299 { 1300 struct inode *src_inode = file_inode(src_file); 1301 struct inode *target_inode = file_inode(dst_file); 1302 struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode); 1303 struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode); 1304 struct cifsFileInfo *smb_file_src = src_file->private_data; 1305 struct cifsFileInfo *smb_file_target = dst_file->private_data; 1306 struct cifs_tcon *target_tcon, *src_tcon; 1307 unsigned long long i_size, new_size; 1308 unsigned long long destend, fstart, fend; 1309 unsigned int xid; 1310 int rc; 1311 1312 if (remap_flags & REMAP_FILE_DEDUP) 1313 return -EOPNOTSUPP; 1314 if (remap_flags & ~REMAP_FILE_ADVISORY) 1315 return -EINVAL; 1316 1317 cifs_dbg(FYI, "clone range\n"); 1318 1319 xid = get_xid(); 1320 1321 if (!smb_file_src || !smb_file_target) { 1322 rc = -EBADF; 1323 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n"); 1324 goto out; 1325 } 1326 1327 src_tcon = tlink_tcon(smb_file_src->tlink); 1328 target_tcon = tlink_tcon(smb_file_target->tlink); 1329 1330 /* 1331 * Note: cifs case is easier than btrfs since server responsible for 1332 * checks for proper open modes and file type and if it wants 1333 * server could even support copy of range where source = target 1334 */ 1335 lock_two_nondirectories(target_inode, src_inode); 1336 1337 if (len == 0) 1338 len = src_inode->i_size - off; 1339 1340 cifs_dbg(FYI, "clone range\n"); 1341 1342 /* Flush the source buffer */ 1343 rc = filemap_write_and_wait_range(src_inode->i_mapping, off, 1344 off + len - 1); 1345 if (rc) 1346 goto unlock; 1347 1348 /* The server-side copy will fail if the source crosses the EOF marker. 1349 * Advance the EOF marker after the flush above to the end of the range 1350 * if it's short of that. 1351 */ 1352 if (netfs_read_remote_i_size(src_inode) < off + len) { 1353 rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len); 1354 if (rc < 0) 1355 goto unlock; 1356 } 1357 1358 new_size = destoff + len; 1359 destend = destoff + len - 1; 1360 1361 /* Flush the folios at either end of the destination range to prevent 1362 * accidental loss of dirty data outside of the range. 1363 */ 1364 fstart = destoff; 1365 fend = destend; 1366 1367 rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true); 1368 if (rc) 1369 goto unlock; 1370 rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false); 1371 if (rc) 1372 goto unlock; 1373 1374 spin_lock(&target_inode->i_lock); 1375 if (fend > target_cifsi->netfs._zero_point) 1376 netfs_write_zero_point(target_inode, fend + 1); 1377 i_size = target_inode->i_size; 1378 spin_unlock(&target_inode->i_lock); 1379 1380 /* Discard all the folios that overlap the destination region. */ 1381 cifs_dbg(FYI, "about to discard pages %llx-%llx\n", fstart, fend); 1382 truncate_inode_pages_range(&target_inode->i_data, fstart, fend); 1383 1384 fscache_invalidate(cifs_inode_cookie(target_inode), NULL, i_size, 0); 1385 1386 rc = -EOPNOTSUPP; 1387 if (target_tcon->ses->server->ops->duplicate_extents) { 1388 rc = target_tcon->ses->server->ops->duplicate_extents(xid, 1389 smb_file_src, smb_file_target, off, len, destoff); 1390 if (rc == 0 && new_size > i_size) { 1391 truncate_setsize(target_inode, new_size); 1392 fscache_resize_cookie(cifs_inode_cookie(target_inode), 1393 new_size); 1394 } else if (rc == -EOPNOTSUPP) { 1395 /* 1396 * copy_file_range syscall man page indicates EINVAL 1397 * is returned e.g when "fd_in and fd_out refer to the 1398 * same file and the source and target ranges overlap." 1399 * Test generic/157 was what showed these cases where 1400 * we need to remap EOPNOTSUPP to EINVAL 1401 */ 1402 if (off >= src_inode->i_size) { 1403 rc = -EINVAL; 1404 } else if (src_inode == target_inode) { 1405 if (off + len > destoff) 1406 rc = -EINVAL; 1407 } 1408 } 1409 if (rc == 0) { 1410 spin_lock(&target_inode->i_lock); 1411 if (new_size > target_cifsi->netfs._zero_point) 1412 netfs_write_zero_point(target_inode, new_size); 1413 spin_unlock(&target_inode->i_lock); 1414 } 1415 } 1416 1417 /* force revalidate of size and timestamps of target file now 1418 that target is updated on the server */ 1419 CIFS_I(target_inode)->time = 0; 1420 unlock: 1421 /* although unlocking in the reverse order from locking is not 1422 strictly necessary here it is a little cleaner to be consistent */ 1423 unlock_two_nondirectories(src_inode, target_inode); 1424 out: 1425 free_xid(xid); 1426 return rc < 0 ? rc : len; 1427 } 1428 1429 ssize_t cifs_file_copychunk_range(unsigned int xid, 1430 struct file *src_file, loff_t off, 1431 struct file *dst_file, loff_t destoff, 1432 size_t len, unsigned int flags) 1433 { 1434 struct inode *src_inode = file_inode(src_file); 1435 struct inode *target_inode = file_inode(dst_file); 1436 struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode); 1437 struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode); 1438 struct cifsFileInfo *smb_file_src; 1439 struct cifsFileInfo *smb_file_target; 1440 struct cifs_tcon *src_tcon; 1441 struct cifs_tcon *target_tcon; 1442 ssize_t rc; 1443 1444 cifs_dbg(FYI, "copychunk range\n"); 1445 1446 if (!src_file->private_data || !dst_file->private_data) { 1447 rc = -EBADF; 1448 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n"); 1449 goto out; 1450 } 1451 1452 rc = -EXDEV; 1453 smb_file_target = dst_file->private_data; 1454 smb_file_src = src_file->private_data; 1455 src_tcon = tlink_tcon(smb_file_src->tlink); 1456 target_tcon = tlink_tcon(smb_file_target->tlink); 1457 1458 if (src_tcon->ses != target_tcon->ses) { 1459 cifs_dbg(FYI, "source and target of copy not on same server\n"); 1460 goto out; 1461 } 1462 1463 rc = -EOPNOTSUPP; 1464 if (!target_tcon->ses->server->ops->copychunk_range) 1465 goto out; 1466 1467 /* 1468 * Note: cifs case is easier than btrfs since server responsible for 1469 * checks for proper open modes and file type and if it wants 1470 * server could even support copy of range where source = target 1471 */ 1472 lock_two_nondirectories(target_inode, src_inode); 1473 1474 cifs_dbg(FYI, "about to flush pages\n"); 1475 1476 rc = filemap_write_and_wait_range(src_inode->i_mapping, off, 1477 off + len - 1); 1478 if (rc) 1479 goto unlock; 1480 1481 /* The server-side copy will fail if the source crosses the EOF marker. 1482 * Advance the EOF marker after the flush above to the end of the range 1483 * if it's short of that. 1484 */ 1485 if (netfs_read_remote_i_size(src_inode) < off + len) { 1486 rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len); 1487 if (rc < 0) 1488 goto unlock; 1489 } 1490 1491 /* Flush and invalidate all the folios in the destination region. If 1492 * the copy was successful, then some of the flush is extra overhead, 1493 * but we need to allow for the copy failing in some way (eg. ENOSPC). 1494 */ 1495 rc = filemap_invalidate_inode(target_inode, true, destoff, destoff + len - 1); 1496 if (rc) 1497 goto unlock; 1498 1499 fscache_invalidate(cifs_inode_cookie(target_inode), NULL, 1500 i_size_read(target_inode), 0); 1501 1502 rc = file_modified(dst_file); 1503 if (!rc) { 1504 rc = target_tcon->ses->server->ops->copychunk_range(xid, 1505 smb_file_src, smb_file_target, off, len, destoff); 1506 if (rc > 0 && destoff + rc > i_size_read(target_inode)) { 1507 truncate_setsize(target_inode, destoff + rc); 1508 netfs_resize_file(&target_cifsi->netfs, 1509 i_size_read(target_inode), true); 1510 fscache_resize_cookie(cifs_inode_cookie(target_inode), 1511 i_size_read(target_inode)); 1512 } 1513 if (rc > 0) { 1514 spin_lock(&target_inode->i_lock); 1515 if (destoff + rc > target_cifsi->netfs._zero_point) 1516 netfs_write_zero_point(target_inode, destoff + rc); 1517 spin_unlock(&target_inode->i_lock); 1518 } 1519 } 1520 1521 file_accessed(src_file); 1522 1523 /* force revalidate of size and timestamps of target file now 1524 * that target is updated on the server 1525 */ 1526 CIFS_I(target_inode)->time = 0; 1527 1528 unlock: 1529 /* although unlocking in the reverse order from locking is not 1530 * strictly necessary here it is a little cleaner to be consistent 1531 */ 1532 unlock_two_nondirectories(src_inode, target_inode); 1533 1534 out: 1535 return rc; 1536 } 1537 1538 /* 1539 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync() 1540 * is a dummy operation. 1541 */ 1542 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1543 { 1544 cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n", 1545 file, datasync); 1546 1547 return 0; 1548 } 1549 1550 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off, 1551 struct file *dst_file, loff_t destoff, 1552 size_t len, unsigned int flags) 1553 { 1554 unsigned int xid = get_xid(); 1555 ssize_t rc; 1556 struct cifsFileInfo *cfile = dst_file->private_data; 1557 1558 if (cfile->swapfile) { 1559 rc = -EOPNOTSUPP; 1560 free_xid(xid); 1561 return rc; 1562 } 1563 1564 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff, 1565 len, flags); 1566 free_xid(xid); 1567 1568 if (rc == -EOPNOTSUPP || rc == -EXDEV) 1569 rc = splice_copy_file_range(src_file, off, dst_file, 1570 destoff, len); 1571 return rc; 1572 } 1573 1574 const struct file_operations cifs_file_ops = { 1575 .read_iter = cifs_loose_read_iter, 1576 .write_iter = cifs_file_write_iter, 1577 .open = cifs_open, 1578 .release = cifs_close, 1579 .lock = cifs_lock, 1580 .flock = cifs_flock, 1581 .fsync = cifs_fsync, 1582 .flush = cifs_flush, 1583 .mmap_prepare = cifs_file_mmap_prepare, 1584 .splice_read = filemap_splice_read, 1585 .splice_write = iter_file_splice_write, 1586 .llseek = cifs_llseek, 1587 .unlocked_ioctl = cifs_ioctl, 1588 .copy_file_range = cifs_copy_file_range, 1589 .remap_file_range = cifs_remap_file_range, 1590 .setlease = cifs_setlease, 1591 .fallocate = cifs_fallocate, 1592 }; 1593 1594 const struct file_operations cifs_file_strict_ops = { 1595 .read_iter = cifs_strict_readv, 1596 .write_iter = cifs_strict_writev, 1597 .open = cifs_open, 1598 .release = cifs_close, 1599 .lock = cifs_lock, 1600 .flock = cifs_flock, 1601 .fsync = cifs_strict_fsync, 1602 .flush = cifs_flush, 1603 .mmap_prepare = cifs_file_strict_mmap_prepare, 1604 .splice_read = filemap_splice_read, 1605 .splice_write = iter_file_splice_write, 1606 .llseek = cifs_llseek, 1607 .unlocked_ioctl = cifs_ioctl, 1608 .copy_file_range = cifs_copy_file_range, 1609 .remap_file_range = cifs_remap_file_range, 1610 .setlease = cifs_setlease, 1611 .fallocate = cifs_fallocate, 1612 }; 1613 1614 const struct file_operations cifs_file_direct_ops = { 1615 .read_iter = netfs_unbuffered_read_iter, 1616 .write_iter = netfs_file_write_iter, 1617 .open = cifs_open, 1618 .release = cifs_close, 1619 .lock = cifs_lock, 1620 .flock = cifs_flock, 1621 .fsync = cifs_fsync, 1622 .flush = cifs_flush, 1623 .mmap_prepare = cifs_file_mmap_prepare, 1624 .splice_read = copy_splice_read, 1625 .splice_write = iter_file_splice_write, 1626 .unlocked_ioctl = cifs_ioctl, 1627 .copy_file_range = cifs_copy_file_range, 1628 .remap_file_range = cifs_remap_file_range, 1629 .llseek = cifs_llseek, 1630 .setlease = cifs_setlease, 1631 .fallocate = cifs_fallocate, 1632 }; 1633 1634 const struct file_operations cifs_file_nobrl_ops = { 1635 .read_iter = cifs_loose_read_iter, 1636 .write_iter = cifs_file_write_iter, 1637 .open = cifs_open, 1638 .release = cifs_close, 1639 .fsync = cifs_fsync, 1640 .flush = cifs_flush, 1641 .mmap_prepare = cifs_file_mmap_prepare, 1642 .splice_read = filemap_splice_read, 1643 .splice_write = iter_file_splice_write, 1644 .llseek = cifs_llseek, 1645 .unlocked_ioctl = cifs_ioctl, 1646 .copy_file_range = cifs_copy_file_range, 1647 .remap_file_range = cifs_remap_file_range, 1648 .setlease = cifs_setlease, 1649 .fallocate = cifs_fallocate, 1650 }; 1651 1652 const struct file_operations cifs_file_strict_nobrl_ops = { 1653 .read_iter = cifs_strict_readv, 1654 .write_iter = cifs_strict_writev, 1655 .open = cifs_open, 1656 .release = cifs_close, 1657 .fsync = cifs_strict_fsync, 1658 .flush = cifs_flush, 1659 .mmap_prepare = cifs_file_strict_mmap_prepare, 1660 .splice_read = filemap_splice_read, 1661 .splice_write = iter_file_splice_write, 1662 .llseek = cifs_llseek, 1663 .unlocked_ioctl = cifs_ioctl, 1664 .copy_file_range = cifs_copy_file_range, 1665 .remap_file_range = cifs_remap_file_range, 1666 .setlease = cifs_setlease, 1667 .fallocate = cifs_fallocate, 1668 }; 1669 1670 const struct file_operations cifs_file_direct_nobrl_ops = { 1671 .read_iter = netfs_unbuffered_read_iter, 1672 .write_iter = netfs_file_write_iter, 1673 .open = cifs_open, 1674 .release = cifs_close, 1675 .fsync = cifs_fsync, 1676 .flush = cifs_flush, 1677 .mmap_prepare = cifs_file_mmap_prepare, 1678 .splice_read = copy_splice_read, 1679 .splice_write = iter_file_splice_write, 1680 .unlocked_ioctl = cifs_ioctl, 1681 .copy_file_range = cifs_copy_file_range, 1682 .remap_file_range = cifs_remap_file_range, 1683 .llseek = cifs_llseek, 1684 .setlease = cifs_setlease, 1685 .fallocate = cifs_fallocate, 1686 }; 1687 1688 const struct file_operations cifs_dir_ops = { 1689 .iterate_shared = cifs_readdir, 1690 .release = cifs_closedir, 1691 .read = generic_read_dir, 1692 .unlocked_ioctl = cifs_ioctl, 1693 .copy_file_range = cifs_copy_file_range, 1694 .remap_file_range = cifs_remap_file_range, 1695 .llseek = generic_file_llseek, 1696 .fsync = cifs_dir_fsync, 1697 }; 1698 1699 static void 1700 cifs_init_once(void *inode) 1701 { 1702 struct cifsInodeInfo *cifsi = inode; 1703 1704 inode_init_once(&cifsi->netfs.inode); 1705 init_rwsem(&cifsi->lock_sem); 1706 } 1707 1708 static int __init 1709 cifs_init_inodecache(void) 1710 { 1711 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache", 1712 sizeof(struct cifsInodeInfo), 1713 0, (SLAB_RECLAIM_ACCOUNT| 1714 SLAB_ACCOUNT), 1715 cifs_init_once); 1716 if (cifs_inode_cachep == NULL) 1717 return -ENOMEM; 1718 1719 return 0; 1720 } 1721 1722 static void 1723 cifs_destroy_inodecache(void) 1724 { 1725 /* 1726 * Make sure all delayed rcu free inodes are flushed before we 1727 * destroy cache. 1728 */ 1729 rcu_barrier(); 1730 kmem_cache_destroy(cifs_inode_cachep); 1731 } 1732 1733 static int 1734 cifs_init_request_bufs(void) 1735 { 1736 /* 1737 * SMB2 maximum header size is bigger than CIFS one - no problems to 1738 * allocate some more bytes for CIFS. 1739 */ 1740 size_t max_hdr_size = MAX_SMB2_HDR_SIZE; 1741 1742 if (CIFSMaxBufSize < 8192) { 1743 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum 1744 Unicode path name has to fit in any SMB/CIFS path based frames */ 1745 CIFSMaxBufSize = 8192; 1746 } else if (CIFSMaxBufSize > 1024*127) { 1747 CIFSMaxBufSize = 1024 * 127; 1748 } else { 1749 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/ 1750 } 1751 /* 1752 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n", 1753 CIFSMaxBufSize, CIFSMaxBufSize); 1754 */ 1755 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request", 1756 CIFSMaxBufSize + max_hdr_size, 0, 1757 SLAB_HWCACHE_ALIGN, 0, 1758 CIFSMaxBufSize + max_hdr_size, 1759 NULL); 1760 if (cifs_req_cachep == NULL) 1761 return -ENOMEM; 1762 1763 if (cifs_min_rcv < 1) 1764 cifs_min_rcv = 1; 1765 else if (cifs_min_rcv > 64) { 1766 cifs_min_rcv = 64; 1767 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n"); 1768 } 1769 1770 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv, 1771 cifs_req_cachep); 1772 1773 if (cifs_req_poolp == NULL) { 1774 kmem_cache_destroy(cifs_req_cachep); 1775 return -ENOMEM; 1776 } 1777 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and 1778 almost all handle based requests (but not write response, nor is it 1779 sufficient for path based requests). A smaller size would have 1780 been more efficient (compacting multiple slab items on one 4k page) 1781 for the case in which debug was on, but this larger size allows 1782 more SMBs to use small buffer alloc and is still much more 1783 efficient to alloc 1 per page off the slab compared to 17K (5page) 1784 alloc of large cifs buffers even when page debugging is on */ 1785 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq", 1786 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN, 1787 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL); 1788 if (cifs_sm_req_cachep == NULL) { 1789 mempool_destroy(cifs_req_poolp); 1790 kmem_cache_destroy(cifs_req_cachep); 1791 return -ENOMEM; 1792 } 1793 1794 if (cifs_min_small < 2) 1795 cifs_min_small = 2; 1796 else if (cifs_min_small > 256) { 1797 cifs_min_small = 256; 1798 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n"); 1799 } 1800 1801 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small, 1802 cifs_sm_req_cachep); 1803 1804 if (cifs_sm_req_poolp == NULL) { 1805 mempool_destroy(cifs_req_poolp); 1806 kmem_cache_destroy(cifs_req_cachep); 1807 kmem_cache_destroy(cifs_sm_req_cachep); 1808 return -ENOMEM; 1809 } 1810 1811 return 0; 1812 } 1813 1814 static void 1815 cifs_destroy_request_bufs(void) 1816 { 1817 mempool_destroy(cifs_req_poolp); 1818 kmem_cache_destroy(cifs_req_cachep); 1819 mempool_destroy(cifs_sm_req_poolp); 1820 kmem_cache_destroy(cifs_sm_req_cachep); 1821 } 1822 1823 static int init_mids(void) 1824 { 1825 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids", 1826 sizeof(struct mid_q_entry), 0, 1827 SLAB_HWCACHE_ALIGN, NULL); 1828 if (cifs_mid_cachep == NULL) 1829 return -ENOMEM; 1830 1831 /* 3 is a reasonable minimum number of simultaneous operations */ 1832 if (mempool_init_slab_pool(&cifs_mid_pool, 3, cifs_mid_cachep) < 0) { 1833 kmem_cache_destroy(cifs_mid_cachep); 1834 return -ENOMEM; 1835 } 1836 1837 return 0; 1838 } 1839 1840 static void destroy_mids(void) 1841 { 1842 mempool_exit(&cifs_mid_pool); 1843 kmem_cache_destroy(cifs_mid_cachep); 1844 } 1845 1846 static int cifs_init_netfs(void) 1847 { 1848 cifs_io_request_cachep = 1849 kmem_cache_create("cifs_io_request", 1850 sizeof(struct cifs_io_request), 0, 1851 SLAB_HWCACHE_ALIGN, NULL); 1852 if (!cifs_io_request_cachep) 1853 goto nomem_req; 1854 1855 if (mempool_init_slab_pool(&cifs_io_request_pool, 100, cifs_io_request_cachep) < 0) 1856 goto nomem_reqpool; 1857 1858 cifs_io_subrequest_cachep = 1859 kmem_cache_create("cifs_io_subrequest", 1860 sizeof(struct cifs_io_subrequest), 0, 1861 SLAB_HWCACHE_ALIGN, NULL); 1862 if (!cifs_io_subrequest_cachep) 1863 goto nomem_subreq; 1864 1865 if (mempool_init_slab_pool(&cifs_io_subrequest_pool, 100, cifs_io_subrequest_cachep) < 0) 1866 goto nomem_subreqpool; 1867 1868 return 0; 1869 1870 nomem_subreqpool: 1871 kmem_cache_destroy(cifs_io_subrequest_cachep); 1872 nomem_subreq: 1873 mempool_exit(&cifs_io_request_pool); 1874 nomem_reqpool: 1875 kmem_cache_destroy(cifs_io_request_cachep); 1876 nomem_req: 1877 return -ENOMEM; 1878 } 1879 1880 static void cifs_destroy_netfs(void) 1881 { 1882 mempool_exit(&cifs_io_subrequest_pool); 1883 kmem_cache_destroy(cifs_io_subrequest_cachep); 1884 mempool_exit(&cifs_io_request_pool); 1885 kmem_cache_destroy(cifs_io_request_cachep); 1886 } 1887 1888 static int __init 1889 init_cifs(void) 1890 { 1891 int rc = 0; 1892 1893 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1894 rc = smb1_init_maperror(); 1895 if (rc) 1896 return rc; 1897 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1898 1899 rc = smb2_init_maperror(); 1900 if (rc) 1901 return rc; 1902 1903 cifs_proc_init(); 1904 INIT_LIST_HEAD(&cifs_tcp_ses_list); 1905 /* 1906 * Initialize Global counters 1907 */ 1908 atomic_set(&sesInfoAllocCount, 0); 1909 atomic_set(&tconInfoAllocCount, 0); 1910 atomic_set(&tcpSesNextId, 0); 1911 atomic_set(&tcpSesAllocCount, 0); 1912 atomic_set(&tcpSesReconnectCount, 0); 1913 atomic_set(&tconInfoReconnectCount, 0); 1914 1915 atomic_set(&buf_alloc_count, 0); 1916 atomic_set(&small_buf_alloc_count, 0); 1917 #ifdef CONFIG_CIFS_STATS2 1918 atomic_set(&total_buf_alloc_count, 0); 1919 atomic_set(&total_small_buf_alloc_count, 0); 1920 if (slow_rsp_threshold < 1) 1921 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n"); 1922 else if (slow_rsp_threshold > 32767) 1923 cifs_dbg(VFS, 1924 "slow response threshold set higher than recommended (0 to 32767)\n"); 1925 #endif /* CONFIG_CIFS_STATS2 */ 1926 1927 atomic_set(&mid_count, 0); 1928 GlobalCurrentXid = 0; 1929 GlobalTotalActiveXid = 0; 1930 GlobalMaxActiveXid = 0; 1931 1932 cifs_lock_secret = get_random_u32(); 1933 1934 if (cifs_max_pending < 2) { 1935 cifs_max_pending = 2; 1936 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n"); 1937 } else if (cifs_max_pending > CIFS_MAX_REQ) { 1938 cifs_max_pending = CIFS_MAX_REQ; 1939 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n", 1940 CIFS_MAX_REQ); 1941 } 1942 1943 /* Limit max to about 18 hours, and setting to zero disables directory entry caching */ 1944 if (dir_cache_timeout > 65000) { 1945 dir_cache_timeout = 65000; 1946 cifs_dbg(VFS, "dir_cache_timeout set to max of 65000 seconds\n"); 1947 } 1948 1949 cifsiod_wq = alloc_workqueue("cifsiod", 1950 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, 1951 0); 1952 if (!cifsiod_wq) { 1953 rc = -ENOMEM; 1954 goto out_clean_proc; 1955 } 1956 1957 /* 1958 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3) 1959 * so that we don't launch too many worker threads but 1960 * Documentation/core-api/workqueue.rst recommends setting it to 0 1961 */ 1962 1963 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */ 1964 decrypt_wq = alloc_workqueue("smb3decryptd", 1965 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0); 1966 if (!decrypt_wq) { 1967 rc = -ENOMEM; 1968 goto out_destroy_cifsiod_wq; 1969 } 1970 1971 fileinfo_put_wq = alloc_workqueue("cifsfileinfoput", 1972 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0); 1973 if (!fileinfo_put_wq) { 1974 rc = -ENOMEM; 1975 goto out_destroy_decrypt_wq; 1976 } 1977 1978 cifsoplockd_wq = alloc_workqueue("cifsoplockd", 1979 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, 1980 0); 1981 if (!cifsoplockd_wq) { 1982 rc = -ENOMEM; 1983 goto out_destroy_fileinfo_put_wq; 1984 } 1985 1986 deferredclose_wq = alloc_workqueue("deferredclose", 1987 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, 1988 0); 1989 if (!deferredclose_wq) { 1990 rc = -ENOMEM; 1991 goto out_destroy_cifsoplockd_wq; 1992 } 1993 1994 serverclose_wq = alloc_workqueue("serverclose", 1995 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, 1996 0); 1997 if (!serverclose_wq) { 1998 rc = -ENOMEM; 1999 goto out_destroy_deferredclose_wq; 2000 } 2001 2002 cfid_put_wq = alloc_workqueue("cfid_put_wq", 2003 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU, 2004 0); 2005 if (!cfid_put_wq) { 2006 rc = -ENOMEM; 2007 goto out_destroy_serverclose_wq; 2008 } 2009 2010 rc = cifs_init_inodecache(); 2011 if (rc) 2012 goto out_destroy_cfid_put_wq; 2013 2014 rc = cifs_init_netfs(); 2015 if (rc) 2016 goto out_destroy_inodecache; 2017 2018 rc = init_mids(); 2019 if (rc) 2020 goto out_destroy_netfs; 2021 2022 rc = cifs_init_request_bufs(); 2023 if (rc) 2024 goto out_destroy_mids; 2025 2026 #ifdef CONFIG_CIFS_DFS_UPCALL 2027 rc = dfs_cache_init(); 2028 if (rc) 2029 goto out_destroy_request_bufs; 2030 #endif /* CONFIG_CIFS_DFS_UPCALL */ 2031 #ifdef CONFIG_CIFS_UPCALL 2032 rc = init_cifs_spnego(); 2033 if (rc) 2034 goto out_destroy_dfs_cache; 2035 #endif /* CONFIG_CIFS_UPCALL */ 2036 #ifdef CONFIG_CIFS_SWN_UPCALL 2037 rc = cifs_genl_init(); 2038 if (rc) 2039 goto out_register_key_type; 2040 #endif /* CONFIG_CIFS_SWN_UPCALL */ 2041 2042 rc = init_cifs_idmap(); 2043 if (rc) 2044 goto out_cifs_swn_init; 2045 2046 rc = register_filesystem(&cifs_fs_type); 2047 if (rc) 2048 goto out_init_cifs_idmap; 2049 2050 rc = register_filesystem(&smb3_fs_type); 2051 if (rc) { 2052 unregister_filesystem(&cifs_fs_type); 2053 goto out_init_cifs_idmap; 2054 } 2055 2056 return 0; 2057 2058 out_init_cifs_idmap: 2059 exit_cifs_idmap(); 2060 out_cifs_swn_init: 2061 #ifdef CONFIG_CIFS_SWN_UPCALL 2062 cifs_genl_exit(); 2063 out_register_key_type: 2064 #endif 2065 #ifdef CONFIG_CIFS_UPCALL 2066 exit_cifs_spnego(); 2067 out_destroy_dfs_cache: 2068 #endif 2069 #ifdef CONFIG_CIFS_DFS_UPCALL 2070 dfs_cache_destroy(); 2071 out_destroy_request_bufs: 2072 #endif 2073 cifs_destroy_request_bufs(); 2074 out_destroy_mids: 2075 destroy_mids(); 2076 out_destroy_netfs: 2077 cifs_destroy_netfs(); 2078 out_destroy_inodecache: 2079 cifs_destroy_inodecache(); 2080 out_destroy_cfid_put_wq: 2081 destroy_workqueue(cfid_put_wq); 2082 out_destroy_serverclose_wq: 2083 destroy_workqueue(serverclose_wq); 2084 out_destroy_deferredclose_wq: 2085 destroy_workqueue(deferredclose_wq); 2086 out_destroy_cifsoplockd_wq: 2087 destroy_workqueue(cifsoplockd_wq); 2088 out_destroy_fileinfo_put_wq: 2089 destroy_workqueue(fileinfo_put_wq); 2090 out_destroy_decrypt_wq: 2091 destroy_workqueue(decrypt_wq); 2092 out_destroy_cifsiod_wq: 2093 destroy_workqueue(cifsiod_wq); 2094 out_clean_proc: 2095 cifs_proc_clean(); 2096 return rc; 2097 } 2098 2099 static void __exit 2100 exit_cifs(void) 2101 { 2102 cifs_dbg(NOISY, "exit_smb3\n"); 2103 unregister_filesystem(&cifs_fs_type); 2104 unregister_filesystem(&smb3_fs_type); 2105 cifs_release_automount_timer(); 2106 exit_cifs_idmap(); 2107 #ifdef CONFIG_CIFS_SWN_UPCALL 2108 cifs_genl_exit(); 2109 #endif 2110 #ifdef CONFIG_CIFS_UPCALL 2111 exit_cifs_spnego(); 2112 #endif 2113 #ifdef CONFIG_CIFS_DFS_UPCALL 2114 dfs_cache_destroy(); 2115 #endif 2116 cifs_destroy_request_bufs(); 2117 destroy_mids(); 2118 cifs_destroy_netfs(); 2119 cifs_destroy_inodecache(); 2120 destroy_workqueue(deferredclose_wq); 2121 destroy_workqueue(cifsoplockd_wq); 2122 destroy_workqueue(decrypt_wq); 2123 destroy_workqueue(fileinfo_put_wq); 2124 destroy_workqueue(serverclose_wq); 2125 destroy_workqueue(cfid_put_wq); 2126 destroy_workqueue(cifsiod_wq); 2127 cifs_proc_clean(); 2128 } 2129 2130 MODULE_AUTHOR("Steve French"); 2131 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */ 2132 MODULE_DESCRIPTION 2133 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and " 2134 "also older servers complying with the SNIA CIFS Specification)"); 2135 MODULE_VERSION(CIFS_VERSION); 2136 MODULE_SOFTDEP("nls"); 2137 MODULE_SOFTDEP("aes"); 2138 MODULE_SOFTDEP("aead2"); 2139 MODULE_SOFTDEP("ccm"); 2140 MODULE_SOFTDEP("gcm"); 2141 module_init(init_cifs) 2142 module_exit(exit_cifs) 2143