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