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