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