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_impl.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 #include <sys/zfs_vfsops.h>
56 #include <sys/zfs_znode.h>
57
58
59 static ulong_t zfs_fsync_sync_cnt = 4;
60
61 int
zfs_fsync(znode_t * zp,int syncflag,cred_t * cr)62 zfs_fsync(znode_t *zp, int syncflag, cred_t *cr)
63 {
64 zfsvfs_t *zfsvfs = ZTOZSB(zp);
65
66 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
67
68 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
69 ZFS_ENTER(zfsvfs);
70 ZFS_VERIFY_ZP(zp);
71 atomic_inc_32(&zp->z_sync_writes_cnt);
72 zil_commit(zfsvfs->z_log, zp->z_id);
73 atomic_dec_32(&zp->z_sync_writes_cnt);
74 ZFS_EXIT(zfsvfs);
75 }
76 tsd_set(zfs_fsyncer_key, NULL);
77
78 return (0);
79 }
80
81
82 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
83 /*
84 * Lseek support for finding holes (cmd == SEEK_HOLE) and
85 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
86 */
87 static int
zfs_holey_common(znode_t * zp,ulong_t cmd,loff_t * off)88 zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off)
89 {
90 zfs_locked_range_t *lr;
91 uint64_t noff = (uint64_t)*off; /* new offset */
92 uint64_t file_sz;
93 int error;
94 boolean_t hole;
95
96 file_sz = zp->z_size;
97 if (noff >= file_sz) {
98 return (SET_ERROR(ENXIO));
99 }
100
101 if (cmd == F_SEEK_HOLE)
102 hole = B_TRUE;
103 else
104 hole = B_FALSE;
105
106 /* Flush any mmap()'d data to disk */
107 if (zn_has_cached_data(zp, 0, file_sz - 1))
108 zn_flush_cached_data(zp, B_FALSE);
109
110 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_READER);
111 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
112 zfs_rangelock_exit(lr);
113
114 if (error == ESRCH)
115 return (SET_ERROR(ENXIO));
116
117 /* File was dirty, so fall back to using generic logic */
118 if (error == EBUSY) {
119 if (hole)
120 *off = file_sz;
121
122 return (0);
123 }
124
125 /*
126 * We could find a hole that begins after the logical end-of-file,
127 * because dmu_offset_next() only works on whole blocks. If the
128 * EOF falls mid-block, then indicate that the "virtual hole"
129 * at the end of the file begins at the logical EOF, rather than
130 * at the end of the last block.
131 */
132 if (noff > file_sz) {
133 ASSERT(hole);
134 noff = file_sz;
135 }
136
137 if (noff < *off)
138 return (error);
139 *off = noff;
140 return (error);
141 }
142
143 int
zfs_holey(znode_t * zp,ulong_t cmd,loff_t * off)144 zfs_holey(znode_t *zp, ulong_t cmd, loff_t *off)
145 {
146 zfsvfs_t *zfsvfs = ZTOZSB(zp);
147 int error;
148
149 ZFS_ENTER(zfsvfs);
150 ZFS_VERIFY_ZP(zp);
151
152 error = zfs_holey_common(zp, cmd, off);
153
154 ZFS_EXIT(zfsvfs);
155 return (error);
156 }
157 #endif /* SEEK_HOLE && SEEK_DATA */
158
159 /*ARGSUSED*/
160 int
zfs_access(znode_t * zp,int mode,int flag,cred_t * cr)161 zfs_access(znode_t *zp, int mode, int flag, cred_t *cr)
162 {
163 zfsvfs_t *zfsvfs = ZTOZSB(zp);
164 int error;
165
166 ZFS_ENTER(zfsvfs);
167 ZFS_VERIFY_ZP(zp);
168
169 if (flag & V_ACE_MASK)
170 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
171 else
172 error = zfs_zaccess_rwx(zp, mode, flag, cr);
173
174 ZFS_EXIT(zfsvfs);
175 return (error);
176 }
177
178 static unsigned long zfs_vnops_read_chunk_size = 1024 * 1024; /* Tunable */
179
180 /*
181 * Read bytes from specified file into supplied buffer.
182 *
183 * IN: zp - inode of file to be read from.
184 * uio - structure supplying read location, range info,
185 * and return buffer.
186 * ioflag - O_SYNC flags; used to provide FRSYNC semantics.
187 * O_DIRECT flag; used to bypass page cache.
188 * cr - credentials of caller.
189 *
190 * OUT: uio - updated offset and range, buffer filled.
191 *
192 * RETURN: 0 on success, error code on failure.
193 *
194 * Side Effects:
195 * inode - atime updated if byte count > 0
196 */
197 /* ARGSUSED */
198 int
zfs_read(struct znode * zp,zfs_uio_t * uio,int ioflag,cred_t * cr)199 zfs_read(struct znode *zp, zfs_uio_t *uio, int ioflag, cred_t *cr)
200 {
201 int error = 0;
202 boolean_t frsync = B_FALSE;
203
204 zfsvfs_t *zfsvfs = ZTOZSB(zp);
205 ZFS_ENTER(zfsvfs);
206 ZFS_VERIFY_ZP(zp);
207
208 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
209 ZFS_EXIT(zfsvfs);
210 return (SET_ERROR(EACCES));
211 }
212
213 /* We don't copy out anything useful for directories. */
214 if (Z_ISDIR(ZTOTYPE(zp))) {
215 ZFS_EXIT(zfsvfs);
216 return (SET_ERROR(EISDIR));
217 }
218
219 /*
220 * Validate file offset
221 */
222 if (zfs_uio_offset(uio) < (offset_t)0) {
223 ZFS_EXIT(zfsvfs);
224 return (SET_ERROR(EINVAL));
225 }
226
227 /*
228 * Fasttrack empty reads
229 */
230 if (zfs_uio_resid(uio) == 0) {
231 ZFS_EXIT(zfsvfs);
232 return (0);
233 }
234
235 #ifdef FRSYNC
236 /*
237 * If we're in FRSYNC mode, sync out this znode before reading it.
238 * Only do this for non-snapshots.
239 *
240 * Some platforms do not support FRSYNC and instead map it
241 * to O_SYNC, which results in unnecessary calls to zil_commit. We
242 * only honor FRSYNC requests on platforms which support it.
243 */
244 frsync = !!(ioflag & FRSYNC);
245 #endif
246 if (zfsvfs->z_log &&
247 (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
248 zil_commit(zfsvfs->z_log, zp->z_id);
249
250 /*
251 * Lock the range against changes.
252 */
253 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock,
254 zfs_uio_offset(uio), zfs_uio_resid(uio), RL_READER);
255
256 /*
257 * If we are reading past end-of-file we can skip
258 * to the end; but we might still need to set atime.
259 */
260 if (zfs_uio_offset(uio) >= zp->z_size) {
261 error = 0;
262 goto out;
263 }
264
265 ASSERT(zfs_uio_offset(uio) < zp->z_size);
266 #if defined(__linux__)
267 ssize_t start_offset = zfs_uio_offset(uio);
268 #endif
269 ssize_t n = MIN(zfs_uio_resid(uio), zp->z_size - zfs_uio_offset(uio));
270 ssize_t start_resid = n;
271
272 while (n > 0) {
273 ssize_t nbytes = MIN(n, zfs_vnops_read_chunk_size -
274 P2PHASE(zfs_uio_offset(uio), zfs_vnops_read_chunk_size));
275 #ifdef UIO_NOCOPY
276 if (zfs_uio_segflg(uio) == UIO_NOCOPY)
277 error = mappedread_sf(zp, nbytes, uio);
278 else
279 #endif
280 if (zn_has_cached_data(zp, zfs_uio_offset(uio),
281 zfs_uio_offset(uio) + nbytes - 1) && !(ioflag & O_DIRECT)) {
282 error = mappedread(zp, nbytes, uio);
283 } else {
284 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
285 uio, nbytes);
286 }
287
288 if (error) {
289 /* convert checksum errors into IO errors */
290 if (error == ECKSUM)
291 error = SET_ERROR(EIO);
292
293 #if defined(__linux__)
294 /*
295 * if we actually read some bytes, bubbling EFAULT
296 * up to become EAGAIN isn't what we want here...
297 *
298 * ...on Linux, at least. On FBSD, doing this breaks.
299 */
300 if (error == EFAULT &&
301 (zfs_uio_offset(uio) - start_offset) != 0)
302 error = 0;
303 #endif
304 break;
305 }
306
307 n -= nbytes;
308 }
309
310 int64_t nread = start_resid - n;
311 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread);
312 task_io_account_read(nread);
313 out:
314 zfs_rangelock_exit(lr);
315
316 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
317 ZFS_EXIT(zfsvfs);
318 return (error);
319 }
320
321 static void
zfs_clear_setid_bits_if_necessary(zfsvfs_t * zfsvfs,znode_t * zp,cred_t * cr,uint64_t * clear_setid_bits_txgp,dmu_tx_t * tx)322 zfs_clear_setid_bits_if_necessary(zfsvfs_t *zfsvfs, znode_t *zp, cred_t *cr,
323 uint64_t *clear_setid_bits_txgp, dmu_tx_t *tx)
324 {
325 zilog_t *zilog = zfsvfs->z_log;
326 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp));
327
328 ASSERT(clear_setid_bits_txgp != NULL);
329 ASSERT(tx != NULL);
330
331 /*
332 * Clear Set-UID/Set-GID bits on successful write if not
333 * privileged and at least one of the execute bits is set.
334 *
335 * It would be nice to do this after all writes have
336 * been done, but that would still expose the ISUID/ISGID
337 * to another app after the partial write is committed.
338 *
339 * Note: we don't call zfs_fuid_map_id() here because
340 * user 0 is not an ephemeral uid.
341 */
342 mutex_enter(&zp->z_acl_lock);
343 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | (S_IXUSR >> 6))) != 0 &&
344 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
345 secpolicy_vnode_setid_retain(zp, cr,
346 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
347 uint64_t newmode;
348
349 zp->z_mode &= ~(S_ISUID | S_ISGID);
350 newmode = zp->z_mode;
351 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
352 (void *)&newmode, sizeof (uint64_t), tx);
353
354 mutex_exit(&zp->z_acl_lock);
355
356 /*
357 * Make sure SUID/SGID bits will be removed when we replay the
358 * log. If the setid bits are keep coming back, don't log more
359 * than one TX_SETATTR per transaction group.
360 */
361 if (*clear_setid_bits_txgp != dmu_tx_get_txg(tx)) {
362 vattr_t va;
363
364 bzero(&va, sizeof (va));
365 va.va_mask = AT_MODE;
366 va.va_nodeid = zp->z_id;
367 va.va_mode = newmode;
368 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, &va, AT_MODE,
369 NULL);
370 *clear_setid_bits_txgp = dmu_tx_get_txg(tx);
371 }
372 } else {
373 mutex_exit(&zp->z_acl_lock);
374 }
375 }
376
377 /*
378 * Write the bytes to a file.
379 *
380 * IN: zp - znode of file to be written to.
381 * uio - structure supplying write location, range info,
382 * and data buffer.
383 * ioflag - O_APPEND flag set if in append mode.
384 * O_DIRECT flag; used to bypass page cache.
385 * cr - credentials of caller.
386 *
387 * OUT: uio - updated offset and range.
388 *
389 * RETURN: 0 if success
390 * error code if failure
391 *
392 * Timestamps:
393 * ip - ctime|mtime updated if byte count > 0
394 */
395
396 /* ARGSUSED */
397 int
zfs_write(znode_t * zp,zfs_uio_t * uio,int ioflag,cred_t * cr)398 zfs_write(znode_t *zp, zfs_uio_t *uio, int ioflag, cred_t *cr)
399 {
400 int error = 0, error1;
401 ssize_t start_resid = zfs_uio_resid(uio);
402 uint64_t clear_setid_bits_txg = 0;
403
404 /*
405 * Fasttrack empty write
406 */
407 ssize_t n = start_resid;
408 if (n == 0)
409 return (0);
410
411 zfsvfs_t *zfsvfs = ZTOZSB(zp);
412 ZFS_ENTER(zfsvfs);
413 ZFS_VERIFY_ZP(zp);
414
415 sa_bulk_attr_t bulk[4];
416 int count = 0;
417 uint64_t mtime[2], ctime[2];
418 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
419 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
420 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
421 &zp->z_size, 8);
422 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
423 &zp->z_pflags, 8);
424
425 /*
426 * Callers might not be able to detect properly that we are read-only,
427 * so check it explicitly here.
428 */
429 if (zfs_is_readonly(zfsvfs)) {
430 ZFS_EXIT(zfsvfs);
431 return (SET_ERROR(EROFS));
432 }
433
434 /*
435 * If immutable or not appending then return EPERM.
436 * Intentionally allow ZFS_READONLY through here.
437 * See zfs_zaccess_common()
438 */
439 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
440 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & O_APPEND) &&
441 (zfs_uio_offset(uio) < zp->z_size))) {
442 ZFS_EXIT(zfsvfs);
443 return (SET_ERROR(EPERM));
444 }
445
446 /*
447 * Validate file offset
448 */
449 offset_t woff = ioflag & O_APPEND ? zp->z_size : zfs_uio_offset(uio);
450 if (woff < 0) {
451 ZFS_EXIT(zfsvfs);
452 return (SET_ERROR(EINVAL));
453 }
454
455 const uint64_t max_blksz = zfsvfs->z_max_blksz;
456
457 /*
458 * Pre-fault the pages to ensure slow (eg NFS) pages
459 * don't hold up txg.
460 * Skip this if uio contains loaned arc_buf.
461 */
462 if (zfs_uio_prefaultpages(MIN(n, max_blksz), uio)) {
463 ZFS_EXIT(zfsvfs);
464 return (SET_ERROR(EFAULT));
465 }
466
467 /*
468 * If in append mode, set the io offset pointer to eof.
469 */
470 zfs_locked_range_t *lr;
471 if (ioflag & O_APPEND) {
472 /*
473 * Obtain an appending range lock to guarantee file append
474 * semantics. We reset the write offset once we have the lock.
475 */
476 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
477 woff = lr->lr_offset;
478 if (lr->lr_length == UINT64_MAX) {
479 /*
480 * We overlocked the file because this write will cause
481 * the file block size to increase.
482 * Note that zp_size cannot change with this lock held.
483 */
484 woff = zp->z_size;
485 }
486 zfs_uio_setoffset(uio, woff);
487 } else {
488 /*
489 * Note that if the file block size will change as a result of
490 * this write, then this range lock will lock the entire file
491 * so that we can re-write the block safely.
492 */
493 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
494 }
495
496 if (zn_rlimit_fsize(zp, uio)) {
497 zfs_rangelock_exit(lr);
498 ZFS_EXIT(zfsvfs);
499 return (SET_ERROR(EFBIG));
500 }
501
502 const rlim64_t limit = MAXOFFSET_T;
503
504 if (woff >= limit) {
505 zfs_rangelock_exit(lr);
506 ZFS_EXIT(zfsvfs);
507 return (SET_ERROR(EFBIG));
508 }
509
510 if (n > limit - woff)
511 n = limit - woff;
512
513 uint64_t end_size = MAX(zp->z_size, woff + n);
514 zilog_t *zilog = zfsvfs->z_log;
515
516 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp));
517 const uint64_t gid = KGID_TO_SGID(ZTOGID(zp));
518 const uint64_t projid = zp->z_projid;
519
520 /*
521 * Write the file in reasonable size chunks. Each chunk is written
522 * in a separate transaction; this keeps the intent log records small
523 * and allows us to do more fine-grained space accounting.
524 */
525 while (n > 0) {
526 woff = zfs_uio_offset(uio);
527
528 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, uid) ||
529 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, gid) ||
530 (projid != ZFS_DEFAULT_PROJID &&
531 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
532 projid))) {
533 error = SET_ERROR(EDQUOT);
534 break;
535 }
536
537 arc_buf_t *abuf = NULL;
538 if (n >= max_blksz && woff >= zp->z_size &&
539 P2PHASE(woff, max_blksz) == 0 &&
540 zp->z_blksz == max_blksz) {
541 /*
542 * This write covers a full block. "Borrow" a buffer
543 * from the dmu so that we can fill it before we enter
544 * a transaction. This avoids the possibility of
545 * holding up the transaction if the data copy hangs
546 * up on a pagefault (e.g., from an NFS server mapping).
547 */
548 size_t cbytes;
549
550 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
551 max_blksz);
552 ASSERT(abuf != NULL);
553 ASSERT(arc_buf_size(abuf) == max_blksz);
554 if ((error = zfs_uiocopy(abuf->b_data, max_blksz,
555 UIO_WRITE, uio, &cbytes))) {
556 dmu_return_arcbuf(abuf);
557 break;
558 }
559 ASSERT3S(cbytes, ==, max_blksz);
560 }
561
562 /*
563 * Start a transaction.
564 */
565 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
566 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
567 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
568 DB_DNODE_ENTER(db);
569 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff,
570 MIN(n, max_blksz));
571 DB_DNODE_EXIT(db);
572 zfs_sa_upgrade_txholds(tx, zp);
573 error = dmu_tx_assign(tx, TXG_WAIT);
574 if (error) {
575 dmu_tx_abort(tx);
576 if (abuf != NULL)
577 dmu_return_arcbuf(abuf);
578 break;
579 }
580
581 /*
582 * NB: We must call zfs_clear_setid_bits_if_necessary before
583 * committing the transaction!
584 */
585
586 /*
587 * If rangelock_enter() over-locked we grow the blocksize
588 * and then reduce the lock range. This will only happen
589 * on the first iteration since rangelock_reduce() will
590 * shrink down lr_length to the appropriate size.
591 */
592 if (lr->lr_length == UINT64_MAX) {
593 uint64_t new_blksz;
594
595 if (zp->z_blksz > max_blksz) {
596 /*
597 * File's blocksize is already larger than the
598 * "recordsize" property. Only let it grow to
599 * the next power of 2.
600 */
601 ASSERT(!ISP2(zp->z_blksz));
602 new_blksz = MIN(end_size,
603 1 << highbit64(zp->z_blksz));
604 } else {
605 new_blksz = MIN(end_size, max_blksz);
606 }
607 zfs_grow_blocksize(zp, new_blksz, tx);
608 zfs_rangelock_reduce(lr, woff, n);
609 }
610
611 /*
612 * XXX - should we really limit each write to z_max_blksz?
613 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
614 */
615 const ssize_t nbytes =
616 MIN(n, max_blksz - P2PHASE(woff, max_blksz));
617
618 ssize_t tx_bytes;
619 if (abuf == NULL) {
620 tx_bytes = zfs_uio_resid(uio);
621 zfs_uio_fault_disable(uio, B_TRUE);
622 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
623 uio, nbytes, tx);
624 zfs_uio_fault_disable(uio, B_FALSE);
625 #ifdef __linux__
626 if (error == EFAULT) {
627 zfs_clear_setid_bits_if_necessary(zfsvfs, zp,
628 cr, &clear_setid_bits_txg, tx);
629 dmu_tx_commit(tx);
630 /*
631 * Account for partial writes before
632 * continuing the loop.
633 * Update needs to occur before the next
634 * zfs_uio_prefaultpages, or prefaultpages may
635 * error, and we may break the loop early.
636 */
637 if (tx_bytes != zfs_uio_resid(uio))
638 n -= tx_bytes - zfs_uio_resid(uio);
639 if (zfs_uio_prefaultpages(MIN(n, max_blksz),
640 uio)) {
641 break;
642 }
643 continue;
644 }
645 #endif
646 /*
647 * On FreeBSD, EFAULT should be propagated back to the
648 * VFS, which will handle faulting and will retry.
649 */
650 if (error != 0 && error != EFAULT) {
651 zfs_clear_setid_bits_if_necessary(zfsvfs, zp,
652 cr, &clear_setid_bits_txg, tx);
653 dmu_tx_commit(tx);
654 break;
655 }
656 tx_bytes -= zfs_uio_resid(uio);
657 } else {
658 /* Implied by abuf != NULL: */
659 ASSERT3S(n, >=, max_blksz);
660 ASSERT0(P2PHASE(woff, max_blksz));
661 /*
662 * We can simplify nbytes to MIN(n, max_blksz) since
663 * P2PHASE(woff, max_blksz) is 0, and knowing
664 * n >= max_blksz lets us simplify further:
665 */
666 ASSERT3S(nbytes, ==, max_blksz);
667 /*
668 * Thus, we're writing a full block at a block-aligned
669 * offset and extending the file past EOF.
670 *
671 * dmu_assign_arcbuf_by_dbuf() will directly assign the
672 * arc buffer to a dbuf.
673 */
674 error = dmu_assign_arcbuf_by_dbuf(
675 sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
676 if (error != 0) {
677 /*
678 * XXX This might not be necessary if
679 * dmu_assign_arcbuf_by_dbuf is guaranteed
680 * to be atomic.
681 */
682 zfs_clear_setid_bits_if_necessary(zfsvfs, zp,
683 cr, &clear_setid_bits_txg, tx);
684 dmu_return_arcbuf(abuf);
685 dmu_tx_commit(tx);
686 break;
687 }
688 ASSERT3S(nbytes, <=, zfs_uio_resid(uio));
689 zfs_uioskip(uio, nbytes);
690 tx_bytes = nbytes;
691 }
692 if (tx_bytes &&
693 zn_has_cached_data(zp, woff, woff + tx_bytes - 1) &&
694 !(ioflag & O_DIRECT)) {
695 update_pages(zp, woff, tx_bytes, zfsvfs->z_os);
696 }
697
698 /*
699 * If we made no progress, we're done. If we made even
700 * partial progress, update the znode and ZIL accordingly.
701 */
702 if (tx_bytes == 0) {
703 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
704 (void *)&zp->z_size, sizeof (uint64_t), tx);
705 dmu_tx_commit(tx);
706 ASSERT(error != 0);
707 break;
708 }
709
710 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, cr,
711 &clear_setid_bits_txg, tx);
712
713 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
714
715 /*
716 * Update the file size (zp_size) if it has changed;
717 * account for possible concurrent updates.
718 */
719 while ((end_size = zp->z_size) < zfs_uio_offset(uio)) {
720 (void) atomic_cas_64(&zp->z_size, end_size,
721 zfs_uio_offset(uio));
722 ASSERT(error == 0 || error == EFAULT);
723 }
724 /*
725 * If we are replaying and eof is non zero then force
726 * the file size to the specified eof. Note, there's no
727 * concurrency during replay.
728 */
729 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
730 zp->z_size = zfsvfs->z_replay_eof;
731
732 error1 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
733 if (error1 != 0)
734 /* Avoid clobbering EFAULT. */
735 error = error1;
736
737 /*
738 * NB: During replay, the TX_SETATTR record logged by
739 * zfs_clear_setid_bits_if_necessary must precede any of
740 * the TX_WRITE records logged here.
741 */
742 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
743 NULL, NULL);
744
745 dmu_tx_commit(tx);
746
747 if (error != 0)
748 break;
749 ASSERT3S(tx_bytes, ==, nbytes);
750 n -= nbytes;
751
752 if (n > 0) {
753 if (zfs_uio_prefaultpages(MIN(n, max_blksz), uio)) {
754 error = SET_ERROR(EFAULT);
755 break;
756 }
757 }
758 }
759
760 zfs_znode_update_vfs(zp);
761 zfs_rangelock_exit(lr);
762
763 /*
764 * If we're in replay mode, or we made no progress, or the
765 * uio data is inaccessible return an error. Otherwise, it's
766 * at least a partial write, so it's successful.
767 */
768 if (zfsvfs->z_replay || zfs_uio_resid(uio) == start_resid ||
769 error == EFAULT) {
770 ZFS_EXIT(zfsvfs);
771 return (error);
772 }
773
774 if (ioflag & (O_SYNC | O_DSYNC) ||
775 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
776 zil_commit(zilog, zp->z_id);
777
778 const int64_t nwritten = start_resid - zfs_uio_resid(uio);
779 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten);
780 task_io_account_write(nwritten);
781
782 ZFS_EXIT(zfsvfs);
783 return (0);
784 }
785
786 /*ARGSUSED*/
787 int
zfs_getsecattr(znode_t * zp,vsecattr_t * vsecp,int flag,cred_t * cr)788 zfs_getsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
789 {
790 zfsvfs_t *zfsvfs = ZTOZSB(zp);
791 int error;
792 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
793
794 ZFS_ENTER(zfsvfs);
795 ZFS_VERIFY_ZP(zp);
796 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
797 ZFS_EXIT(zfsvfs);
798
799 return (error);
800 }
801
802 /*ARGSUSED*/
803 int
zfs_setsecattr(znode_t * zp,vsecattr_t * vsecp,int flag,cred_t * cr)804 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
805 {
806 zfsvfs_t *zfsvfs = ZTOZSB(zp);
807 int error;
808 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
809 zilog_t *zilog = zfsvfs->z_log;
810
811 ZFS_ENTER(zfsvfs);
812 ZFS_VERIFY_ZP(zp);
813
814 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
815
816 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
817 zil_commit(zilog, 0);
818
819 ZFS_EXIT(zfsvfs);
820 return (error);
821 }
822
823 #ifdef ZFS_DEBUG
824 static int zil_fault_io = 0;
825 #endif
826
827 static void zfs_get_done(zgd_t *zgd, int error);
828
829 /*
830 * Get data to generate a TX_WRITE intent log record.
831 */
832 int
zfs_get_data(void * arg,uint64_t gen,lr_write_t * lr,char * buf,struct lwb * lwb,zio_t * zio)833 zfs_get_data(void *arg, uint64_t gen, lr_write_t *lr, char *buf,
834 struct lwb *lwb, zio_t *zio)
835 {
836 zfsvfs_t *zfsvfs = arg;
837 objset_t *os = zfsvfs->z_os;
838 znode_t *zp;
839 uint64_t object = lr->lr_foid;
840 uint64_t offset = lr->lr_offset;
841 uint64_t size = lr->lr_length;
842 dmu_buf_t *db;
843 zgd_t *zgd;
844 int error = 0;
845 uint64_t zp_gen;
846
847 ASSERT3P(lwb, !=, NULL);
848 ASSERT3P(zio, !=, NULL);
849 ASSERT3U(size, !=, 0);
850
851 /*
852 * Nothing to do if the file has been removed
853 */
854 if (zfs_zget(zfsvfs, object, &zp) != 0)
855 return (SET_ERROR(ENOENT));
856 if (zp->z_unlinked) {
857 /*
858 * Release the vnode asynchronously as we currently have the
859 * txg stopped from syncing.
860 */
861 zfs_zrele_async(zp);
862 return (SET_ERROR(ENOENT));
863 }
864 /* check if generation number matches */
865 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
866 sizeof (zp_gen)) != 0) {
867 zfs_zrele_async(zp);
868 return (SET_ERROR(EIO));
869 }
870 if (zp_gen != gen) {
871 zfs_zrele_async(zp);
872 return (SET_ERROR(ENOENT));
873 }
874
875 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
876 zgd->zgd_lwb = lwb;
877 zgd->zgd_private = zp;
878
879 /*
880 * Write records come in two flavors: immediate and indirect.
881 * For small writes it's cheaper to store the data with the
882 * log record (immediate); for large writes it's cheaper to
883 * sync the data and get a pointer to it (indirect) so that
884 * we don't have to write the data twice.
885 */
886 if (buf != NULL) { /* immediate write */
887 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock,
888 offset, size, RL_READER);
889 /* test for truncation needs to be done while range locked */
890 if (offset >= zp->z_size) {
891 error = SET_ERROR(ENOENT);
892 } else {
893 error = dmu_read(os, object, offset, size, buf,
894 DMU_READ_NO_PREFETCH);
895 }
896 ASSERT(error == 0 || error == ENOENT);
897 } else { /* indirect write */
898 /*
899 * Have to lock the whole block to ensure when it's
900 * written out and its checksum is being calculated
901 * that no one can change the data. We need to re-check
902 * blocksize after we get the lock in case it's changed!
903 */
904 for (;;) {
905 uint64_t blkoff;
906 size = zp->z_blksz;
907 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
908 offset -= blkoff;
909 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock,
910 offset, size, RL_READER);
911 if (zp->z_blksz == size)
912 break;
913 offset += blkoff;
914 zfs_rangelock_exit(zgd->zgd_lr);
915 }
916 /* test for truncation needs to be done while range locked */
917 if (lr->lr_offset >= zp->z_size)
918 error = SET_ERROR(ENOENT);
919 #ifdef ZFS_DEBUG
920 if (zil_fault_io) {
921 error = SET_ERROR(EIO);
922 zil_fault_io = 0;
923 }
924 #endif
925 if (error == 0)
926 error = dmu_buf_hold(os, object, offset, zgd, &db,
927 DMU_READ_NO_PREFETCH);
928
929 if (error == 0) {
930 blkptr_t *bp = &lr->lr_blkptr;
931
932 zgd->zgd_db = db;
933 zgd->zgd_bp = bp;
934
935 ASSERT(db->db_offset == offset);
936 ASSERT(db->db_size == size);
937
938 error = dmu_sync(zio, lr->lr_common.lrc_txg,
939 zfs_get_done, zgd);
940 ASSERT(error || lr->lr_length <= size);
941
942 /*
943 * On success, we need to wait for the write I/O
944 * initiated by dmu_sync() to complete before we can
945 * release this dbuf. We will finish everything up
946 * in the zfs_get_done() callback.
947 */
948 if (error == 0)
949 return (0);
950
951 if (error == EALREADY) {
952 lr->lr_common.lrc_txtype = TX_WRITE2;
953 /*
954 * TX_WRITE2 relies on the data previously
955 * written by the TX_WRITE that caused
956 * EALREADY. We zero out the BP because
957 * it is the old, currently-on-disk BP.
958 */
959 zgd->zgd_bp = NULL;
960 BP_ZERO(bp);
961 error = 0;
962 }
963 }
964 }
965
966 zfs_get_done(zgd, error);
967
968 return (error);
969 }
970
971
972 /* ARGSUSED */
973 static void
zfs_get_done(zgd_t * zgd,int error)974 zfs_get_done(zgd_t *zgd, int error)
975 {
976 znode_t *zp = zgd->zgd_private;
977
978 if (zgd->zgd_db)
979 dmu_buf_rele(zgd->zgd_db, zgd);
980
981 zfs_rangelock_exit(zgd->zgd_lr);
982
983 /*
984 * Release the vnode asynchronously as we currently have the
985 * txg stopped from syncing.
986 */
987 zfs_zrele_async(zp);
988
989 kmem_free(zgd, sizeof (zgd_t));
990 }
991
992 EXPORT_SYMBOL(zfs_access);
993 EXPORT_SYMBOL(zfs_fsync);
994 EXPORT_SYMBOL(zfs_holey);
995 EXPORT_SYMBOL(zfs_read);
996 EXPORT_SYMBOL(zfs_write);
997 EXPORT_SYMBOL(zfs_getsecattr);
998 EXPORT_SYMBOL(zfs_setsecattr);
999
1000 ZFS_MODULE_PARAM(zfs_vnops, zfs_vnops_, read_chunk_size, ULONG, ZMOD_RW,
1001 "Bytes to read per chunk");
1002