vfs.c 43.3 KB
Newer Older
N
Namjae Jeon 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
 *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
 */

#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/backing-dev.h>
#include <linux/writeback.h>
#include <linux/xattr.h>
#include <linux/falloc.h>
#include <linux/genhd.h>
#include <linux/fsnotify.h>
#include <linux/dcache.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/sched/xacct.h>
#include <linux/crc32c.h>

#include "glob.h"
#include "oplock.h"
#include "connection.h"
#include "vfs.h"
#include "vfs_cache.h"
#include "smbacl.h"
#include "ndr.h"
#include "auth.h"
30
#include "misc.h"
N
Namjae Jeon 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include "smb_common.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/user_session.h"
#include "mgmt/user_config.h"

static char *extract_last_component(char *path)
{
	char *p = strrchr(path, '/');

	if (p && p[1] != '\0') {
		*p = '\0';
		p++;
	} else {
		p = NULL;
N
Namjae Jeon 已提交
47
		pr_err("Invalid path %s\n", path);
N
Namjae Jeon 已提交
48 49 50 51 52
	}
	return p;
}

static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
53 54
				    struct inode *parent_inode,
				    struct inode *inode)
N
Namjae Jeon 已提交
55 56
{
	if (!test_share_config_flag(work->tcon->share_conf,
57
				    KSMBD_SHARE_FLAG_INHERIT_OWNER))
N
Namjae Jeon 已提交
58 59 60 61 62 63 64
		return;

	i_uid_write(inode, i_uid_read(parent_inode));
}

int ksmbd_vfs_inode_permission(struct dentry *dentry, int acc_mode, bool delete)
{
65
	int mask, ret = 0;
N
Namjae Jeon 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

	mask = 0;
	acc_mode &= O_ACCMODE;

	if (acc_mode == O_RDONLY)
		mask = MAY_READ;
	else if (acc_mode == O_WRONLY)
		mask = MAY_WRITE;
	else if (acc_mode == O_RDWR)
		mask = MAY_READ | MAY_WRITE;

	if (inode_permission(&init_user_ns, d_inode(dentry), mask | MAY_OPEN))
		return -EACCES;

	if (delete) {
81
		struct dentry *child, *parent;
N
Namjae Jeon 已提交
82 83

		parent = dget_parent(dentry);
84 85
		inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
		child = lookup_one_len(dentry->d_name.name, parent,
86
				       dentry->d_name.len);
87 88 89 90 91 92 93 94 95 96 97
		if (IS_ERR(child)) {
			ret = PTR_ERR(child);
			goto out_lock;
		}

		if (child != dentry) {
			ret = -ESTALE;
			dput(child);
			goto out_lock;
		}
		dput(child);
N
Namjae Jeon 已提交
98 99

		if (inode_permission(&init_user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE)) {
100 101
			ret = -EACCES;
			goto out_lock;
N
Namjae Jeon 已提交
102
		}
103 104
out_lock:
		inode_unlock(d_inode(parent));
N
Namjae Jeon 已提交
105 106
		dput(parent);
	}
107
	return ret;
N
Namjae Jeon 已提交
108 109 110 111
}

int ksmbd_vfs_query_maximal_access(struct dentry *dentry, __le32 *daccess)
{
112 113
	struct dentry *parent, *child;
	int ret = 0;
N
Namjae Jeon 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	*daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);

	if (!inode_permission(&init_user_ns, d_inode(dentry), MAY_OPEN | MAY_WRITE))
		*daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
				FILE_WRITE_DATA | FILE_APPEND_DATA |
				FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
				FILE_DELETE_CHILD);

	if (!inode_permission(&init_user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
		*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;

	if (!inode_permission(&init_user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
		*daccess |= FILE_EXECUTE_LE;

	parent = dget_parent(dentry);
130 131
	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
	child = lookup_one_len(dentry->d_name.name, parent,
132
			       dentry->d_name.len);
133 134 135 136 137 138 139 140 141 142 143
	if (IS_ERR(child)) {
		ret = PTR_ERR(child);
		goto out_lock;
	}

	if (child != dentry) {
		ret = -ESTALE;
		dput(child);
		goto out_lock;
	}
	dput(child);
N
Namjae Jeon 已提交
144 145 146

	if (!inode_permission(&init_user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
		*daccess |= FILE_DELETE_LE;
147 148 149

out_lock:
	inode_unlock(d_inode(parent));
N
Namjae Jeon 已提交
150
	dput(parent);
151
	return ret;
N
Namjae Jeon 已提交
152 153 154 155 156 157 158 159 160 161
}

/**
 * ksmbd_vfs_create() - vfs helper for smb create file
 * @work:	work
 * @name:	file name
 * @mode:	file create mode
 *
 * Return:	0 on success, otherwise error
 */
162
int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
N
Namjae Jeon 已提交
163 164 165 166 167 168 169 170 171
{
	struct path path;
	struct dentry *dentry;
	int err;

	dentry = kern_path_create(AT_FDCWD, name, &path, 0);
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
		if (err != -ENOENT)
N
Namjae Jeon 已提交
172 173
			pr_err("path create failed for %s, err %d\n",
			       name, err);
N
Namjae Jeon 已提交
174 175 176 177 178 179 180
		return err;
	}

	mode |= S_IFREG;
	err = vfs_create(&init_user_ns, d_inode(path.dentry), dentry, mode, true);
	if (!err) {
		ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
181
					d_inode(dentry));
N
Namjae Jeon 已提交
182
	} else {
N
Namjae Jeon 已提交
183
		pr_err("File(%s): creation failed (err:%d)\n", name, err);
N
Namjae Jeon 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
	}
	done_path_create(&path, dentry);
	return err;
}

/**
 * ksmbd_vfs_mkdir() - vfs helper for smb create directory
 * @work:	work
 * @name:	directory name
 * @mode:	directory create mode
 *
 * Return:	0 on success, otherwise error
 */
197
int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
N
Namjae Jeon 已提交
198 199 200 201 202 203 204 205 206 207
{
	struct path path;
	struct dentry *dentry;
	int err;

	dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
		if (err != -EEXIST)
			ksmbd_debug(VFS, "path create failed for %s, err %d\n",
208
				    name, err);
N
Namjae Jeon 已提交
209 210 211 212 213
		return err;
	}

	mode |= S_IFDIR;
	err = vfs_mkdir(&init_user_ns, d_inode(path.dentry), dentry, mode);
214
	if (err) {
215
		goto out;
216
	} else if (d_unhashed(dentry)) {
217 218
		struct dentry *d;

219 220
		d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
				   dentry->d_name.len);
221 222 223 224 225 226 227 228 229 230
		if (IS_ERR(d)) {
			err = PTR_ERR(d);
			goto out;
		}
		if (unlikely(d_is_negative(d))) {
			dput(d);
			err = -ENOENT;
			goto out;
		}

231
		ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
232
		dput(d);
233
	}
234
out:
N
Namjae Jeon 已提交
235
	done_path_create(&path, dentry);
236
	if (err)
N
Namjae Jeon 已提交
237
		pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
N
Namjae Jeon 已提交
238 239 240
	return err;
}

241
static ssize_t ksmbd_vfs_getcasexattr(struct dentry *dentry, char *attr_name,
242
				      int attr_name_len, char **attr_value)
N
Namjae Jeon 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
{
	char *name, *xattr_list = NULL;
	ssize_t value_len = -ENOENT, xattr_list_len;

	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
	if (xattr_list_len <= 0)
		goto out;

	for (name = xattr_list; name - xattr_list < xattr_list_len;
			name += strlen(name) + 1) {
		ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
		if (strncasecmp(attr_name, name, attr_name_len))
			continue;

		value_len = ksmbd_vfs_getxattr(dentry,
					       name,
					       attr_value);
		if (value_len < 0)
N
Namjae Jeon 已提交
261
			pr_err("failed to get xattr in file\n");
N
Namjae Jeon 已提交
262 263 264 265
		break;
	}

out:
266
	kvfree(xattr_list);
N
Namjae Jeon 已提交
267 268 269 270
	return value_len;
}

static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
271
				 size_t count)
N
Namjae Jeon 已提交
272 273 274 275 276
{
	ssize_t v_len;
	char *stream_buf = NULL;

	ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
277
		    *pos, count);
N
Namjae Jeon 已提交
278 279 280 281 282

	v_len = ksmbd_vfs_getcasexattr(fp->filp->f_path.dentry,
				       fp->stream.name,
				       fp->stream.size,
				       &stream_buf);
283 284
	if ((int)v_len <= 0)
		return (int)v_len;
N
Namjae Jeon 已提交
285

286 287 288 289 290 291 292 293
	if (v_len <= *pos) {
		count = -EINVAL;
		goto free_buf;
	}

	if (v_len - *pos < count)
		count = v_len - *pos;

N
Namjae Jeon 已提交
294
	memcpy(buf, &stream_buf[*pos], count);
295 296

free_buf:
297
	kvfree(stream_buf);
298
	return count;
N
Namjae Jeon 已提交
299 300 301 302 303 304 305 306 307 308 309
}

/**
 * check_lock_range() - vfs helper for smb byte range file locking
 * @filp:	the file to apply the lock to
 * @start:	lock start byte offset
 * @end:	lock end byte offset
 * @type:	byte range type read/write
 *
 * Return:	0 on success, otherwise error
 */
310
static int check_lock_range(struct file *filp, loff_t start, loff_t end,
311
			    unsigned char type)
N
Namjae Jeon 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
	struct file_lock *flock;
	struct file_lock_context *ctx = file_inode(filp)->i_flctx;
	int error = 0;

	if (!ctx || list_empty_careful(&ctx->flc_posix))
		return 0;

	spin_lock(&ctx->flc_lock);
	list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
		/* check conflict locks */
		if (flock->fl_end >= start && end >= flock->fl_start) {
			if (flock->fl_type == F_RDLCK) {
				if (type == WRITE) {
N
Namjae Jeon 已提交
326
					pr_err("not allow write by shared lock\n");
N
Namjae Jeon 已提交
327 328 329 330 331 332 333
					error = 1;
					goto out;
				}
			} else if (flock->fl_type == F_WRLCK) {
				/* check owner in lock */
				if (flock->fl_file != filp) {
					error = 1;
N
Namjae Jeon 已提交
334
					pr_err("not allow rw access by exclusive lock from other opens\n");
N
Namjae Jeon 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
					goto out;
				}
			}
		}
	}
out:
	spin_unlock(&ctx->flc_lock);
	return error;
}

/**
 * ksmbd_vfs_read() - vfs helper for smb file read
 * @work:	smb work
 * @fid:	file id of open file
 * @count:	read byte count
 * @pos:	file pos
 *
 * Return:	number of read bytes on success, otherwise error
 */
354
int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
355
		   loff_t *pos)
N
Namjae Jeon 已提交
356
{
357
	struct file *filp = fp->filp;
N
Namjae Jeon 已提交
358
	ssize_t nbytes = 0;
359 360
	char *rbuf = work->aux_payload_buf;
	struct inode *inode = file_inode(filp);
N
Namjae Jeon 已提交
361 362 363 364 365 366 367 368 369

	if (S_ISDIR(inode->i_mode))
		return -EISDIR;

	if (unlikely(count == 0))
		return 0;

	if (work->conn->connection_type) {
		if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
N
Namjae Jeon 已提交
370
			pr_err("no right to read(%s)\n", FP_FILENAME(fp));
N
Namjae Jeon 已提交
371 372 373 374 375 376 377
			return -EACCES;
		}
	}

	if (ksmbd_stream_fd(fp))
		return ksmbd_vfs_stream_read(fp, rbuf, pos, count);

378 379 380
	if (!work->tcon->posix_extensions) {
		int ret;

381
		ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
382
		if (ret) {
N
Namjae Jeon 已提交
383
			pr_err("unable to read due to lock\n");
384 385
			return -EAGAIN;
		}
N
Namjae Jeon 已提交
386 387 388 389
	}

	nbytes = kernel_read(filp, rbuf, count, pos);
	if (nbytes < 0) {
N
Namjae Jeon 已提交
390 391
		pr_err("smb read failed for (%s), err = %zd\n",
		       fp->filename, nbytes);
N
Namjae Jeon 已提交
392 393 394 395 396 397 398 399
		return nbytes;
	}

	filp->f_pos = *pos;
	return nbytes;
}

static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
400
				  size_t count)
N
Namjae Jeon 已提交
401 402 403 404 405 406
{
	char *stream_buf = NULL, *wbuf;
	size_t size, v_len;
	int err = 0;

	ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
407
		    *pos, count);
N
Namjae Jeon 已提交
408 409 410 411 412 413 414 415 416 417 418

	size = *pos + count;
	if (size > XATTR_SIZE_MAX) {
		size = XATTR_SIZE_MAX;
		count = (*pos + count) - XATTR_SIZE_MAX;
	}

	v_len = ksmbd_vfs_getcasexattr(fp->filp->f_path.dentry,
				       fp->stream.name,
				       fp->stream.size,
				       &stream_buf);
419
	if ((int)v_len < 0) {
N
Namjae Jeon 已提交
420
		pr_err("not found stream in xattr : %zd\n", v_len);
421
		err = (int)v_len;
N
Namjae Jeon 已提交
422 423 424 425
		goto out;
	}

	if (v_len < size) {
426
		wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
N
Namjae Jeon 已提交
427 428 429 430 431 432 433
		if (!wbuf) {
			err = -ENOMEM;
			goto out;
		}

		if (v_len > 0)
			memcpy(wbuf, stream_buf, v_len);
434
		kvfree(stream_buf);
N
Namjae Jeon 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		stream_buf = wbuf;
	}

	memcpy(&stream_buf[*pos], buf, count);

	err = ksmbd_vfs_setxattr(fp->filp->f_path.dentry,
				 fp->stream.name,
				 (void *)stream_buf,
				 size,
				 0);
	if (err < 0)
		goto out;

	fp->filp->f_pos = *pos;
	err = 0;
out:
451
	kvfree(stream_buf);
N
Namjae Jeon 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	return err;
}

/**
 * ksmbd_vfs_write() - vfs helper for smb file write
 * @work:	work
 * @fid:	file id of open file
 * @buf:	buf containing data for writing
 * @count:	read byte count
 * @pos:	file pos
 * @sync:	fsync after write
 * @written:	number of bytes written
 *
 * Return:	0 on success, otherwise error
 */
int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
468 469
		    char *buf, size_t count, loff_t *pos, bool sync,
		    ssize_t *written)
N
Namjae Jeon 已提交
470 471 472 473 474 475 476 477
{
	struct ksmbd_session *sess = work->sess;
	struct file *filp;
	loff_t	offset = *pos;
	int err = 0;

	if (sess->conn->connection_type) {
		if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
N
Namjae Jeon 已提交
478
			pr_err("no right to write(%s)\n", FP_FILENAME(fp));
N
Namjae Jeon 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492
			err = -EACCES;
			goto out;
		}
	}

	filp = fp->filp;

	if (ksmbd_stream_fd(fp)) {
		err = ksmbd_vfs_stream_write(fp, buf, pos, count);
		if (!err)
			*written = count;
		goto out;
	}

493 494 495
	if (!work->tcon->posix_extensions) {
		err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
		if (err) {
N
Namjae Jeon 已提交
496
			pr_err("unable to write due to lock\n");
497 498 499
			err = -EAGAIN;
			goto out;
		}
N
Namjae Jeon 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	}

	/* Do we need to break any of a levelII oplock? */
	smb_break_all_levII_oplock(work, fp, 1);

	err = kernel_write(filp, buf, count, pos);
	if (err < 0) {
		ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
		goto out;
	}

	filp->f_pos = *pos;
	*written = err;
	err = 0;
	if (sync) {
		err = vfs_fsync_range(filp, offset, offset + *written, 0);
		if (err < 0)
N
Namjae Jeon 已提交
517 518
			pr_err("fsync failed for filename = %s, err = %d\n",
			       FP_FILENAME(fp), err);
N
Namjae Jeon 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	}

out:
	return err;
}

/**
 * ksmbd_vfs_getattr() - vfs helper for smb getattr
 * @work:	work
 * @fid:	file id of open file
 * @attrs:	inode attributes
 *
 * Return:	0 on success, otherwise error
 */
int ksmbd_vfs_getattr(struct path *path, struct kstat *stat)
{
	int err;

	err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
	if (err)
N
Namjae Jeon 已提交
539
		pr_err("getattr failed, err %d\n", err);
N
Namjae Jeon 已提交
540 541 542 543 544 545 546 547 548 549
	return err;
}

/**
 * ksmbd_vfs_fsync() - vfs helper for smb fsync
 * @work:	work
 * @fid:	file id of open file
 *
 * Return:	0 on success, otherwise error
 */
550
int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
N
Namjae Jeon 已提交
551 552 553 554 555 556
{
	struct ksmbd_file *fp;
	int err;

	fp = ksmbd_lookup_fd_slow(work, fid, p_id);
	if (!fp) {
N
Namjae Jeon 已提交
557
		pr_err("failed to get filp for fid %llu\n", fid);
N
Namjae Jeon 已提交
558 559 560 561
		return -ENOENT;
	}
	err = vfs_fsync(fp->filp, 0);
	if (err < 0)
N
Namjae Jeon 已提交
562
		pr_err("smb fsync failed, err = %d\n", err);
N
Namjae Jeon 已提交
563 564 565 566 567 568 569 570 571 572 573 574
	ksmbd_fd_put(work, fp);
	return err;
}

/**
 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
 * @name:	absolute directory or file name
 *
 * Return:	0 on success, otherwise error
 */
int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name)
{
575 576
	struct path path;
	struct dentry *dentry, *parent;
577
	int err;
578
	int flags = 0;
N
Namjae Jeon 已提交
579 580 581 582

	if (ksmbd_override_fsids(work))
		return -ENOMEM;

583 584 585 586 587
	if (test_share_config_flag(work->tcon->share_conf,
				   KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
		flags = LOOKUP_FOLLOW;

	err = kern_path(name, flags, &path);
N
Namjae Jeon 已提交
588 589 590 591 592 593
	if (err) {
		ksmbd_debug(VFS, "can't get %s, err %d\n", name, err);
		ksmbd_revert_fsids(work);
		return err;
	}

594 595 596
	parent = dget_parent(path.dentry);
	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
	dentry = lookup_one_len(path.dentry->d_name.name, parent,
597
				strlen(path.dentry->d_name.name));
N
Namjae Jeon 已提交
598 599
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
600
		ksmbd_debug(VFS, "%s: lookup failed, err %d\n",
601
			    path.dentry->d_name.name, err);
N
Namjae Jeon 已提交
602 603 604 605 606 607 608 609 610 611
		goto out_err;
	}

	if (!d_inode(dentry) || !d_inode(dentry)->i_nlink) {
		dput(dentry);
		err = -ENOENT;
		goto out_err;
	}

	if (S_ISDIR(d_inode(dentry)->i_mode)) {
612
		err = vfs_rmdir(&init_user_ns, d_inode(parent), dentry);
N
Namjae Jeon 已提交
613 614
		if (err && err != -ENOTEMPTY)
			ksmbd_debug(VFS, "%s: rmdir failed, err %d\n", name,
615
				    err);
N
Namjae Jeon 已提交
616
	} else {
617
		err = vfs_unlink(&init_user_ns, d_inode(parent), dentry, NULL);
N
Namjae Jeon 已提交
618 619
		if (err)
			ksmbd_debug(VFS, "%s: unlink failed, err %d\n", name,
620
				    err);
N
Namjae Jeon 已提交
621 622 623 624
	}

	dput(dentry);
out_err:
625 626 627
	inode_unlock(d_inode(parent));
	dput(parent);
	path_put(&path);
N
Namjae Jeon 已提交
628 629 630 631 632 633 634 635 636 637 638
	ksmbd_revert_fsids(work);
	return err;
}

/**
 * ksmbd_vfs_link() - vfs helper for creating smb hardlink
 * @oldname:	source file name
 * @newname:	hardlink name
 *
 * Return:	0 on success, otherwise error
 */
639
int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
640
		   const char *newname)
N
Namjae Jeon 已提交
641 642 643 644
{
	struct path oldpath, newpath;
	struct dentry *dentry;
	int err;
645
	int flags = 0;
N
Namjae Jeon 已提交
646 647 648 649

	if (ksmbd_override_fsids(work))
		return -ENOMEM;

650 651 652 653 654
	if (test_share_config_flag(work->tcon->share_conf,
				   KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
		flags = LOOKUP_FOLLOW;

	err = kern_path(oldname, flags, &oldpath);
N
Namjae Jeon 已提交
655
	if (err) {
N
Namjae Jeon 已提交
656 657
		pr_err("cannot get linux path for %s, err = %d\n",
		       oldname, err);
N
Namjae Jeon 已提交
658 659 660 661
		goto out1;
	}

	dentry = kern_path_create(AT_FDCWD, newname, &newpath,
662
				  flags | LOOKUP_REVAL);
N
Namjae Jeon 已提交
663 664
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
N
Namjae Jeon 已提交
665
		pr_err("path create err for %s, err %d\n", newname, err);
N
Namjae Jeon 已提交
666 667 668 669 670
		goto out2;
	}

	err = -EXDEV;
	if (oldpath.mnt != newpath.mnt) {
N
Namjae Jeon 已提交
671
		pr_err("vfs_link failed err %d\n", err);
N
Namjae Jeon 已提交
672 673 674 675
		goto out3;
	}

	err = vfs_link(oldpath.dentry, &init_user_ns, d_inode(newpath.dentry),
676
		       dentry, NULL);
N
Namjae Jeon 已提交
677 678 679 680 681 682 683 684 685 686 687 688
	if (err)
		ksmbd_debug(VFS, "vfs_link failed err %d\n", err);

out3:
	done_path_create(&newpath, dentry);
out2:
	path_put(&oldpath);
out1:
	ksmbd_revert_fsids(work);
	return err;
}

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
static int ksmbd_validate_entry_in_use(struct dentry *src_dent)
{
	struct dentry *dst_dent;

	spin_lock(&src_dent->d_lock);
	list_for_each_entry(dst_dent, &src_dent->d_subdirs, d_child) {
		struct ksmbd_file *child_fp;

		if (d_really_is_negative(dst_dent))
			continue;

		child_fp = ksmbd_lookup_fd_inode(d_inode(dst_dent));
		if (child_fp) {
			spin_unlock(&src_dent->d_lock);
			ksmbd_debug(VFS, "Forbid rename, sub file/dir is in use\n");
			return -EACCES;
		}
	}
	spin_unlock(&src_dent->d_lock);

	return 0;
}

N
Namjae Jeon 已提交
712
static int __ksmbd_vfs_rename(struct ksmbd_work *work,
713 714 715 716 717
			      struct dentry *src_dent_parent,
			      struct dentry *src_dent,
			      struct dentry *dst_dent_parent,
			      struct dentry *trap_dent,
			      char *dst_name)
N
Namjae Jeon 已提交
718 719 720 721
{
	struct dentry *dst_dent;
	int err;

722
	if (!work->tcon->posix_extensions) {
723 724 725
		err = ksmbd_validate_entry_in_use(src_dent);
		if (err)
			return err;
N
Namjae Jeon 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	}

	if (d_really_is_negative(src_dent_parent))
		return -ENOENT;
	if (d_really_is_negative(dst_dent_parent))
		return -ENOENT;
	if (d_really_is_negative(src_dent))
		return -ENOENT;
	if (src_dent == trap_dent)
		return -EINVAL;

	if (ksmbd_override_fsids(work))
		return -ENOMEM;

	dst_dent = lookup_one_len(dst_name, dst_dent_parent, strlen(dst_name));
	err = PTR_ERR(dst_dent);
	if (IS_ERR(dst_dent)) {
N
Namjae Jeon 已提交
743
		pr_err("lookup failed %s [%d]\n", dst_name, err);
N
Namjae Jeon 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
		goto out;
	}

	err = -ENOTEMPTY;
	if (dst_dent != trap_dent && !d_really_is_positive(dst_dent)) {
		struct renamedata rd = {
			.old_mnt_userns	= &init_user_ns,
			.old_dir	= d_inode(src_dent_parent),
			.old_dentry	= src_dent,
			.new_mnt_userns	= &init_user_ns,
			.new_dir	= d_inode(dst_dent_parent),
			.new_dentry	= dst_dent,
		};
		err = vfs_rename(&rd);
	}
	if (err)
N
Namjae Jeon 已提交
760
		pr_err("vfs_rename failed err %d\n", err);
N
Namjae Jeon 已提交
761 762 763 764 765 766 767 768
	if (dst_dent)
		dput(dst_dent);
out:
	ksmbd_revert_fsids(work);
	return err;
}

int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
769
			char *newname)
N
Namjae Jeon 已提交
770 771 772
{
	struct path dst_path;
	struct dentry *src_dent_parent, *dst_dent_parent;
773
	struct dentry *src_dent, *trap_dent, *src_child;
N
Namjae Jeon 已提交
774 775
	char *dst_name;
	int err;
776
	int flags;
N
Namjae Jeon 已提交
777 778 779 780 781 782 783 784

	dst_name = extract_last_component(newname);
	if (!dst_name)
		return -EINVAL;

	src_dent_parent = dget_parent(fp->filp->f_path.dentry);
	src_dent = fp->filp->f_path.dentry;

785 786 787 788 789 790
	flags = LOOKUP_DIRECTORY;
	if (test_share_config_flag(work->tcon->share_conf,
				   KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
		flags |= LOOKUP_FOLLOW;

	err = kern_path(newname, flags, &dst_path);
N
Namjae Jeon 已提交
791 792 793 794 795 796 797
	if (err) {
		ksmbd_debug(VFS, "Cannot get path for %s [%d]\n", newname, err);
		goto out;
	}
	dst_dent_parent = dst_path.dentry;

	trap_dent = lock_rename(src_dent_parent, dst_dent_parent);
798 799 800
	dget(src_dent);
	dget(dst_dent_parent);
	src_child = lookup_one_len(src_dent->d_name.name, src_dent_parent,
801
				   src_dent->d_name.len);
802 803 804 805 806 807 808 809 810 811 812 813
	if (IS_ERR(src_child)) {
		err = PTR_ERR(src_child);
		goto out_lock;
	}

	if (src_child != src_dent) {
		err = -ESTALE;
		dput(src_child);
		goto out_lock;
	}
	dput(src_child);

N
Namjae Jeon 已提交
814 815 816 817 818 819
	err = __ksmbd_vfs_rename(work,
				 src_dent_parent,
				 src_dent,
				 dst_dent_parent,
				 trap_dent,
				 dst_name);
820 821
out_lock:
	dput(src_dent);
N
Namjae Jeon 已提交
822
	dput(dst_dent_parent);
823
	unlock_rename(src_dent_parent, dst_dent_parent);
N
Namjae Jeon 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
	path_put(&dst_path);
out:
	dput(src_dent_parent);
	return err;
}

/**
 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
 * @work:	work
 * @name:	old filename
 * @fid:	file id of old file
 * @size:	truncate to given size
 *
 * Return:	0 on success, otherwise error
 */
int ksmbd_vfs_truncate(struct ksmbd_work *work, const char *name,
840
		       struct ksmbd_file *fp, loff_t size)
N
Namjae Jeon 已提交
841 842 843 844 845 846 847
{
	struct path path;
	int err = 0;

	if (name) {
		err = kern_path(name, 0, &path);
		if (err) {
N
Namjae Jeon 已提交
848 849
			pr_err("cannot get linux path for %s, err %d\n",
			       name, err);
N
Namjae Jeon 已提交
850 851 852 853
			return err;
		}
		err = vfs_truncate(&path, size);
		if (err)
N
Namjae Jeon 已提交
854 855
			pr_err("truncate failed for %s err %d\n",
			       name, err);
N
Namjae Jeon 已提交
856 857 858 859 860 861 862 863 864
		path_put(&path);
	} else {
		struct file *filp;

		filp = fp->filp;

		/* Do we need to break any of a levelII oplock? */
		smb_break_all_levII_oplock(work, fp, 1);

865 866
		if (!work->tcon->posix_extensions) {
			struct inode *inode = file_inode(filp);
N
Namjae Jeon 已提交
867

868 869
			if (size < inode->i_size) {
				err = check_lock_range(filp, size,
870
						       inode->i_size - 1, WRITE);
871 872
			} else {
				err = check_lock_range(filp, inode->i_size,
873
						       size - 1, WRITE);
874 875 876
			}

			if (err) {
N
Namjae Jeon 已提交
877
				pr_err("failed due to lock\n");
878 879
				return -EAGAIN;
			}
N
Namjae Jeon 已提交
880 881 882 883
		}

		err = vfs_truncate(&filp->f_path, size);
		if (err)
N
Namjae Jeon 已提交
884 885
			pr_err("truncate failed for filename : %s err %d\n",
			       fp->filename, err);
N
Namjae Jeon 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
	}

	return err;
}

/**
 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
 * @dentry:	dentry of file for listing xattrs
 * @list:	destination buffer
 * @size:	destination buffer length
 *
 * Return:	xattr list length on success, otherwise error
 */
ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
{
	ssize_t size;
	char *vlist = NULL;

	size = vfs_listxattr(dentry, NULL, 0);
	if (size <= 0)
		return size;

908
	vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
N
Namjae Jeon 已提交
909 910 911 912 913 914 915
	if (!vlist)
		return -ENOMEM;

	*list = vlist;
	size = vfs_listxattr(dentry, vlist, size);
	if (size < 0) {
		ksmbd_debug(VFS, "listxattr failed\n");
916
		kvfree(vlist);
N
Namjae Jeon 已提交
917 918 919 920 921 922
		*list = NULL;
	}

	return size;
}

923
static ssize_t ksmbd_vfs_xattr_len(struct dentry *dentry, char *xattr_name)
N
Namjae Jeon 已提交
924 925 926 927 928 929 930 931 932 933 934 935
{
	return vfs_getxattr(&init_user_ns, dentry, xattr_name, NULL, 0);
}

/**
 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
 * @dentry:	dentry of file for getting xattrs
 * @xattr_name:	name of xattr name to query
 * @xattr_buf:	destination buffer xattr value
 *
 * Return:	read xattr value length on success, otherwise error
 */
936
ssize_t ksmbd_vfs_getxattr(struct dentry *dentry, char *xattr_name,
937
			   char **xattr_buf)
N
Namjae Jeon 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950
{
	ssize_t xattr_len;
	char *buf;

	*xattr_buf = NULL;
	xattr_len = ksmbd_vfs_xattr_len(dentry, xattr_name);
	if (xattr_len < 0)
		return xattr_len;

	buf = kmalloc(xattr_len + 1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

951 952
	xattr_len = vfs_getxattr(&init_user_ns, dentry, xattr_name,
				 (void *)buf, xattr_len);
N
Namjae Jeon 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	if (xattr_len > 0)
		*xattr_buf = buf;
	else
		kfree(buf);
	return xattr_len;
}

/**
 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
 * @dentry:	dentry to set XATTR at
 * @name:	xattr name for setxattr
 * @value:	xattr value to set
 * @size:	size of xattr value
 * @flags:	destination buffer length
 *
 * Return:	0 on success, otherwise error
 */
970
int ksmbd_vfs_setxattr(struct dentry *dentry, const char *attr_name,
971
		       const void *attr_value, size_t attr_size, int flags)
N
Namjae Jeon 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
{
	int err;

	err = vfs_setxattr(&init_user_ns, dentry,
			   attr_name,
			   attr_value,
			   attr_size,
			   flags);
	if (err)
		ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
	return err;
}

/**
 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
 * @filp:	file pointer for IO
 * @options:	smb IO options
 */
void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
{
	struct address_space *mapping;

	mapping = filp->f_mapping;

	if (!option || !mapping)
		return;

999
	if (option & FILE_WRITE_THROUGH_LE) {
N
Namjae Jeon 已提交
1000
		filp->f_flags |= O_SYNC;
1001
	} else if (option & FILE_SEQUENTIAL_ONLY_LE) {
N
Namjae Jeon 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
		spin_lock(&filp->f_lock);
		filp->f_mode &= ~FMODE_RANDOM;
		spin_unlock(&filp->f_lock);
	} else if (option & FILE_RANDOM_ACCESS_LE) {
		spin_lock(&filp->f_lock);
		filp->f_mode |= FMODE_RANDOM;
		spin_unlock(&filp->f_lock);
	}
}

1013
int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
1014
			loff_t off, loff_t len)
N
Namjae Jeon 已提交
1015 1016 1017 1018
{
	smb_break_all_levII_oplock(work, fp, 1);
	if (fp->f_ci->m_fattr & ATTR_SPARSE_FILE_LE)
		return vfs_fallocate(fp->filp,
1019 1020
				     FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
				     off, len);
N
Namjae Jeon 已提交
1021 1022 1023 1024 1025

	return vfs_fallocate(fp->filp, FALLOC_FL_ZERO_RANGE, off, len);
}

int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
1026 1027
			 struct file_allocated_range_buffer *ranges,
			 int in_count, int *out_count)
N
Namjae Jeon 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
{
	struct file *f = fp->filp;
	struct inode *inode = FP_INODE(fp);
	loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
	loff_t extent_start, extent_end;
	int ret = 0;

	if (start > maxbytes)
		return -EFBIG;

	if (!in_count)
		return 0;

	/*
	 * Shrink request scope to what the fs can actually handle.
	 */
	if (length > maxbytes || (maxbytes - length) < start)
		length = maxbytes - start;

	if (start + length > inode->i_size)
		length = inode->i_size - start;

	*out_count = 0;
	end = start + length;
	while (start < end && *out_count < in_count) {
		extent_start = f->f_op->llseek(f, start, SEEK_DATA);
		if (extent_start < 0) {
			if (extent_start != -ENXIO)
				ret = (int)extent_start;
			break;
		}

		if (extent_start >= end)
			break;

		extent_end = f->f_op->llseek(f, extent_start, SEEK_HOLE);
		if (extent_end < 0) {
			if (extent_end != -ENXIO)
				ret = (int)extent_end;
			break;
1068
		} else if (extent_start >= extent_end) {
N
Namjae Jeon 已提交
1069
			break;
1070
		}
N
Namjae Jeon 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

		ranges[*out_count].file_offset = cpu_to_le64(extent_start);
		ranges[(*out_count)++].length =
			cpu_to_le64(min(extent_end, end) - extent_start);

		start = extent_end;
	}

	return ret;
}

int ksmbd_vfs_remove_xattr(struct dentry *dentry, char *attr_name)
{
	return vfs_removexattr(&init_user_ns, dentry, attr_name);
}

int ksmbd_vfs_unlink(struct dentry *dir, struct dentry *dentry)
{
1089
	struct dentry *child;
N
Namjae Jeon 已提交
1090 1091 1092
	int err = 0;

	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
1093
	dget(dentry);
1094
	child = lookup_one_len(dentry->d_name.name, dir, dentry->d_name.len);
1095 1096 1097 1098 1099 1100 1101 1102
	if (IS_ERR(child)) {
		err = PTR_ERR(child);
		goto out;
	}

	if (child != dentry) {
		err = -ESTALE;
		dput(child);
N
Namjae Jeon 已提交
1103 1104
		goto out;
	}
1105
	dput(child);
N
Namjae Jeon 已提交
1106 1107 1108 1109 1110 1111 1112 1113

	if (S_ISDIR(d_inode(dentry)->i_mode))
		err = vfs_rmdir(&init_user_ns, d_inode(dir), dentry);
	else
		err = vfs_unlink(&init_user_ns, d_inode(dir), dentry, NULL);

out:
	dput(dentry);
1114
	inode_unlock(d_inode(dir));
N
Namjae Jeon 已提交
1115 1116 1117 1118 1119 1120
	if (err)
		ksmbd_debug(VFS, "failed to delete, err %d\n", err);

	return err;
}

1121
static int __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1122
		       loff_t offset, u64 ino, unsigned int d_type)
N
Namjae Jeon 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
{
	struct ksmbd_readdir_data *buf;

	buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
	buf->dirent_count++;

	if (buf->dirent_count > 2)
		return -ENOTEMPTY;
	return 0;
}

/**
 * ksmbd_vfs_empty_dir() - check for empty directory
 * @fp:	ksmbd file pointer
 *
 * Return:	true if directory empty, otherwise false
 */
int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
{
	int err;
	struct ksmbd_readdir_data readdir_data;

	memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));

	set_ctx_actor(&readdir_data.ctx, __dir_empty);
	readdir_data.dirent_count = 0;

1150
	err = iterate_dir(fp->filp, &readdir_data.ctx);
N
Namjae Jeon 已提交
1151 1152 1153 1154 1155 1156 1157
	if (readdir_data.dirent_count > 2)
		err = -ENOTEMPTY;
	else
		err = 0;
	return err;
}

1158
static int __caseless_lookup(struct dir_context *ctx, const char *name,
1159 1160
			     int namlen, loff_t offset, u64 ino,
			     unsigned int d_type)
N
Namjae Jeon 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
{
	struct ksmbd_readdir_data *buf;

	buf = container_of(ctx, struct ksmbd_readdir_data, ctx);

	if (buf->used != namlen)
		return 0;
	if (!strncasecmp((char *)buf->private, name, namlen)) {
		memcpy((char *)buf->private, name, namlen);
		buf->dirent_count = 1;
		return -EEXIST;
	}
	return 0;
}

/**
 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1178 1179 1180
 * @dir:	path info
 * @name:	filename to lookup
 * @namelen:	filename length
N
Namjae Jeon 已提交
1181 1182 1183
 *
 * Return:	0 on success, otherwise error
 */
1184
static int ksmbd_vfs_lookup_in_dir(struct path *dir, char *name, size_t namelen)
N
Namjae Jeon 已提交
1185 1186 1187
{
	int ret;
	struct file *dfilp;
1188
	int flags = O_RDONLY | O_LARGEFILE;
N
Namjae Jeon 已提交
1189 1190
	struct ksmbd_readdir_data readdir_data = {
		.ctx.actor	= __caseless_lookup,
1191 1192 1193
		.private	= name,
		.used		= namelen,
		.dirent_count	= 0,
N
Namjae Jeon 已提交
1194 1195
	};

1196 1197 1198
	dfilp = dentry_open(dir, flags, current_cred());
	if (IS_ERR(dfilp))
		return PTR_ERR(dfilp);
N
Namjae Jeon 已提交
1199

1200
	ret = iterate_dir(dfilp, &readdir_data.ctx);
N
Namjae Jeon 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	if (readdir_data.dirent_count > 0)
		ret = 0;
	fput(dfilp);
	return ret;
}

/**
 * ksmbd_vfs_kern_path() - lookup a file and get path info
 * @name:	name of file for lookup
 * @flags:	lookup flags
 * @path:	if lookup succeed, return path info
 * @caseless:	caseless filename lookup
 *
 * Return:	0 on success, otherwise error
 */
int ksmbd_vfs_kern_path(char *name, unsigned int flags, struct path *path,
1217
			bool caseless)
N
Namjae Jeon 已提交
1218 1219 1220
{
	int err;

1221 1222 1223
	if (name[0] != '/')
		return -EINVAL;

N
Namjae Jeon 已提交
1224 1225
	err = kern_path(name, flags, path);
	if (!err)
1226
		return 0;
N
Namjae Jeon 已提交
1227 1228

	if (caseless) {
1229 1230 1231
		char *filepath;
		struct path parent;
		size_t path_len, remain_len;
N
Namjae Jeon 已提交
1232

1233 1234 1235 1236 1237 1238
		filepath = kstrdup(name, GFP_KERNEL);
		if (!filepath)
			return -ENOMEM;

		path_len = strlen(filepath);
		remain_len = path_len - 1;
N
Namjae Jeon 已提交
1239

1240
		err = kern_path("/", flags, &parent);
N
Namjae Jeon 已提交
1241 1242 1243
		if (err)
			goto out;

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		while (d_can_lookup(parent.dentry)) {
			char *filename = filepath + path_len - remain_len;
			char *next = strchrnul(filename, '/');
			size_t filename_len = next - filename;
			bool is_last = !next[0];

			if (filename_len == 0)
				break;

			err = ksmbd_vfs_lookup_in_dir(&parent, filename,
						      filename_len);
			if (err) {
				path_put(&parent);
				goto out;
			}

			path_put(&parent);
			next[0] = '\0';

			err = kern_path(filepath, flags, &parent);
			if (err)
				goto out;

			if (is_last) {
				path->mnt = parent.mnt;
				path->dentry = parent.dentry;
				goto out;
			}

			next[0] = '/';
			remain_len -= filename_len + 1;
		}

		path_put(&parent);
		err = -EINVAL;
N
Namjae Jeon 已提交
1279
out:
1280 1281
		kfree(filepath);
	}
N
Namjae Jeon 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	return err;
}

int ksmbd_vfs_remove_acl_xattrs(struct dentry *dentry)
{
	char *name, *xattr_list = NULL;
	ssize_t xattr_list_len;
	int err = 0;

	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
	if (xattr_list_len < 0) {
		goto out;
	} else if (!xattr_list_len) {
		ksmbd_debug(SMB, "empty xattr in the file\n");
		goto out;
	}

	for (name = xattr_list; name - xattr_list < xattr_list_len;
1300
	     name += strlen(name) + 1) {
N
Namjae Jeon 已提交
1301 1302 1303
		ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));

		if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1304
			     sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
N
Namjae Jeon 已提交
1305
		    !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1306
			     sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
N
Namjae Jeon 已提交
1307 1308 1309
			err = ksmbd_vfs_remove_xattr(dentry, name);
			if (err)
				ksmbd_debug(SMB,
1310
					    "remove acl xattr failed : %s\n", name);
N
Namjae Jeon 已提交
1311 1312 1313
		}
	}
out:
1314
	kvfree(xattr_list);
N
Namjae Jeon 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	return err;
}

int ksmbd_vfs_remove_sd_xattrs(struct dentry *dentry)
{
	char *name, *xattr_list = NULL;
	ssize_t xattr_list_len;
	int err = 0;

	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
	if (xattr_list_len < 0) {
		goto out;
	} else if (!xattr_list_len) {
		ksmbd_debug(SMB, "empty xattr in the file\n");
		goto out;
	}

	for (name = xattr_list; name - xattr_list < xattr_list_len;
			name += strlen(name) + 1) {
		ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));

		if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
			err = ksmbd_vfs_remove_xattr(dentry, name);
			if (err)
				ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
		}
	}
out:
1343
	kvfree(xattr_list);
N
Namjae Jeon 已提交
1344 1345 1346 1347
	return err;
}

static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct inode *inode,
1348
							    int acl_type)
N
Namjae Jeon 已提交
1349 1350 1351 1352 1353 1354 1355
{
	struct xattr_smb_acl *smb_acl = NULL;
	struct posix_acl *posix_acls;
	struct posix_acl_entry *pa_entry;
	struct xattr_acl_entry *xa_entry;
	int i;

1356
	posix_acls = get_acl(inode, acl_type);
N
Namjae Jeon 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	if (!posix_acls)
		return NULL;

	smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
			  sizeof(struct xattr_acl_entry) * posix_acls->a_count,
			  GFP_KERNEL);
	if (!smb_acl)
		goto out;

	smb_acl->count = posix_acls->a_count;
	pa_entry = posix_acls->a_entries;
	xa_entry = smb_acl->entries;
	for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
		switch (pa_entry->e_tag) {
		case ACL_USER:
			xa_entry->type = SMB_ACL_USER;
			xa_entry->uid = from_kuid(&init_user_ns, pa_entry->e_uid);
			break;
		case ACL_USER_OBJ:
			xa_entry->type = SMB_ACL_USER_OBJ;
			break;
		case ACL_GROUP:
			xa_entry->type = SMB_ACL_GROUP;
			xa_entry->gid = from_kgid(&init_user_ns, pa_entry->e_gid);
			break;
		case ACL_GROUP_OBJ:
			xa_entry->type = SMB_ACL_GROUP_OBJ;
			break;
		case ACL_OTHER:
			xa_entry->type = SMB_ACL_OTHER;
			break;
		case ACL_MASK:
			xa_entry->type = SMB_ACL_MASK;
			break;
		default:
N
Namjae Jeon 已提交
1392
			pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
N
Namjae Jeon 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
			goto out;
		}

		if (pa_entry->e_perm & ACL_READ)
			xa_entry->perm |= SMB_ACL_READ;
		if (pa_entry->e_perm & ACL_WRITE)
			xa_entry->perm |= SMB_ACL_WRITE;
		if (pa_entry->e_perm & ACL_EXECUTE)
			xa_entry->perm |= SMB_ACL_EXECUTE;
	}
out:
	posix_acl_release(posix_acls);
	return smb_acl;
}

int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn, struct dentry *dentry,
1409
			   struct smb_ntsd *pntsd, int len)
N
Namjae Jeon 已提交
1410 1411 1412 1413 1414
{
	int rc;
	struct ndr sd_ndr = {0}, acl_ndr = {0};
	struct xattr_ntacl acl = {0};
	struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
N
Namjae Jeon 已提交
1415
	struct inode *inode = d_inode(dentry);
N
Namjae Jeon 已提交
1416 1417 1418

	acl.version = 4;
	acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
N
Namjae Jeon 已提交
1419
	acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
N
Namjae Jeon 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

	memcpy(acl.desc, "posix_acl", 9);
	acl.desc_len = 10;

	pntsd->osidoffset =
		cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
	pntsd->gsidoffset =
		cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
	pntsd->dacloffset =
		cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);

	acl.sd_buf = (char *)pntsd;
	acl.sd_size = len;

	rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
	if (rc) {
N
Namjae Jeon 已提交
1436
		pr_err("failed to generate hash for ndr acl\n");
N
Namjae Jeon 已提交
1437 1438 1439
		return rc;
	}

N
Namjae Jeon 已提交
1440
	smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode, ACL_TYPE_ACCESS);
N
Namjae Jeon 已提交
1441
	if (S_ISDIR(inode->i_mode))
N
Namjae Jeon 已提交
1442
		def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode,
1443
							     ACL_TYPE_DEFAULT);
N
Namjae Jeon 已提交
1444 1445 1446

	rc = ndr_encode_posix_acl(&acl_ndr, inode, smb_acl, def_smb_acl);
	if (rc) {
N
Namjae Jeon 已提交
1447
		pr_err("failed to encode ndr to posix acl\n");
N
Namjae Jeon 已提交
1448 1449 1450 1451
		goto out;
	}

	rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1452
			       acl.posix_acl_hash);
N
Namjae Jeon 已提交
1453
	if (rc) {
N
Namjae Jeon 已提交
1454
		pr_err("failed to generate hash for ndr acl\n");
N
Namjae Jeon 已提交
1455 1456 1457 1458 1459
		goto out;
	}

	rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
	if (rc) {
N
Namjae Jeon 已提交
1460
		pr_err("failed to encode ndr to posix acl\n");
N
Namjae Jeon 已提交
1461 1462 1463 1464
		goto out;
	}

	rc = ksmbd_vfs_setxattr(dentry, XATTR_NAME_SD, sd_ndr.data,
1465
				sd_ndr.offset, 0);
N
Namjae Jeon 已提交
1466
	if (rc < 0)
N
Namjae Jeon 已提交
1467
		pr_err("Failed to store XATTR ntacl :%d\n", rc);
N
Namjae Jeon 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

	kfree(sd_ndr.data);
out:
	kfree(acl_ndr.data);
	kfree(smb_acl);
	kfree(def_smb_acl);
	return rc;
}

int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, struct dentry *dentry,
1478
			   struct smb_ntsd **pntsd)
N
Namjae Jeon 已提交
1479 1480 1481 1482 1483 1484
{
	int rc;
	struct ndr n;

	rc = ksmbd_vfs_getxattr(dentry, XATTR_NAME_SD, &n.data);
	if (rc > 0) {
N
Namjae Jeon 已提交
1485
		struct inode *inode = d_inode(dentry);
N
Namjae Jeon 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
		struct ndr acl_ndr = {0};
		struct xattr_ntacl acl;
		struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
		__u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};

		n.length = rc;
		rc = ndr_decode_v4_ntacl(&n, &acl);
		if (rc)
			return rc;

		smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode,
1497
							 ACL_TYPE_ACCESS);
N
Namjae Jeon 已提交
1498 1499
		if (S_ISDIR(inode->i_mode))
			def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode,
1500
								     ACL_TYPE_DEFAULT);
N
Namjae Jeon 已提交
1501 1502 1503

		rc = ndr_encode_posix_acl(&acl_ndr, inode, smb_acl, def_smb_acl);
		if (rc) {
N
Namjae Jeon 已提交
1504
			pr_err("failed to encode ndr to posix acl\n");
N
Namjae Jeon 已提交
1505 1506 1507 1508
			goto out;
		}

		rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1509
				       cmp_hash);
N
Namjae Jeon 已提交
1510
		if (rc) {
N
Namjae Jeon 已提交
1511
			pr_err("failed to generate hash for ndr acl\n");
N
Namjae Jeon 已提交
1512 1513 1514 1515
			goto out;
		}

		if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
N
Namjae Jeon 已提交
1516
			pr_err("hash value diff\n");
N
Namjae Jeon 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
			rc = -EINVAL;
			goto out;
		}

		*pntsd = acl.sd_buf;
		(*pntsd)->osidoffset =
			cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) - NDR_NTSD_OFFSETOF);
		(*pntsd)->gsidoffset =
			cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) - NDR_NTSD_OFFSETOF);
		(*pntsd)->dacloffset =
			cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) - NDR_NTSD_OFFSETOF);

		rc = acl.sd_size;
out:
		kfree(n.data);
		kfree(acl_ndr.data);
		kfree(smb_acl);
		kfree(def_smb_acl);
	}

	return rc;
}

int ksmbd_vfs_set_dos_attrib_xattr(struct dentry *dentry,
1541
				   struct xattr_dos_attrib *da)
N
Namjae Jeon 已提交
1542 1543 1544 1545 1546 1547 1548 1549
{
	struct ndr n;
	int err;

	err = ndr_encode_dos_attr(&n, da);
	if (err)
		return err;

1550 1551
	err = ksmbd_vfs_setxattr(dentry, XATTR_NAME_DOS_ATTRIBUTE,
				 (void *)n.data, n.offset, 0);
N
Namjae Jeon 已提交
1552 1553 1554 1555 1556 1557 1558 1559
	if (err)
		ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
	kfree(n.data);

	return err;
}

int ksmbd_vfs_get_dos_attrib_xattr(struct dentry *dentry,
1560
				   struct xattr_dos_attrib *da)
N
Namjae Jeon 已提交
1561 1562 1563 1564
{
	struct ndr n;
	int err;

1565 1566
	err = ksmbd_vfs_getxattr(dentry, XATTR_NAME_DOS_ATTRIBUTE,
				 (char **)&n.data);
N
Namjae Jeon 已提交
1567 1568 1569 1570
	if (err > 0) {
		n.length = err;
		if (ndr_decode_dos_attr(&n, da))
			err = -EINVAL;
1571
		kfree(n.data);
1572
	} else {
N
Namjae Jeon 已提交
1573
		ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1574
	}
N
Namjae Jeon 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610

	return err;
}

/**
 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
 * @p:          destination buffer
 * @ksmbd_kstat:      ksmbd kstat wrapper
 */
void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
{
	struct file_directory_info *info = (struct file_directory_info *)(*p);
	struct kstat *kstat = ksmbd_kstat->kstat;
	u64 time;

	info->FileIndex = 0;
	info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
	time = ksmbd_UnixTimeToNT(kstat->atime);
	info->LastAccessTime = cpu_to_le64(time);
	time = ksmbd_UnixTimeToNT(kstat->mtime);
	info->LastWriteTime = cpu_to_le64(time);
	time = ksmbd_UnixTimeToNT(kstat->ctime);
	info->ChangeTime = cpu_to_le64(time);

	if (ksmbd_kstat->file_attributes & ATTR_DIRECTORY_LE) {
		info->EndOfFile = 0;
		info->AllocationSize = 0;
	} else {
		info->EndOfFile = cpu_to_le64(kstat->size);
		info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
	}
	info->ExtFileAttributes = ksmbd_kstat->file_attributes;

	return info;
}

1611
int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, struct dentry *dentry,
1612
				struct ksmbd_kstat *ksmbd_kstat)
N
Namjae Jeon 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
{
	u64 time;
	int rc;

	generic_fillattr(&init_user_ns, d_inode(dentry), ksmbd_kstat->kstat);

	time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
	ksmbd_kstat->create_time = time;

	/*
	 * set default value for the case that store dos attributes is not yes
	 * or that acl is disable in server's filesystem and the config is yes.
	 */
	if (S_ISDIR(ksmbd_kstat->kstat->mode))
		ksmbd_kstat->file_attributes = ATTR_DIRECTORY_LE;
	else
		ksmbd_kstat->file_attributes = ATTR_ARCHIVE_LE;

	if (test_share_config_flag(work->tcon->share_conf,
1632
				   KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
N
Namjae Jeon 已提交
1633 1634 1635 1636 1637 1638
		struct xattr_dos_attrib da;

		rc = ksmbd_vfs_get_dos_attrib_xattr(dentry, &da);
		if (rc > 0) {
			ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
			ksmbd_kstat->create_time = da.create_time;
1639
		} else {
N
Namjae Jeon 已提交
1640
			ksmbd_debug(VFS, "fail to load dos attribute.\n");
1641
		}
N
Namjae Jeon 已提交
1642 1643 1644 1645 1646
	}

	return 0;
}

1647
ssize_t ksmbd_vfs_casexattr_len(struct dentry *dentry, char *attr_name,
1648
				int attr_name_len)
N
Namjae Jeon 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
{
	char *name, *xattr_list = NULL;
	ssize_t value_len = -ENOENT, xattr_list_len;

	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
	if (xattr_list_len <= 0)
		goto out;

	for (name = xattr_list; name - xattr_list < xattr_list_len;
			name += strlen(name) + 1) {
		ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
		if (strncasecmp(attr_name, name, attr_name_len))
			continue;

		value_len = ksmbd_vfs_xattr_len(dentry, name);
		break;
	}

out:
1668
	kvfree(xattr_list);
N
Namjae Jeon 已提交
1669 1670 1671
	return value_len;
}

1672
int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1673
				size_t *xattr_stream_name_size, int s_type)
N
Namjae Jeon 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
{
	int stream_name_size;
	char *xattr_stream_name_buf;
	char *type;
	int type_len;

	if (s_type == DIR_STREAM)
		type = ":$INDEX_ALLOCATION";
	else
		type = ":$DATA";

	type_len = strlen(type);
	stream_name_size = strlen(stream_name);
	*xattr_stream_name_size = stream_name_size + XATTR_NAME_STREAM_LEN + 1;
	xattr_stream_name_buf = kmalloc(*xattr_stream_name_size + type_len,
1689
					GFP_KERNEL);
N
Namjae Jeon 已提交
1690 1691 1692
	if (!xattr_stream_name_buf)
		return -ENOMEM;

1693
	memcpy(xattr_stream_name_buf, XATTR_NAME_STREAM, XATTR_NAME_STREAM_LEN);
N
Namjae Jeon 已提交
1694 1695 1696

	if (stream_name_size) {
		memcpy(&xattr_stream_name_buf[XATTR_NAME_STREAM_LEN],
1697
		       stream_name, stream_name_size);
N
Namjae Jeon 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	}
	memcpy(&xattr_stream_name_buf[*xattr_stream_name_size - 1], type, type_len);
		*xattr_stream_name_size += type_len;

	xattr_stream_name_buf[*xattr_stream_name_size - 1] = '\0';
	*xattr_stream_name = xattr_stream_name_buf;

	return 0;
}

int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1709 1710 1711 1712 1713 1714 1715
			       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)
N
Namjae Jeon 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
{
	unsigned int i;
	loff_t src_off, dst_off, src_file_size;
	size_t len;
	int ret;

	*chunk_count_written = 0;
	*chunk_size_written = 0;
	*total_size_written = 0;

	if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
N
Namjae Jeon 已提交
1727
		pr_err("no right to read(%s)\n", FP_FILENAME(src_fp));
N
Namjae Jeon 已提交
1728 1729 1730
		return -EACCES;
	}
	if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
N
Namjae Jeon 已提交
1731
		pr_err("no right to write(%s)\n", FP_FILENAME(dst_fp));
N
Namjae Jeon 已提交
1732 1733 1734 1735 1736 1737 1738 1739
		return -EACCES;
	}

	if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
		return -EBADF;

	smb_break_all_levII_oplock(work, dst_fp, 1);

1740 1741 1742 1743 1744 1745 1746
	if (!work->tcon->posix_extensions) {
		for (i = 0; i < chunk_count; i++) {
			src_off = le64_to_cpu(chunks[i].SourceOffset);
			dst_off = le64_to_cpu(chunks[i].TargetOffset);
			len = le32_to_cpu(chunks[i].Length);

			if (check_lock_range(src_fp->filp, src_off,
1747
					     src_off + len - 1, READ))
1748 1749
				return -EAGAIN;
			if (check_lock_range(dst_fp->filp, dst_off,
1750
					     dst_off + len - 1, WRITE))
1751 1752
				return -EAGAIN;
		}
N
Namjae Jeon 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	}

	src_file_size = i_size_read(file_inode(src_fp->filp));

	for (i = 0; i < chunk_count; i++) {
		src_off = le64_to_cpu(chunks[i].SourceOffset);
		dst_off = le64_to_cpu(chunks[i].TargetOffset);
		len = le32_to_cpu(chunks[i].Length);

		if (src_off + len > src_file_size)
			return -E2BIG;

1765 1766
		ret = vfs_copy_file_range(src_fp->filp, src_off,
					  dst_fp->filp, dst_off, len, 0);
N
Namjae Jeon 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
		if (ret < 0)
			return ret;

		*chunk_count_written += 1;
		*total_size_written += ret;
	}
	return 0;
}

int ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
{
	return wait_event_interruptible(flock->fl_wait, !flock->fl_blocker);
}

int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
{
	return wait_event_interruptible_timeout(flock->fl_wait,
						!flock->fl_blocker,
						timeout);
}

void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
{
	locks_delete_block(flock);
}

int ksmbd_vfs_set_init_posix_acl(struct inode *inode)
{
	struct posix_acl_state acl_state;
	struct posix_acl *acls;
	int rc;

	ksmbd_debug(SMB, "Set posix acls\n");
	rc = init_acl_state(&acl_state, 1);
	if (rc)
		return rc;

	/* Set default owner group */
	acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
	acl_state.group.allow = (inode->i_mode & 0070) >> 3;
	acl_state.other.allow = inode->i_mode & 0007;
	acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
	acl_state.users->aces[acl_state.users->n++].perms.allow =
		acl_state.owner.allow;
	acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
	acl_state.groups->aces[acl_state.groups->n++].perms.allow =
		acl_state.group.allow;
	acl_state.mask.allow = 0x07;

1816
	acls = posix_acl_alloc(6, GFP_KERNEL);
N
Namjae Jeon 已提交
1817 1818 1819 1820 1821
	if (!acls) {
		free_acl_state(&acl_state);
		return -ENOMEM;
	}
	posix_state_to_acl(&acl_state, acls->a_entries);
1822
	rc = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS, acls);
N
Namjae Jeon 已提交
1823 1824
	if (rc < 0)
		ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1825
			    rc);
N
Namjae Jeon 已提交
1826 1827
	else if (S_ISDIR(inode->i_mode)) {
		posix_state_to_acl(&acl_state, acls->a_entries);
1828 1829
		rc = set_posix_acl(&init_user_ns, inode, ACL_TYPE_DEFAULT,
				   acls);
N
Namjae Jeon 已提交
1830 1831
		if (rc < 0)
			ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1832
				    rc);
N
Namjae Jeon 已提交
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
	}
	free_acl_state(&acl_state);
	posix_acl_release(acls);
	return rc;
}

int ksmbd_vfs_inherit_posix_acl(struct inode *inode, struct inode *parent_inode)
{
	struct posix_acl *acls;
	struct posix_acl_entry *pace;
	int rc, i;

1845
	acls = get_acl(parent_inode, ACL_TYPE_DEFAULT);
N
Namjae Jeon 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
	if (!acls)
		return -ENOENT;
	pace = acls->a_entries;

	for (i = 0; i < acls->a_count; i++, pace++) {
		if (pace->e_tag == ACL_MASK) {
			pace->e_perm = 0x07;
			break;
		}
	}

1857
	rc = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS, acls);
N
Namjae Jeon 已提交
1858 1859
	if (rc < 0)
		ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1860
			    rc);
N
Namjae Jeon 已提交
1861
	if (S_ISDIR(inode->i_mode)) {
1862 1863
		rc = set_posix_acl(&init_user_ns, inode, ACL_TYPE_DEFAULT,
				   acls);
N
Namjae Jeon 已提交
1864 1865
		if (rc < 0)
			ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1866
				    rc);
N
Namjae Jeon 已提交
1867 1868 1869 1870
	}
	posix_acl_release(acls);
	return rc;
}