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