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