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