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