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