]>
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; | |
bde1694a | 48 | pr_err("Invalid path %s\n", path); |
f4415848 NJ |
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) | |
bde1694a NJ |
173 | pr_err("path create failed for %s, err %d\n", |
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 | 183 | } else { |
bde1694a | 184 | pr_err("File(%s): creation failed (err:%d)\n", name, err); |
f4415848 NJ |
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 | 237 | if (err) |
bde1694a | 238 | pr_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) | |
bde1694a | 262 | pr_err("failed to get xattr in file\n"); |
f4415848 NJ |
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) { | |
bde1694a | 327 | pr_err("not allow write by shared lock\n"); |
f4415848 NJ |
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; | |
bde1694a | 335 | pr_err("not allow rw access by exclusive lock from other opens\n"); |
f4415848 NJ |
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))) { | |
bde1694a | 371 | pr_err("no right to read(%s)\n", FP_FILENAME(fp)); |
f4415848 NJ |
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 | 383 | if (ret) { |
bde1694a | 384 | pr_err("unable to read due to lock\n"); |
c36fca86 NJ |
385 | return -EAGAIN; |
386 | } | |
f4415848 NJ |
387 | } |
388 | ||
389 | nbytes = kernel_read(filp, rbuf, count, pos); | |
390 | if (nbytes < 0) { | |
bde1694a NJ |
391 | pr_err("smb read failed for (%s), err = %zd\n", |
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) { |
bde1694a | 421 | pr_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)) { | |
bde1694a | 479 | pr_err("no right to write(%s)\n", FP_FILENAME(fp)); |
f4415848 NJ |
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) { | |
bde1694a | 497 | pr_err("unable to write due to lock\n"); |
c36fca86 NJ |
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) | |
bde1694a NJ |
518 | pr_err("fsync failed for filename = %s, err = %d\n", |
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) | |
bde1694a | 540 | pr_err("getattr failed, err %d\n", err); |
f4415848 NJ |
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) { | |
bde1694a | 558 | pr_err("failed to get filp for fid %llu\n", fid); |
f4415848 NJ |
559 | return -ENOENT; |
560 | } | |
561 | err = vfs_fsync(fp->filp, 0); | |
562 | if (err < 0) | |
bde1694a | 563 | pr_err("smb fsync failed, err = %d\n", err); |
f4415848 NJ |
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 | 656 | if (err) { |
bde1694a NJ |
657 | pr_err("cannot get linux path for %s, err = %d\n", |
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); | |
bde1694a | 666 | pr_err("path create err for %s, err %d\n", newname, err); |
f4415848 NJ |
667 | goto out2; |
668 | } | |
669 | ||
670 | err = -EXDEV; | |
671 | if (oldpath.mnt != newpath.mnt) { | |
bde1694a | 672 | pr_err("vfs_link failed err %d\n", err); |
f4415848 NJ |
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)) { | |
bde1694a | 733 | pr_err("lookup failed %s [%d]\n", dst_name, err); |
f4415848 NJ |
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) | |
bde1694a | 750 | pr_err("vfs_rename failed err %d\n", err); |
f4415848 NJ |
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) { | |
bde1694a NJ |
838 | pr_err("cannot get linux path for %s, err %d\n", |
839 | name, err); | |
f4415848 NJ |
840 | return err; |
841 | } | |
842 | err = vfs_truncate(&path, size); | |
843 | if (err) | |
bde1694a NJ |
844 | pr_err("truncate failed for %s err %d\n", |
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) { | |
bde1694a | 867 | pr_err("failed due to lock\n"); |
c36fca86 NJ |
868 | return -EAGAIN; |
869 | } | |
f4415848 NJ |
870 | } |
871 | ||
872 | err = vfs_truncate(&filp->f_path, size); | |
873 | if (err) | |
bde1694a NJ |
874 | pr_err("truncate failed for filename : %s err %d\n", |
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 | ||
64b39f4a | 1003 | int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp, |
070fb21e | 1004 | loff_t off, loff_t len) |
f4415848 NJ |
1005 | { |
1006 | smb_break_all_levII_oplock(work, fp, 1); | |
1007 | if (fp->f_ci->m_fattr & ATTR_SPARSE_FILE_LE) | |
1008 | return vfs_fallocate(fp->filp, | |
070fb21e NJ |
1009 | FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, |
1010 | off, len); | |
f4415848 NJ |
1011 | |
1012 | return vfs_fallocate(fp->filp, FALLOC_FL_ZERO_RANGE, off, len); | |
1013 | } | |
1014 | ||
1015 | int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length, | |
070fb21e NJ |
1016 | struct file_allocated_range_buffer *ranges, |
1017 | int in_count, int *out_count) | |
f4415848 NJ |
1018 | { |
1019 | struct file *f = fp->filp; | |
1020 | struct inode *inode = FP_INODE(fp); | |
1021 | loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end; | |
1022 | loff_t extent_start, extent_end; | |
1023 | int ret = 0; | |
1024 | ||
1025 | if (start > maxbytes) | |
1026 | return -EFBIG; | |
1027 | ||
1028 | if (!in_count) | |
1029 | return 0; | |
1030 | ||
1031 | /* | |
1032 | * Shrink request scope to what the fs can actually handle. | |
1033 | */ | |
1034 | if (length > maxbytes || (maxbytes - length) < start) | |
1035 | length = maxbytes - start; | |
1036 | ||
1037 | if (start + length > inode->i_size) | |
1038 | length = inode->i_size - start; | |
1039 | ||
1040 | *out_count = 0; | |
1041 | end = start + length; | |
1042 | while (start < end && *out_count < in_count) { | |
1043 | extent_start = f->f_op->llseek(f, start, SEEK_DATA); | |
1044 | if (extent_start < 0) { | |
1045 | if (extent_start != -ENXIO) | |
1046 | ret = (int)extent_start; | |
1047 | break; | |
1048 | } | |
1049 | ||
1050 | if (extent_start >= end) | |
1051 | break; | |
1052 | ||
1053 | extent_end = f->f_op->llseek(f, extent_start, SEEK_HOLE); | |
1054 | if (extent_end < 0) { | |
1055 | if (extent_end != -ENXIO) | |
1056 | ret = (int)extent_end; | |
1057 | break; | |
64b39f4a | 1058 | } else if (extent_start >= extent_end) { |
f4415848 | 1059 | break; |
64b39f4a | 1060 | } |
f4415848 NJ |
1061 | |
1062 | ranges[*out_count].file_offset = cpu_to_le64(extent_start); | |
1063 | ranges[(*out_count)++].length = | |
1064 | cpu_to_le64(min(extent_end, end) - extent_start); | |
1065 | ||
1066 | start = extent_end; | |
1067 | } | |
1068 | ||
1069 | return ret; | |
1070 | } | |
1071 | ||
1072 | int ksmbd_vfs_remove_xattr(struct dentry *dentry, char *attr_name) | |
1073 | { | |
1074 | return vfs_removexattr(&init_user_ns, dentry, attr_name); | |
1075 | } | |
1076 | ||
f4415848 NJ |
1077 | int ksmbd_vfs_unlink(struct dentry *dir, struct dentry *dentry) |
1078 | { | |
ff1d5727 | 1079 | struct dentry *child; |
f4415848 NJ |
1080 | int err = 0; |
1081 | ||
f4415848 | 1082 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
ff1d5727 | 1083 | dget(dentry); |
070fb21e | 1084 | child = lookup_one_len(dentry->d_name.name, dir, dentry->d_name.len); |
ff1d5727 NJ |
1085 | if (IS_ERR(child)) { |
1086 | err = PTR_ERR(child); | |
1087 | goto out; | |
1088 | } | |
1089 | ||
1090 | if (child != dentry) { | |
1091 | err = -ESTALE; | |
1092 | dput(child); | |
f4415848 NJ |
1093 | goto out; |
1094 | } | |
ff1d5727 | 1095 | dput(child); |
f4415848 NJ |
1096 | |
1097 | if (S_ISDIR(d_inode(dentry)->i_mode)) | |
1098 | err = vfs_rmdir(&init_user_ns, d_inode(dir), dentry); | |
1099 | else | |
1100 | err = vfs_unlink(&init_user_ns, d_inode(dir), dentry, NULL); | |
1101 | ||
1102 | out: | |
f4415848 | 1103 | dput(dentry); |
ff1d5727 | 1104 | inode_unlock(d_inode(dir)); |
f4415848 NJ |
1105 | if (err) |
1106 | ksmbd_debug(VFS, "failed to delete, err %d\n", err); | |
1107 | ||
1108 | return err; | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * ksmbd_vfs_get_logical_sector_size() - get logical sector size from inode | |
1113 | * @inode: inode | |
1114 | * | |
1115 | * Return: logical sector size | |
1116 | */ | |
1117 | unsigned short ksmbd_vfs_logical_sector_size(struct inode *inode) | |
1118 | { | |
1119 | struct request_queue *q; | |
1120 | unsigned short ret_val = 512; | |
1121 | ||
1122 | if (!inode->i_sb->s_bdev) | |
1123 | return ret_val; | |
1124 | ||
1125 | q = inode->i_sb->s_bdev->bd_disk->queue; | |
1126 | ||
1127 | if (q && q->limits.logical_block_size) | |
1128 | ret_val = q->limits.logical_block_size; | |
1129 | ||
1130 | return ret_val; | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * ksmbd_vfs_get_smb2_sector_size() - get fs sector sizes | |
1135 | * @inode: inode | |
1136 | * @fs_ss: fs sector size struct | |
1137 | */ | |
1138 | void ksmbd_vfs_smb2_sector_size(struct inode *inode, | |
070fb21e | 1139 | struct ksmbd_fs_sector_size *fs_ss) |
f4415848 NJ |
1140 | { |
1141 | struct request_queue *q; | |
1142 | ||
1143 | fs_ss->logical_sector_size = 512; | |
1144 | fs_ss->physical_sector_size = 512; | |
1145 | fs_ss->optimal_io_size = 512; | |
1146 | ||
1147 | if (!inode->i_sb->s_bdev) | |
1148 | return; | |
1149 | ||
1150 | q = inode->i_sb->s_bdev->bd_disk->queue; | |
1151 | ||
1152 | if (q) { | |
1153 | if (q->limits.logical_block_size) | |
1154 | fs_ss->logical_sector_size = | |
1155 | q->limits.logical_block_size; | |
1156 | if (q->limits.physical_block_size) | |
1157 | fs_ss->physical_sector_size = | |
1158 | q->limits.physical_block_size; | |
1159 | if (q->limits.io_opt) | |
1160 | fs_ss->optimal_io_size = q->limits.io_opt; | |
1161 | } | |
1162 | } | |
1163 | ||
64b39f4a | 1164 | static int __dir_empty(struct dir_context *ctx, const char *name, int namlen, |
070fb21e | 1165 | loff_t offset, u64 ino, unsigned int d_type) |
f4415848 NJ |
1166 | { |
1167 | struct ksmbd_readdir_data *buf; | |
1168 | ||
1169 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); | |
1170 | buf->dirent_count++; | |
1171 | ||
1172 | if (buf->dirent_count > 2) | |
1173 | return -ENOTEMPTY; | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | /** | |
1178 | * ksmbd_vfs_empty_dir() - check for empty directory | |
1179 | * @fp: ksmbd file pointer | |
1180 | * | |
1181 | * Return: true if directory empty, otherwise false | |
1182 | */ | |
1183 | int ksmbd_vfs_empty_dir(struct ksmbd_file *fp) | |
1184 | { | |
1185 | int err; | |
1186 | struct ksmbd_readdir_data readdir_data; | |
1187 | ||
1188 | memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data)); | |
1189 | ||
1190 | set_ctx_actor(&readdir_data.ctx, __dir_empty); | |
1191 | readdir_data.dirent_count = 0; | |
1192 | ||
e8c06191 | 1193 | err = iterate_dir(fp->filp, &readdir_data.ctx); |
f4415848 NJ |
1194 | if (readdir_data.dirent_count > 2) |
1195 | err = -ENOTEMPTY; | |
1196 | else | |
1197 | err = 0; | |
1198 | return err; | |
1199 | } | |
1200 | ||
64b39f4a | 1201 | static int __caseless_lookup(struct dir_context *ctx, const char *name, |
070fb21e NJ |
1202 | int namlen, loff_t offset, u64 ino, |
1203 | unsigned int d_type) | |
f4415848 NJ |
1204 | { |
1205 | struct ksmbd_readdir_data *buf; | |
1206 | ||
1207 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); | |
1208 | ||
1209 | if (buf->used != namlen) | |
1210 | return 0; | |
1211 | if (!strncasecmp((char *)buf->private, name, namlen)) { | |
1212 | memcpy((char *)buf->private, name, namlen); | |
1213 | buf->dirent_count = 1; | |
1214 | return -EEXIST; | |
1215 | } | |
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | /** | |
1220 | * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory | |
3c203783 HL |
1221 | * @dir: path info |
1222 | * @name: filename to lookup | |
1223 | * @namelen: filename length | |
f4415848 NJ |
1224 | * |
1225 | * Return: 0 on success, otherwise error | |
1226 | */ | |
3c203783 | 1227 | static int ksmbd_vfs_lookup_in_dir(struct path *dir, char *name, size_t namelen) |
f4415848 | 1228 | { |
f4415848 NJ |
1229 | int ret; |
1230 | struct file *dfilp; | |
64b39f4a | 1231 | int flags = O_RDONLY | O_LARGEFILE; |
f4415848 NJ |
1232 | struct ksmbd_readdir_data readdir_data = { |
1233 | .ctx.actor = __caseless_lookup, | |
3c203783 HL |
1234 | .private = name, |
1235 | .used = namelen, | |
1236 | .dirent_count = 0, | |
f4415848 NJ |
1237 | }; |
1238 | ||
3c203783 HL |
1239 | dfilp = dentry_open(dir, flags, current_cred()); |
1240 | if (IS_ERR(dfilp)) | |
1241 | return PTR_ERR(dfilp); | |
f4415848 | 1242 | |
e8c06191 | 1243 | ret = iterate_dir(dfilp, &readdir_data.ctx); |
f4415848 NJ |
1244 | if (readdir_data.dirent_count > 0) |
1245 | ret = 0; | |
f4415848 | 1246 | fput(dfilp); |
f4415848 NJ |
1247 | return ret; |
1248 | } | |
1249 | ||
1250 | /** | |
1251 | * ksmbd_vfs_kern_path() - lookup a file and get path info | |
1252 | * @name: name of file for lookup | |
1253 | * @flags: lookup flags | |
1254 | * @path: if lookup succeed, return path info | |
1255 | * @caseless: caseless filename lookup | |
1256 | * | |
1257 | * Return: 0 on success, otherwise error | |
1258 | */ | |
1259 | int ksmbd_vfs_kern_path(char *name, unsigned int flags, struct path *path, | |
070fb21e | 1260 | bool caseless) |
f4415848 | 1261 | { |
f4415848 NJ |
1262 | int err; |
1263 | ||
3c203783 HL |
1264 | if (name[0] != '/') |
1265 | return -EINVAL; | |
1266 | ||
f4415848 NJ |
1267 | err = kern_path(name, flags, path); |
1268 | if (!err) | |
3c203783 | 1269 | return 0; |
f4415848 NJ |
1270 | |
1271 | if (caseless) { | |
3c203783 HL |
1272 | char *filepath; |
1273 | struct path parent; | |
1274 | size_t path_len, remain_len; | |
f4415848 | 1275 | |
3c203783 HL |
1276 | filepath = kstrdup(name, GFP_KERNEL); |
1277 | if (!filepath) | |
1278 | return -ENOMEM; | |
1279 | ||
1280 | path_len = strlen(filepath); | |
1281 | remain_len = path_len - 1; | |
f4415848 | 1282 | |
3c203783 | 1283 | err = kern_path("/", flags, &parent); |
f4415848 NJ |
1284 | if (err) |
1285 | goto out; | |
f4415848 | 1286 | |
3c203783 HL |
1287 | while (d_can_lookup(parent.dentry)) { |
1288 | char *filename = filepath + path_len - remain_len; | |
1289 | char *next = strchrnul(filename, '/'); | |
1290 | size_t filename_len = next - filename; | |
1291 | bool is_last = !next[0]; | |
1292 | ||
1293 | if (filename_len == 0) | |
1294 | break; | |
1295 | ||
1296 | err = ksmbd_vfs_lookup_in_dir(&parent, filename, | |
1297 | filename_len); | |
1298 | if (err) { | |
1299 | path_put(&parent); | |
1300 | goto out; | |
1301 | } | |
1302 | ||
1303 | path_put(&parent); | |
1304 | next[0] = '\0'; | |
1305 | ||
1306 | err = kern_path(filepath, flags, &parent); | |
1307 | if (err) | |
1308 | goto out; | |
1309 | ||
1310 | if (is_last) { | |
1311 | path->mnt = parent.mnt; | |
1312 | path->dentry = parent.dentry; | |
1313 | goto out; | |
1314 | } | |
1315 | ||
1316 | next[0] = '/'; | |
1317 | remain_len -= filename_len + 1; | |
1318 | } | |
1319 | ||
1320 | path_put(&parent); | |
1321 | err = -EINVAL; | |
f4415848 | 1322 | out: |
3c203783 HL |
1323 | kfree(filepath); |
1324 | } | |
f4415848 NJ |
1325 | return err; |
1326 | } | |
1327 | ||
1328 | int ksmbd_vfs_remove_acl_xattrs(struct dentry *dentry) | |
1329 | { | |
1330 | char *name, *xattr_list = NULL; | |
1331 | ssize_t xattr_list_len; | |
1332 | int err = 0; | |
1333 | ||
1334 | xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); | |
1335 | if (xattr_list_len < 0) { | |
1336 | goto out; | |
1337 | } else if (!xattr_list_len) { | |
1338 | ksmbd_debug(SMB, "empty xattr in the file\n"); | |
1339 | goto out; | |
1340 | } | |
1341 | ||
1342 | for (name = xattr_list; name - xattr_list < xattr_list_len; | |
070fb21e | 1343 | name += strlen(name) + 1) { |
f4415848 NJ |
1344 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
1345 | ||
1346 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, | |
64b39f4a | 1347 | sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) || |
f4415848 | 1348 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, |
64b39f4a | 1349 | sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) { |
f4415848 NJ |
1350 | err = ksmbd_vfs_remove_xattr(dentry, name); |
1351 | if (err) | |
1352 | ksmbd_debug(SMB, | |
070fb21e | 1353 | "remove acl xattr failed : %s\n", name); |
f4415848 NJ |
1354 | } |
1355 | } | |
1356 | out: | |
79f6b11a | 1357 | kvfree(xattr_list); |
f4415848 NJ |
1358 | return err; |
1359 | } | |
1360 | ||
1361 | int ksmbd_vfs_remove_sd_xattrs(struct dentry *dentry) | |
1362 | { | |
1363 | char *name, *xattr_list = NULL; | |
1364 | ssize_t xattr_list_len; | |
1365 | int err = 0; | |
1366 | ||
1367 | xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); | |
1368 | if (xattr_list_len < 0) { | |
1369 | goto out; | |
1370 | } else if (!xattr_list_len) { | |
1371 | ksmbd_debug(SMB, "empty xattr in the file\n"); | |
1372 | goto out; | |
1373 | } | |
1374 | ||
1375 | for (name = xattr_list; name - xattr_list < xattr_list_len; | |
1376 | name += strlen(name) + 1) { | |
1377 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); | |
1378 | ||
1379 | if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) { | |
1380 | err = ksmbd_vfs_remove_xattr(dentry, name); | |
1381 | if (err) | |
1382 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); | |
1383 | } | |
1384 | } | |
1385 | out: | |
79f6b11a | 1386 | kvfree(xattr_list); |
f4415848 NJ |
1387 | return err; |
1388 | } | |
1389 | ||
1390 | static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct inode *inode, | |
070fb21e | 1391 | int acl_type) |
f4415848 NJ |
1392 | { |
1393 | struct xattr_smb_acl *smb_acl = NULL; | |
1394 | struct posix_acl *posix_acls; | |
1395 | struct posix_acl_entry *pa_entry; | |
1396 | struct xattr_acl_entry *xa_entry; | |
1397 | int i; | |
1398 | ||
1399 | posix_acls = ksmbd_vfs_get_acl(inode, acl_type); | |
1400 | if (!posix_acls) | |
1401 | return NULL; | |
1402 | ||
1403 | smb_acl = kzalloc(sizeof(struct xattr_smb_acl) + | |
1404 | sizeof(struct xattr_acl_entry) * posix_acls->a_count, | |
1405 | GFP_KERNEL); | |
1406 | if (!smb_acl) | |
1407 | goto out; | |
1408 | ||
1409 | smb_acl->count = posix_acls->a_count; | |
1410 | pa_entry = posix_acls->a_entries; | |
1411 | xa_entry = smb_acl->entries; | |
1412 | for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) { | |
1413 | switch (pa_entry->e_tag) { | |
1414 | case ACL_USER: | |
1415 | xa_entry->type = SMB_ACL_USER; | |
1416 | xa_entry->uid = from_kuid(&init_user_ns, pa_entry->e_uid); | |
1417 | break; | |
1418 | case ACL_USER_OBJ: | |
1419 | xa_entry->type = SMB_ACL_USER_OBJ; | |
1420 | break; | |
1421 | case ACL_GROUP: | |
1422 | xa_entry->type = SMB_ACL_GROUP; | |
1423 | xa_entry->gid = from_kgid(&init_user_ns, pa_entry->e_gid); | |
1424 | break; | |
1425 | case ACL_GROUP_OBJ: | |
1426 | xa_entry->type = SMB_ACL_GROUP_OBJ; | |
1427 | break; | |
1428 | case ACL_OTHER: | |
1429 | xa_entry->type = SMB_ACL_OTHER; | |
1430 | break; | |
1431 | case ACL_MASK: | |
1432 | xa_entry->type = SMB_ACL_MASK; | |
1433 | break; | |
1434 | default: | |
bde1694a | 1435 | pr_err("unknown type : 0x%x\n", pa_entry->e_tag); |
f4415848 NJ |
1436 | goto out; |
1437 | } | |
1438 | ||
1439 | if (pa_entry->e_perm & ACL_READ) | |
1440 | xa_entry->perm |= SMB_ACL_READ; | |
1441 | if (pa_entry->e_perm & ACL_WRITE) | |
1442 | xa_entry->perm |= SMB_ACL_WRITE; | |
1443 | if (pa_entry->e_perm & ACL_EXECUTE) | |
1444 | xa_entry->perm |= SMB_ACL_EXECUTE; | |
1445 | } | |
1446 | out: | |
1447 | posix_acl_release(posix_acls); | |
1448 | return smb_acl; | |
1449 | } | |
1450 | ||
1451 | int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn, struct dentry *dentry, | |
070fb21e | 1452 | struct smb_ntsd *pntsd, int len) |
f4415848 NJ |
1453 | { |
1454 | int rc; | |
1455 | struct ndr sd_ndr = {0}, acl_ndr = {0}; | |
1456 | struct xattr_ntacl acl = {0}; | |
1457 | struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL; | |
fba08fa0 | 1458 | struct inode *inode = d_inode(dentry); |
f4415848 NJ |
1459 | |
1460 | acl.version = 4; | |
1461 | acl.hash_type = XATTR_SD_HASH_TYPE_SHA256; | |
fba08fa0 | 1462 | acl.current_time = ksmbd_UnixTimeToNT(current_time(inode)); |
f4415848 NJ |
1463 | |
1464 | memcpy(acl.desc, "posix_acl", 9); | |
1465 | acl.desc_len = 10; | |
1466 | ||
1467 | pntsd->osidoffset = | |
1468 | cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF); | |
1469 | pntsd->gsidoffset = | |
1470 | cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF); | |
1471 | pntsd->dacloffset = | |
1472 | cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF); | |
1473 | ||
1474 | acl.sd_buf = (char *)pntsd; | |
1475 | acl.sd_size = len; | |
1476 | ||
1477 | rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash); | |
1478 | if (rc) { | |
bde1694a | 1479 | pr_err("failed to generate hash for ndr acl\n"); |
f4415848 NJ |
1480 | return rc; |
1481 | } | |
1482 | ||
fba08fa0 | 1483 | smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode, ACL_TYPE_ACCESS); |
f4415848 | 1484 | if (S_ISDIR(inode->i_mode)) |
fba08fa0 | 1485 | def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode, |
070fb21e | 1486 | ACL_TYPE_DEFAULT); |
f4415848 NJ |
1487 | |
1488 | rc = ndr_encode_posix_acl(&acl_ndr, inode, smb_acl, def_smb_acl); | |
1489 | if (rc) { | |
bde1694a | 1490 | pr_err("failed to encode ndr to posix acl\n"); |
f4415848 NJ |
1491 | goto out; |
1492 | } | |
1493 | ||
1494 | rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, | |
070fb21e | 1495 | acl.posix_acl_hash); |
f4415848 | 1496 | if (rc) { |
bde1694a | 1497 | pr_err("failed to generate hash for ndr acl\n"); |
f4415848 NJ |
1498 | goto out; |
1499 | } | |
1500 | ||
1501 | rc = ndr_encode_v4_ntacl(&sd_ndr, &acl); | |
1502 | if (rc) { | |
bde1694a | 1503 | pr_err("failed to encode ndr to posix acl\n"); |
f4415848 NJ |
1504 | goto out; |
1505 | } | |
1506 | ||
1507 | rc = ksmbd_vfs_setxattr(dentry, XATTR_NAME_SD, sd_ndr.data, | |
070fb21e | 1508 | sd_ndr.offset, 0); |
f4415848 | 1509 | if (rc < 0) |
bde1694a | 1510 | pr_err("Failed to store XATTR ntacl :%d\n", rc); |
f4415848 NJ |
1511 | |
1512 | kfree(sd_ndr.data); | |
1513 | out: | |
1514 | kfree(acl_ndr.data); | |
1515 | kfree(smb_acl); | |
1516 | kfree(def_smb_acl); | |
1517 | return rc; | |
1518 | } | |
1519 | ||
1520 | int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, struct dentry *dentry, | |
070fb21e | 1521 | struct smb_ntsd **pntsd) |
f4415848 NJ |
1522 | { |
1523 | int rc; | |
1524 | struct ndr n; | |
1525 | ||
1526 | rc = ksmbd_vfs_getxattr(dentry, XATTR_NAME_SD, &n.data); | |
1527 | if (rc > 0) { | |
fba08fa0 | 1528 | struct inode *inode = d_inode(dentry); |
f4415848 NJ |
1529 | struct ndr acl_ndr = {0}; |
1530 | struct xattr_ntacl acl; | |
1531 | struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL; | |
1532 | __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0}; | |
1533 | ||
1534 | n.length = rc; | |
1535 | rc = ndr_decode_v4_ntacl(&n, &acl); | |
1536 | if (rc) | |
1537 | return rc; | |
1538 | ||
1539 | smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode, | |
070fb21e | 1540 | ACL_TYPE_ACCESS); |
f4415848 NJ |
1541 | if (S_ISDIR(inode->i_mode)) |
1542 | def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(inode, | |
070fb21e | 1543 | ACL_TYPE_DEFAULT); |
f4415848 NJ |
1544 | |
1545 | rc = ndr_encode_posix_acl(&acl_ndr, inode, smb_acl, def_smb_acl); | |
1546 | if (rc) { | |
bde1694a | 1547 | pr_err("failed to encode ndr to posix acl\n"); |
f4415848 NJ |
1548 | goto out; |
1549 | } | |
1550 | ||
1551 | rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, | |
070fb21e | 1552 | cmp_hash); |
f4415848 | 1553 | if (rc) { |
bde1694a | 1554 | pr_err("failed to generate hash for ndr acl\n"); |
f4415848 NJ |
1555 | goto out; |
1556 | } | |
1557 | ||
1558 | if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) { | |
bde1694a | 1559 | pr_err("hash value diff\n"); |
f4415848 NJ |
1560 | rc = -EINVAL; |
1561 | goto out; | |
1562 | } | |
1563 | ||
1564 | *pntsd = acl.sd_buf; | |
1565 | (*pntsd)->osidoffset = | |
1566 | cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) - NDR_NTSD_OFFSETOF); | |
1567 | (*pntsd)->gsidoffset = | |
1568 | cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) - NDR_NTSD_OFFSETOF); | |
1569 | (*pntsd)->dacloffset = | |
1570 | cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) - NDR_NTSD_OFFSETOF); | |
1571 | ||
1572 | rc = acl.sd_size; | |
1573 | out: | |
1574 | kfree(n.data); | |
1575 | kfree(acl_ndr.data); | |
1576 | kfree(smb_acl); | |
1577 | kfree(def_smb_acl); | |
1578 | } | |
1579 | ||
1580 | return rc; | |
1581 | } | |
1582 | ||
1583 | int ksmbd_vfs_set_dos_attrib_xattr(struct dentry *dentry, | |
070fb21e | 1584 | struct xattr_dos_attrib *da) |
f4415848 NJ |
1585 | { |
1586 | struct ndr n; | |
1587 | int err; | |
1588 | ||
1589 | err = ndr_encode_dos_attr(&n, da); | |
1590 | if (err) | |
1591 | return err; | |
1592 | ||
070fb21e NJ |
1593 | err = ksmbd_vfs_setxattr(dentry, XATTR_NAME_DOS_ATTRIBUTE, |
1594 | (void *)n.data, n.offset, 0); | |
f4415848 NJ |
1595 | if (err) |
1596 | ksmbd_debug(SMB, "failed to store dos attribute in xattr\n"); | |
1597 | kfree(n.data); | |
1598 | ||
1599 | return err; | |
1600 | } | |
1601 | ||
1602 | int ksmbd_vfs_get_dos_attrib_xattr(struct dentry *dentry, | |
070fb21e | 1603 | struct xattr_dos_attrib *da) |
f4415848 NJ |
1604 | { |
1605 | struct ndr n; | |
1606 | int err; | |
1607 | ||
070fb21e NJ |
1608 | err = ksmbd_vfs_getxattr(dentry, XATTR_NAME_DOS_ATTRIBUTE, |
1609 | (char **)&n.data); | |
f4415848 NJ |
1610 | if (err > 0) { |
1611 | n.length = err; | |
1612 | if (ndr_decode_dos_attr(&n, da)) | |
1613 | err = -EINVAL; | |
79f6b11a | 1614 | kfree(n.data); |
64b39f4a | 1615 | } else { |
f4415848 | 1616 | ksmbd_debug(SMB, "failed to load dos attribute in xattr\n"); |
64b39f4a | 1617 | } |
f4415848 NJ |
1618 | |
1619 | return err; | |
1620 | } | |
1621 | ||
1622 | struct posix_acl *ksmbd_vfs_posix_acl_alloc(int count, gfp_t flags) | |
1623 | { | |
1624 | #if IS_ENABLED(CONFIG_FS_POSIX_ACL) | |
1625 | return posix_acl_alloc(count, flags); | |
1626 | #else | |
1627 | return NULL; | |
1628 | #endif | |
1629 | } | |
1630 | ||
1631 | struct posix_acl *ksmbd_vfs_get_acl(struct inode *inode, int type) | |
1632 | { | |
1633 | #if IS_ENABLED(CONFIG_FS_POSIX_ACL) | |
1634 | return get_acl(inode, type); | |
1635 | #else | |
1636 | return NULL; | |
1637 | #endif | |
1638 | } | |
1639 | ||
1640 | int ksmbd_vfs_set_posix_acl(struct inode *inode, int type, | |
070fb21e | 1641 | struct posix_acl *acl) |
f4415848 NJ |
1642 | { |
1643 | #if IS_ENABLED(CONFIG_FS_POSIX_ACL) | |
1644 | return set_posix_acl(&init_user_ns, inode, type, acl); | |
1645 | #else | |
1646 | return -EOPNOTSUPP; | |
1647 | #endif | |
1648 | } | |
1649 | ||
1650 | /** | |
1651 | * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format | |
1652 | * @p: destination buffer | |
1653 | * @ksmbd_kstat: ksmbd kstat wrapper | |
1654 | */ | |
1655 | void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat) | |
1656 | { | |
1657 | struct file_directory_info *info = (struct file_directory_info *)(*p); | |
1658 | struct kstat *kstat = ksmbd_kstat->kstat; | |
1659 | u64 time; | |
1660 | ||
1661 | info->FileIndex = 0; | |
1662 | info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); | |
1663 | time = ksmbd_UnixTimeToNT(kstat->atime); | |
1664 | info->LastAccessTime = cpu_to_le64(time); | |
1665 | time = ksmbd_UnixTimeToNT(kstat->mtime); | |
1666 | info->LastWriteTime = cpu_to_le64(time); | |
1667 | time = ksmbd_UnixTimeToNT(kstat->ctime); | |
1668 | info->ChangeTime = cpu_to_le64(time); | |
1669 | ||
1670 | if (ksmbd_kstat->file_attributes & ATTR_DIRECTORY_LE) { | |
1671 | info->EndOfFile = 0; | |
1672 | info->AllocationSize = 0; | |
1673 | } else { | |
1674 | info->EndOfFile = cpu_to_le64(kstat->size); | |
1675 | info->AllocationSize = cpu_to_le64(kstat->blocks << 9); | |
1676 | } | |
1677 | info->ExtFileAttributes = ksmbd_kstat->file_attributes; | |
1678 | ||
1679 | return info; | |
1680 | } | |
1681 | ||
64b39f4a | 1682 | int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, struct dentry *dentry, |
070fb21e | 1683 | struct ksmbd_kstat *ksmbd_kstat) |
f4415848 NJ |
1684 | { |
1685 | u64 time; | |
1686 | int rc; | |
1687 | ||
1688 | generic_fillattr(&init_user_ns, d_inode(dentry), ksmbd_kstat->kstat); | |
1689 | ||
1690 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); | |
1691 | ksmbd_kstat->create_time = time; | |
1692 | ||
1693 | /* | |
1694 | * set default value for the case that store dos attributes is not yes | |
1695 | * or that acl is disable in server's filesystem and the config is yes. | |
1696 | */ | |
1697 | if (S_ISDIR(ksmbd_kstat->kstat->mode)) | |
1698 | ksmbd_kstat->file_attributes = ATTR_DIRECTORY_LE; | |
1699 | else | |
1700 | ksmbd_kstat->file_attributes = ATTR_ARCHIVE_LE; | |
1701 | ||
1702 | if (test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 1703 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
f4415848 NJ |
1704 | struct xattr_dos_attrib da; |
1705 | ||
1706 | rc = ksmbd_vfs_get_dos_attrib_xattr(dentry, &da); | |
1707 | if (rc > 0) { | |
1708 | ksmbd_kstat->file_attributes = cpu_to_le32(da.attr); | |
1709 | ksmbd_kstat->create_time = da.create_time; | |
64b39f4a | 1710 | } else { |
f4415848 | 1711 | ksmbd_debug(VFS, "fail to load dos attribute.\n"); |
64b39f4a | 1712 | } |
f4415848 NJ |
1713 | } |
1714 | ||
1715 | return 0; | |
1716 | } | |
1717 | ||
64b39f4a | 1718 | ssize_t ksmbd_vfs_casexattr_len(struct dentry *dentry, char *attr_name, |
070fb21e | 1719 | int attr_name_len) |
f4415848 NJ |
1720 | { |
1721 | char *name, *xattr_list = NULL; | |
1722 | ssize_t value_len = -ENOENT, xattr_list_len; | |
1723 | ||
1724 | xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); | |
1725 | if (xattr_list_len <= 0) | |
1726 | goto out; | |
1727 | ||
1728 | for (name = xattr_list; name - xattr_list < xattr_list_len; | |
1729 | name += strlen(name) + 1) { | |
1730 | ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); | |
1731 | if (strncasecmp(attr_name, name, attr_name_len)) | |
1732 | continue; | |
1733 | ||
1734 | value_len = ksmbd_vfs_xattr_len(dentry, name); | |
1735 | break; | |
1736 | } | |
1737 | ||
1738 | out: | |
79f6b11a | 1739 | kvfree(xattr_list); |
f4415848 NJ |
1740 | return value_len; |
1741 | } | |
1742 | ||
64b39f4a | 1743 | int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name, |
070fb21e | 1744 | size_t *xattr_stream_name_size, int s_type) |
f4415848 NJ |
1745 | { |
1746 | int stream_name_size; | |
1747 | char *xattr_stream_name_buf; | |
1748 | char *type; | |
1749 | int type_len; | |
1750 | ||
1751 | if (s_type == DIR_STREAM) | |
1752 | type = ":$INDEX_ALLOCATION"; | |
1753 | else | |
1754 | type = ":$DATA"; | |
1755 | ||
1756 | type_len = strlen(type); | |
1757 | stream_name_size = strlen(stream_name); | |
1758 | *xattr_stream_name_size = stream_name_size + XATTR_NAME_STREAM_LEN + 1; | |
1759 | xattr_stream_name_buf = kmalloc(*xattr_stream_name_size + type_len, | |
070fb21e | 1760 | GFP_KERNEL); |
f4415848 NJ |
1761 | if (!xattr_stream_name_buf) |
1762 | return -ENOMEM; | |
1763 | ||
070fb21e | 1764 | memcpy(xattr_stream_name_buf, XATTR_NAME_STREAM, XATTR_NAME_STREAM_LEN); |
f4415848 NJ |
1765 | |
1766 | if (stream_name_size) { | |
1767 | memcpy(&xattr_stream_name_buf[XATTR_NAME_STREAM_LEN], | |
070fb21e | 1768 | stream_name, stream_name_size); |
f4415848 NJ |
1769 | } |
1770 | memcpy(&xattr_stream_name_buf[*xattr_stream_name_size - 1], type, type_len); | |
1771 | *xattr_stream_name_size += type_len; | |
1772 | ||
1773 | xattr_stream_name_buf[*xattr_stream_name_size - 1] = '\0'; | |
1774 | *xattr_stream_name = xattr_stream_name_buf; | |
1775 | ||
1776 | return 0; | |
1777 | } | |
1778 | ||
f4415848 | 1779 | int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work, |
070fb21e NJ |
1780 | struct ksmbd_file *src_fp, |
1781 | struct ksmbd_file *dst_fp, | |
1782 | struct srv_copychunk *chunks, | |
1783 | unsigned int chunk_count, | |
1784 | unsigned int *chunk_count_written, | |
1785 | unsigned int *chunk_size_written, | |
1786 | loff_t *total_size_written) | |
f4415848 NJ |
1787 | { |
1788 | unsigned int i; | |
1789 | loff_t src_off, dst_off, src_file_size; | |
1790 | size_t len; | |
1791 | int ret; | |
1792 | ||
1793 | *chunk_count_written = 0; | |
1794 | *chunk_size_written = 0; | |
1795 | *total_size_written = 0; | |
1796 | ||
1797 | if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { | |
bde1694a | 1798 | pr_err("no right to read(%s)\n", FP_FILENAME(src_fp)); |
f4415848 NJ |
1799 | return -EACCES; |
1800 | } | |
1801 | if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) { | |
bde1694a | 1802 | pr_err("no right to write(%s)\n", FP_FILENAME(dst_fp)); |
f4415848 NJ |
1803 | return -EACCES; |
1804 | } | |
1805 | ||
1806 | if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp)) | |
1807 | return -EBADF; | |
1808 | ||
1809 | smb_break_all_levII_oplock(work, dst_fp, 1); | |
1810 | ||
c36fca86 NJ |
1811 | if (!work->tcon->posix_extensions) { |
1812 | for (i = 0; i < chunk_count; i++) { | |
1813 | src_off = le64_to_cpu(chunks[i].SourceOffset); | |
1814 | dst_off = le64_to_cpu(chunks[i].TargetOffset); | |
1815 | len = le32_to_cpu(chunks[i].Length); | |
1816 | ||
1817 | if (check_lock_range(src_fp->filp, src_off, | |
64b39f4a | 1818 | src_off + len - 1, READ)) |
c36fca86 NJ |
1819 | return -EAGAIN; |
1820 | if (check_lock_range(dst_fp->filp, dst_off, | |
64b39f4a | 1821 | dst_off + len - 1, WRITE)) |
c36fca86 NJ |
1822 | return -EAGAIN; |
1823 | } | |
f4415848 NJ |
1824 | } |
1825 | ||
1826 | src_file_size = i_size_read(file_inode(src_fp->filp)); | |
1827 | ||
1828 | for (i = 0; i < chunk_count; i++) { | |
1829 | src_off = le64_to_cpu(chunks[i].SourceOffset); | |
1830 | dst_off = le64_to_cpu(chunks[i].TargetOffset); | |
1831 | len = le32_to_cpu(chunks[i].Length); | |
1832 | ||
1833 | if (src_off + len > src_file_size) | |
1834 | return -E2BIG; | |
1835 | ||
f8524776 NJ |
1836 | ret = vfs_copy_file_range(src_fp->filp, src_off, |
1837 | dst_fp->filp, dst_off, len, 0); | |
f4415848 NJ |
1838 | if (ret < 0) |
1839 | return ret; | |
1840 | ||
1841 | *chunk_count_written += 1; | |
1842 | *total_size_written += ret; | |
1843 | } | |
1844 | return 0; | |
1845 | } | |
1846 | ||
1847 | int ksmbd_vfs_posix_lock_wait(struct file_lock *flock) | |
1848 | { | |
1849 | return wait_event_interruptible(flock->fl_wait, !flock->fl_blocker); | |
1850 | } | |
1851 | ||
1852 | int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout) | |
1853 | { | |
1854 | return wait_event_interruptible_timeout(flock->fl_wait, | |
1855 | !flock->fl_blocker, | |
1856 | timeout); | |
1857 | } | |
1858 | ||
1859 | void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock) | |
1860 | { | |
1861 | locks_delete_block(flock); | |
1862 | } | |
1863 | ||
1864 | int ksmbd_vfs_set_init_posix_acl(struct inode *inode) | |
1865 | { | |
1866 | struct posix_acl_state acl_state; | |
1867 | struct posix_acl *acls; | |
1868 | int rc; | |
1869 | ||
1870 | ksmbd_debug(SMB, "Set posix acls\n"); | |
1871 | rc = init_acl_state(&acl_state, 1); | |
1872 | if (rc) | |
1873 | return rc; | |
1874 | ||
1875 | /* Set default owner group */ | |
1876 | acl_state.owner.allow = (inode->i_mode & 0700) >> 6; | |
1877 | acl_state.group.allow = (inode->i_mode & 0070) >> 3; | |
1878 | acl_state.other.allow = inode->i_mode & 0007; | |
1879 | acl_state.users->aces[acl_state.users->n].uid = inode->i_uid; | |
1880 | acl_state.users->aces[acl_state.users->n++].perms.allow = | |
1881 | acl_state.owner.allow; | |
1882 | acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid; | |
1883 | acl_state.groups->aces[acl_state.groups->n++].perms.allow = | |
1884 | acl_state.group.allow; | |
1885 | acl_state.mask.allow = 0x07; | |
1886 | ||
1887 | acls = ksmbd_vfs_posix_acl_alloc(6, GFP_KERNEL); | |
1888 | if (!acls) { | |
1889 | free_acl_state(&acl_state); | |
1890 | return -ENOMEM; | |
1891 | } | |
1892 | posix_state_to_acl(&acl_state, acls->a_entries); | |
1893 | rc = ksmbd_vfs_set_posix_acl(inode, ACL_TYPE_ACCESS, acls); | |
1894 | if (rc < 0) | |
1895 | ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", | |
070fb21e | 1896 | rc); |
f4415848 NJ |
1897 | else if (S_ISDIR(inode->i_mode)) { |
1898 | posix_state_to_acl(&acl_state, acls->a_entries); | |
1899 | rc = ksmbd_vfs_set_posix_acl(inode, ACL_TYPE_DEFAULT, acls); | |
1900 | if (rc < 0) | |
1901 | ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", | |
070fb21e | 1902 | rc); |
f4415848 NJ |
1903 | } |
1904 | free_acl_state(&acl_state); | |
1905 | posix_acl_release(acls); | |
1906 | return rc; | |
1907 | } | |
1908 | ||
1909 | int ksmbd_vfs_inherit_posix_acl(struct inode *inode, struct inode *parent_inode) | |
1910 | { | |
1911 | struct posix_acl *acls; | |
1912 | struct posix_acl_entry *pace; | |
1913 | int rc, i; | |
1914 | ||
1915 | acls = ksmbd_vfs_get_acl(parent_inode, ACL_TYPE_DEFAULT); | |
1916 | if (!acls) | |
1917 | return -ENOENT; | |
1918 | pace = acls->a_entries; | |
1919 | ||
1920 | for (i = 0; i < acls->a_count; i++, pace++) { | |
1921 | if (pace->e_tag == ACL_MASK) { | |
1922 | pace->e_perm = 0x07; | |
1923 | break; | |
1924 | } | |
1925 | } | |
1926 | ||
1927 | rc = ksmbd_vfs_set_posix_acl(inode, ACL_TYPE_ACCESS, acls); | |
1928 | if (rc < 0) | |
1929 | ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", | |
070fb21e | 1930 | rc); |
f4415848 NJ |
1931 | if (S_ISDIR(inode->i_mode)) { |
1932 | rc = ksmbd_vfs_set_posix_acl(inode, ACL_TYPE_DEFAULT, acls); | |
1933 | if (rc < 0) | |
1934 | ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", | |
070fb21e | 1935 | rc); |
f4415848 NJ |
1936 | } |
1937 | posix_acl_release(acls); | |
1938 | return rc; | |
1939 | } |