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