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 */ 8 9 #include <linux/slab.h> 10 #include <linux/ctype.h> 11 #include <linux/mempool.h> 12 #include <linux/vmalloc.h> 13 #include "cifsglob.h" 14 #include "cifsproto.h" 15 #include "cifs_debug.h" 16 #include "smberr.h" 17 #include "nterr.h" 18 #include "cifs_unicode.h" 19 #include "smb2pdu.h" 20 #include "smb2proto.h" 21 #include "smb1proto.h" 22 #include "cifsfs.h" 23 #ifdef CONFIG_CIFS_DFS_UPCALL 24 #include "dns_resolve.h" 25 #include "dfs_cache.h" 26 #include "dfs.h" 27 #endif 28 #include "fs_context.h" 29 #include "cached_dir.h" 30 31 struct tcon_list { 32 struct list_head entry; 33 struct cifs_tcon *tcon; 34 }; 35 36 /* The xid serves as a useful identifier for each incoming vfs request, 37 in a similar way to the mid which is useful to track each sent smb, 38 and CurrentXid can also provide a running counter (although it 39 will eventually wrap past zero) of the total vfs operations handled 40 since the cifs fs was mounted */ 41 42 unsigned int 43 _get_xid(void) 44 { 45 unsigned int xid; 46 47 spin_lock(&GlobalMid_Lock); 48 GlobalTotalActiveXid++; 49 50 /* keep high water mark for number of simultaneous ops in filesystem */ 51 if (GlobalTotalActiveXid > GlobalMaxActiveXid) 52 GlobalMaxActiveXid = GlobalTotalActiveXid; 53 if (GlobalTotalActiveXid > 65000) 54 cifs_dbg(FYI, "warning: more than 65000 requests active\n"); 55 xid = GlobalCurrentXid++; 56 spin_unlock(&GlobalMid_Lock); 57 return xid; 58 } 59 60 void 61 _free_xid(unsigned int xid) 62 { 63 spin_lock(&GlobalMid_Lock); 64 /* if (GlobalTotalActiveXid == 0) 65 BUG(); */ 66 GlobalTotalActiveXid--; 67 spin_unlock(&GlobalMid_Lock); 68 } 69 70 struct cifs_ses * 71 sesInfoAlloc(void) 72 { 73 struct cifs_ses *ret_buf; 74 75 ret_buf = kzalloc_obj(struct cifs_ses); 76 if (ret_buf) { 77 atomic_inc(&sesInfoAllocCount); 78 spin_lock_init(&ret_buf->ses_lock); 79 ret_buf->ses_status = SES_NEW; 80 ++ret_buf->ses_count; 81 INIT_LIST_HEAD(&ret_buf->smb_ses_list); 82 INIT_LIST_HEAD(&ret_buf->tcon_list); 83 mutex_init(&ret_buf->session_mutex); 84 spin_lock_init(&ret_buf->iface_lock); 85 INIT_LIST_HEAD(&ret_buf->iface_list); 86 spin_lock_init(&ret_buf->chan_lock); 87 } 88 return ret_buf; 89 } 90 91 void 92 sesInfoFree(struct cifs_ses *buf_to_free) 93 { 94 struct cifs_server_iface *iface = NULL, *niface = NULL; 95 96 if (buf_to_free == NULL) { 97 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n"); 98 return; 99 } 100 101 unload_nls(buf_to_free->local_nls); 102 atomic_dec(&sesInfoAllocCount); 103 kfree(buf_to_free->serverOS); 104 kfree(buf_to_free->serverDomain); 105 kfree(buf_to_free->serverNOS); 106 kfree_sensitive(buf_to_free->password); 107 kfree_sensitive(buf_to_free->password2); 108 kfree(buf_to_free->user_name); 109 kfree(buf_to_free->domainName); 110 kfree(buf_to_free->dns_dom); 111 kfree_sensitive(buf_to_free->auth_key.response); 112 spin_lock(&buf_to_free->iface_lock); 113 list_for_each_entry_safe(iface, niface, &buf_to_free->iface_list, 114 iface_head) 115 kref_put(&iface->refcount, release_iface); 116 spin_unlock(&buf_to_free->iface_lock); 117 kfree_sensitive(buf_to_free); 118 } 119 120 struct cifs_tcon * 121 tcon_info_alloc(bool dir_leases_enabled, enum smb3_tcon_ref_trace trace) 122 { 123 struct cifs_tcon *ret_buf; 124 static atomic_t tcon_debug_id; 125 126 ret_buf = kzalloc_obj(*ret_buf); 127 if (!ret_buf) 128 return NULL; 129 130 if (dir_leases_enabled == true) { 131 ret_buf->cfids = init_cached_dirs(); 132 if (!ret_buf->cfids) { 133 kfree(ret_buf); 134 return NULL; 135 } 136 } 137 /* else ret_buf->cfids is already set to NULL above */ 138 139 atomic_inc(&tconInfoAllocCount); 140 ret_buf->status = TID_NEW; 141 ret_buf->debug_id = atomic_inc_return(&tcon_debug_id); 142 ret_buf->tc_count = 1; 143 spin_lock_init(&ret_buf->tc_lock); 144 INIT_LIST_HEAD(&ret_buf->openFileList); 145 INIT_LIST_HEAD(&ret_buf->tcon_list); 146 INIT_LIST_HEAD(&ret_buf->cifs_sb_list); 147 spin_lock_init(&ret_buf->open_file_lock); 148 spin_lock_init(&ret_buf->stat_lock); 149 spin_lock_init(&ret_buf->sb_list_lock); 150 atomic_set(&ret_buf->num_local_opens, 0); 151 atomic_set(&ret_buf->num_remote_opens, 0); 152 ret_buf->stats_from_time = ktime_get_real_seconds(); 153 #ifdef CONFIG_CIFS_FSCACHE 154 mutex_init(&ret_buf->fscache_lock); 155 #endif 156 trace_smb3_tcon_ref(ret_buf->debug_id, ret_buf->tc_count, trace); 157 #ifdef CONFIG_CIFS_DFS_UPCALL 158 INIT_LIST_HEAD(&ret_buf->dfs_ses_list); 159 #endif 160 INIT_LIST_HEAD(&ret_buf->pending_opens); 161 INIT_DELAYED_WORK(&ret_buf->query_interfaces, 162 smb2_query_server_interfaces); 163 #ifdef CONFIG_CIFS_DFS_UPCALL 164 INIT_DELAYED_WORK(&ret_buf->dfs_cache_work, dfs_cache_refresh); 165 #endif 166 167 return ret_buf; 168 } 169 170 void 171 tconInfoFree(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace) 172 { 173 if (tcon == NULL) { 174 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n"); 175 return; 176 } 177 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, trace); 178 free_cached_dirs(tcon->cfids); 179 atomic_dec(&tconInfoAllocCount); 180 kfree(tcon->nativeFileSystem); 181 kfree_sensitive(tcon->password); 182 kfree(tcon->origin_fullpath); 183 kfree(tcon); 184 } 185 186 void * 187 cifs_buf_get(void) 188 { 189 void *ret_buf = NULL; 190 /* 191 * SMB2 header is bigger than CIFS one - no problems to clean some 192 * more bytes for CIFS. 193 */ 194 size_t buf_size = sizeof(struct smb2_hdr); 195 196 /* 197 * We could use negotiated size instead of max_msgsize - 198 * but it may be more efficient to always alloc same size 199 * albeit slightly larger than necessary and maxbuffersize 200 * defaults to this and can not be bigger. 201 */ 202 ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS); 203 204 /* clear the first few header bytes */ 205 /* for most paths, more is cleared in header_assemble */ 206 memset(ret_buf, 0, buf_size + 3); 207 atomic_inc(&buf_alloc_count); 208 #ifdef CONFIG_CIFS_STATS2 209 atomic_inc(&total_buf_alloc_count); 210 #endif /* CONFIG_CIFS_STATS2 */ 211 212 return ret_buf; 213 } 214 215 void 216 cifs_buf_release(void *buf_to_free) 217 { 218 if (!buf_to_free) 219 return; 220 mempool_free(buf_to_free, cifs_req_poolp); 221 222 atomic_dec(&buf_alloc_count); 223 return; 224 } 225 226 void * 227 cifs_small_buf_get(void) 228 { 229 void *ret_buf = NULL; 230 231 /* We could use negotiated size instead of max_msgsize - 232 but it may be more efficient to always alloc same size 233 albeit slightly larger than necessary and maxbuffersize 234 defaults to this and can not be bigger */ 235 ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS); 236 /* No need to clear memory here, cleared in header assemble */ 237 atomic_inc(&small_buf_alloc_count); 238 #ifdef CONFIG_CIFS_STATS2 239 atomic_inc(&total_small_buf_alloc_count); 240 #endif /* CONFIG_CIFS_STATS2 */ 241 242 return ret_buf; 243 } 244 245 void 246 cifs_small_buf_release(void *buf_to_free) 247 { 248 249 if (buf_to_free == NULL) { 250 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n"); 251 return; 252 } 253 mempool_free(buf_to_free, cifs_sm_req_poolp); 254 255 atomic_dec(&small_buf_alloc_count); 256 return; 257 } 258 259 void 260 free_rsp_buf(int resp_buftype, void *rsp) 261 { 262 if (resp_buftype == CIFS_SMALL_BUFFER) 263 cifs_small_buf_release(rsp); 264 else if (resp_buftype == CIFS_LARGE_BUFFER) 265 cifs_buf_release(rsp); 266 } 267 268 void 269 dump_smb(void *buf, int smb_buf_length) 270 { 271 if (traceSMB == 0) 272 return; 273 274 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf, 275 smb_buf_length, true); 276 } 277 278 void 279 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb, const char *reason, int rc) 280 { 281 unsigned int sbflags = cifs_sb_flags(cifs_sb); 282 283 if (sbflags & CIFS_MOUNT_SERVER_INUM) { 284 struct cifs_tcon *tcon = NULL; 285 286 if (cifs_sb->master_tlink) 287 tcon = cifs_sb_master_tcon(cifs_sb); 288 289 atomic_andnot(CIFS_MOUNT_SERVER_INUM, &cifs_sb->mnt_cifs_flags); 290 cifs_sb->mnt_cifs_serverino_autodisabled = true; 291 if (rc) 292 cifs_dbg(VFS, "%s: %d\n", reason, rc); 293 else 294 cifs_dbg(VFS, "%s\n", reason); 295 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n", 296 tcon ? tcon->tree_name : "new server"); 297 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n"); 298 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n"); 299 300 } 301 } 302 303 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock) 304 { 305 oplock &= 0xF; 306 307 if (oplock == OPLOCK_EXCLUSIVE) { 308 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG; 309 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n", 310 &cinode->netfs.inode); 311 } else if (oplock == OPLOCK_READ) { 312 cinode->oplock = CIFS_CACHE_READ_FLG; 313 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n", 314 &cinode->netfs.inode); 315 } else 316 cinode->oplock = 0; 317 } 318 319 /* 320 * We wait for oplock breaks to be processed before we attempt to perform 321 * writes. 322 */ 323 int cifs_get_writer(struct cifsInodeInfo *cinode) 324 { 325 int rc; 326 327 start: 328 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK, 329 TASK_KILLABLE); 330 if (rc) 331 return rc; 332 333 spin_lock(&cinode->writers_lock); 334 if (!cinode->writers) 335 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 336 cinode->writers++; 337 /* Check to see if we have started servicing an oplock break */ 338 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) { 339 cinode->writers--; 340 if (cinode->writers == 0) { 341 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 342 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 343 } 344 spin_unlock(&cinode->writers_lock); 345 goto start; 346 } 347 spin_unlock(&cinode->writers_lock); 348 return 0; 349 } 350 351 void cifs_put_writer(struct cifsInodeInfo *cinode) 352 { 353 spin_lock(&cinode->writers_lock); 354 cinode->writers--; 355 if (cinode->writers == 0) { 356 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 357 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 358 } 359 spin_unlock(&cinode->writers_lock); 360 } 361 362 /** 363 * cifs_queue_oplock_break - queue the oplock break handler for cfile 364 * @cfile: The file to break the oplock on 365 * 366 * This function is called from the demultiplex thread when it 367 * receives an oplock break for @cfile. 368 * 369 * Assumes the tcon->open_file_lock is held. 370 * Assumes cfile->file_info_lock is NOT held. 371 */ 372 void cifs_queue_oplock_break(struct cifsFileInfo *cfile) 373 { 374 /* 375 * Bump the handle refcount now while we hold the 376 * open_file_lock to enforce the validity of it for the oplock 377 * break handler. The matching put is done at the end of the 378 * handler. 379 * 380 * Only take a reference if the work is actually queued. 381 */ 382 if (queue_work(cifsoplockd_wq, &cfile->oplock_break)) 383 cifsFileInfo_get(cfile); 384 } 385 386 void cifs_done_oplock_break(struct cifsInodeInfo *cinode) 387 { 388 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags); 389 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK); 390 } 391 392 bool 393 backup_cred(struct cifs_sb_info *cifs_sb) 394 { 395 unsigned int sbflags = cifs_sb_flags(cifs_sb); 396 397 if (sbflags & CIFS_MOUNT_CIFS_BACKUPUID) { 398 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid())) 399 return true; 400 } 401 if (sbflags & CIFS_MOUNT_CIFS_BACKUPGID) { 402 if (in_group_p(cifs_sb->ctx->backupgid)) 403 return true; 404 } 405 406 return false; 407 } 408 409 void 410 cifs_del_pending_open(struct cifs_pending_open *open) 411 { 412 spin_lock(&tlink_tcon(open->tlink)->open_file_lock); 413 list_del(&open->olist); 414 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 415 } 416 417 void 418 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink, 419 struct cifs_pending_open *open) 420 { 421 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE); 422 open->oplock = CIFS_OPLOCK_NO_CHANGE; 423 open->tlink = tlink; 424 fid->pending_open = open; 425 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens); 426 } 427 428 void 429 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink, 430 struct cifs_pending_open *open) 431 { 432 spin_lock(&tlink_tcon(tlink)->open_file_lock); 433 cifs_add_pending_open_locked(fid, tlink, open); 434 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 435 } 436 437 /* 438 * Critical section which runs after acquiring deferred_lock. 439 * As there is no reference count on cifs_deferred_close, pdclose 440 * should not be used outside deferred_lock. 441 */ 442 bool 443 cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose) 444 { 445 struct cifs_deferred_close *dclose; 446 447 list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) { 448 if ((dclose->netfid == cfile->fid.netfid) && 449 (dclose->persistent_fid == cfile->fid.persistent_fid) && 450 (dclose->volatile_fid == cfile->fid.volatile_fid)) { 451 *pdclose = dclose; 452 return true; 453 } 454 } 455 return false; 456 } 457 458 /* 459 * Critical section which runs after acquiring deferred_lock. 460 */ 461 void 462 cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose) 463 { 464 bool is_deferred = false; 465 struct cifs_deferred_close *pdclose; 466 467 is_deferred = cifs_is_deferred_close(cfile, &pdclose); 468 if (is_deferred) { 469 kfree(dclose); 470 return; 471 } 472 473 dclose->tlink = cfile->tlink; 474 dclose->netfid = cfile->fid.netfid; 475 dclose->persistent_fid = cfile->fid.persistent_fid; 476 dclose->volatile_fid = cfile->fid.volatile_fid; 477 list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes); 478 } 479 480 /* 481 * Critical section which runs after acquiring deferred_lock. 482 */ 483 void 484 cifs_del_deferred_close(struct cifsFileInfo *cfile) 485 { 486 bool is_deferred = false; 487 struct cifs_deferred_close *dclose; 488 489 is_deferred = cifs_is_deferred_close(cfile, &dclose); 490 if (!is_deferred) 491 return; 492 list_del(&dclose->dlist); 493 kfree(dclose); 494 } 495 496 void 497 cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) 498 { 499 struct cifsFileInfo *cfile = NULL, *failed_cfile = NULL; 500 struct file_list *tmp_list, *tmp_next_list; 501 LIST_HEAD(file_head); 502 503 if (cifs_inode == NULL) 504 return; 505 506 spin_lock(&cifs_inode->open_file_lock); 507 list_for_each_entry(cfile, &cifs_inode->openFileList, flist) { 508 if (delayed_work_pending(&cfile->deferred)) { 509 if (cancel_delayed_work(&cfile->deferred)) { 510 spin_lock(&cifs_inode->deferred_lock); 511 cifs_del_deferred_close(cfile); 512 spin_unlock(&cifs_inode->deferred_lock); 513 514 tmp_list = kmalloc_obj(struct file_list, 515 GFP_ATOMIC); 516 if (tmp_list == NULL) { 517 failed_cfile = cfile; 518 break; 519 } 520 tmp_list->cfile = cfile; 521 list_add_tail(&tmp_list->list, &file_head); 522 } 523 } 524 } 525 spin_unlock(&cifs_inode->open_file_lock); 526 527 if (failed_cfile) 528 _cifsFileInfo_put(failed_cfile, false, false); 529 530 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 531 _cifsFileInfo_put(tmp_list->cfile, false, false); 532 list_del(&tmp_list->list); 533 kfree(tmp_list); 534 } 535 } 536 537 void 538 cifs_close_all_deferred_files(struct cifs_tcon *tcon) 539 { 540 struct cifsFileInfo *cfile, *failed_cfile = NULL; 541 struct file_list *tmp_list, *tmp_next_list; 542 LIST_HEAD(file_head); 543 544 spin_lock(&tcon->open_file_lock); 545 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 546 if (delayed_work_pending(&cfile->deferred)) { 547 if (cancel_delayed_work(&cfile->deferred)) { 548 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 549 cifs_del_deferred_close(cfile); 550 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 551 552 tmp_list = kmalloc_obj(struct file_list, 553 GFP_ATOMIC); 554 if (tmp_list == NULL) { 555 failed_cfile = cfile; 556 break; 557 } 558 tmp_list->cfile = cfile; 559 list_add_tail(&tmp_list->list, &file_head); 560 } 561 } 562 } 563 spin_unlock(&tcon->open_file_lock); 564 565 if (failed_cfile) 566 _cifsFileInfo_put(failed_cfile, true, false); 567 568 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 569 _cifsFileInfo_put(tmp_list->cfile, true, false); 570 list_del(&tmp_list->list); 571 kfree(tmp_list); 572 } 573 } 574 575 void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb) 576 { 577 struct rb_root *root = &cifs_sb->tlink_tree; 578 struct rb_node *node; 579 struct cifs_tcon *tcon; 580 struct tcon_link *tlink; 581 struct tcon_list *tmp_list, *q; 582 LIST_HEAD(tcon_head); 583 584 spin_lock(&cifs_sb->tlink_tree_lock); 585 for (node = rb_first(root); node; node = rb_next(node)) { 586 tlink = rb_entry(node, struct tcon_link, tl_rbnode); 587 tcon = tlink_tcon(tlink); 588 if (IS_ERR(tcon)) 589 continue; 590 tmp_list = kmalloc_obj(struct tcon_list, GFP_ATOMIC); 591 if (tmp_list == NULL) 592 break; 593 tmp_list->tcon = tcon; 594 /* Take a reference on tcon to prevent it from being freed */ 595 spin_lock(&tcon->tc_lock); 596 ++tcon->tc_count; 597 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 598 netfs_trace_tcon_ref_get_close_defer_files); 599 spin_unlock(&tcon->tc_lock); 600 list_add_tail(&tmp_list->entry, &tcon_head); 601 } 602 spin_unlock(&cifs_sb->tlink_tree_lock); 603 604 list_for_each_entry_safe(tmp_list, q, &tcon_head, entry) { 605 cifs_close_all_deferred_files(tmp_list->tcon); 606 list_del(&tmp_list->entry); 607 cifs_put_tcon(tmp_list->tcon, netfs_trace_tcon_ref_put_close_defer_files); 608 kfree(tmp_list); 609 } 610 } 611 612 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, 613 struct dentry *dentry) 614 { 615 struct file_list *tmp_list, *tmp_next_list; 616 struct cifsFileInfo *cfile, *failed_cfile = NULL; 617 LIST_HEAD(file_head); 618 619 spin_lock(&tcon->open_file_lock); 620 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 621 if ((cfile->dentry == dentry) && 622 delayed_work_pending(&cfile->deferred) && 623 cancel_delayed_work(&cfile->deferred)) { 624 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 625 cifs_del_deferred_close(cfile); 626 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 627 628 tmp_list = kmalloc_obj(struct file_list, GFP_ATOMIC); 629 if (tmp_list == NULL) { 630 failed_cfile = cfile; 631 break; 632 } 633 tmp_list->cfile = cfile; 634 list_add_tail(&tmp_list->list, &file_head); 635 } 636 } 637 spin_unlock(&tcon->open_file_lock); 638 639 if (failed_cfile) 640 _cifsFileInfo_put(failed_cfile, true, false); 641 642 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 643 _cifsFileInfo_put(tmp_list->cfile, true, false); 644 list_del(&tmp_list->list); 645 kfree(tmp_list); 646 } 647 } 648 649 /* 650 * If a dentry has been deleted, all corresponding open handles should know that 651 * so that we do not defer close them. 652 */ 653 void cifs_mark_open_handles_for_deleted_file(struct inode *inode, 654 const char *path) 655 { 656 struct cifsFileInfo *cfile; 657 void *page; 658 const char *full_path; 659 struct cifsInodeInfo *cinode = CIFS_I(inode); 660 661 page = alloc_dentry_path(); 662 spin_lock(&cinode->open_file_lock); 663 664 /* 665 * note: we need to construct path from dentry and compare only if the 666 * inode has any hardlinks. When number of hardlinks is 1, we can just 667 * mark all open handles since they are going to be from the same file. 668 */ 669 if (inode->i_nlink > 1) { 670 list_for_each_entry(cfile, &cinode->openFileList, flist) { 671 full_path = build_path_from_dentry(cfile->dentry, page); 672 if (!IS_ERR(full_path) && strcmp(full_path, path) == 0) 673 cfile->status_file_deleted = true; 674 } 675 } else { 676 list_for_each_entry(cfile, &cinode->openFileList, flist) 677 cfile->status_file_deleted = true; 678 } 679 spin_unlock(&cinode->open_file_lock); 680 free_dentry_path(page); 681 } 682 683 /* parses DFS referral V3 structure 684 * caller is responsible for freeing target_nodes 685 * returns: 686 * - on success - 0 687 * - on failure - errno 688 */ 689 int 690 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size, 691 unsigned int *num_of_nodes, 692 struct dfs_info3_param **target_nodes, 693 const struct nls_table *nls_codepage, int remap, 694 const char *searchName, bool is_unicode) 695 { 696 int i, rc = 0; 697 char *data_end; 698 struct dfs_referral_level_3 *ref; 699 unsigned int path_consumed; 700 size_t search_name_len; 701 702 if (rsp_size < sizeof(*rsp)) { 703 cifs_dbg(VFS | ONCE, 704 "%s: header is malformed (size is %u, must be %zu)\n", 705 __func__, rsp_size, sizeof(*rsp)); 706 rc = -EINVAL; 707 goto parse_DFS_referrals_exit; 708 } 709 710 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals); 711 712 if (*num_of_nodes < 1) { 713 cifs_dbg(VFS | ONCE, "%s: [path=%s] num_referrals must be at least > 0, but we got %d\n", 714 __func__, searchName, *num_of_nodes); 715 rc = -ENOENT; 716 goto parse_DFS_referrals_exit; 717 } 718 719 if (sizeof(*rsp) + *num_of_nodes * sizeof(REFERRAL3) > rsp_size) { 720 cifs_dbg(VFS | ONCE, 721 "%s: malformed buffer (size is %u, must be at least %zu)\n", 722 __func__, rsp_size, 723 sizeof(*rsp) + *num_of_nodes * sizeof(REFERRAL3)); 724 rc = -EINVAL; 725 goto parse_DFS_referrals_exit; 726 } 727 728 ref = (struct dfs_referral_level_3 *) &(rsp->referrals); 729 if (ref->VersionNumber != cpu_to_le16(3)) { 730 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n", 731 le16_to_cpu(ref->VersionNumber)); 732 rc = -EINVAL; 733 goto parse_DFS_referrals_exit; 734 } 735 736 /* get the upper boundary of the resp buffer */ 737 data_end = (char *)rsp + rsp_size; 738 739 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n", 740 *num_of_nodes, le32_to_cpu(rsp->DFSFlags)); 741 742 *target_nodes = kzalloc_objs(struct dfs_info3_param, *num_of_nodes); 743 if (*target_nodes == NULL) { 744 rc = -ENOMEM; 745 goto parse_DFS_referrals_exit; 746 } 747 search_name_len = strlen(searchName); 748 749 /* collect necessary data from referrals */ 750 for (i = 0; i < *num_of_nodes; i++) { 751 char *temp; 752 int max_len; 753 struct dfs_info3_param *node = (*target_nodes)+i; 754 755 node->flags = le32_to_cpu(rsp->DFSFlags); 756 path_consumed = le16_to_cpu(rsp->PathConsumed); 757 if (is_unicode) { 758 size_t search_name_utf16_len = search_name_len * 2 + 2; 759 __le16 *tmp; 760 761 if (path_consumed > search_name_utf16_len) { 762 rc = -EINVAL; 763 goto parse_DFS_referrals_exit; 764 } 765 766 tmp = kmalloc(search_name_utf16_len, GFP_KERNEL); 767 if (!tmp) { 768 rc = -ENOMEM; 769 goto parse_DFS_referrals_exit; 770 } 771 cifsConvertToUTF16((__le16 *)tmp, searchName, 772 PATH_MAX, nls_codepage, remap); 773 node->path_consumed = cifs_utf16_bytes(tmp, path_consumed, 774 nls_codepage); 775 kfree(tmp); 776 } else { 777 if (path_consumed > search_name_len) { 778 rc = -EINVAL; 779 goto parse_DFS_referrals_exit; 780 } 781 782 node->path_consumed = path_consumed; 783 } 784 785 node->server_type = le16_to_cpu(ref->ServerType); 786 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags); 787 788 /* copy DfsPath */ 789 if (le16_to_cpu(ref->DfsPathOffset) < sizeof(*ref) || 790 le16_to_cpu(ref->DfsPathOffset) > data_end - (char *)ref) { 791 cifs_dbg(VFS, "%s: DfsPathOffset %u out of range [%zu, %td]\n", 792 __func__, le16_to_cpu(ref->DfsPathOffset), 793 sizeof(*ref), data_end - (char *)ref); 794 rc = -EINVAL; 795 goto parse_DFS_referrals_exit; 796 } 797 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset); 798 max_len = data_end - temp; 799 node->path_name = cifs_strndup_from_utf16(temp, max_len, 800 is_unicode, nls_codepage); 801 if (!node->path_name) { 802 rc = -ENOMEM; 803 goto parse_DFS_referrals_exit; 804 } 805 806 /* copy link target UNC */ 807 if (le16_to_cpu(ref->NetworkAddressOffset) < sizeof(*ref) || 808 le16_to_cpu(ref->NetworkAddressOffset) > data_end - (char *)ref) { 809 cifs_dbg(VFS, "%s: NetworkAddressOffset %u out of range [%zu, %td]\n", 810 __func__, le16_to_cpu(ref->NetworkAddressOffset), 811 sizeof(*ref), data_end - (char *)ref); 812 rc = -EINVAL; 813 goto parse_DFS_referrals_exit; 814 } 815 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset); 816 max_len = data_end - temp; 817 node->node_name = cifs_strndup_from_utf16(temp, max_len, 818 is_unicode, nls_codepage); 819 if (!node->node_name) { 820 rc = -ENOMEM; 821 goto parse_DFS_referrals_exit; 822 } 823 824 node->ttl = le32_to_cpu(ref->TimeToLive); 825 826 ref++; 827 } 828 829 parse_DFS_referrals_exit: 830 if (rc) { 831 free_dfs_info_array(*target_nodes, *num_of_nodes); 832 *target_nodes = NULL; 833 *num_of_nodes = 0; 834 } 835 return rc; 836 } 837 838 void extract_unc_hostname(const char *unc, const char **h, size_t *len) 839 { 840 const char *end; 841 842 /* skip initial slashes */ 843 while (*unc && (*unc == '\\' || *unc == '/')) 844 unc++; 845 846 end = unc; 847 848 while (*end && !(*end == '\\' || *end == '/')) 849 end++; 850 851 *h = unc; 852 *len = end - unc; 853 } 854 855 /** 856 * copy_path_name - copy src path to dst, possibly truncating 857 * @dst: The destination buffer 858 * @src: The source name 859 * 860 * returns number of bytes written (including trailing nul) 861 */ 862 int copy_path_name(char *dst, const char *src) 863 { 864 int name_len; 865 866 /* 867 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it 868 * will truncate and strlen(dst) will be PATH_MAX-1 869 */ 870 name_len = strscpy(dst, src, PATH_MAX); 871 if (WARN_ON_ONCE(name_len < 0)) 872 name_len = PATH_MAX-1; 873 874 /* we count the trailing nul */ 875 name_len++; 876 return name_len; 877 } 878 879 struct super_cb_data { 880 void *data; 881 struct super_block *sb; 882 }; 883 884 static void tcon_super_cb(struct super_block *sb, void *arg) 885 { 886 struct super_cb_data *sd = arg; 887 struct cifs_sb_info *cifs_sb; 888 struct cifs_tcon *t1 = sd->data, *t2; 889 890 if (sd->sb) 891 return; 892 893 cifs_sb = CIFS_SB(sb); 894 t2 = cifs_sb_master_tcon(cifs_sb); 895 896 spin_lock(&t2->tc_lock); 897 if ((t1->ses == t2->ses || 898 t1->ses->dfs_root_ses == t2->ses->dfs_root_ses) && 899 t1->ses->server == t2->ses->server && 900 t2->origin_fullpath && 901 dfs_src_pathname_equal(t2->origin_fullpath, t1->origin_fullpath)) { 902 /* 903 * Take the active reference while iterate_supers_type() still 904 * holds s_umount shared. 905 */ 906 cifs_sb_active(sb); 907 sd->sb = sb; 908 } 909 spin_unlock(&t2->tc_lock); 910 } 911 912 static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *), 913 void *data) 914 { 915 struct super_cb_data sd = { 916 .data = data, 917 .sb = NULL, 918 }; 919 struct file_system_type **fs_type = (struct file_system_type *[]) { 920 &cifs_fs_type, &smb3_fs_type, NULL, 921 }; 922 923 for (; *fs_type; fs_type++) { 924 iterate_supers_type(*fs_type, f, &sd); 925 if (sd.sb) 926 return sd.sb; 927 } 928 pr_warn_once("%s: could not find dfs superblock\n", __func__); 929 return ERR_PTR(-EINVAL); 930 } 931 932 static void __cifs_put_super(struct super_block *sb) 933 { 934 if (!IS_ERR_OR_NULL(sb)) 935 cifs_sb_deactive(sb); 936 } 937 938 struct super_block *cifs_get_dfs_tcon_super(struct cifs_tcon *tcon) 939 { 940 spin_lock(&tcon->tc_lock); 941 if (!tcon->origin_fullpath) { 942 spin_unlock(&tcon->tc_lock); 943 return ERR_PTR(-ENOENT); 944 } 945 spin_unlock(&tcon->tc_lock); 946 return __cifs_get_super(tcon_super_cb, tcon); 947 } 948 949 void cifs_put_tcp_super(struct super_block *sb) 950 { 951 __cifs_put_super(sb); 952 } 953 954 #ifdef CONFIG_CIFS_DFS_UPCALL 955 int match_target_ip(struct TCP_Server_Info *server, 956 const char *host, size_t hostlen, 957 bool *result) 958 { 959 struct sockaddr_storage ss; 960 int rc; 961 962 cifs_dbg(FYI, "%s: hostname=%.*s\n", __func__, (int)hostlen, host); 963 964 *result = false; 965 966 rc = dns_resolve_name(server->dns_dom, host, hostlen, 967 (struct sockaddr *)&ss); 968 if (rc < 0) 969 return rc; 970 971 spin_lock(&server->srv_lock); 972 *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr, (struct sockaddr *)&ss); 973 spin_unlock(&server->srv_lock); 974 cifs_dbg(FYI, "%s: ip addresses matched: %s\n", __func__, str_yes_no(*result)); 975 return 0; 976 } 977 978 int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix) 979 { 980 int rc; 981 982 kfree(cifs_sb->prepath); 983 cifs_sb->prepath = NULL; 984 985 if (prefix && *prefix) { 986 cifs_sb->prepath = cifs_sanitize_prepath(prefix, GFP_ATOMIC); 987 if (IS_ERR(cifs_sb->prepath)) { 988 rc = PTR_ERR(cifs_sb->prepath); 989 cifs_sb->prepath = NULL; 990 return rc; 991 } 992 if (cifs_sb->prepath) 993 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb)); 994 } 995 996 atomic_or(CIFS_MOUNT_USE_PREFIX_PATH, &cifs_sb->mnt_cifs_flags); 997 return 0; 998 } 999 1000 /* 1001 * Handle weird Windows SMB server behaviour. It responds with 1002 * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request for 1003 * "\<server>\<dfsname>\<linkpath>" DFS reference, where <dfsname> contains 1004 * non-ASCII unicode symbols. 1005 */ 1006 int cifs_inval_name_dfs_link_error(const unsigned int xid, 1007 struct cifs_tcon *tcon, 1008 struct cifs_sb_info *cifs_sb, 1009 const char *full_path, 1010 bool *islink) 1011 { 1012 struct TCP_Server_Info *server = tcon->ses->server; 1013 struct cifs_ses *ses = tcon->ses; 1014 size_t len; 1015 char *path; 1016 char *ref_path; 1017 1018 *islink = false; 1019 1020 /* 1021 * Fast path - skip check when @full_path doesn't have a prefix path to 1022 * look up or tcon is not DFS. 1023 */ 1024 if (strlen(full_path) < 2 || !cifs_sb || 1025 (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NO_DFS) || 1026 !is_tcon_dfs(tcon)) 1027 return 0; 1028 1029 spin_lock(&server->srv_lock); 1030 if (!server->leaf_fullpath) { 1031 spin_unlock(&server->srv_lock); 1032 return 0; 1033 } 1034 spin_unlock(&server->srv_lock); 1035 1036 /* 1037 * Slow path - tcon is DFS and @full_path has prefix path, so attempt 1038 * to get a referral to figure out whether it is an DFS link. 1039 */ 1040 len = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1) + strlen(full_path) + 1; 1041 path = kmalloc(len, GFP_KERNEL); 1042 if (!path) 1043 return -ENOMEM; 1044 1045 scnprintf(path, len, "%s%s", tcon->tree_name, full_path); 1046 ref_path = dfs_cache_canonical_path(path + 1, cifs_sb->local_nls, 1047 cifs_remap(cifs_sb)); 1048 kfree(path); 1049 1050 if (IS_ERR(ref_path)) { 1051 if (PTR_ERR(ref_path) != -EINVAL) 1052 return PTR_ERR(ref_path); 1053 } else { 1054 struct dfs_info3_param *refs = NULL; 1055 int num_refs = 0; 1056 1057 /* 1058 * XXX: we are not using dfs_cache_find() here because we might 1059 * end up filling all the DFS cache and thus potentially 1060 * removing cached DFS targets that the client would eventually 1061 * need during failover. 1062 */ 1063 ses = CIFS_DFS_ROOT_SES(ses); 1064 if (ses->server->ops->get_dfs_refer && 1065 !ses->server->ops->get_dfs_refer(xid, ses, ref_path, &refs, 1066 &num_refs, cifs_sb->local_nls, 1067 cifs_remap(cifs_sb))) 1068 *islink = refs[0].server_type == DFS_TYPE_LINK; 1069 free_dfs_info_array(refs, num_refs); 1070 kfree(ref_path); 1071 } 1072 return 0; 1073 } 1074 #endif 1075 1076 int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, bool retry) 1077 { 1078 int timeout = 10; 1079 int rc; 1080 1081 spin_lock(&server->srv_lock); 1082 if (server->tcpStatus != CifsNeedReconnect) { 1083 spin_unlock(&server->srv_lock); 1084 return 0; 1085 } 1086 timeout *= server->nr_targets; 1087 spin_unlock(&server->srv_lock); 1088 1089 /* 1090 * Give demultiplex thread up to 10 seconds to each target available for 1091 * reconnect -- should be greater than cifs socket timeout which is 7 1092 * seconds. 1093 * 1094 * On "soft" mounts we wait once. Hard mounts keep retrying until 1095 * process is killed or server comes back on-line. 1096 */ 1097 do { 1098 rc = wait_event_interruptible_timeout(server->response_q, 1099 (server->tcpStatus != CifsNeedReconnect), 1100 timeout * HZ); 1101 if (rc < 0) { 1102 cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n", 1103 __func__); 1104 return -ERESTARTSYS; 1105 } 1106 1107 /* are we still trying to reconnect? */ 1108 spin_lock(&server->srv_lock); 1109 if (server->tcpStatus != CifsNeedReconnect) { 1110 spin_unlock(&server->srv_lock); 1111 return 0; 1112 } 1113 spin_unlock(&server->srv_lock); 1114 } while (retry); 1115 1116 cifs_dbg(FYI, "%s: gave up waiting on reconnect\n", __func__); 1117 return -EHOSTDOWN; 1118 } 1119