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