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