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