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