1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
24 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
25 */
26
27
28 #ifdef CONFIG_COMPAT
29 #include <linux/compat.h>
30 #endif
31 #include <linux/fs.h>
32 #include <linux/migrate.h>
33 #include <sys/file.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/zfs_znode.h>
36 #include <sys/zfs_vfsops.h>
37 #include <sys/zfs_vnops.h>
38 #include <sys/zfs_project.h>
39 #include <linux/pagemap_compat.h>
40 #include <linux/fadvise.h>
41 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
42 #include <linux/writeback.h>
43 #endif
44
45 /*
46 * When using fallocate(2) to preallocate space, inflate the requested
47 * capacity check by 10% to account for the required metadata blocks.
48 */
49 static unsigned int zfs_fallocate_reserve_percent = 110;
50
51 static int
zpl_open(struct inode * ip,struct file * filp)52 zpl_open(struct inode *ip, struct file *filp)
53 {
54 cred_t *cr = CRED();
55 int error;
56 fstrans_cookie_t cookie;
57
58 error = generic_file_open(ip, filp);
59 if (error)
60 return (error);
61
62 crhold(cr);
63 cookie = spl_fstrans_mark();
64 error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
65 spl_fstrans_unmark(cookie);
66 crfree(cr);
67 ASSERT3S(error, <=, 0);
68
69 return (error);
70 }
71
72 static int
zpl_release(struct inode * ip,struct file * filp)73 zpl_release(struct inode *ip, struct file *filp)
74 {
75 cred_t *cr = CRED();
76 int error;
77 fstrans_cookie_t cookie;
78
79 cookie = spl_fstrans_mark();
80 if (ITOZ(ip)->z_atime_dirty)
81 zfs_mark_inode_dirty(ip);
82
83 crhold(cr);
84 error = -zfs_close(ip, filp->f_flags, cr);
85 spl_fstrans_unmark(cookie);
86 crfree(cr);
87 ASSERT3S(error, <=, 0);
88
89 return (error);
90 }
91
92 static int
zpl_iterate(struct file * filp,struct dir_context * ctx)93 zpl_iterate(struct file *filp, struct dir_context *ctx)
94 {
95 cred_t *cr = CRED();
96 int error;
97 fstrans_cookie_t cookie;
98
99 crhold(cr);
100 cookie = spl_fstrans_mark();
101 error = -zfs_readdir(file_inode(filp), ctx, cr);
102 spl_fstrans_unmark(cookie);
103 crfree(cr);
104 ASSERT3S(error, <=, 0);
105
106 return (error);
107 }
108
109 static int
zpl_fsync(struct file * filp,loff_t start,loff_t end,int datasync)110 zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
111 {
112 struct inode *inode = filp->f_mapping->host;
113 znode_t *zp = ITOZ(inode);
114 zfsvfs_t *zfsvfs = ITOZSB(inode);
115 cred_t *cr = CRED();
116 int error;
117 fstrans_cookie_t cookie;
118
119 /*
120 * The variables z_sync_writes_cnt and z_async_writes_cnt work in
121 * tandem so that sync writes can detect if there are any non-sync
122 * writes going on and vice-versa. The "vice-versa" part to this logic
123 * is located in zfs_putpage() where non-sync writes check if there are
124 * any ongoing sync writes. If any sync and non-sync writes overlap,
125 * we do a commit to complete the non-sync writes since the latter can
126 * potentially take several seconds to complete and thus block sync
127 * writes in the upcoming call to filemap_write_and_wait_range().
128 */
129 atomic_inc_32(&zp->z_sync_writes_cnt);
130 /*
131 * If the following check does not detect an overlapping non-sync write
132 * (say because it's just about to start), then it is guaranteed that
133 * the non-sync write will detect this sync write. This is because we
134 * always increment z_sync_writes_cnt / z_async_writes_cnt before doing
135 * the check on z_async_writes_cnt / z_sync_writes_cnt here and in
136 * zfs_putpage() respectively.
137 */
138 if (atomic_load_32(&zp->z_async_writes_cnt) > 0) {
139 if ((error = zpl_enter(zfsvfs, FTAG)) != 0) {
140 atomic_dec_32(&zp->z_sync_writes_cnt);
141 return (error);
142 }
143 zil_commit(zfsvfs->z_log, zp->z_id);
144 zpl_exit(zfsvfs, FTAG);
145 }
146
147 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
148
149 /*
150 * The sync write is not complete yet but we decrement
151 * z_sync_writes_cnt since zfs_fsync() increments and decrements
152 * it internally. If a non-sync write starts just after the decrement
153 * operation but before we call zfs_fsync(), it may not detect this
154 * overlapping sync write but it does not matter since we have already
155 * gone past filemap_write_and_wait_range() and we won't block due to
156 * the non-sync write.
157 */
158 atomic_dec_32(&zp->z_sync_writes_cnt);
159
160 if (error)
161 return (error);
162
163 crhold(cr);
164 cookie = spl_fstrans_mark();
165 error = -zfs_fsync(zp, datasync, cr);
166 spl_fstrans_unmark(cookie);
167 crfree(cr);
168 ASSERT3S(error, <=, 0);
169
170 return (error);
171 }
172
173 static inline int
zfs_io_flags(struct kiocb * kiocb)174 zfs_io_flags(struct kiocb *kiocb)
175 {
176 int flags = 0;
177
178 #if defined(IOCB_DSYNC)
179 if (kiocb->ki_flags & IOCB_DSYNC)
180 flags |= O_DSYNC;
181 #endif
182 #if defined(IOCB_SYNC)
183 if (kiocb->ki_flags & IOCB_SYNC)
184 flags |= O_SYNC;
185 #endif
186 #if defined(IOCB_APPEND)
187 if (kiocb->ki_flags & IOCB_APPEND)
188 flags |= O_APPEND;
189 #endif
190 #if defined(IOCB_DIRECT)
191 if (kiocb->ki_flags & IOCB_DIRECT)
192 flags |= O_DIRECT;
193 #endif
194 return (flags);
195 }
196
197 /*
198 * If relatime is enabled, call file_accessed() if zfs_relatime_need_update()
199 * is true. This is needed since datasets with inherited "relatime" property
200 * aren't necessarily mounted with the MNT_RELATIME flag (e.g. after
201 * `zfs set relatime=...`), which is what relatime test in VFS by
202 * relatime_need_update() is based on.
203 */
204 static inline void
zpl_file_accessed(struct file * filp)205 zpl_file_accessed(struct file *filp)
206 {
207 struct inode *ip = filp->f_mapping->host;
208
209 if (!IS_NOATIME(ip) && ITOZSB(ip)->z_relatime) {
210 if (zfs_relatime_need_update(ip))
211 file_accessed(filp);
212 } else {
213 file_accessed(filp);
214 }
215 }
216
217 static ssize_t
zpl_iter_read(struct kiocb * kiocb,struct iov_iter * to)218 zpl_iter_read(struct kiocb *kiocb, struct iov_iter *to)
219 {
220 cred_t *cr = CRED();
221 fstrans_cookie_t cookie;
222 struct file *filp = kiocb->ki_filp;
223 ssize_t count = iov_iter_count(to);
224 zfs_uio_t uio;
225
226 zfs_uio_iov_iter_init(&uio, to, kiocb->ki_pos, count);
227
228 crhold(cr);
229 cookie = spl_fstrans_mark();
230
231 ssize_t ret = -zfs_read(ITOZ(filp->f_mapping->host), &uio,
232 filp->f_flags | zfs_io_flags(kiocb), cr);
233
234 spl_fstrans_unmark(cookie);
235 crfree(cr);
236
237 if (ret < 0)
238 return (ret);
239
240 ssize_t read = count - uio.uio_resid;
241 kiocb->ki_pos += read;
242
243 zpl_file_accessed(filp);
244
245 return (read);
246 }
247
248 static inline ssize_t
zpl_generic_write_checks(struct kiocb * kiocb,struct iov_iter * from,size_t * countp)249 zpl_generic_write_checks(struct kiocb *kiocb, struct iov_iter *from,
250 size_t *countp)
251 {
252 ssize_t ret = generic_write_checks(kiocb, from);
253 if (ret <= 0)
254 return (ret);
255
256 *countp = ret;
257
258 return (0);
259 }
260
261 static ssize_t
zpl_iter_write(struct kiocb * kiocb,struct iov_iter * from)262 zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from)
263 {
264 cred_t *cr = CRED();
265 fstrans_cookie_t cookie;
266 struct file *filp = kiocb->ki_filp;
267 struct inode *ip = filp->f_mapping->host;
268 zfs_uio_t uio;
269 size_t count = 0;
270 ssize_t ret;
271
272 ret = zpl_generic_write_checks(kiocb, from, &count);
273 if (ret)
274 return (ret);
275
276 zfs_uio_iov_iter_init(&uio, from, kiocb->ki_pos, count);
277
278 crhold(cr);
279 cookie = spl_fstrans_mark();
280
281 ret = -zfs_write(ITOZ(ip), &uio,
282 filp->f_flags | zfs_io_flags(kiocb), cr);
283
284 spl_fstrans_unmark(cookie);
285 crfree(cr);
286
287 if (ret < 0)
288 return (ret);
289
290 ssize_t wrote = count - uio.uio_resid;
291 kiocb->ki_pos += wrote;
292
293 return (wrote);
294 }
295
296 static ssize_t
zpl_direct_IO(struct kiocb * kiocb,struct iov_iter * iter)297 zpl_direct_IO(struct kiocb *kiocb, struct iov_iter *iter)
298 {
299 /*
300 * All O_DIRECT requests should be handled by
301 * zpl_iter_write/read}(). There is no way kernel generic code should
302 * call the direct_IO address_space_operations function. We set this
303 * code path to be fatal if it is executed.
304 */
305 PANIC(0);
306 return (0);
307 }
308
309 static loff_t
zpl_llseek(struct file * filp,loff_t offset,int whence)310 zpl_llseek(struct file *filp, loff_t offset, int whence)
311 {
312 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
313 fstrans_cookie_t cookie;
314
315 if (whence == SEEK_DATA || whence == SEEK_HOLE) {
316 struct inode *ip = filp->f_mapping->host;
317 loff_t maxbytes = ip->i_sb->s_maxbytes;
318 loff_t error;
319
320 spl_inode_lock_shared(ip);
321 cookie = spl_fstrans_mark();
322 error = -zfs_holey(ITOZ(ip), whence, &offset);
323 spl_fstrans_unmark(cookie);
324 if (error == 0)
325 error = lseek_execute(filp, ip, offset, maxbytes);
326 spl_inode_unlock_shared(ip);
327
328 return (error);
329 }
330 #endif /* SEEK_HOLE && SEEK_DATA */
331
332 return (generic_file_llseek(filp, offset, whence));
333 }
334
335 /*
336 * It's worth taking a moment to describe how mmap is implemented
337 * for zfs because it differs considerably from other Linux filesystems.
338 * However, this issue is handled the same way under OpenSolaris.
339 *
340 * The issue is that by design zfs bypasses the Linux page cache and
341 * leaves all caching up to the ARC. This has been shown to work
342 * well for the common read(2)/write(2) case. However, mmap(2)
343 * is problem because it relies on being tightly integrated with the
344 * page cache. To handle this we cache mmap'ed files twice, once in
345 * the ARC and a second time in the page cache. The code is careful
346 * to keep both copies synchronized.
347 *
348 * When a file with an mmap'ed region is written to using write(2)
349 * both the data in the ARC and existing pages in the page cache
350 * are updated. For a read(2) data will be read first from the page
351 * cache then the ARC if needed. Neither a write(2) or read(2) will
352 * will ever result in new pages being added to the page cache.
353 *
354 * New pages are added to the page cache only via .readpage() which
355 * is called when the vfs needs to read a page off disk to back the
356 * virtual memory region. These pages may be modified without
357 * notifying the ARC and will be written out periodically via
358 * .writepage(). This will occur due to either a sync or the usual
359 * page aging behavior. Note because a read(2) of a mmap'ed file
360 * will always check the page cache first even when the ARC is out
361 * of date correct data will still be returned.
362 *
363 * While this implementation ensures correct behavior it does have
364 * have some drawbacks. The most obvious of which is that it
365 * increases the required memory footprint when access mmap'ed
366 * files. It also adds additional complexity to the code keeping
367 * both caches synchronized.
368 *
369 * Longer term it may be possible to cleanly resolve this wart by
370 * mapping page cache pages directly on to the ARC buffers. The
371 * Linux address space operations are flexible enough to allow
372 * selection of which pages back a particular index. The trick
373 * would be working out the details of which subsystem is in
374 * charge, the ARC, the page cache, or both. It may also prove
375 * helpful to move the ARC buffers to a scatter-gather lists
376 * rather than a vmalloc'ed region.
377 */
378 static int
zpl_mmap(struct file * filp,struct vm_area_struct * vma)379 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
380 {
381 struct inode *ip = filp->f_mapping->host;
382 int error;
383 fstrans_cookie_t cookie;
384
385 cookie = spl_fstrans_mark();
386 error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
387 (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
388 spl_fstrans_unmark(cookie);
389
390 if (error)
391 return (error);
392
393 error = generic_file_mmap(filp, vma);
394 if (error)
395 return (error);
396
397 return (error);
398 }
399
400 /*
401 * Populate a page with data for the Linux page cache. This function is
402 * only used to support mmap(2). There will be an identical copy of the
403 * data in the ARC which is kept up to date via .write() and .writepage().
404 */
405 static inline int
zpl_readpage_common(struct page * pp)406 zpl_readpage_common(struct page *pp)
407 {
408 fstrans_cookie_t cookie;
409
410 ASSERT(PageLocked(pp));
411
412 cookie = spl_fstrans_mark();
413 int error = -zfs_getpage(pp->mapping->host, pp);
414 spl_fstrans_unmark(cookie);
415
416 unlock_page(pp);
417
418 return (error);
419 }
420
421 #ifdef HAVE_VFS_READ_FOLIO
422 static int
zpl_read_folio(struct file * filp,struct folio * folio)423 zpl_read_folio(struct file *filp, struct folio *folio)
424 {
425 return (zpl_readpage_common(&folio->page));
426 }
427 #else
428 static int
zpl_readpage(struct file * filp,struct page * pp)429 zpl_readpage(struct file *filp, struct page *pp)
430 {
431 return (zpl_readpage_common(pp));
432 }
433 #endif
434
435 static int
zpl_readpage_filler(void * data,struct page * pp)436 zpl_readpage_filler(void *data, struct page *pp)
437 {
438 return (zpl_readpage_common(pp));
439 }
440
441 /*
442 * Populate a set of pages with data for the Linux page cache. This
443 * function will only be called for read ahead and never for demand
444 * paging. For simplicity, the code relies on read_cache_pages() to
445 * correctly lock each page for IO and call zpl_readpage().
446 */
447 #ifdef HAVE_VFS_READPAGES
448 static int
zpl_readpages(struct file * filp,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)449 zpl_readpages(struct file *filp, struct address_space *mapping,
450 struct list_head *pages, unsigned nr_pages)
451 {
452 return (read_cache_pages(mapping, pages, zpl_readpage_filler, NULL));
453 }
454 #else
455 static void
zpl_readahead(struct readahead_control * ractl)456 zpl_readahead(struct readahead_control *ractl)
457 {
458 struct page *page;
459
460 while ((page = readahead_page(ractl)) != NULL) {
461 int ret;
462
463 ret = zpl_readpage_filler(NULL, page);
464 put_page(page);
465 if (ret)
466 break;
467 }
468 }
469 #endif
470
471 static int
zpl_putpage(struct page * pp,struct writeback_control * wbc,void * data)472 zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
473 {
474 boolean_t *for_sync = data;
475 fstrans_cookie_t cookie;
476 int ret;
477
478 ASSERT(PageLocked(pp));
479 ASSERT(!PageWriteback(pp));
480
481 cookie = spl_fstrans_mark();
482 ret = zfs_putpage(pp->mapping->host, pp, wbc, *for_sync);
483 spl_fstrans_unmark(cookie);
484
485 return (ret);
486 }
487
488 #ifdef HAVE_WRITEPAGE_T_FOLIO
489 static int
zpl_putfolio(struct folio * pp,struct writeback_control * wbc,void * data)490 zpl_putfolio(struct folio *pp, struct writeback_control *wbc, void *data)
491 {
492 return (zpl_putpage(&pp->page, wbc, data));
493 }
494 #endif
495
496 static inline int
zpl_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,void * data)497 zpl_write_cache_pages(struct address_space *mapping,
498 struct writeback_control *wbc, void *data)
499 {
500 int result;
501
502 #ifdef HAVE_WRITEPAGE_T_FOLIO
503 result = write_cache_pages(mapping, wbc, zpl_putfolio, data);
504 #else
505 result = write_cache_pages(mapping, wbc, zpl_putpage, data);
506 #endif
507 return (result);
508 }
509
510 static int
zpl_writepages(struct address_space * mapping,struct writeback_control * wbc)511 zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
512 {
513 znode_t *zp = ITOZ(mapping->host);
514 zfsvfs_t *zfsvfs = ITOZSB(mapping->host);
515 enum writeback_sync_modes sync_mode;
516 int result;
517
518 if ((result = zpl_enter(zfsvfs, FTAG)) != 0)
519 return (result);
520 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
521 wbc->sync_mode = WB_SYNC_ALL;
522 zpl_exit(zfsvfs, FTAG);
523 sync_mode = wbc->sync_mode;
524
525 /*
526 * We don't want to run write_cache_pages() in SYNC mode here, because
527 * that would make putpage() wait for a single page to be committed to
528 * disk every single time, resulting in atrocious performance. Instead
529 * we run it once in non-SYNC mode so that the ZIL gets all the data,
530 * and then we commit it all in one go.
531 */
532 boolean_t for_sync = (sync_mode == WB_SYNC_ALL);
533 wbc->sync_mode = WB_SYNC_NONE;
534 result = zpl_write_cache_pages(mapping, wbc, &for_sync);
535 if (sync_mode != wbc->sync_mode) {
536 if ((result = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
537 return (result);
538 if (zfsvfs->z_log != NULL)
539 zil_commit(zfsvfs->z_log, zp->z_id);
540 zpl_exit(zfsvfs, FTAG);
541
542 /*
543 * We need to call write_cache_pages() again (we can't just
544 * return after the commit) because the previous call in
545 * non-SYNC mode does not guarantee that we got all the dirty
546 * pages (see the implementation of write_cache_pages() for
547 * details). That being said, this is a no-op in most cases.
548 */
549 wbc->sync_mode = sync_mode;
550 result = zpl_write_cache_pages(mapping, wbc, &for_sync);
551 }
552 return (result);
553 }
554
555 #ifdef HAVE_VFS_WRITEPAGE
556 /*
557 * Write out dirty pages to the ARC, this function is only required to
558 * support mmap(2). Mapped pages may be dirtied by memory operations
559 * which never call .write(). These dirty pages are kept in sync with
560 * the ARC buffers via this hook.
561 */
562 static int
zpl_writepage(struct page * pp,struct writeback_control * wbc)563 zpl_writepage(struct page *pp, struct writeback_control *wbc)
564 {
565 if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS)
566 wbc->sync_mode = WB_SYNC_ALL;
567
568 boolean_t for_sync = (wbc->sync_mode == WB_SYNC_ALL);
569
570 return (zpl_putpage(pp, wbc, &for_sync));
571 }
572 #endif
573
574 /*
575 * The flag combination which matches the behavior of zfs_space() is
576 * FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE. The FALLOC_FL_PUNCH_HOLE
577 * flag was introduced in the 2.6.38 kernel.
578 *
579 * The original mode=0 (allocate space) behavior can be reasonably emulated
580 * by checking if enough space exists and creating a sparse file, as real
581 * persistent space reservation is not possible due to COW, snapshots, etc.
582 */
583 static long
zpl_fallocate_common(struct inode * ip,int mode,loff_t offset,loff_t len)584 zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
585 {
586 cred_t *cr = CRED();
587 loff_t olen;
588 fstrans_cookie_t cookie;
589 int error = 0;
590
591 int test_mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE;
592
593 if ((mode & ~(FALLOC_FL_KEEP_SIZE | test_mode)) != 0)
594 return (-EOPNOTSUPP);
595
596 if (offset < 0 || len <= 0)
597 return (-EINVAL);
598
599 spl_inode_lock(ip);
600 olen = i_size_read(ip);
601
602 crhold(cr);
603 cookie = spl_fstrans_mark();
604 if (mode & (test_mode)) {
605 flock64_t bf;
606
607 if (mode & FALLOC_FL_KEEP_SIZE) {
608 if (offset > olen)
609 goto out_unmark;
610
611 if (offset + len > olen)
612 len = olen - offset;
613 }
614 bf.l_type = F_WRLCK;
615 bf.l_whence = SEEK_SET;
616 bf.l_start = offset;
617 bf.l_len = len;
618 bf.l_pid = 0;
619
620 error = -zfs_space(ITOZ(ip), F_FREESP, &bf, O_RDWR, offset, cr);
621 } else if ((mode & ~FALLOC_FL_KEEP_SIZE) == 0) {
622 unsigned int percent = zfs_fallocate_reserve_percent;
623 struct kstatfs statfs;
624
625 /* Legacy mode, disable fallocate compatibility. */
626 if (percent == 0) {
627 error = -EOPNOTSUPP;
628 goto out_unmark;
629 }
630
631 /*
632 * Use zfs_statvfs() instead of dmu_objset_space() since it
633 * also checks project quota limits, which are relevant here.
634 */
635 error = zfs_statvfs(ip, &statfs);
636 if (error)
637 goto out_unmark;
638
639 /*
640 * Shrink available space a bit to account for overhead/races.
641 * We know the product previously fit into availbytes from
642 * dmu_objset_space(), so the smaller product will also fit.
643 */
644 if (len > statfs.f_bavail * (statfs.f_bsize * 100 / percent)) {
645 error = -ENOSPC;
646 goto out_unmark;
647 }
648 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > olen)
649 error = zfs_freesp(ITOZ(ip), offset + len, 0, 0, FALSE);
650 }
651 out_unmark:
652 spl_fstrans_unmark(cookie);
653 spl_inode_unlock(ip);
654
655 crfree(cr);
656
657 return (error);
658 }
659
660 static long
zpl_fallocate(struct file * filp,int mode,loff_t offset,loff_t len)661 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
662 {
663 return zpl_fallocate_common(file_inode(filp),
664 mode, offset, len);
665 }
666
667 static int
zpl_ioctl_getversion(struct file * filp,void __user * arg)668 zpl_ioctl_getversion(struct file *filp, void __user *arg)
669 {
670 uint32_t generation = file_inode(filp)->i_generation;
671
672 return (copy_to_user(arg, &generation, sizeof (generation)));
673 }
674
675 static int
zpl_fadvise(struct file * filp,loff_t offset,loff_t len,int advice)676 zpl_fadvise(struct file *filp, loff_t offset, loff_t len, int advice)
677 {
678 struct inode *ip = file_inode(filp);
679 znode_t *zp = ITOZ(ip);
680 zfsvfs_t *zfsvfs = ITOZSB(ip);
681 objset_t *os = zfsvfs->z_os;
682 int error = 0;
683
684 if (S_ISFIFO(ip->i_mode))
685 return (-ESPIPE);
686
687 if (offset < 0 || len < 0)
688 return (-EINVAL);
689
690 if ((error = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
691 return (error);
692
693 switch (advice) {
694 case POSIX_FADV_SEQUENTIAL:
695 case POSIX_FADV_WILLNEED:
696 #ifdef HAVE_GENERIC_FADVISE
697 if (zn_has_cached_data(zp, offset, offset + len - 1))
698 error = generic_fadvise(filp, offset, len, advice);
699 #endif
700 /*
701 * Pass on the caller's size directly, but note that
702 * dmu_prefetch_max will effectively cap it. If there
703 * really is a larger sequential access pattern, perhaps
704 * dmu_zfetch will detect it.
705 */
706 if (len == 0)
707 len = i_size_read(ip) - offset;
708
709 dmu_prefetch(os, zp->z_id, 0, offset, len,
710 ZIO_PRIORITY_ASYNC_READ);
711 break;
712 case POSIX_FADV_NORMAL:
713 case POSIX_FADV_RANDOM:
714 case POSIX_FADV_DONTNEED:
715 case POSIX_FADV_NOREUSE:
716 /* ignored for now */
717 break;
718 default:
719 error = -EINVAL;
720 break;
721 }
722
723 zfs_exit(zfsvfs, FTAG);
724
725 return (error);
726 }
727
728 #define ZFS_FL_USER_VISIBLE (FS_FL_USER_VISIBLE | ZFS_PROJINHERIT_FL)
729 #define ZFS_FL_USER_MODIFIABLE (FS_FL_USER_MODIFIABLE | ZFS_PROJINHERIT_FL)
730
731 static uint32_t
__zpl_ioctl_getflags(struct inode * ip)732 __zpl_ioctl_getflags(struct inode *ip)
733 {
734 uint64_t zfs_flags = ITOZ(ip)->z_pflags;
735 uint32_t ioctl_flags = 0;
736
737 if (zfs_flags & ZFS_IMMUTABLE)
738 ioctl_flags |= FS_IMMUTABLE_FL;
739
740 if (zfs_flags & ZFS_APPENDONLY)
741 ioctl_flags |= FS_APPEND_FL;
742
743 if (zfs_flags & ZFS_NODUMP)
744 ioctl_flags |= FS_NODUMP_FL;
745
746 if (zfs_flags & ZFS_PROJINHERIT)
747 ioctl_flags |= ZFS_PROJINHERIT_FL;
748
749 return (ioctl_flags & ZFS_FL_USER_VISIBLE);
750 }
751
752 /*
753 * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
754 * attributes common to both Linux and Solaris are mapped.
755 */
756 static int
zpl_ioctl_getflags(struct file * filp,void __user * arg)757 zpl_ioctl_getflags(struct file *filp, void __user *arg)
758 {
759 uint32_t flags;
760 int err;
761
762 flags = __zpl_ioctl_getflags(file_inode(filp));
763 err = copy_to_user(arg, &flags, sizeof (flags));
764
765 return (err);
766 }
767
768 /*
769 * fchange() is a helper macro to detect if we have been asked to change a
770 * flag. This is ugly, but the requirement that we do this is a consequence of
771 * how the Linux file attribute interface was designed. Another consequence is
772 * that concurrent modification of files suffers from a TOCTOU race. Neither
773 * are things we can fix without modifying the kernel-userland interface, which
774 * is outside of our jurisdiction.
775 */
776
777 #define fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
778
779 static int
__zpl_ioctl_setflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)780 __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
781 {
782 uint64_t zfs_flags = ITOZ(ip)->z_pflags;
783 xoptattr_t *xoap;
784
785 if (ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
786 ZFS_PROJINHERIT_FL))
787 return (-EOPNOTSUPP);
788
789 if (ioctl_flags & ~ZFS_FL_USER_MODIFIABLE)
790 return (-EACCES);
791
792 if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
793 fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
794 !capable(CAP_LINUX_IMMUTABLE))
795 return (-EPERM);
796
797 if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
798 return (-EACCES);
799
800 xva_init(xva);
801 xoap = xva_getxoptattr(xva);
802
803 #define FLAG_CHANGE(iflag, zflag, xflag, xfield) do { \
804 if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) || \
805 ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) { \
806 XVA_SET_REQ(xva, (xflag)); \
807 (xfield) = ((ioctl_flags & (iflag)) != 0); \
808 } \
809 } while (0)
810
811 FLAG_CHANGE(FS_IMMUTABLE_FL, ZFS_IMMUTABLE, XAT_IMMUTABLE,
812 xoap->xoa_immutable);
813 FLAG_CHANGE(FS_APPEND_FL, ZFS_APPENDONLY, XAT_APPENDONLY,
814 xoap->xoa_appendonly);
815 FLAG_CHANGE(FS_NODUMP_FL, ZFS_NODUMP, XAT_NODUMP,
816 xoap->xoa_nodump);
817 FLAG_CHANGE(ZFS_PROJINHERIT_FL, ZFS_PROJINHERIT, XAT_PROJINHERIT,
818 xoap->xoa_projinherit);
819
820 #undef FLAG_CHANGE
821
822 return (0);
823 }
824
825 static int
zpl_ioctl_setflags(struct file * filp,void __user * arg)826 zpl_ioctl_setflags(struct file *filp, void __user *arg)
827 {
828 struct inode *ip = file_inode(filp);
829 uint32_t flags;
830 cred_t *cr = CRED();
831 xvattr_t xva;
832 int err;
833 fstrans_cookie_t cookie;
834
835 if (copy_from_user(&flags, arg, sizeof (flags)))
836 return (-EFAULT);
837
838 err = __zpl_ioctl_setflags(ip, flags, &xva);
839 if (err)
840 return (err);
841
842 crhold(cr);
843 cookie = spl_fstrans_mark();
844 err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
845 spl_fstrans_unmark(cookie);
846 crfree(cr);
847
848 return (err);
849 }
850
851 static int
zpl_ioctl_getxattr(struct file * filp,void __user * arg)852 zpl_ioctl_getxattr(struct file *filp, void __user *arg)
853 {
854 zfsxattr_t fsx = { 0 };
855 struct inode *ip = file_inode(filp);
856 int err;
857
858 fsx.fsx_xflags = __zpl_ioctl_getflags(ip);
859 fsx.fsx_projid = ITOZ(ip)->z_projid;
860 err = copy_to_user(arg, &fsx, sizeof (fsx));
861
862 return (err);
863 }
864
865 static int
zpl_ioctl_setxattr(struct file * filp,void __user * arg)866 zpl_ioctl_setxattr(struct file *filp, void __user *arg)
867 {
868 struct inode *ip = file_inode(filp);
869 zfsxattr_t fsx;
870 cred_t *cr = CRED();
871 xvattr_t xva;
872 xoptattr_t *xoap;
873 int err;
874 fstrans_cookie_t cookie;
875
876 if (copy_from_user(&fsx, arg, sizeof (fsx)))
877 return (-EFAULT);
878
879 if (!zpl_is_valid_projid(fsx.fsx_projid))
880 return (-EINVAL);
881
882 err = __zpl_ioctl_setflags(ip, fsx.fsx_xflags, &xva);
883 if (err)
884 return (err);
885
886 xoap = xva_getxoptattr(&xva);
887 XVA_SET_REQ(&xva, XAT_PROJID);
888 xoap->xoa_projid = fsx.fsx_projid;
889
890 crhold(cr);
891 cookie = spl_fstrans_mark();
892 err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
893 spl_fstrans_unmark(cookie);
894 crfree(cr);
895
896 return (err);
897 }
898
899 /*
900 * Expose Additional File Level Attributes of ZFS.
901 */
902 static int
zpl_ioctl_getdosflags(struct file * filp,void __user * arg)903 zpl_ioctl_getdosflags(struct file *filp, void __user *arg)
904 {
905 struct inode *ip = file_inode(filp);
906 uint64_t dosflags = ITOZ(ip)->z_pflags;
907 dosflags &= ZFS_DOS_FL_USER_VISIBLE;
908 int err = copy_to_user(arg, &dosflags, sizeof (dosflags));
909
910 return (err);
911 }
912
913 static int
__zpl_ioctl_setdosflags(struct inode * ip,uint64_t ioctl_flags,xvattr_t * xva)914 __zpl_ioctl_setdosflags(struct inode *ip, uint64_t ioctl_flags, xvattr_t *xva)
915 {
916 uint64_t zfs_flags = ITOZ(ip)->z_pflags;
917 xoptattr_t *xoap;
918
919 if (ioctl_flags & (~ZFS_DOS_FL_USER_VISIBLE))
920 return (-EOPNOTSUPP);
921
922 if ((fchange(ioctl_flags, zfs_flags, ZFS_IMMUTABLE, ZFS_IMMUTABLE) ||
923 fchange(ioctl_flags, zfs_flags, ZFS_APPENDONLY, ZFS_APPENDONLY)) &&
924 !capable(CAP_LINUX_IMMUTABLE))
925 return (-EPERM);
926
927 if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
928 return (-EACCES);
929
930 xva_init(xva);
931 xoap = xva_getxoptattr(xva);
932
933 #define FLAG_CHANGE(iflag, xflag, xfield) do { \
934 if (((ioctl_flags & (iflag)) && !(zfs_flags & (iflag))) || \
935 ((zfs_flags & (iflag)) && !(ioctl_flags & (iflag)))) { \
936 XVA_SET_REQ(xva, (xflag)); \
937 (xfield) = ((ioctl_flags & (iflag)) != 0); \
938 } \
939 } while (0)
940
941 FLAG_CHANGE(ZFS_IMMUTABLE, XAT_IMMUTABLE, xoap->xoa_immutable);
942 FLAG_CHANGE(ZFS_APPENDONLY, XAT_APPENDONLY, xoap->xoa_appendonly);
943 FLAG_CHANGE(ZFS_NODUMP, XAT_NODUMP, xoap->xoa_nodump);
944 FLAG_CHANGE(ZFS_READONLY, XAT_READONLY, xoap->xoa_readonly);
945 FLAG_CHANGE(ZFS_HIDDEN, XAT_HIDDEN, xoap->xoa_hidden);
946 FLAG_CHANGE(ZFS_SYSTEM, XAT_SYSTEM, xoap->xoa_system);
947 FLAG_CHANGE(ZFS_ARCHIVE, XAT_ARCHIVE, xoap->xoa_archive);
948 FLAG_CHANGE(ZFS_NOUNLINK, XAT_NOUNLINK, xoap->xoa_nounlink);
949 FLAG_CHANGE(ZFS_REPARSE, XAT_REPARSE, xoap->xoa_reparse);
950 FLAG_CHANGE(ZFS_OFFLINE, XAT_OFFLINE, xoap->xoa_offline);
951 FLAG_CHANGE(ZFS_SPARSE, XAT_SPARSE, xoap->xoa_sparse);
952
953 #undef FLAG_CHANGE
954
955 return (0);
956 }
957
958 /*
959 * Set Additional File Level Attributes of ZFS.
960 */
961 static int
zpl_ioctl_setdosflags(struct file * filp,void __user * arg)962 zpl_ioctl_setdosflags(struct file *filp, void __user *arg)
963 {
964 struct inode *ip = file_inode(filp);
965 uint64_t dosflags;
966 cred_t *cr = CRED();
967 xvattr_t xva;
968 int err;
969 fstrans_cookie_t cookie;
970
971 if (copy_from_user(&dosflags, arg, sizeof (dosflags)))
972 return (-EFAULT);
973
974 err = __zpl_ioctl_setdosflags(ip, dosflags, &xva);
975 if (err)
976 return (err);
977
978 crhold(cr);
979 cookie = spl_fstrans_mark();
980 err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
981 spl_fstrans_unmark(cookie);
982 crfree(cr);
983
984 return (err);
985 }
986
987 static int
zpl_ioctl_rewrite(struct file * filp,void __user * arg)988 zpl_ioctl_rewrite(struct file *filp, void __user *arg)
989 {
990 struct inode *ip = file_inode(filp);
991 zfs_rewrite_args_t args;
992 fstrans_cookie_t cookie;
993 int err;
994
995 if (copy_from_user(&args, arg, sizeof (args)))
996 return (-EFAULT);
997
998 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
999 return (-EBADF);
1000
1001 cookie = spl_fstrans_mark();
1002 err = -zfs_rewrite(ITOZ(ip), args.off, args.len, args.flags, args.arg);
1003 spl_fstrans_unmark(cookie);
1004
1005 return (err);
1006 }
1007
1008 static long
zpl_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1009 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1010 {
1011 switch (cmd) {
1012 case FS_IOC_GETVERSION:
1013 return (zpl_ioctl_getversion(filp, (void *)arg));
1014 case FS_IOC_GETFLAGS:
1015 return (zpl_ioctl_getflags(filp, (void *)arg));
1016 case FS_IOC_SETFLAGS:
1017 return (zpl_ioctl_setflags(filp, (void *)arg));
1018 case ZFS_IOC_FSGETXATTR:
1019 return (zpl_ioctl_getxattr(filp, (void *)arg));
1020 case ZFS_IOC_FSSETXATTR:
1021 return (zpl_ioctl_setxattr(filp, (void *)arg));
1022 case ZFS_IOC_GETDOSFLAGS:
1023 return (zpl_ioctl_getdosflags(filp, (void *)arg));
1024 case ZFS_IOC_SETDOSFLAGS:
1025 return (zpl_ioctl_setdosflags(filp, (void *)arg));
1026 case ZFS_IOC_REWRITE:
1027 return (zpl_ioctl_rewrite(filp, (void *)arg));
1028 default:
1029 return (-ENOTTY);
1030 }
1031 }
1032
1033 #ifdef CONFIG_COMPAT
1034 static long
zpl_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1035 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1036 {
1037 switch (cmd) {
1038 case FS_IOC32_GETVERSION:
1039 cmd = FS_IOC_GETVERSION;
1040 break;
1041 case FS_IOC32_GETFLAGS:
1042 cmd = FS_IOC_GETFLAGS;
1043 break;
1044 case FS_IOC32_SETFLAGS:
1045 cmd = FS_IOC_SETFLAGS;
1046 break;
1047 default:
1048 return (-ENOTTY);
1049 }
1050 return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
1051 }
1052 #endif /* CONFIG_COMPAT */
1053
1054 const struct address_space_operations zpl_address_space_operations = {
1055 #ifdef HAVE_VFS_READPAGES
1056 .readpages = zpl_readpages,
1057 #else
1058 .readahead = zpl_readahead,
1059 #endif
1060 #ifdef HAVE_VFS_READ_FOLIO
1061 .read_folio = zpl_read_folio,
1062 #else
1063 .readpage = zpl_readpage,
1064 #endif
1065 #ifdef HAVE_VFS_WRITEPAGE
1066 .writepage = zpl_writepage,
1067 #endif
1068 .writepages = zpl_writepages,
1069 .direct_IO = zpl_direct_IO,
1070 #ifdef HAVE_VFS_SET_PAGE_DIRTY_NOBUFFERS
1071 .set_page_dirty = __set_page_dirty_nobuffers,
1072 #endif
1073 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
1074 .dirty_folio = filemap_dirty_folio,
1075 #endif
1076 #ifdef HAVE_VFS_MIGRATE_FOLIO
1077 .migrate_folio = migrate_folio,
1078 #elif defined(HAVE_VFS_MIGRATEPAGE)
1079 .migratepage = migrate_page,
1080 #endif
1081 };
1082
1083 const struct file_operations zpl_file_operations = {
1084 .open = zpl_open,
1085 .release = zpl_release,
1086 .llseek = zpl_llseek,
1087 .read_iter = zpl_iter_read,
1088 .write_iter = zpl_iter_write,
1089 #ifdef HAVE_COPY_SPLICE_READ
1090 .splice_read = copy_splice_read,
1091 #else
1092 .splice_read = generic_file_splice_read,
1093 #endif
1094 .splice_write = iter_file_splice_write,
1095 .mmap = zpl_mmap,
1096 .fsync = zpl_fsync,
1097 .fallocate = zpl_fallocate,
1098 .copy_file_range = zpl_copy_file_range,
1099 #ifdef HAVE_VFS_CLONE_FILE_RANGE
1100 .clone_file_range = zpl_clone_file_range,
1101 #endif
1102 #ifdef HAVE_VFS_REMAP_FILE_RANGE
1103 .remap_file_range = zpl_remap_file_range,
1104 #endif
1105 #ifdef HAVE_VFS_DEDUPE_FILE_RANGE
1106 .dedupe_file_range = zpl_dedupe_file_range,
1107 #endif
1108 .fadvise = zpl_fadvise,
1109 .unlocked_ioctl = zpl_ioctl,
1110 #ifdef CONFIG_COMPAT
1111 .compat_ioctl = zpl_compat_ioctl,
1112 #endif
1113 };
1114
1115 const struct file_operations zpl_dir_file_operations = {
1116 .llseek = generic_file_llseek,
1117 .read = generic_read_dir,
1118 .iterate_shared = zpl_iterate,
1119 .fsync = zpl_fsync,
1120 .unlocked_ioctl = zpl_ioctl,
1121 #ifdef CONFIG_COMPAT
1122 .compat_ioctl = zpl_compat_ioctl,
1123 #endif
1124 };
1125
1126 module_param(zfs_fallocate_reserve_percent, uint, 0644);
1127 MODULE_PARM_DESC(zfs_fallocate_reserve_percent,
1128 "Percentage of length to use for the available capacity check");
1129