xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 99d0d3f5)
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, 2017 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  * Copyright 2020 Joyent, Inc.
27  * Copyright 2017 Nexenta Systems, Inc.
28  */
29 
30 /* Portions Copyright 2007 Jeremy Teo */
31 /* Portions Copyright 2010 Robert Milkowski */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/stat.h>
44 #include <sys/kmem.h>
45 #include <sys/taskq.h>
46 #include <sys/uio.h>
47 #include <sys/vmsystm.h>
48 #include <sys/atomic.h>
49 #include <sys/vm.h>
50 #include <vm/seg_vn.h>
51 #include <vm/pvn.h>
52 #include <vm/as.h>
53 #include <vm/kpm.h>
54 #include <vm/seg_kpm.h>
55 #include <sys/mman.h>
56 #include <sys/pathname.h>
57 #include <sys/cmn_err.h>
58 #include <sys/errno.h>
59 #include <sys/unistd.h>
60 #include <sys/zfs_dir.h>
61 #include <sys/zfs_acl.h>
62 #include <sys/zfs_ioctl.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/dmu.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/spa.h>
67 #include <sys/txg.h>
68 #include <sys/dbuf.h>
69 #include <sys/zap.h>
70 #include <sys/sa.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/sunddi.h>
74 #include <sys/filio.h>
75 #include <sys/sid.h>
76 #include "fs/fs_subr.h"
77 #include <sys/zfs_ctldir.h>
78 #include <sys/zfs_fuid.h>
79 #include <sys/zfs_sa.h>
80 #include <sys/dnlc.h>
81 #include <sys/zfs_rlock.h>
82 #include <sys/extdirent.h>
83 #include <sys/kidmap.h>
84 #include <sys/cred.h>
85 #include <sys/attr.h>
86 #include <sys/zil.h>
87 #include <sys/sa_impl.h>
88 #include <sys/zfs_project.h>
89 
90 /*
91  * Programming rules.
92  *
93  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
94  * properly lock its in-core state, create a DMU transaction, do the work,
95  * record this work in the intent log (ZIL), commit the DMU transaction,
96  * and wait for the intent log to commit if it is a synchronous operation.
97  * Moreover, the vnode ops must work in both normal and log replay context.
98  * The ordering of events is important to avoid deadlocks and references
99  * to freed memory.  The example below illustrates the following Big Rules:
100  *
101  *  (1)	A check must be made in each zfs thread for a mounted file system.
102  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
103  *	A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
104  *	must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
105  *	can return EIO from the calling function.
106  *
107  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
108  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
109  *	First, if it's the last reference, the vnode/znode
110  *	can be freed, so the zp may point to freed memory.  Second, the last
111  *	reference will call zfs_zinactive(), which may induce a lot of work --
112  *	pushing cached pages (which acquires range locks) and syncing out
113  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
114  *	which could deadlock the system if you were already holding one.
115  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
116  *
117  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
118  *	as they can span dmu_tx_assign() calls.
119  *
120  *  (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
121  *      dmu_tx_assign().  This is critical because we don't want to block
122  *      while holding locks.
123  *
124  *	If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT.  This
125  *	reduces lock contention and CPU usage when we must wait (note that if
126  *	throughput is constrained by the storage, nearly every transaction
127  *	must wait).
128  *
129  *      Note, in particular, that if a lock is sometimes acquired before
130  *      the tx assigns, and sometimes after (e.g. z_lock), then failing
131  *      to use a non-blocking assign can deadlock the system.  The scenario:
132  *
133  *	Thread A has grabbed a lock before calling dmu_tx_assign().
134  *	Thread B is in an already-assigned tx, and blocks for this lock.
135  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
136  *	forever, because the previous txg can't quiesce until B's tx commits.
137  *
138  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
139  *	then drop all locks, call dmu_tx_wait(), and try again.  On subsequent
140  *	calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
141  *	to indicate that this operation has already called dmu_tx_wait().
142  *	This will ensure that we don't retry forever, waiting a short bit
143  *	each time.
144  *
145  *  (5)	If the operation succeeded, generate the intent log entry for it
146  *	before dropping locks.  This ensures that the ordering of events
147  *	in the intent log matches the order in which they actually occurred.
148  *	During ZIL replay the zfs_log_* functions will update the sequence
149  *	number to indicate the zil transaction has replayed.
150  *
151  *  (6)	At the end of each vnode op, the DMU tx must always commit,
152  *	regardless of whether there were any errors.
153  *
154  *  (7)	After dropping all locks, invoke zil_commit(zilog, foid)
155  *	to ensure that synchronous semantics are provided when necessary.
156  *
157  * In general, this is how things should be ordered in each vnode op:
158  *
159  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
160  * top:
161  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
162  *	rw_enter(...);			// grab any other locks you need
163  *	tx = dmu_tx_create(...);	// get DMU tx
164  *	dmu_tx_hold_*();		// hold each object you might modify
165  *	error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
166  *	if (error) {
167  *		rw_exit(...);		// drop locks
168  *		zfs_dirent_unlock(dl);	// unlock directory entry
169  *		VN_RELE(...);		// release held vnodes
170  *		if (error == ERESTART) {
171  *			waited = B_TRUE;
172  *			dmu_tx_wait(tx);
173  *			dmu_tx_abort(tx);
174  *			goto top;
175  *		}
176  *		dmu_tx_abort(tx);	// abort DMU tx
177  *		ZFS_EXIT(zfsvfs);	// finished in zfs
178  *		return (error);		// really out of space
179  *	}
180  *	error = do_real_work();		// do whatever this VOP does
181  *	if (error == 0)
182  *		zfs_log_*(...);		// on success, make ZIL entry
183  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
184  *	rw_exit(...);			// drop locks
185  *	zfs_dirent_unlock(dl);		// unlock directory entry
186  *	VN_RELE(...);			// release held vnodes
187  *	zil_commit(zilog, foid);	// synchronous when necessary
188  *	ZFS_EXIT(zfsvfs);		// finished in zfs
189  *	return (error);			// done, report error
190  */
191 
192 /* ARGSUSED */
193 static int
zfs_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)194 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
195 {
196 	znode_t	*zp = VTOZ(*vpp);
197 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
198 
199 	ZFS_ENTER(zfsvfs);
200 	ZFS_VERIFY_ZP(zp);
201 
202 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
203 	    ((flag & FAPPEND) == 0)) {
204 		ZFS_EXIT(zfsvfs);
205 		return (SET_ERROR(EPERM));
206 	}
207 
208 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
209 	    ZTOV(zp)->v_type == VREG &&
210 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
211 		if (fs_vscan(*vpp, cr, 0) != 0) {
212 			ZFS_EXIT(zfsvfs);
213 			return (SET_ERROR(EACCES));
214 		}
215 	}
216 
217 	/* Keep a count of the synchronous opens in the znode */
218 	if (flag & (FSYNC | FDSYNC))
219 		atomic_inc_32(&zp->z_sync_cnt);
220 
221 	ZFS_EXIT(zfsvfs);
222 	return (0);
223 }
224 
225 /* ARGSUSED */
226 static int
zfs_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)227 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
228     caller_context_t *ct)
229 {
230 	znode_t	*zp = VTOZ(vp);
231 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
232 
233 	/*
234 	 * Clean up any locks held by this process on the vp.
235 	 */
236 	cleanlocks(vp, ddi_get_pid(), 0);
237 	cleanshares(vp, ddi_get_pid());
238 
239 	ZFS_ENTER(zfsvfs);
240 	ZFS_VERIFY_ZP(zp);
241 
242 	/* Decrement the synchronous opens in the znode */
243 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
244 		atomic_dec_32(&zp->z_sync_cnt);
245 
246 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
247 	    ZTOV(zp)->v_type == VREG &&
248 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
249 		VERIFY(fs_vscan(vp, cr, 1) == 0);
250 
251 	ZFS_EXIT(zfsvfs);
252 	return (0);
253 }
254 
255 /*
256  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
257  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
258  */
259 static int
zfs_holey(vnode_t * vp,int cmd,offset_t * off)260 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
261 {
262 	znode_t	*zp = VTOZ(vp);
263 	uint64_t noff = (uint64_t)*off; /* new offset */
264 	uint64_t file_sz;
265 	int error;
266 	boolean_t hole;
267 
268 	file_sz = zp->z_size;
269 	if (noff >= file_sz)  {
270 		return (SET_ERROR(ENXIO));
271 	}
272 
273 	if (cmd == _FIO_SEEK_HOLE)
274 		hole = B_TRUE;
275 	else
276 		hole = B_FALSE;
277 
278 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
279 
280 	if (error == ESRCH)
281 		return (SET_ERROR(ENXIO));
282 
283 	/*
284 	 * We could find a hole that begins after the logical end-of-file,
285 	 * because dmu_offset_next() only works on whole blocks.  If the
286 	 * EOF falls mid-block, then indicate that the "virtual hole"
287 	 * at the end of the file begins at the logical EOF, rather than
288 	 * at the end of the last block.
289 	 */
290 	if (noff > file_sz) {
291 		ASSERT(hole);
292 		noff = file_sz;
293 	}
294 
295 	if (noff < *off)
296 		return (error);
297 	*off = noff;
298 	return (error);
299 }
300 
301 static int
zfs_ioctl_getxattr(vnode_t * vp,intptr_t data,int flag,cred_t * cr,caller_context_t * ct)302 zfs_ioctl_getxattr(vnode_t *vp, intptr_t data, int flag, cred_t *cr,
303     caller_context_t *ct)
304 {
305 	zfsxattr_t fsx = { 0 };
306 	znode_t *zp = VTOZ(vp);
307 
308 	if (zp->z_pflags & ZFS_PROJINHERIT)
309 		fsx.fsx_xflags = ZFS_PROJINHERIT_FL;
310 	if (zp->z_pflags & ZFS_PROJID)
311 		fsx.fsx_projid = zp->z_projid;
312 	if (ddi_copyout(&fsx, (void *)data, sizeof (fsx), flag))
313 		return (SET_ERROR(EFAULT));
314 
315 	return (0);
316 }
317 
318 static int zfs_setattr(vnode_t *, vattr_t *, int, cred_t *, caller_context_t *);
319 
320 static int
zfs_ioctl_setxattr(vnode_t * vp,intptr_t data,int flags,cred_t * cr,caller_context_t * ct)321 zfs_ioctl_setxattr(vnode_t *vp, intptr_t data, int flags, cred_t *cr,
322     caller_context_t *ct)
323 {
324 	znode_t *zp = VTOZ(vp);
325 	zfsxattr_t fsx;
326 	xvattr_t xva;
327 	xoptattr_t *xoap;
328 	int err;
329 
330 	if (ddi_copyin((void *)data, &fsx, sizeof (fsx), flags))
331 		return (SET_ERROR(EFAULT));
332 
333 	if (!zpl_is_valid_projid(fsx.fsx_projid))
334 		return (SET_ERROR(EINVAL));
335 
336 	if (fsx.fsx_xflags & ~ZFS_PROJINHERIT_FL)
337 		return (SET_ERROR(EOPNOTSUPP));
338 
339 	xva_init(&xva);
340 	xoap = xva_getxoptattr(&xva);
341 
342 	XVA_SET_REQ(&xva, XAT_PROJINHERIT);
343 	if (fsx.fsx_xflags & ZFS_PROJINHERIT_FL)
344 		xoap->xoa_projinherit = B_TRUE;
345 
346 	XVA_SET_REQ(&xva, XAT_PROJID);
347 	xoap->xoa_projid = fsx.fsx_projid;
348 
349 	return (zfs_setattr(vp, (vattr_t *)&xva, flags, cr, ct));
350 }
351 
352 /* ARGSUSED */
353 static int
zfs_ioctl(vnode_t * vp,int com,intptr_t data,int flag,cred_t * cred,int * rvalp,caller_context_t * ct)354 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
355     int *rvalp, caller_context_t *ct)
356 {
357 	offset_t off;
358 	offset_t ndata;
359 	dmu_object_info_t doi;
360 	int error;
361 	zfsvfs_t *zfsvfs;
362 	znode_t *zp;
363 
364 	switch (com) {
365 	case _FIOFFS:
366 	{
367 		return (zfs_sync(vp->v_vfsp, 0, cred));
368 
369 		/*
370 		 * The following two ioctls are used by bfu.  Faking out,
371 		 * necessary to avoid bfu errors.
372 		 */
373 	}
374 	case _FIOGDIO:
375 	case _FIOSDIO:
376 	{
377 		return (0);
378 	}
379 
380 	case _FIODIRECTIO:
381 	{
382 		/*
383 		 * ZFS inherently provides the basic semantics for directio.
384 		 * This is the summary from the ZFS on Linux support for
385 		 * O_DIRECT, which is the common form of directio, and required
386 		 * no changes to ZFS.
387 		 *
388 		 * 1. Minimize cache effects of the I/O.
389 		 *
390 		 *    By design the ARC is already scan-resistant, which helps
391 		 *    mitigate the need for special O_DIRECT handling.
392 		 *
393 		 * 2. O_DIRECT _MAY_ impose restrictions on IO alignment and
394 		 *    length.
395 		 *
396 		 *    No additional alignment or length restrictions are
397 		 *    imposed by ZFS.
398 		 *
399 		 * 3. O_DIRECT _MAY_ perform unbuffered IO operations directly
400 		 *    between user memory and block device.
401 		 *
402 		 *    No unbuffered IO operations are currently supported. In
403 		 *    order to support features such as compression, encryption,
404 		 *    and checksumming a copy must be made to transform the
405 		 *    data.
406 		 *
407 		 * 4. O_DIRECT _MAY_ imply O_DSYNC (XFS).
408 		 *
409 		 *    O_DIRECT does not imply O_DSYNC for ZFS.
410 		 *
411 		 * 5. O_DIRECT _MAY_ disable file locking that serializes IO
412 		 *    operations.
413 		 *
414 		 *    All I/O in ZFS is locked for correctness and this locking
415 		 *    is not disabled by O_DIRECT.
416 		 */
417 		return (0);
418 	}
419 
420 	case _FIO_SEEK_DATA:
421 	case _FIO_SEEK_HOLE:
422 	{
423 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
424 			return (SET_ERROR(EFAULT));
425 
426 		zp = VTOZ(vp);
427 		zfsvfs = zp->z_zfsvfs;
428 		ZFS_ENTER(zfsvfs);
429 		ZFS_VERIFY_ZP(zp);
430 
431 		/* offset parameter is in/out */
432 		error = zfs_holey(vp, com, &off);
433 		ZFS_EXIT(zfsvfs);
434 		if (error)
435 			return (error);
436 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
437 			return (SET_ERROR(EFAULT));
438 		return (0);
439 	}
440 	case _FIO_COUNT_FILLED:
441 	{
442 		/*
443 		 * _FIO_COUNT_FILLED adds a new ioctl command which
444 		 * exposes the number of filled blocks in a
445 		 * ZFS object.
446 		 */
447 		zp = VTOZ(vp);
448 		zfsvfs = zp->z_zfsvfs;
449 		ZFS_ENTER(zfsvfs);
450 		ZFS_VERIFY_ZP(zp);
451 
452 		/*
453 		 * Wait for all dirty blocks for this object
454 		 * to get synced out to disk, and the DMU info
455 		 * updated.
456 		 */
457 		error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
458 		if (error) {
459 			ZFS_EXIT(zfsvfs);
460 			return (error);
461 		}
462 
463 		/*
464 		 * Retrieve fill count from DMU object.
465 		 */
466 		error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
467 		if (error) {
468 			ZFS_EXIT(zfsvfs);
469 			return (error);
470 		}
471 
472 		ndata = doi.doi_fill_count;
473 
474 		ZFS_EXIT(zfsvfs);
475 		if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
476 			return (SET_ERROR(EFAULT));
477 		return (0);
478 	}
479 	case ZFS_IOC_FSGETXATTR:
480 		return (zfs_ioctl_getxattr(vp, data, flag, cred, ct));
481 	case ZFS_IOC_FSSETXATTR:
482 		return (zfs_ioctl_setxattr(vp, data, flag, cred, ct));
483 	}
484 	return (SET_ERROR(ENOTTY));
485 }
486 
487 /*
488  * Utility functions to map and unmap a single physical page.  These
489  * are used to manage the mappable copies of ZFS file data, and therefore
490  * do not update ref/mod bits.
491  */
492 caddr_t
zfs_map_page(page_t * pp,enum seg_rw rw)493 zfs_map_page(page_t *pp, enum seg_rw rw)
494 {
495 	if (kpm_enable)
496 		return (hat_kpm_mapin(pp, 0));
497 	ASSERT(rw == S_READ || rw == S_WRITE);
498 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
499 	    (caddr_t)-1));
500 }
501 
502 void
zfs_unmap_page(page_t * pp,caddr_t addr)503 zfs_unmap_page(page_t *pp, caddr_t addr)
504 {
505 	if (kpm_enable) {
506 		hat_kpm_mapout(pp, 0, addr);
507 	} else {
508 		ppmapout(addr);
509 	}
510 }
511 
512 /*
513  * When a file is memory mapped, we must keep the IO data synchronized
514  * between the DMU cache and the memory mapped pages.  What this means:
515  *
516  * On Write:	If we find a memory mapped page, we write to *both*
517  *		the page and the dmu buffer.
518  */
519 static void
update_pages(vnode_t * vp,int64_t start,int len,objset_t * os,uint64_t oid)520 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
521 {
522 	int64_t	off;
523 
524 	off = start & PAGEOFFSET;
525 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
526 		page_t *pp;
527 		uint64_t nbytes = MIN(PAGESIZE - off, len);
528 
529 		if (pp = page_lookup(vp, start, SE_SHARED)) {
530 			caddr_t va;
531 
532 			va = zfs_map_page(pp, S_WRITE);
533 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
534 			    DMU_READ_PREFETCH);
535 			zfs_unmap_page(pp, va);
536 			page_unlock(pp);
537 		}
538 		len -= nbytes;
539 		off = 0;
540 	}
541 }
542 
543 /*
544  * When a file is memory mapped, we must keep the IO data synchronized
545  * between the DMU cache and the memory mapped pages.  What this means:
546  *
547  * On Read:	We "read" preferentially from memory mapped pages,
548  *		else we default from the dmu buffer.
549  *
550  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
551  *	 the file is memory mapped.
552  */
553 static int
mappedread(vnode_t * vp,int nbytes,uio_t * uio)554 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
555 {
556 	znode_t *zp = VTOZ(vp);
557 	int64_t	start, off;
558 	int len = nbytes;
559 	int error = 0;
560 
561 	start = uio->uio_loffset;
562 	off = start & PAGEOFFSET;
563 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
564 		page_t *pp;
565 		uint64_t bytes = MIN(PAGESIZE - off, len);
566 
567 		if (pp = page_lookup(vp, start, SE_SHARED)) {
568 			caddr_t va;
569 
570 			va = zfs_map_page(pp, S_READ);
571 			error = uiomove(va + off, bytes, UIO_READ, uio);
572 			zfs_unmap_page(pp, va);
573 			page_unlock(pp);
574 		} else {
575 			error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
576 			    uio, bytes);
577 		}
578 		len -= bytes;
579 		off = 0;
580 		if (error)
581 			break;
582 	}
583 	return (error);
584 }
585 
586 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
587 
588 /*
589  * Read bytes from specified file into supplied buffer.
590  *
591  *	IN:	vp	- vnode of file to be read from.
592  *		uio	- structure supplying read location, range info,
593  *			  and return buffer.
594  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
595  *		cr	- credentials of caller.
596  *		ct	- caller context
597  *
598  *	OUT:	uio	- updated offset and range, buffer filled.
599  *
600  *	RETURN:	0 on success, error code on failure.
601  *
602  * Side Effects:
603  *	vp - atime updated if byte count > 0
604  */
605 /* ARGSUSED */
606 static int
zfs_read(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)607 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
608 {
609 	znode_t		*zp = VTOZ(vp);
610 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
611 	ssize_t		n, nbytes;
612 	int		error = 0;
613 	boolean_t	frsync = B_FALSE;
614 	xuio_t		*xuio = NULL;
615 
616 	ZFS_ENTER(zfsvfs);
617 	ZFS_VERIFY_ZP(zp);
618 
619 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
620 		ZFS_EXIT(zfsvfs);
621 		return (SET_ERROR(EACCES));
622 	}
623 
624 	/*
625 	 * Validate file offset
626 	 */
627 	if (uio->uio_loffset < (offset_t)0) {
628 		ZFS_EXIT(zfsvfs);
629 		return (SET_ERROR(EINVAL));
630 	}
631 
632 	/*
633 	 * Fasttrack empty reads
634 	 */
635 	if (uio->uio_resid == 0) {
636 		ZFS_EXIT(zfsvfs);
637 		return (0);
638 	}
639 
640 	/*
641 	 * Check for mandatory locks
642 	 */
643 	if (MANDMODE(zp->z_mode)) {
644 		if (error = chklock(vp, FREAD,
645 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
646 			ZFS_EXIT(zfsvfs);
647 			return (error);
648 		}
649 	}
650 
651 #ifdef FRSYNC
652 	/*
653 	 * If we're in FRSYNC mode, sync out this znode before reading it.
654 	 * Only do this for non-snapshots.
655 	 *
656 	 * Some platforms do not support FRSYNC and instead map it
657 	 * to FSYNC, which results in unnecessary calls to zil_commit. We
658 	 * only honor FRSYNC requests on platforms which support it.
659 	 */
660 	frsync = !!(ioflag & FRSYNC);
661 #endif
662 
663 	if (zfsvfs->z_log &&
664 	    (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
665 		zil_commit(zfsvfs->z_log, zp->z_id);
666 
667 	/*
668 	 * Lock the range against changes.
669 	 */
670 	locked_range_t *lr = rangelock_enter(&zp->z_rangelock,
671 	    uio->uio_loffset, uio->uio_resid, RL_READER);
672 
673 	/*
674 	 * If we are reading past end-of-file we can skip
675 	 * to the end; but we might still need to set atime.
676 	 */
677 	if (uio->uio_loffset >= zp->z_size) {
678 		error = 0;
679 		goto out;
680 	}
681 
682 	ASSERT(uio->uio_loffset < zp->z_size);
683 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
684 
685 	if ((uio->uio_extflg == UIO_XUIO) &&
686 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
687 		int nblk;
688 		int blksz = zp->z_blksz;
689 		uint64_t offset = uio->uio_loffset;
690 
691 		xuio = (xuio_t *)uio;
692 		if ((ISP2(blksz))) {
693 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
694 			    blksz)) / blksz;
695 		} else {
696 			ASSERT(offset + n <= blksz);
697 			nblk = 1;
698 		}
699 		(void) dmu_xuio_init(xuio, nblk);
700 
701 		if (vn_has_cached_data(vp)) {
702 			/*
703 			 * For simplicity, we always allocate a full buffer
704 			 * even if we only expect to read a portion of a block.
705 			 */
706 			while (--nblk >= 0) {
707 				(void) dmu_xuio_add(xuio,
708 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
709 				    blksz), 0, blksz);
710 			}
711 		}
712 	}
713 
714 	while (n > 0) {
715 		nbytes = MIN(n, zfs_read_chunk_size -
716 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
717 
718 		if (vn_has_cached_data(vp)) {
719 			error = mappedread(vp, nbytes, uio);
720 		} else {
721 			error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
722 			    uio, nbytes);
723 		}
724 		if (error) {
725 			/* convert checksum errors into IO errors */
726 			if (error == ECKSUM)
727 				error = SET_ERROR(EIO);
728 			break;
729 		}
730 
731 		n -= nbytes;
732 	}
733 out:
734 	rangelock_exit(lr);
735 
736 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
737 	ZFS_EXIT(zfsvfs);
738 	return (error);
739 }
740 
741 /*
742  * Write the bytes to a file.
743  *
744  *	IN:	vp	- vnode of file to be written to.
745  *		uio	- structure supplying write location, range info,
746  *			  and data buffer.
747  *		ioflag	- FAPPEND, FSYNC, and/or FDSYNC.  FAPPEND is
748  *			  set if in append mode.
749  *		cr	- credentials of caller.
750  *		ct	- caller context (NFS/CIFS fem monitor only)
751  *
752  *	OUT:	uio	- updated offset and range.
753  *
754  *	RETURN:	0 on success, error code on failure.
755  *
756  * Timestamps:
757  *	vp - ctime|mtime updated if byte count > 0
758  */
759 
760 /* ARGSUSED */
761 static int
zfs_write(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)762 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
763 {
764 	znode_t		*zp = VTOZ(vp);
765 	rlim64_t	limit = uio->uio_llimit;
766 	ssize_t		start_resid = uio->uio_resid;
767 	ssize_t		tx_bytes;
768 	uint64_t	end_size;
769 	dmu_tx_t	*tx;
770 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
771 	zilog_t		*zilog;
772 	offset_t	woff;
773 	ssize_t		n, nbytes;
774 	int		max_blksz = zfsvfs->z_max_blksz;
775 	int		error = 0;
776 	int		prev_error;
777 	arc_buf_t	*abuf;
778 	iovec_t		*aiov = NULL;
779 	xuio_t		*xuio = NULL;
780 	int		i_iov = 0;
781 	int		iovcnt = uio->uio_iovcnt;
782 	iovec_t		*iovp = uio->uio_iov;
783 	int		write_eof;
784 	int		count = 0;
785 	sa_bulk_attr_t	bulk[4];
786 	uint64_t	mtime[2], ctime[2];
787 
788 	/*
789 	 * Fasttrack empty write
790 	 */
791 	n = start_resid;
792 	if (n == 0)
793 		return (0);
794 
795 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
796 		limit = MAXOFFSET_T;
797 
798 	ZFS_ENTER(zfsvfs);
799 	ZFS_VERIFY_ZP(zp);
800 
801 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
802 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
803 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
804 	    &zp->z_size, 8);
805 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
806 	    &zp->z_pflags, 8);
807 
808 	/*
809 	 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
810 	 * callers might not be able to detect properly that we are read-only,
811 	 * so check it explicitly here.
812 	 */
813 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
814 		ZFS_EXIT(zfsvfs);
815 		return (SET_ERROR(EROFS));
816 	}
817 
818 	/*
819 	 * If immutable or not appending then return EPERM.
820 	 * Intentionally allow ZFS_READONLY through here.
821 	 * See zfs_zaccess_common()
822 	 */
823 	if ((zp->z_pflags & ZFS_IMMUTABLE) ||
824 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
825 	    (uio->uio_loffset < zp->z_size))) {
826 		ZFS_EXIT(zfsvfs);
827 		return (SET_ERROR(EPERM));
828 	}
829 
830 	zilog = zfsvfs->z_log;
831 
832 	/*
833 	 * Validate file offset
834 	 */
835 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
836 	if (woff < 0) {
837 		ZFS_EXIT(zfsvfs);
838 		return (SET_ERROR(EINVAL));
839 	}
840 
841 	/*
842 	 * Check for mandatory locks before calling rangelock_enter()
843 	 * in order to prevent a deadlock with locks set via fcntl().
844 	 */
845 	if (MANDMODE((mode_t)zp->z_mode) &&
846 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
847 		ZFS_EXIT(zfsvfs);
848 		return (error);
849 	}
850 
851 	/*
852 	 * Pre-fault the pages to ensure slow (eg NFS) pages
853 	 * don't hold up txg.
854 	 * Skip this if uio contains loaned arc_buf.
855 	 */
856 	if ((uio->uio_extflg == UIO_XUIO) &&
857 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
858 		xuio = (xuio_t *)uio;
859 	else
860 		uio_prefaultpages(MIN(n, max_blksz), uio);
861 
862 	/*
863 	 * If in append mode, set the io offset pointer to eof.
864 	 */
865 	locked_range_t *lr;
866 	if (ioflag & FAPPEND) {
867 		/*
868 		 * Obtain an appending range lock to guarantee file append
869 		 * semantics.  We reset the write offset once we have the lock.
870 		 */
871 		lr = rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
872 		woff = lr->lr_offset;
873 		if (lr->lr_length == UINT64_MAX) {
874 			/*
875 			 * We overlocked the file because this write will cause
876 			 * the file block size to increase.
877 			 * Note that zp_size cannot change with this lock held.
878 			 */
879 			woff = zp->z_size;
880 		}
881 		uio->uio_loffset = woff;
882 	} else {
883 		/*
884 		 * Note that if the file block size will change as a result of
885 		 * this write, then this range lock will lock the entire file
886 		 * so that we can re-write the block safely.
887 		 */
888 		lr = rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
889 	}
890 
891 	if (woff >= limit) {
892 		rangelock_exit(lr);
893 		ZFS_EXIT(zfsvfs);
894 		return (SET_ERROR(EFBIG));
895 	}
896 
897 	if ((woff + n) > limit || woff > (limit - n))
898 		n = limit - woff;
899 
900 	/* Will this write extend the file length? */
901 	write_eof = (woff + n > zp->z_size);
902 
903 	end_size = MAX(zp->z_size, woff + n);
904 
905 	/*
906 	 * Write the file in reasonable size chunks.  Each chunk is written
907 	 * in a separate transaction; this keeps the intent log records small
908 	 * and allows us to do more fine-grained space accounting.
909 	 */
910 	while (n > 0) {
911 		woff = uio->uio_loffset;
912 
913 		if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
914 		    zp->z_uid) ||
915 		    zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
916 		    zp->z_gid) ||
917 		    (zp->z_projid != ZFS_DEFAULT_PROJID &&
918 		    zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
919 		    zp->z_projid))) {
920 			error = SET_ERROR(EDQUOT);
921 			break;
922 		}
923 
924 		arc_buf_t *abuf = NULL;
925 		if (xuio) {
926 			ASSERT(i_iov < iovcnt);
927 			aiov = &iovp[i_iov];
928 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
929 			dmu_xuio_clear(xuio, i_iov);
930 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
931 			    iovec_t *, aiov, arc_buf_t *, abuf);
932 			ASSERT((aiov->iov_base == abuf->b_data) ||
933 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
934 			    aiov->iov_len == arc_buf_size(abuf)));
935 			i_iov++;
936 		} else if (n >= max_blksz && woff >= zp->z_size &&
937 		    P2PHASE(woff, max_blksz) == 0 &&
938 		    zp->z_blksz == max_blksz) {
939 			/*
940 			 * This write covers a full block.  "Borrow" a buffer
941 			 * from the dmu so that we can fill it before we enter
942 			 * a transaction.  This avoids the possibility of
943 			 * holding up the transaction if the data copy hangs
944 			 * up on a pagefault (e.g., from an NFS server mapping).
945 			 */
946 			size_t cbytes;
947 
948 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
949 			    max_blksz);
950 			ASSERT(abuf != NULL);
951 			ASSERT(arc_buf_size(abuf) == max_blksz);
952 			if (error = uiocopy(abuf->b_data, max_blksz,
953 			    UIO_WRITE, uio, &cbytes)) {
954 				dmu_return_arcbuf(abuf);
955 				break;
956 			}
957 			ASSERT(cbytes == max_blksz);
958 		}
959 
960 		/*
961 		 * Start a transaction.
962 		 */
963 		tx = dmu_tx_create(zfsvfs->z_os);
964 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
965 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
966 		zfs_sa_upgrade_txholds(tx, zp);
967 		error = dmu_tx_assign(tx, TXG_WAIT);
968 		if (error) {
969 			dmu_tx_abort(tx);
970 			if (abuf != NULL)
971 				dmu_return_arcbuf(abuf);
972 			break;
973 		}
974 
975 		/*
976 		 * If rangelock_enter() over-locked we grow the blocksize
977 		 * and then reduce the lock range.  This will only happen
978 		 * on the first iteration since rangelock_reduce() will
979 		 * shrink down lr_length to the appropriate size.
980 		 */
981 		if (lr->lr_length == UINT64_MAX) {
982 			uint64_t new_blksz;
983 
984 			if (zp->z_blksz > max_blksz) {
985 				/*
986 				 * File's blocksize is already larger than the
987 				 * "recordsize" property.  Only let it grow to
988 				 * the next power of 2.
989 				 */
990 				ASSERT(!ISP2(zp->z_blksz));
991 				new_blksz = MIN(end_size,
992 				    1 << highbit64(zp->z_blksz));
993 			} else {
994 				new_blksz = MIN(end_size, max_blksz);
995 			}
996 			zfs_grow_blocksize(zp, new_blksz, tx);
997 			rangelock_reduce(lr, woff, n);
998 		}
999 
1000 		/*
1001 		 * XXX - should we really limit each write to z_max_blksz?
1002 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1003 		 */
1004 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1005 
1006 		if (abuf == NULL) {
1007 			tx_bytes = uio->uio_resid;
1008 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1009 			    uio, nbytes, tx);
1010 			tx_bytes -= uio->uio_resid;
1011 		} else {
1012 			tx_bytes = nbytes;
1013 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1014 			/*
1015 			 * If this is not a full block write, but we are
1016 			 * extending the file past EOF and this data starts
1017 			 * block-aligned, use assign_arcbuf().  Otherwise,
1018 			 * write via dmu_write().
1019 			 */
1020 			if (tx_bytes < max_blksz && (!write_eof ||
1021 			    aiov->iov_base != abuf->b_data)) {
1022 				ASSERT(xuio);
1023 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
1024 				    aiov->iov_len, aiov->iov_base, tx);
1025 				dmu_return_arcbuf(abuf);
1026 				xuio_stat_wbuf_copied();
1027 			} else {
1028 				ASSERT(xuio || tx_bytes == max_blksz);
1029 				dmu_assign_arcbuf_by_dbuf(
1030 				    sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
1031 			}
1032 			ASSERT(tx_bytes <= uio->uio_resid);
1033 			uioskip(uio, tx_bytes);
1034 		}
1035 		if (tx_bytes && vn_has_cached_data(vp)) {
1036 			update_pages(vp, woff,
1037 			    tx_bytes, zfsvfs->z_os, zp->z_id);
1038 		}
1039 
1040 		/*
1041 		 * If we made no progress, we're done.  If we made even
1042 		 * partial progress, update the znode and ZIL accordingly.
1043 		 */
1044 		if (tx_bytes == 0) {
1045 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1046 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
1047 			dmu_tx_commit(tx);
1048 			ASSERT(error != 0);
1049 			break;
1050 		}
1051 
1052 		/*
1053 		 * Clear Set-UID/Set-GID bits on successful write if not
1054 		 * privileged and at least one of the excute bits is set.
1055 		 *
1056 		 * It would be nice to to this after all writes have
1057 		 * been done, but that would still expose the ISUID/ISGID
1058 		 * to another app after the partial write is committed.
1059 		 *
1060 		 * Note: we don't call zfs_fuid_map_id() here because
1061 		 * user 0 is not an ephemeral uid.
1062 		 */
1063 		mutex_enter(&zp->z_acl_lock);
1064 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1065 		    (S_IXUSR >> 6))) != 0 &&
1066 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1067 		    secpolicy_vnode_setid_retain(cr,
1068 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1069 			uint64_t newmode;
1070 			zp->z_mode &= ~(S_ISUID | S_ISGID);
1071 			newmode = zp->z_mode;
1072 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1073 			    (void *)&newmode, sizeof (uint64_t), tx);
1074 		}
1075 		mutex_exit(&zp->z_acl_lock);
1076 
1077 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1078 		    B_TRUE);
1079 
1080 		/*
1081 		 * Update the file size (zp_size) if it has changed;
1082 		 * account for possible concurrent updates.
1083 		 */
1084 		while ((end_size = zp->z_size) < uio->uio_loffset) {
1085 			(void) atomic_cas_64(&zp->z_size, end_size,
1086 			    uio->uio_loffset);
1087 		}
1088 		/*
1089 		 * If we are replaying and eof is non zero then force
1090 		 * the file size to the specified eof. Note, there's no
1091 		 * concurrency during replay.
1092 		 */
1093 		if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1094 			zp->z_size = zfsvfs->z_replay_eof;
1095 
1096 		/*
1097 		 * Keep track of a possible pre-existing error from a partial
1098 		 * write via dmu_write_uio_dbuf above.
1099 		 */
1100 		prev_error = error;
1101 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1102 
1103 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1104 		dmu_tx_commit(tx);
1105 
1106 		if (prev_error != 0 || error != 0)
1107 			break;
1108 		ASSERT(tx_bytes == nbytes);
1109 		n -= nbytes;
1110 
1111 		if (!xuio && n > 0)
1112 			uio_prefaultpages(MIN(n, max_blksz), uio);
1113 	}
1114 
1115 	rangelock_exit(lr);
1116 
1117 	/*
1118 	 * If we're in replay mode, or we made no progress, return error.
1119 	 * Otherwise, it's at least a partial write, so it's successful.
1120 	 */
1121 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1122 		ZFS_EXIT(zfsvfs);
1123 		return (error);
1124 	}
1125 
1126 	if (ioflag & (FSYNC | FDSYNC) ||
1127 	    zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1128 		zil_commit(zilog, zp->z_id);
1129 
1130 	ZFS_EXIT(zfsvfs);
1131 	return (0);
1132 }
1133 
1134 /* ARGSUSED */
1135 void
zfs_get_done(zgd_t * zgd,int error)1136 zfs_get_done(zgd_t *zgd, int error)
1137 {
1138 	znode_t *zp = zgd->zgd_private;
1139 	objset_t *os = zp->z_zfsvfs->z_os;
1140 
1141 	if (zgd->zgd_db)
1142 		dmu_buf_rele(zgd->zgd_db, zgd);
1143 
1144 	rangelock_exit(zgd->zgd_lr);
1145 
1146 	/*
1147 	 * Release the vnode asynchronously as we currently have the
1148 	 * txg stopped from syncing.
1149 	 */
1150 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1151 
1152 	kmem_free(zgd, sizeof (zgd_t));
1153 }
1154 
1155 #ifdef DEBUG
1156 static int zil_fault_io = 0;
1157 #endif
1158 
1159 /*
1160  * Get data to generate a TX_WRITE intent log record.
1161  */
1162 int
zfs_get_data(void * arg,lr_write_t * lr,char * buf,struct lwb * lwb,zio_t * zio)1163 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1164 {
1165 	zfsvfs_t *zfsvfs = arg;
1166 	objset_t *os = zfsvfs->z_os;
1167 	znode_t *zp;
1168 	uint64_t object = lr->lr_foid;
1169 	uint64_t offset = lr->lr_offset;
1170 	uint64_t size = lr->lr_length;
1171 	dmu_buf_t *db;
1172 	zgd_t *zgd;
1173 	int error = 0;
1174 
1175 	ASSERT3P(lwb, !=, NULL);
1176 	ASSERT3P(zio, !=, NULL);
1177 	ASSERT3U(size, !=, 0);
1178 
1179 	/*
1180 	 * Nothing to do if the file has been removed
1181 	 */
1182 	if (zfs_zget(zfsvfs, object, &zp) != 0)
1183 		return (SET_ERROR(ENOENT));
1184 	if (zp->z_unlinked) {
1185 		/*
1186 		 * Release the vnode asynchronously as we currently have the
1187 		 * txg stopped from syncing.
1188 		 */
1189 		VN_RELE_ASYNC(ZTOV(zp),
1190 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1191 		return (SET_ERROR(ENOENT));
1192 	}
1193 
1194 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1195 	zgd->zgd_lwb = lwb;
1196 	zgd->zgd_private = zp;
1197 
1198 	/*
1199 	 * Write records come in two flavors: immediate and indirect.
1200 	 * For small writes it's cheaper to store the data with the
1201 	 * log record (immediate); for large writes it's cheaper to
1202 	 * sync the data and get a pointer to it (indirect) so that
1203 	 * we don't have to write the data twice.
1204 	 */
1205 	if (buf != NULL) { /* immediate write */
1206 		zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1207 		    offset, size, RL_READER);
1208 		/* test for truncation needs to be done while range locked */
1209 		if (offset >= zp->z_size) {
1210 			error = SET_ERROR(ENOENT);
1211 		} else {
1212 			error = dmu_read(os, object, offset, size, buf,
1213 			    DMU_READ_NO_PREFETCH);
1214 		}
1215 		ASSERT(error == 0 || error == ENOENT);
1216 	} else { /* indirect write */
1217 		/*
1218 		 * Have to lock the whole block to ensure when it's
1219 		 * written out and its checksum is being calculated
1220 		 * that no one can change the data. We need to re-check
1221 		 * blocksize after we get the lock in case it's changed!
1222 		 */
1223 		for (;;) {
1224 			uint64_t blkoff;
1225 			size = zp->z_blksz;
1226 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1227 			offset -= blkoff;
1228 			zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1229 			    offset, size, RL_READER);
1230 			if (zp->z_blksz == size)
1231 				break;
1232 			offset += blkoff;
1233 			rangelock_exit(zgd->zgd_lr);
1234 		}
1235 		/* test for truncation needs to be done while range locked */
1236 		if (lr->lr_offset >= zp->z_size)
1237 			error = SET_ERROR(ENOENT);
1238 #ifdef DEBUG
1239 		if (zil_fault_io) {
1240 			error = SET_ERROR(EIO);
1241 			zil_fault_io = 0;
1242 		}
1243 #endif
1244 		if (error == 0)
1245 			error = dmu_buf_hold(os, object, offset, zgd, &db,
1246 			    DMU_READ_NO_PREFETCH);
1247 
1248 		if (error == 0) {
1249 			blkptr_t *bp = &lr->lr_blkptr;
1250 
1251 			zgd->zgd_db = db;
1252 			zgd->zgd_bp = bp;
1253 
1254 			ASSERT(db->db_offset == offset);
1255 			ASSERT(db->db_size == size);
1256 
1257 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1258 			    zfs_get_done, zgd);
1259 			ASSERT(error || lr->lr_length <= size);
1260 
1261 			/*
1262 			 * On success, we need to wait for the write I/O
1263 			 * initiated by dmu_sync() to complete before we can
1264 			 * release this dbuf.  We will finish everything up
1265 			 * in the zfs_get_done() callback.
1266 			 */
1267 			if (error == 0)
1268 				return (0);
1269 
1270 			if (error == EALREADY) {
1271 				lr->lr_common.lrc_txtype = TX_WRITE2;
1272 				/*
1273 				 * TX_WRITE2 relies on the data previously
1274 				 * written by the TX_WRITE that caused
1275 				 * EALREADY.  We zero out the BP because
1276 				 * it is the old, currently-on-disk BP.
1277 				 */
1278 				zgd->zgd_bp = NULL;
1279 				BP_ZERO(bp);
1280 				error = 0;
1281 			}
1282 		}
1283 	}
1284 
1285 	zfs_get_done(zgd, error);
1286 
1287 	return (error);
1288 }
1289 
1290 /*ARGSUSED*/
1291 static int
zfs_access(vnode_t * vp,int mode,int flag,cred_t * cr,caller_context_t * ct)1292 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1293     caller_context_t *ct)
1294 {
1295 	znode_t *zp = VTOZ(vp);
1296 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1297 	int error;
1298 
1299 	ZFS_ENTER(zfsvfs);
1300 	ZFS_VERIFY_ZP(zp);
1301 
1302 	if (flag & V_ACE_MASK)
1303 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1304 	else
1305 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
1306 
1307 	ZFS_EXIT(zfsvfs);
1308 	return (error);
1309 }
1310 
1311 /*
1312  * If vnode is for a device return a specfs vnode instead.
1313  */
1314 static int
specvp_check(vnode_t ** vpp,cred_t * cr)1315 specvp_check(vnode_t **vpp, cred_t *cr)
1316 {
1317 	int error = 0;
1318 
1319 	if (IS_DEVVP(*vpp)) {
1320 		struct vnode *svp;
1321 
1322 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1323 		VN_RELE(*vpp);
1324 		if (svp == NULL)
1325 			error = SET_ERROR(ENOSYS);
1326 		*vpp = svp;
1327 	}
1328 	return (error);
1329 }
1330 
1331 
1332 /*
1333  * Lookup an entry in a directory, or an extended attribute directory.
1334  * If it exists, return a held vnode reference for it.
1335  *
1336  *	IN:	dvp	- vnode of directory to search.
1337  *		nm	- name of entry to lookup.
1338  *		pnp	- full pathname to lookup [UNUSED].
1339  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1340  *		rdir	- root directory vnode [UNUSED].
1341  *		cr	- credentials of caller.
1342  *		ct	- caller context
1343  *		direntflags - directory lookup flags
1344  *		realpnp - returned pathname.
1345  *
1346  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1347  *
1348  *	RETURN:	0 on success, error code on failure.
1349  *
1350  * Timestamps:
1351  *	NA
1352  */
1353 /* ARGSUSED */
1354 static int
zfs_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)1355 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1356     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
1357     int *direntflags, pathname_t *realpnp)
1358 {
1359 	znode_t *zdp = VTOZ(dvp);
1360 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1361 	int	error = 0;
1362 
1363 	/*
1364 	 * Fast path lookup, however we must skip DNLC lookup
1365 	 * for case folding or normalizing lookups because the
1366 	 * DNLC code only stores the passed in name.  This means
1367 	 * creating 'a' and removing 'A' on a case insensitive
1368 	 * file system would work, but DNLC still thinks 'a'
1369 	 * exists and won't let you create it again on the next
1370 	 * pass through fast path.
1371 	 */
1372 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1373 
1374 		if (dvp->v_type != VDIR) {
1375 			return (SET_ERROR(ENOTDIR));
1376 		} else if (zdp->z_sa_hdl == NULL) {
1377 			return (SET_ERROR(EIO));
1378 		}
1379 
1380 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1381 			error = zfs_fastaccesschk_execute(zdp, cr);
1382 			if (!error) {
1383 				*vpp = dvp;
1384 				VN_HOLD(*vpp);
1385 				return (0);
1386 			}
1387 			return (error);
1388 		} else if (!zdp->z_zfsvfs->z_norm &&
1389 		    (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1390 
1391 			vnode_t *tvp = dnlc_lookup(dvp, nm);
1392 
1393 			if (tvp) {
1394 				error = zfs_fastaccesschk_execute(zdp, cr);
1395 				if (error) {
1396 					VN_RELE(tvp);
1397 					return (error);
1398 				}
1399 				if (tvp == DNLC_NO_VNODE) {
1400 					VN_RELE(tvp);
1401 					return (SET_ERROR(ENOENT));
1402 				} else {
1403 					*vpp = tvp;
1404 					return (specvp_check(vpp, cr));
1405 				}
1406 			}
1407 		}
1408 	}
1409 
1410 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1411 
1412 	ZFS_ENTER(zfsvfs);
1413 	ZFS_VERIFY_ZP(zdp);
1414 
1415 	*vpp = NULL;
1416 
1417 	if (flags & LOOKUP_XATTR) {
1418 		/*
1419 		 * If the xattr property is off, refuse the lookup request.
1420 		 */
1421 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1422 			ZFS_EXIT(zfsvfs);
1423 			return (SET_ERROR(EINVAL));
1424 		}
1425 
1426 		/*
1427 		 * We don't allow recursive attributes..
1428 		 * Maybe someday we will.
1429 		 */
1430 		if (zdp->z_pflags & ZFS_XATTR) {
1431 			ZFS_EXIT(zfsvfs);
1432 			return (SET_ERROR(EINVAL));
1433 		}
1434 
1435 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1436 			ZFS_EXIT(zfsvfs);
1437 			return (error);
1438 		}
1439 
1440 		/*
1441 		 * Do we have permission to get into attribute directory?
1442 		 */
1443 
1444 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1445 		    B_FALSE, cr)) {
1446 			VN_RELE(*vpp);
1447 			*vpp = NULL;
1448 		}
1449 
1450 		ZFS_EXIT(zfsvfs);
1451 		return (error);
1452 	}
1453 
1454 	if (dvp->v_type != VDIR) {
1455 		ZFS_EXIT(zfsvfs);
1456 		return (SET_ERROR(ENOTDIR));
1457 	}
1458 
1459 	/*
1460 	 * Check accessibility of directory.
1461 	 */
1462 
1463 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1464 		ZFS_EXIT(zfsvfs);
1465 		return (error);
1466 	}
1467 
1468 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1469 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1470 		ZFS_EXIT(zfsvfs);
1471 		return (SET_ERROR(EILSEQ));
1472 	}
1473 
1474 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1475 	if (error == 0)
1476 		error = specvp_check(vpp, cr);
1477 
1478 	ZFS_EXIT(zfsvfs);
1479 	return (error);
1480 }
1481 
1482 /*
1483  * Attempt to create a new entry in a directory.  If the entry
1484  * already exists, truncate the file if permissible, else return
1485  * an error.  Return the vp of the created or trunc'd file.
1486  *
1487  *	IN:	dvp	- vnode of directory to put new file entry in.
1488  *		name	- name of new file entry.
1489  *		vap	- attributes of new file.
1490  *		excl	- flag indicating exclusive or non-exclusive mode.
1491  *		mode	- mode to open file with.
1492  *		cr	- credentials of caller.
1493  *		flag	- large file flag [UNUSED].
1494  *		ct	- caller context
1495  *		vsecp	- ACL to be set
1496  *
1497  *	OUT:	vpp	- vnode of created or trunc'd entry.
1498  *
1499  *	RETURN:	0 on success, error code on failure.
1500  *
1501  * Timestamps:
1502  *	dvp - ctime|mtime updated if new entry created
1503  *	 vp - ctime|mtime always, atime if new
1504  */
1505 
1506 /* ARGSUSED */
1507 static int
zfs_create(vnode_t * dvp,char * name,vattr_t * vap,vcexcl_t excl,int mode,vnode_t ** vpp,cred_t * cr,int flag,caller_context_t * ct,vsecattr_t * vsecp)1508 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1509     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1510     vsecattr_t *vsecp)
1511 {
1512 	znode_t		*zp, *dzp = VTOZ(dvp);
1513 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1514 	zilog_t		*zilog;
1515 	objset_t	*os;
1516 	zfs_dirlock_t	*dl;
1517 	dmu_tx_t	*tx;
1518 	int		error;
1519 	ksid_t		*ksid;
1520 	uid_t		uid;
1521 	gid_t		gid = crgetgid(cr);
1522 	zfs_acl_ids_t   acl_ids;
1523 	boolean_t	fuid_dirtied;
1524 	boolean_t	have_acl = B_FALSE;
1525 	boolean_t	waited = B_FALSE;
1526 
1527 	/*
1528 	 * If we have an ephemeral id, ACL, or XVATTR then
1529 	 * make sure file system is at proper version
1530 	 */
1531 
1532 	ksid = crgetsid(cr, KSID_OWNER);
1533 	if (ksid)
1534 		uid = ksid_getid(ksid);
1535 	else
1536 		uid = crgetuid(cr);
1537 
1538 	if (zfsvfs->z_use_fuids == B_FALSE &&
1539 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1540 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1541 		return (SET_ERROR(EINVAL));
1542 
1543 	ZFS_ENTER(zfsvfs);
1544 	ZFS_VERIFY_ZP(dzp);
1545 	os = zfsvfs->z_os;
1546 	zilog = zfsvfs->z_log;
1547 
1548 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1549 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1550 		ZFS_EXIT(zfsvfs);
1551 		return (SET_ERROR(EILSEQ));
1552 	}
1553 
1554 	if (vap->va_mask & AT_XVATTR) {
1555 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1556 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1557 			ZFS_EXIT(zfsvfs);
1558 			return (error);
1559 		}
1560 	}
1561 top:
1562 	*vpp = NULL;
1563 
1564 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1565 		vap->va_mode &= ~VSVTX;
1566 
1567 	if (*name == '\0') {
1568 		/*
1569 		 * Null component name refers to the directory itself.
1570 		 */
1571 		VN_HOLD(dvp);
1572 		zp = dzp;
1573 		dl = NULL;
1574 		error = 0;
1575 	} else {
1576 		/* possible VN_HOLD(zp) */
1577 		int zflg = 0;
1578 
1579 		if (flag & FIGNORECASE)
1580 			zflg |= ZCILOOK;
1581 
1582 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1583 		    NULL, NULL);
1584 		if (error) {
1585 			if (have_acl)
1586 				zfs_acl_ids_free(&acl_ids);
1587 			if (strcmp(name, "..") == 0)
1588 				error = SET_ERROR(EISDIR);
1589 			ZFS_EXIT(zfsvfs);
1590 			return (error);
1591 		}
1592 	}
1593 
1594 	if (zp == NULL) {
1595 		uint64_t txtype;
1596 		uint64_t projid = ZFS_DEFAULT_PROJID;
1597 
1598 		/*
1599 		 * Create a new file object and update the directory
1600 		 * to reference it.
1601 		 */
1602 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1603 			if (have_acl)
1604 				zfs_acl_ids_free(&acl_ids);
1605 			goto out;
1606 		}
1607 
1608 		/*
1609 		 * We only support the creation of regular files in
1610 		 * extended attribute directories.
1611 		 */
1612 
1613 		if ((dzp->z_pflags & ZFS_XATTR) &&
1614 		    (vap->va_type != VREG)) {
1615 			if (have_acl)
1616 				zfs_acl_ids_free(&acl_ids);
1617 			error = SET_ERROR(EINVAL);
1618 			goto out;
1619 		}
1620 
1621 		if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1622 		    cr, vsecp, &acl_ids)) != 0)
1623 			goto out;
1624 		have_acl = B_TRUE;
1625 
1626 		if (vap->va_type == VREG || vap->va_type == VDIR)
1627 			projid = zfs_inherit_projid(dzp);
1628 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1629 			zfs_acl_ids_free(&acl_ids);
1630 			error = SET_ERROR(EDQUOT);
1631 			goto out;
1632 		}
1633 
1634 		tx = dmu_tx_create(os);
1635 
1636 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1637 		    ZFS_SA_BASE_ATTR_SIZE);
1638 
1639 		fuid_dirtied = zfsvfs->z_fuid_dirty;
1640 		if (fuid_dirtied)
1641 			zfs_fuid_txhold(zfsvfs, tx);
1642 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1643 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1644 		if (!zfsvfs->z_use_sa &&
1645 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1646 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1647 			    0, acl_ids.z_aclp->z_acl_bytes);
1648 		}
1649 		error = dmu_tx_assign(tx,
1650 		    (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1651 		if (error) {
1652 			zfs_dirent_unlock(dl);
1653 			if (error == ERESTART) {
1654 				waited = B_TRUE;
1655 				dmu_tx_wait(tx);
1656 				dmu_tx_abort(tx);
1657 				goto top;
1658 			}
1659 			zfs_acl_ids_free(&acl_ids);
1660 			dmu_tx_abort(tx);
1661 			ZFS_EXIT(zfsvfs);
1662 			return (error);
1663 		}
1664 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1665 
1666 		if (fuid_dirtied)
1667 			zfs_fuid_sync(zfsvfs, tx);
1668 
1669 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1670 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1671 		if (flag & FIGNORECASE)
1672 			txtype |= TX_CI;
1673 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1674 		    vsecp, acl_ids.z_fuidp, vap);
1675 		zfs_acl_ids_free(&acl_ids);
1676 		dmu_tx_commit(tx);
1677 	} else {
1678 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1679 
1680 		if (have_acl)
1681 			zfs_acl_ids_free(&acl_ids);
1682 		have_acl = B_FALSE;
1683 
1684 		/*
1685 		 * A directory entry already exists for this name.
1686 		 */
1687 		/*
1688 		 * Can't truncate an existing file if in exclusive mode.
1689 		 */
1690 		if (excl == EXCL) {
1691 			error = SET_ERROR(EEXIST);
1692 			goto out;
1693 		}
1694 		/*
1695 		 * Can't open a directory for writing.
1696 		 */
1697 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1698 			error = SET_ERROR(EISDIR);
1699 			goto out;
1700 		}
1701 		/*
1702 		 * Verify requested access to file.
1703 		 */
1704 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1705 			goto out;
1706 		}
1707 
1708 		mutex_enter(&dzp->z_lock);
1709 		dzp->z_seq++;
1710 		mutex_exit(&dzp->z_lock);
1711 
1712 		/*
1713 		 * Truncate regular files if requested.
1714 		 */
1715 		if ((ZTOV(zp)->v_type == VREG) &&
1716 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1717 			/* we can't hold any locks when calling zfs_freesp() */
1718 			zfs_dirent_unlock(dl);
1719 			dl = NULL;
1720 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1721 			if (error == 0) {
1722 				vnevent_create(ZTOV(zp), ct);
1723 			}
1724 		}
1725 	}
1726 out:
1727 
1728 	if (dl)
1729 		zfs_dirent_unlock(dl);
1730 
1731 	if (error) {
1732 		if (zp)
1733 			VN_RELE(ZTOV(zp));
1734 	} else {
1735 		*vpp = ZTOV(zp);
1736 		error = specvp_check(vpp, cr);
1737 	}
1738 
1739 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1740 		zil_commit(zilog, 0);
1741 
1742 	ZFS_EXIT(zfsvfs);
1743 	return (error);
1744 }
1745 
1746 /*
1747  * Remove an entry from a directory.
1748  *
1749  *	IN:	dvp	- vnode of directory to remove entry from.
1750  *		name	- name of entry to remove.
1751  *		cr	- credentials of caller.
1752  *		ct	- caller context
1753  *		flags	- case flags
1754  *
1755  *	RETURN:	0 on success, error code on failure.
1756  *
1757  * Timestamps:
1758  *	dvp - ctime|mtime
1759  *	 vp - ctime (if nlink > 0)
1760  */
1761 
1762 uint64_t null_xattr = 0;
1763 
1764 /*ARGSUSED*/
1765 static int
zfs_remove(vnode_t * dvp,char * name,cred_t * cr,caller_context_t * ct,int flags)1766 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1767     int flags)
1768 {
1769 	znode_t		*zp, *dzp = VTOZ(dvp);
1770 	znode_t		*xzp;
1771 	vnode_t		*vp;
1772 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1773 	zilog_t		*zilog;
1774 	uint64_t	acl_obj, xattr_obj;
1775 	uint64_t	xattr_obj_unlinked = 0;
1776 	uint64_t	obj = 0;
1777 	zfs_dirlock_t	*dl;
1778 	dmu_tx_t	*tx;
1779 	boolean_t	may_delete_now, delete_now = FALSE;
1780 	boolean_t	unlinked, toobig = FALSE;
1781 	uint64_t	txtype;
1782 	pathname_t	*realnmp = NULL;
1783 	pathname_t	realnm;
1784 	int		error;
1785 	int		zflg = ZEXISTS;
1786 	boolean_t	waited = B_FALSE;
1787 
1788 	ZFS_ENTER(zfsvfs);
1789 	ZFS_VERIFY_ZP(dzp);
1790 	zilog = zfsvfs->z_log;
1791 
1792 	if (flags & FIGNORECASE) {
1793 		zflg |= ZCILOOK;
1794 		pn_alloc(&realnm);
1795 		realnmp = &realnm;
1796 	}
1797 
1798 top:
1799 	xattr_obj = 0;
1800 	xzp = NULL;
1801 	/*
1802 	 * Attempt to lock directory; fail if entry doesn't exist.
1803 	 */
1804 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1805 	    NULL, realnmp)) {
1806 		if (realnmp)
1807 			pn_free(realnmp);
1808 		ZFS_EXIT(zfsvfs);
1809 		return (error);
1810 	}
1811 
1812 	vp = ZTOV(zp);
1813 
1814 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1815 		goto out;
1816 	}
1817 
1818 	/*
1819 	 * Need to use rmdir for removing directories.
1820 	 */
1821 	if (vp->v_type == VDIR) {
1822 		error = SET_ERROR(EPERM);
1823 		goto out;
1824 	}
1825 
1826 	vnevent_remove(vp, dvp, name, ct);
1827 
1828 	if (realnmp)
1829 		dnlc_remove(dvp, realnmp->pn_buf);
1830 	else
1831 		dnlc_remove(dvp, name);
1832 
1833 	mutex_enter(&vp->v_lock);
1834 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1835 	mutex_exit(&vp->v_lock);
1836 
1837 	/*
1838 	 * We may delete the znode now, or we may put it in the unlinked set;
1839 	 * it depends on whether we're the last link, and on whether there are
1840 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1841 	 * allow for either case.
1842 	 */
1843 	obj = zp->z_id;
1844 	tx = dmu_tx_create(zfsvfs->z_os);
1845 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1846 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1847 	zfs_sa_upgrade_txholds(tx, zp);
1848 	zfs_sa_upgrade_txholds(tx, dzp);
1849 	if (may_delete_now) {
1850 		toobig =
1851 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1852 		/* if the file is too big, only hold_free a token amount */
1853 		dmu_tx_hold_free(tx, zp->z_id, 0,
1854 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1855 	}
1856 
1857 	/* are there any extended attributes? */
1858 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1859 	    &xattr_obj, sizeof (xattr_obj));
1860 	if (error == 0 && xattr_obj) {
1861 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1862 		ASSERT0(error);
1863 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1864 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1865 	}
1866 
1867 	mutex_enter(&zp->z_lock);
1868 	if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1869 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1870 	mutex_exit(&zp->z_lock);
1871 
1872 	/* charge as an update -- would be nice not to charge at all */
1873 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1874 
1875 	/*
1876 	 * Mark this transaction as typically resulting in a net free of space
1877 	 */
1878 	dmu_tx_mark_netfree(tx);
1879 
1880 	error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1881 	if (error) {
1882 		zfs_dirent_unlock(dl);
1883 		VN_RELE(vp);
1884 		if (xzp)
1885 			VN_RELE(ZTOV(xzp));
1886 		if (error == ERESTART) {
1887 			waited = B_TRUE;
1888 			dmu_tx_wait(tx);
1889 			dmu_tx_abort(tx);
1890 			goto top;
1891 		}
1892 		if (realnmp)
1893 			pn_free(realnmp);
1894 		dmu_tx_abort(tx);
1895 		ZFS_EXIT(zfsvfs);
1896 		return (error);
1897 	}
1898 
1899 	/*
1900 	 * Remove the directory entry.
1901 	 */
1902 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1903 
1904 	if (error) {
1905 		dmu_tx_commit(tx);
1906 		goto out;
1907 	}
1908 
1909 	if (unlinked) {
1910 		/*
1911 		 * Hold z_lock so that we can make sure that the ACL obj
1912 		 * hasn't changed.  Could have been deleted due to
1913 		 * zfs_sa_upgrade().
1914 		 */
1915 		mutex_enter(&zp->z_lock);
1916 		mutex_enter(&vp->v_lock);
1917 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1918 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1919 		delete_now = may_delete_now && !toobig &&
1920 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1921 		    xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1922 		    acl_obj;
1923 		mutex_exit(&vp->v_lock);
1924 	}
1925 
1926 	if (delete_now) {
1927 		if (xattr_obj_unlinked) {
1928 			ASSERT3U(xzp->z_links, ==, 2);
1929 			mutex_enter(&xzp->z_lock);
1930 			xzp->z_unlinked = 1;
1931 			xzp->z_links = 0;
1932 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1933 			    &xzp->z_links, sizeof (xzp->z_links), tx);
1934 			ASSERT3U(error,  ==,  0);
1935 			mutex_exit(&xzp->z_lock);
1936 			zfs_unlinked_add(xzp, tx);
1937 
1938 			if (zp->z_is_sa)
1939 				error = sa_remove(zp->z_sa_hdl,
1940 				    SA_ZPL_XATTR(zfsvfs), tx);
1941 			else
1942 				error = sa_update(zp->z_sa_hdl,
1943 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
1944 				    sizeof (uint64_t), tx);
1945 			ASSERT0(error);
1946 		}
1947 		mutex_enter(&vp->v_lock);
1948 		VN_RELE_LOCKED(vp);
1949 		ASSERT0(vp->v_count);
1950 		mutex_exit(&vp->v_lock);
1951 		mutex_exit(&zp->z_lock);
1952 		zfs_znode_delete(zp, tx);
1953 	} else if (unlinked) {
1954 		mutex_exit(&zp->z_lock);
1955 		zfs_unlinked_add(zp, tx);
1956 	}
1957 
1958 	txtype = TX_REMOVE;
1959 	if (flags & FIGNORECASE)
1960 		txtype |= TX_CI;
1961 	zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1962 
1963 	dmu_tx_commit(tx);
1964 out:
1965 	if (realnmp)
1966 		pn_free(realnmp);
1967 
1968 	zfs_dirent_unlock(dl);
1969 
1970 	if (!delete_now)
1971 		VN_RELE(vp);
1972 	if (xzp)
1973 		VN_RELE(ZTOV(xzp));
1974 
1975 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1976 		zil_commit(zilog, 0);
1977 
1978 	ZFS_EXIT(zfsvfs);
1979 	return (error);
1980 }
1981 
1982 /*
1983  * Create a new directory and insert it into dvp using the name
1984  * provided.  Return a pointer to the inserted directory.
1985  *
1986  *	IN:	dvp	- vnode of directory to add subdir to.
1987  *		dirname	- name of new directory.
1988  *		vap	- attributes of new directory.
1989  *		cr	- credentials of caller.
1990  *		ct	- caller context
1991  *		flags	- case flags
1992  *		vsecp	- ACL to be set
1993  *
1994  *	OUT:	vpp	- vnode of created directory.
1995  *
1996  *	RETURN:	0 on success, error code on failure.
1997  *
1998  * Timestamps:
1999  *	dvp - ctime|mtime updated
2000  *	 vp - ctime|mtime|atime updated
2001  */
2002 /*ARGSUSED*/
2003 static int
zfs_mkdir(vnode_t * dvp,char * dirname,vattr_t * vap,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)2004 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2005     caller_context_t *ct, int flags, vsecattr_t *vsecp)
2006 {
2007 	znode_t		*zp, *dzp = VTOZ(dvp);
2008 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2009 	zilog_t		*zilog;
2010 	zfs_dirlock_t	*dl;
2011 	uint64_t	txtype;
2012 	dmu_tx_t	*tx;
2013 	int		error;
2014 	int		zf = ZNEW;
2015 	ksid_t		*ksid;
2016 	uid_t		uid;
2017 	gid_t		gid = crgetgid(cr);
2018 	zfs_acl_ids_t   acl_ids;
2019 	boolean_t	fuid_dirtied;
2020 	boolean_t	waited = B_FALSE;
2021 
2022 	ASSERT(vap->va_type == VDIR);
2023 
2024 	/*
2025 	 * If we have an ephemeral id, ACL, or XVATTR then
2026 	 * make sure file system is at proper version
2027 	 */
2028 
2029 	ksid = crgetsid(cr, KSID_OWNER);
2030 	if (ksid)
2031 		uid = ksid_getid(ksid);
2032 	else
2033 		uid = crgetuid(cr);
2034 	if (zfsvfs->z_use_fuids == B_FALSE &&
2035 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
2036 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2037 		return (SET_ERROR(EINVAL));
2038 
2039 	ZFS_ENTER(zfsvfs);
2040 	ZFS_VERIFY_ZP(dzp);
2041 	zilog = zfsvfs->z_log;
2042 
2043 	if (dzp->z_pflags & ZFS_XATTR) {
2044 		ZFS_EXIT(zfsvfs);
2045 		return (SET_ERROR(EINVAL));
2046 	}
2047 
2048 	if (zfsvfs->z_utf8 && u8_validate(dirname,
2049 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2050 		ZFS_EXIT(zfsvfs);
2051 		return (SET_ERROR(EILSEQ));
2052 	}
2053 	if (flags & FIGNORECASE)
2054 		zf |= ZCILOOK;
2055 
2056 	if (vap->va_mask & AT_XVATTR) {
2057 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
2058 		    crgetuid(cr), cr, vap->va_type)) != 0) {
2059 			ZFS_EXIT(zfsvfs);
2060 			return (error);
2061 		}
2062 	}
2063 
2064 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2065 	    vsecp, &acl_ids)) != 0) {
2066 		ZFS_EXIT(zfsvfs);
2067 		return (error);
2068 	}
2069 	/*
2070 	 * First make sure the new directory doesn't exist.
2071 	 *
2072 	 * Existence is checked first to make sure we don't return
2073 	 * EACCES instead of EEXIST which can cause some applications
2074 	 * to fail.
2075 	 */
2076 top:
2077 	*vpp = NULL;
2078 
2079 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2080 	    NULL, NULL)) {
2081 		zfs_acl_ids_free(&acl_ids);
2082 		ZFS_EXIT(zfsvfs);
2083 		return (error);
2084 	}
2085 
2086 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2087 		zfs_acl_ids_free(&acl_ids);
2088 		zfs_dirent_unlock(dl);
2089 		ZFS_EXIT(zfsvfs);
2090 		return (error);
2091 	}
2092 
2093 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
2094 		zfs_acl_ids_free(&acl_ids);
2095 		zfs_dirent_unlock(dl);
2096 		ZFS_EXIT(zfsvfs);
2097 		return (SET_ERROR(EDQUOT));
2098 	}
2099 
2100 	/*
2101 	 * Add a new entry to the directory.
2102 	 */
2103 	tx = dmu_tx_create(zfsvfs->z_os);
2104 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2105 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2106 	fuid_dirtied = zfsvfs->z_fuid_dirty;
2107 	if (fuid_dirtied)
2108 		zfs_fuid_txhold(zfsvfs, tx);
2109 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2110 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2111 		    acl_ids.z_aclp->z_acl_bytes);
2112 	}
2113 
2114 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2115 	    ZFS_SA_BASE_ATTR_SIZE);
2116 
2117 	error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
2118 	if (error) {
2119 		zfs_dirent_unlock(dl);
2120 		if (error == ERESTART) {
2121 			waited = B_TRUE;
2122 			dmu_tx_wait(tx);
2123 			dmu_tx_abort(tx);
2124 			goto top;
2125 		}
2126 		zfs_acl_ids_free(&acl_ids);
2127 		dmu_tx_abort(tx);
2128 		ZFS_EXIT(zfsvfs);
2129 		return (error);
2130 	}
2131 
2132 	/*
2133 	 * Create new node.
2134 	 */
2135 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2136 
2137 	if (fuid_dirtied)
2138 		zfs_fuid_sync(zfsvfs, tx);
2139 
2140 	/*
2141 	 * Now put new name in parent dir.
2142 	 */
2143 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2144 
2145 	*vpp = ZTOV(zp);
2146 
2147 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2148 	if (flags & FIGNORECASE)
2149 		txtype |= TX_CI;
2150 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2151 	    acl_ids.z_fuidp, vap);
2152 
2153 	zfs_acl_ids_free(&acl_ids);
2154 
2155 	dmu_tx_commit(tx);
2156 
2157 	zfs_dirent_unlock(dl);
2158 
2159 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2160 		zil_commit(zilog, 0);
2161 
2162 	ZFS_EXIT(zfsvfs);
2163 	return (0);
2164 }
2165 
2166 /*
2167  * Remove a directory subdir entry.  If the current working
2168  * directory is the same as the subdir to be removed, the
2169  * remove will fail.
2170  *
2171  *	IN:	dvp	- vnode of directory to remove from.
2172  *		name	- name of directory to be removed.
2173  *		cwd	- vnode of current working directory.
2174  *		cr	- credentials of caller.
2175  *		ct	- caller context
2176  *		flags	- case flags
2177  *
2178  *	RETURN:	0 on success, error code on failure.
2179  *
2180  * Timestamps:
2181  *	dvp - ctime|mtime updated
2182  */
2183 /*ARGSUSED*/
2184 static int
zfs_rmdir(vnode_t * dvp,char * name,vnode_t * cwd,cred_t * cr,caller_context_t * ct,int flags)2185 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2186     caller_context_t *ct, int flags)
2187 {
2188 	znode_t		*dzp = VTOZ(dvp);
2189 	znode_t		*zp;
2190 	vnode_t		*vp;
2191 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2192 	zilog_t		*zilog;
2193 	zfs_dirlock_t	*dl;
2194 	dmu_tx_t	*tx;
2195 	int		error;
2196 	int		zflg = ZEXISTS;
2197 	boolean_t	waited = B_FALSE;
2198 
2199 	ZFS_ENTER(zfsvfs);
2200 	ZFS_VERIFY_ZP(dzp);
2201 	zilog = zfsvfs->z_log;
2202 
2203 	if (flags & FIGNORECASE)
2204 		zflg |= ZCILOOK;
2205 top:
2206 	zp = NULL;
2207 
2208 	/*
2209 	 * Attempt to lock directory; fail if entry doesn't exist.
2210 	 */
2211 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2212 	    NULL, NULL)) {
2213 		ZFS_EXIT(zfsvfs);
2214 		return (error);
2215 	}
2216 
2217 	vp = ZTOV(zp);
2218 
2219 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2220 		goto out;
2221 	}
2222 
2223 	if (vp->v_type != VDIR) {
2224 		error = SET_ERROR(ENOTDIR);
2225 		goto out;
2226 	}
2227 
2228 	if (vp == cwd) {
2229 		error = SET_ERROR(EINVAL);
2230 		goto out;
2231 	}
2232 
2233 	vnevent_rmdir(vp, dvp, name, ct);
2234 
2235 	/*
2236 	 * Grab a lock on the directory to make sure that noone is
2237 	 * trying to add (or lookup) entries while we are removing it.
2238 	 */
2239 	rw_enter(&zp->z_name_lock, RW_WRITER);
2240 
2241 	/*
2242 	 * Grab a lock on the parent pointer to make sure we play well
2243 	 * with the treewalk and directory rename code.
2244 	 */
2245 	rw_enter(&zp->z_parent_lock, RW_WRITER);
2246 
2247 	tx = dmu_tx_create(zfsvfs->z_os);
2248 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2249 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2250 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2251 	zfs_sa_upgrade_txholds(tx, zp);
2252 	zfs_sa_upgrade_txholds(tx, dzp);
2253 	dmu_tx_mark_netfree(tx);
2254 	error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
2255 	if (error) {
2256 		rw_exit(&zp->z_parent_lock);
2257 		rw_exit(&zp->z_name_lock);
2258 		zfs_dirent_unlock(dl);
2259 		VN_RELE(vp);
2260 		if (error == ERESTART) {
2261 			waited = B_TRUE;
2262 			dmu_tx_wait(tx);
2263 			dmu_tx_abort(tx);
2264 			goto top;
2265 		}
2266 		dmu_tx_abort(tx);
2267 		ZFS_EXIT(zfsvfs);
2268 		return (error);
2269 	}
2270 
2271 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2272 
2273 	if (error == 0) {
2274 		uint64_t txtype = TX_RMDIR;
2275 		if (flags & FIGNORECASE)
2276 			txtype |= TX_CI;
2277 		zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT,
2278 		    B_FALSE);
2279 	}
2280 
2281 	dmu_tx_commit(tx);
2282 
2283 	rw_exit(&zp->z_parent_lock);
2284 	rw_exit(&zp->z_name_lock);
2285 out:
2286 	zfs_dirent_unlock(dl);
2287 
2288 	VN_RELE(vp);
2289 
2290 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2291 		zil_commit(zilog, 0);
2292 
2293 	ZFS_EXIT(zfsvfs);
2294 	return (error);
2295 }
2296 
2297 /*
2298  * Read as many directory entries as will fit into the provided
2299  * buffer from the given directory cursor position (specified in
2300  * the uio structure).
2301  *
2302  *	IN:	vp	- vnode of directory to read.
2303  *		uio	- structure supplying read location, range info,
2304  *			  and return buffer.
2305  *		cr	- credentials of caller.
2306  *		ct	- caller context
2307  *		flags	- case flags
2308  *
2309  *	OUT:	uio	- updated offset and range, buffer filled.
2310  *		eofp	- set to true if end-of-file detected.
2311  *
2312  *	RETURN:	0 on success, error code on failure.
2313  *
2314  * Timestamps:
2315  *	vp - atime updated
2316  *
2317  * Note that the low 4 bits of the cookie returned by zap is always zero.
2318  * This allows us to use the low range for "special" directory entries:
2319  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2320  * we use the offset 2 for the '.zfs' directory.
2321  */
2322 /* ARGSUSED */
2323 static int
zfs_readdir(vnode_t * vp,uio_t * uio,cred_t * cr,int * eofp,caller_context_t * ct,int flags)2324 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2325     caller_context_t *ct, int flags)
2326 {
2327 	znode_t		*zp = VTOZ(vp);
2328 	iovec_t		*iovp;
2329 	edirent_t	*eodp;
2330 	dirent64_t	*odp;
2331 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2332 	objset_t	*os;
2333 	caddr_t		outbuf;
2334 	size_t		bufsize;
2335 	zap_cursor_t	zc;
2336 	zap_attribute_t	zap;
2337 	uint_t		bytes_wanted;
2338 	uint64_t	offset; /* must be unsigned; checks for < 1 */
2339 	uint64_t	parent;
2340 	int		local_eof;
2341 	int		outcount;
2342 	int		error;
2343 	uint8_t		prefetch;
2344 	boolean_t	check_sysattrs;
2345 
2346 	ZFS_ENTER(zfsvfs);
2347 	ZFS_VERIFY_ZP(zp);
2348 
2349 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2350 	    &parent, sizeof (parent))) != 0) {
2351 		ZFS_EXIT(zfsvfs);
2352 		return (error);
2353 	}
2354 
2355 	/*
2356 	 * If we are not given an eof variable,
2357 	 * use a local one.
2358 	 */
2359 	if (eofp == NULL)
2360 		eofp = &local_eof;
2361 
2362 	/*
2363 	 * Check for valid iov_len.
2364 	 */
2365 	if (uio->uio_iov->iov_len <= 0) {
2366 		ZFS_EXIT(zfsvfs);
2367 		return (SET_ERROR(EINVAL));
2368 	}
2369 
2370 	/*
2371 	 * Quit if directory has been removed (posix)
2372 	 */
2373 	if ((*eofp = zp->z_unlinked) != 0) {
2374 		ZFS_EXIT(zfsvfs);
2375 		return (0);
2376 	}
2377 
2378 	error = 0;
2379 	os = zfsvfs->z_os;
2380 	offset = uio->uio_loffset;
2381 	prefetch = zp->z_zn_prefetch;
2382 
2383 	/*
2384 	 * Initialize the iterator cursor.
2385 	 */
2386 	if (offset <= 3) {
2387 		/*
2388 		 * Start iteration from the beginning of the directory.
2389 		 */
2390 		zap_cursor_init(&zc, os, zp->z_id);
2391 	} else {
2392 		/*
2393 		 * The offset is a serialized cursor.
2394 		 */
2395 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2396 	}
2397 
2398 	/*
2399 	 * Get space to change directory entries into fs independent format.
2400 	 */
2401 	iovp = uio->uio_iov;
2402 	bytes_wanted = iovp->iov_len;
2403 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2404 		bufsize = bytes_wanted;
2405 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2406 		odp = (struct dirent64 *)outbuf;
2407 	} else {
2408 		bufsize = bytes_wanted;
2409 		outbuf = NULL;
2410 		odp = (struct dirent64 *)iovp->iov_base;
2411 	}
2412 	eodp = (struct edirent *)odp;
2413 
2414 	/*
2415 	 * If this VFS supports the system attribute view interface; and
2416 	 * we're looking at an extended attribute directory; and we care
2417 	 * about normalization conflicts on this vfs; then we must check
2418 	 * for normalization conflicts with the sysattr name space.
2419 	 */
2420 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2421 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2422 	    (flags & V_RDDIR_ENTFLAGS);
2423 
2424 	/*
2425 	 * Transform to file-system independent format
2426 	 */
2427 	outcount = 0;
2428 	while (outcount < bytes_wanted) {
2429 		ino64_t objnum;
2430 		ushort_t reclen;
2431 		off64_t *next = NULL;
2432 
2433 		/*
2434 		 * Special case `.', `..', and `.zfs'.
2435 		 */
2436 		if (offset == 0) {
2437 			(void) strcpy(zap.za_name, ".");
2438 			zap.za_normalization_conflict = 0;
2439 			objnum = zp->z_id;
2440 		} else if (offset == 1) {
2441 			(void) strcpy(zap.za_name, "..");
2442 			zap.za_normalization_conflict = 0;
2443 			objnum = parent;
2444 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2445 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2446 			zap.za_normalization_conflict = 0;
2447 			objnum = ZFSCTL_INO_ROOT;
2448 		} else {
2449 			/*
2450 			 * Grab next entry.
2451 			 */
2452 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2453 				if ((*eofp = (error == ENOENT)) != 0)
2454 					break;
2455 				else
2456 					goto update;
2457 			}
2458 
2459 			if (zap.za_integer_length != 8 ||
2460 			    zap.za_num_integers != 1) {
2461 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2462 				    "entry, obj = %lld, offset = %lld\n",
2463 				    (u_longlong_t)zp->z_id,
2464 				    (u_longlong_t)offset);
2465 				error = SET_ERROR(ENXIO);
2466 				goto update;
2467 			}
2468 
2469 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2470 			/*
2471 			 * MacOS X can extract the object type here such as:
2472 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2473 			 */
2474 
2475 			if (check_sysattrs && !zap.za_normalization_conflict) {
2476 				zap.za_normalization_conflict =
2477 				    xattr_sysattr_casechk(zap.za_name);
2478 			}
2479 		}
2480 
2481 		if (flags & V_RDDIR_ACCFILTER) {
2482 			/*
2483 			 * If we have no access at all, don't include
2484 			 * this entry in the returned information
2485 			 */
2486 			znode_t	*ezp;
2487 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2488 				goto skip_entry;
2489 			if (!zfs_has_access(ezp, cr)) {
2490 				VN_RELE(ZTOV(ezp));
2491 				goto skip_entry;
2492 			}
2493 			VN_RELE(ZTOV(ezp));
2494 		}
2495 
2496 		if (flags & V_RDDIR_ENTFLAGS)
2497 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2498 		else
2499 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2500 
2501 		/*
2502 		 * Will this entry fit in the buffer?
2503 		 */
2504 		if (outcount + reclen > bufsize) {
2505 			/*
2506 			 * Did we manage to fit anything in the buffer?
2507 			 */
2508 			if (!outcount) {
2509 				error = SET_ERROR(EINVAL);
2510 				goto update;
2511 			}
2512 			break;
2513 		}
2514 		if (flags & V_RDDIR_ENTFLAGS) {
2515 			/*
2516 			 * Add extended flag entry:
2517 			 */
2518 			eodp->ed_ino = objnum;
2519 			eodp->ed_reclen = reclen;
2520 			/* NOTE: ed_off is the offset for the *next* entry */
2521 			next = &(eodp->ed_off);
2522 			eodp->ed_eflags = zap.za_normalization_conflict ?
2523 			    ED_CASE_CONFLICT : 0;
2524 			(void) strncpy(eodp->ed_name, zap.za_name,
2525 			    EDIRENT_NAMELEN(reclen));
2526 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
2527 		} else {
2528 			/*
2529 			 * Add normal entry:
2530 			 */
2531 			odp->d_ino = objnum;
2532 			odp->d_reclen = reclen;
2533 			/* NOTE: d_off is the offset for the *next* entry */
2534 			next = &(odp->d_off);
2535 			(void) strncpy(odp->d_name, zap.za_name,
2536 			    DIRENT64_NAMELEN(reclen));
2537 			odp = (dirent64_t *)((intptr_t)odp + reclen);
2538 		}
2539 		outcount += reclen;
2540 
2541 		ASSERT(outcount <= bufsize);
2542 
2543 		/* Prefetch znode */
2544 		if (prefetch)
2545 			dmu_prefetch(os, objnum, 0, 0, 0,
2546 			    ZIO_PRIORITY_SYNC_READ);
2547 
2548 	skip_entry:
2549 		/*
2550 		 * Move to the next entry, fill in the previous offset.
2551 		 */
2552 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2553 			zap_cursor_advance(&zc);
2554 			offset = zap_cursor_serialize(&zc);
2555 		} else {
2556 			offset += 1;
2557 		}
2558 		if (next)
2559 			*next = offset;
2560 	}
2561 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2562 
2563 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2564 		iovp->iov_base += outcount;
2565 		iovp->iov_len -= outcount;
2566 		uio->uio_resid -= outcount;
2567 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2568 		/*
2569 		 * Reset the pointer.
2570 		 */
2571 		offset = uio->uio_loffset;
2572 	}
2573 
2574 update:
2575 	zap_cursor_fini(&zc);
2576 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2577 		kmem_free(outbuf, bufsize);
2578 
2579 	if (error == ENOENT)
2580 		error = 0;
2581 
2582 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2583 
2584 	uio->uio_loffset = offset;
2585 	ZFS_EXIT(zfsvfs);
2586 	return (error);
2587 }
2588 
2589 ulong_t zfs_fsync_sync_cnt = 4;
2590 
2591 static int
zfs_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)2592 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2593 {
2594 	znode_t	*zp = VTOZ(vp);
2595 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2596 
2597 	/*
2598 	 * Regardless of whether this is required for standards conformance,
2599 	 * this is the logical behavior when fsync() is called on a file with
2600 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
2601 	 * going to be pushed out as part of the zil_commit().
2602 	 */
2603 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2604 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2605 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2606 
2607 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2608 
2609 	if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2610 		ZFS_ENTER(zfsvfs);
2611 		ZFS_VERIFY_ZP(zp);
2612 		zil_commit(zfsvfs->z_log, zp->z_id);
2613 		ZFS_EXIT(zfsvfs);
2614 	}
2615 	return (0);
2616 }
2617 
2618 
2619 /*
2620  * Get the requested file attributes and place them in the provided
2621  * vattr structure.
2622  *
2623  *	IN:	vp	- vnode of file.
2624  *		vap	- va_mask identifies requested attributes.
2625  *			  If AT_XVATTR set, then optional attrs are requested
2626  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2627  *		cr	- credentials of caller.
2628  *		ct	- caller context
2629  *
2630  *	OUT:	vap	- attribute values.
2631  *
2632  *	RETURN:	0 (always succeeds).
2633  */
2634 /* ARGSUSED */
2635 static int
zfs_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2636 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2637     caller_context_t *ct)
2638 {
2639 	znode_t *zp = VTOZ(vp);
2640 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2641 	int	error = 0;
2642 	uint64_t links;
2643 	uint64_t mtime[2], ctime[2];
2644 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2645 	xoptattr_t *xoap = NULL;
2646 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2647 	sa_bulk_attr_t bulk[2];
2648 	int count = 0;
2649 
2650 	ZFS_ENTER(zfsvfs);
2651 	ZFS_VERIFY_ZP(zp);
2652 
2653 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2654 
2655 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2656 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2657 
2658 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2659 		ZFS_EXIT(zfsvfs);
2660 		return (error);
2661 	}
2662 
2663 	/*
2664 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2665 	 * Also, if we are the owner don't bother, since owner should
2666 	 * always be allowed to read basic attributes of file.
2667 	 */
2668 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2669 	    (vap->va_uid != crgetuid(cr))) {
2670 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2671 		    skipaclchk, cr)) {
2672 			ZFS_EXIT(zfsvfs);
2673 			return (error);
2674 		}
2675 	}
2676 
2677 	/*
2678 	 * Return all attributes.  It's cheaper to provide the answer
2679 	 * than to determine whether we were asked the question.
2680 	 */
2681 
2682 	mutex_enter(&zp->z_lock);
2683 	vap->va_type = vp->v_type;
2684 	vap->va_mode = zp->z_mode & MODEMASK;
2685 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2686 	vap->va_nodeid = zp->z_id;
2687 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2688 		links = zp->z_links + 1;
2689 	else
2690 		links = zp->z_links;
2691 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2692 	vap->va_size = zp->z_size;
2693 	vap->va_rdev = vp->v_rdev;
2694 	vap->va_seq = zp->z_seq;
2695 
2696 	/*
2697 	 * Add in any requested optional attributes and the create time.
2698 	 * Also set the corresponding bits in the returned attribute bitmap.
2699 	 */
2700 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2701 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2702 			xoap->xoa_archive =
2703 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2704 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
2705 		}
2706 
2707 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2708 			xoap->xoa_readonly =
2709 			    ((zp->z_pflags & ZFS_READONLY) != 0);
2710 			XVA_SET_RTN(xvap, XAT_READONLY);
2711 		}
2712 
2713 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2714 			xoap->xoa_system =
2715 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
2716 			XVA_SET_RTN(xvap, XAT_SYSTEM);
2717 		}
2718 
2719 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2720 			xoap->xoa_hidden =
2721 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
2722 			XVA_SET_RTN(xvap, XAT_HIDDEN);
2723 		}
2724 
2725 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2726 			xoap->xoa_nounlink =
2727 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2728 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
2729 		}
2730 
2731 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2732 			xoap->xoa_immutable =
2733 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2734 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2735 		}
2736 
2737 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2738 			xoap->xoa_appendonly =
2739 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2740 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
2741 		}
2742 
2743 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2744 			xoap->xoa_nodump =
2745 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
2746 			XVA_SET_RTN(xvap, XAT_NODUMP);
2747 		}
2748 
2749 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2750 			xoap->xoa_opaque =
2751 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
2752 			XVA_SET_RTN(xvap, XAT_OPAQUE);
2753 		}
2754 
2755 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2756 			xoap->xoa_av_quarantined =
2757 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2758 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2759 		}
2760 
2761 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2762 			xoap->xoa_av_modified =
2763 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2764 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2765 		}
2766 
2767 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2768 		    vp->v_type == VREG) {
2769 			zfs_sa_get_scanstamp(zp, xvap);
2770 		}
2771 
2772 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2773 			uint64_t times[2];
2774 
2775 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2776 			    times, sizeof (times));
2777 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2778 			XVA_SET_RTN(xvap, XAT_CREATETIME);
2779 		}
2780 
2781 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2782 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2783 			XVA_SET_RTN(xvap, XAT_REPARSE);
2784 		}
2785 		if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2786 			xoap->xoa_generation = zp->z_gen;
2787 			XVA_SET_RTN(xvap, XAT_GEN);
2788 		}
2789 
2790 		if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2791 			xoap->xoa_offline =
2792 			    ((zp->z_pflags & ZFS_OFFLINE) != 0);
2793 			XVA_SET_RTN(xvap, XAT_OFFLINE);
2794 		}
2795 
2796 		if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2797 			xoap->xoa_sparse =
2798 			    ((zp->z_pflags & ZFS_SPARSE) != 0);
2799 			XVA_SET_RTN(xvap, XAT_SPARSE);
2800 		}
2801 
2802 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2803 			xoap->xoa_projinherit =
2804 			    ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2805 			XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2806 		}
2807 
2808 		if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2809 			xoap->xoa_projid = zp->z_projid;
2810 			XVA_SET_RTN(xvap, XAT_PROJID);
2811 		}
2812 	}
2813 
2814 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2815 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2816 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2817 
2818 	mutex_exit(&zp->z_lock);
2819 
2820 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2821 
2822 	if (zp->z_blksz == 0) {
2823 		/*
2824 		 * Block size hasn't been set; suggest maximal I/O transfers.
2825 		 */
2826 		vap->va_blksize = zfsvfs->z_max_blksz;
2827 	}
2828 
2829 	ZFS_EXIT(zfsvfs);
2830 	return (0);
2831 }
2832 
2833 /*
2834  * For the operation of changing file's user/group/project, we need to
2835  * handle not only the main object that is assigned to the file directly,
2836  * but also the ones that are used by the file via hidden xattr directory.
2837  *
2838  * Because the xattr directory may contain many EA entries, it may be
2839  * impossible to change all of them in the same transaction as changing the
2840  * main object's user/group/project attributes. If so, we have to change them
2841  * via other multiple independent transactions one by one. It may be not a good
2842  * solution, but we have no better idea yet.
2843  */
2844 static int
zfs_setattr_dir(znode_t * dzp)2845 zfs_setattr_dir(znode_t *dzp)
2846 {
2847 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2848 	objset_t	*os = zfsvfs->z_os;
2849 	zap_cursor_t	zc;
2850 	zap_attribute_t	zap;
2851 	zfs_dirlock_t	*dl;
2852 	znode_t		*zp = NULL;
2853 	dmu_tx_t	*tx = NULL;
2854 	sa_bulk_attr_t	bulk[4];
2855 	int		count;
2856 	int		err;
2857 
2858 	zap_cursor_init(&zc, os, dzp->z_id);
2859 	while ((err = zap_cursor_retrieve(&zc, &zap)) == 0) {
2860 		count = 0;
2861 		if (zap.za_integer_length != 8 || zap.za_num_integers != 1) {
2862 			err = ENXIO;
2863 			break;
2864 		}
2865 
2866 		err = zfs_dirent_lock(&dl, dzp, (char *)zap.za_name, &zp,
2867 		    ZEXISTS, NULL, NULL);
2868 		if (err == ENOENT)
2869 			goto next;
2870 		if (err)
2871 			break;
2872 
2873 		if (zp->z_uid == dzp->z_uid &&
2874 		    zp->z_gid == dzp->z_gid &&
2875 		    zp->z_projid == dzp->z_projid)
2876 			goto next;
2877 
2878 		tx = dmu_tx_create(os);
2879 		if (!(zp->z_pflags & ZFS_PROJID))
2880 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2881 		else
2882 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2883 
2884 		err = dmu_tx_assign(tx, TXG_WAIT);
2885 		if (err)
2886 			break;
2887 
2888 		mutex_enter(&dzp->z_lock);
2889 
2890 		if (zp->z_uid != dzp->z_uid) {
2891 			zp->z_uid = dzp->z_uid;
2892 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2893 			    &dzp->z_uid, sizeof (dzp->z_uid));
2894 		}
2895 
2896 		if (zp->z_gid != dzp->z_gid) {
2897 			zp->z_gid = dzp->z_gid;
2898 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
2899 			    &dzp->z_gid, sizeof (dzp->z_gid));
2900 		}
2901 
2902 		if (zp->z_projid != dzp->z_projid) {
2903 			if (!(zp->z_pflags & ZFS_PROJID)) {
2904 				zp->z_pflags |= ZFS_PROJID;
2905 				SA_ADD_BULK_ATTR(bulk, count,
2906 				    SA_ZPL_FLAGS(zfsvfs), NULL, &zp->z_pflags,
2907 				    sizeof (zp->z_pflags));
2908 			}
2909 
2910 			zp->z_projid = dzp->z_projid;
2911 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PROJID(zfsvfs),
2912 			    NULL, &zp->z_projid, sizeof (zp->z_projid));
2913 		}
2914 
2915 		mutex_exit(&dzp->z_lock);
2916 
2917 		if (likely(count > 0)) {
2918 			err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2919 			dmu_tx_commit(tx);
2920 		} else {
2921 			dmu_tx_abort(tx);
2922 		}
2923 		tx = NULL;
2924 		if (err != 0 && err != ENOENT)
2925 			break;
2926 
2927 next:
2928 		if (zp) {
2929 			VN_RELE(ZTOV(zp));
2930 			zp = NULL;
2931 			zfs_dirent_unlock(dl);
2932 		}
2933 		zap_cursor_advance(&zc);
2934 	}
2935 
2936 	if (tx)
2937 		dmu_tx_abort(tx);
2938 	if (zp) {
2939 		VN_RELE(ZTOV(zp));
2940 		zfs_dirent_unlock(dl);
2941 	}
2942 	zap_cursor_fini(&zc);
2943 
2944 	return (err == ENOENT ? 0 : err);
2945 }
2946 
2947 /*
2948  * Set the file attributes to the values contained in the
2949  * vattr structure.
2950  *
2951  *	IN:	vp	- vnode of file to be modified.
2952  *		vap	- new attribute values.
2953  *			  If AT_XVATTR set, then optional attrs are being set
2954  *		flags	- ATTR_UTIME set if non-default time values provided.
2955  *			- ATTR_NOACLCHECK (CIFS context only).
2956  *		cr	- credentials of caller.
2957  *		ct	- caller context
2958  *
2959  *	RETURN:	0 on success, error code on failure.
2960  *
2961  * Timestamps:
2962  *	vp - ctime updated, mtime updated if size changed.
2963  */
2964 /* ARGSUSED */
2965 static int
zfs_setattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2966 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2967     caller_context_t *ct)
2968 {
2969 	znode_t		*zp = VTOZ(vp);
2970 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2971 	objset_t	*os = zfsvfs->z_os;
2972 	zilog_t		*zilog;
2973 	dmu_tx_t	*tx;
2974 	vattr_t		oldva;
2975 	xvattr_t	tmpxvattr;
2976 	uint_t		mask = vap->va_mask;
2977 	uint_t		saved_mask = 0;
2978 	int		trim_mask = 0;
2979 	uint64_t	new_mode;
2980 	uint64_t	new_uid, new_gid;
2981 	uint64_t	xattr_obj;
2982 	uint64_t	mtime[2], ctime[2];
2983 	uint64_t	projid = ZFS_INVALID_PROJID;
2984 	znode_t		*attrzp;
2985 	int		need_policy = FALSE;
2986 	int		err, err2 = 0;
2987 	zfs_fuid_info_t *fuidp = NULL;
2988 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2989 	xoptattr_t	*xoap;
2990 	zfs_acl_t	*aclp;
2991 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2992 	boolean_t	fuid_dirtied = B_FALSE;
2993 	boolean_t	handle_eadir = B_FALSE;
2994 	sa_bulk_attr_t	bulk[8], xattr_bulk[8];
2995 	int		count = 0, xattr_count = 0;
2996 
2997 	if (mask == 0)
2998 		return (0);
2999 
3000 	if (mask & AT_NOSET)
3001 		return (SET_ERROR(EINVAL));
3002 
3003 	ZFS_ENTER(zfsvfs);
3004 	ZFS_VERIFY_ZP(zp);
3005 
3006 	/*
3007 	 * If this is a xvattr_t, then get a pointer to the structure of
3008 	 * optional attributes.  If this is NULL, then we have a vattr_t.
3009 	 */
3010 	xoap = xva_getxoptattr(xvap);
3011 	if (xoap != NULL && (mask & AT_XVATTR)) {
3012 		if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
3013 			if (!dmu_objset_projectquota_enabled(os) ||
3014 			    (vp->v_type != VREG && vp->v_type != VDIR)) {
3015 				ZFS_EXIT(zfsvfs);
3016 				return (SET_ERROR(ENOTSUP));
3017 			}
3018 
3019 			projid = xoap->xoa_projid;
3020 			if (unlikely(projid == ZFS_INVALID_PROJID)) {
3021 				ZFS_EXIT(zfsvfs);
3022 				return (SET_ERROR(EINVAL));
3023 			}
3024 
3025 			if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
3026 				projid = ZFS_INVALID_PROJID;
3027 			else
3028 				need_policy = TRUE;
3029 		}
3030 
3031 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
3032 		    (!dmu_objset_projectquota_enabled(os) ||
3033 		    (vp->v_type != VREG && vp->v_type != VDIR))) {
3034 				ZFS_EXIT(zfsvfs);
3035 				return (SET_ERROR(ENOTSUP));
3036 		}
3037 	}
3038 
3039 	zilog = zfsvfs->z_log;
3040 
3041 	/*
3042 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
3043 	 * that file system is at proper version level
3044 	 */
3045 
3046 	if (zfsvfs->z_use_fuids == B_FALSE &&
3047 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3048 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3049 	    (mask & AT_XVATTR))) {
3050 		ZFS_EXIT(zfsvfs);
3051 		return (SET_ERROR(EINVAL));
3052 	}
3053 
3054 	if (mask & AT_SIZE && vp->v_type == VDIR) {
3055 		ZFS_EXIT(zfsvfs);
3056 		return (SET_ERROR(EISDIR));
3057 	}
3058 
3059 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3060 		ZFS_EXIT(zfsvfs);
3061 		return (SET_ERROR(EINVAL));
3062 	}
3063 
3064 	xva_init(&tmpxvattr);
3065 
3066 	/*
3067 	 * Immutable files can only alter immutable bit and atime
3068 	 */
3069 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3070 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3071 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3072 		ZFS_EXIT(zfsvfs);
3073 		return (SET_ERROR(EPERM));
3074 	}
3075 
3076 	/*
3077 	 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
3078 	 */
3079 
3080 	/*
3081 	 * Verify timestamps doesn't overflow 32 bits.
3082 	 * ZFS can handle large timestamps, but 32bit syscalls can't
3083 	 * handle times greater than 2039.  This check should be removed
3084 	 * once large timestamps are fully supported.
3085 	 */
3086 	if (mask & (AT_ATIME | AT_MTIME)) {
3087 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3088 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3089 			ZFS_EXIT(zfsvfs);
3090 			return (SET_ERROR(EOVERFLOW));
3091 		}
3092 	}
3093 
3094 top:
3095 	attrzp = NULL;
3096 	aclp = NULL;
3097 
3098 	/* Can this be moved to before the top label? */
3099 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3100 		ZFS_EXIT(zfsvfs);
3101 		return (SET_ERROR(EROFS));
3102 	}
3103 
3104 	/*
3105 	 * First validate permissions
3106 	 */
3107 
3108 	if (mask & AT_SIZE) {
3109 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
3110 		if (err) {
3111 			ZFS_EXIT(zfsvfs);
3112 			return (err);
3113 		}
3114 		/*
3115 		 * XXX - Note, we are not providing any open
3116 		 * mode flags here (like FNDELAY), so we may
3117 		 * block if there are locks present... this
3118 		 * should be addressed in openat().
3119 		 */
3120 		/* XXX - would it be OK to generate a log record here? */
3121 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3122 		if (err) {
3123 			ZFS_EXIT(zfsvfs);
3124 			return (err);
3125 		}
3126 
3127 		if (vap->va_size == 0)
3128 			vnevent_truncate(ZTOV(zp), ct);
3129 	}
3130 
3131 	if (mask & (AT_ATIME|AT_MTIME) ||
3132 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3133 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3134 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3135 	    XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3136 	    XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3137 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3138 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3139 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3140 		    skipaclchk, cr);
3141 	}
3142 
3143 	if (mask & (AT_UID|AT_GID)) {
3144 		int	idmask = (mask & (AT_UID|AT_GID));
3145 		int	take_owner;
3146 		int	take_group;
3147 
3148 		/*
3149 		 * NOTE: even if a new mode is being set,
3150 		 * we may clear S_ISUID/S_ISGID bits.
3151 		 */
3152 
3153 		if (!(mask & AT_MODE))
3154 			vap->va_mode = zp->z_mode;
3155 
3156 		/*
3157 		 * Take ownership or chgrp to group we are a member of
3158 		 */
3159 
3160 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3161 		take_group = (mask & AT_GID) &&
3162 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
3163 
3164 		/*
3165 		 * If both AT_UID and AT_GID are set then take_owner and
3166 		 * take_group must both be set in order to allow taking
3167 		 * ownership.
3168 		 *
3169 		 * Otherwise, send the check through secpolicy_vnode_setattr()
3170 		 *
3171 		 */
3172 
3173 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3174 		    ((idmask == AT_UID) && take_owner) ||
3175 		    ((idmask == AT_GID) && take_group)) {
3176 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3177 			    skipaclchk, cr) == 0) {
3178 				/*
3179 				 * Remove setuid/setgid for non-privileged users
3180 				 */
3181 				secpolicy_setid_clear(vap, cr);
3182 				trim_mask = (mask & (AT_UID|AT_GID));
3183 			} else {
3184 				need_policy =  TRUE;
3185 			}
3186 		} else {
3187 			need_policy =  TRUE;
3188 		}
3189 	}
3190 
3191 	mutex_enter(&zp->z_lock);
3192 	oldva.va_mode = zp->z_mode;
3193 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3194 	if (mask & AT_XVATTR) {
3195 		/*
3196 		 * Update xvattr mask to include only those attributes
3197 		 * that are actually changing.
3198 		 *
3199 		 * the bits will be restored prior to actually setting
3200 		 * the attributes so the caller thinks they were set.
3201 		 */
3202 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3203 			if (xoap->xoa_appendonly !=
3204 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3205 				need_policy = TRUE;
3206 			} else {
3207 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3208 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3209 			}
3210 		}
3211 
3212 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
3213 			if (xoap->xoa_projinherit !=
3214 			    ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
3215 				need_policy = TRUE;
3216 			} else {
3217 				XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
3218 				XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
3219 			}
3220 		}
3221 
3222 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3223 			if (xoap->xoa_nounlink !=
3224 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3225 				need_policy = TRUE;
3226 			} else {
3227 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3228 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3229 			}
3230 		}
3231 
3232 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3233 			if (xoap->xoa_immutable !=
3234 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3235 				need_policy = TRUE;
3236 			} else {
3237 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3238 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3239 			}
3240 		}
3241 
3242 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3243 			if (xoap->xoa_nodump !=
3244 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3245 				need_policy = TRUE;
3246 			} else {
3247 				XVA_CLR_REQ(xvap, XAT_NODUMP);
3248 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3249 			}
3250 		}
3251 
3252 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3253 			if (xoap->xoa_av_modified !=
3254 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3255 				need_policy = TRUE;
3256 			} else {
3257 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3258 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3259 			}
3260 		}
3261 
3262 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3263 			if ((vp->v_type != VREG &&
3264 			    xoap->xoa_av_quarantined) ||
3265 			    xoap->xoa_av_quarantined !=
3266 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3267 				need_policy = TRUE;
3268 			} else {
3269 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3270 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3271 			}
3272 		}
3273 
3274 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3275 			mutex_exit(&zp->z_lock);
3276 			ZFS_EXIT(zfsvfs);
3277 			return (SET_ERROR(EPERM));
3278 		}
3279 
3280 		if (need_policy == FALSE &&
3281 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3282 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3283 			need_policy = TRUE;
3284 		}
3285 	}
3286 
3287 	mutex_exit(&zp->z_lock);
3288 
3289 	if (mask & AT_MODE) {
3290 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3291 			err = secpolicy_setid_setsticky_clear(vp, vap,
3292 			    &oldva, cr);
3293 			if (err) {
3294 				ZFS_EXIT(zfsvfs);
3295 				return (err);
3296 			}
3297 			trim_mask |= AT_MODE;
3298 		} else {
3299 			need_policy = TRUE;
3300 		}
3301 	}
3302 
3303 	if (need_policy) {
3304 		/*
3305 		 * If trim_mask is set then take ownership
3306 		 * has been granted or write_acl is present and user
3307 		 * has the ability to modify mode.  In that case remove
3308 		 * UID|GID and or MODE from mask so that
3309 		 * secpolicy_vnode_setattr() doesn't revoke it.
3310 		 */
3311 
3312 		if (trim_mask) {
3313 			saved_mask = vap->va_mask;
3314 			vap->va_mask &= ~trim_mask;
3315 		}
3316 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3317 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3318 		if (err) {
3319 			ZFS_EXIT(zfsvfs);
3320 			return (err);
3321 		}
3322 
3323 		if (trim_mask)
3324 			vap->va_mask |= saved_mask;
3325 	}
3326 
3327 	/*
3328 	 * secpolicy_vnode_setattr, or take ownership may have
3329 	 * changed va_mask
3330 	 */
3331 	mask = vap->va_mask;
3332 
3333 	if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
3334 		handle_eadir = B_TRUE;
3335 		err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3336 		    &xattr_obj, sizeof (xattr_obj));
3337 
3338 		if (err == 0 && xattr_obj) {
3339 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3340 			if (err)
3341 				goto out2;
3342 		}
3343 		if (mask & AT_UID) {
3344 			new_uid = zfs_fuid_create(zfsvfs,
3345 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3346 			if (new_uid != zp->z_uid &&
3347 			    zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
3348 			    new_uid)) {
3349 				if (attrzp)
3350 					VN_RELE(ZTOV(attrzp));
3351 				err = SET_ERROR(EDQUOT);
3352 				goto out2;
3353 			}
3354 		}
3355 
3356 		if (mask & AT_GID) {
3357 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3358 			    cr, ZFS_GROUP, &fuidp);
3359 			if (new_gid != zp->z_gid &&
3360 			    zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
3361 			    new_gid)) {
3362 				if (attrzp)
3363 					VN_RELE(ZTOV(attrzp));
3364 				err = SET_ERROR(EDQUOT);
3365 				goto out2;
3366 			}
3367 		}
3368 
3369 		if (projid != ZFS_INVALID_PROJID &&
3370 		    zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
3371 			if (attrzp)
3372 				VN_RELE(ZTOV(attrzp));
3373