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 inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT); 101 d = lookup_one_qstr_excl(&last, parent_path->dentry, 0); 102 if (IS_ERR(d)) 103 goto err_out; 104 105 if (d_is_negative(d)) { 106 dput(d); 107 goto err_out; 108 } 109 110 path->dentry = d; 111 path->mnt = mntget(parent_path->mnt); 112 113 if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) { 114 err = follow_down(path, 0); 115 if (err < 0) { 116 path_put(path); 117 goto err_out; 118 } 119 } 120 121 putname(filename); 122 return 0; 123 124 err_out: 125 inode_unlock(d_inode(parent_path->dentry)); 126 path_put(parent_path); 127 putname(filename); 128 return -ENOENT; 129 } 130 131 void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap, 132 struct dentry *dentry, __le32 *daccess) 133 { 134 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL); 135 136 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE)) 137 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | 138 FILE_WRITE_DATA | FILE_APPEND_DATA | 139 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | 140 FILE_DELETE_CHILD); 141 142 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ)) 143 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE; 144 145 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC)) 146 *daccess |= FILE_EXECUTE_LE; 147 148 if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE)) 149 *daccess |= FILE_DELETE_LE; 150 } 151 152 /** 153 * ksmbd_vfs_create() - vfs helper for smb create file 154 * @work: work 155 * @name: file name that is relative to share 156 * @mode: file create mode 157 * 158 * Return: 0 on success, otherwise error 159 */ 160 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode) 161 { 162 struct path path; 163 struct dentry *dentry; 164 int err; 165 166 dentry = ksmbd_vfs_kern_path_create(work, name, 167 LOOKUP_NO_SYMLINKS, &path); 168 if (IS_ERR(dentry)) { 169 err = PTR_ERR(dentry); 170 if (err != -ENOENT) 171 pr_err("path create failed for %s, err %d\n", 172 name, err); 173 return err; 174 } 175 176 err = mnt_want_write(path.mnt); 177 if (err) 178 goto out_err; 179 180 mode |= S_IFREG; 181 err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry), 182 dentry, mode, true); 183 if (!err) { 184 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), 185 d_inode(dentry)); 186 } else { 187 pr_err("File(%s): creation failed (err:%d)\n", name, err); 188 } 189 mnt_drop_write(path.mnt); 190 191 out_err: 192 done_path_create(&path, dentry); 193 return err; 194 } 195 196 /** 197 * ksmbd_vfs_mkdir() - vfs helper for smb create directory 198 * @work: work 199 * @name: directory name that is relative to share 200 * @mode: directory create mode 201 * 202 * Return: 0 on success, otherwise error 203 */ 204 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode) 205 { 206 struct mnt_idmap *idmap; 207 struct path path; 208 struct dentry *dentry; 209 int err; 210 211 dentry = ksmbd_vfs_kern_path_create(work, name, 212 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY, 213 &path); 214 if (IS_ERR(dentry)) { 215 err = PTR_ERR(dentry); 216 if (err != -EEXIST) 217 ksmbd_debug(VFS, "path create failed for %s, err %d\n", 218 name, err); 219 return err; 220 } 221 222 err = mnt_want_write(path.mnt); 223 if (err) 224 goto out_err2; 225 226 idmap = mnt_idmap(path.mnt); 227 mode |= S_IFDIR; 228 err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode); 229 if (!err && d_unhashed(dentry)) { 230 struct dentry *d; 231 232 d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent, 233 dentry->d_name.len); 234 if (IS_ERR(d)) { 235 err = PTR_ERR(d); 236 goto out_err1; 237 } 238 if (unlikely(d_is_negative(d))) { 239 dput(d); 240 err = -ENOENT; 241 goto out_err1; 242 } 243 244 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d)); 245 dput(d); 246 } 247 248 out_err1: 249 mnt_drop_write(path.mnt); 250 out_err2: 251 done_path_create(&path, dentry); 252 if (err) 253 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err); 254 return err; 255 } 256 257 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap, 258 struct dentry *dentry, char *attr_name, 259 int attr_name_len, char **attr_value) 260 { 261 char *name, *xattr_list = NULL; 262 ssize_t value_len = -ENOENT, xattr_list_len; 263 264 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 265 if (xattr_list_len <= 0) 266 goto out; 267 268 for (name = xattr_list; name - xattr_list < xattr_list_len; 269 name += strlen(name) + 1) { 270 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 271 if (strncasecmp(attr_name, name, attr_name_len)) 272 continue; 273 274 value_len = ksmbd_vfs_getxattr(idmap, 275 dentry, 276 name, 277 attr_value); 278 if (value_len < 0) 279 pr_err("failed to get xattr in file\n"); 280 break; 281 } 282 283 out: 284 kvfree(xattr_list); 285 return value_len; 286 } 287 288 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos, 289 size_t count) 290 { 291 ssize_t v_len; 292 char *stream_buf = NULL; 293 294 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n", 295 *pos, count); 296 297 v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp), 298 fp->filp->f_path.dentry, 299 fp->stream.name, 300 fp->stream.size, 301 &stream_buf); 302 if ((int)v_len <= 0) 303 return (int)v_len; 304 305 if (v_len <= *pos) { 306 count = -EINVAL; 307 goto free_buf; 308 } 309 310 if (v_len - *pos < count) 311 count = v_len - *pos; 312 313 memcpy(buf, &stream_buf[*pos], count); 314 315 free_buf: 316 kvfree(stream_buf); 317 return count; 318 } 319 320 /** 321 * check_lock_range() - vfs helper for smb byte range file locking 322 * @filp: the file to apply the lock to 323 * @start: lock start byte offset 324 * @end: lock end byte offset 325 * @type: byte range type read/write 326 * 327 * Return: 0 on success, otherwise error 328 */ 329 static int check_lock_range(struct file *filp, loff_t start, loff_t end, 330 unsigned char type) 331 { 332 struct file_lock *flock; 333 struct file_lock_context *ctx = locks_inode_context(file_inode(filp)); 334 int error = 0; 335 336 if (!ctx || list_empty_careful(&ctx->flc_posix)) 337 return 0; 338 339 spin_lock(&ctx->flc_lock); 340 list_for_each_entry(flock, &ctx->flc_posix, fl_list) { 341 /* check conflict locks */ 342 if (flock->fl_end >= start && end >= flock->fl_start) { 343 if (flock->fl_type == F_RDLCK) { 344 if (type == WRITE) { 345 pr_err("not allow write by shared lock\n"); 346 error = 1; 347 goto out; 348 } 349 } else if (flock->fl_type == F_WRLCK) { 350 /* check owner in lock */ 351 if (flock->fl_file != filp) { 352 error = 1; 353 pr_err("not allow rw access by exclusive lock from other opens\n"); 354 goto out; 355 } 356 } 357 } 358 } 359 out: 360 spin_unlock(&ctx->flc_lock); 361 return error; 362 } 363 364 /** 365 * ksmbd_vfs_read() - vfs helper for smb file read 366 * @work: smb work 367 * @fid: file id of open file 368 * @count: read byte count 369 * @pos: file pos 370 * @rbuf: read data buffer 371 * 372 * Return: number of read bytes on success, otherwise error 373 */ 374 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count, 375 loff_t *pos, char *rbuf) 376 { 377 struct file *filp = fp->filp; 378 ssize_t nbytes = 0; 379 struct inode *inode = file_inode(filp); 380 381 if (S_ISDIR(inode->i_mode)) 382 return -EISDIR; 383 384 if (unlikely(count == 0)) 385 return 0; 386 387 if (work->conn->connection_type) { 388 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 389 pr_err("no right to read(%pD)\n", fp->filp); 390 return -EACCES; 391 } 392 } 393 394 if (ksmbd_stream_fd(fp)) 395 return ksmbd_vfs_stream_read(fp, rbuf, pos, count); 396 397 if (!work->tcon->posix_extensions) { 398 int ret; 399 400 ret = check_lock_range(filp, *pos, *pos + count - 1, READ); 401 if (ret) { 402 pr_err("unable to read due to lock\n"); 403 return -EAGAIN; 404 } 405 } 406 407 nbytes = kernel_read(filp, rbuf, count, pos); 408 if (nbytes < 0) { 409 pr_err("smb read failed, err = %zd\n", nbytes); 410 return nbytes; 411 } 412 413 filp->f_pos = *pos; 414 return nbytes; 415 } 416 417 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, 418 size_t count) 419 { 420 char *stream_buf = NULL, *wbuf; 421 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 422 size_t size; 423 ssize_t v_len; 424 int err = 0; 425 426 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n", 427 *pos, count); 428 429 size = *pos + count; 430 if (size > XATTR_SIZE_MAX) { 431 size = XATTR_SIZE_MAX; 432 count = (*pos + count) - XATTR_SIZE_MAX; 433 } 434 435 v_len = ksmbd_vfs_getcasexattr(idmap, 436 fp->filp->f_path.dentry, 437 fp->stream.name, 438 fp->stream.size, 439 &stream_buf); 440 if (v_len < 0) { 441 pr_err("not found stream in xattr : %zd\n", v_len); 442 err = v_len; 443 goto out; 444 } 445 446 if (v_len < size) { 447 wbuf = kvzalloc(size, GFP_KERNEL); 448 if (!wbuf) { 449 err = -ENOMEM; 450 goto out; 451 } 452 453 if (v_len > 0) 454 memcpy(wbuf, stream_buf, v_len); 455 kvfree(stream_buf); 456 stream_buf = wbuf; 457 } 458 459 memcpy(&stream_buf[*pos], buf, count); 460 461 err = ksmbd_vfs_setxattr(idmap, 462 &fp->filp->f_path, 463 fp->stream.name, 464 (void *)stream_buf, 465 size, 466 0); 467 if (err < 0) 468 goto out; 469 470 fp->filp->f_pos = *pos; 471 err = 0; 472 out: 473 kvfree(stream_buf); 474 return err; 475 } 476 477 /** 478 * ksmbd_vfs_write() - vfs helper for smb file write 479 * @work: work 480 * @fid: file id of open file 481 * @buf: buf containing data for writing 482 * @count: read byte count 483 * @pos: file pos 484 * @sync: fsync after write 485 * @written: number of bytes written 486 * 487 * Return: 0 on success, otherwise error 488 */ 489 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp, 490 char *buf, size_t count, loff_t *pos, bool sync, 491 ssize_t *written) 492 { 493 struct file *filp; 494 loff_t offset = *pos; 495 int err = 0; 496 497 if (work->conn->connection_type) { 498 if (!(fp->daccess & FILE_WRITE_DATA_LE)) { 499 pr_err("no right to write(%pD)\n", fp->filp); 500 err = -EACCES; 501 goto out; 502 } 503 } 504 505 filp = fp->filp; 506 507 if (ksmbd_stream_fd(fp)) { 508 err = ksmbd_vfs_stream_write(fp, buf, pos, count); 509 if (!err) 510 *written = count; 511 goto out; 512 } 513 514 if (!work->tcon->posix_extensions) { 515 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE); 516 if (err) { 517 pr_err("unable to write due to lock\n"); 518 err = -EAGAIN; 519 goto out; 520 } 521 } 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 err = mnt_want_write(path->mnt); 609 if (err) 610 goto out_err; 611 612 idmap = mnt_idmap(path->mnt); 613 if (S_ISDIR(d_inode(path->dentry)->i_mode)) { 614 err = vfs_rmdir(idmap, d_inode(parent), path->dentry); 615 if (err && err != -ENOTEMPTY) 616 ksmbd_debug(VFS, "rmdir failed, err %d\n", err); 617 } else { 618 err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL); 619 if (err) 620 ksmbd_debug(VFS, "unlink failed, err %d\n", err); 621 } 622 mnt_drop_write(path->mnt); 623 624 out_err: 625 ksmbd_revert_fsids(work); 626 return err; 627 } 628 629 /** 630 * ksmbd_vfs_link() - vfs helper for creating smb hardlink 631 * @oldname: source file name 632 * @newname: hardlink name that is relative to share 633 * 634 * Return: 0 on success, otherwise error 635 */ 636 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname, 637 const char *newname) 638 { 639 struct path oldpath, newpath; 640 struct dentry *dentry; 641 int err; 642 643 if (ksmbd_override_fsids(work)) 644 return -ENOMEM; 645 646 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath); 647 if (err) { 648 pr_err("cannot get linux path for %s, err = %d\n", 649 oldname, err); 650 goto out1; 651 } 652 653 dentry = ksmbd_vfs_kern_path_create(work, newname, 654 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL, 655 &newpath); 656 if (IS_ERR(dentry)) { 657 err = PTR_ERR(dentry); 658 pr_err("path create err for %s, err %d\n", newname, err); 659 goto out2; 660 } 661 662 err = -EXDEV; 663 if (oldpath.mnt != newpath.mnt) { 664 pr_err("vfs_link failed err %d\n", err); 665 goto out3; 666 } 667 668 err = mnt_want_write(newpath.mnt); 669 if (err) 670 goto out3; 671 672 err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt), 673 d_inode(newpath.dentry), 674 dentry, NULL); 675 if (err) 676 ksmbd_debug(VFS, "vfs_link failed err %d\n", err); 677 mnt_drop_write(newpath.mnt); 678 679 out3: 680 done_path_create(&newpath, dentry); 681 out2: 682 path_put(&oldpath); 683 out1: 684 ksmbd_revert_fsids(work); 685 return err; 686 } 687 688 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, 689 char *newname, int flags) 690 { 691 struct dentry *old_parent, *new_dentry, *trap; 692 struct dentry *old_child = old_path->dentry; 693 struct path new_path; 694 struct qstr new_last; 695 struct renamedata rd; 696 struct filename *to; 697 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 698 struct ksmbd_file *parent_fp; 699 int new_type; 700 int err, lookup_flags = LOOKUP_NO_SYMLINKS; 701 702 if (ksmbd_override_fsids(work)) 703 return -ENOMEM; 704 705 to = getname_kernel(newname); 706 if (IS_ERR(to)) { 707 err = PTR_ERR(to); 708 goto revert_fsids; 709 } 710 711 retry: 712 err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH, 713 &new_path, &new_last, &new_type, 714 &share_conf->vfs_path); 715 if (err) 716 goto out1; 717 718 if (old_path->mnt != new_path.mnt) { 719 err = -EXDEV; 720 goto out2; 721 } 722 723 err = mnt_want_write(old_path->mnt); 724 if (err) 725 goto out2; 726 727 trap = lock_rename_child(old_child, new_path.dentry); 728 729 old_parent = dget(old_child->d_parent); 730 if (d_unhashed(old_child)) { 731 err = -EINVAL; 732 goto out3; 733 } 734 735 parent_fp = ksmbd_lookup_fd_inode(d_inode(old_child->d_parent)); 736 if (parent_fp) { 737 if (parent_fp->daccess & FILE_DELETE_LE) { 738 pr_err("parent dir is opened with delete access\n"); 739 err = -ESHARE; 740 ksmbd_fd_put(work, parent_fp); 741 goto out3; 742 } 743 ksmbd_fd_put(work, parent_fp); 744 } 745 746 new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry, 747 lookup_flags | LOOKUP_RENAME_TARGET); 748 if (IS_ERR(new_dentry)) { 749 err = PTR_ERR(new_dentry); 750 goto out3; 751 } 752 753 if (d_is_symlink(new_dentry)) { 754 err = -EACCES; 755 goto out4; 756 } 757 758 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) { 759 err = -EEXIST; 760 goto out4; 761 } 762 763 if (old_child == trap) { 764 err = -EINVAL; 765 goto out4; 766 } 767 768 if (new_dentry == trap) { 769 err = -ENOTEMPTY; 770 goto out4; 771 } 772 773 rd.old_mnt_idmap = mnt_idmap(old_path->mnt), 774 rd.old_dir = d_inode(old_parent), 775 rd.old_dentry = old_child, 776 rd.new_mnt_idmap = mnt_idmap(new_path.mnt), 777 rd.new_dir = new_path.dentry->d_inode, 778 rd.new_dentry = new_dentry, 779 rd.flags = flags, 780 rd.delegated_inode = NULL, 781 err = vfs_rename(&rd); 782 if (err) 783 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err); 784 785 out4: 786 dput(new_dentry); 787 out3: 788 dput(old_parent); 789 unlock_rename(old_parent, new_path.dentry); 790 mnt_drop_write(old_path->mnt); 791 out2: 792 path_put(&new_path); 793 794 if (retry_estale(err, lookup_flags)) { 795 lookup_flags |= LOOKUP_REVAL; 796 goto retry; 797 } 798 out1: 799 putname(to); 800 revert_fsids: 801 ksmbd_revert_fsids(work); 802 return err; 803 } 804 805 /** 806 * ksmbd_vfs_truncate() - vfs helper for smb file truncate 807 * @work: work 808 * @fid: file id of old file 809 * @size: truncate to given size 810 * 811 * Return: 0 on success, otherwise error 812 */ 813 int ksmbd_vfs_truncate(struct ksmbd_work *work, 814 struct ksmbd_file *fp, loff_t size) 815 { 816 int err = 0; 817 struct file *filp; 818 819 filp = fp->filp; 820 821 /* Do we need to break any of a levelII oplock? */ 822 smb_break_all_levII_oplock(work, fp, 1); 823 824 if (!work->tcon->posix_extensions) { 825 struct inode *inode = file_inode(filp); 826 827 if (size < inode->i_size) { 828 err = check_lock_range(filp, size, 829 inode->i_size - 1, WRITE); 830 } else { 831 err = check_lock_range(filp, inode->i_size, 832 size - 1, WRITE); 833 } 834 835 if (err) { 836 pr_err("failed due to lock\n"); 837 return -EAGAIN; 838 } 839 } 840 841 err = vfs_truncate(&filp->f_path, size); 842 if (err) 843 pr_err("truncate failed, err %d\n", err); 844 return err; 845 } 846 847 /** 848 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes 849 * @dentry: dentry of file for listing xattrs 850 * @list: destination buffer 851 * @size: destination buffer length 852 * 853 * Return: xattr list length on success, otherwise error 854 */ 855 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list) 856 { 857 ssize_t size; 858 char *vlist = NULL; 859 860 size = vfs_listxattr(dentry, NULL, 0); 861 if (size <= 0) 862 return size; 863 864 vlist = kvzalloc(size, GFP_KERNEL); 865 if (!vlist) 866 return -ENOMEM; 867 868 *list = vlist; 869 size = vfs_listxattr(dentry, vlist, size); 870 if (size < 0) { 871 ksmbd_debug(VFS, "listxattr failed\n"); 872 kvfree(vlist); 873 *list = NULL; 874 } 875 876 return size; 877 } 878 879 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap, 880 struct dentry *dentry, char *xattr_name) 881 { 882 return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0); 883 } 884 885 /** 886 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value 887 * @idmap: idmap 888 * @dentry: dentry of file for getting xattrs 889 * @xattr_name: name of xattr name to query 890 * @xattr_buf: destination buffer xattr value 891 * 892 * Return: read xattr value length on success, otherwise error 893 */ 894 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap, 895 struct dentry *dentry, 896 char *xattr_name, char **xattr_buf) 897 { 898 ssize_t xattr_len; 899 char *buf; 900 901 *xattr_buf = NULL; 902 xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name); 903 if (xattr_len < 0) 904 return xattr_len; 905 906 buf = kmalloc(xattr_len + 1, GFP_KERNEL); 907 if (!buf) 908 return -ENOMEM; 909 910 xattr_len = vfs_getxattr(idmap, dentry, xattr_name, 911 (void *)buf, xattr_len); 912 if (xattr_len > 0) 913 *xattr_buf = buf; 914 else 915 kfree(buf); 916 return xattr_len; 917 } 918 919 /** 920 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value 921 * @idmap: idmap of the relevant mount 922 * @dentry: dentry to set XATTR at 923 * @attr_name: xattr name for setxattr 924 * @attr_value: xattr value to set 925 * @attr_size: size of xattr value 926 * @flags: destination buffer length 927 * 928 * Return: 0 on success, otherwise error 929 */ 930 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap, 931 const struct path *path, const char *attr_name, 932 void *attr_value, size_t attr_size, int flags) 933 { 934 int err; 935 936 err = mnt_want_write(path->mnt); 937 if (err) 938 return err; 939 940 err = vfs_setxattr(idmap, 941 path->dentry, 942 attr_name, 943 attr_value, 944 attr_size, 945 flags); 946 if (err) 947 ksmbd_debug(VFS, "setxattr failed, err %d\n", err); 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 * @path: if lookup succeed, return path info 1200 * @caseless: caseless filename lookup 1201 * 1202 * Return: 0 on success, otherwise error 1203 */ 1204 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name, 1205 unsigned int flags, struct path *parent_path, 1206 struct path *path, bool caseless) 1207 { 1208 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 1209 int err; 1210 1211 err = ksmbd_vfs_path_lookup_locked(share_conf, name, flags, parent_path, 1212 path); 1213 if (!err) 1214 return 0; 1215 1216 if (caseless) { 1217 char *filepath; 1218 size_t path_len, remain_len; 1219 1220 filepath = kstrdup(name, GFP_KERNEL); 1221 if (!filepath) 1222 return -ENOMEM; 1223 1224 path_len = strlen(filepath); 1225 remain_len = path_len; 1226 1227 *parent_path = share_conf->vfs_path; 1228 path_get(parent_path); 1229 1230 while (d_can_lookup(parent_path->dentry)) { 1231 char *filename = filepath + path_len - remain_len; 1232 char *next = strchrnul(filename, '/'); 1233 size_t filename_len = next - filename; 1234 bool is_last = !next[0]; 1235 1236 if (filename_len == 0) 1237 break; 1238 1239 err = ksmbd_vfs_lookup_in_dir(parent_path, filename, 1240 filename_len, 1241 work->conn->um); 1242 if (err) 1243 goto out2; 1244 1245 next[0] = '\0'; 1246 1247 err = vfs_path_lookup(share_conf->vfs_path.dentry, 1248 share_conf->vfs_path.mnt, 1249 filepath, 1250 flags, 1251 path); 1252 if (err) 1253 goto out2; 1254 else if (is_last) 1255 goto out1; 1256 path_put(parent_path); 1257 *parent_path = *path; 1258 1259 next[0] = '/'; 1260 remain_len -= filename_len + 1; 1261 } 1262 1263 err = -EINVAL; 1264 out2: 1265 path_put(parent_path); 1266 out1: 1267 kfree(filepath); 1268 } 1269 1270 if (!err) { 1271 err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry); 1272 if (err) { 1273 path_put(path); 1274 path_put(parent_path); 1275 } 1276 } 1277 return err; 1278 } 1279 1280 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work, 1281 const char *name, 1282 unsigned int flags, 1283 struct path *path) 1284 { 1285 char *abs_name; 1286 struct dentry *dent; 1287 1288 abs_name = convert_to_unix_name(work->tcon->share_conf, name); 1289 if (!abs_name) 1290 return ERR_PTR(-ENOMEM); 1291 1292 dent = kern_path_create(AT_FDCWD, abs_name, path, flags); 1293 kfree(abs_name); 1294 return dent; 1295 } 1296 1297 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap, 1298 const struct path *path) 1299 { 1300 char *name, *xattr_list = NULL; 1301 ssize_t xattr_list_len; 1302 int err = 0; 1303 1304 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1305 if (xattr_list_len < 0) { 1306 goto out; 1307 } else if (!xattr_list_len) { 1308 ksmbd_debug(SMB, "empty xattr in the file\n"); 1309 goto out; 1310 } 1311 1312 err = mnt_want_write(path->mnt); 1313 if (err) 1314 goto out; 1315 1316 for (name = xattr_list; name - xattr_list < xattr_list_len; 1317 name += strlen(name) + 1) { 1318 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1319 1320 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, 1321 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) || 1322 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, 1323 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) { 1324 err = vfs_remove_acl(idmap, path->dentry, name); 1325 if (err) 1326 ksmbd_debug(SMB, 1327 "remove acl xattr failed : %s\n", name); 1328 } 1329 } 1330 mnt_drop_write(path->mnt); 1331 1332 out: 1333 kvfree(xattr_list); 1334 return err; 1335 } 1336 1337 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap, const struct path *path) 1338 { 1339 char *name, *xattr_list = NULL; 1340 ssize_t xattr_list_len; 1341 int err = 0; 1342 1343 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1344 if (xattr_list_len < 0) { 1345 goto out; 1346 } else if (!xattr_list_len) { 1347 ksmbd_debug(SMB, "empty xattr in the file\n"); 1348 goto out; 1349 } 1350 1351 for (name = xattr_list; name - xattr_list < xattr_list_len; 1352 name += strlen(name) + 1) { 1353 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1354 1355 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) { 1356 err = ksmbd_vfs_remove_xattr(idmap, path, name); 1357 if (err) 1358 ksmbd_debug(SMB, "remove xattr failed : %s\n", name); 1359 } 1360 } 1361 out: 1362 kvfree(xattr_list); 1363 return err; 1364 } 1365 1366 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap, 1367 struct inode *inode, 1368 int acl_type) 1369 { 1370 struct xattr_smb_acl *smb_acl = NULL; 1371 struct posix_acl *posix_acls; 1372 struct posix_acl_entry *pa_entry; 1373 struct xattr_acl_entry *xa_entry; 1374 int i; 1375 1376 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1377 return NULL; 1378 1379 posix_acls = get_inode_acl(inode, acl_type); 1380 if (IS_ERR_OR_NULL(posix_acls)) 1381 return NULL; 1382 1383 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) + 1384 sizeof(struct xattr_acl_entry) * posix_acls->a_count, 1385 GFP_KERNEL); 1386 if (!smb_acl) 1387 goto out; 1388 1389 smb_acl->count = posix_acls->a_count; 1390 pa_entry = posix_acls->a_entries; 1391 xa_entry = smb_acl->entries; 1392 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) { 1393 switch (pa_entry->e_tag) { 1394 case ACL_USER: 1395 xa_entry->type = SMB_ACL_USER; 1396 xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry); 1397 break; 1398 case ACL_USER_OBJ: 1399 xa_entry->type = SMB_ACL_USER_OBJ; 1400 break; 1401 case ACL_GROUP: 1402 xa_entry->type = SMB_ACL_GROUP; 1403 xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry); 1404 break; 1405 case ACL_GROUP_OBJ: 1406 xa_entry->type = SMB_ACL_GROUP_OBJ; 1407 break; 1408 case ACL_OTHER: 1409 xa_entry->type = SMB_ACL_OTHER; 1410 break; 1411 case ACL_MASK: 1412 xa_entry->type = SMB_ACL_MASK; 1413 break; 1414 default: 1415 pr_err("unknown type : 0x%x\n", pa_entry->e_tag); 1416 goto out; 1417 } 1418 1419 if (pa_entry->e_perm & ACL_READ) 1420 xa_entry->perm |= SMB_ACL_READ; 1421 if (pa_entry->e_perm & ACL_WRITE) 1422 xa_entry->perm |= SMB_ACL_WRITE; 1423 if (pa_entry->e_perm & ACL_EXECUTE) 1424 xa_entry->perm |= SMB_ACL_EXECUTE; 1425 } 1426 out: 1427 posix_acl_release(posix_acls); 1428 return smb_acl; 1429 } 1430 1431 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn, 1432 struct mnt_idmap *idmap, 1433 const struct path *path, 1434 struct smb_ntsd *pntsd, int len) 1435 { 1436 int rc; 1437 struct ndr sd_ndr = {0}, acl_ndr = {0}; 1438 struct xattr_ntacl acl = {0}; 1439 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL; 1440 struct dentry *dentry = path->dentry; 1441 struct inode *inode = d_inode(dentry); 1442 1443 acl.version = 4; 1444 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256; 1445 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode)); 1446 1447 memcpy(acl.desc, "posix_acl", 9); 1448 acl.desc_len = 10; 1449 1450 pntsd->osidoffset = 1451 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF); 1452 pntsd->gsidoffset = 1453 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF); 1454 pntsd->dacloffset = 1455 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF); 1456 1457 acl.sd_buf = (char *)pntsd; 1458 acl.sd_size = len; 1459 1460 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash); 1461 if (rc) { 1462 pr_err("failed to generate hash for ndr acl\n"); 1463 return rc; 1464 } 1465 1466 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1467 ACL_TYPE_ACCESS); 1468 if (S_ISDIR(inode->i_mode)) 1469 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1470 ACL_TYPE_DEFAULT); 1471 1472 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, 1473 smb_acl, def_smb_acl); 1474 if (rc) { 1475 pr_err("failed to encode ndr to posix acl\n"); 1476 goto out; 1477 } 1478 1479 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, 1480 acl.posix_acl_hash); 1481 if (rc) { 1482 pr_err("failed to generate hash for ndr acl\n"); 1483 goto out; 1484 } 1485 1486 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl); 1487 if (rc) { 1488 pr_err("failed to encode ndr to posix acl\n"); 1489 goto out; 1490 } 1491 1492 rc = ksmbd_vfs_setxattr(idmap, path, 1493 XATTR_NAME_SD, sd_ndr.data, 1494 sd_ndr.offset, 0); 1495 if (rc < 0) 1496 pr_err("Failed to store XATTR ntacl :%d\n", rc); 1497 1498 kfree(sd_ndr.data); 1499 out: 1500 kfree(acl_ndr.data); 1501 kfree(smb_acl); 1502 kfree(def_smb_acl); 1503 return rc; 1504 } 1505 1506 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, 1507 struct mnt_idmap *idmap, 1508 struct dentry *dentry, 1509 struct smb_ntsd **pntsd) 1510 { 1511 int rc; 1512 struct ndr n; 1513 struct inode *inode = d_inode(dentry); 1514 struct ndr acl_ndr = {0}; 1515 struct xattr_ntacl acl; 1516 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL; 1517 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0}; 1518 1519 rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data); 1520 if (rc <= 0) 1521 return rc; 1522 1523 n.length = rc; 1524 rc = ndr_decode_v4_ntacl(&n, &acl); 1525 if (rc) 1526 goto free_n_data; 1527 1528 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1529 ACL_TYPE_ACCESS); 1530 if (S_ISDIR(inode->i_mode)) 1531 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1532 ACL_TYPE_DEFAULT); 1533 1534 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl, 1535 def_smb_acl); 1536 if (rc) { 1537 pr_err("failed to encode ndr to posix acl\n"); 1538 goto out_free; 1539 } 1540 1541 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash); 1542 if (rc) { 1543 pr_err("failed to generate hash for ndr acl\n"); 1544 goto out_free; 1545 } 1546 1547 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) { 1548 pr_err("hash value diff\n"); 1549 rc = -EINVAL; 1550 goto out_free; 1551 } 1552 1553 *pntsd = acl.sd_buf; 1554 if (acl.sd_size < sizeof(struct smb_ntsd)) { 1555 pr_err("sd size is invalid\n"); 1556 goto out_free; 1557 } 1558 1559 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) - 1560 NDR_NTSD_OFFSETOF); 1561 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) - 1562 NDR_NTSD_OFFSETOF); 1563 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) - 1564 NDR_NTSD_OFFSETOF); 1565 1566 rc = acl.sd_size; 1567 out_free: 1568 kfree(acl_ndr.data); 1569 kfree(smb_acl); 1570 kfree(def_smb_acl); 1571 if (rc < 0) { 1572 kfree(acl.sd_buf); 1573 *pntsd = NULL; 1574 } 1575 1576 free_n_data: 1577 kfree(n.data); 1578 return rc; 1579 } 1580 1581 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap, 1582 const struct path *path, 1583 struct xattr_dos_attrib *da) 1584 { 1585 struct ndr n; 1586 int err; 1587 1588 err = ndr_encode_dos_attr(&n, da); 1589 if (err) 1590 return err; 1591 1592 err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE, 1593 (void *)n.data, n.offset, 0); 1594 if (err) 1595 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n"); 1596 kfree(n.data); 1597 1598 return err; 1599 } 1600 1601 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap, 1602 struct dentry *dentry, 1603 struct xattr_dos_attrib *da) 1604 { 1605 struct ndr n; 1606 int err; 1607 1608 err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE, 1609 (char **)&n.data); 1610 if (err > 0) { 1611 n.length = err; 1612 if (ndr_decode_dos_attr(&n, da)) 1613 err = -EINVAL; 1614 kfree(n.data); 1615 } else { 1616 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n"); 1617 } 1618 1619 return err; 1620 } 1621 1622 /** 1623 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format 1624 * @p: destination buffer 1625 * @ksmbd_kstat: ksmbd kstat wrapper 1626 */ 1627 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat) 1628 { 1629 struct file_directory_info *info = (struct file_directory_info *)(*p); 1630 struct kstat *kstat = ksmbd_kstat->kstat; 1631 u64 time; 1632 1633 info->FileIndex = 0; 1634 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); 1635 time = ksmbd_UnixTimeToNT(kstat->atime); 1636 info->LastAccessTime = cpu_to_le64(time); 1637 time = ksmbd_UnixTimeToNT(kstat->mtime); 1638 info->LastWriteTime = cpu_to_le64(time); 1639 time = ksmbd_UnixTimeToNT(kstat->ctime); 1640 info->ChangeTime = cpu_to_le64(time); 1641 1642 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { 1643 info->EndOfFile = 0; 1644 info->AllocationSize = 0; 1645 } else { 1646 info->EndOfFile = cpu_to_le64(kstat->size); 1647 info->AllocationSize = cpu_to_le64(kstat->blocks << 9); 1648 } 1649 info->ExtFileAttributes = ksmbd_kstat->file_attributes; 1650 1651 return info; 1652 } 1653 1654 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, 1655 struct mnt_idmap *idmap, 1656 struct dentry *dentry, 1657 struct ksmbd_kstat *ksmbd_kstat) 1658 { 1659 u64 time; 1660 int rc; 1661 1662 generic_fillattr(idmap, STATX_BASIC_STATS, d_inode(dentry), 1663 ksmbd_kstat->kstat); 1664 1665 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); 1666 ksmbd_kstat->create_time = time; 1667 1668 /* 1669 * set default value for the case that store dos attributes is not yes 1670 * or that acl is disable in server's filesystem and the config is yes. 1671 */ 1672 if (S_ISDIR(ksmbd_kstat->kstat->mode)) 1673 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE; 1674 else 1675 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE; 1676 1677 if (test_share_config_flag(work->tcon->share_conf, 1678 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { 1679 struct xattr_dos_attrib da; 1680 1681 rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da); 1682 if (rc > 0) { 1683 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr); 1684 ksmbd_kstat->create_time = da.create_time; 1685 } else { 1686 ksmbd_debug(VFS, "fail to load dos attribute.\n"); 1687 } 1688 } 1689 1690 return 0; 1691 } 1692 1693 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap, 1694 struct dentry *dentry, char *attr_name, 1695 int attr_name_len) 1696 { 1697 char *name, *xattr_list = NULL; 1698 ssize_t value_len = -ENOENT, xattr_list_len; 1699 1700 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 1701 if (xattr_list_len <= 0) 1702 goto out; 1703 1704 for (name = xattr_list; name - xattr_list < xattr_list_len; 1705 name += strlen(name) + 1) { 1706 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 1707 if (strncasecmp(attr_name, name, attr_name_len)) 1708 continue; 1709 1710 value_len = ksmbd_vfs_xattr_len(idmap, dentry, name); 1711 break; 1712 } 1713 1714 out: 1715 kvfree(xattr_list); 1716 return value_len; 1717 } 1718 1719 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name, 1720 size_t *xattr_stream_name_size, int s_type) 1721 { 1722 char *type, *buf; 1723 1724 if (s_type == DIR_STREAM) 1725 type = ":$INDEX_ALLOCATION"; 1726 else 1727 type = ":$DATA"; 1728 1729 buf = kasprintf(GFP_KERNEL, "%s%s%s", 1730 XATTR_NAME_STREAM, stream_name, type); 1731 if (!buf) 1732 return -ENOMEM; 1733 1734 *xattr_stream_name = buf; 1735 *xattr_stream_name_size = strlen(buf) + 1; 1736 1737 return 0; 1738 } 1739 1740 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work, 1741 struct ksmbd_file *src_fp, 1742 struct ksmbd_file *dst_fp, 1743 struct srv_copychunk *chunks, 1744 unsigned int chunk_count, 1745 unsigned int *chunk_count_written, 1746 unsigned int *chunk_size_written, 1747 loff_t *total_size_written) 1748 { 1749 unsigned int i; 1750 loff_t src_off, dst_off, src_file_size; 1751 size_t len; 1752 int ret; 1753 1754 *chunk_count_written = 0; 1755 *chunk_size_written = 0; 1756 *total_size_written = 0; 1757 1758 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 1759 pr_err("no right to read(%pD)\n", src_fp->filp); 1760 return -EACCES; 1761 } 1762 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) { 1763 pr_err("no right to write(%pD)\n", dst_fp->filp); 1764 return -EACCES; 1765 } 1766 1767 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp)) 1768 return -EBADF; 1769 1770 smb_break_all_levII_oplock(work, dst_fp, 1); 1771 1772 if (!work->tcon->posix_extensions) { 1773 for (i = 0; i < chunk_count; i++) { 1774 src_off = le64_to_cpu(chunks[i].SourceOffset); 1775 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1776 len = le32_to_cpu(chunks[i].Length); 1777 1778 if (check_lock_range(src_fp->filp, src_off, 1779 src_off + len - 1, READ)) 1780 return -EAGAIN; 1781 if (check_lock_range(dst_fp->filp, dst_off, 1782 dst_off + len - 1, WRITE)) 1783 return -EAGAIN; 1784 } 1785 } 1786 1787 src_file_size = i_size_read(file_inode(src_fp->filp)); 1788 1789 for (i = 0; i < chunk_count; i++) { 1790 src_off = le64_to_cpu(chunks[i].SourceOffset); 1791 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1792 len = le32_to_cpu(chunks[i].Length); 1793 1794 if (src_off + len > src_file_size) 1795 return -E2BIG; 1796 1797 ret = vfs_copy_file_range(src_fp->filp, src_off, 1798 dst_fp->filp, dst_off, len, 0); 1799 if (ret == -EOPNOTSUPP || ret == -EXDEV) 1800 ret = vfs_copy_file_range(src_fp->filp, src_off, 1801 dst_fp->filp, dst_off, len, 1802 COPY_FILE_SPLICE); 1803 if (ret < 0) 1804 return ret; 1805 1806 *chunk_count_written += 1; 1807 *total_size_written += ret; 1808 } 1809 return 0; 1810 } 1811 1812 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock) 1813 { 1814 wait_event(flock->fl_wait, !flock->fl_blocker); 1815 } 1816 1817 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout) 1818 { 1819 return wait_event_interruptible_timeout(flock->fl_wait, 1820 !flock->fl_blocker, 1821 timeout); 1822 } 1823 1824 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock) 1825 { 1826 locks_delete_block(flock); 1827 } 1828 1829 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap, 1830 struct path *path) 1831 { 1832 struct posix_acl_state acl_state; 1833 struct posix_acl *acls; 1834 struct dentry *dentry = path->dentry; 1835 struct inode *inode = d_inode(dentry); 1836 int rc; 1837 1838 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1839 return -EOPNOTSUPP; 1840 1841 ksmbd_debug(SMB, "Set posix acls\n"); 1842 rc = init_acl_state(&acl_state, 1); 1843 if (rc) 1844 return rc; 1845 1846 /* Set default owner group */ 1847 acl_state.owner.allow = (inode->i_mode & 0700) >> 6; 1848 acl_state.group.allow = (inode->i_mode & 0070) >> 3; 1849 acl_state.other.allow = inode->i_mode & 0007; 1850 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid; 1851 acl_state.users->aces[acl_state.users->n++].perms.allow = 1852 acl_state.owner.allow; 1853 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid; 1854 acl_state.groups->aces[acl_state.groups->n++].perms.allow = 1855 acl_state.group.allow; 1856 acl_state.mask.allow = 0x07; 1857 1858 acls = posix_acl_alloc(6, GFP_KERNEL); 1859 if (!acls) { 1860 free_acl_state(&acl_state); 1861 return -ENOMEM; 1862 } 1863 posix_state_to_acl(&acl_state, acls->a_entries); 1864 1865 rc = mnt_want_write(path->mnt); 1866 if (rc) 1867 goto out_err; 1868 1869 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1870 if (rc < 0) 1871 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1872 rc); 1873 else if (S_ISDIR(inode->i_mode)) { 1874 posix_state_to_acl(&acl_state, acls->a_entries); 1875 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls); 1876 if (rc < 0) 1877 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1878 rc); 1879 } 1880 mnt_drop_write(path->mnt); 1881 1882 out_err: 1883 free_acl_state(&acl_state); 1884 posix_acl_release(acls); 1885 return rc; 1886 } 1887 1888 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap, 1889 struct path *path, struct inode *parent_inode) 1890 { 1891 struct posix_acl *acls; 1892 struct posix_acl_entry *pace; 1893 struct dentry *dentry = path->dentry; 1894 struct inode *inode = d_inode(dentry); 1895 int rc, i; 1896 1897 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1898 return -EOPNOTSUPP; 1899 1900 acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT); 1901 if (IS_ERR_OR_NULL(acls)) 1902 return -ENOENT; 1903 pace = acls->a_entries; 1904 1905 for (i = 0; i < acls->a_count; i++, pace++) { 1906 if (pace->e_tag == ACL_MASK) { 1907 pace->e_perm = 0x07; 1908 break; 1909 } 1910 } 1911 1912 rc = mnt_want_write(path->mnt); 1913 if (rc) 1914 goto out_err; 1915 1916 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1917 if (rc < 0) 1918 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1919 rc); 1920 if (S_ISDIR(inode->i_mode)) { 1921 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, 1922 acls); 1923 if (rc < 0) 1924 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1925 rc); 1926 } 1927 mnt_drop_write(path->mnt); 1928 1929 out_err: 1930 posix_acl_release(acls); 1931 return rc; 1932 } 1933