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