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 void 836 cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path) 837 { 838 struct cifsFileInfo *cfile; 839 struct file_list *tmp_list, *tmp_next_list; 840 void *page; 841 const char *full_path; 842 LIST_HEAD(file_head); 843 844 page = alloc_dentry_path(); 845 spin_lock(&tcon->open_file_lock); 846 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 847 full_path = build_path_from_dentry(cfile->dentry, page); 848 if (strstr(full_path, path)) { 849 if (delayed_work_pending(&cfile->deferred)) { 850 if (cancel_delayed_work(&cfile->deferred)) { 851 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 852 cifs_del_deferred_close(cfile); 853 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 854 855 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 856 if (tmp_list == NULL) 857 break; 858 tmp_list->cfile = cfile; 859 list_add_tail(&tmp_list->list, &file_head); 860 } 861 } 862 } 863 } 864 spin_unlock(&tcon->open_file_lock); 865 866 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 867 _cifsFileInfo_put(tmp_list->cfile, true, false); 868 list_del(&tmp_list->list); 869 kfree(tmp_list); 870 } 871 free_dentry_path(page); 872 } 873 874 /* 875 * If a dentry has been deleted, all corresponding open handles should know that 876 * so that we do not defer close them. 877 */ 878 void cifs_mark_open_handles_for_deleted_file(struct inode *inode, 879 const char *path) 880 { 881 struct cifsFileInfo *cfile; 882 void *page; 883 const char *full_path; 884 struct cifsInodeInfo *cinode = CIFS_I(inode); 885 886 page = alloc_dentry_path(); 887 spin_lock(&cinode->open_file_lock); 888 889 /* 890 * note: we need to construct path from dentry and compare only if the 891 * inode has any hardlinks. When number of hardlinks is 1, we can just 892 * mark all open handles since they are going to be from the same file. 893 */ 894 if (inode->i_nlink > 1) { 895 list_for_each_entry(cfile, &cinode->openFileList, flist) { 896 full_path = build_path_from_dentry(cfile->dentry, page); 897 if (!IS_ERR(full_path) && strcmp(full_path, path) == 0) 898 cfile->status_file_deleted = true; 899 } 900 } else { 901 list_for_each_entry(cfile, &cinode->openFileList, flist) 902 cfile->status_file_deleted = true; 903 } 904 spin_unlock(&cinode->open_file_lock); 905 free_dentry_path(page); 906 } 907 908 /* parses DFS referral V3 structure 909 * caller is responsible for freeing target_nodes 910 * returns: 911 * - on success - 0 912 * - on failure - errno 913 */ 914 int 915 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size, 916 unsigned int *num_of_nodes, 917 struct dfs_info3_param **target_nodes, 918 const struct nls_table *nls_codepage, int remap, 919 const char *searchName, bool is_unicode) 920 { 921 int i, rc = 0; 922 char *data_end; 923 struct dfs_referral_level_3 *ref; 924 925 *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals); 926 927 if (*num_of_nodes < 1) { 928 cifs_dbg(VFS | ONCE, "%s: [path=%s] num_referrals must be at least > 0, but we got %d\n", 929 __func__, searchName, *num_of_nodes); 930 rc = -ENOENT; 931 goto parse_DFS_referrals_exit; 932 } 933 934 ref = (struct dfs_referral_level_3 *) &(rsp->referrals); 935 if (ref->VersionNumber != cpu_to_le16(3)) { 936 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n", 937 le16_to_cpu(ref->VersionNumber)); 938 rc = -EINVAL; 939 goto parse_DFS_referrals_exit; 940 } 941 942 /* get the upper boundary of the resp buffer */ 943 data_end = (char *)rsp + rsp_size; 944 945 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n", 946 *num_of_nodes, le32_to_cpu(rsp->DFSFlags)); 947 948 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param), 949 GFP_KERNEL); 950 if (*target_nodes == NULL) { 951 rc = -ENOMEM; 952 goto parse_DFS_referrals_exit; 953 } 954 955 /* collect necessary data from referrals */ 956 for (i = 0; i < *num_of_nodes; i++) { 957 char *temp; 958 int max_len; 959 struct dfs_info3_param *node = (*target_nodes)+i; 960 961 node->flags = le32_to_cpu(rsp->DFSFlags); 962 if (is_unicode) { 963 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2, 964 GFP_KERNEL); 965 if (tmp == NULL) { 966 rc = -ENOMEM; 967 goto parse_DFS_referrals_exit; 968 } 969 cifsConvertToUTF16((__le16 *) tmp, searchName, 970 PATH_MAX, nls_codepage, remap); 971 node->path_consumed = cifs_utf16_bytes(tmp, 972 le16_to_cpu(rsp->PathConsumed), 973 nls_codepage); 974 kfree(tmp); 975 } else 976 node->path_consumed = le16_to_cpu(rsp->PathConsumed); 977 978 node->server_type = le16_to_cpu(ref->ServerType); 979 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags); 980 981 /* copy DfsPath */ 982 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset); 983 max_len = data_end - temp; 984 node->path_name = cifs_strndup_from_utf16(temp, max_len, 985 is_unicode, nls_codepage); 986 if (!node->path_name) { 987 rc = -ENOMEM; 988 goto parse_DFS_referrals_exit; 989 } 990 991 /* copy link target UNC */ 992 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset); 993 max_len = data_end - temp; 994 node->node_name = cifs_strndup_from_utf16(temp, max_len, 995 is_unicode, nls_codepage); 996 if (!node->node_name) { 997 rc = -ENOMEM; 998 goto parse_DFS_referrals_exit; 999 } 1000 1001 node->ttl = le32_to_cpu(ref->TimeToLive); 1002 1003 ref++; 1004 } 1005 1006 parse_DFS_referrals_exit: 1007 if (rc) { 1008 free_dfs_info_array(*target_nodes, *num_of_nodes); 1009 *target_nodes = NULL; 1010 *num_of_nodes = 0; 1011 } 1012 return rc; 1013 } 1014 1015 /** 1016 * cifs_alloc_hash - allocate hash and hash context together 1017 * @name: The name of the crypto hash algo 1018 * @sdesc: SHASH descriptor where to put the pointer to the hash TFM 1019 * 1020 * The caller has to make sure @sdesc is initialized to either NULL or 1021 * a valid context. It can be freed via cifs_free_hash(). 1022 */ 1023 int 1024 cifs_alloc_hash(const char *name, struct shash_desc **sdesc) 1025 { 1026 int rc = 0; 1027 struct crypto_shash *alg = NULL; 1028 1029 if (*sdesc) 1030 return 0; 1031 1032 alg = crypto_alloc_shash(name, 0, 0); 1033 if (IS_ERR(alg)) { 1034 cifs_dbg(VFS, "Could not allocate shash TFM '%s'\n", name); 1035 rc = PTR_ERR(alg); 1036 *sdesc = NULL; 1037 return rc; 1038 } 1039 1040 *sdesc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(alg), GFP_KERNEL); 1041 if (*sdesc == NULL) { 1042 cifs_dbg(VFS, "no memory left to allocate shash TFM '%s'\n", name); 1043 crypto_free_shash(alg); 1044 return -ENOMEM; 1045 } 1046 1047 (*sdesc)->tfm = alg; 1048 return 0; 1049 } 1050 1051 /** 1052 * cifs_free_hash - free hash and hash context together 1053 * @sdesc: Where to find the pointer to the hash TFM 1054 * 1055 * Freeing a NULL descriptor is safe. 1056 */ 1057 void 1058 cifs_free_hash(struct shash_desc **sdesc) 1059 { 1060 if (unlikely(!sdesc) || !*sdesc) 1061 return; 1062 1063 if ((*sdesc)->tfm) { 1064 crypto_free_shash((*sdesc)->tfm); 1065 (*sdesc)->tfm = NULL; 1066 } 1067 1068 kfree_sensitive(*sdesc); 1069 *sdesc = NULL; 1070 } 1071 1072 void extract_unc_hostname(const char *unc, const char **h, size_t *len) 1073 { 1074 const char *end; 1075 1076 /* skip initial slashes */ 1077 while (*unc && (*unc == '\\' || *unc == '/')) 1078 unc++; 1079 1080 end = unc; 1081 1082 while (*end && !(*end == '\\' || *end == '/')) 1083 end++; 1084 1085 *h = unc; 1086 *len = end - unc; 1087 } 1088 1089 /** 1090 * copy_path_name - copy src path to dst, possibly truncating 1091 * @dst: The destination buffer 1092 * @src: The source name 1093 * 1094 * returns number of bytes written (including trailing nul) 1095 */ 1096 int copy_path_name(char *dst, const char *src) 1097 { 1098 int name_len; 1099 1100 /* 1101 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it 1102 * will truncate and strlen(dst) will be PATH_MAX-1 1103 */ 1104 name_len = strscpy(dst, src, PATH_MAX); 1105 if (WARN_ON_ONCE(name_len < 0)) 1106 name_len = PATH_MAX-1; 1107 1108 /* we count the trailing nul */ 1109 name_len++; 1110 return name_len; 1111 } 1112 1113 struct super_cb_data { 1114 void *data; 1115 struct super_block *sb; 1116 }; 1117 1118 static void tcon_super_cb(struct super_block *sb, void *arg) 1119 { 1120 struct super_cb_data *sd = arg; 1121 struct cifs_sb_info *cifs_sb; 1122 struct cifs_tcon *t1 = sd->data, *t2; 1123 1124 if (sd->sb) 1125 return; 1126 1127 cifs_sb = CIFS_SB(sb); 1128 t2 = cifs_sb_master_tcon(cifs_sb); 1129 1130 spin_lock(&t2->tc_lock); 1131 if ((t1->ses == t2->ses || 1132 t1->ses->dfs_root_ses == t2->ses->dfs_root_ses) && 1133 t1->ses->server == t2->ses->server && 1134 t2->origin_fullpath && 1135 dfs_src_pathname_equal(t2->origin_fullpath, t1->origin_fullpath)) 1136 sd->sb = sb; 1137 spin_unlock(&t2->tc_lock); 1138 } 1139 1140 static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *), 1141 void *data) 1142 { 1143 struct super_cb_data sd = { 1144 .data = data, 1145 .sb = NULL, 1146 }; 1147 struct file_system_type **fs_type = (struct file_system_type *[]) { 1148 &cifs_fs_type, &smb3_fs_type, NULL, 1149 }; 1150 1151 for (; *fs_type; fs_type++) { 1152 iterate_supers_type(*fs_type, f, &sd); 1153 if (sd.sb) { 1154 /* 1155 * Grab an active reference in order to prevent automounts (DFS links) 1156 * of expiring and then freeing up our cifs superblock pointer while 1157 * we're doing failover. 1158 */ 1159 cifs_sb_active(sd.sb); 1160 return sd.sb; 1161 } 1162 } 1163 pr_warn_once("%s: could not find dfs superblock\n", __func__); 1164 return ERR_PTR(-EINVAL); 1165 } 1166 1167 static void __cifs_put_super(struct super_block *sb) 1168 { 1169 if (!IS_ERR_OR_NULL(sb)) 1170 cifs_sb_deactive(sb); 1171 } 1172 1173 struct super_block *cifs_get_dfs_tcon_super(struct cifs_tcon *tcon) 1174 { 1175 spin_lock(&tcon->tc_lock); 1176 if (!tcon->origin_fullpath) { 1177 spin_unlock(&tcon->tc_lock); 1178 return ERR_PTR(-ENOENT); 1179 } 1180 spin_unlock(&tcon->tc_lock); 1181 return __cifs_get_super(tcon_super_cb, tcon); 1182 } 1183 1184 void cifs_put_tcp_super(struct super_block *sb) 1185 { 1186 __cifs_put_super(sb); 1187 } 1188 1189 #ifdef CONFIG_CIFS_DFS_UPCALL 1190 int match_target_ip(struct TCP_Server_Info *server, 1191 const char *host, size_t hostlen, 1192 bool *result) 1193 { 1194 struct sockaddr_storage ss; 1195 int rc; 1196 1197 cifs_dbg(FYI, "%s: hostname=%.*s\n", __func__, (int)hostlen, host); 1198 1199 *result = false; 1200 1201 rc = dns_resolve_name(server->dns_dom, host, hostlen, 1202 (struct sockaddr *)&ss); 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 matched: %s\n", __func__, str_yes_no(*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 TCP_Server_Info *server = tcon->ses->server; 1248 struct cifs_ses *ses = tcon->ses; 1249 size_t len; 1250 char *path; 1251 char *ref_path; 1252 1253 *islink = false; 1254 1255 /* 1256 * Fast path - skip check when @full_path doesn't have a prefix path to 1257 * look up or tcon is not DFS. 1258 */ 1259 if (strlen(full_path) < 2 || !cifs_sb || 1260 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) || 1261 !is_tcon_dfs(tcon)) 1262 return 0; 1263 1264 spin_lock(&server->srv_lock); 1265 if (!server->leaf_fullpath) { 1266 spin_unlock(&server->srv_lock); 1267 return 0; 1268 } 1269 spin_unlock(&server->srv_lock); 1270 1271 /* 1272 * Slow path - tcon is DFS and @full_path has prefix path, so attempt 1273 * to get a referral to figure out whether it is an DFS link. 1274 */ 1275 len = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1) + strlen(full_path) + 1; 1276 path = kmalloc(len, GFP_KERNEL); 1277 if (!path) 1278 return -ENOMEM; 1279 1280 scnprintf(path, len, "%s%s", tcon->tree_name, full_path); 1281 ref_path = dfs_cache_canonical_path(path + 1, cifs_sb->local_nls, 1282 cifs_remap(cifs_sb)); 1283 kfree(path); 1284 1285 if (IS_ERR(ref_path)) { 1286 if (PTR_ERR(ref_path) != -EINVAL) 1287 return PTR_ERR(ref_path); 1288 } else { 1289 struct dfs_info3_param *refs = NULL; 1290 int num_refs = 0; 1291 1292 /* 1293 * XXX: we are not using dfs_cache_find() here because we might 1294 * end up filling all the DFS cache and thus potentially 1295 * removing cached DFS targets that the client would eventually 1296 * need during failover. 1297 */ 1298 ses = CIFS_DFS_ROOT_SES(ses); 1299 if (ses->server->ops->get_dfs_refer && 1300 !ses->server->ops->get_dfs_refer(xid, ses, ref_path, &refs, 1301 &num_refs, cifs_sb->local_nls, 1302 cifs_remap(cifs_sb))) 1303 *islink = refs[0].server_type == DFS_TYPE_LINK; 1304 free_dfs_info_array(refs, num_refs); 1305 kfree(ref_path); 1306 } 1307 return 0; 1308 } 1309 #endif 1310 1311 int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, bool retry) 1312 { 1313 int timeout = 10; 1314 int rc; 1315 1316 spin_lock(&server->srv_lock); 1317 if (server->tcpStatus != CifsNeedReconnect) { 1318 spin_unlock(&server->srv_lock); 1319 return 0; 1320 } 1321 timeout *= server->nr_targets; 1322 spin_unlock(&server->srv_lock); 1323 1324 /* 1325 * Give demultiplex thread up to 10 seconds to each target available for 1326 * reconnect -- should be greater than cifs socket timeout which is 7 1327 * seconds. 1328 * 1329 * On "soft" mounts we wait once. Hard mounts keep retrying until 1330 * process is killed or server comes back on-line. 1331 */ 1332 do { 1333 rc = wait_event_interruptible_timeout(server->response_q, 1334 (server->tcpStatus != CifsNeedReconnect), 1335 timeout * HZ); 1336 if (rc < 0) { 1337 cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n", 1338 __func__); 1339 return -ERESTARTSYS; 1340 } 1341 1342 /* are we still trying to reconnect? */ 1343 spin_lock(&server->srv_lock); 1344 if (server->tcpStatus != CifsNeedReconnect) { 1345 spin_unlock(&server->srv_lock); 1346 return 0; 1347 } 1348 spin_unlock(&server->srv_lock); 1349 } while (retry); 1350 1351 cifs_dbg(FYI, "%s: gave up waiting on reconnect\n", __func__); 1352 return -EHOSTDOWN; 1353 } 1354