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