4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/sysmacros.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zfs_acl.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/fs/zfs.h>
48 #include <sys/dmu_objset.h>
52 #include <sys/policy.h>
53 #include <sys/zfs_vnops.h>
54 #include <sys/zfs_quota.h>
55 #include <sys/zfs_vfsops.h>
56 #include <sys/zfs_znode.h>
59 static ulong_t zfs_fsync_sync_cnt = 4;
62 zfs_fsync(znode_t *zp, int syncflag, cred_t *cr)
64 zfsvfs_t *zfsvfs = ZTOZSB(zp);
66 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
68 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
71 zil_commit(zfsvfs->z_log, zp->z_id);
74 tsd_set(zfs_fsyncer_key, NULL);
80 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
82 * Lseek support for finding holes (cmd == SEEK_HOLE) and
83 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
86 zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off)
88 uint64_t noff = (uint64_t)*off; /* new offset */
94 if (noff >= file_sz) {
95 return (SET_ERROR(ENXIO));
98 if (cmd == F_SEEK_HOLE)
103 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
106 return (SET_ERROR(ENXIO));
108 /* file was dirty, so fall back to using generic logic */
109 if (error == EBUSY) {
117 * We could find a hole that begins after the logical end-of-file,
118 * because dmu_offset_next() only works on whole blocks. If the
119 * EOF falls mid-block, then indicate that the "virtual hole"
120 * at the end of the file begins at the logical EOF, rather than
121 * at the end of the last block.
123 if (noff > file_sz) {
135 zfs_holey(znode_t *zp, ulong_t cmd, loff_t *off)
137 zfsvfs_t *zfsvfs = ZTOZSB(zp);
143 error = zfs_holey_common(zp, cmd, off);
148 #endif /* SEEK_HOLE && SEEK_DATA */
152 zfs_access(znode_t *zp, int mode, int flag, cred_t *cr)
154 zfsvfs_t *zfsvfs = ZTOZSB(zp);
160 if (flag & V_ACE_MASK)
161 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
163 error = zfs_zaccess_rwx(zp, mode, flag, cr);
169 static unsigned long zfs_vnops_read_chunk_size = 1024 * 1024; /* Tunable */
172 * Read bytes from specified file into supplied buffer.
174 * IN: zp - inode of file to be read from.
175 * uio - structure supplying read location, range info,
177 * ioflag - O_SYNC flags; used to provide FRSYNC semantics.
178 * O_DIRECT flag; used to bypass page cache.
179 * cr - credentials of caller.
181 * OUT: uio - updated offset and range, buffer filled.
183 * RETURN: 0 on success, error code on failure.
186 * inode - atime updated if byte count > 0
190 zfs_read(struct znode *zp, uio_t *uio, int ioflag, cred_t *cr)
193 boolean_t frsync = B_FALSE;
195 zfsvfs_t *zfsvfs = ZTOZSB(zp);
199 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
201 return (SET_ERROR(EACCES));
204 /* We don't copy out anything useful for directories. */
205 if (Z_ISDIR(ZTOTYPE(zp))) {
207 return (SET_ERROR(EISDIR));
211 * Validate file offset
213 if (uio->uio_loffset < (offset_t)0) {
215 return (SET_ERROR(EINVAL));
219 * Fasttrack empty reads
221 if (uio->uio_resid == 0) {
228 * If we're in FRSYNC mode, sync out this znode before reading it.
229 * Only do this for non-snapshots.
231 * Some platforms do not support FRSYNC and instead map it
232 * to O_SYNC, which results in unnecessary calls to zil_commit. We
233 * only honor FRSYNC requests on platforms which support it.
235 frsync = !!(ioflag & FRSYNC);
238 (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
239 zil_commit(zfsvfs->z_log, zp->z_id);
242 * Lock the range against changes.
244 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock,
245 uio->uio_loffset, uio->uio_resid, RL_READER);
248 * If we are reading past end-of-file we can skip
249 * to the end; but we might still need to set atime.
251 if (uio->uio_loffset >= zp->z_size) {
256 ASSERT(uio->uio_loffset < zp->z_size);
257 ssize_t n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
258 ssize_t start_resid = n;
261 ssize_t nbytes = MIN(n, zfs_vnops_read_chunk_size -
262 P2PHASE(uio->uio_loffset, zfs_vnops_read_chunk_size));
264 if (uio->uio_segflg == UIO_NOCOPY)
265 error = mappedread_sf(zp, nbytes, uio);
268 if (zn_has_cached_data(zp) && !(ioflag & O_DIRECT)) {
269 error = mappedread(zp, nbytes, uio);
271 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
276 /* convert checksum errors into IO errors */
278 error = SET_ERROR(EIO);
285 int64_t nread = start_resid - n;
286 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread);
287 task_io_account_read(nread);
289 zfs_rangelock_exit(lr);
291 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
297 * Write the bytes to a file.
299 * IN: zp - znode of file to be written to.
300 * uio - structure supplying write location, range info,
302 * ioflag - O_APPEND flag set if in append mode.
303 * O_DIRECT flag; used to bypass page cache.
304 * cr - credentials of caller.
306 * OUT: uio - updated offset and range.
308 * RETURN: 0 if success
309 * error code if failure
312 * ip - ctime|mtime updated if byte count > 0
317 zfs_write(znode_t *zp, uio_t *uio, int ioflag, cred_t *cr)
320 ssize_t start_resid = uio->uio_resid;
323 * Fasttrack empty write
325 ssize_t n = start_resid;
329 rlim64_t limit = MAXOFFSET_T;
331 zfsvfs_t *zfsvfs = ZTOZSB(zp);
335 sa_bulk_attr_t bulk[4];
337 uint64_t mtime[2], ctime[2];
338 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
339 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
340 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
342 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
346 * Callers might not be able to detect properly that we are read-only,
347 * so check it explicitly here.
349 if (zfs_is_readonly(zfsvfs)) {
351 return (SET_ERROR(EROFS));
355 * If immutable or not appending then return EPERM
357 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
358 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & O_APPEND) &&
359 (uio->uio_loffset < zp->z_size))) {
361 return (SET_ERROR(EPERM));
365 * Validate file offset
367 offset_t woff = ioflag & O_APPEND ? zp->z_size : uio->uio_loffset;
370 return (SET_ERROR(EINVAL));
373 int max_blksz = zfsvfs->z_max_blksz;
376 * Pre-fault the pages to ensure slow (eg NFS) pages
378 * Skip this if uio contains loaned arc_buf.
380 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
382 return (SET_ERROR(EFAULT));
386 * If in append mode, set the io offset pointer to eof.
388 zfs_locked_range_t *lr;
389 if (ioflag & O_APPEND) {
391 * Obtain an appending range lock to guarantee file append
392 * semantics. We reset the write offset once we have the lock.
394 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
395 woff = lr->lr_offset;
396 if (lr->lr_length == UINT64_MAX) {
398 * We overlocked the file because this write will cause
399 * the file block size to increase.
400 * Note that zp_size cannot change with this lock held.
404 uio->uio_loffset = woff;
407 * Note that if the file block size will change as a result of
408 * this write, then this range lock will lock the entire file
409 * so that we can re-write the block safely.
411 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
414 if (zn_rlimit_fsize(zp, uio, uio->uio_td)) {
415 zfs_rangelock_exit(lr);
421 zfs_rangelock_exit(lr);
423 return (SET_ERROR(EFBIG));
426 if ((woff + n) > limit || woff > (limit - n))
429 uint64_t end_size = MAX(zp->z_size, woff + n);
430 zilog_t *zilog = zfsvfs->z_log;
433 * Write the file in reasonable size chunks. Each chunk is written
434 * in a separate transaction; this keeps the intent log records small
435 * and allows us to do more fine-grained space accounting.
438 woff = uio->uio_loffset;
440 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
441 KUID_TO_SUID(ZTOUID(zp))) ||
442 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
443 KGID_TO_SGID(ZTOGID(zp))) ||
444 (zp->z_projid != ZFS_DEFAULT_PROJID &&
445 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
447 error = SET_ERROR(EDQUOT);
451 arc_buf_t *abuf = NULL;
452 if (n >= max_blksz && woff >= zp->z_size &&
453 P2PHASE(woff, max_blksz) == 0 &&
454 zp->z_blksz == max_blksz) {
456 * This write covers a full block. "Borrow" a buffer
457 * from the dmu so that we can fill it before we enter
458 * a transaction. This avoids the possibility of
459 * holding up the transaction if the data copy hangs
460 * up on a pagefault (e.g., from an NFS server mapping).
464 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
466 ASSERT(abuf != NULL);
467 ASSERT(arc_buf_size(abuf) == max_blksz);
468 if ((error = uiocopy(abuf->b_data, max_blksz,
469 UIO_WRITE, uio, &cbytes))) {
470 dmu_return_arcbuf(abuf);
473 ASSERT(cbytes == max_blksz);
477 * Start a transaction.
479 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
480 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
481 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
483 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff,
486 zfs_sa_upgrade_txholds(tx, zp);
487 error = dmu_tx_assign(tx, TXG_WAIT);
491 dmu_return_arcbuf(abuf);
496 * If rangelock_enter() over-locked we grow the blocksize
497 * and then reduce the lock range. This will only happen
498 * on the first iteration since rangelock_reduce() will
499 * shrink down lr_length to the appropriate size.
501 if (lr->lr_length == UINT64_MAX) {
504 if (zp->z_blksz > max_blksz) {
506 * File's blocksize is already larger than the
507 * "recordsize" property. Only let it grow to
508 * the next power of 2.
510 ASSERT(!ISP2(zp->z_blksz));
511 new_blksz = MIN(end_size,
512 1 << highbit64(zp->z_blksz));
514 new_blksz = MIN(end_size, max_blksz);
516 zfs_grow_blocksize(zp, new_blksz, tx);
517 zfs_rangelock_reduce(lr, woff, n);
521 * XXX - should we really limit each write to z_max_blksz?
522 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
524 ssize_t nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
528 tx_bytes = uio->uio_resid;
529 uio_fault_disable(uio, B_TRUE);
530 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
532 uio_fault_disable(uio, B_FALSE);
534 if (error == EFAULT) {
537 * Account for partial writes before
538 * continuing the loop.
539 * Update needs to occur before the next
540 * uio_prefaultpages, or prefaultpages may
541 * error, and we may break the loop early.
543 if (tx_bytes != uio->uio_resid)
544 n -= tx_bytes - uio->uio_resid;
545 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
555 tx_bytes -= uio->uio_resid;
558 * Is this block ever reached?
562 * If this is not a full block write, but we are
563 * extending the file past EOF and this data starts
564 * block-aligned, use assign_arcbuf(). Otherwise,
565 * write via dmu_write().
568 if (tx_bytes == max_blksz) {
569 error = dmu_assign_arcbuf_by_dbuf(
570 sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
572 dmu_return_arcbuf(abuf);
577 ASSERT(tx_bytes <= uio->uio_resid);
578 uioskip(uio, tx_bytes);
580 if (tx_bytes && zn_has_cached_data(zp) &&
581 !(ioflag & O_DIRECT)) {
582 update_pages(zp, woff,
583 tx_bytes, zfsvfs->z_os, zp->z_id);
587 * If we made no progress, we're done. If we made even
588 * partial progress, update the znode and ZIL accordingly.
591 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
592 (void *)&zp->z_size, sizeof (uint64_t), tx);
599 * Clear Set-UID/Set-GID bits on successful write if not
600 * privileged and at least one of the execute bits is set.
602 * It would be nice to do this after all writes have
603 * been done, but that would still expose the ISUID/ISGID
604 * to another app after the partial write is committed.
606 * Note: we don't call zfs_fuid_map_id() here because
607 * user 0 is not an ephemeral uid.
609 mutex_enter(&zp->z_acl_lock);
610 uint32_t uid = KUID_TO_SUID(ZTOUID(zp));
611 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
612 (S_IXUSR >> 6))) != 0 &&
613 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
614 secpolicy_vnode_setid_retain(zp, cr,
615 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
617 zp->z_mode &= ~(S_ISUID | S_ISGID);
618 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
619 (void *)&newmode, sizeof (uint64_t), tx);
621 mutex_exit(&zp->z_acl_lock);
623 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
626 * Update the file size (zp_size) if it has changed;
627 * account for possible concurrent updates.
629 while ((end_size = zp->z_size) < uio->uio_loffset) {
630 (void) atomic_cas_64(&zp->z_size, end_size,
635 * If we are replaying and eof is non zero then force
636 * the file size to the specified eof. Note, there's no
637 * concurrency during replay.
639 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
640 zp->z_size = zfsvfs->z_replay_eof;
642 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
644 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
650 ASSERT(tx_bytes == nbytes);
654 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
661 zfs_inode_update(zp);
662 zfs_rangelock_exit(lr);
665 * If we're in replay mode, or we made no progress, return error.
666 * Otherwise, it's at least a partial write, so it's successful.
668 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
673 if (ioflag & (O_SYNC | O_DSYNC) ||
674 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
675 zil_commit(zilog, zp->z_id);
677 int64_t nwritten = start_resid - uio->uio_resid;
678 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten);
679 task_io_account_write(nwritten);
687 zfs_getsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
689 zfsvfs_t *zfsvfs = ZTOZSB(zp);
691 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
695 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
703 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
705 zfsvfs_t *zfsvfs = ZTOZSB(zp);
707 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
708 zilog_t *zilog = zfsvfs->z_log;
713 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
715 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
716 zil_commit(zilog, 0);
723 static int zil_fault_io = 0;
726 static void zfs_get_done(zgd_t *zgd, int error);
729 * Get data to generate a TX_WRITE intent log record.
732 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
734 zfsvfs_t *zfsvfs = arg;
735 objset_t *os = zfsvfs->z_os;
737 uint64_t object = lr->lr_foid;
738 uint64_t offset = lr->lr_offset;
739 uint64_t size = lr->lr_length;
744 ASSERT3P(lwb, !=, NULL);
745 ASSERT3P(zio, !=, NULL);
746 ASSERT3U(size, !=, 0);
749 * Nothing to do if the file has been removed
751 if (zfs_zget(zfsvfs, object, &zp) != 0)
752 return (SET_ERROR(ENOENT));
753 if (zp->z_unlinked) {
755 * Release the vnode asynchronously as we currently have the
756 * txg stopped from syncing.
759 return (SET_ERROR(ENOENT));
762 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
764 zgd->zgd_private = zp;
767 * Write records come in two flavors: immediate and indirect.
768 * For small writes it's cheaper to store the data with the
769 * log record (immediate); for large writes it's cheaper to
770 * sync the data and get a pointer to it (indirect) so that
771 * we don't have to write the data twice.
773 if (buf != NULL) { /* immediate write */
774 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock,
775 offset, size, RL_READER);
776 /* test for truncation needs to be done while range locked */
777 if (offset >= zp->z_size) {
778 error = SET_ERROR(ENOENT);
780 error = dmu_read(os, object, offset, size, buf,
781 DMU_READ_NO_PREFETCH);
783 ASSERT(error == 0 || error == ENOENT);
784 } else { /* indirect write */
786 * Have to lock the whole block to ensure when it's
787 * written out and its checksum is being calculated
788 * that no one can change the data. We need to re-check
789 * blocksize after we get the lock in case it's changed!
794 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
796 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock,
797 offset, size, RL_READER);
798 if (zp->z_blksz == size)
801 zfs_rangelock_exit(zgd->zgd_lr);
803 /* test for truncation needs to be done while range locked */
804 if (lr->lr_offset >= zp->z_size)
805 error = SET_ERROR(ENOENT);
808 error = SET_ERROR(EIO);
813 error = dmu_buf_hold(os, object, offset, zgd, &db,
814 DMU_READ_NO_PREFETCH);
817 blkptr_t *bp = &lr->lr_blkptr;
822 ASSERT(db->db_offset == offset);
823 ASSERT(db->db_size == size);
825 error = dmu_sync(zio, lr->lr_common.lrc_txg,
827 ASSERT(error || lr->lr_length <= size);
830 * On success, we need to wait for the write I/O
831 * initiated by dmu_sync() to complete before we can
832 * release this dbuf. We will finish everything up
833 * in the zfs_get_done() callback.
838 if (error == EALREADY) {
839 lr->lr_common.lrc_txtype = TX_WRITE2;
841 * TX_WRITE2 relies on the data previously
842 * written by the TX_WRITE that caused
843 * EALREADY. We zero out the BP because
844 * it is the old, currently-on-disk BP.
853 zfs_get_done(zgd, error);
861 zfs_get_done(zgd_t *zgd, int error)
863 znode_t *zp = zgd->zgd_private;
866 dmu_buf_rele(zgd->zgd_db, zgd);
868 zfs_rangelock_exit(zgd->zgd_lr);
871 * Release the vnode asynchronously as we currently have the
872 * txg stopped from syncing.
876 kmem_free(zgd, sizeof (zgd_t));
879 EXPORT_SYMBOL(zfs_access);
880 EXPORT_SYMBOL(zfs_fsync);
881 EXPORT_SYMBOL(zfs_holey);
882 EXPORT_SYMBOL(zfs_read);
883 EXPORT_SYMBOL(zfs_write);
884 EXPORT_SYMBOL(zfs_getsecattr);
885 EXPORT_SYMBOL(zfs_setsecattr);
887 ZFS_MODULE_PARAM(zfs_vnops, zfs_vnops_, read_chunk_size, ULONG, ZMOD_RW,
888 "Bytes to read per chunk");