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