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