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 155 return ret_buf; 156 } 157 158 void 159 tconInfoFree(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace) 160 { 161 if (tcon == NULL) { 162 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n"); 163 return; 164 } 165 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, trace); 166 free_cached_dirs(tcon->cfids); 167 atomic_dec(&tconInfoAllocCount); 168 kfree(tcon->nativeFileSystem); 169 kfree_sensitive(tcon->password); 170 kfree(tcon->origin_fullpath); 171 kfree(tcon); 172 } 173 174 struct smb_hdr * 175 cifs_buf_get(void) 176 { 177 struct smb_hdr *ret_buf = NULL; 178 /* 179 * SMB2 header is bigger than CIFS one - no problems to clean some 180 * more bytes for CIFS. 181 */ 182 size_t buf_size = sizeof(struct smb2_hdr); 183 184 /* 185 * We could use negotiated size instead of max_msgsize - 186 * but it may be more efficient to always alloc same size 187 * albeit slightly larger than necessary and maxbuffersize 188 * defaults to this and can not be bigger. 189 */ 190 ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS); 191 192 /* clear the first few header bytes */ 193 /* for most paths, more is cleared in header_assemble */ 194 memset(ret_buf, 0, buf_size + 3); 195 atomic_inc(&buf_alloc_count); 196 #ifdef CONFIG_CIFS_STATS2 197 atomic_inc(&total_buf_alloc_count); 198 #endif /* CONFIG_CIFS_STATS2 */ 199 200 return ret_buf; 201 } 202 203 void 204 cifs_buf_release(void *buf_to_free) 205 { 206 if (buf_to_free == NULL) { 207 /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/ 208 return; 209 } 210 mempool_free(buf_to_free, cifs_req_poolp); 211 212 atomic_dec(&buf_alloc_count); 213 return; 214 } 215 216 struct smb_hdr * 217 cifs_small_buf_get(void) 218 { 219 struct smb_hdr *ret_buf = NULL; 220 221 /* We could use negotiated size instead of max_msgsize - 222 but it may be more efficient to always alloc same size 223 albeit slightly larger than necessary and maxbuffersize 224 defaults to this and can not be bigger */ 225 ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS); 226 /* No need to clear memory here, cleared in header assemble */ 227 /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/ 228 atomic_inc(&small_buf_alloc_count); 229 #ifdef CONFIG_CIFS_STATS2 230 atomic_inc(&total_small_buf_alloc_count); 231 #endif /* CONFIG_CIFS_STATS2 */ 232 233 return ret_buf; 234 } 235 236 void 237 cifs_small_buf_release(void *buf_to_free) 238 { 239 240 if (buf_to_free == NULL) { 241 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n"); 242 return; 243 } 244 mempool_free(buf_to_free, cifs_sm_req_poolp); 245 246 atomic_dec(&small_buf_alloc_count); 247 return; 248 } 249 250 void 251 free_rsp_buf(int resp_buftype, void *rsp) 252 { 253 if (resp_buftype == CIFS_SMALL_BUFFER) 254 cifs_small_buf_release(rsp); 255 else if (resp_buftype == CIFS_LARGE_BUFFER) 256 cifs_buf_release(rsp); 257 } 258 259 /* NB: MID can not be set if treeCon not passed in, in that 260 case it is responsibility of caller to set the mid */ 261 void 262 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ , 263 const struct cifs_tcon *treeCon, int word_count 264 /* length of fixed section (word count) in two byte units */) 265 { 266 char *temp = (char *) buffer; 267 268 memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */ 269 270 buffer->smb_buf_length = cpu_to_be32( 271 (2 * word_count) + sizeof(struct smb_hdr) - 272 4 /* RFC 1001 length field does not count */ + 273 2 /* for bcc field itself */) ; 274 275 buffer->Protocol[0] = 0xFF; 276 buffer->Protocol[1] = 'S'; 277 buffer->Protocol[2] = 'M'; 278 buffer->Protocol[3] = 'B'; 279 buffer->Command = smb_command; 280 buffer->Flags = 0x00; /* case sensitive */ 281 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES; 282 buffer->Pid = cpu_to_le16((__u16)current->tgid); 283 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16)); 284 if (treeCon) { 285 buffer->Tid = treeCon->tid; 286 if (treeCon->ses) { 287 if (treeCon->ses->capabilities & CAP_UNICODE) 288 buffer->Flags2 |= SMBFLG2_UNICODE; 289 if (treeCon->ses->capabilities & CAP_STATUS32) 290 buffer->Flags2 |= SMBFLG2_ERR_STATUS; 291 292 /* Uid is not converted */ 293 buffer->Uid = treeCon->ses->Suid; 294 if (treeCon->ses->server) 295 buffer->Mid = get_next_mid(treeCon->ses->server); 296 } 297 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS) 298 buffer->Flags2 |= SMBFLG2_DFS; 299 if (treeCon->nocase) 300 buffer->Flags |= SMBFLG_CASELESS; 301 if ((treeCon->ses) && (treeCon->ses->server)) 302 if (treeCon->ses->server->sign) 303 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 304 } 305 306 /* endian conversion of flags is now done just before sending */ 307 buffer->WordCount = (char) word_count; 308 return; 309 } 310 311 static int 312 check_smb_hdr(struct smb_hdr *smb) 313 { 314 /* does it have the right SMB "signature" ? */ 315 if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) { 316 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n", 317 *(unsigned int *)smb->Protocol); 318 return 1; 319 } 320 321 /* if it's a response then accept */ 322 if (smb->Flags & SMBFLG_RESPONSE) 323 return 0; 324 325 /* only one valid case where server sends us request */ 326 if (smb->Command == SMB_COM_LOCKING_ANDX) 327 return 0; 328 329 /* 330 * Windows NT server returns error resposne (e.g. STATUS_DELETE_PENDING 331 * or STATUS_OBJECT_NAME_NOT_FOUND or ERRDOS/ERRbadfile or any other) 332 * for some TRANS2 requests without the RESPONSE flag set in header. 333 */ 334 if (smb->Command == SMB_COM_TRANSACTION2 && smb->Status.CifsError != 0) 335 return 0; 336 337 cifs_dbg(VFS, "Server sent request, not response. mid=%u\n", 338 get_mid(smb)); 339 return 1; 340 } 341 342 int 343 checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server) 344 { 345 struct smb_hdr *smb = (struct smb_hdr *)buf; 346 __u32 rfclen = be32_to_cpu(smb->smb_buf_length); 347 __u32 clc_len; /* calculated length */ 348 cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n", 349 total_read, rfclen); 350 351 /* is this frame too small to even get to a BCC? */ 352 if (total_read < 2 + sizeof(struct smb_hdr)) { 353 if ((total_read >= sizeof(struct smb_hdr) - 1) 354 && (smb->Status.CifsError != 0)) { 355 /* it's an error return */ 356 smb->WordCount = 0; 357 /* some error cases do not return wct and bcc */ 358 return 0; 359 } else if ((total_read == sizeof(struct smb_hdr) + 1) && 360 (smb->WordCount == 0)) { 361 char *tmp = (char *)smb; 362 /* Need to work around a bug in two servers here */ 363 /* First, check if the part of bcc they sent was zero */ 364 if (tmp[sizeof(struct smb_hdr)] == 0) { 365 /* some servers return only half of bcc 366 * on simple responses (wct, bcc both zero) 367 * in particular have seen this on 368 * ulogoffX and FindClose. This leaves 369 * one byte of bcc potentially uninitialized 370 */ 371 /* zero rest of bcc */ 372 tmp[sizeof(struct smb_hdr)+1] = 0; 373 return 0; 374 } 375 cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n"); 376 } else { 377 cifs_dbg(VFS, "Length less than smb header size\n"); 378 } 379 return -EIO; 380 } else if (total_read < sizeof(*smb) + 2 * smb->WordCount) { 381 cifs_dbg(VFS, "%s: can't read BCC due to invalid WordCount(%u)\n", 382 __func__, smb->WordCount); 383 return -EIO; 384 } 385 386 /* otherwise, there is enough to get to the BCC */ 387 if (check_smb_hdr(smb)) 388 return -EIO; 389 clc_len = smbCalcSize(smb); 390 391 if (4 + rfclen != total_read) { 392 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n", 393 rfclen); 394 return -EIO; 395 } 396 397 if (4 + rfclen != clc_len) { 398 __u16 mid = get_mid(smb); 399 /* check if bcc wrapped around for large read responses */ 400 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) { 401 /* check if lengths match mod 64K */ 402 if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF)) 403 return 0; /* bcc wrapped */ 404 } 405 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n", 406 clc_len, 4 + rfclen, mid); 407 408 if (4 + rfclen < clc_len) { 409 cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n", 410 rfclen, mid); 411 return -EIO; 412 } else if (rfclen > clc_len + 512) { 413 /* 414 * Some servers (Windows XP in particular) send more 415 * data than the lengths in the SMB packet would 416 * indicate on certain calls (byte range locks and 417 * trans2 find first calls in particular). While the 418 * client can handle such a frame by ignoring the 419 * trailing data, we choose limit the amount of extra 420 * data to 512 bytes. 421 */ 422 cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n", 423 rfclen, mid); 424 return -EIO; 425 } 426 } 427 return 0; 428 } 429 430 bool 431 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv) 432 { 433 struct smb_hdr *buf = (struct smb_hdr *)buffer; 434 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf; 435 struct TCP_Server_Info *pserver; 436 struct cifs_ses *ses; 437 struct cifs_tcon *tcon; 438 struct cifsInodeInfo *pCifsInode; 439 struct cifsFileInfo *netfile; 440 441 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n"); 442 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) && 443 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) { 444 struct smb_com_transaction_change_notify_rsp *pSMBr = 445 (struct smb_com_transaction_change_notify_rsp *)buf; 446 struct file_notify_information *pnotify; 447 __u32 data_offset = 0; 448 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length); 449 450 if (get_bcc(buf) > sizeof(struct file_notify_information)) { 451 data_offset = le32_to_cpu(pSMBr->DataOffset); 452 453 if (data_offset > 454 len - sizeof(struct file_notify_information)) { 455 cifs_dbg(FYI, "Invalid data_offset %u\n", 456 data_offset); 457 return true; 458 } 459 pnotify = (struct file_notify_information *) 460 ((char *)&pSMBr->hdr.Protocol + data_offset); 461 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n", 462 pnotify->FileName, pnotify->Action); 463 /* cifs_dump_mem("Rcvd notify Data: ",buf, 464 sizeof(struct smb_hdr)+60); */ 465 return true; 466 } 467 if (pSMBr->hdr.Status.CifsError) { 468 cifs_dbg(FYI, "notify err 0x%x\n", 469 pSMBr->hdr.Status.CifsError); 470 return true; 471 } 472 return false; 473 } 474 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX) 475 return false; 476 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) { 477 /* no sense logging error on invalid handle on oplock 478 break - harmless race between close request and oplock 479 break response is expected from time to time writing out 480 large dirty files cached on the client */ 481 if ((NT_STATUS_INVALID_HANDLE) == 482 le32_to_cpu(pSMB->hdr.Status.CifsError)) { 483 cifs_dbg(FYI, "Invalid handle on oplock break\n"); 484 return true; 485 } else if (ERRbadfid == 486 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) { 487 return true; 488 } else { 489 return false; /* on valid oplock brk we get "request" */ 490 } 491 } 492 if (pSMB->hdr.WordCount != 8) 493 return false; 494 495 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n", 496 pSMB->LockType, pSMB->OplockLevel); 497 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)) 498 return false; 499 500 /* If server is a channel, select the primary channel */ 501 pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv; 502 503 /* look up tcon based on tid & uid */ 504 spin_lock(&cifs_tcp_ses_lock); 505 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 506 if (cifs_ses_exiting(ses)) 507 continue; 508 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 509 if (tcon->tid != buf->Tid) 510 continue; 511 512 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks); 513 spin_lock(&tcon->open_file_lock); 514 list_for_each_entry(netfile, &tcon->openFileList, tlist) { 515 if (pSMB->Fid != netfile->fid.netfid) 516 continue; 517 518 cifs_dbg(FYI, "file id match, oplock break\n"); 519 pCifsInode = CIFS_I(d_inode(netfile->dentry)); 520 521 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, 522 &pCifsInode->flags); 523 524 netfile->oplock_epoch = 0; 525 netfile->oplock_level = pSMB->OplockLevel; 526 netfile->oplock_break_cancelled = false; 527 cifs_queue_oplock_break(netfile); 528 529 spin_unlock(&tcon->open_file_lock); 530 spin_unlock(&cifs_tcp_ses_lock); 531 return true; 532 } 533 spin_unlock(&tcon->open_file_lock); 534 spin_unlock(&cifs_tcp_ses_lock); 535 cifs_dbg(FYI, "No matching file for oplock break\n"); 536 return true; 537 } 538 } 539 spin_unlock(&cifs_tcp_ses_lock); 540 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n"); 541 return true; 542 } 543 544 void 545 dump_smb(void *buf, int smb_buf_length) 546 { 547 if (traceSMB == 0) 548 return; 549 550 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf, 551 smb_buf_length, true); 552 } 553 554 void 555 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb) 556 { 557 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 558 struct cifs_tcon *tcon = NULL; 559 560 if (cifs_sb->master_tlink) 561 tcon = cifs_sb_master_tcon(cifs_sb); 562 563 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; 564 cifs_sb->mnt_cifs_serverino_autodisabled = true; 565 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n", 566 tcon ? tcon->tree_name : "new server"); 567 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n"); 568 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n"); 569 570 } 571 } 572 573 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock) 574 { 575 oplock &= 0xF; 576 577 if (oplock == OPLOCK_EXCLUSIVE) { 578 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG; 579 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n", 580 &cinode->netfs.inode); 581 } else if (oplock == OPLOCK_READ) { 582 cinode->oplock = CIFS_CACHE_READ_FLG; 583 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n", 584 &cinode->netfs.inode); 585 } else 586 cinode->oplock = 0; 587 } 588 589 /* 590 * We wait for oplock breaks to be processed before we attempt to perform 591 * writes. 592 */ 593 int cifs_get_writer(struct cifsInodeInfo *cinode) 594 { 595 int rc; 596 597 start: 598 rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK, 599 TASK_KILLABLE); 600 if (rc) 601 return rc; 602 603 spin_lock(&cinode->writers_lock); 604 if (!cinode->writers) 605 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 606 cinode->writers++; 607 /* Check to see if we have started servicing an oplock break */ 608 if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) { 609 cinode->writers--; 610 if (cinode->writers == 0) { 611 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 612 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 613 } 614 spin_unlock(&cinode->writers_lock); 615 goto start; 616 } 617 spin_unlock(&cinode->writers_lock); 618 return 0; 619 } 620 621 void cifs_put_writer(struct cifsInodeInfo *cinode) 622 { 623 spin_lock(&cinode->writers_lock); 624 cinode->writers--; 625 if (cinode->writers == 0) { 626 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags); 627 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS); 628 } 629 spin_unlock(&cinode->writers_lock); 630 } 631 632 /** 633 * cifs_queue_oplock_break - queue the oplock break handler for cfile 634 * @cfile: The file to break the oplock on 635 * 636 * This function is called from the demultiplex thread when it 637 * receives an oplock break for @cfile. 638 * 639 * Assumes the tcon->open_file_lock is held. 640 * Assumes cfile->file_info_lock is NOT held. 641 */ 642 void cifs_queue_oplock_break(struct cifsFileInfo *cfile) 643 { 644 /* 645 * Bump the handle refcount now while we hold the 646 * open_file_lock to enforce the validity of it for the oplock 647 * break handler. The matching put is done at the end of the 648 * handler. 649 */ 650 cifsFileInfo_get(cfile); 651 652 queue_work(cifsoplockd_wq, &cfile->oplock_break); 653 } 654 655 void cifs_done_oplock_break(struct cifsInodeInfo *cinode) 656 { 657 clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags); 658 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK); 659 } 660 661 bool 662 backup_cred(struct cifs_sb_info *cifs_sb) 663 { 664 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) { 665 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid())) 666 return true; 667 } 668 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) { 669 if (in_group_p(cifs_sb->ctx->backupgid)) 670 return true; 671 } 672 673 return false; 674 } 675 676 void 677 cifs_del_pending_open(struct cifs_pending_open *open) 678 { 679 spin_lock(&tlink_tcon(open->tlink)->open_file_lock); 680 list_del(&open->olist); 681 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 682 } 683 684 void 685 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink, 686 struct cifs_pending_open *open) 687 { 688 memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE); 689 open->oplock = CIFS_OPLOCK_NO_CHANGE; 690 open->tlink = tlink; 691 fid->pending_open = open; 692 list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens); 693 } 694 695 void 696 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink, 697 struct cifs_pending_open *open) 698 { 699 spin_lock(&tlink_tcon(tlink)->open_file_lock); 700 cifs_add_pending_open_locked(fid, tlink, open); 701 spin_unlock(&tlink_tcon(open->tlink)->open_file_lock); 702 } 703 704 /* 705 * Critical section which runs after acquiring deferred_lock. 706 * As there is no reference count on cifs_deferred_close, pdclose 707 * should not be used outside deferred_lock. 708 */ 709 bool 710 cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose) 711 { 712 struct cifs_deferred_close *dclose; 713 714 list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) { 715 if ((dclose->netfid == cfile->fid.netfid) && 716 (dclose->persistent_fid == cfile->fid.persistent_fid) && 717 (dclose->volatile_fid == cfile->fid.volatile_fid)) { 718 *pdclose = dclose; 719 return true; 720 } 721 } 722 return false; 723 } 724 725 /* 726 * Critical section which runs after acquiring deferred_lock. 727 */ 728 void 729 cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose) 730 { 731 bool is_deferred = false; 732 struct cifs_deferred_close *pdclose; 733 734 is_deferred = cifs_is_deferred_close(cfile, &pdclose); 735 if (is_deferred) { 736 kfree(dclose); 737 return; 738 } 739 740 dclose->tlink = cfile->tlink; 741 dclose->netfid = cfile->fid.netfid; 742 dclose->persistent_fid = cfile->fid.persistent_fid; 743 dclose->volatile_fid = cfile->fid.volatile_fid; 744 list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes); 745 } 746 747 /* 748 * Critical section which runs after acquiring deferred_lock. 749 */ 750 void 751 cifs_del_deferred_close(struct cifsFileInfo *cfile) 752 { 753 bool is_deferred = false; 754 struct cifs_deferred_close *dclose; 755 756 is_deferred = cifs_is_deferred_close(cfile, &dclose); 757 if (!is_deferred) 758 return; 759 list_del(&dclose->dlist); 760 kfree(dclose); 761 } 762 763 void 764 cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) 765 { 766 struct cifsFileInfo *cfile = NULL; 767 struct file_list *tmp_list, *tmp_next_list; 768 LIST_HEAD(file_head); 769 770 if (cifs_inode == NULL) 771 return; 772 773 spin_lock(&cifs_inode->open_file_lock); 774 list_for_each_entry(cfile, &cifs_inode->openFileList, flist) { 775 if (delayed_work_pending(&cfile->deferred)) { 776 if (cancel_delayed_work(&cfile->deferred)) { 777 spin_lock(&cifs_inode->deferred_lock); 778 cifs_del_deferred_close(cfile); 779 spin_unlock(&cifs_inode->deferred_lock); 780 781 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 782 if (tmp_list == NULL) 783 break; 784 tmp_list->cfile = cfile; 785 list_add_tail(&tmp_list->list, &file_head); 786 } 787 } 788 } 789 spin_unlock(&cifs_inode->open_file_lock); 790 791 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 792 _cifsFileInfo_put(tmp_list->cfile, false, false); 793 list_del(&tmp_list->list); 794 kfree(tmp_list); 795 } 796 } 797 798 void 799 cifs_close_all_deferred_files(struct cifs_tcon *tcon) 800 { 801 struct cifsFileInfo *cfile; 802 struct file_list *tmp_list, *tmp_next_list; 803 LIST_HEAD(file_head); 804 805 spin_lock(&tcon->open_file_lock); 806 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 807 if (delayed_work_pending(&cfile->deferred)) { 808 if (cancel_delayed_work(&cfile->deferred)) { 809 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 810 cifs_del_deferred_close(cfile); 811 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 812 813 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 814 if (tmp_list == NULL) 815 break; 816 tmp_list->cfile = cfile; 817 list_add_tail(&tmp_list->list, &file_head); 818 } 819 } 820 } 821 spin_unlock(&tcon->open_file_lock); 822 823 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 824 _cifsFileInfo_put(tmp_list->cfile, true, false); 825 list_del(&tmp_list->list); 826 kfree(tmp_list); 827 } 828 } 829 void 830 cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path) 831 { 832 struct cifsFileInfo *cfile; 833 struct file_list *tmp_list, *tmp_next_list; 834 void *page; 835 const char *full_path; 836 LIST_HEAD(file_head); 837 838 page = alloc_dentry_path(); 839 spin_lock(&tcon->open_file_lock); 840 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 841 full_path = build_path_from_dentry(cfile->dentry, page); 842 if (strstr(full_path, path)) { 843 if (delayed_work_pending(&cfile->deferred)) { 844 if (cancel_delayed_work(&cfile->deferred)) { 845 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 846 cifs_del_deferred_close(cfile); 847 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 848 849 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC); 850 if (tmp_list == NULL) 851 break; 852 tmp_list->cfile = cfile; 853 list_add_tail(&tmp_list->list, &file_head); 854 } 855 } 856 } 857 } 858 spin_unlock(&tcon->open_file_lock); 859 860 list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) { 861 _cifsFileInfo_put(tmp_list->cfile, true, false); 862 list_del(&tmp_list->list); 863 kfree(tmp_list); 864 } 865 free_dentry_path(page); 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