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