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(sizeof(struct cifs_ses), GFP_KERNEL); 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(sizeof(*ret_buf), GFP_KERNEL); 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 /* NB: MID can not be set if treeCon not passed in, in that 266 case it is responsibility of caller to set the mid */ 267 unsigned int 268 header_assemble(struct smb_hdr *buffer, char smb_command, 269 const struct cifs_tcon *treeCon, int word_count 270 /* length of fixed section (word count) in two byte units */) 271 { 272 unsigned int in_len; 273 char *temp = (char *) buffer; 274 275 memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */ 276 277 in_len = (2 * word_count) + sizeof(struct smb_hdr) + 278 2 /* for bcc field itself */; 279 280 buffer->Protocol[0] = 0xFF; 281 buffer->Protocol[1] = 'S'; 282 buffer->Protocol[2] = 'M'; 283 buffer->Protocol[3] = 'B'; 284 buffer->Command = smb_command; 285 buffer->Flags = 0x00; /* case sensitive */ 286 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES; 287 buffer->Pid = cpu_to_le16((__u16)current->tgid); 288 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16)); 289 if (treeCon) { 290 buffer->Tid = treeCon->tid; 291 if (treeCon->ses) { 292 if (treeCon->ses->capabilities & CAP_UNICODE) 293 buffer->Flags2 |= SMBFLG2_UNICODE; 294 if (treeCon->ses->capabilities & CAP_STATUS32) 295 buffer->Flags2 |= SMBFLG2_ERR_STATUS; 296 297 /* Uid is not converted */ 298 buffer->Uid = treeCon->ses->Suid; 299 if (treeCon->ses->server) 300 buffer->Mid = get_next_mid(treeCon->ses->server); 301 } 302 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS) 303 buffer->Flags2 |= SMBFLG2_DFS; 304 if (treeCon->nocase) 305 buffer->Flags |= SMBFLG_CASELESS; 306 if ((treeCon->ses) && (treeCon->ses->server)) 307 if (treeCon->ses->server->sign) 308 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 309 } 310 311 /* endian conversion of flags is now done just before sending */ 312 buffer->WordCount = (char) word_count; 313 return in_len; 314 } 315 316 bool 317 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv) 318 { 319 struct smb_hdr *buf = (struct smb_hdr *)buffer; 320 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf; 321 struct TCP_Server_Info *pserver; 322 struct cifs_ses *ses; 323 struct cifs_tcon *tcon; 324 struct cifsInodeInfo *pCifsInode; 325 struct cifsFileInfo *netfile; 326 327 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n"); 328 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) && 329 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) { 330 struct smb_com_transaction_change_notify_rsp *pSMBr = 331 (struct smb_com_transaction_change_notify_rsp *)buf; 332 struct file_notify_information *pnotify; 333 __u32 data_offset = 0; 334 size_t len = srv->total_read - srv->pdu_size; 335 336 if (get_bcc(buf) > sizeof(struct file_notify_information)) { 337 data_offset = le32_to_cpu(pSMBr->DataOffset); 338 339 if (data_offset > 340 len - sizeof(struct file_notify_information)) { 341 cifs_dbg(FYI, "Invalid data_offset %u\n", 342 data_offset); 343 return true; 344 } 345 pnotify = (struct file_notify_information *) 346 ((char *)&pSMBr->hdr.Protocol + data_offset); 347 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n", 348 pnotify->FileName, pnotify->Action); 349 /* cifs_dump_mem("Rcvd notify Data: ",buf, 350 sizeof(struct smb_hdr)+60); */ 351 return true; 352 } 353 if (pSMBr->hdr.Status.CifsError) { 354 cifs_dbg(FYI, "notify err 0x%x\n", 355 pSMBr->hdr.Status.CifsError); 356 return true; 357 } 358 return false; 359 } 360 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX) 361 return false; 362 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) { 363 /* no sense logging error on invalid handle on oplock 364 break - harmless race between close request and oplock 365 break response is expected from time to time writing out 366 large dirty files cached on the client */ 367 if ((NT_STATUS_INVALID_HANDLE) == 368 le32_to_cpu(pSMB->hdr.Status.CifsError)) { 369 cifs_dbg(FYI, "Invalid handle on oplock break\n"); 370 return true; 371 } else if (ERRbadfid == 372 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) { 373 return true; 374 } else { 375 return false; /* on valid oplock brk we get "request" */ 376 } 377 } 378 if (pSMB->hdr.WordCount != 8) 379 return false; 380 381 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n", 382 pSMB->LockType, pSMB->OplockLevel); 383 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)) 384 return false; 385 386 /* If server is a channel, select the primary channel */ 387 pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv; 388 389 /* look up tcon based on tid & uid */ 390 spin_lock(&cifs_tcp_ses_lock); 391 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 392 if (cifs_ses_exiting(ses)) 393 continue; 394 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 395 if (tcon->tid != buf->Tid) 396 continue; 397 398 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks); 399 spin_lock(&tcon->open_file_lock); 400 list_for_each_entry(netfile, &tcon->openFileList, tlist) { 401 if (pSMB->Fid != netfile->fid.netfid) 402 continue; 403 404 cifs_dbg(FYI, "file id match, oplock break\n"); 405 pCifsInode = CIFS_I(d_inode(netfile->dentry)); 406 407 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, 408 &pCifsInode->flags); 409 410 netfile->oplock_epoch = 0; 411 netfile->oplock_level = pSMB->OplockLevel; 412 netfile->oplock_break_cancelled = false; 413 cifs_queue_oplock_break(netfile); 414 415 spin_unlock(&tcon->open_file_lock); 416 spin_unlock(&cifs_tcp_ses_lock); 417 return true; 418 } 419 spin_unlock(&tcon->open_file_lock); 420 spin_unlock(&cifs_tcp_ses_lock); 421 cifs_dbg(FYI, "No matching file for oplock break\n"); 422 return true; 423 } 424 } 425 spin_unlock(&cifs_tcp_ses_lock); 426 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n"); 427 return true; 428 } 429 430 void 431 dump_smb(void *buf, int smb_buf_length) 432 { 433 if (traceSMB == 0) 434 return; 435 436 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf, 437 smb_buf_length, true); 438 } 439 440 void 441 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb) 442 { 443 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 444 struct cifs_tcon *tcon = NULL; 445 446 if (cifs_sb->master_tlink) 447 tcon = cifs_sb_master_tcon(cifs_sb); 448 449 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; 450 cifs_sb->mnt_cifs_serverino_autodisabled = true; 451 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n", 452 tcon ? tcon->tree_name : "new server"); 453 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n"); 454 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n"); 455 456 } 457 } 458 459 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock) 460 { 461 oplock &= 0xF; 462 463 if (oplock == OPLOCK_EXCLUSIVE) { 464 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG; 465 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n", 466 &cinode->netfs.inode); 467 } else if (oplock == OPLOCK_READ) { 468 cinode->oplock = CIFS_CACHE_READ_FLG; 469 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n", 470 &cinode->netfs.inode); 471 } else 472 cinode->oplock = 0; 473 } 474 475 /* 476 * We wait for oplock breaks to be processed before we attempt to perform 477 * writes. 478 */ 479 int cifs_get_writer(struct cifsInodeInfo *cinode) 480 { 481 int rc; 482 483 start: 484 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK, 485 TASK_KILLABLE); 486 if (rc) 487 return rc; 488 489 spin_lock(&cinode->writers_lock); 490 if (!cinode->writers) 491 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 492 cinode->writers++; 493 /* Check to see if we have started servicing an oplock break */ 494 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) { 495 cinode->writers--; 496 if (cinode->writers == 0) { 497 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 498 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 499 } 500 spin_unlock(&cinode->writers_lock); 501 goto start; 502 } 503 spin_unlock(&cinode->writers_lock); 504 return 0; 505 } 506 507 void cifs_put_writer(struct cifsInodeInfo *cinode) 508 { 509 spin_lock(&cinode->writers_lock); 510 cinode->writers--; 511 if (cinode->writers == 0) { 512 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 513 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 514 } 515 spin_unlock(&cinode->writers_lock); 516 } 517 518 /** 519 * cifs_queue_oplock_break - queue the oplock break handler for cfile 520 * @cfile: The file to break the oplock on 521 * 522 * This function is called from the demultiplex thread when it 523 * receives an oplock break for @cfile. 524 * 525 * Assumes the tcon->open_file_lock is held. 526 * Assumes cfile->file_info_lock is NOT held. 527 */ 528 void cifs_queue_oplock_break(struct cifsFileInfo *cfile) 529 { 530 /* 531 * Bump the handle refcount now while we hold the 532 * open_file_lock to enforce the validity of it for the oplock 533 * break handler. The matching put is done at the end of the 534 * handler. 535 */ 536 cifsFileInfo_get(cfile); 537 538 queue_work(cifsoplockd_wq, &cfile->oplock_break); 539 } 540 541 void cifs_done_oplock_break(struct cifsInodeInfo *cinode) 542 { 543 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags); 544 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK); 545 } 546 547 bool 548 backup_cred(struct cifs_sb_info *cifs_sb) 549 { 550 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) { 551 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid())) 552 return true; 553 } 554 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) { 555 if (in_group_p(cifs_sb->ctx->backupgid)) 556 return true; 557 } 558 559 return false; 560 } 561 562 void 563 cifs_del_pending_open(struct cifs_pending_open *open) 564 { 565 spin_lock(&tlink_tcon(open->tlink)->open_file_lock); 566 list_del(&open->olist); 567 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 568 } 569 570 void 571 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink, 572 struct cifs_pending_open *open) 573 { 574 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE); 575 open->oplock = CIFS_OPLOCK_NO_CHANGE; 576 open->tlink = tlink; 577 fid->pending_open = open; 578 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens); 579 } 580 581 void 582 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink, 583 struct cifs_pending_open *open) 584 { 585 spin_lock(&tlink_tcon(tlink)->open_file_lock); 586 cifs_add_pending_open_locked(fid, tlink, open); 587 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 588 } 589 590 /* 591 * Critical section which runs after acquiring deferred_lock. 592 * As there is no reference count on cifs_deferred_close, pdclose 593 * should not be used outside deferred_lock. 594 */ 595 bool 596 cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose) 597 { 598 struct cifs_deferred_close *dclose; 599 600 list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) { 601 if ((dclose->netfid == cfile->fid.netfid) && 602 (dclose->persistent_fid == cfile->fid.persistent_fid) && 603 (dclose->volatile_fid == cfile->fid.volatile_fid)) { 604 *pdclose = dclose; 605 return true; 606 } 607 } 608 return false; 609 } 610 611 /* 612 * Critical section which runs after acquiring deferred_lock. 613 */ 614 void 615 cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose) 616 { 617 bool is_deferred = false; 618 struct cifs_deferred_close *pdclose; 619 620 is_deferred = cifs_is_deferred_close(cfile, &pdclose); 621 if (is_deferred) { 622 kfree(dclose); 623 return; 624 } 625 626 dclose->tlink = cfile->tlink; 627 dclose->netfid = cfile->fid.netfid; 628 dclose->persistent_fid = cfile->fid.persistent_fid; 629 dclose->volatile_fid = cfile->fid.volatile_fid; 630 list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes); 631 } 632 633 /* 634 * Critical section which runs after acquiring deferred_lock. 635 */ 636 void 637 cifs_del_deferred_close(struct cifsFileInfo *cfile) 638 { 639 bool is_deferred = false; 640 struct cifs_deferred_close *dclose; 641 642 is_deferred = cifs_is_deferred_close(cfile, &dclose); 643 if (!is_deferred) 644 return; 645 list_del(&dclose->dlist); 646 kfree(dclose); 647 } 648 649 void 650 cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) 651 { 652 struct cifsFileInfo *cfile = NULL; 653 struct file_list *tmp_list, *tmp_next_list; 654 LIST_HEAD(file_head); 655 656 if (cifs_inode == NULL) 657 return; 658 659 spin_lock(&cifs_inode->open_file_lock); 660 list_for_each_entry(cfile, &cifs_inode->openFileList, flist) { 661 if (delayed_work_pending(&cfile->deferred)) { 662 if (cancel_delayed_work(&cfile->deferred)) { 663 spin_lock(&cifs_inode->deferred_lock); 664 cifs_del_deferred_close(cfile); 665 spin_unlock(&cifs_inode->deferred_lock); 666 667 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 668 if (tmp_list == NULL) 669 break; 670 tmp_list->cfile = cfile; 671 list_add_tail(&tmp_list->list, &file_head); 672 } 673 } 674 } 675 spin_unlock(&cifs_inode->open_file_lock); 676 677 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 678 _cifsFileInfo_put(tmp_list->cfile, false, false); 679 list_del(&tmp_list->list); 680 kfree(tmp_list); 681 } 682 } 683 684 void 685 cifs_close_all_deferred_files(struct cifs_tcon *tcon) 686 { 687 struct cifsFileInfo *cfile; 688 struct file_list *tmp_list, *tmp_next_list; 689 LIST_HEAD(file_head); 690 691 spin_lock(&tcon->open_file_lock); 692 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 693 if (delayed_work_pending(&cfile->deferred)) { 694 if (cancel_delayed_work(&cfile->deferred)) { 695 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 696 cifs_del_deferred_close(cfile); 697 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 698 699 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 700 if (tmp_list == NULL) 701 break; 702 tmp_list->cfile = cfile; 703 list_add_tail(&tmp_list->list, &file_head); 704 } 705 } 706 } 707 spin_unlock(&tcon->open_file_lock); 708 709 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 710 _cifsFileInfo_put(tmp_list->cfile, true, false); 711 list_del(&tmp_list->list); 712 kfree(tmp_list); 713 } 714 } 715 716 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, 717 struct dentry *dentry) 718 { 719 struct file_list *tmp_list, *tmp_next_list; 720 struct cifsFileInfo *cfile; 721 LIST_HEAD(file_head); 722 723 spin_lock(&tcon->open_file_lock); 724 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 725 if ((cfile->dentry == dentry) && 726 delayed_work_pending(&cfile->deferred) && 727 cancel_delayed_work(&cfile->deferred)) { 728 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 729 cifs_del_deferred_close(cfile); 730 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 731 732 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 733 if (tmp_list == NULL) 734 break; 735 tmp_list->cfile = cfile; 736 list_add_tail(&tmp_list->list, &file_head); 737 } 738 } 739 spin_unlock(&tcon->open_file_lock); 740 741 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 742 _cifsFileInfo_put(tmp_list->cfile, true, false); 743 list_del(&tmp_list->list); 744 kfree(tmp_list); 745 } 746 } 747 748 /* 749 * If a dentry has been deleted, all corresponding open handles should know that 750 * so that we do not defer close them. 751 */ 752 void cifs_mark_open_handles_for_deleted_file(struct inode *inode, 753 const char *path) 754 { 755 struct cifsFileInfo *cfile; 756 void *page; 757 const char *full_path; 758 struct cifsInodeInfo *cinode = CIFS_I(inode); 759 760 page = alloc_dentry_path(); 761 spin_lock(&cinode->open_file_lock); 762 763 /* 764 * note: we need to construct path from dentry and compare only if the 765 * inode has any hardlinks. When number of hardlinks is 1, we can just 766 * mark all open handles since they are going to be from the same file. 767 */ 768 if (inode->i_nlink > 1) { 769 list_for_each_entry(cfile, &cinode->openFileList, flist) { 770 full_path = build_path_from_dentry(cfile->dentry, page); 771 if (!IS_ERR(full_path) && strcmp(full_path, path) == 0) 772 cfile->status_file_deleted = true; 773 } 774 } else { 775 list_for_each_entry(cfile, &cinode->openFileList, flist) 776 cfile->status_file_deleted = true; 777 } 778 spin_unlock(&cinode->open_file_lock); 779 free_dentry_path(page); 780 } 781 782 /* parses DFS referral V3 structure 783 * caller is responsible for freeing target_nodes 784 * returns: 785 * - on success - 0 786 * - on failure - errno 787 */ 788 int 789 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size, 790 unsigned int *num_of_nodes, 791 struct dfs_info3_param **target_nodes, 792 const struct nls_table *nls_codepage, int remap, 793 const char *searchName, bool is_unicode) 794 { 795 int i, rc = 0; 796 char *data_end; 797 struct dfs_referral_level_3 *ref; 798 799 if (rsp_size < sizeof(*rsp)) { 800 cifs_dbg(VFS | ONCE, 801 "%s: header is malformed (size is %u, must be %zu)\n", 802 __func__, rsp_size, sizeof(*rsp)); 803 rc = -EINVAL; 804 goto parse_DFS_referrals_exit; 805 } 806 807 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals); 808 809 if (*num_of_nodes < 1) { 810 cifs_dbg(VFS | ONCE, "%s: [path=%s] num_referrals must be at least > 0, but we got %d\n", 811 __func__, searchName, *num_of_nodes); 812 rc = -ENOENT; 813 goto parse_DFS_referrals_exit; 814 } 815 816 if (sizeof(*rsp) + *num_of_nodes * sizeof(REFERRAL3) > rsp_size) { 817 cifs_dbg(VFS | ONCE, 818 "%s: malformed buffer (size is %u, must be at least %zu)\n", 819 __func__, rsp_size, 820 sizeof(*rsp) + *num_of_nodes * sizeof(REFERRAL3)); 821 rc = -EINVAL; 822 goto parse_DFS_referrals_exit; 823 } 824 825 ref = (struct dfs_referral_level_3 *) &(rsp->referrals); 826 if (ref->VersionNumber != cpu_to_le16(3)) { 827 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n", 828 le16_to_cpu(ref->VersionNumber)); 829 rc = -EINVAL; 830 goto parse_DFS_referrals_exit; 831 } 832 833 /* get the upper boundary of the resp buffer */ 834 data_end = (char *)rsp + rsp_size; 835 836 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n", 837 *num_of_nodes, le32_to_cpu(rsp->DFSFlags)); 838 839 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param), 840 GFP_KERNEL); 841 if (*target_nodes == NULL) { 842 rc = -ENOMEM; 843 goto parse_DFS_referrals_exit; 844 } 845 846 /* collect necessary data from referrals */ 847 for (i = 0; i < *num_of_nodes; i++) { 848 char *temp; 849 int max_len; 850 struct dfs_info3_param *node = (*target_nodes)+i; 851 852 node->flags = le32_to_cpu(rsp->DFSFlags); 853 if (is_unicode) { 854 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2, 855 GFP_KERNEL); 856 if (tmp == NULL) { 857 rc = -ENOMEM; 858 goto parse_DFS_referrals_exit; 859 } 860 cifsConvertToUTF16((__le16 *) tmp, searchName, 861 PATH_MAX, nls_codepage, remap); 862 node->path_consumed = cifs_utf16_bytes(tmp, 863 le16_to_cpu(rsp->PathConsumed), 864 nls_codepage); 865 kfree(tmp); 866 } else 867 node->path_consumed = le16_to_cpu(rsp->PathConsumed); 868 869 node->server_type = le16_to_cpu(ref->ServerType); 870 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags); 871 872 /* copy DfsPath */ 873 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset); 874 max_len = data_end - temp; 875 node->path_name = cifs_strndup_from_utf16(temp, max_len, 876 is_unicode, nls_codepage); 877 if (!node->path_name) { 878 rc = -ENOMEM; 879 goto parse_DFS_referrals_exit; 880 } 881 882 /* copy link target UNC */ 883 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset); 884 max_len = data_end - temp; 885 node->node_name = cifs_strndup_from_utf16(temp, max_len, 886 is_unicode, nls_codepage); 887 if (!node->node_name) { 888 rc = -ENOMEM; 889 goto parse_DFS_referrals_exit; 890 } 891 892 node->ttl = le32_to_cpu(ref->TimeToLive); 893 894 ref++; 895 } 896 897 parse_DFS_referrals_exit: 898 if (rc) { 899 free_dfs_info_array(*target_nodes, *num_of_nodes); 900 *target_nodes = NULL; 901 *num_of_nodes = 0; 902 } 903 return rc; 904 } 905 906 /** 907 * cifs_alloc_hash - allocate hash and hash context together 908 * @name: The name of the crypto hash algo 909 * @sdesc: SHASH descriptor where to put the pointer to the hash TFM 910 * 911 * The caller has to make sure @sdesc is initialized to either NULL or 912 * a valid context. It can be freed via cifs_free_hash(). 913 */ 914 int 915 cifs_alloc_hash(const char *name, struct shash_desc **sdesc) 916 { 917 int rc = 0; 918 struct crypto_shash *alg = NULL; 919 920 if (*sdesc) 921 return 0; 922 923 alg = crypto_alloc_shash(name, 0, 0); 924 if (IS_ERR(alg)) { 925 cifs_dbg(VFS, "Could not allocate shash TFM '%s'\n", name); 926 rc = PTR_ERR(alg); 927 *sdesc = NULL; 928 return rc; 929 } 930 931 *sdesc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(alg), GFP_KERNEL); 932 if (*sdesc == NULL) { 933 cifs_dbg(VFS, "no memory left to allocate shash TFM '%s'\n", name); 934 crypto_free_shash(alg); 935 return -ENOMEM; 936 } 937 938 (*sdesc)->tfm = alg; 939 return 0; 940 } 941 942 /** 943 * cifs_free_hash - free hash and hash context together 944 * @sdesc: Where to find the pointer to the hash TFM 945 * 946 * Freeing a NULL descriptor is safe. 947 */ 948 void 949 cifs_free_hash(struct shash_desc **sdesc) 950 { 951 if (unlikely(!sdesc) || !*sdesc) 952 return; 953 954 if ((*sdesc)->tfm) { 955 crypto_free_shash((*sdesc)->tfm); 956 (*sdesc)->tfm = NULL; 957 } 958 959 kfree_sensitive(*sdesc); 960 *sdesc = NULL; 961 } 962 963 void extract_unc_hostname(const char *unc, const char **h, size_t *len) 964 { 965 const char *end; 966 967 /* skip initial slashes */ 968 while (*unc && (*unc == '\\' || *unc == '/')) 969 unc++; 970 971 end = unc; 972 973 while (*end && !(*end == '\\' || *end == '/')) 974 end++; 975 976 *h = unc; 977 *len = end - unc; 978 } 979 980 /** 981 * copy_path_name - copy src path to dst, possibly truncating 982 * @dst: The destination buffer 983 * @src: The source name 984 * 985 * returns number of bytes written (including trailing nul) 986 */ 987 int copy_path_name(char *dst, const char *src) 988 { 989 int name_len; 990 991 /* 992 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it 993 * will truncate and strlen(dst) will be PATH_MAX-1 994 */ 995 name_len = strscpy(dst, src, PATH_MAX); 996 if (WARN_ON_ONCE(name_len < 0)) 997 name_len = PATH_MAX-1; 998 999 /* we count the trailing nul */ 1000 name_len++; 1001 return name_len; 1002 } 1003 1004 struct super_cb_data { 1005 void *data; 1006 struct super_block *sb; 1007 }; 1008 1009 static void tcon_super_cb(struct super_block *sb, void *arg) 1010 { 1011 struct super_cb_data *sd = arg; 1012 struct cifs_sb_info *cifs_sb; 1013 struct cifs_tcon *t1 = sd->data, *t2; 1014 1015 if (sd->sb) 1016 return; 1017 1018 cifs_sb = CIFS_SB(sb); 1019 t2 = cifs_sb_master_tcon(cifs_sb); 1020 1021 spin_lock(&t2->tc_lock); 1022 if ((t1->ses == t2->ses || 1023 t1->ses->dfs_root_ses == t2->ses->dfs_root_ses) && 1024 t1->ses->server == t2->ses->server && 1025 t2->origin_fullpath && 1026 dfs_src_pathname_equal(t2->origin_fullpath, t1->origin_fullpath)) 1027 sd->sb = sb; 1028 spin_unlock(&t2->tc_lock); 1029 } 1030 1031 static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *), 1032 void *data) 1033 { 1034 struct super_cb_data sd = { 1035 .data = data, 1036 .sb = NULL, 1037 }; 1038 struct file_system_type **fs_type = (struct file_system_type *[]) { 1039 &cifs_fs_type, &smb3_fs_type, NULL, 1040 }; 1041 1042 for (; *fs_type; fs_type++) { 1043 iterate_supers_type(*fs_type, f, &sd); 1044 if (sd.sb) { 1045 /* 1046 * Grab an active reference in order to prevent automounts (DFS links) 1047 * of expiring and then freeing up our cifs superblock pointer while 1048 * we're doing failover. 1049 */ 1050 cifs_sb_active(sd.sb); 1051 return sd.sb; 1052 } 1053 } 1054 pr_warn_once("%s: could not find dfs superblock\n", __func__); 1055 return ERR_PTR(-EINVAL); 1056 } 1057 1058 static void __cifs_put_super(struct super_block *sb) 1059 { 1060 if (!IS_ERR_OR_NULL(sb)) 1061 cifs_sb_deactive(sb); 1062 } 1063 1064 struct super_block *cifs_get_dfs_tcon_super(struct cifs_tcon *tcon) 1065 { 1066 spin_lock(&tcon->tc_lock); 1067 if (!tcon->origin_fullpath) { 1068 spin_unlock(&tcon->tc_lock); 1069 return ERR_PTR(-ENOENT); 1070 } 1071 spin_unlock(&tcon->tc_lock); 1072 return __cifs_get_super(tcon_super_cb, tcon); 1073 } 1074 1075 void cifs_put_tcp_super(struct super_block *sb) 1076 { 1077 __cifs_put_super(sb); 1078 } 1079 1080 #ifdef CONFIG_CIFS_DFS_UPCALL 1081 int match_target_ip(struct TCP_Server_Info *server, 1082 const char *host, size_t hostlen, 1083 bool *result) 1084 { 1085 struct sockaddr_storage ss; 1086 int rc; 1087 1088 cifs_dbg(FYI, "%s: hostname=%.*s\n", __func__, (int)hostlen, host); 1089 1090 *result = false; 1091 1092 rc = dns_resolve_name(server->dns_dom, host, hostlen, 1093 (struct sockaddr *)&ss); 1094 if (rc < 0) 1095 return rc; 1096 1097 spin_lock(&server->srv_lock); 1098 *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr, (struct sockaddr *)&ss); 1099 spin_unlock(&server->srv_lock); 1100 cifs_dbg(FYI, "%s: ip addresses matched: %s\n", __func__, str_yes_no(*result)); 1101 return 0; 1102 } 1103 1104 int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix) 1105 { 1106 int rc; 1107 1108 kfree(cifs_sb->prepath); 1109 cifs_sb->prepath = NULL; 1110 1111 if (prefix && *prefix) { 1112 cifs_sb->prepath = cifs_sanitize_prepath(prefix, GFP_ATOMIC); 1113 if (IS_ERR(cifs_sb->prepath)) { 1114 rc = PTR_ERR(cifs_sb->prepath); 1115 cifs_sb->prepath = NULL; 1116 return rc; 1117 } 1118 if (cifs_sb->prepath) 1119 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb)); 1120 } 1121 1122 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 1123 return 0; 1124 } 1125 1126 /* 1127 * Handle weird Windows SMB server behaviour. It responds with 1128 * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request for 1129 * "\<server>\<dfsname>\<linkpath>" DFS reference, where <dfsname> contains 1130 * non-ASCII unicode symbols. 1131 */ 1132 int cifs_inval_name_dfs_link_error(const unsigned int xid, 1133 struct cifs_tcon *tcon, 1134 struct cifs_sb_info *cifs_sb, 1135 const char *full_path, 1136 bool *islink) 1137 { 1138 struct TCP_Server_Info *server = tcon->ses->server; 1139 struct cifs_ses *ses = tcon->ses; 1140 size_t len; 1141 char *path; 1142 char *ref_path; 1143 1144 *islink = false; 1145 1146 /* 1147 * Fast path - skip check when @full_path doesn't have a prefix path to 1148 * look up or tcon is not DFS. 1149 */ 1150 if (strlen(full_path) < 2 || !cifs_sb || 1151 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) || 1152 !is_tcon_dfs(tcon)) 1153 return 0; 1154 1155 spin_lock(&server->srv_lock); 1156 if (!server->leaf_fullpath) { 1157 spin_unlock(&server->srv_lock); 1158 return 0; 1159 } 1160 spin_unlock(&server->srv_lock); 1161 1162 /* 1163 * Slow path - tcon is DFS and @full_path has prefix path, so attempt 1164 * to get a referral to figure out whether it is an DFS link. 1165 */ 1166 len = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1) + strlen(full_path) + 1; 1167 path = kmalloc(len, GFP_KERNEL); 1168 if (!path) 1169 return -ENOMEM; 1170 1171 scnprintf(path, len, "%s%s", tcon->tree_name, full_path); 1172 ref_path = dfs_cache_canonical_path(path + 1, cifs_sb->local_nls, 1173 cifs_remap(cifs_sb)); 1174 kfree(path); 1175 1176 if (IS_ERR(ref_path)) { 1177 if (PTR_ERR(ref_path) != -EINVAL) 1178 return PTR_ERR(ref_path); 1179 } else { 1180 struct dfs_info3_param *refs = NULL; 1181 int num_refs = 0; 1182 1183 /* 1184 * XXX: we are not using dfs_cache_find() here because we might 1185 * end up filling all the DFS cache and thus potentially 1186 * removing cached DFS targets that the client would eventually 1187 * need during failover. 1188 */ 1189 ses = CIFS_DFS_ROOT_SES(ses); 1190 if (ses->server->ops->get_dfs_refer && 1191 !ses->server->ops->get_dfs_refer(xid, ses, ref_path, &refs, 1192 &num_refs, cifs_sb->local_nls, 1193 cifs_remap(cifs_sb))) 1194 *islink = refs[0].server_type == DFS_TYPE_LINK; 1195 free_dfs_info_array(refs, num_refs); 1196 kfree(ref_path); 1197 } 1198 return 0; 1199 } 1200 #endif 1201 1202 int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, bool retry) 1203 { 1204 int timeout = 10; 1205 int rc; 1206 1207 spin_lock(&server->srv_lock); 1208 if (server->tcpStatus != CifsNeedReconnect) { 1209 spin_unlock(&server->srv_lock); 1210 return 0; 1211 } 1212 timeout *= server->nr_targets; 1213 spin_unlock(&server->srv_lock); 1214 1215 /* 1216 * Give demultiplex thread up to 10 seconds to each target available for 1217 * reconnect -- should be greater than cifs socket timeout which is 7 1218 * seconds. 1219 * 1220 * On "soft" mounts we wait once. Hard mounts keep retrying until 1221 * process is killed or server comes back on-line. 1222 */ 1223 do { 1224 rc = wait_event_interruptible_timeout(server->response_q, 1225 (server->tcpStatus != CifsNeedReconnect), 1226 timeout * HZ); 1227 if (rc < 0) { 1228 cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n", 1229 __func__); 1230 return -ERESTARTSYS; 1231 } 1232 1233 /* are we still trying to reconnect? */ 1234 spin_lock(&server->srv_lock); 1235 if (server->tcpStatus != CifsNeedReconnect) { 1236 spin_unlock(&server->srv_lock); 1237 return 0; 1238 } 1239 spin_unlock(&server->srv_lock); 1240 } while (retry); 1241 1242 cifs_dbg(FYI, "%s: gave up waiting on reconnect\n", __func__); 1243 return -EHOSTDOWN; 1244 } 1245