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