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