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