1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/fs.h> 9 #include <linux/filelock.h> 10 #include <linux/uaccess.h> 11 #include <linux/backing-dev.h> 12 #include <linux/writeback.h> 13 #include <linux/xattr.h> 14 #include <linux/falloc.h> 15 #include <linux/fsnotify.h> 16 #include <linux/dcache.h> 17 #include <linux/slab.h> 18 #include <linux/vmalloc.h> 19 #include <linux/sched/xacct.h> 20 #include <linux/crc32c.h> 21 #include <linux/namei.h> 22 23 #include "glob.h" 24 #include "oplock.h" 25 #include "connection.h" 26 #include "vfs.h" 27 #include "vfs_cache.h" 28 #include "smbacl.h" 29 #include "ndr.h" 30 #include "auth.h" 31 #include "misc.h" 32 33 #include "smb_common.h" 34 #include "mgmt/share_config.h" 35 #include "mgmt/tree_connect.h" 36 #include "mgmt/user_session.h" 37 #include "mgmt/user_config.h" 38 39 static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work, 40 struct inode *parent_inode, 41 struct inode *inode) 42 { 43 if (!test_share_config_flag(work->tcon->share_conf, 44 KSMBD_SHARE_FLAG_INHERIT_OWNER)) 45 return; 46 47 i_uid_write(inode, i_uid_read(parent_inode)); 48 } 49 50 /** 51 * ksmbd_vfs_lock_parent() - lock parent dentry if it is stable 52 */ 53 int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child) 54 { 55 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT); 56 if (child->d_parent != parent) { 57 inode_unlock(d_inode(parent)); 58 return -ENOENT; 59 } 60 61 return 0; 62 } 63 64 static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf, 65 char *pathname, unsigned int flags, 66 struct path *parent_path, 67 struct path *path) 68 { 69 struct qstr last; 70 struct filename *filename; 71 struct path *root_share_path = &share_conf->vfs_path; 72 int err, type; 73 struct dentry *d; 74 75 if (pathname[0] == '\0') { 76 pathname = share_conf->path; 77 root_share_path = NULL; 78 } else { 79 flags |= LOOKUP_BENEATH; 80 } 81 82 filename = getname_kernel(pathname); 83 if (IS_ERR(filename)) 84 return PTR_ERR(filename); 85 86 err = vfs_path_parent_lookup(filename, flags, 87 parent_path, &last, &type, 88 root_share_path); 89 if (err) { 90 putname(filename); 91 return err; 92 } 93 94 if (unlikely(type != LAST_NORM)) { 95 path_put(parent_path); 96 putname(filename); 97 return -ENOENT; 98 } 99 100 err = mnt_want_write(parent_path->mnt); 101 if (err) { 102 path_put(parent_path); 103 putname(filename); 104 return -ENOENT; 105 } 106 107 inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT); 108 d = lookup_one_qstr_excl(&last, parent_path->dentry, 0); 109 if (IS_ERR(d)) 110 goto err_out; 111 112 if (d_is_negative(d)) { 113 dput(d); 114 goto err_out; 115 } 116 117 path->dentry = d; 118 path->mnt = mntget(parent_path->mnt); 119 120 if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) { 121 err = follow_down(path, 0); 122 if (err < 0) { 123 path_put(path); 124 goto err_out; 125 } 126 } 127 128 putname(filename); 129 return 0; 130 131 err_out: 132 inode_unlock(d_inode(parent_path->dentry)); 133 mnt_drop_write(parent_path->mnt); 134 path_put(parent_path); 135 putname(filename); 136 return -ENOENT; 137 } 138 139 void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap, 140 struct dentry *dentry, __le32 *daccess) 141 { 142 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL); 143 144 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE)) 145 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | 146 FILE_WRITE_DATA | FILE_APPEND_DATA | 147 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | 148 FILE_DELETE_CHILD); 149 150 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ)) 151 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE; 152 153 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC)) 154 *daccess |= FILE_EXECUTE_LE; 155 156 if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE)) 157 *daccess |= FILE_DELETE_LE; 158 } 159 160 /** 161 * ksmbd_vfs_create() - vfs helper for smb create file 162 * @work: work 163 * @name: file name that is relative to share 164 * @mode: file create mode 165 * 166 * Return: 0 on success, otherwise error 167 */ 168 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode) 169 { 170 struct path path; 171 struct dentry *dentry; 172 int err; 173 174 dentry = ksmbd_vfs_kern_path_create(work, name, 175 LOOKUP_NO_SYMLINKS, &path); 176 if (IS_ERR(dentry)) { 177 err = PTR_ERR(dentry); 178 if (err != -ENOENT) 179 pr_err("path create failed for %s, err %d\n", 180 name, err); 181 return err; 182 } 183 184 mode |= S_IFREG; 185 err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry), 186 dentry, mode, true); 187 if (!err) { 188 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), 189 d_inode(dentry)); 190 } else { 191 pr_err("File(%s): creation failed (err:%d)\n", name, err); 192 } 193 194 done_path_create(&path, dentry); 195 return err; 196 } 197 198 /** 199 * ksmbd_vfs_mkdir() - vfs helper for smb create directory 200 * @work: work 201 * @name: directory name that is relative to share 202 * @mode: directory create mode 203 * 204 * Return: 0 on success, otherwise error 205 */ 206 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode) 207 { 208 struct mnt_idmap *idmap; 209 struct path path; 210 struct dentry *dentry; 211 int err; 212 213 dentry = ksmbd_vfs_kern_path_create(work, name, 214 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY, 215 &path); 216 if (IS_ERR(dentry)) { 217 err = PTR_ERR(dentry); 218 if (err != -EEXIST) 219 ksmbd_debug(VFS, "path create failed for %s, err %d\n", 220 name, err); 221 return err; 222 } 223 224 idmap = mnt_idmap(path.mnt); 225 mode |= S_IFDIR; 226 err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode); 227 if (!err && d_unhashed(dentry)) { 228 struct dentry *d; 229 230 d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent, 231 dentry->d_name.len); 232 if (IS_ERR(d)) { 233 err = PTR_ERR(d); 234 goto out_err; 235 } 236 if (unlikely(d_is_negative(d))) { 237 dput(d); 238 err = -ENOENT; 239 goto out_err; 240 } 241 242 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d)); 243 dput(d); 244 } 245 246 out_err: 247 done_path_create(&path, dentry); 248 if (err) 249 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err); 250 return err; 251 } 252 253 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap, 254 struct dentry *dentry, char *attr_name, 255 int attr_name_len, char **attr_value) 256 { 257 char *name, *xattr_list = NULL; 258 ssize_t value_len = -ENOENT, xattr_list_len; 259 260 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 261 if (xattr_list_len <= 0) 262 goto out; 263 264 for (name = xattr_list; name - xattr_list < xattr_list_len; 265 name += strlen(name) + 1) { 266 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 267 if (strncasecmp(attr_name, name, attr_name_len)) 268 continue; 269 270 value_len = ksmbd_vfs_getxattr(idmap, 271 dentry, 272 name, 273 attr_value); 274 if (value_len < 0) 275 pr_err("failed to get xattr in file\n"); 276 break; 277 } 278 279 out: 280 kvfree(xattr_list); 281 return value_len; 282 } 283 284 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos, 285 size_t count) 286 { 287 ssize_t v_len; 288 char *stream_buf = NULL; 289 290 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n", 291 *pos, count); 292 293 v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp), 294 fp->filp->f_path.dentry, 295 fp->stream.name, 296 fp->stream.size, 297 &stream_buf); 298 if ((int)v_len <= 0) 299 return (int)v_len; 300 301 if (v_len <= *pos) { 302 count = -EINVAL; 303 goto free_buf; 304 } 305 306 if (v_len - *pos < count) 307 count = v_len - *pos; 308 309 memcpy(buf, &stream_buf[*pos], count); 310 311 free_buf: 312 kvfree(stream_buf); 313 return count; 314 } 315 316 /** 317 * check_lock_range() - vfs helper for smb byte range file locking 318 * @filp: the file to apply the lock to 319 * @start: lock start byte offset 320 * @end: lock end byte offset 321 * @type: byte range type read/write 322 * 323 * Return: 0 on success, otherwise error 324 */ 325 static int check_lock_range(struct file *filp, loff_t start, loff_t end, 326 unsigned char type) 327 { 328 struct file_lock *flock; 329 struct file_lock_context *ctx = locks_inode_context(file_inode(filp)); 330 int error = 0; 331 332 if (!ctx || list_empty_careful(&ctx->flc_posix)) 333 return 0; 334 335 spin_lock(&ctx->flc_lock); 336 list_for_each_entry(flock, &ctx->flc_posix, fl_list) { 337 /* check conflict locks */ 338 if (flock->fl_end >= start && end >= flock->fl_start) { 339 if (flock->fl_type == F_RDLCK) { 340 if (type == WRITE) { 341 pr_err("not allow write by shared lock\n"); 342 error = 1; 343 goto out; 344 } 345 } else if (flock->fl_type == F_WRLCK) { 346 /* check owner in lock */ 347 if (flock->fl_file != filp) { 348 error = 1; 349 pr_err("not allow rw access by exclusive lock from other opens\n"); 350 goto out; 351 } 352 } 353 } 354 } 355 out: 356 spin_unlock(&ctx->flc_lock); 357 return error; 358 } 359 360 /** 361 * ksmbd_vfs_read() - vfs helper for smb file read 362 * @work: smb work 363 * @fid: file id of open file 364 * @count: read byte count 365 * @pos: file pos 366 * @rbuf: read data buffer 367 * 368 * Return: number of read bytes on success, otherwise error 369 */ 370 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count, 371 loff_t *pos, char *rbuf) 372 { 373 struct file *filp = fp->filp; 374 ssize_t nbytes = 0; 375 struct inode *inode = file_inode(filp); 376 377 if (S_ISDIR(inode->i_mode)) 378 return -EISDIR; 379 380 if (unlikely(count == 0)) 381 return 0; 382 383 if (work->conn->connection_type) { 384 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 385 pr_err("no right to read(%pD)\n", fp->filp); 386 return -EACCES; 387 } 388 } 389 390 if (ksmbd_stream_fd(fp)) 391 return ksmbd_vfs_stream_read(fp, rbuf, pos, count); 392 393 if (!work->tcon->posix_extensions) { 394 int ret; 395 396 ret = check_lock_range(filp, *pos, *pos + count - 1, READ); 397 if (ret) { 398 pr_err("unable to read due to lock\n"); 399 return -EAGAIN; 400 } 401 } 402 403 nbytes = kernel_read(filp, rbuf, count, pos); 404 if (nbytes < 0) { 405 pr_err("smb read failed, err = %zd\n", nbytes); 406 return nbytes; 407 } 408 409 filp->f_pos = *pos; 410 return nbytes; 411 } 412 413 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, 414 size_t count) 415 { 416 char *stream_buf = NULL, *wbuf; 417 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 418 size_t size; 419 ssize_t v_len; 420 int err = 0; 421 422 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n", 423 *pos, count); 424 425 size = *pos + count; 426 if (size > XATTR_SIZE_MAX) { 427 size = XATTR_SIZE_MAX; 428 count = (*pos + count) - XATTR_SIZE_MAX; 429 } 430 431 v_len = ksmbd_vfs_getcasexattr(idmap, 432 fp->filp->f_path.dentry, 433 fp->stream.name, 434 fp->stream.size, 435 &stream_buf); 436 if (v_len < 0) { 437 pr_err("not found stream in xattr : %zd\n", v_len); 438 err = v_len; 439 goto out; 440 } 441 442 if (v_len < size) { 443 wbuf = kvzalloc(size, GFP_KERNEL); 444 if (!wbuf) { 445 err = -ENOMEM; 446 goto out; 447 } 448 449 if (v_len > 0) 450 memcpy(wbuf, stream_buf, v_len); 451 kvfree(stream_buf); 452 stream_buf = wbuf; 453 } 454 455 memcpy(&stream_buf[*pos], buf, count); 456 457 err = ksmbd_vfs_setxattr(idmap, 458 &fp->filp->f_path, 459 fp->stream.name, 460 (void *)stream_buf, 461 size, 462 0, 463 true); 464 if (err < 0) 465 goto out; 466 467 fp->filp->f_pos = *pos; 468 err = 0; 469 out: 470 kvfree(stream_buf); 471 return err; 472 } 473 474 /** 475 * ksmbd_vfs_write() - vfs helper for smb file write 476 * @work: work 477 * @fid: file id of open file 478 * @buf: buf containing data for writing 479 * @count: read byte count 480 * @pos: file pos 481 * @sync: fsync after write 482 * @written: number of bytes written 483 * 484 * Return: 0 on success, otherwise error 485 */ 486 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp, 487 char *buf, size_t count, loff_t *pos, bool sync, 488 ssize_t *written) 489 { 490 struct file *filp; 491 loff_t offset = *pos; 492 int err = 0; 493 494 if (work->conn->connection_type) { 495 if (!(fp->daccess & FILE_WRITE_DATA_LE)) { 496 pr_err("no right to write(%pD)\n", fp->filp); 497 err = -EACCES; 498 goto out; 499 } 500 } 501 502 filp = fp->filp; 503 504 if (ksmbd_stream_fd(fp)) { 505 err = ksmbd_vfs_stream_write(fp, buf, pos, count); 506 if (!err) 507 *written = count; 508 goto out; 509 } 510 511 if (!work->tcon->posix_extensions) { 512 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE); 513 if (err) { 514 pr_err("unable to write due to lock\n"); 515 err = -EAGAIN; 516 goto out; 517 } 518 } 519 520 /* Reserve lease break for parent dir at closing time */ 521 fp->reserve_lease_break = true; 522 523 /* Do we need to break any of a levelII oplock? */ 524 smb_break_all_levII_oplock(work, fp, 1); 525 526 err = kernel_write(filp, buf, count, pos); 527 if (err < 0) { 528 ksmbd_debug(VFS, "smb write failed, err = %d\n", err); 529 goto out; 530 } 531 532 filp->f_pos = *pos; 533 *written = err; 534 err = 0; 535 if (sync) { 536 err = vfs_fsync_range(filp, offset, offset + *written, 0); 537 if (err < 0) 538 pr_err("fsync failed for filename = %pD, err = %d\n", 539 fp->filp, err); 540 } 541 542 out: 543 return err; 544 } 545 546 /** 547 * ksmbd_vfs_getattr() - vfs helper for smb getattr 548 * @work: work 549 * @fid: file id of open file 550 * @attrs: inode attributes 551 * 552 * Return: 0 on success, otherwise error 553 */ 554 int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat) 555 { 556 int err; 557 558 err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT); 559 if (err) 560 pr_err("getattr failed, err %d\n", err); 561 return err; 562 } 563 564 /** 565 * ksmbd_vfs_fsync() - vfs helper for smb fsync 566 * @work: work 567 * @fid: file id of open file 568 * 569 * Return: 0 on success, otherwise error 570 */ 571 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id) 572 { 573 struct ksmbd_file *fp; 574 int err; 575 576 fp = ksmbd_lookup_fd_slow(work, fid, p_id); 577 if (!fp) { 578 pr_err("failed to get filp for fid %llu\n", fid); 579 return -ENOENT; 580 } 581 err = vfs_fsync(fp->filp, 0); 582 if (err < 0) 583 pr_err("smb fsync failed, err = %d\n", err); 584 ksmbd_fd_put(work, fp); 585 return err; 586 } 587 588 /** 589 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink 590 * @name: directory or file name that is relative to share 591 * 592 * Return: 0 on success, otherwise error 593 */ 594 int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path) 595 { 596 struct mnt_idmap *idmap; 597 struct dentry *parent = path->dentry->d_parent; 598 int err; 599 600 if (ksmbd_override_fsids(work)) 601 return -ENOMEM; 602 603 if (!d_inode(path->dentry)->i_nlink) { 604 err = -ENOENT; 605 goto out_err; 606 } 607 608 idmap = mnt_idmap(path->mnt); 609 if (S_ISDIR(d_inode(path->dentry)->i_mode)) { 610 err = vfs_rmdir(idmap, d_inode(parent), path->dentry); 611 if (err && err != -ENOTEMPTY) 612 ksmbd_debug(VFS, "rmdir failed, err %d\n", err); 613 } else { 614 err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL); 615 if (err) 616 ksmbd_debug(VFS, "unlink failed, err %d\n", err); 617 } 618 619 out_err: 620 ksmbd_revert_fsids(work); 621 return err; 622 } 623 624 /** 625 * ksmbd_vfs_link() - vfs helper for creating smb hardlink 626 * @oldname: source file name 627 * @newname: hardlink name that is relative to share 628 * 629 * Return: 0 on success, otherwise error 630 */ 631 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname, 632 const char *newname) 633 { 634 struct path oldpath, newpath; 635 struct dentry *dentry; 636 int err; 637 638 if (ksmbd_override_fsids(work)) 639 return -ENOMEM; 640 641 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath); 642 if (err) { 643 pr_err("cannot get linux path for %s, err = %d\n", 644 oldname, err); 645 goto out1; 646 } 647 648 dentry = ksmbd_vfs_kern_path_create(work, newname, 649 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL, 650 &newpath); 651 if (IS_ERR(dentry)) { 652 err = PTR_ERR(dentry); 653 pr_err("path create err for %s, err %d\n", newname, err); 654 goto out2; 655 } 656 657 err = -EXDEV; 658 if (oldpath.mnt != newpath.mnt) { 659 pr_err("vfs_link failed err %d\n", err); 660 goto out3; 661 } 662 663 err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt), 664 d_inode(newpath.dentry), 665 dentry, NULL); 666 if (err) 667 ksmbd_debug(VFS, "vfs_link failed err %d\n", err); 668 669 out3: 670 done_path_create(&newpath, dentry); 671 out2: 672 path_put(&oldpath); 673 out1: 674 ksmbd_revert_fsids(work); 675 return err; 676 } 677 678 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, 679 char *newname, int flags) 680 { 681 struct dentry *old_parent, *new_dentry, *trap; 682 struct dentry *old_child = old_path->dentry; 683 struct path new_path; 684 struct qstr new_last; 685 struct renamedata rd; 686 struct filename *to; 687 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 688 struct ksmbd_file *parent_fp; 689 int new_type; 690 int err, lookup_flags = LOOKUP_NO_SYMLINKS; 691 692 if (ksmbd_override_fsids(work)) 693 return -ENOMEM; 694 695 to = getname_kernel(newname); 696 if (IS_ERR(to)) { 697 err = PTR_ERR(to); 698 goto revert_fsids; 699 } 700 701 retry: 702 err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH, 703 &new_path, &new_last, &new_type, 704 &share_conf->vfs_path); 705 if (err) 706 goto out1; 707 708 if (old_path->mnt != new_path.mnt) { 709 err = -EXDEV; 710 goto out2; 711 } 712 713 err = mnt_want_write(old_path->mnt); 714 if (err) 715 goto out2; 716 717 trap = lock_rename_child(old_child, new_path.dentry); 718 719 old_parent = dget(old_child->d_parent); 720 if (d_unhashed(old_child)) { 721 err = -EINVAL; 722 goto out3; 723 } 724 725 parent_fp = ksmbd_lookup_fd_inode(old_child->d_parent); 726 if (parent_fp) { 727 if (parent_fp->daccess & FILE_DELETE_LE) { 728 pr_err("parent dir is opened with delete access\n"); 729 err = -ESHARE; 730 ksmbd_fd_put(work, parent_fp); 731 goto out3; 732 } 733 ksmbd_fd_put(work, parent_fp); 734 } 735 736 new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry, 737 lookup_flags | LOOKUP_RENAME_TARGET); 738 if (IS_ERR(new_dentry)) { 739 err = PTR_ERR(new_dentry); 740 goto out3; 741 } 742 743 if (d_is_symlink(new_dentry)) { 744 err = -EACCES; 745 goto out4; 746 } 747 748 /* 749 * explicitly handle file overwrite case, for compatibility with 750 * filesystems that may not support rename flags (e.g: fuse) 751 */ 752 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) { 753 err = -EEXIST; 754 goto out4; 755 } 756 flags &= ~(RENAME_NOREPLACE); 757 758 if (old_child == trap) { 759 err = -EINVAL; 760 goto out4; 761 } 762 763 if (new_dentry == trap) { 764 err = -ENOTEMPTY; 765 goto out4; 766 } 767 768 rd.old_mnt_idmap = mnt_idmap(old_path->mnt), 769 rd.old_dir = d_inode(old_parent), 770 rd.old_dentry = old_child, 771 rd.new_mnt_idmap = mnt_idmap(new_path.mnt), 772 rd.new_dir = new_path.dentry->d_inode, 773 rd.new_dentry = new_dentry, 774 rd.flags = flags, 775 rd.delegated_inode = NULL, 776 err = vfs_rename(&rd); 777 if (err) 778 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err); 779 780 out4: 781 dput(new_dentry); 782 out3: 783 dput(old_parent); 784 unlock_rename(old_parent, new_path.dentry); 785 mnt_drop_write(old_path->mnt); 786 out2: 787 path_put(&new_path); 788 789 if (retry_estale(err, lookup_flags)) { 790 lookup_flags |= LOOKUP_REVAL; 791 goto retry; 792 } 793 out1: 794 putname(to); 795 revert_fsids: 796 ksmbd_revert_fsids(work); 797 return err; 798 } 799 800 /** 801 * ksmbd_vfs_truncate() - vfs helper for smb file truncate 802 * @work: work 803 * @fid: file id of old file 804 * @size: truncate to given size 805 * 806 * Return: 0 on success, otherwise error 807 */ 808 int ksmbd_vfs_truncate(struct ksmbd_work *work, 809 struct ksmbd_file *fp, loff_t size) 810 { 811 int err = 0; 812 struct file *filp; 813 814 filp = fp->filp; 815 816 /* Do we need to break any of a levelII oplock? */ 817 smb_break_all_levII_oplock(work, fp, 1); 818 819 if (!work->tcon->posix_extensions) { 820 struct inode *inode = file_inode(filp); 821 822 if (size < inode->i_size) { 823 err = check_lock_range(filp, size, 824 inode->i_size - 1, WRITE); 825 } else { 826 err = check_lock_range(filp, inode->i_size, 827 size - 1, WRITE); 828 } 829 830 if (err) { 831 pr_err("failed due to lock\n"); 832 return -EAGAIN; 833 } 834 } 835 836 err = vfs_truncate(&filp->f_path, size); 837 if (err) 838 pr_err("truncate failed, err %d\n", err); 839 return err; 840 } 841 842 /** 843 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes 844 * @dentry: dentry of file for listing xattrs 845 * @list: destination buffer 846 * @size: destination buffer length 847 * 848 * Return: xattr list length on success, otherwise error 849 */ 850 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list) 851 { 852 ssize_t size; 853 char *vlist = NULL; 854 855 size = vfs_listxattr(dentry, NULL, 0); 856 if (size <= 0) 857 return size; 858 859 vlist = kvzalloc(size, GFP_KERNEL); 860 if (!vlist) 861 return -ENOMEM; 862 863 *list = vlist; 864 size = vfs_listxattr(dentry, vlist, size); 865 if (size < 0) { 866 ksmbd_debug(VFS, "listxattr failed\n"); 867 kvfree(vlist); 868 *list = NULL; 869 } 870 871 return size; 872 } 873 874 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap, 875 struct dentry *dentry, char *xattr_name) 876 { 877 return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0); 878 } 879 880 /** 881 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value 882 * @idmap: idmap 883 * @dentry: dentry of file for getting xattrs 884 * @xattr_name: name of xattr name to query 885 * @xattr_buf: destination buffer xattr value 886 * 887 * Return: read xattr value length on success, otherwise error 888 */ 889 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap, 890 struct dentry *dentry, 891 char *xattr_name, char **xattr_buf) 892 { 893 ssize_t xattr_len; 894 char *buf; 895 896 *xattr_buf = NULL; 897 xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name); 898 if (xattr_len < 0) 899 return xattr_len; 900 901 buf = kmalloc(xattr_len + 1, GFP_KERNEL); 902 if (!buf) 903 return -ENOMEM; 904 905 xattr_len = vfs_getxattr(idmap, dentry, xattr_name, 906 (void *)buf, xattr_len); 907 if (xattr_len > 0) 908 *xattr_buf = buf; 909 else 910 kfree(buf); 911 return xattr_len; 912 } 913 914 /** 915 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value 916 * @idmap: idmap of the relevant mount 917 * @path: path of dentry to set XATTR at 918 * @attr_name: xattr name for setxattr 919 * @attr_value: xattr value to set 920 * @attr_size: size of xattr value 921 * @flags: destination buffer length 922 * @get_write: get write access to a mount 923 * 924 * Return: 0 on success, otherwise error 925 */ 926 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap, 927 const struct path *path, const char *attr_name, 928 void *attr_value, size_t attr_size, int flags, 929 bool get_write) 930 { 931 int err; 932 933 if (get_write == true) { 934 err = mnt_want_write(path->mnt); 935 if (err) 936 return err; 937 } 938 939 err = vfs_setxattr(idmap, 940 path->dentry, 941 attr_name, 942 attr_value, 943 attr_size, 944 flags); 945 if (err) 946 ksmbd_debug(VFS, "setxattr failed, err %d\n", err); 947 if (get_write == true) 948 mnt_drop_write(path->mnt); 949 return err; 950 } 951 952 /** 953 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options 954 * @filp: file pointer for IO 955 * @options: smb IO options 956 */ 957 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option) 958 { 959 struct address_space *mapping; 960 961 mapping = filp->f_mapping; 962 963 if (!option || !mapping) 964 return; 965 966 if (option & FILE_WRITE_THROUGH_LE) { 967 filp->f_flags |= O_SYNC; 968 } else if (option & FILE_SEQUENTIAL_ONLY_LE) { 969 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2; 970 spin_lock(&filp->f_lock); 971 filp->f_mode &= ~FMODE_RANDOM; 972 spin_unlock(&filp->f_lock); 973 } else if (option & FILE_RANDOM_ACCESS_LE) { 974 spin_lock(&filp->f_lock); 975 filp->f_mode |= FMODE_RANDOM; 976 spin_unlock(&filp->f_lock); 977 } 978 } 979 980 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp, 981 loff_t off, loff_t len) 982 { 983 smb_break_all_levII_oplock(work, fp, 1); 984 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE) 985 return vfs_fallocate(fp->filp, 986 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 987 off, len); 988 989 return vfs_fallocate(fp->filp, 990 FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, 991 off, len); 992 } 993 994 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length, 995 struct file_allocated_range_buffer *ranges, 996 unsigned int in_count, unsigned int *out_count) 997 { 998 struct file *f = fp->filp; 999 struct inode *inode = file_inode(fp->filp); 1000 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end; 1001 loff_t extent_start, extent_end; 1002 int ret = 0; 1003 1004 if (start > maxbytes) 1005 return -EFBIG; 1006 1007 if (!in_count) 1008 return 0; 1009 1010 /* 1011 * Shrink request scope to what the fs can actually handle. 1012 */ 1013 if (length > maxbytes || (maxbytes - length) < start) 1014 length = maxbytes - start; 1015 1016 if (start + length > inode->i_size) 1017 length = inode->i_size - start; 1018 1019 *out_count = 0; 1020 end = start + length; 1021 while (start < end && *out_count < in_count) { 1022 extent_start = vfs_llseek(f, start, SEEK_DATA); 1023 if (extent_start < 0) { 1024 if (extent_start != -ENXIO) 1025 ret = (int)extent_start; 1026 break; 1027 } 1028 1029 if (extent_start >= end) 1030 break; 1031 1032 extent_end = vfs_llseek(f, extent_start, SEEK_HOLE); 1033 if (extent_end < 0) { 1034 if (extent_end != -ENXIO) 1035 ret = (int)extent_end; 1036 break; 1037 } else if (extent_start >= extent_end) { 1038 break; 1039 } 1040 1041 ranges[*out_count].file_offset = cpu_to_le64(extent_start); 1042 ranges[(*out_count)++].length = 1043 cpu_to_le64(min(extent_end, end) - extent_start); 1044 1045 start = extent_end; 1046 } 1047 1048 return ret; 1049 } 1050 1051 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap, 1052 const struct path *path, char *attr_name) 1053 { 1054 int err; 1055 1056 err = mnt_want_write(path->mnt); 1057 if (err) 1058 return err; 1059 1060 err = vfs_removexattr(idmap, path->dentry, attr_name); 1061 mnt_drop_write(path->mnt); 1062 1063 return err; 1064 } 1065 1066 int ksmbd_vfs_unlink(struct file *filp) 1067 { 1068 int err = 0; 1069 struct dentry *dir, *dentry = filp->f_path.dentry; 1070 struct mnt_idmap *idmap = file_mnt_idmap(filp); 1071 1072 err = mnt_want_write(filp->f_path.mnt); 1073 if (err) 1074 return err; 1075 1076 dir = dget_parent(dentry); 1077 err = ksmbd_vfs_lock_parent(dir, dentry); 1078 if (err) 1079 goto out; 1080 dget(dentry); 1081 1082 if (S_ISDIR(d_inode(dentry)->i_mode)) 1083 err = vfs_rmdir(idmap, d_inode(dir), dentry); 1084 else 1085 err = vfs_unlink(idmap, d_inode(dir), dentry, NULL); 1086 1087 dput(dentry); 1088 inode_unlock(d_inode(dir)); 1089 if (err) 1090 ksmbd_debug(VFS, "failed to delete, err %d\n", err); 1091 out: 1092 dput(dir); 1093 mnt_drop_write(filp->f_path.mnt); 1094 1095 return err; 1096 } 1097 1098 static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen, 1099 loff_t offset, u64 ino, unsigned int d_type) 1100 { 1101 struct ksmbd_readdir_data *buf; 1102 1103 buf = container_of(ctx, struct ksmbd_readdir_data, ctx); 1104 buf->dirent_count++; 1105 1106 return buf->dirent_count <= 2; 1107 } 1108 1109 /** 1110 * ksmbd_vfs_empty_dir() - check for empty directory 1111 * @fp: ksmbd file pointer 1112 * 1113 * Return: true if directory empty, otherwise false 1114 */ 1115 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp) 1116 { 1117 int err; 1118 struct ksmbd_readdir_data readdir_data; 1119 1120 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data)); 1121 1122 set_ctx_actor(&readdir_data.ctx, __dir_empty); 1123 readdir_data.dirent_count = 0; 1124 1125 err = iterate_dir(fp->filp, &readdir_data.ctx); 1126 if (readdir_data.dirent_count > 2) 1127 err = -ENOTEMPTY; 1128 else 1129 err = 0; 1130 return err; 1131 } 1132 1133 static bool __caseless_lookup(struct dir_context *ctx, const char *name, 1134 int namlen, loff_t offset, u64 ino, 1135 unsigned int d_type) 1136 { 1137 struct ksmbd_readdir_data *buf; 1138 int cmp = -EINVAL; 1139 1140 buf = container_of(ctx, struct ksmbd_readdir_data, ctx); 1141 1142 if (buf->used != namlen) 1143 return true; 1144 if (IS_ENABLED(CONFIG_UNICODE) && buf->um) { 1145 const struct qstr q_buf = {.name = buf->private, 1146 .len = buf->used}; 1147 const struct qstr q_name = {.name = name, 1148 .len = namlen}; 1149 1150 cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name); 1151 } 1152 if (cmp < 0) 1153 cmp = strncasecmp((char *)buf->private, name, namlen); 1154 if (!cmp) { 1155 memcpy((char *)buf->private, name, namlen); 1156 buf->dirent_count = 1; 1157 return false; 1158 } 1159 return true; 1160 } 1161 1162 /** 1163 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory 1164 * @dir: path info 1165 * @name: filename to lookup 1166 * @namelen: filename length 1167 * 1168 * Return: 0 on success, otherwise error 1169 */ 1170 static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name, 1171 size_t namelen, struct unicode_map *um) 1172 { 1173 int ret; 1174 struct file *dfilp; 1175 int flags = O_RDONLY | O_LARGEFILE; 1176 struct ksmbd_readdir_data readdir_data = { 1177 .ctx.actor = __caseless_lookup, 1178 .private = name, 1179 .used = namelen, 1180 .dirent_count = 0, 1181 .um = um, 1182 }; 1183 1184 dfilp = dentry_open(dir, flags, current_cred()); 1185 if (IS_ERR(dfilp)) 1186 return PTR_ERR(dfilp); 1187 1188 ret = iterate_dir(dfilp, &readdir_data.ctx); 1189 if (readdir_data.dirent_count > 0) 1190 ret = 0; 1191 fput(dfilp); 1192 return ret; 1193 } 1194 1195 /** 1196 * ksmbd_vfs_kern_path_locked() - lookup a file and get path info 1197 * @name: file path that is relative to share 1198 * @flags: lookup flags 1199 * @parent_path: if lookup succeed, return parent_path info 1200 * @path: if lookup succeed, return path info 1201 * @caseless: caseless filename lookup 1202 * 1203 * Return: 0 on success, otherwise error 1204 */ 1205 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name, 1206 unsigned int flags, struct path *parent_path, 1207 struct path *path, bool caseless) 1208 { 1209 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 1210 int err; 1211 1212 err = ksmbd_vfs_path_lookup_locked(share_conf, name, flags, parent_path, 1213 path); 1214 if (!err) 1215 return 0; 1216 1217 if (caseless) { 1218 char *filepath; 1219 size_t path_len, remain_len; 1220 1221 filepath = kstrdup(name, GFP_KERNEL); 1222 if (!filepath) 1223 return -ENOMEM; 1224 1225 path_len = strlen(filepath); 1226 remain_len = path_len; 1227 1228 *parent_path = share_conf->vfs_path; 1229 path_get(parent_path); 1230 1231 while (d_can_lookup(parent_path->dentry)) { 1232 char *filename = filepath + path_len - remain_len; 1233 char *next = strchrnul(filename, '/'); 1234 size_t filename_len = next - filename; 1235 bool is_last = !next[0]; 1236 1237 if (filename_len == 0) 1238 break; 1239 1240 err = ksmbd_vfs_lookup_in_dir(parent_path, filename, 1241 filename_len, 1242 work->conn->um); 1243 if (err) 1244 goto out2; 1245 1246 next[0] = '\0'; 1247 1248 err = vfs_path_lookup(share_conf->vfs_path.dentry, 1249 share_conf->vfs_path.mnt, 1250 filepath, 1251 flags, 1252 path); 1253 if (err) 1254 goto out2; 1255 else if (is_last) 1256 goto out1; 1257 path_put(parent_path); 1258 *parent_path = *path; 1259 1260 next[0] = '/'; 1261 remain_len -= filename_len + 1; 1262 } 1263 1264 err = -EINVAL; 1265 out2: 1266 path_put(parent_path); 1267 out1: 1268 kfree(filepath); 1269 } 1270 1271 if (!err) { 1272 err = mnt_want_write(parent_path->mnt); 1273 if (err) { 1274 path_put(path); 1275 path_put(parent_path); 1276 return err; 1277 } 1278 1279 err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry); 1280 if (err) { 1281 path_put(path); 1282 path_put(parent_path); 1283 } 1284 } 1285 return err; 1286 } 1287 1288 void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path) 1289 { 1290 inode_unlock(d_inode(parent_path->dentry)); 1291 mnt_drop_write(parent_path->mnt); 1292 path_put(path); 1293 path_put(parent_path); 1294 } 1295 1296 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work, 1297 const char *name, 1298 unsigned int flags, 1299 struct path *path) 1300 { 1301 char *abs_name; 1302 struct dentry *dent; 1303 1304 abs_name = convert_to_unix_name(work->tcon->share_conf, name); 1305 if (!abs_name) 1306 return ERR_PTR(-ENOMEM); 1307 1308 dent = kern_path_create(AT_FDCWD, abs_name, path, flags); 1309 kfree(abs_name); 1310 return dent; 1311 } 1312 1313 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap, 1314 const struct path *path) 1315 { 1316 char *name, *xattr_list = NULL; 1317 ssize_t xattr_list_len; 1318 int err = 0; 1319 1320 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1321 if (xattr_list_len < 0) { 1322 goto out; 1323 } else if (!xattr_list_len) { 1324 ksmbd_debug(SMB, "empty xattr in the file\n"); 1325 goto out; 1326 } 1327 1328 err = mnt_want_write(path->mnt); 1329 if (err) 1330 goto out; 1331 1332 for (name = xattr_list; name - xattr_list < xattr_list_len; 1333 name += strlen(name) + 1) { 1334 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1335 1336 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, 1337 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) || 1338 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, 1339 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) { 1340 err = vfs_remove_acl(idmap, path->dentry, name); 1341 if (err) 1342 ksmbd_debug(SMB, 1343 "remove acl xattr failed : %s\n", name); 1344 } 1345 } 1346 mnt_drop_write(path->mnt); 1347 1348 out: 1349 kvfree(xattr_list); 1350 return err; 1351 } 1352 1353 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap, const struct path *path) 1354 { 1355 char *name, *xattr_list = NULL; 1356 ssize_t xattr_list_len; 1357 int err = 0; 1358 1359 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1360 if (xattr_list_len < 0) { 1361 goto out; 1362 } else if (!xattr_list_len) { 1363 ksmbd_debug(SMB, "empty xattr in the file\n"); 1364 goto out; 1365 } 1366 1367 for (name = xattr_list; name - xattr_list < xattr_list_len; 1368 name += strlen(name) + 1) { 1369 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1370 1371 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) { 1372 err = ksmbd_vfs_remove_xattr(idmap, path, name); 1373 if (err) 1374 ksmbd_debug(SMB, "remove xattr failed : %s\n", name); 1375 } 1376 } 1377 out: 1378 kvfree(xattr_list); 1379 return err; 1380 } 1381 1382 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap, 1383 struct inode *inode, 1384 int acl_type) 1385 { 1386 struct xattr_smb_acl *smb_acl = NULL; 1387 struct posix_acl *posix_acls; 1388 struct posix_acl_entry *pa_entry; 1389 struct xattr_acl_entry *xa_entry; 1390 int i; 1391 1392 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1393 return NULL; 1394 1395 posix_acls = get_inode_acl(inode, acl_type); 1396 if (IS_ERR_OR_NULL(posix_acls)) 1397 return NULL; 1398 1399 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) + 1400 sizeof(struct xattr_acl_entry) * posix_acls->a_count, 1401 GFP_KERNEL); 1402 if (!smb_acl) 1403 goto out; 1404 1405 smb_acl->count = posix_acls->a_count; 1406 pa_entry = posix_acls->a_entries; 1407 xa_entry = smb_acl->entries; 1408 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) { 1409 switch (pa_entry->e_tag) { 1410 case ACL_USER: 1411 xa_entry->type = SMB_ACL_USER; 1412 xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry); 1413 break; 1414 case ACL_USER_OBJ: 1415 xa_entry->type = SMB_ACL_USER_OBJ; 1416 break; 1417 case ACL_GROUP: 1418 xa_entry->type = SMB_ACL_GROUP; 1419 xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry); 1420 break; 1421 case ACL_GROUP_OBJ: 1422 xa_entry->type = SMB_ACL_GROUP_OBJ; 1423 break; 1424 case ACL_OTHER: 1425 xa_entry->type = SMB_ACL_OTHER; 1426 break; 1427 case ACL_MASK: 1428 xa_entry->type = SMB_ACL_MASK; 1429 break; 1430 default: 1431 pr_err("unknown type : 0x%x\n", pa_entry->e_tag); 1432 goto out; 1433 } 1434 1435 if (pa_entry->e_perm & ACL_READ) 1436 xa_entry->perm |= SMB_ACL_READ; 1437 if (pa_entry->e_perm & ACL_WRITE) 1438 xa_entry->perm |= SMB_ACL_WRITE; 1439 if (pa_entry->e_perm & ACL_EXECUTE) 1440 xa_entry->perm |= SMB_ACL_EXECUTE; 1441 } 1442 out: 1443 posix_acl_release(posix_acls); 1444 return smb_acl; 1445 } 1446 1447 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn, 1448 struct mnt_idmap *idmap, 1449 const struct path *path, 1450 struct smb_ntsd *pntsd, int len, 1451 bool get_write) 1452 { 1453 int rc; 1454 struct ndr sd_ndr = {0}, acl_ndr = {0}; 1455 struct xattr_ntacl acl = {0}; 1456 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL; 1457 struct dentry *dentry = path->dentry; 1458 struct inode *inode = d_inode(dentry); 1459 1460 acl.version = 4; 1461 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256; 1462 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode)); 1463 1464 memcpy(acl.desc, "posix_acl", 9); 1465 acl.desc_len = 10; 1466 1467 pntsd->osidoffset = 1468 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF); 1469 pntsd->gsidoffset = 1470 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF); 1471 pntsd->dacloffset = 1472 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF); 1473 1474 acl.sd_buf = (char *)pntsd; 1475 acl.sd_size = len; 1476 1477 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash); 1478 if (rc) { 1479 pr_err("failed to generate hash for ndr acl\n"); 1480 return rc; 1481 } 1482 1483 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1484 ACL_TYPE_ACCESS); 1485 if (S_ISDIR(inode->i_mode)) 1486 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1487 ACL_TYPE_DEFAULT); 1488 1489 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, 1490 smb_acl, def_smb_acl); 1491 if (rc) { 1492 pr_err("failed to encode ndr to posix acl\n"); 1493 goto out; 1494 } 1495 1496 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, 1497 acl.posix_acl_hash); 1498 if (rc) { 1499 pr_err("failed to generate hash for ndr acl\n"); 1500 goto out; 1501 } 1502 1503 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl); 1504 if (rc) { 1505 pr_err("failed to encode ndr to posix acl\n"); 1506 goto out; 1507 } 1508 1509 rc = ksmbd_vfs_setxattr(idmap, path, 1510 XATTR_NAME_SD, sd_ndr.data, 1511 sd_ndr.offset, 0, get_write); 1512 if (rc < 0) 1513 pr_err("Failed to store XATTR ntacl :%d\n", rc); 1514 1515 kfree(sd_ndr.data); 1516 out: 1517 kfree(acl_ndr.data); 1518 kfree(smb_acl); 1519 kfree(def_smb_acl); 1520 return rc; 1521 } 1522 1523 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, 1524 struct mnt_idmap *idmap, 1525 struct dentry *dentry, 1526 struct smb_ntsd **pntsd) 1527 { 1528 int rc; 1529 struct ndr n; 1530 struct inode *inode = d_inode(dentry); 1531 struct ndr acl_ndr = {0}; 1532 struct xattr_ntacl acl; 1533 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL; 1534 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0}; 1535 1536 rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data); 1537 if (rc <= 0) 1538 return rc; 1539 1540 n.length = rc; 1541 rc = ndr_decode_v4_ntacl(&n, &acl); 1542 if (rc) 1543 goto free_n_data; 1544 1545 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1546 ACL_TYPE_ACCESS); 1547 if (S_ISDIR(inode->i_mode)) 1548 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1549 ACL_TYPE_DEFAULT); 1550 1551 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl, 1552 def_smb_acl); 1553 if (rc) { 1554 pr_err("failed to encode ndr to posix acl\n"); 1555 goto out_free; 1556 } 1557 1558 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash); 1559 if (rc) { 1560 pr_err("failed to generate hash for ndr acl\n"); 1561 goto out_free; 1562 } 1563 1564 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) { 1565 pr_err("hash value diff\n"); 1566 rc = -EINVAL; 1567 goto out_free; 1568 } 1569 1570 *pntsd = acl.sd_buf; 1571 if (acl.sd_size < sizeof(struct smb_ntsd)) { 1572 pr_err("sd size is invalid\n"); 1573 goto out_free; 1574 } 1575 1576 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) - 1577 NDR_NTSD_OFFSETOF); 1578 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) - 1579 NDR_NTSD_OFFSETOF); 1580 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) - 1581 NDR_NTSD_OFFSETOF); 1582 1583 rc = acl.sd_size; 1584 out_free: 1585 kfree(acl_ndr.data); 1586 kfree(smb_acl); 1587 kfree(def_smb_acl); 1588 if (rc < 0) { 1589 kfree(acl.sd_buf); 1590 *pntsd = NULL; 1591 } 1592 1593 free_n_data: 1594 kfree(n.data); 1595 return rc; 1596 } 1597 1598 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap, 1599 const struct path *path, 1600 struct xattr_dos_attrib *da, 1601 bool get_write) 1602 { 1603 struct ndr n; 1604 int err; 1605 1606 err = ndr_encode_dos_attr(&n, da); 1607 if (err) 1608 return err; 1609 1610 err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE, 1611 (void *)n.data, n.offset, 0, get_write); 1612 if (err) 1613 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n"); 1614 kfree(n.data); 1615 1616 return err; 1617 } 1618 1619 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap, 1620 struct dentry *dentry, 1621 struct xattr_dos_attrib *da) 1622 { 1623 struct ndr n; 1624 int err; 1625 1626 err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE, 1627 (char **)&n.data); 1628 if (err > 0) { 1629 n.length = err; 1630 if (ndr_decode_dos_attr(&n, da)) 1631 err = -EINVAL; 1632 kfree(n.data); 1633 } else { 1634 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n"); 1635 } 1636 1637 return err; 1638 } 1639 1640 /** 1641 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format 1642 * @p: destination buffer 1643 * @ksmbd_kstat: ksmbd kstat wrapper 1644 */ 1645 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat) 1646 { 1647 struct file_directory_info *info = (struct file_directory_info *)(*p); 1648 struct kstat *kstat = ksmbd_kstat->kstat; 1649 u64 time; 1650 1651 info->FileIndex = 0; 1652 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); 1653 time = ksmbd_UnixTimeToNT(kstat->atime); 1654 info->LastAccessTime = cpu_to_le64(time); 1655 time = ksmbd_UnixTimeToNT(kstat->mtime); 1656 info->LastWriteTime = cpu_to_le64(time); 1657 time = ksmbd_UnixTimeToNT(kstat->ctime); 1658 info->ChangeTime = cpu_to_le64(time); 1659 1660 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { 1661 info->EndOfFile = 0; 1662 info->AllocationSize = 0; 1663 } else { 1664 info->EndOfFile = cpu_to_le64(kstat->size); 1665 info->AllocationSize = cpu_to_le64(kstat->blocks << 9); 1666 } 1667 info->ExtFileAttributes = ksmbd_kstat->file_attributes; 1668 1669 return info; 1670 } 1671 1672 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, 1673 struct mnt_idmap *idmap, 1674 struct dentry *dentry, 1675 struct ksmbd_kstat *ksmbd_kstat) 1676 { 1677 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 1678 u64 time; 1679 int rc; 1680 struct path path = { 1681 .mnt = share_conf->vfs_path.mnt, 1682 .dentry = dentry, 1683 }; 1684 1685 rc = vfs_getattr(&path, ksmbd_kstat->kstat, 1686 STATX_BASIC_STATS | STATX_BTIME, 1687 AT_STATX_SYNC_AS_STAT); 1688 if (rc) 1689 return rc; 1690 1691 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); 1692 ksmbd_kstat->create_time = time; 1693 1694 /* 1695 * set default value for the case that store dos attributes is not yes 1696 * or that acl is disable in server's filesystem and the config is yes. 1697 */ 1698 if (S_ISDIR(ksmbd_kstat->kstat->mode)) 1699 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE; 1700 else 1701 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE; 1702 1703 if (test_share_config_flag(work->tcon->share_conf, 1704 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { 1705 struct xattr_dos_attrib da; 1706 1707 rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da); 1708 if (rc > 0) { 1709 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr); 1710 ksmbd_kstat->create_time = da.create_time; 1711 } else { 1712 ksmbd_debug(VFS, "fail to load dos attribute.\n"); 1713 } 1714 } 1715 1716 return 0; 1717 } 1718 1719 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap, 1720 struct dentry *dentry, char *attr_name, 1721 int attr_name_len) 1722 { 1723 char *name, *xattr_list = NULL; 1724 ssize_t value_len = -ENOENT, xattr_list_len; 1725 1726 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 1727 if (xattr_list_len <= 0) 1728 goto out; 1729 1730 for (name = xattr_list; name - xattr_list < xattr_list_len; 1731 name += strlen(name) + 1) { 1732 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 1733 if (strncasecmp(attr_name, name, attr_name_len)) 1734 continue; 1735 1736 value_len = ksmbd_vfs_xattr_len(idmap, dentry, name); 1737 break; 1738 } 1739 1740 out: 1741 kvfree(xattr_list); 1742 return value_len; 1743 } 1744 1745 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name, 1746 size_t *xattr_stream_name_size, int s_type) 1747 { 1748 char *type, *buf; 1749 1750 if (s_type == DIR_STREAM) 1751 type = ":$INDEX_ALLOCATION"; 1752 else 1753 type = ":$DATA"; 1754 1755 buf = kasprintf(GFP_KERNEL, "%s%s%s", 1756 XATTR_NAME_STREAM, stream_name, type); 1757 if (!buf) 1758 return -ENOMEM; 1759 1760 *xattr_stream_name = buf; 1761 *xattr_stream_name_size = strlen(buf) + 1; 1762 1763 return 0; 1764 } 1765 1766 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work, 1767 struct ksmbd_file *src_fp, 1768 struct ksmbd_file *dst_fp, 1769 struct srv_copychunk *chunks, 1770 unsigned int chunk_count, 1771 unsigned int *chunk_count_written, 1772 unsigned int *chunk_size_written, 1773 loff_t *total_size_written) 1774 { 1775 unsigned int i; 1776 loff_t src_off, dst_off, src_file_size; 1777 size_t len; 1778 int ret; 1779 1780 *chunk_count_written = 0; 1781 *chunk_size_written = 0; 1782 *total_size_written = 0; 1783 1784 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 1785 pr_err("no right to read(%pD)\n", src_fp->filp); 1786 return -EACCES; 1787 } 1788 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) { 1789 pr_err("no right to write(%pD)\n", dst_fp->filp); 1790 return -EACCES; 1791 } 1792 1793 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp)) 1794 return -EBADF; 1795 1796 smb_break_all_levII_oplock(work, dst_fp, 1); 1797 1798 if (!work->tcon->posix_extensions) { 1799 for (i = 0; i < chunk_count; i++) { 1800 src_off = le64_to_cpu(chunks[i].SourceOffset); 1801 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1802 len = le32_to_cpu(chunks[i].Length); 1803 1804 if (check_lock_range(src_fp->filp, src_off, 1805 src_off + len - 1, READ)) 1806 return -EAGAIN; 1807 if (check_lock_range(dst_fp->filp, dst_off, 1808 dst_off + len - 1, WRITE)) 1809 return -EAGAIN; 1810 } 1811 } 1812 1813 src_file_size = i_size_read(file_inode(src_fp->filp)); 1814 1815 for (i = 0; i < chunk_count; i++) { 1816 src_off = le64_to_cpu(chunks[i].SourceOffset); 1817 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1818 len = le32_to_cpu(chunks[i].Length); 1819 1820 if (src_off + len > src_file_size) 1821 return -E2BIG; 1822 1823 ret = vfs_copy_file_range(src_fp->filp, src_off, 1824 dst_fp->filp, dst_off, len, 0); 1825 if (ret == -EOPNOTSUPP || ret == -EXDEV) 1826 ret = vfs_copy_file_range(src_fp->filp, src_off, 1827 dst_fp->filp, dst_off, len, 1828 COPY_FILE_SPLICE); 1829 if (ret < 0) 1830 return ret; 1831 1832 *chunk_count_written += 1; 1833 *total_size_written += ret; 1834 } 1835 return 0; 1836 } 1837 1838 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock) 1839 { 1840 wait_event(flock->fl_wait, !flock->fl_blocker); 1841 } 1842 1843 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout) 1844 { 1845 return wait_event_interruptible_timeout(flock->fl_wait, 1846 !flock->fl_blocker, 1847 timeout); 1848 } 1849 1850 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock) 1851 { 1852 locks_delete_block(flock); 1853 } 1854 1855 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap, 1856 struct path *path) 1857 { 1858 struct posix_acl_state acl_state; 1859 struct posix_acl *acls; 1860 struct dentry *dentry = path->dentry; 1861 struct inode *inode = d_inode(dentry); 1862 int rc; 1863 1864 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1865 return -EOPNOTSUPP; 1866 1867 ksmbd_debug(SMB, "Set posix acls\n"); 1868 rc = init_acl_state(&acl_state, 1); 1869 if (rc) 1870 return rc; 1871 1872 /* Set default owner group */ 1873 acl_state.owner.allow = (inode->i_mode & 0700) >> 6; 1874 acl_state.group.allow = (inode->i_mode & 0070) >> 3; 1875 acl_state.other.allow = inode->i_mode & 0007; 1876 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid; 1877 acl_state.users->aces[acl_state.users->n++].perms.allow = 1878 acl_state.owner.allow; 1879 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid; 1880 acl_state.groups->aces[acl_state.groups->n++].perms.allow = 1881 acl_state.group.allow; 1882 acl_state.mask.allow = 0x07; 1883 1884 acls = posix_acl_alloc(6, GFP_KERNEL); 1885 if (!acls) { 1886 free_acl_state(&acl_state); 1887 return -ENOMEM; 1888 } 1889 posix_state_to_acl(&acl_state, acls->a_entries); 1890 1891 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1892 if (rc < 0) 1893 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1894 rc); 1895 else if (S_ISDIR(inode->i_mode)) { 1896 posix_state_to_acl(&acl_state, acls->a_entries); 1897 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls); 1898 if (rc < 0) 1899 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1900 rc); 1901 } 1902 1903 free_acl_state(&acl_state); 1904 posix_acl_release(acls); 1905 return rc; 1906 } 1907 1908 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap, 1909 struct path *path, struct inode *parent_inode) 1910 { 1911 struct posix_acl *acls; 1912 struct posix_acl_entry *pace; 1913 struct dentry *dentry = path->dentry; 1914 struct inode *inode = d_inode(dentry); 1915 int rc, i; 1916 1917 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1918 return -EOPNOTSUPP; 1919 1920 acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT); 1921 if (IS_ERR_OR_NULL(acls)) 1922 return -ENOENT; 1923 pace = acls->a_entries; 1924 1925 for (i = 0; i < acls->a_count; i++, pace++) { 1926 if (pace->e_tag == ACL_MASK) { 1927 pace->e_perm = 0x07; 1928 break; 1929 } 1930 } 1931 1932 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1933 if (rc < 0) 1934 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1935 rc); 1936 if (S_ISDIR(inode->i_mode)) { 1937 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, 1938 acls); 1939 if (rc < 0) 1940 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1941 rc); 1942 } 1943 1944 posix_acl_release(acls); 1945 return rc; 1946 } 1947