]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/zfs_vnops.c
Consolidate zfs_holey and zfs_access
[FreeBSD/FreeBSD.git] / module / zfs / zfs_vnops.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
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.
27  */
28
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/sysmacros.h>
36 #include <sys/vfs.h>
37 #include <sys/uio.h>
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 #include <sys/kmem.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>
47 #include <sys/dmu.h>
48 #include <sys/dmu_objset.h>
49 #include <sys/spa.h>
50 #include <sys/txg.h>
51 #include <sys/dbuf.h>
52 #include <sys/policy.h>
53 #include <sys/zfs_vnops.h>
54 #include <sys/zfs_quota.h>
55
56
57 static ulong_t zfs_fsync_sync_cnt = 4;
58
59 int
60 zfs_fsync(znode_t *zp, int syncflag, cred_t *cr)
61 {
62         zfsvfs_t *zfsvfs = ZTOZSB(zp);
63
64         (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
65
66         if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
67                 ZFS_ENTER(zfsvfs);
68                 ZFS_VERIFY_ZP(zp);
69                 zil_commit(zfsvfs->z_log, zp->z_id);
70                 ZFS_EXIT(zfsvfs);
71         }
72         tsd_set(zfs_fsyncer_key, NULL);
73
74         return (0);
75 }
76
77
78 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
79 /*
80  * Lseek support for finding holes (cmd == SEEK_HOLE) and
81  * data (cmd == SEEK_DATA). "off" is an in/out parameter.
82  */
83 static int
84 zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off)
85 {
86         uint64_t noff = (uint64_t)*off; /* new offset */
87         uint64_t file_sz;
88         int error;
89         boolean_t hole;
90
91         file_sz = zp->z_size;
92         if (noff >= file_sz)  {
93                 return (SET_ERROR(ENXIO));
94         }
95
96         if (cmd == F_SEEK_HOLE)
97                 hole = B_TRUE;
98         else
99                 hole = B_FALSE;
100
101         error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
102
103         if (error == ESRCH)
104                 return (SET_ERROR(ENXIO));
105
106         /* file was dirty, so fall back to using generic logic */
107         if (error == EBUSY) {
108                 if (hole)
109                         *off = file_sz;
110
111                 return (0);
112         }
113
114         /*
115          * We could find a hole that begins after the logical end-of-file,
116          * because dmu_offset_next() only works on whole blocks.  If the
117          * EOF falls mid-block, then indicate that the "virtual hole"
118          * at the end of the file begins at the logical EOF, rather than
119          * at the end of the last block.
120          */
121         if (noff > file_sz) {
122                 ASSERT(hole);
123                 noff = file_sz;
124         }
125
126         if (noff < *off)
127                 return (error);
128         *off = noff;
129         return (error);
130 }
131
132 int
133 zfs_holey(znode_t *zp, ulong_t cmd, loff_t *off)
134 {
135         zfsvfs_t *zfsvfs = ZTOZSB(zp);
136         int error;
137
138         ZFS_ENTER(zfsvfs);
139         ZFS_VERIFY_ZP(zp);
140
141         error = zfs_holey_common(zp, cmd, off);
142
143         ZFS_EXIT(zfsvfs);
144         return (error);
145 }
146 #endif /* SEEK_HOLE && SEEK_DATA */
147
148 /*ARGSUSED*/
149 int
150 zfs_access(znode_t *zp, int mode, int flag, cred_t *cr)
151 {
152         zfsvfs_t *zfsvfs = ZTOZSB(zp);
153         int error;
154
155         ZFS_ENTER(zfsvfs);
156         ZFS_VERIFY_ZP(zp);
157
158         if (flag & V_ACE_MASK)
159                 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
160         else
161                 error = zfs_zaccess_rwx(zp, mode, flag, cr);
162
163         ZFS_EXIT(zfsvfs);
164         return (error);
165 }
166
167 static unsigned long zfs_vnops_read_chunk_size = 1024 * 1024; /* Tunable */
168
169 /*
170  * Read bytes from specified file into supplied buffer.
171  *
172  *      IN:     zp      - inode of file to be read from.
173  *              uio     - structure supplying read location, range info,
174  *                        and return buffer.
175  *              ioflag  - O_SYNC flags; used to provide FRSYNC semantics.
176  *                        O_DIRECT flag; used to bypass page cache.
177  *              cr      - credentials of caller.
178  *
179  *      OUT:    uio     - updated offset and range, buffer filled.
180  *
181  *      RETURN: 0 on success, error code on failure.
182  *
183  * Side Effects:
184  *      inode - atime updated if byte count > 0
185  */
186 /* ARGSUSED */
187 int
188 zfs_read(struct znode *zp, uio_t *uio, int ioflag, cred_t *cr)
189 {
190         int error = 0;
191         boolean_t frsync = B_FALSE;
192
193         zfsvfs_t *zfsvfs = ZTOZSB(zp);
194         ZFS_ENTER(zfsvfs);
195         ZFS_VERIFY_ZP(zp);
196
197         if (zp->z_pflags & ZFS_AV_QUARANTINED) {
198                 ZFS_EXIT(zfsvfs);
199                 return (SET_ERROR(EACCES));
200         }
201
202         /* We don't copy out anything useful for directories. */
203         if (Z_ISDIR(ZTOTYPE(zp))) {
204                 ZFS_EXIT(zfsvfs);
205                 return (SET_ERROR(EISDIR));
206         }
207
208         /*
209          * Validate file offset
210          */
211         if (uio->uio_loffset < (offset_t)0) {
212                 ZFS_EXIT(zfsvfs);
213                 return (SET_ERROR(EINVAL));
214         }
215
216         /*
217          * Fasttrack empty reads
218          */
219         if (uio->uio_resid == 0) {
220                 ZFS_EXIT(zfsvfs);
221                 return (0);
222         }
223
224 #ifdef FRSYNC
225         /*
226          * If we're in FRSYNC mode, sync out this znode before reading it.
227          * Only do this for non-snapshots.
228          *
229          * Some platforms do not support FRSYNC and instead map it
230          * to O_SYNC, which results in unnecessary calls to zil_commit. We
231          * only honor FRSYNC requests on platforms which support it.
232          */
233         frsync = !!(ioflag & FRSYNC);
234 #endif
235         if (zfsvfs->z_log &&
236             (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
237                 zil_commit(zfsvfs->z_log, zp->z_id);
238
239         /*
240          * Lock the range against changes.
241          */
242         zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock,
243             uio->uio_loffset, uio->uio_resid, RL_READER);
244
245         /*
246          * If we are reading past end-of-file we can skip
247          * to the end; but we might still need to set atime.
248          */
249         if (uio->uio_loffset >= zp->z_size) {
250                 error = 0;
251                 goto out;
252         }
253
254         ASSERT(uio->uio_loffset < zp->z_size);
255         ssize_t n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
256         ssize_t start_resid = n;
257
258         while (n > 0) {
259                 ssize_t nbytes = MIN(n, zfs_vnops_read_chunk_size -
260                     P2PHASE(uio->uio_loffset, zfs_vnops_read_chunk_size));
261 #ifdef UIO_NOCOPY
262                 if (uio->uio_segflg == UIO_NOCOPY)
263                         error = mappedread_sf(zp, nbytes, uio);
264                 else
265 #endif
266                 if (zn_has_cached_data(zp) && !(ioflag & O_DIRECT)) {
267                         error = mappedread(zp, nbytes, uio);
268                 } else {
269                         error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
270                             uio, nbytes);
271                 }
272
273                 if (error) {
274                         /* convert checksum errors into IO errors */
275                         if (error == ECKSUM)
276                                 error = SET_ERROR(EIO);
277                         break;
278                 }
279
280                 n -= nbytes;
281         }
282
283         int64_t nread = start_resid - n;
284         dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread);
285         task_io_account_read(nread);
286 out:
287         zfs_rangelock_exit(lr);
288
289         ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
290         ZFS_EXIT(zfsvfs);
291         return (error);
292 }
293
294 /*
295  * Write the bytes to a file.
296  *
297  *      IN:     zp      - znode of file to be written to.
298  *              uio     - structure supplying write location, range info,
299  *                        and data buffer.
300  *              ioflag  - O_APPEND flag set if in append mode.
301  *                        O_DIRECT flag; used to bypass page cache.
302  *              cr      - credentials of caller.
303  *
304  *      OUT:    uio     - updated offset and range.
305  *
306  *      RETURN: 0 if success
307  *              error code if failure
308  *
309  * Timestamps:
310  *      ip - ctime|mtime updated if byte count > 0
311  */
312
313 /* ARGSUSED */
314 int
315 zfs_write(znode_t *zp, uio_t *uio, int ioflag, cred_t *cr)
316 {
317         int error = 0;
318         ssize_t start_resid = uio->uio_resid;
319
320         /*
321          * Fasttrack empty write
322          */
323         ssize_t n = start_resid;
324         if (n == 0)
325                 return (0);
326
327         rlim64_t limit = MAXOFFSET_T;
328
329         zfsvfs_t *zfsvfs = ZTOZSB(zp);
330         ZFS_ENTER(zfsvfs);
331         ZFS_VERIFY_ZP(zp);
332
333         sa_bulk_attr_t bulk[4];
334         int count = 0;
335         uint64_t mtime[2], ctime[2];
336         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
337         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
338         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
339             &zp->z_size, 8);
340         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
341             &zp->z_pflags, 8);
342
343         /*
344          * Callers might not be able to detect properly that we are read-only,
345          * so check it explicitly here.
346          */
347         if (zfs_is_readonly(zfsvfs)) {
348                 ZFS_EXIT(zfsvfs);
349                 return (SET_ERROR(EROFS));
350         }
351
352         /*
353          * If immutable or not appending then return EPERM
354          */
355         if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
356             ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & O_APPEND) &&
357             (uio->uio_loffset < zp->z_size))) {
358                 ZFS_EXIT(zfsvfs);
359                 return (SET_ERROR(EPERM));
360         }
361
362         /*
363          * Validate file offset
364          */
365         offset_t woff = ioflag & O_APPEND ? zp->z_size : uio->uio_loffset;
366         if (woff < 0) {
367                 ZFS_EXIT(zfsvfs);
368                 return (SET_ERROR(EINVAL));
369         }
370
371         int max_blksz = zfsvfs->z_max_blksz;
372
373         /*
374          * Pre-fault the pages to ensure slow (eg NFS) pages
375          * don't hold up txg.
376          * Skip this if uio contains loaned arc_buf.
377          */
378         if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
379                 ZFS_EXIT(zfsvfs);
380                 return (SET_ERROR(EFAULT));
381         }
382
383         /*
384          * If in append mode, set the io offset pointer to eof.
385          */
386         zfs_locked_range_t *lr;
387         if (ioflag & O_APPEND) {
388                 /*
389                  * Obtain an appending range lock to guarantee file append
390                  * semantics.  We reset the write offset once we have the lock.
391                  */
392                 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
393                 woff = lr->lr_offset;
394                 if (lr->lr_length == UINT64_MAX) {
395                         /*
396                          * We overlocked the file because this write will cause
397                          * the file block size to increase.
398                          * Note that zp_size cannot change with this lock held.
399                          */
400                         woff = zp->z_size;
401                 }
402                 uio->uio_loffset = woff;
403         } else {
404                 /*
405                  * Note that if the file block size will change as a result of
406                  * this write, then this range lock will lock the entire file
407                  * so that we can re-write the block safely.
408                  */
409                 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
410         }
411
412         if (zn_rlimit_fsize(zp, uio, uio->uio_td)) {
413                 zfs_rangelock_exit(lr);
414                 ZFS_EXIT(zfsvfs);
415                 return (EFBIG);
416         }
417
418         if (woff >= limit) {
419                 zfs_rangelock_exit(lr);
420                 ZFS_EXIT(zfsvfs);
421                 return (SET_ERROR(EFBIG));
422         }
423
424         if ((woff + n) > limit || woff > (limit - n))
425                 n = limit - woff;
426
427         uint64_t end_size = MAX(zp->z_size, woff + n);
428         zilog_t *zilog = zfsvfs->z_log;
429
430         /*
431          * Write the file in reasonable size chunks.  Each chunk is written
432          * in a separate transaction; this keeps the intent log records small
433          * and allows us to do more fine-grained space accounting.
434          */
435         while (n > 0) {
436                 woff = uio->uio_loffset;
437
438                 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
439                     KUID_TO_SUID(ZTOUID(zp))) ||
440                     zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
441                     KGID_TO_SGID(ZTOGID(zp))) ||
442                     (zp->z_projid != ZFS_DEFAULT_PROJID &&
443                     zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
444                     zp->z_projid))) {
445                         error = SET_ERROR(EDQUOT);
446                         break;
447                 }
448
449                 arc_buf_t *abuf = NULL;
450                 if (n >= max_blksz && woff >= zp->z_size &&
451                     P2PHASE(woff, max_blksz) == 0 &&
452                     zp->z_blksz == max_blksz) {
453                         /*
454                          * This write covers a full block.  "Borrow" a buffer
455                          * from the dmu so that we can fill it before we enter
456                          * a transaction.  This avoids the possibility of
457                          * holding up the transaction if the data copy hangs
458                          * up on a pagefault (e.g., from an NFS server mapping).
459                          */
460                         size_t cbytes;
461
462                         abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
463                             max_blksz);
464                         ASSERT(abuf != NULL);
465                         ASSERT(arc_buf_size(abuf) == max_blksz);
466                         if ((error = uiocopy(abuf->b_data, max_blksz,
467                             UIO_WRITE, uio, &cbytes))) {
468                                 dmu_return_arcbuf(abuf);
469                                 break;
470                         }
471                         ASSERT(cbytes == max_blksz);
472                 }
473
474                 /*
475                  * Start a transaction.
476                  */
477                 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
478                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
479                 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
480                 DB_DNODE_ENTER(db);
481                 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff,
482                     MIN(n, max_blksz));
483                 DB_DNODE_EXIT(db);
484                 zfs_sa_upgrade_txholds(tx, zp);
485                 error = dmu_tx_assign(tx, TXG_WAIT);
486                 if (error) {
487                         dmu_tx_abort(tx);
488                         if (abuf != NULL)
489                                 dmu_return_arcbuf(abuf);
490                         break;
491                 }
492
493                 /*
494                  * If rangelock_enter() over-locked we grow the blocksize
495                  * and then reduce the lock range.  This will only happen
496                  * on the first iteration since rangelock_reduce() will
497                  * shrink down lr_length to the appropriate size.
498                  */
499                 if (lr->lr_length == UINT64_MAX) {
500                         uint64_t new_blksz;
501
502                         if (zp->z_blksz > max_blksz) {
503                                 /*
504                                  * File's blocksize is already larger than the
505                                  * "recordsize" property.  Only let it grow to
506                                  * the next power of 2.
507                                  */
508                                 ASSERT(!ISP2(zp->z_blksz));
509                                 new_blksz = MIN(end_size,
510                                     1 << highbit64(zp->z_blksz));
511                         } else {
512                                 new_blksz = MIN(end_size, max_blksz);
513                         }
514                         zfs_grow_blocksize(zp, new_blksz, tx);
515                         zfs_rangelock_reduce(lr, woff, n);
516                 }
517
518                 /*
519                  * XXX - should we really limit each write to z_max_blksz?
520                  * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
521                  */
522                 ssize_t nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
523
524                 ssize_t tx_bytes;
525                 if (abuf == NULL) {
526                         tx_bytes = uio->uio_resid;
527                         uio_fault_disable(uio, B_TRUE);
528                         error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
529                             uio, nbytes, tx);
530                         uio_fault_disable(uio, B_FALSE);
531 #ifdef __linux__
532                         if (error == EFAULT) {
533                                 dmu_tx_commit(tx);
534                                 /*
535                                  * Account for partial writes before
536                                  * continuing the loop.
537                                  * Update needs to occur before the next
538                                  * uio_prefaultpages, or prefaultpages may
539                                  * error, and we may break the loop early.
540                                  */
541                                 if (tx_bytes != uio->uio_resid)
542                                         n -= tx_bytes - uio->uio_resid;
543                                 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
544                                         break;
545                                 }
546                                 continue;
547                         }
548 #endif
549                         if (error != 0) {
550                                 dmu_tx_commit(tx);
551                                 break;
552                         }
553                         tx_bytes -= uio->uio_resid;
554                 } else {
555                         /*
556                          * Is this block ever reached?
557                          */
558                         tx_bytes = nbytes;
559                         /*
560                          * If this is not a full block write, but we are
561                          * extending the file past EOF and this data starts
562                          * block-aligned, use assign_arcbuf().  Otherwise,
563                          * write via dmu_write().
564                          */
565
566                         if (tx_bytes == max_blksz) {
567                                 error = dmu_assign_arcbuf_by_dbuf(
568                                     sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
569                                 if (error != 0) {
570                                         dmu_return_arcbuf(abuf);
571                                         dmu_tx_commit(tx);
572                                         break;
573                                 }
574                         }
575                         ASSERT(tx_bytes <= uio->uio_resid);
576                         uioskip(uio, tx_bytes);
577                 }
578                 if (tx_bytes && zn_has_cached_data(zp) &&
579                     !(ioflag & O_DIRECT)) {
580                         update_pages(zp, woff,
581                             tx_bytes, zfsvfs->z_os, zp->z_id);
582                 }
583
584                 /*
585                  * If we made no progress, we're done.  If we made even
586                  * partial progress, update the znode and ZIL accordingly.
587                  */
588                 if (tx_bytes == 0) {
589                         (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
590                             (void *)&zp->z_size, sizeof (uint64_t), tx);
591                         dmu_tx_commit(tx);
592                         ASSERT(error != 0);
593                         break;
594                 }
595
596                 /*
597                  * Clear Set-UID/Set-GID bits on successful write if not
598                  * privileged and at least one of the execute bits is set.
599                  *
600                  * It would be nice to do this after all writes have
601                  * been done, but that would still expose the ISUID/ISGID
602                  * to another app after the partial write is committed.
603                  *
604                  * Note: we don't call zfs_fuid_map_id() here because
605                  * user 0 is not an ephemeral uid.
606                  */
607                 mutex_enter(&zp->z_acl_lock);
608                 uint32_t uid = KUID_TO_SUID(ZTOUID(zp));
609                 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
610                     (S_IXUSR >> 6))) != 0 &&
611                     (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
612                     secpolicy_vnode_setid_retain(zp, cr,
613                     ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
614                         uint64_t newmode;
615                         zp->z_mode &= ~(S_ISUID | S_ISGID);
616                         (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
617                             (void *)&newmode, sizeof (uint64_t), tx);
618                 }
619                 mutex_exit(&zp->z_acl_lock);
620
621                 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
622
623                 /*
624                  * Update the file size (zp_size) if it has changed;
625                  * account for possible concurrent updates.
626                  */
627                 while ((end_size = zp->z_size) < uio->uio_loffset) {
628                         (void) atomic_cas_64(&zp->z_size, end_size,
629                             uio->uio_loffset);
630                         ASSERT(error == 0);
631                 }
632                 /*
633                  * If we are replaying and eof is non zero then force
634                  * the file size to the specified eof. Note, there's no
635                  * concurrency during replay.
636                  */
637                 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
638                         zp->z_size = zfsvfs->z_replay_eof;
639
640                 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
641
642                 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
643                     NULL, NULL);
644                 dmu_tx_commit(tx);
645
646                 if (error != 0)
647                         break;
648                 ASSERT(tx_bytes == nbytes);
649                 n -= nbytes;
650
651                 if (n > 0) {
652                         if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
653                                 error = EFAULT;
654                                 break;
655                         }
656                 }
657         }
658
659         zfs_inode_update(zp);
660         zfs_rangelock_exit(lr);
661
662         /*
663          * If we're in replay mode, or we made no progress, return error.
664          * Otherwise, it's at least a partial write, so it's successful.
665          */
666         if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
667                 ZFS_EXIT(zfsvfs);
668                 return (error);
669         }
670
671         if (ioflag & (O_SYNC | O_DSYNC) ||
672             zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
673                 zil_commit(zilog, zp->z_id);
674
675         int64_t nwritten = start_resid - uio->uio_resid;
676         dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten);
677         task_io_account_write(nwritten);
678
679         ZFS_EXIT(zfsvfs);
680         return (0);
681 }
682
683 /*ARGSUSED*/
684 int
685 zfs_getsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
686 {
687         zfsvfs_t *zfsvfs = ZTOZSB(zp);
688         int error;
689         boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
690
691         ZFS_ENTER(zfsvfs);
692         ZFS_VERIFY_ZP(zp);
693         error = zfs_getacl(zp, vsecp, skipaclchk, cr);
694         ZFS_EXIT(zfsvfs);
695
696         return (error);
697 }
698
699 /*ARGSUSED*/
700 int
701 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
702 {
703         zfsvfs_t *zfsvfs = ZTOZSB(zp);
704         int error;
705         boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
706         zilog_t *zilog = zfsvfs->z_log;
707
708         ZFS_ENTER(zfsvfs);
709         ZFS_VERIFY_ZP(zp);
710
711         error = zfs_setacl(zp, vsecp, skipaclchk, cr);
712
713         if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
714                 zil_commit(zilog, 0);
715
716         ZFS_EXIT(zfsvfs);
717         return (error);
718 }
719
720 EXPORT_SYMBOL(zfs_access);
721 EXPORT_SYMBOL(zfs_fsync);
722 EXPORT_SYMBOL(zfs_holey);
723 EXPORT_SYMBOL(zfs_read);
724 EXPORT_SYMBOL(zfs_write);
725 EXPORT_SYMBOL(zfs_getsecattr);
726 EXPORT_SYMBOL(zfs_setsecattr);
727
728 ZFS_MODULE_PARAM(zfs_vnops, zfs_vnops_, read_chunk_size, ULONG, ZMOD_RW,
729         "Bytes to read per chunk");