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_DELETE: 350 trace_smb3_delete_enter(xid, tcon->tid, ses->Suid, full_path); 351 break; 352 case SMB2_OP_MKDIR: 353 /* 354 * Directories are created through parameters in the 355 * SMB2_open() call. 356 */ 357 trace_smb3_mkdir_enter(xid, tcon->tid, ses->Suid, full_path); 358 break; 359 case SMB2_OP_RMDIR: 360 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 361 rqst[num_rqst].rq_nvec = 1; 362 363 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */ 364 data[0] = &delete_pending[0]; 365 366 rc = SMB2_set_info_init(tcon, server, 367 &rqst[num_rqst], COMPOUND_FID, 368 COMPOUND_FID, current->tgid, 369 FILE_DISPOSITION_INFORMATION, 370 SMB2_O_INFO_FILE, 0, data, size); 371 if (rc) 372 goto finished; 373 smb2_set_next_command(tcon, &rqst[num_rqst]); 374 smb2_set_related(&rqst[num_rqst++]); 375 trace_smb3_rmdir_enter(xid, tcon->tid, ses->Suid, full_path); 376 break; 377 case SMB2_OP_SET_EOF: 378 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 379 rqst[num_rqst].rq_nvec = 1; 380 381 size[0] = in_iov[i].iov_len; 382 data[0] = in_iov[i].iov_base; 383 384 if (cfile) { 385 rc = SMB2_set_info_init(tcon, server, 386 &rqst[num_rqst], 387 cfile->fid.persistent_fid, 388 cfile->fid.volatile_fid, 389 current->tgid, 390 FILE_END_OF_FILE_INFORMATION, 391 SMB2_O_INFO_FILE, 0, 392 data, size); 393 } else { 394 rc = SMB2_set_info_init(tcon, server, 395 &rqst[num_rqst], 396 COMPOUND_FID, 397 COMPOUND_FID, 398 current->tgid, 399 FILE_END_OF_FILE_INFORMATION, 400 SMB2_O_INFO_FILE, 0, 401 data, size); 402 } 403 if (!rc && (!cfile || num_rqst > 1)) { 404 smb2_set_next_command(tcon, &rqst[num_rqst]); 405 smb2_set_related(&rqst[num_rqst]); 406 } else if (rc) { 407 goto finished; 408 } 409 num_rqst++; 410 trace_smb3_set_eof_enter(xid, tcon->tid, ses->Suid, full_path); 411 break; 412 case SMB2_OP_SET_INFO: 413 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 414 rqst[num_rqst].rq_nvec = 1; 415 416 size[0] = in_iov[i].iov_len; 417 data[0] = in_iov[i].iov_base; 418 419 if (cfile) { 420 rc = SMB2_set_info_init(tcon, server, 421 &rqst[num_rqst], 422 cfile->fid.persistent_fid, 423 cfile->fid.volatile_fid, current->tgid, 424 FILE_BASIC_INFORMATION, 425 SMB2_O_INFO_FILE, 0, data, size); 426 } else { 427 rc = SMB2_set_info_init(tcon, server, 428 &rqst[num_rqst], 429 COMPOUND_FID, 430 COMPOUND_FID, current->tgid, 431 FILE_BASIC_INFORMATION, 432 SMB2_O_INFO_FILE, 0, data, size); 433 } 434 if (!rc && (!cfile || num_rqst > 1)) { 435 smb2_set_next_command(tcon, &rqst[num_rqst]); 436 smb2_set_related(&rqst[num_rqst]); 437 } else if (rc) { 438 goto finished; 439 } 440 num_rqst++; 441 trace_smb3_set_info_compound_enter(xid, tcon->tid, 442 ses->Suid, full_path); 443 break; 444 case SMB2_OP_RENAME: 445 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 446 rqst[num_rqst].rq_nvec = 2; 447 448 len = in_iov[i].iov_len; 449 450 vars->rename_info.ReplaceIfExists = 1; 451 vars->rename_info.RootDirectory = 0; 452 vars->rename_info.FileNameLength = cpu_to_le32(len); 453 454 size[0] = sizeof(struct smb2_file_rename_info); 455 data[0] = &vars->rename_info; 456 457 size[1] = len + 2 /* null */; 458 data[1] = in_iov[i].iov_base; 459 460 if (cfile) { 461 rc = SMB2_set_info_init(tcon, server, 462 &rqst[num_rqst], 463 cfile->fid.persistent_fid, 464 cfile->fid.volatile_fid, 465 current->tgid, FILE_RENAME_INFORMATION, 466 SMB2_O_INFO_FILE, 0, data, size); 467 } else { 468 rc = SMB2_set_info_init(tcon, server, 469 &rqst[num_rqst], 470 COMPOUND_FID, COMPOUND_FID, 471 current->tgid, FILE_RENAME_INFORMATION, 472 SMB2_O_INFO_FILE, 0, data, size); 473 } 474 if (!rc && (!cfile || num_rqst > 1)) { 475 smb2_set_next_command(tcon, &rqst[num_rqst]); 476 smb2_set_related(&rqst[num_rqst]); 477 } else if (rc) { 478 goto finished; 479 } 480 num_rqst++; 481 trace_smb3_rename_enter(xid, tcon->tid, ses->Suid, full_path); 482 break; 483 case SMB2_OP_HARDLINK: 484 rqst[num_rqst].rq_iov = &vars->si_iov[0]; 485 rqst[num_rqst].rq_nvec = 2; 486 487 len = in_iov[i].iov_len; 488 489 vars->link_info.ReplaceIfExists = 0; 490 vars->link_info.RootDirectory = 0; 491 vars->link_info.FileNameLength = cpu_to_le32(len); 492 493 size[0] = sizeof(struct smb2_file_link_info); 494 data[0] = &vars->link_info; 495 496 size[1] = len + 2 /* null */; 497 data[1] = in_iov[i].iov_base; 498 499 rc = SMB2_set_info_init(tcon, server, 500 &rqst[num_rqst], COMPOUND_FID, 501 COMPOUND_FID, current->tgid, 502 FILE_LINK_INFORMATION, 503 SMB2_O_INFO_FILE, 0, data, size); 504 if (rc) 505 goto finished; 506 smb2_set_next_command(tcon, &rqst[num_rqst]); 507 smb2_set_related(&rqst[num_rqst++]); 508 trace_smb3_hardlink_enter(xid, tcon->tid, ses->Suid, full_path); 509 break; 510 case SMB2_OP_SET_REPARSE: 511 rqst[num_rqst].rq_iov = vars->io_iov; 512 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov); 513 514 if (cfile) { 515 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 516 cfile->fid.persistent_fid, 517 cfile->fid.volatile_fid, 518 FSCTL_SET_REPARSE_POINT, 519 in_iov[i].iov_base, 520 in_iov[i].iov_len, 0); 521 } else { 522 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 523 COMPOUND_FID, COMPOUND_FID, 524 FSCTL_SET_REPARSE_POINT, 525 in_iov[i].iov_base, 526 in_iov[i].iov_len, 0); 527 } 528 if (!rc && (!cfile || num_rqst > 1)) { 529 smb2_set_next_command(tcon, &rqst[num_rqst]); 530 smb2_set_related(&rqst[num_rqst]); 531 } else if (rc) { 532 goto finished; 533 } 534 num_rqst++; 535 trace_smb3_set_reparse_compound_enter(xid, tcon->tid, 536 ses->Suid, full_path); 537 break; 538 case SMB2_OP_GET_REPARSE: 539 rqst[num_rqst].rq_iov = vars->io_iov; 540 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov); 541 542 if (cfile) { 543 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 544 cfile->fid.persistent_fid, 545 cfile->fid.volatile_fid, 546 FSCTL_GET_REPARSE_POINT, 547 NULL, 0, CIFSMaxBufSize); 548 } else { 549 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst], 550 COMPOUND_FID, COMPOUND_FID, 551 FSCTL_GET_REPARSE_POINT, 552 NULL, 0, CIFSMaxBufSize); 553 } 554 if (!rc && (!cfile || num_rqst > 1)) { 555 smb2_set_next_command(tcon, &rqst[num_rqst]); 556 smb2_set_related(&rqst[num_rqst]); 557 } else if (rc) { 558 goto finished; 559 } 560 num_rqst++; 561 trace_smb3_get_reparse_compound_enter(xid, tcon->tid, 562 ses->Suid, full_path); 563 break; 564 case SMB2_OP_QUERY_WSL_EA: 565 rqst[num_rqst].rq_iov = &vars->ea_iov; 566 rqst[num_rqst].rq_nvec = 1; 567 568 if (cfile) { 569 rc = SMB2_query_info_init(tcon, server, 570 &rqst[num_rqst], 571 cfile->fid.persistent_fid, 572 cfile->fid.volatile_fid, 573 FILE_FULL_EA_INFORMATION, 574 SMB2_O_INFO_FILE, 0, 575 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE, 576 sizeof(wsl_query_eas), 577 (void *)wsl_query_eas); 578 } else { 579 rc = SMB2_query_info_init(tcon, server, 580 &rqst[num_rqst], 581 COMPOUND_FID, 582 COMPOUND_FID, 583 FILE_FULL_EA_INFORMATION, 584 SMB2_O_INFO_FILE, 0, 585 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE, 586 sizeof(wsl_query_eas), 587 (void *)wsl_query_eas); 588 } 589 if (!rc && (!cfile || num_rqst > 1)) { 590 smb2_set_next_command(tcon, &rqst[num_rqst]); 591 smb2_set_related(&rqst[num_rqst]); 592 } else if (rc) { 593 goto finished; 594 } 595 num_rqst++; 596 trace_smb3_query_wsl_ea_compound_enter(xid, tcon->tid, 597 ses->Suid, full_path); 598 break; 599 default: 600 cifs_dbg(VFS, "Invalid command\n"); 601 rc = -EINVAL; 602 } 603 } 604 if (rc) 605 goto finished; 606 607 /* We already have a handle so we can skip the close */ 608 if (cfile) 609 goto after_close; 610 /* Close */ 611 flags |= CIFS_CP_CREATE_CLOSE_OP; 612 rqst[num_rqst].rq_iov = &vars->close_iov; 613 rqst[num_rqst].rq_nvec = 1; 614 rc = SMB2_close_init(tcon, server, 615 &rqst[num_rqst], COMPOUND_FID, 616 COMPOUND_FID, false); 617 smb2_set_related(&rqst[num_rqst]); 618 if (rc) 619 goto finished; 620 after_close: 621 num_rqst++; 622 623 if (cfile) { 624 if (retries) 625 for (i = 1; i < num_rqst - 2; i++) 626 smb2_set_replay(server, &rqst[i]); 627 628 rc = compound_send_recv(xid, ses, server, 629 flags, num_rqst - 2, 630 &rqst[1], &resp_buftype[1], 631 &rsp_iov[1]); 632 } else { 633 if (retries) 634 for (i = 0; i < num_rqst; i++) 635 smb2_set_replay(server, &rqst[i]); 636 637 rc = compound_send_recv(xid, ses, server, 638 flags, num_rqst, 639 rqst, resp_buftype, 640 rsp_iov); 641 } 642 643 finished: 644 num_rqst = 0; 645 SMB2_open_free(&rqst[num_rqst++]); 646 if (rc == -EREMCHG) { 647 pr_warn_once("server share %s deleted\n", tcon->tree_name); 648 tcon->need_reconnect = true; 649 } 650 651 tmp_rc = rc; 652 653 if (rc == 0 && num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY) { 654 create_rsp = rsp_iov[0].iov_base; 655 idata = in_iov[0].iov_base; 656 idata->fi.CreationTime = create_rsp->CreationTime; 657 idata->fi.LastAccessTime = create_rsp->LastAccessTime; 658 idata->fi.LastWriteTime = create_rsp->LastWriteTime; 659 idata->fi.ChangeTime = create_rsp->ChangeTime; 660 idata->fi.Attributes = create_rsp->FileAttributes; 661 idata->fi.AllocationSize = create_rsp->AllocationSize; 662 idata->fi.EndOfFile = create_rsp->EndofFile; 663 if (le32_to_cpu(idata->fi.NumberOfLinks) == 0) 664 idata->fi.NumberOfLinks = cpu_to_le32(1); /* dummy value */ 665 idata->fi.DeletePending = 0; 666 idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY); 667 668 /* smb2_parse_contexts() fills idata->fi.IndexNumber */ 669 rc = smb2_parse_contexts(server, &rsp_iov[0], &oparms->fid->epoch, 670 oparms->fid->lease_key, &oplock, &idata->fi, NULL); 671 if (rc) 672 cifs_dbg(VFS, "rc: %d parsing context of compound op\n", rc); 673 } 674 675 for (i = 0; i < num_cmds; i++) { 676 char *buf = rsp_iov[i + i].iov_base; 677 678 if (buf && resp_buftype[i + 1] != CIFS_NO_BUFFER) 679 rc = server->ops->map_error(buf, false); 680 else 681 rc = tmp_rc; 682 switch (cmds[i]) { 683 case SMB2_OP_QUERY_INFO: 684 idata = in_iov[i].iov_base; 685 idata->contains_posix_file_info = false; 686 if (rc == 0 && cfile && cfile->symlink_target) { 687 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL); 688 if (!idata->symlink_target) 689 rc = -ENOMEM; 690 } 691 if (rc == 0) { 692 qi_rsp = (struct smb2_query_info_rsp *) 693 rsp_iov[i + 1].iov_base; 694 rc = smb2_validate_and_copy_iov( 695 le16_to_cpu(qi_rsp->OutputBufferOffset), 696 le32_to_cpu(qi_rsp->OutputBufferLength), 697 &rsp_iov[i + 1], sizeof(idata->fi), (char *)&idata->fi); 698 } 699 SMB2_query_info_free(&rqst[num_rqst++]); 700 if (rc) 701 trace_smb3_query_info_compound_err(xid, tcon->tid, 702 ses->Suid, rc); 703 else 704 trace_smb3_query_info_compound_done(xid, tcon->tid, 705 ses->Suid); 706 break; 707 case SMB2_OP_POSIX_QUERY_INFO: 708 idata = in_iov[i].iov_base; 709 idata->contains_posix_file_info = true; 710 if (rc == 0 && cfile && cfile->symlink_target) { 711 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL); 712 if (!idata->symlink_target) 713 rc = -ENOMEM; 714 } 715 if (rc == 0) { 716 qi_rsp = (struct smb2_query_info_rsp *) 717 rsp_iov[i + 1].iov_base; 718 rc = smb2_validate_and_copy_iov( 719 le16_to_cpu(qi_rsp->OutputBufferOffset), 720 le32_to_cpu(qi_rsp->OutputBufferLength), 721 &rsp_iov[i + 1], sizeof(idata->posix_fi) /* add SIDs */, 722 (char *)&idata->posix_fi); 723 } 724 if (rc == 0) 725 rc = parse_posix_sids(idata, &rsp_iov[i + 1]); 726 727 SMB2_query_info_free(&rqst[num_rqst++]); 728 if (rc) 729 trace_smb3_posix_query_info_compound_err(xid, tcon->tid, 730 ses->Suid, rc); 731 else 732 trace_smb3_posix_query_info_compound_done(xid, tcon->tid, 733 ses->Suid); 734 break; 735 case SMB2_OP_DELETE: 736 if (rc) 737 trace_smb3_delete_err(xid, tcon->tid, ses->Suid, rc); 738 else { 739 /* 740 * If dentry (hence, inode) is NULL, lease break is going to 741 * take care of degrading leases on handles for deleted files. 742 */ 743 if (inode) 744 cifs_mark_open_handles_for_deleted_file(inode, full_path); 745 trace_smb3_delete_done(xid, tcon->tid, ses->Suid); 746 } 747 break; 748 case SMB2_OP_MKDIR: 749 if (rc) 750 trace_smb3_mkdir_err(xid, tcon->tid, ses->Suid, rc); 751 else 752 trace_smb3_mkdir_done(xid, tcon->tid, ses->Suid); 753 break; 754 case SMB2_OP_HARDLINK: 755 if (rc) 756 trace_smb3_hardlink_err(xid, tcon->tid, ses->Suid, rc); 757 else 758 trace_smb3_hardlink_done(xid, tcon->tid, ses->Suid); 759 SMB2_set_info_free(&rqst[num_rqst++]); 760 break; 761 case SMB2_OP_RENAME: 762 if (rc) 763 trace_smb3_rename_err(xid, tcon->tid, ses->Suid, rc); 764 else 765 trace_smb3_rename_done(xid, tcon->tid, ses->Suid); 766 SMB2_set_info_free(&rqst[num_rqst++]); 767 break; 768 case SMB2_OP_RMDIR: 769 if (rc) 770 trace_smb3_rmdir_err(xid, tcon->tid, ses->Suid, rc); 771 else 772 trace_smb3_rmdir_done(xid, tcon->tid, ses->Suid); 773 SMB2_set_info_free(&rqst[num_rqst++]); 774 break; 775 case SMB2_OP_SET_EOF: 776 if (rc) 777 trace_smb3_set_eof_err(xid, tcon->tid, ses->Suid, rc); 778 else 779 trace_smb3_set_eof_done(xid, tcon->tid, ses->Suid); 780 SMB2_set_info_free(&rqst[num_rqst++]); 781 break; 782 case SMB2_OP_SET_INFO: 783 if (rc) 784 trace_smb3_set_info_compound_err(xid, tcon->tid, 785 ses->Suid, rc); 786 else 787 trace_smb3_set_info_compound_done(xid, tcon->tid, 788 ses->Suid); 789 SMB2_set_info_free(&rqst[num_rqst++]); 790 break; 791 case SMB2_OP_SET_REPARSE: 792 if (rc) { 793 trace_smb3_set_reparse_compound_err(xid, tcon->tid, 794 ses->Suid, rc); 795 } else { 796 trace_smb3_set_reparse_compound_done(xid, tcon->tid, 797 ses->Suid); 798 } 799 SMB2_ioctl_free(&rqst[num_rqst++]); 800 break; 801 case SMB2_OP_GET_REPARSE: 802 if (!rc) { 803 iov = &rsp_iov[i + 1]; 804 idata = in_iov[i].iov_base; 805 idata->reparse.io.iov = *iov; 806 idata->reparse.io.buftype = resp_buftype[i + 1]; 807 idata->contains_posix_file_info = false; /* BB VERIFY */ 808 rbuf = reparse_buf_ptr(iov); 809 if (IS_ERR(rbuf)) { 810 rc = PTR_ERR(rbuf); 811 trace_smb3_get_reparse_compound_err(xid, tcon->tid, 812 ses->Suid, rc); 813 } else { 814 idata->reparse.tag = le32_to_cpu(rbuf->ReparseTag); 815 trace_smb3_get_reparse_compound_done(xid, tcon->tid, 816 ses->Suid); 817 } 818 memset(iov, 0, sizeof(*iov)); 819 resp_buftype[i + 1] = CIFS_NO_BUFFER; 820 } else { 821 trace_smb3_get_reparse_compound_err(xid, tcon->tid, 822 ses->Suid, rc); 823 } 824 SMB2_ioctl_free(&rqst[num_rqst++]); 825 break; 826 case SMB2_OP_QUERY_WSL_EA: 827 if (!rc) { 828 idata = in_iov[i].iov_base; 829 idata->contains_posix_file_info = false; 830 qi_rsp = rsp_iov[i + 1].iov_base; 831 data[0] = (u8 *)qi_rsp + le16_to_cpu(qi_rsp->OutputBufferOffset); 832 size[0] = le32_to_cpu(qi_rsp->OutputBufferLength); 833 rc = check_wsl_eas(&rsp_iov[i + 1]); 834 if (!rc) { 835 memcpy(idata->wsl.eas, data[0], size[0]); 836 idata->wsl.eas_len = size[0]; 837 } 838 } 839 if (!rc) { 840 trace_smb3_query_wsl_ea_compound_done(xid, tcon->tid, 841 ses->Suid); 842 } else { 843 trace_smb3_query_wsl_ea_compound_err(xid, tcon->tid, 844 ses->Suid, rc); 845 } 846 SMB2_query_info_free(&rqst[num_rqst++]); 847 break; 848 } 849 } 850 SMB2_close_free(&rqst[num_rqst]); 851 rc = tmp_rc; 852 853 num_cmds += 2; 854 if (out_iov && out_buftype) { 855 memcpy(out_iov, rsp_iov, num_cmds * sizeof(*out_iov)); 856 memcpy(out_buftype, resp_buftype, 857 num_cmds * sizeof(*out_buftype)); 858 } else { 859 for (i = 0; i < num_cmds; i++) 860 free_rsp_buf(resp_buftype[i], rsp_iov[i].iov_base); 861 } 862 num_cmds -= 2; /* correct num_cmds as there could be a retry */ 863 kfree(vars); 864 865 if (is_replayable_error(rc) && 866 smb2_should_replay(tcon, &retries, &cur_sleep)) 867 goto replay_again; 868 869 out: 870 if (cfile) 871 cifsFileInfo_put(cfile); 872 873 return rc; 874 } 875 876 static int parse_create_response(struct cifs_open_info_data *data, 877 struct cifs_sb_info *cifs_sb, 878 const char *full_path, 879 const struct kvec *iov) 880 { 881 struct smb2_create_rsp *rsp = iov->iov_base; 882 bool reparse_point = false; 883 u32 tag = 0; 884 int rc = 0; 885 886 switch (rsp->hdr.Status) { 887 case STATUS_IO_REPARSE_TAG_NOT_HANDLED: 888 reparse_point = true; 889 break; 890 case STATUS_STOPPED_ON_SYMLINK: 891 rc = smb2_parse_symlink_response(cifs_sb, iov, 892 full_path, 893 &data->symlink_target); 894 if (rc) 895 return rc; 896 tag = IO_REPARSE_TAG_SYMLINK; 897 reparse_point = true; 898 break; 899 case STATUS_SUCCESS: 900 reparse_point = !!(rsp->Flags & SMB2_CREATE_FLAG_REPARSEPOINT); 901 break; 902 } 903 data->reparse_point = reparse_point; 904 data->reparse.tag = tag; 905 return rc; 906 } 907 908 /* Check only if SMB2_OP_QUERY_WSL_EA command failed in the compound chain */ 909 static bool ea_unsupported(int *cmds, int num_cmds, 910 struct kvec *out_iov, int *out_buftype) 911 { 912 int i; 913 914 if (cmds[num_cmds - 1] != SMB2_OP_QUERY_WSL_EA) 915 return false; 916 917 for (i = 1; i < num_cmds - 1; i++) { 918 struct smb2_hdr *hdr = out_iov[i].iov_base; 919 920 if (out_buftype[i] == CIFS_NO_BUFFER || !hdr || 921 hdr->Status != STATUS_SUCCESS) 922 return false; 923 } 924 return true; 925 } 926 927 static inline void free_rsp_iov(struct kvec *iovs, int *buftype, int count) 928 { 929 int i; 930 931 for (i = 0; i < count; i++) { 932 free_rsp_buf(buftype[i], iovs[i].iov_base); 933 memset(&iovs[i], 0, sizeof(*iovs)); 934 buftype[i] = CIFS_NO_BUFFER; 935 } 936 } 937 938 int smb2_query_path_info(const unsigned int xid, 939 struct cifs_tcon *tcon, 940 struct cifs_sb_info *cifs_sb, 941 const char *full_path, 942 struct cifs_open_info_data *data) 943 { 944 struct kvec in_iov[3], out_iov[5] = {}; 945 struct cached_fid *cfid = NULL; 946 struct cifs_open_parms oparms; 947 struct cifsFileInfo *cfile; 948 __u32 create_options = 0; 949 int out_buftype[5] = {}; 950 struct smb2_hdr *hdr; 951 int num_cmds = 0; 952 int cmds[3]; 953 bool islink; 954 int rc, rc2; 955 956 data->adjust_tz = false; 957 data->reparse_point = false; 958 959 /* 960 * BB TODO: Add support for using cached root handle in SMB3.1.1 POSIX. 961 * Create SMB2_query_posix_info worker function to do non-compounded 962 * query when we already have an open file handle for this. For now this 963 * is fast enough (always using the compounded version). 964 */ 965 if (!tcon->posix_extensions) { 966 if (*full_path) { 967 rc = -ENOENT; 968 } else { 969 rc = open_cached_dir(xid, tcon, full_path, 970 cifs_sb, false, &cfid); 971 } 972 /* If it is a root and its handle is cached then use it */ 973 if (!rc) { 974 if (cfid->file_all_info_is_valid) { 975 memcpy(&data->fi, &cfid->file_all_info, 976 sizeof(data->fi)); 977 } else { 978 rc = SMB2_query_info(xid, tcon, 979 cfid->fid.persistent_fid, 980 cfid->fid.volatile_fid, 981 &data->fi); 982 } 983 close_cached_dir(cfid); 984 return rc; 985 } 986 cmds[num_cmds++] = SMB2_OP_QUERY_INFO; 987 } else { 988 cmds[num_cmds++] = SMB2_OP_POSIX_QUERY_INFO; 989 } 990 991 in_iov[0].iov_base = data; 992 in_iov[0].iov_len = sizeof(*data); 993 in_iov[1] = in_iov[0]; 994 in_iov[2] = in_iov[0]; 995 996 cifs_get_readable_path(tcon, full_path, &cfile); 997 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_READ_ATTRIBUTES, 998 FILE_OPEN, create_options, ACL_NO_MODE); 999 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1000 &oparms, in_iov, cmds, num_cmds, 1001 cfile, out_iov, out_buftype, NULL); 1002 hdr = out_iov[0].iov_base; 1003 /* 1004 * If first iov is unset, then SMB session was dropped or we've got a 1005 * cached open file (@cfile). 1006 */ 1007 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER) 1008 goto out; 1009 1010 switch (rc) { 1011 case 0: 1012 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1013 break; 1014 case -EACCES: 1015 /* 1016 * If SMB2_OP_QUERY_INFO (called when POSIX extensions are not used) failed with 1017 * STATUS_ACCESS_DENIED then it means that caller does not have permission to 1018 * open the path with FILE_READ_ATTRIBUTES access and therefore cannot issue 1019 * SMB2_OP_QUERY_INFO command. 1020 * 1021 * There is an alternative way how to query limited information about path but still 1022 * suitable for stat() syscall. SMB2 OPEN/CREATE operation returns in its successful 1023 * response subset of query information. 1024 * 1025 * So try to open the path without FILE_READ_ATTRIBUTES but with MAXIMUM_ALLOWED 1026 * access which will grant the maximum possible access to the file and the response 1027 * will contain required query information for stat() syscall. 1028 */ 1029 1030 if (tcon->posix_extensions) 1031 break; 1032 1033 num_cmds = 1; 1034 cmds[0] = SMB2_OP_OPEN_QUERY; 1035 in_iov[0].iov_base = data; 1036 in_iov[0].iov_len = sizeof(*data); 1037 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, MAXIMUM_ALLOWED, 1038 FILE_OPEN, create_options, ACL_NO_MODE); 1039 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1040 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1041 &oparms, in_iov, cmds, num_cmds, 1042 cfile, out_iov, out_buftype, NULL); 1043 1044 hdr = out_iov[0].iov_base; 1045 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER) 1046 goto out; 1047 1048 if (!rc) 1049 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1050 break; 1051 case -EOPNOTSUPP: 1052 /* 1053 * BB TODO: When support for special files added to Samba 1054 * re-verify this path. 1055 */ 1056 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]); 1057 if (rc || !data->reparse_point) 1058 goto out; 1059 1060 /* 1061 * Skip SMB2_OP_GET_REPARSE if symlink already parsed in create 1062 * response. 1063 */ 1064 if (data->reparse.tag != IO_REPARSE_TAG_SYMLINK) { 1065 cmds[num_cmds++] = SMB2_OP_GET_REPARSE; 1066 if (!tcon->posix_extensions) 1067 cmds[num_cmds++] = SMB2_OP_QUERY_WSL_EA; 1068 } 1069 1070 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1071 FILE_READ_ATTRIBUTES | 1072 FILE_READ_EA | SYNCHRONIZE, 1073 FILE_OPEN, create_options | 1074 OPEN_REPARSE_POINT, ACL_NO_MODE); 1075 cifs_get_readable_path(tcon, full_path, &cfile); 1076 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1077 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, 1078 &oparms, in_iov, cmds, num_cmds, 1079 cfile, out_iov, out_buftype, NULL); 1080 if (rc && ea_unsupported(cmds, num_cmds, 1081 out_iov, out_buftype)) { 1082 if (data->reparse.tag != IO_REPARSE_TAG_LX_BLK && 1083 data->reparse.tag != IO_REPARSE_TAG_LX_CHR) 1084 rc = 0; 1085 else 1086 rc = -EOPNOTSUPP; 1087 } 1088 1089 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) { 1090 bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY; 1091 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb); 1092 } 1093 break; 1094 case -EREMOTE: 1095 break; 1096 default: 1097 if (hdr->Status != STATUS_OBJECT_NAME_INVALID) 1098 break; 1099 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb, 1100 full_path, &islink); 1101 if (rc2) { 1102 rc = rc2; 1103 goto out; 1104 } 1105 if (islink) 1106 rc = -EREMOTE; 1107 } 1108 1109 out: 1110 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov)); 1111 return rc; 1112 } 1113 1114 int 1115 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode, 1116 struct cifs_tcon *tcon, const char *name, 1117 struct cifs_sb_info *cifs_sb) 1118 { 1119 struct cifs_open_parms oparms; 1120 1121 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES, 1122 FILE_CREATE, CREATE_NOT_FILE, mode); 1123 return smb2_compound_op(xid, tcon, cifs_sb, 1124 name, &oparms, NULL, 1125 &(int){SMB2_OP_MKDIR}, 1, 1126 NULL, NULL, NULL, NULL); 1127 } 1128 1129 void 1130 smb2_mkdir_setinfo(struct inode *inode, const char *name, 1131 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon, 1132 const unsigned int xid) 1133 { 1134 struct cifs_open_parms oparms; 1135 FILE_BASIC_INFO data = {}; 1136 struct cifsInodeInfo *cifs_i; 1137 struct cifsFileInfo *cfile; 1138 struct kvec in_iov; 1139 u32 dosattrs; 1140 int tmprc; 1141 1142 in_iov.iov_base = &data; 1143 in_iov.iov_len = sizeof(data); 1144 cifs_i = CIFS_I(inode); 1145 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY; 1146 data.Attributes = cpu_to_le32(dosattrs); 1147 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile); 1148 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES, 1149 FILE_CREATE, CREATE_NOT_FILE, ACL_NO_MODE); 1150 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name, 1151 &oparms, &in_iov, 1152 &(int){SMB2_OP_SET_INFO}, 1, 1153 cfile, NULL, NULL, NULL); 1154 if (tmprc == 0) 1155 cifs_i->cifsAttrs = dosattrs; 1156 } 1157 1158 int 1159 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, 1160 struct cifs_sb_info *cifs_sb) 1161 { 1162 struct cifs_open_parms oparms; 1163 1164 drop_cached_dir_by_name(xid, tcon, name, cifs_sb); 1165 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE, 1166 FILE_OPEN, CREATE_NOT_FILE, ACL_NO_MODE); 1167 return smb2_compound_op(xid, tcon, cifs_sb, 1168 name, &oparms, NULL, 1169 &(int){SMB2_OP_RMDIR}, 1, 1170 NULL, NULL, NULL, NULL); 1171 } 1172 1173 int 1174 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, 1175 struct cifs_sb_info *cifs_sb, struct dentry *dentry) 1176 { 1177 struct cifs_open_parms oparms; 1178 1179 oparms = CIFS_OPARMS(cifs_sb, tcon, name, 1180 DELETE, FILE_OPEN, 1181 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT, 1182 ACL_NO_MODE); 1183 int rc = smb2_compound_op(xid, tcon, cifs_sb, name, &oparms, 1184 NULL, &(int){SMB2_OP_DELETE}, 1, 1185 NULL, NULL, NULL, dentry); 1186 if (rc == -EINVAL) { 1187 cifs_dbg(FYI, "invalid lease key, resending request without lease"); 1188 rc = smb2_compound_op(xid, tcon, cifs_sb, name, &oparms, 1189 NULL, &(int){SMB2_OP_DELETE}, 1, 1190 NULL, NULL, NULL, NULL); 1191 } 1192 return rc; 1193 } 1194 1195 static int smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon, 1196 const char *from_name, const char *to_name, 1197 struct cifs_sb_info *cifs_sb, 1198 __u32 create_options, __u32 access, 1199 int command, struct cifsFileInfo *cfile, 1200 struct dentry *dentry) 1201 { 1202 struct cifs_open_parms oparms; 1203 struct kvec in_iov; 1204 __le16 *smb2_to_name = NULL; 1205 int rc; 1206 1207 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb); 1208 if (smb2_to_name == NULL) { 1209 rc = -ENOMEM; 1210 goto smb2_rename_path; 1211 } 1212 in_iov.iov_base = smb2_to_name; 1213 in_iov.iov_len = 2 * UniStrnlen((wchar_t *)smb2_to_name, PATH_MAX); 1214 oparms = CIFS_OPARMS(cifs_sb, tcon, from_name, access, FILE_OPEN, 1215 create_options, ACL_NO_MODE); 1216 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, 1217 &oparms, &in_iov, &command, 1, 1218 cfile, NULL, NULL, dentry); 1219 smb2_rename_path: 1220 kfree(smb2_to_name); 1221 return rc; 1222 } 1223 1224 int smb2_rename_path(const unsigned int xid, 1225 struct cifs_tcon *tcon, 1226 struct dentry *source_dentry, 1227 const char *from_name, const char *to_name, 1228 struct cifs_sb_info *cifs_sb) 1229 { 1230 struct cifsFileInfo *cfile; 1231 __u32 co = file_create_options(source_dentry); 1232 1233 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb); 1234 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile); 1235 1236 int rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, 1237 co, DELETE, SMB2_OP_RENAME, cfile, source_dentry); 1238 if (rc == -EINVAL) { 1239 cifs_dbg(FYI, "invalid lease key, resending request without lease"); 1240 cifs_get_writable_path(tcon, from_name, 1241 FIND_WR_WITH_DELETE, &cfile); 1242 rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, 1243 co, DELETE, SMB2_OP_RENAME, cfile, NULL); 1244 } 1245 return rc; 1246 } 1247 1248 int smb2_create_hardlink(const unsigned int xid, 1249 struct cifs_tcon *tcon, 1250 struct dentry *source_dentry, 1251 const char *from_name, const char *to_name, 1252 struct cifs_sb_info *cifs_sb) 1253 { 1254 __u32 co = file_create_options(source_dentry); 1255 1256 return smb2_set_path_attr(xid, tcon, from_name, to_name, 1257 cifs_sb, co, FILE_READ_ATTRIBUTES, 1258 SMB2_OP_HARDLINK, NULL, NULL); 1259 } 1260 1261 int 1262 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon, 1263 const char *full_path, __u64 size, 1264 struct cifs_sb_info *cifs_sb, bool set_alloc, 1265 struct dentry *dentry) 1266 { 1267 struct cifs_open_parms oparms; 1268 struct cifsFileInfo *cfile; 1269 struct kvec in_iov; 1270 __le64 eof = cpu_to_le64(size); 1271 int rc; 1272 1273 in_iov.iov_base = &eof; 1274 in_iov.iov_len = sizeof(eof); 1275 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1276 1277 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_DATA, 1278 FILE_OPEN, 0, ACL_NO_MODE); 1279 rc = smb2_compound_op(xid, tcon, cifs_sb, 1280 full_path, &oparms, &in_iov, 1281 &(int){SMB2_OP_SET_EOF}, 1, 1282 cfile, NULL, NULL, dentry); 1283 if (rc == -EINVAL) { 1284 cifs_dbg(FYI, "invalid lease key, resending request without lease"); 1285 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1286 rc = smb2_compound_op(xid, tcon, cifs_sb, 1287 full_path, &oparms, &in_iov, 1288 &(int){SMB2_OP_SET_EOF}, 1, 1289 cfile, NULL, NULL, NULL); 1290 } 1291 return rc; 1292 } 1293 1294 int 1295 smb2_set_file_info(struct inode *inode, const char *full_path, 1296 FILE_BASIC_INFO *buf, const unsigned int xid) 1297 { 1298 struct cifs_open_parms oparms; 1299 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1300 struct tcon_link *tlink; 1301 struct cifs_tcon *tcon; 1302 struct cifsFileInfo *cfile; 1303 struct kvec in_iov = { .iov_base = buf, .iov_len = sizeof(*buf), }; 1304 int rc; 1305 1306 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) && 1307 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) && 1308 (buf->Attributes == 0)) 1309 return 0; /* would be a no op, no sense sending this */ 1310 1311 tlink = cifs_sb_tlink(cifs_sb); 1312 if (IS_ERR(tlink)) 1313 return PTR_ERR(tlink); 1314 tcon = tlink_tcon(tlink); 1315 1316 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1317 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_ATTRIBUTES, 1318 FILE_OPEN, 0, ACL_NO_MODE); 1319 rc = smb2_compound_op(xid, tcon, cifs_sb, 1320 full_path, &oparms, &in_iov, 1321 &(int){SMB2_OP_SET_INFO}, 1, 1322 cfile, NULL, NULL, NULL); 1323 cifs_put_tlink(tlink); 1324 return rc; 1325 } 1326 1327 struct inode *smb2_create_reparse_inode(struct cifs_open_info_data *data, 1328 struct super_block *sb, 1329 const unsigned int xid, 1330 struct cifs_tcon *tcon, 1331 const char *full_path, 1332 bool directory, 1333 struct kvec *reparse_iov, 1334 struct kvec *xattr_iov) 1335 { 1336 struct cifs_open_parms oparms; 1337 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 1338 struct cifsFileInfo *cfile; 1339 struct inode *new = NULL; 1340 int out_buftype[4] = {}; 1341 struct kvec out_iov[4] = {}; 1342 struct kvec in_iov[2]; 1343 int cmds[2]; 1344 int rc; 1345 int i; 1346 1347 /* 1348 * If server filesystem does not support reparse points then do not 1349 * attempt to create reparse point. This will prevent creating unusable 1350 * empty object on the server. 1351 */ 1352 if (!CIFS_REPARSE_SUPPORT(tcon)) 1353 return ERR_PTR(-EOPNOTSUPP); 1354 1355 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1356 SYNCHRONIZE | DELETE | 1357 FILE_READ_ATTRIBUTES | 1358 FILE_WRITE_ATTRIBUTES, 1359 FILE_CREATE, 1360 (directory ? CREATE_NOT_FILE : CREATE_NOT_DIR) | OPEN_REPARSE_POINT, 1361 ACL_NO_MODE); 1362 if (xattr_iov) 1363 oparms.ea_cctx = xattr_iov; 1364 1365 cmds[0] = SMB2_OP_SET_REPARSE; 1366 in_iov[0] = *reparse_iov; 1367 in_iov[1].iov_base = data; 1368 in_iov[1].iov_len = sizeof(*data); 1369 1370 if (tcon->posix_extensions) { 1371 cmds[1] = SMB2_OP_POSIX_QUERY_INFO; 1372 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1373 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, 1374 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL); 1375 if (!rc) { 1376 rc = smb311_posix_get_inode_info(&new, full_path, 1377 data, sb, xid); 1378 } 1379 } else { 1380 cmds[1] = SMB2_OP_QUERY_INFO; 1381 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile); 1382 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, 1383 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL); 1384 if (!rc) { 1385 rc = cifs_get_inode_info(&new, full_path, 1386 data, sb, xid, NULL); 1387 } 1388 } 1389 1390 1391 /* 1392 * If CREATE was successful but SMB2_OP_SET_REPARSE failed then 1393 * remove the intermediate object created by CREATE. Otherwise 1394 * empty object stay on the server when reparse call failed. 1395 */ 1396 if (rc && 1397 out_iov[0].iov_base != NULL && out_buftype[0] != CIFS_NO_BUFFER && 1398 ((struct smb2_hdr *)out_iov[0].iov_base)->Status == STATUS_SUCCESS && 1399 (out_iov[1].iov_base == NULL || out_buftype[1] == CIFS_NO_BUFFER || 1400 ((struct smb2_hdr *)out_iov[1].iov_base)->Status != STATUS_SUCCESS)) 1401 smb2_unlink(xid, tcon, full_path, cifs_sb, NULL); 1402 1403 for (i = 0; i < ARRAY_SIZE(out_buftype); i++) 1404 free_rsp_buf(out_buftype[i], out_iov[i].iov_base); 1405 1406 return rc ? ERR_PTR(rc) : new; 1407 } 1408 1409 int smb2_query_reparse_point(const unsigned int xid, 1410 struct cifs_tcon *tcon, 1411 struct cifs_sb_info *cifs_sb, 1412 const char *full_path, 1413 u32 *tag, struct kvec *rsp, 1414 int *rsp_buftype) 1415 { 1416 struct cifs_open_parms oparms; 1417 struct cifs_open_info_data data = {}; 1418 struct cifsFileInfo *cfile; 1419 struct kvec in_iov = { .iov_base = &data, .iov_len = sizeof(data), }; 1420 int rc; 1421 1422 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path); 1423 1424 cifs_get_readable_path(tcon, full_path, &cfile); 1425 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, 1426 FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE, 1427 FILE_OPEN, OPEN_REPARSE_POINT, ACL_NO_MODE); 1428 rc = smb2_compound_op(xid, tcon, cifs_sb, 1429 full_path, &oparms, &in_iov, 1430 &(int){SMB2_OP_GET_REPARSE}, 1, 1431 cfile, NULL, NULL, NULL); 1432 if (rc) 1433 goto out; 1434 1435 *tag = data.reparse.tag; 1436 *rsp = data.reparse.io.iov; 1437 *rsp_buftype = data.reparse.io.buftype; 1438 memset(&data.reparse.io.iov, 0, sizeof(data.reparse.io.iov)); 1439 data.reparse.io.buftype = CIFS_NO_BUFFER; 1440 out: 1441 cifs_free_open_info(&data); 1442 return rc; 1443 } 1444