1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Copyright (C) International Business Machines Corp., 2002, 2011 5 * Etersoft, 2012 6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org), 7 * Steve French (sfrench@us.ibm.com) 8 * 9 */ 10 #include <linux/fs.h> 11 #include <linux/stat.h> 12 #include <linux/slab.h> 13 #include <linux/pagemap.h> 14 #include <asm/div64.h> 15 #include "cifsfs.h" 16 #include "cifspdu.h" 17 #include "cifsglob.h" 18 #include "cifsproto.h" 19 #include "cifs_debug.h" 20 #include "cifs_fs_sb.h" 21 #include "cifs_unicode.h" 22 #include "fscache.h" 23 #include "smb2glob.h" 24 #include "smb2pdu.h" 25 #include "smb2proto.h" 26 #include "cached_dir.h" 27 #include "../common/smb2status.h" 28 29 static struct reparse_data_buffer *reparse_buf_ptr(struct kvec *iov) 30 { 31 struct reparse_data_buffer *buf; 32 struct smb2_ioctl_rsp *io = iov->iov_base; 33 u32 off, count, len; 34 35 count = le32_to_cpu(io->OutputCount); 36 off = le32_to_cpu(io->OutputOffset); 37 if (check_add_overflow(off, count, &len) || len > iov->iov_len) 38 return ERR_PTR(-EIO); 39 40 buf = (struct reparse_data_buffer *)((u8 *)io + off); 41 len = sizeof(*buf); 42 if (count < len || count < le16_to_cpu(buf->ReparseDataLength) + len) 43 return ERR_PTR(-EIO); 44 return buf; 45 } 46 47 static inline __u32 file_create_options(struct dentry *dentry) 48 { 49 struct cifsInodeInfo *ci; 50 51 if (dentry) { 52 ci = CIFS_I(d_inode(dentry)); 53 if (ci->cifsAttrs & ATTR_REPARSE) 54 return OPEN_REPARSE_POINT; 55 } 56 return 0; 57 } 58 59 /* Parse owner and group from SMB3.1.1 POSIX query info */ 60 static int parse_posix_sids(struct cifs_open_info_data *data, 61 struct kvec *rsp_iov) 62 { 63 struct smb2_query_info_rsp *qi = rsp_iov->iov_base; 64 unsigned int out_len = le32_to_cpu(qi->OutputBufferLength); 65 unsigned int qi_len = sizeof(data->posix_fi); 66 int owner_len, group_len; 67 u8 *sidsbuf, *sidsbuf_end; 68 69 if (out_len <= qi_len) 70 return -EINVAL; 71 72 sidsbuf = (u8 *)qi + le16_to_cpu(qi->OutputBufferOffset) + qi_len; 73 sidsbuf_end = sidsbuf + out_len - qi_len; 74 75 owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end); 76 if (owner_len == -1) 77 return -EINVAL; 78 79 memcpy(&data->posix_owner, sidsbuf, owner_len); 80 group_len = posix_info_sid_size(sidsbuf + owner_len, sidsbuf_end); 81 if (group_len == -1) 82 return -EINVAL; 83 84 memcpy(&data->posix_group, sidsbuf + owner_len, group_len); 85 return 0; 86 } 87 88 struct wsl_query_ea { 89 __le32 next; 90 __u8 name_len; 91 __u8 name[SMB2_WSL_XATTR_NAME_LEN + 1]; 92 } __packed; 93 94 #define NEXT_OFF cpu_to_le32(sizeof(struct wsl_query_ea)) 95 96 static const struct wsl_query_ea wsl_query_eas[] = { 97 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_UID, }, 98 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_GID, }, 99 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_MODE, }, 100 { .next = 0, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_DEV, }, 101 }; 102 103 static int check_wsl_eas(struct kvec *rsp_iov) 104 { 105 struct smb2_file_full_ea_info *ea; 106 struct smb2_query_info_rsp *rsp = rsp_iov->iov_base; 107 unsigned long addr; 108 u32 outlen, next; 109 u16 vlen; 110 u8 nlen; 111 u8 *end; 112 113 outlen = le32_to_cpu(rsp->OutputBufferLength); 114 if (outlen < SMB2_WSL_MIN_QUERY_EA_RESP_SIZE || 115 outlen > SMB2_WSL_MAX_QUERY_EA_RESP_SIZE) 116 return -EINVAL; 117 118 ea = (void *)((u8 *)rsp_iov->iov_base + 119 le16_to_cpu(rsp->OutputBufferOffset)); 120 end = (u8 *)rsp_iov->iov_base + rsp_iov->iov_len; 121 for (;;) { 122 if ((u8 *)ea > end - sizeof(*ea)) 123 return -EINVAL; 124 125 nlen = ea->ea_name_length; 126 vlen = le16_to_cpu(ea->ea_value_length); 127 if (nlen != SMB2_WSL_XATTR_NAME_LEN || 128 (u8 *)ea + nlen + 1 + vlen > end) 129 return -EINVAL; 130 131 switch (vlen) { 132 case 4: 133 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) && 134 strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) && 135 strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen)) 136 return -EINVAL; 137 break; 138 case 8: 139 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen)) 140 return -EINVAL; 141 break; 142 case 0: 143 if (!strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) || 144 !strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) || 145 !strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen) || 146 !strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen)) 147 break; 148 fallthrough; 149 default: 150 return -EINVAL; 151 } 152 153 next = le32_to_cpu(ea->next_entry_offset); 154 if (!next) 155 break; 156 if (!IS_ALIGNED(next, 4) || 157 check_add_overflow((unsigned long)ea, next, &addr)) 158 return -EINVAL; 159 ea = (void *)addr; 160 } 161 return 0; 162 } 163 164 /* 165 * note: If cfile is passed, the reference to it is dropped here. 166 * So make sure that you do not reuse cfile after return from this func. 167 * 168 * If passing @out_iov and @out_buftype, ensure to make them both large enough 169 * (>= 3) to hold all compounded responses. Caller is also responsible for 170 * freeing them up with free_rsp_buf(). 171 */ 172 static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, 173 struct cifs_sb_info *cifs_sb, const char *full_path, 174 struct cifs_open_parms *oparms, struct kvec *in_iov, 175 int *cmds, int num_cmds, struct cifsFileInfo *cfile, 176 struct kvec *out_iov, int *out_buftype, struct dentry *dentry) 177 { 178 179 struct smb2_create_rsp *create_rsp = NULL; 180 struct smb2_query_info_rsp *qi_rsp = NULL; 181 struct smb2_compound_vars *vars = NULL; 182 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; 183 struct cifs_open_info_data *idata; 184 struct cifs_ses *ses = tcon->ses; 185 struct reparse_data_buffer *rbuf; 186 struct TCP_Server_Info *server; 187 int resp_buftype[MAX_COMPOUND]; 188 int retries = 0, cur_sleep = 1; 189 __u8 delete_pending[8] = {1,}; 190 struct kvec *rsp_iov, *iov; 191 struct inode *inode = NULL; 192 __le16 *utf16_path = NULL; 193 struct smb_rqst *rqst; 194 unsigned int size[2]; 195 struct cifs_fid fid; 196 int num_rqst = 0, i; 197 unsigned int len; 198 int tmp_rc, rc; 199 int flags = 0; 200 void *data[2]; 201 202 replay_again: 203 /* reinitialize for possible replay */ 204 flags = 0; 205 oplock = SMB2_OPLOCK_LEVEL_NONE; 206 num_rqst = 0; 207 server = cifs_pick_channel(ses); 208 209 vars = kzalloc(sizeof(*vars), GFP_ATOMIC); 210 if (vars == NULL) { 211 rc = -ENOMEM; 212 goto out; 213 } 214 rqst = &vars->rqst[0]; 215 rsp_iov = &vars->rsp_iov[0]; 216 217 if (smb3_encryption_required(tcon)) 218 flags |= CIFS_TRANSFORM_REQ; 219 220 for (i = 0; i < ARRAY_SIZE(resp_buftype); i++) 221 resp_buftype[i] = CIFS_NO_BUFFER; 222 223 /* We already have a handle so we can skip the open */ 224 if (cfile) 225 goto after_open; 226 227 /* Open */ 228 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); 229 if (!utf16_path) { 230 rc = -ENOMEM; 231 goto finished; 232 } 233 234 /* if there is an existing lease, reuse it */ 235 236 /* 237 * note: files with hardlinks cause unexpected behaviour. As per MS-SMB2, 238 * lease keys are associated with the filepath. We are maintaining lease keys 239 * with the inode on the client. If the file has hardlinks, it is possible 240 * that the lease for a file be reused for an operation on its hardlink or 241 * vice versa. 242 * As a workaround, send request using an existing lease key and if the server 243 * returns STATUS_INVALID_PARAMETER, which maps to EINVAL, send the request 244 * again without the lease. 245 */ 246 if (dentry) { 247 inode = d_inode(dentry); 248 if (CIFS_I(inode)->lease_granted && server->ops->get_lease_key) { 249 oplock = SMB2_OPLOCK_LEVEL_LEASE; 250 server->ops->get_lease_key(inode, &fid); 251 } 252 } 253 254 vars->oparms = *oparms; 255 vars->oparms.fid = &fid; 256 257 rqst[num_rqst].rq_iov = &vars->open_iov[0]; 258 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE; 259 rc = SMB2_open_init(tcon, server, 260 &rqst[num_rqst], &oplock, &vars->oparms, 261 utf16_path); 262 kfree(utf16_path); 263 if (rc) 264 goto finished; 265 266 smb2_set_next_command(tcon, &rqst[num_rqst]); 267 after_open: 268 num_rqst++; 269 rc = 0; 270 271 i = 0; 272 273 /* Skip the leading explicit OPEN operation */ 274 if (num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY) 275 i++; 276 277 for (; i < num_cmds; i++) { 278 /* Operation */ 279 switch (cmds[i]) { 280 case SMB2_OP_QUERY_INFO: 281 rqst[num_rqst].rq_iov = &vars->qi_iov; 282 rqst[num_rqst].rq_nvec = 1; 283 284 if (cfile) { 285 rc = SMB2_query_info_init(tcon, server, 286 &rqst[num_rqst], 287 cfile->fid.persistent_fid, 288 cfile->fid.volatile_fid, 289 FILE_ALL_INFORMATION, 290 SMB2_O_INFO_FILE, 0, 291 sizeof(struct smb2_file_all_info) + 292 PATH_MAX * 2, 0, NULL); 293 } else { 294 rc = SMB2_query_info_init(tcon, server, 295 &rqst[num_rqst], 296 COMPOUND_FID, 297 COMPOUND_FID, 298 FILE_ALL_INFORMATION, 299 SMB2_O_INFO_FILE, 0, 300 sizeof(struct smb2_file_all_info) + 301 PATH_MAX * 2, 0, NULL); 302 } 303 if (!rc && (!cfile || num_rqst > 1)) { 304 smb2_set_next_command(tcon, &rqst[num_rqst]); 305 smb2_set_related(&rqst[num_rqst]); 306 } else if (rc) { 307 goto finished; 308 } 309 num_rqst++; 310 trace_smb3_query_info_compound_enter(xid, tcon->tid, 311 ses->Suid, full_path); 312 break; 313 case SMB2_OP_POSIX_QUERY_INFO: 314 rqst[num_rqst].rq_iov = &vars->qi_iov; 315 rqst[num_rqst].rq_nvec = 1; 316 317 if (cfile) { 318 /* TBD: fix following to allow for longer SIDs */ 319 rc = SMB2_query_info_init(tcon, server, 320 &rqst[num_rqst], 321 cfile->fid.persistent_fid, 322 cfile->fid.volatile_fid, 323 SMB_FIND_FILE_POSIX_INFO, 324 SMB2_O_INFO_FILE, 0, 325 sizeof(struct smb311_posix_qinfo *) + 326 (PATH_MAX * 2) + 327 (sizeof(struct smb_sid) * 2), 0, NULL); 328 } else { 329 rc = SMB2_query_info_init(tcon, server, 330 &rqst[num_rqst], 331 COMPOUND_FID, 332 COMPOUND_FID, 333 SMB_FIND_FILE_POSIX_INFO, 334 SMB2_O_INFO_FILE, 0, 335 sizeof(struct smb311_posix_qinfo *) + 336 (PATH_MAX * 2) + 337 (sizeof(struct smb_sid) * 2), 0, NULL); 338 } 339 if (!rc && (!cfile || num_rqst > 1)) { 340 smb2_set_next_command(tcon, &rqst[num_rqst]); 341 smb2_set_related(&rqst[num_rqst]); 342 } else if (rc) { 343 goto finished; 344 } 345 num_rqst++; 346 trace_smb3_posix_query_info_compound_enter(xid, tcon->tid, 347 ses->Suid, full_path); 348 break; 349 case SMB2_OP_MKDIR: 350 /* 351 * Directories are created through parameters in the 352 * SMB2_open() call. 353 */ 354 trace_smb3_mkdir_enter(xid, tcon->tid, ses->Suid, full_path); 355 break; 356 case SMB2_OP_UNLINK: 357 rqst[num_rqst].rq_iov = vars->unlink_iov; 358 rqst[num_rqst].rq_nvec = 1; 359 360 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */ 361 data[0] = &delete_pending[0]; 362 363 if (cfile) { 364 rc = SMB2_set_info_init(tcon, server, 365 &rqst[num_rqst], 366 cfile->fid.persistent_fid, 367 cfile->fid.volatile_fid, 368 current->tgid, 369 FILE_DISPOSITION_INFORMATION, 370 SMB2_O_INFO_FILE, 0, 371 data, size); 372 } else { 373 rc = SMB2_set_info_init(tcon, server, 374 &rqst[num_rqst], 375 COMPOUND_FID, 376 COMPOUND_FID, 377 current->tgid, 378 FILE_DISPOSITION_INFORMATION, 379 SMB2_O_INFO_FILE, 0, 380 data, size); 381 } 382 if (!rc && (!cfile || num_rqst > 1)) { 383 smb2_set_next_command(tcon, &rqst[num_rqst]); 384 smb2_set_related(&rqst[num_rqst]); 385 } else if (rc) { 386 goto finished; 387 } 388 num_rqst++; 389 trace_smb3_unlink_enter(xid, tcon->tid, ses->Suid, full_path); 390 break; 391 case SMB2_OP_SET_EOF: 392 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 393 rqst[num_rqst].rq_nvec = 1; 394 395 size[0] = in_iov[i].iov_len; 396 data[0] = in_iov[i].iov_base; 397 398 if (cfile) { 399 rc = SMB2_set_info_init(tcon, server, 400 &rqst[num_rqst], 401 cfile->fid.persistent_fid, 402 cfile->fid.volatile_fid, 403 current->tgid, 404 FILE_END_OF_FILE_INFORMATION, 405 SMB2_O_INFO_FILE, 0, 406 data, size); 407 } else { 408 rc = SMB2_set_info_init(tcon, server, 409 &rqst[num_rqst], 410 COMPOUND_FID, 411 COMPOUND_FID, 412 current->tgid, 413 FILE_END_OF_FILE_INFORMATION, 414 SMB2_O_INFO_FILE, 0, 415 data, size); 416 } 417 if (!rc && (!cfile || num_rqst > 1)) { 418 smb2_set_next_command(tcon, &rqst[num_rqst]); 419 smb2_set_related(&rqst[num_rqst]); 420 } else if (rc) { 421 goto finished; 422 } 423 num_rqst++; 424 trace_smb3_set_eof_enter(xid, tcon->tid, ses->Suid, full_path); 425 break; 426 case SMB2_OP_SET_INFO: 427 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 428 rqst[num_rqst].rq_nvec = 1; 429 430 size[0] = in_iov[i].iov_len; 431 data[0] = in_iov[i].iov_base; 432 433 if (cfile) { 434 rc = SMB2_set_info_init(tcon, server, 435 &rqst[num_rqst], 436 cfile->fid.persistent_fid, 437 cfile->fid.volatile_fid, current->tgid, 438 FILE_BASIC_INFORMATION, 439 SMB2_O_INFO_FILE, 0, data, size); 440 } else { 441 rc = SMB2_set_info_init(tcon, server, 442 &rqst[num_rqst], 443 COMPOUND_FID, 444 COMPOUND_FID, current->tgid, 445 FILE_BASIC_INFORMATION, 446 SMB2_O_INFO_FILE, 0, data, size); 447 } 448 if (!rc && (!cfile || num_rqst > 1)) { 449 smb2_set_next_command(tcon, &rqst[num_rqst]); 450 smb2_set_related(&rqst[num_rqst]); 451 } else if (rc) { 452 goto finished; 453 } 454 num_rqst++; 455 trace_smb3_set_info_compound_enter(xid, tcon->tid, 456 ses->Suid, full_path); 457 break; 458 case SMB2_OP_RENAME: 459 rqst[num_rqst].rq_iov = vars->rename_iov; 460 rqst[num_rqst].rq_nvec = 2; 461 462 len = in_iov[i].iov_len; 463 464 vars->rename_info.ReplaceIfExists = 1; 465 vars->rename_info.RootDirectory = 0; 466 vars->rename_info.FileNameLength = cpu_to_le32(len); 467 468 size[0] = sizeof(struct smb2_file_rename_info); 469 data[0] = &vars->rename_info; 470 471 size[1] = len + 2 /* null */; 472 data[1] = in_iov[i].iov_base; 473 474 if (cfile) { 475 rc = SMB2_set_info_init(tcon, server, 476 &rqst[num_rqst], 477 cfile->fid.persistent_fid, 478 cfile->fid.volatile_fid, 479 current->tgid, FILE_RENAME_INFORMATION, 480 SMB2_O_INFO_FILE, 0, data, size); 481 } else { 482 rc = SMB2_set_info_init(tcon, server, 483 &rqst[num_rqst], 484 COMPOUND_FID, COMPOUND_FID, 485 current->tgid, FILE_RENAME_INFORMATION, 486 SMB2_O_INFO_FILE, 0, data, size); 487 } 488 if (!rc && (!cfile || num_rqst > 1)) { 489 smb2_set_next_command(tcon, &rqst[num_rqst]); 490 smb2_set_related(&rqst[num_rqst]); 491 } else if (rc) { 492 goto finished; 493 } 494 num_rqst++; 495 trace_smb3_rename_enter(xid, tcon->tid, ses->Suid, full_path); 496 break; 497 case SMB2_OP_HARDLINK: 498 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 499 rqst[num_rqst].rq_nvec = 2; 500 501 len = in_iov[i].iov_len; 502 503 vars->link_info.ReplaceIfExists = 0; 504 vars->link_info.RootDirectory = 0; 505 vars->link_info.FileNameLength = cpu_to_le32(len); 506 507 size[0] = sizeof(struct smb2_file_link_info); 508 data[0] = &vars->link_info; 509 510 size[1] = len + 2 /* null */; 511 data[1] = in_iov[i].iov_base; 512 513 rc = SMB2_set_info_init(tcon, server, 514 &rqst[num_rqst], COMPOUND_FID, 515 COMPOUND_FID, current->tgid, 516 FILE_LINK_INFORMATION, 517 SMB2_O_INFO_FILE, 0, data, size); 518 if (rc) 519 goto finished; 520 smb2_set_next_command(tcon, &rqst[num_rqst]); 521 smb2_set_related(&rqst[num_rqst++]); 522 trace_smb3_hardlink_enter(xid, tcon->tid, ses->Suid, full_path); 523 break; 524 case SMB2_OP_SET_REPARSE: 525 rqst[num_rqst].rq_iov = vars->io_iov; 526 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov); 527 528 if (cfile) { 529 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 530 cfile->fid.persistent_fid, 531 cfile->fid.volatile_fid, 532 FSCTL_SET_REPARSE_POINT, 533 in_iov[i].iov_base, 534 in_iov[i].iov_len, 0); 535 } else { 536 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 537 COMPOUND_FID, COMPOUND_FID, 538 FSCTL_SET_REPARSE_POINT, 539 in_iov[i].iov_base, 540 in_iov[i].iov_len, 0); 541 } 542 if (!rc && (!cfile || num_rqst > 1)) { 543 smb2_set_next_command(tcon, &rqst[num_rqst]); 544 smb2_set_related(&rqst[num_rqst]); 545 } else if (rc) { 546 goto finished; 547 } 548 num_rqst++; 549 trace_smb3_set_reparse_compound_enter(xid, tcon->tid, 550 ses->Suid, full_path); 551 break; 552 case SMB2_OP_GET_REPARSE: 553 rqst[num_rqst].rq_iov = vars->io_iov; 554 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov); 555 556 if (cfile) { 557 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 558 cfile->fid.persistent_fid, 559 cfile->fid.volatile_fid, 560 FSCTL_GET_REPARSE_POINT, 561 NULL, 0, CIFSMaxBufSize); 562 } else { 563 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 564 COMPOUND_FID, COMPOUND_FID, 565 FSCTL_GET_REPARSE_POINT, 566 NULL, 0, CIFSMaxBufSize); 567 } 568 if (!rc && (!cfile || num_rqst > 1)) { 569 smb2_set_next_command(tcon, &rqst[num_rqst]); 570 smb2_set_related(&rqst[num_rqst]); 571 } else if (rc) { 572 goto finished; 573 } 574 num_rqst++; 575 trace_smb3_get_reparse_compound_enter(xid, tcon->tid, 576 ses->Suid, full_path); 577 break; 578 case SMB2_OP_QUERY_WSL_EA: 579 rqst[num_rqst].rq_iov = &vars->ea_iov; 580 rqst[num_rqst].rq_nvec = 1; 581 582 if (cfile) { 583 rc = SMB2_query_info_init(tcon, server, 584 &rqst[num_rqst], 585 cfile->fid.persistent_fid, 586 cfile->fid.volatile_fid, 587 FILE_FULL_EA_INFORMATION, 588 SMB2_O_INFO_FILE, 0, 589 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE, 590 sizeof(wsl_query_eas), 591 (void *)wsl_query_eas); 592 } else { 593 rc = SMB2_query_info_init(tcon, server, 594 &rqst[num_rqst], 595 COMPOUND_FID, 596 COMPOUND_FID, 597 FILE_FULL_EA_INFORMATION, 598 SMB2_O_INFO_FILE, 0, 599 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE, 600 sizeof(wsl_query_eas), 601 (void *)wsl_query_eas); 602 } 603 if (!rc && (!cfile || num_rqst > 1)) { 604 smb2_set_next_command(tcon, &rqst[num_rqst]); 605 smb2_set_related(&rqst[num_rqst]); 606 } else if (rc) { 607 goto finished; 608 } 609 num_rqst++; 610 trace_smb3_query_wsl_ea_compound_enter(xid, tcon->tid, 611 ses->Suid, full_path); 612 break; 613 default: 614 cifs_dbg(VFS, "Invalid command\n"); 615 rc = -EINVAL; 616 } 617 } 618 if (rc) 619 goto finished; 620 621 /* We already have a handle so we can skip the close */ 622 if (cfile) 623 goto after_close; 624 /* Close */ 625 flags |= CIFS_CP_CREATE_CLOSE_OP; 626 rqst[num_rqst].rq_iov = &vars->close_iov; 627 rqst[num_rqst].rq_nvec = 1; 628 rc = SMB2_close_init(tcon, server, 629 &rqst[num_rqst], COMPOUND_FID, 630 COMPOUND_FID, false); 631 smb2_set_related(&rqst[num_rqst]); 632 if (rc) 633 goto finished; 634 after_close: 635 num_rqst++; 636 637 if (cfile) { 638 if (retries) 639 for (i = 1; i < num_rqst - 2; i++) 640 smb2_set_replay(server, &rqst[i]); 641 642 rc = compound_send_recv(xid, ses, server, 643 flags, num_rqst - 2, 644 &rqst[1], &resp_buftype[1], 645 &rsp_iov[1]); 646 } else { 647 if (retries) 648 for (i = 0; i < num_rqst; i++) 649 smb2_set_replay(server, &rqst[i]); 650 651 rc = compound_send_recv(xid, ses, server, 652 flags, num_rqst, 653 rqst, resp_buftype, 654 rsp_iov); 655 } 656 657 finished: 658 num_rqst = 0; 659 SMB2_open_free(&rqst[num_rqst++]); 660 if (rc == -EREMCHG) { 661 pr_warn_once("server share %s deleted\n", tcon->tree_name); 662 tcon->need_reconnect = true; 663 } 664 665 tmp_rc = rc; 666 667 if (rc == 0 && num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY) { 668 create_rsp = rsp_iov[0].iov_base; 669 idata = in_iov[0].iov_base; 670 idata->fi.CreationTime = create_rsp->CreationTime; 671 idata->fi.LastAccessTime = create_rsp->LastAccessTime; 672 idata->fi.LastWriteTime = create_rsp->LastWriteTime; 673 idata->fi.ChangeTime = create_rsp->ChangeTime; 674 idata->fi.Attributes = create_rsp->FileAttributes; 675 idata->fi.AllocationSize = create_rsp->AllocationSize; 676 idata->fi.EndOfFile = create_rsp->EndofFile; 677 if (le32_to_cpu(idata->fi.NumberOfLinks) == 0) 678 idata->fi.NumberOfLinks = cpu_to_le32(1); /* dummy value */ 679 idata->fi.DeletePending = 0; 680 idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY); 681 682 /* smb2_parse_contexts() fills idata->fi.IndexNumber */ 683 rc = smb2_parse_contexts(server, &rsp_iov[0], &oparms->fid->epoch, 684 oparms->fid->lease_key, &oplock, &idata->fi, NULL); 685 if (rc) 686 cifs_dbg(VFS, "rc: %d parsing context of compound op\n", rc); 687 } 688 689 for (i = 0; i < num_cmds; i++) { 690 char *buf = rsp_iov[i + 1].iov_base; 691 692 if (buf && resp_buftype[i + 1] != CIFS_NO_BUFFER) 693 rc = server->ops->map_error(buf, false); 694 else 695 rc = tmp_rc; 696 switch (cmds[i]) { 697 case SMB2_OP_QUERY_INFO: 698 idata = in_iov[i].iov_base; 699 idata->contains_posix_file_info = false; 700 if (rc == 0 && cfile && cfile->symlink_target) { 701 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL); 702 if (!idata->symlink_target) 703 rc = -ENOMEM; 704 } 705 if (rc == 0) { 706 qi_rsp = (struct smb2_query_info_rsp *) 707 rsp_iov[i + 1].iov_base; 708 rc = smb2_validate_and_copy_iov( 709 le16_to_cpu(qi_rsp->OutputBufferOffset), 710 le32_to_cpu(qi_rsp->OutputBufferLength), 711 &rsp_iov[i + 1], sizeof(idata->fi), (char *)&idata->fi); 712 } 713 SMB2_query_info_free(&rqst[num_rqst++]); 714 if (rc) 715 trace_smb3_query_info_compound_err(xid, tcon->tid, 716 ses->Suid, rc); 717 else 718 trace_smb3_query_info_compound_done(xid, tcon->tid, 719 ses->Suid); 720 break; 721 case SMB2_OP_POSIX_QUERY_INFO: 722 idata = in_iov[i].iov_base; 723 idata->contains_posix_file_info = true; 724 if (rc == 0 && cfile && cfile->symlink_target) { 725 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL); 726 if (!idata->symlink_target) 727 rc = -ENOMEM; 728 } 729 if (rc == 0) { 730 qi_rsp = (struct smb2_query_info_rsp *) 731 rsp_iov[i + 1].iov_base; 732 rc = smb2_validate_and_copy_iov( 733 le16_to_cpu(qi_rsp->OutputBufferOffset), 734 le32_to_cpu(qi_rsp->OutputBufferLength), 735 &rsp_iov[i + 1], sizeof(idata->posix_fi) /* add SIDs */, 736 (char *)&idata->posix_fi); 737 } 738 if (rc == 0) 739 rc = parse_posix_sids(idata, &rsp_iov[i + 1]); 740 741 SMB2_query_info_free(&rqst[num_rqst++]); 742 if (rc) 743 trace_smb3_posix_query_info_compound_err(xid, tcon->tid, 744 ses->Suid, rc); 745 else 746 trace_smb3_posix_query_info_compound_done(xid, tcon->tid, 747 ses->Suid); 748 break; 749 case SMB2_OP_MKDIR: 750 if (rc) 751 trace_smb3_mkdir_err(xid, tcon->tid, ses->Suid, rc); 752 else 753 trace_smb3_mkdir_done(xid, tcon->tid, ses->Suid); 754 break; 755 case SMB2_OP_HARDLINK: 756 if (rc) 757 trace_smb3_hardlink_err(xid, tcon->tid, ses->Suid, rc); 758 else 759 trace_smb3_hardlink_done(xid, tcon->tid, ses->Suid); 760 SMB2_set_info_free(&rqst[num_rqst++]); 761 break; 762 case SMB2_OP_RENAME: 763 if (rc) 764 trace_smb3_rename_err(xid, tcon->tid, ses->Suid, rc); 765 else 766 trace_smb3_rename_done(xid, tcon->tid, ses->Suid); 767 SMB2_set_info_free(&rqst[num_rqst++]); 768 break; 769 case SMB2_OP_UNLINK: 770 if (!rc) 771 trace_smb3_unlink_done(xid, tcon->tid, ses->Suid); 772 else 773 trace_smb3_unlink_err(xid, tcon->tid, ses->Suid, rc); 774 SMB2_set_info_free(&rqst[num_rqst++]); 775 break; 776 case SMB2_OP_SET_EOF: 777 if (rc) 778 trace_smb3_set_eof_err(xid, tcon->tid, ses->Suid, rc); 779 else 780 trace_smb3_set_eof_done(xid, tcon->tid, ses->Suid); 781 SMB2_set_info_free(&rqst[num_rqst++]); 782 break; 783 case SMB2_OP_SET_INFO: 784 if (rc) 785 trace_smb3_set_info_compound_err(xid, tcon->tid, 786 ses->Suid, rc); 787 else 788 trace_smb3_set_info_compound_done(xid, tcon->tid, 789 ses->Suid); 790 SMB2_set_info_free(&rqst[num_rqst++]); 791 break; 792 case SMB2_OP_SET_REPARSE: 793 if (rc) { 794 trace_smb3_set_reparse_compound_err(xid, tcon->tid, 795 ses->Suid, rc); 796 } else { 797 trace_smb3_set_reparse_compound_done(xid, tcon->tid, 798 ses->Suid); 799 } 800 SMB2_ioctl_free(&rqst[num_rqst++]); 801 break; 802 case SMB2_OP_GET_REPARSE: 803 if (!rc) { 804 iov = &rsp_iov[i + 1]; 805 idata = in_iov[i].iov_base; 806 idata->reparse.io.iov = *iov; 807 idata->reparse.io.buftype = resp_buftype[i + 1]; 808 idata->contains_posix_file_info = false; /* BB VERIFY */ 809 rbuf = reparse_buf_ptr(iov); 810 if (IS_ERR(rbuf)) { 811 rc = PTR_ERR(rbuf); 812 trace_smb3_get_reparse_compound_err(xid, tcon->tid, 813 ses->Suid, rc); 814 } else { 815 idata->reparse.tag = le32_to_cpu(rbuf->ReparseTag); 816 trace_smb3_get_reparse_compound_done(xid, tcon->tid, 817 ses->Suid); 818 } 819 memset(iov, 0, sizeof(*iov)); 820 resp_buftype[i + 1] = CIFS_NO_BUFFER; 821 } else { 822 trace_smb3_get_reparse_compound_err(xid, tcon->tid, 823 ses->Suid, rc); 824 } 825 SMB2_ioctl_free(&rqst[num_rqst++]); 826 break; 827 case SMB2_OP_QUERY_WSL_EA: 828 if (!rc) { 829 idata = in_iov[i].iov_base; 830 idata->contains_posix_file_info = false; 831 qi_rsp = rsp_iov[i + 1].iov_base; 832 data[0] = (u8 *)qi_rsp + le16_to_cpu(qi_rsp->OutputBufferOffset); 833 size[0] = le32_to_cpu(qi_rsp->OutputBufferLength); 834 rc = check_wsl_eas(&rsp_iov[i + 1]); 835 if (!rc) { 836 memcpy(idata->wsl.eas, data[0], size[0]); 837 idata->wsl.eas_len = size[0]; 838 } 839 } 840 if (!rc) { 841 trace_smb3_query_wsl_ea_compound_done(xid, tcon->tid, 842 ses->Suid); 843 } else { 844 trace_smb3_query_wsl_ea_compound_err(xid, tcon->tid, 845 ses->Suid, rc); 846 } 847 SMB2_query_info_free(&rqst[num_rqst++]); 848 break; 849 } 850 } 851 SMB2_close_free(&rqst[num_rqst]); 852 rc = tmp_rc; 853 854 num_cmds += 2; 855 if (out_iov && out_buftype) { 856 memcpy(out_iov, rsp_iov, num_cmds * sizeof(*out_iov)); 857 memcpy(out_buftype, resp_buftype, 858 num_cmds * sizeof(*out_buftype)); 859 } else { 860 for (i = 0; i < num_cmds; i++) 861 free_rsp_buf(resp_buftype[i], rsp_iov[i].iov_base); 862 } 863 num_cmds -= 2; /* correct num_cmds as there could be a retry */ 864 kfree(vars); 865 866 if (is_replayable_error(rc) && 867 smb2_should_replay(tcon, &retries, &cur_sleep)) 868 goto replay_again; 869 870 out: 871 if (cfile) 872 cifsFileInfo_put(cfile); 873 874 return rc; 875 } 876 877 static int parse_create_response(struct cifs_open_info_data *data, 878 struct cifs_sb_info *cifs_sb, 879 const char *full_path, 880 const struct kvec *iov) 881 { 882 struct smb2_create_rsp *rsp = iov->iov_base; 883 bool reparse_point = false; 884 u32 tag = 0; 885 int rc = 0; 886 887 switch (rsp->hdr.Status) { 888 case STATUS_IO_REPARSE_TAG_NOT_HANDLED: 889 reparse_point = true; 890 break; 891 case STATUS_STOPPED_ON_SYMLINK: 892 rc = smb2_parse_symlink_response(cifs_sb, iov, 893 full_path, 894 &data->symlink_target); 895 if (rc) 896 return rc; 897 tag = IO_REPARSE_TAG_SYMLINK; 898 reparse_point = true; 899 break; 900 case STATUS_SUCCESS: 901 reparse_point = !!(rsp->Flags & SMB2_CREATE_FLAG_REPARSEPOINT); 902 break; 903 } 904 data->reparse_point = reparse_point; 905 data->reparse.tag = tag; 906 return rc; 907 } 908 909 /* Check only if SMB2_OP_QUERY_WSL_EA command failed in the compound chain */ 910 static bool ea_unsupported(int *cmds, int num_cmds, 911 struct kvec *out_iov, int *out_buftype) 912 { 913 int i; 914 915 if (cmds[num_cmds - 1] != SMB2_OP_QUERY_WSL_EA) 916 return false; 917 918 for (i = 1; i < num_cmds - 1; i++) { 919 struct smb2_hdr *hdr = out_iov[i].iov_base; 920 921 if (out_buftype[i] == CIFS_NO_BUFFER || !hdr || 922 hdr->Status != STATUS_SUCCESS) 923 return false; 924 } 925 return true; 926 } 927 928 static inline void free_rsp_iov(struct kvec *iovs, int *buftype, int count) 929 { 930 int i; 931 932 for (i = 0; i < count; i++) { 933 free_rsp_buf(buftype[i], iovs[i].iov_base); 934 memset(&iovs[i], 0, sizeof(*iovs)); 935 buftype[i] = CIFS_NO_BUFFER; 936 } 937 } 938 939 int smb2_query_path_info(const unsigned int xid, 940 struct cifs_tcon *tcon, 941 struct cifs_sb_info *cifs_sb, 942 const char *full_path, 943 struct cifs_open_info_data *data) 944 { 945 struct kvec in_iov[3], out_iov[5] = {}; 946 struct cached_fid *cfid = NULL; 947 struct cifs_open_parms oparms; 948 struct cifsFileInfo *cfile; 949 __u32 create_options = 0; 950 int out_buftype[5] = {}; 951 struct smb2_hdr *hdr; 952 int num_cmds = 0; 953 int cmds[3]; 954 bool islink; 955 int rc, rc2; 956 957 data->adjust_tz = false; 958 data->reparse_point = false; 959 960 /* 961 * BB TODO: Add support for using cached root handle in SMB3.1.1 POSIX. 962 * Create SMB2_query_posix_info worker function to do non-compounded 963 * query when we already have an open file handle for this. For now this 964 * is fast enough (always using the compounded version). 965 */ 966 if (!tcon->posix_extensions) { 967 if (*full_path) { 968 rc = -ENOENT; 969 } else { 970 rc = open_cached_dir(xid, tcon, full_path, 971 cifs_sb, false, &cfid); 972 } 973 /* If it is a root and its handle is cached then use it */ 974 if (!rc) { 975 if (cfid->file_all_info_is_valid) { 976 memcpy(&data->fi, &cfid->file_all_info, 977 sizeof(data->fi)); 978 } else { 979 rc = SMB2_query_info(xid, tcon, 980 cfid->fid.persistent_fid, 981 cfid->fid.volatile_fid, 982 &data->fi); 983 } 984 close_cached_dir(cfid); 985 return rc; 986 } 987 cmds[num_cmds++] = SMB2_OP_QUERY_INFO; 988 } else { 989 cmds[num_cmds++] = SMB2_OP_POSIX_QUERY_INFO; 990 } 991 992 in_iov[0].iov_base = data; 993 in_iov[0].iov_len = sizeof(*data); 994 in_iov[1] = in_iov[0]; 995 in_iov[2] = in_iov[0]; 996 997 cifs_get_readable_path(tcon, full_path, &cfile); 998 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_READ_ATTRIBUTES, 999 FILE_OPEN, create_options, ACL_NO_MODE); 1000 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1001 &oparms, in_iov, cmds, num_cmds, 1002 cfile, out_iov, out_buftype, NULL); 1003 hdr = out_iov[0].iov_base; 1004 /* 1005 * If first iov is unset, then SMB session was dropped or we've got a 1006 * cached open file (@cfile). 1007 */ 1008 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER) 1009 goto out; 1010 1011 switch (rc) { 1012 case 0: 1013 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1014 break; 1015 case -EACCES: 1016 /* 1017 * If SMB2_OP_QUERY_INFO (called when POSIX extensions are not used) failed with 1018 * STATUS_ACCESS_DENIED then it means that caller does not have permission to 1019 * open the path with FILE_READ_ATTRIBUTES access and therefore cannot issue 1020 * SMB2_OP_QUERY_INFO command. 1021 * 1022 * There is an alternative way how to query limited information about path but still 1023 * suitable for stat() syscall. SMB2 OPEN/CREATE operation returns in its successful 1024 * response subset of query information. 1025 * 1026 * So try to open the path without FILE_READ_ATTRIBUTES but with MAXIMUM_ALLOWED 1027 * access which will grant the maximum possible access to the file and the response 1028 * will contain required query information for stat() syscall. 1029 */ 1030 1031 if (tcon->posix_extensions) 1032 break; 1033 1034 num_cmds = 1; 1035 cmds[0] = SMB2_OP_OPEN_QUERY; 1036 in_iov[0].iov_base = data; 1037 in_iov[0].iov_len = sizeof(*data); 1038 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, MAXIMUM_ALLOWED, 1039 FILE_OPEN, create_options, ACL_NO_MODE); 1040 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1041 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1042 &oparms, in_iov, cmds, num_cmds, 1043 cfile, out_iov, out_buftype, NULL); 1044 1045 hdr = out_iov[0].iov_base; 1046 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER) 1047 goto out; 1048 1049 if (!rc) 1050 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1051 break; 1052 case -EOPNOTSUPP: 1053 /* 1054 * BB TODO: When support for special files added to Samba 1055 * re-verify this path. 1056 */ 1057 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1058 if (rc || !data->reparse_point) 1059 goto out; 1060 1061 /* 1062 * Skip SMB2_OP_GET_REPARSE if symlink already parsed in create 1063 * response. 1064 */ 1065 if (data->reparse.tag != IO_REPARSE_TAG_SYMLINK) { 1066 cmds[num_cmds++] = SMB2_OP_GET_REPARSE; 1067 if (!tcon->posix_extensions) 1068 cmds[num_cmds++] = SMB2_OP_QUERY_WSL_EA; 1069 } 1070 1071 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1072 FILE_READ_ATTRIBUTES | 1073 FILE_READ_EA | SYNCHRONIZE, 1074 FILE_OPEN, create_options | 1075 OPEN_REPARSE_POINT, ACL_NO_MODE); 1076 cifs_get_readable_path(tcon, full_path, &cfile); 1077 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1078 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1079 &oparms, in_iov, cmds, num_cmds, 1080 cfile, out_iov, out_buftype, NULL); 1081 if (rc && ea_unsupported(cmds, num_cmds, 1082 out_iov, out_buftype)) { 1083 if (data->reparse.tag != IO_REPARSE_TAG_LX_BLK && 1084 data->reparse.tag != IO_REPARSE_TAG_LX_CHR) 1085 rc = 0; 1086 else 1087 rc = -EOPNOTSUPP; 1088 } 1089 1090 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) { 1091 bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY; 1092 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb); 1093 } 1094 break; 1095 case -EREMOTE: 1096 break; 1097 default: 1098 if (hdr->Status != STATUS_OBJECT_NAME_INVALID) 1099 break; 1100 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb, 1101 full_path, &islink); 1102 if (rc2) { 1103 rc = rc2; 1104 goto out; 1105 } 1106 if (islink) 1107 rc = -EREMOTE; 1108 } 1109 1110 out: 1111 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1112 return rc; 1113 } 1114 1115 int 1116 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode, 1117 struct cifs_tcon *tcon, const char *name, 1118 struct cifs_sb_info *cifs_sb) 1119 { 1120 struct cifs_open_parms oparms; 1121 1122 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES, 1123 FILE_CREATE, CREATE_NOT_FILE, mode); 1124 return smb2_compound_op(xid, tcon, cifs_sb, 1125 name, &oparms, NULL, 1126 &(int){SMB2_OP_MKDIR}, 1, 1127 NULL, NULL, NULL, NULL); 1128 } 1129 1130 void 1131 smb2_mkdir_setinfo(struct inode *inode, const char *name, 1132 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon, 1133 const unsigned int xid) 1134 { 1135 struct cifs_open_parms oparms; 1136 FILE_BASIC_INFO data = {}; 1137 struct cifsInodeInfo *cifs_i; 1138 struct cifsFileInfo *cfile; 1139 struct kvec in_iov; 1140 u32 dosattrs; 1141 int tmprc; 1142 1143 in_iov.iov_base = &data; 1144 in_iov.iov_len = sizeof(data); 1145 cifs_i = CIFS_I(inode); 1146 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY; 1147 data.Attributes = cpu_to_le32(dosattrs); 1148 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile); 1149 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES, 1150 FILE_CREATE, CREATE_NOT_FILE, ACL_NO_MODE); 1151 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name, 1152 &oparms, &in_iov, 1153 &(int){SMB2_OP_SET_INFO}, 1, 1154 cfile, NULL, NULL, NULL); 1155 if (tmprc == 0) 1156 cifs_i->cifsAttrs = dosattrs; 1157 } 1158 1159 int 1160 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, 1161 struct cifs_sb_info *cifs_sb) 1162 { 1163 struct cifs_open_parms oparms; 1164 1165 drop_cached_dir_by_name(xid, tcon, name, cifs_sb); 1166 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE, 1167 FILE_OPEN, CREATE_NOT_FILE, ACL_NO_MODE); 1168 return smb2_compound_op(xid, tcon, cifs_sb, 1169 name, &oparms, NULL, 1170 &(int){SMB2_OP_UNLINK}, 1, 1171 NULL, NULL, NULL, NULL); 1172 } 1173 1174 int 1175 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, 1176 struct cifs_sb_info *cifs_sb, struct dentry *dentry) 1177 { 1178 struct kvec open_iov[SMB2_CREATE_IOV_SIZE]; 1179 __le16 *utf16_path __free(kfree) = NULL; 1180 int retries = 0, cur_sleep = 1; 1181 struct TCP_Server_Info *server; 1182 struct cifs_open_parms oparms; 1183 struct smb2_create_req *creq; 1184 struct inode *inode = NULL; 1185 struct smb_rqst rqst[2]; 1186 struct kvec rsp_iov[2]; 1187 struct kvec close_iov; 1188 int resp_buftype[2]; 1189 struct cifs_fid fid; 1190 int flags = 0; 1191 __u8 oplock; 1192 int rc; 1193 1194 utf16_path = cifs_convert_path_to_utf16(name, cifs_sb); 1195 if (!utf16_path) 1196 return -ENOMEM; 1197 1198 if (smb3_encryption_required(tcon)) 1199 flags |= CIFS_TRANSFORM_REQ; 1200 again: 1201 oplock = SMB2_OPLOCK_LEVEL_NONE; 1202 server = cifs_pick_channel(tcon->ses); 1203 1204 memset(rqst, 0, sizeof(rqst)); 1205 memset(resp_buftype, 0, sizeof(resp_buftype)); 1206 memset(rsp_iov, 0, sizeof(rsp_iov)); 1207 1208 rqst[0].rq_iov = open_iov; 1209 rqst[0].rq_nvec = ARRAY_SIZE(open_iov); 1210 1211 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE | FILE_READ_ATTRIBUTES, 1212 FILE_OPEN, CREATE_DELETE_ON_CLOSE | 1213 OPEN_REPARSE_POINT, ACL_NO_MODE); 1214 oparms.fid = &fid; 1215 1216 if (dentry) { 1217 inode = d_inode(dentry); 1218 if (CIFS_I(inode)->lease_granted && server->ops->get_lease_key) { 1219 oplock = SMB2_OPLOCK_LEVEL_LEASE; 1220 server->ops->get_lease_key(inode, &fid); 1221 } 1222 } 1223 1224 rc = SMB2_open_init(tcon, server, 1225 &rqst[0], &oplock, &oparms, utf16_path); 1226 if (rc) 1227 goto err_free; 1228 smb2_set_next_command(tcon, &rqst[0]); 1229 creq = rqst[0].rq_iov[0].iov_base; 1230 creq->ShareAccess = FILE_SHARE_DELETE_LE; 1231 1232 rqst[1].rq_iov = &close_iov; 1233 rqst[1].rq_nvec = 1; 1234 1235 rc = SMB2_close_init(tcon, server, &rqst[1], 1236 COMPOUND_FID, COMPOUND_FID, false); 1237 smb2_set_related(&rqst[1]); 1238 if (rc) 1239 goto err_free; 1240 1241 if (retries) { 1242 for (int i = 0; i < ARRAY_SIZE(rqst); i++) 1243 smb2_set_replay(server, &rqst[i]); 1244 } 1245 1246 rc = compound_send_recv(xid, tcon->ses, server, flags, 1247 ARRAY_SIZE(rqst), rqst, 1248 resp_buftype, rsp_iov); 1249 SMB2_open_free(&rqst[0]); 1250 SMB2_close_free(&rqst[1]); 1251 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base); 1252 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base); 1253 1254 if (is_replayable_error(rc) && 1255 smb2_should_replay(tcon, &retries, &cur_sleep)) 1256 goto again; 1257 1258 /* Retry compound request without lease */ 1259 if (rc == -EINVAL && dentry) { 1260 dentry = NULL; 1261 retries = 0; 1262 cur_sleep = 1; 1263 goto again; 1264 } 1265 /* 1266 * If dentry (hence, inode) is NULL, lease break is going to 1267 * take care of degrading leases on handles for deleted files. 1268 */ 1269 if (!rc && inode) 1270 cifs_mark_open_handles_for_deleted_file(inode, name); 1271 1272 return rc; 1273 1274 err_free: 1275 SMB2_open_free(&rqst[0]); 1276 SMB2_close_free(&rqst[1]); 1277 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base); 1278 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base); 1279 return rc; 1280 } 1281 1282 static int smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon, 1283 const char *from_name, const char *to_name, 1284 struct cifs_sb_info *cifs_sb, 1285 __u32 create_options, __u32 access, 1286 int command, struct cifsFileInfo *cfile, 1287 struct dentry *dentry) 1288 { 1289 struct cifs_open_parms oparms; 1290 struct kvec in_iov; 1291 __le16 *smb2_to_name = NULL; 1292 int rc; 1293 1294 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb); 1295 if (smb2_to_name == NULL) { 1296 rc = -ENOMEM; 1297 goto smb2_rename_path; 1298 } 1299 in_iov.iov_base = smb2_to_name; 1300 in_iov.iov_len = 2 * UniStrnlen((wchar_t *)smb2_to_name, PATH_MAX); 1301 oparms = CIFS_OPARMS(cifs_sb, tcon, from_name, access, FILE_OPEN, 1302 create_options, ACL_NO_MODE); 1303 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, 1304 &oparms, &in_iov, &command, 1, 1305 cfile, NULL, NULL, dentry); 1306 smb2_rename_path: 1307 kfree(smb2_to_name); 1308 return rc; 1309 } 1310 1311 int smb2_rename_path(const unsigned int xid, 1312 struct cifs_tcon *tcon, 1313 struct dentry *source_dentry, 1314 const char *from_name, const char *to_name, 1315 struct cifs_sb_info *cifs_sb) 1316 { 1317 struct cifsFileInfo *cfile; 1318 __u32 co = file_create_options(source_dentry); 1319 1320 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb); 1321 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile); 1322 1323 int rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, 1324 co, DELETE, SMB2_OP_RENAME, cfile, source_dentry); 1325 if (rc == -EINVAL) { 1326 cifs_dbg(FYI, "invalid lease key, resending request without lease"); 1327 cifs_get_writable_path(tcon, from_name, 1328 FIND_WR_WITH_DELETE, &cfile); 1329 rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, 1330 co, DELETE, SMB2_OP_RENAME, cfile, NULL); 1331 } 1332 return rc; 1333 } 1334 1335 int smb2_create_hardlink(const unsigned int xid, 1336 struct cifs_tcon *tcon, 1337 struct dentry *source_dentry, 1338 const char *from_name, const char *to_name, 1339 struct cifs_sb_info *cifs_sb) 1340 { 1341 __u32 co = file_create_options(source_dentry); 1342 1343 return smb2_set_path_attr(xid, tcon, from_name, to_name, 1344 cifs_sb, co, FILE_READ_ATTRIBUTES, 1345 SMB2_OP_HARDLINK, NULL, NULL); 1346 } 1347 1348 int 1349 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon, 1350 const char *full_path, __u64 size, 1351 struct cifs_sb_info *cifs_sb, bool set_alloc, 1352 struct dentry *dentry) 1353 { 1354 struct cifs_open_parms oparms; 1355 struct cifsFileInfo *cfile; 1356 struct kvec in_iov; 1357 __le64 eof = cpu_to_le64(size); 1358 int rc; 1359 1360 in_iov.iov_base = &eof; 1361 in_iov.iov_len = sizeof(eof); 1362 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1363 1364 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_DATA, 1365 FILE_OPEN, 0, ACL_NO_MODE); 1366 rc = smb2_compound_op(xid, tcon, cifs_sb, 1367 full_path, &oparms, &in_iov, 1368 &(int){SMB2_OP_SET_EOF}, 1, 1369 cfile, NULL, NULL, dentry); 1370 if (rc == -EINVAL) { 1371 cifs_dbg(FYI, "invalid lease key, resending request without lease"); 1372 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1373 rc = smb2_compound_op(xid, tcon, cifs_sb, 1374 full_path, &oparms, &in_iov, 1375 &(int){SMB2_OP_SET_EOF}, 1, 1376 cfile, NULL, NULL, NULL); 1377 } 1378 return rc; 1379 } 1380 1381 int 1382 smb2_set_file_info(struct inode *inode, const char *full_path, 1383 FILE_BASIC_INFO *buf, const unsigned int xid) 1384 { 1385 struct cifs_open_parms oparms; 1386 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1387 struct tcon_link *tlink; 1388 struct cifs_tcon *tcon; 1389 struct cifsFileInfo *cfile; 1390 struct kvec in_iov = { .iov_base = buf, .iov_len = sizeof(*buf), }; 1391 int rc; 1392 1393 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) && 1394 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) && 1395 (buf->Attributes == 0)) 1396 return 0; /* would be a no op, no sense sending this */ 1397 1398 tlink = cifs_sb_tlink(cifs_sb); 1399 if (IS_ERR(tlink)) 1400 return PTR_ERR(tlink); 1401 tcon = tlink_tcon(tlink); 1402 1403 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1404 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_ATTRIBUTES, 1405 FILE_OPEN, 0, ACL_NO_MODE); 1406 rc = smb2_compound_op(xid, tcon, cifs_sb, 1407 full_path, &oparms, &in_iov, 1408 &(int){SMB2_OP_SET_INFO}, 1, 1409 cfile, NULL, NULL, NULL); 1410 cifs_put_tlink(tlink); 1411 return rc; 1412 } 1413 1414 struct inode *smb2_create_reparse_inode(struct cifs_open_info_data *data, 1415 struct super_block *sb, 1416 const unsigned int xid, 1417 struct cifs_tcon *tcon, 1418 const char *full_path, 1419 bool directory, 1420 struct kvec *reparse_iov, 1421 struct kvec *xattr_iov) 1422 { 1423 struct cifs_open_parms oparms; 1424 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 1425 struct cifsFileInfo *cfile; 1426 struct inode *new = NULL; 1427 int out_buftype[4] = {}; 1428 struct kvec out_iov[4] = {}; 1429 struct kvec in_iov[2]; 1430 int cmds[2]; 1431 int rc; 1432 int i; 1433 1434 /* 1435 * If server filesystem does not support reparse points then do not 1436 * attempt to create reparse point. This will prevent creating unusable 1437 * empty object on the server. 1438 */ 1439 if (!CIFS_REPARSE_SUPPORT(tcon)) 1440 return ERR_PTR(-EOPNOTSUPP); 1441 1442 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1443 SYNCHRONIZE | DELETE | 1444 FILE_READ_ATTRIBUTES | 1445 FILE_WRITE_ATTRIBUTES, 1446 FILE_CREATE, 1447 (directory ? CREATE_NOT_FILE : CREATE_NOT_DIR) | OPEN_REPARSE_POINT, 1448 ACL_NO_MODE); 1449 if (xattr_iov) 1450 oparms.ea_cctx = xattr_iov; 1451 1452 cmds[0] = SMB2_OP_SET_REPARSE; 1453 in_iov[0] = *reparse_iov; 1454 in_iov[1].iov_base = data; 1455 in_iov[1].iov_len = sizeof(*data); 1456 1457 if (tcon->posix_extensions) { 1458 cmds[1] = SMB2_OP_POSIX_QUERY_INFO; 1459 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1460 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, 1461 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL); 1462 if (!rc) { 1463 rc = smb311_posix_get_inode_info(&new, full_path, 1464 data, sb, xid); 1465 } 1466 } else { 1467 cmds[1] = SMB2_OP_QUERY_INFO; 1468 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1469 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, 1470 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL); 1471 if (!rc) { 1472 rc = cifs_get_inode_info(&new, full_path, 1473 data, sb, xid, NULL); 1474 } 1475 } 1476 1477 1478 /* 1479 * If CREATE was successful but SMB2_OP_SET_REPARSE failed then 1480 * remove the intermediate object created by CREATE. Otherwise 1481 * empty object stay on the server when reparse call failed. 1482 */ 1483 if (rc && 1484 out_iov[0].iov_base != NULL && out_buftype[0] != CIFS_NO_BUFFER && 1485 ((struct smb2_hdr *)out_iov[0].iov_base)->Status == STATUS_SUCCESS && 1486 (out_iov[1].iov_base == NULL || out_buftype[1] == CIFS_NO_BUFFER || 1487 ((struct smb2_hdr *)out_iov[1].iov_base)->Status != STATUS_SUCCESS)) 1488 smb2_unlink(xid, tcon, full_path, cifs_sb, NULL); 1489 1490 for (i = 0; i < ARRAY_SIZE(out_buftype); i++) 1491 free_rsp_buf(out_buftype[i], out_iov[i].iov_base); 1492 1493 return rc ? ERR_PTR(rc) : new; 1494 } 1495 1496 int smb2_query_reparse_point(const unsigned int xid, 1497 struct cifs_tcon *tcon, 1498 struct cifs_sb_info *cifs_sb, 1499 const char *full_path, 1500 u32 *tag, struct kvec *rsp, 1501 int *rsp_buftype) 1502 { 1503 struct cifs_open_parms oparms; 1504 struct cifs_open_info_data data = {}; 1505 struct cifsFileInfo *cfile; 1506 struct kvec in_iov = { .iov_base = &data, .iov_len = sizeof(data), }; 1507 int rc; 1508 1509 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path); 1510 1511 cifs_get_readable_path(tcon, full_path, &cfile); 1512 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1513 FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE, 1514 FILE_OPEN, OPEN_REPARSE_POINT, ACL_NO_MODE); 1515 rc = smb2_compound_op(xid, tcon, cifs_sb, 1516 full_path, &oparms, &in_iov, 1517 &(int){SMB2_OP_GET_REPARSE}, 1, 1518 cfile, NULL, NULL, NULL); 1519 if (rc) 1520 goto out; 1521 1522 *tag = data.reparse.tag; 1523 *rsp = data.reparse.io.iov; 1524 *rsp_buftype = data.reparse.io.buftype; 1525 memset(&data.reparse.io.iov, 0, sizeof(data.reparse.io.iov)); 1526 data.reparse.io.buftype = CIFS_NO_BUFFER; 1527 out: 1528 cifs_free_open_info(&data); 1529 return rc; 1530 } 1531 1532 static inline __le16 *utf16_smb2_path(struct cifs_sb_info *cifs_sb, 1533 const char *name, size_t namelen) 1534 { 1535 int len; 1536 1537 if (*name == '\\' || 1538 (cifs_sb_master_tlink(cifs_sb) && 1539 cifs_sb_master_tcon(cifs_sb)->posix_extensions && *name == '/')) 1540 name++; 1541 return cifs_strndup_to_utf16(name, namelen, &len, 1542 cifs_sb->local_nls, 1543 cifs_remap(cifs_sb)); 1544 } 1545 1546 int smb2_rename_pending_delete(const char *full_path, 1547 struct dentry *dentry, 1548 const unsigned int xid) 1549 { 1550 struct cifs_sb_info *cifs_sb = CIFS_SB(d_inode(dentry)->i_sb); 1551 struct cifsInodeInfo *cinode = CIFS_I(d_inode(dentry)); 1552 __le16 *utf16_path __free(kfree) = NULL; 1553 __u32 co = file_create_options(dentry); 1554 int cmds[] = { 1555 SMB2_OP_SET_INFO, 1556 SMB2_OP_RENAME, 1557 SMB2_OP_UNLINK, 1558 }; 1559 const int num_cmds = ARRAY_SIZE(cmds); 1560 char *to_name __free(kfree) = NULL; 1561 __u32 attrs = cinode->cifsAttrs; 1562 struct cifs_open_parms oparms; 1563 static atomic_t sillycounter; 1564 struct cifsFileInfo *cfile; 1565 struct tcon_link *tlink; 1566 struct cifs_tcon *tcon; 1567 struct kvec iov[2]; 1568 const char *ppath; 1569 void *page; 1570 size_t len; 1571 int rc; 1572 1573 tlink = cifs_sb_tlink(cifs_sb); 1574 if (IS_ERR(tlink)) 1575 return PTR_ERR(tlink); 1576 tcon = tlink_tcon(tlink); 1577 1578 page = alloc_dentry_path(); 1579 1580 ppath = build_path_from_dentry(dentry->d_parent, page); 1581 if (IS_ERR(ppath)) { 1582 rc = PTR_ERR(ppath); 1583 goto out; 1584 } 1585 1586 len = strlen(ppath) + strlen("/.__smb1234") + 1; 1587 to_name = kmalloc(len, GFP_KERNEL); 1588 if (!to_name) { 1589 rc = -ENOMEM; 1590 goto out; 1591 } 1592 1593 scnprintf(to_name, len, "%s%c.__smb%04X", ppath, CIFS_DIR_SEP(cifs_sb), 1594 atomic_inc_return(&sillycounter) & 0xffff); 1595 1596 utf16_path = utf16_smb2_path(cifs_sb, to_name, len); 1597 if (!utf16_path) { 1598 rc = -ENOMEM; 1599 goto out; 1600 } 1601 1602 drop_cached_dir_by_name(xid, tcon, full_path, cifs_sb); 1603 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1604 DELETE | FILE_WRITE_ATTRIBUTES, 1605 FILE_OPEN, co, ACL_NO_MODE); 1606 1607 attrs &= ~ATTR_READONLY; 1608 if (!attrs) 1609 attrs = ATTR_NORMAL; 1610 if (d_inode(dentry)->i_nlink <= 1) 1611 attrs |= ATTR_HIDDEN; 1612 iov[0].iov_base = &(FILE_BASIC_INFO) { 1613 .Attributes = cpu_to_le32(attrs), 1614 }; 1615 iov[0].iov_len = sizeof(FILE_BASIC_INFO); 1616 iov[1].iov_base = utf16_path; 1617 iov[1].iov_len = sizeof(*utf16_path) * UniStrlen((wchar_t *)utf16_path); 1618 1619 cifs_get_writable_path(tcon, full_path, FIND_WR_WITH_DELETE, &cfile); 1620 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, iov, 1621 cmds, num_cmds, cfile, NULL, NULL, dentry); 1622 if (rc == -EINVAL) { 1623 cifs_dbg(FYI, "invalid lease key, resending request without lease\n"); 1624 cifs_get_writable_path(tcon, full_path, 1625 FIND_WR_WITH_DELETE, &cfile); 1626 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, iov, 1627 cmds, num_cmds, cfile, NULL, NULL, NULL); 1628 } 1629 if (!rc) { 1630 set_bit(CIFS_INO_DELETE_PENDING, &cinode->flags); 1631 } else { 1632 cifs_tcon_dbg(FYI, "%s: failed to rename '%s' to '%s': %d\n", 1633 __func__, full_path, to_name, rc); 1634 rc = -EIO; 1635 } 1636 out: 1637 cifs_put_tlink(tlink); 1638 free_dentry_path(page); 1639 return rc; 1640 } 1641