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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016 Joyent, Inc.
27 */
28
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/resource.h>
38 #include <sys/vfs.h>
39 #include <sys/vfs_opreg.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/kmem.h>
44 #include <sys/taskq.h>
45 #include <sys/uio.h>
46 #include <sys/vmsystm.h>
47 #include <sys/atomic.h>
48 #include <sys/vm.h>
49 #include <vm/seg_vn.h>
50 #include <vm/pvn.h>
51 #include <vm/as.h>
52 #include <vm/kpm.h>
53 #include <vm/seg_kpm.h>
54 #include <sys/mman.h>
55 #include <sys/pathname.h>
56 #include <sys/cmn_err.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_acl.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/dmu.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa.h>
66 #include <sys/txg.h>
67 #include <sys/dbuf.h>
68 #include <sys/zap.h>
69 #include <sys/sa.h>
70 #include <sys/dirent.h>
71 #include <sys/policy.h>
72 #include <sys/sunddi.h>
73 #include <sys/filio.h>
74 #include <sys/sid.h>
75 #include "fs/fs_subr.h"
76 #include <sys/zfs_ctldir.h>
77 #include <sys/zfs_fuid.h>
78 #include <sys/zfs_sa.h>
79 #include <sys/dnlc.h>
80 #include <sys/zfs_rlock.h>
81 #include <sys/extdirent.h>
82 #include <sys/kidmap.h>
83 #include <sys/cred.h>
84 #include <sys/attr.h>
85
86 /*
87 * Programming rules.
88 *
89 * Each vnode op performs some logical unit of work. To do this, the ZPL must
90 * properly lock its in-core state, create a DMU transaction, do the work,
91 * record this work in the intent log (ZIL), commit the DMU transaction,
92 * and wait for the intent log to commit if it is a synchronous operation.
93 * Moreover, the vnode ops must work in both normal and log replay context.
94 * The ordering of events is important to avoid deadlocks and references
95 * to freed memory. The example below illustrates the following Big Rules:
96 *
97 * (1) A check must be made in each zfs thread for a mounted file system.
98 * This is done avoiding races using ZFS_ENTER(zfsvfs).
99 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
100 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
101 * can return EIO from the calling function.
102 *
103 * (2) VN_RELE() should always be the last thing except for zil_commit()
104 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
105 * First, if it's the last reference, the vnode/znode
106 * can be freed, so the zp may point to freed memory. Second, the last
107 * reference will call zfs_zinactive(), which may induce a lot of work --
108 * pushing cached pages (which acquires range locks) and syncing out
109 * cached atime changes. Third, zfs_zinactive() may require a new tx,
110 * which could deadlock the system if you were already holding one.
111 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
112 *
113 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
114 * as they can span dmu_tx_assign() calls.
115 *
116 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
117 * dmu_tx_assign(). This is critical because we don't want to block
118 * while holding locks.
119 *
120 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
121 * reduces lock contention and CPU usage when we must wait (note that if
122 * throughput is constrained by the storage, nearly every transaction
123 * must wait).
124 *
125 * Note, in particular, that if a lock is sometimes acquired before
126 * the tx assigns, and sometimes after (e.g. z_lock), then failing
127 * to use a non-blocking assign can deadlock the system. The scenario:
128 *
129 * Thread A has grabbed a lock before calling dmu_tx_assign().
130 * Thread B is in an already-assigned tx, and blocks for this lock.
131 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
132 * forever, because the previous txg can't quiesce until B's tx commits.
133 *
134 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
135 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
136 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
137 * to indicate that this operation has already called dmu_tx_wait().
138 * This will ensure that we don't retry forever, waiting a short bit
139 * each time.
140 *
141 * (5) If the operation succeeded, generate the intent log entry for it
142 * before dropping locks. This ensures that the ordering of events
143 * in the intent log matches the order in which they actually occurred.
144 * During ZIL replay the zfs_log_* functions will update the sequence
145 * number to indicate the zil transaction has replayed.
146 *
147 * (6) At the end of each vnode op, the DMU tx must always commit,
148 * regardless of whether there were any errors.
149 *
150 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
151 * to ensure that synchronous semantics are provided when necessary.
152 *
153 * In general, this is how things should be ordered in each vnode op:
154 *
155 * ZFS_ENTER(zfsvfs); // exit if unmounted
156 * top:
157 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
158 * rw_enter(...); // grab any other locks you need
159 * tx = dmu_tx_create(...); // get DMU tx
160 * dmu_tx_hold_*(); // hold each object you might modify
161 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
162 * if (error) {
163 * rw_exit(...); // drop locks
164 * zfs_dirent_unlock(dl); // unlock directory entry
165 * VN_RELE(...); // release held vnodes
166 * if (error == ERESTART) {
167 * waited = B_TRUE;
168 * dmu_tx_wait(tx);
169 * dmu_tx_abort(tx);
170 * goto top;
171 * }
172 * dmu_tx_abort(tx); // abort DMU tx
173 * ZFS_EXIT(zfsvfs); // finished in zfs
174 * return (error); // really out of space
175 * }
176 * error = do_real_work(); // do whatever this VOP does
177 * if (error == 0)
178 * zfs_log_*(...); // on success, make ZIL entry
179 * dmu_tx_commit(tx); // commit DMU tx -- error or not
180 * rw_exit(...); // drop locks
181 * zfs_dirent_unlock(dl); // unlock directory entry
182 * VN_RELE(...); // release held vnodes
183 * zil_commit(zilog, foid); // synchronous when necessary
184 * ZFS_EXIT(zfsvfs); // finished in zfs
185 * return (error); // done, report error
186 */
187
188 /* ARGSUSED */
189 static int
190 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
191 {
192 znode_t *zp = VTOZ(*vpp);
193 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
194
195 ZFS_ENTER(zfsvfs);
196 ZFS_VERIFY_ZP(zp);
197
198 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
199 ((flag & FAPPEND) == 0)) {
200 ZFS_EXIT(zfsvfs);
201 return (SET_ERROR(EPERM));
202 }
203
204 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
205 ZTOV(zp)->v_type == VREG &&
206 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
207 if (fs_vscan(*vpp, cr, 0) != 0) {
208 ZFS_EXIT(zfsvfs);
209 return (SET_ERROR(EACCES));
210 }
211 }
212
213 /* Keep a count of the synchronous opens in the znode */
214 if (flag & (FSYNC | FDSYNC))
215 atomic_inc_32(&zp->z_sync_cnt);
216
217 ZFS_EXIT(zfsvfs);
218 return (0);
219 }
220
221 /* ARGSUSED */
222 static int
223 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
224 caller_context_t *ct)
225 {
226 znode_t *zp = VTOZ(vp);
227 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
228
229 /*
230 * Clean up any locks held by this process on the vp.
231 */
232 cleanlocks(vp, ddi_get_pid(), 0);
233 cleanshares(vp, ddi_get_pid());
234
235 ZFS_ENTER(zfsvfs);
236 ZFS_VERIFY_ZP(zp);
237
238 /* Decrement the synchronous opens in the znode */
239 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
240 atomic_dec_32(&zp->z_sync_cnt);
241
242 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
243 ZTOV(zp)->v_type == VREG &&
244 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
245 VERIFY(fs_vscan(vp, cr, 1) == 0);
246
247 ZFS_EXIT(zfsvfs);
248 return (0);
249 }
250
251 /*
252 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
253 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
254 */
255 static int
256 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
257 {
258 znode_t *zp = VTOZ(vp);
259 uint64_t noff = (uint64_t)*off; /* new offset */
260 uint64_t file_sz;
261 int error;
262 boolean_t hole;
263
264 file_sz = zp->z_size;
265 if (noff >= file_sz) {
266 return (SET_ERROR(ENXIO));
267 }
268
269 if (cmd == _FIO_SEEK_HOLE)
270 hole = B_TRUE;
271 else
272 hole = B_FALSE;
273
274 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
275
276 if (error == ESRCH)
277 return (SET_ERROR(ENXIO));
278
279 /*
280 * We could find a hole that begins after the logical end-of-file,
281 * because dmu_offset_next() only works on whole blocks. If the
282 * EOF falls mid-block, then indicate that the "virtual hole"
283 * at the end of the file begins at the logical EOF, rather than
284 * at the end of the last block.
285 */
286 if (noff > file_sz) {
287 ASSERT(hole);
288 noff = file_sz;
289 }
290
291 if (noff < *off)
292 return (error);
293 *off = noff;
294 return (error);
295 }
296
297 /* ARGSUSED */
298 static int
299 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
300 int *rvalp, caller_context_t *ct)
301 {
302 offset_t off;
303 offset_t ndata;
304 dmu_object_info_t doi;
305 int error;
306 zfsvfs_t *zfsvfs;
307 znode_t *zp;
308
309 switch (com) {
310 case _FIOFFS:
311 {
312 return (zfs_sync(vp->v_vfsp, 0, cred));
313
314 /*
315 * The following two ioctls are used by bfu. Faking out,
316 * necessary to avoid bfu errors.
317 */
318 }
319 case _FIOGDIO:
320 case _FIOSDIO:
321 {
322 return (0);
323 }
324
325 case _FIO_SEEK_DATA:
326 case _FIO_SEEK_HOLE:
327 {
328 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
329 return (SET_ERROR(EFAULT));
330
331 zp = VTOZ(vp);
332 zfsvfs = zp->z_zfsvfs;
333 ZFS_ENTER(zfsvfs);
334 ZFS_VERIFY_ZP(zp);
335
336 /* offset parameter is in/out */
337 error = zfs_holey(vp, com, &off);
338 ZFS_EXIT(zfsvfs);
339 if (error)
340 return (error);
341 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
342 return (SET_ERROR(EFAULT));
343 return (0);
344 }
345 case _FIO_COUNT_FILLED:
346 {
347 /*
348 * _FIO_COUNT_FILLED adds a new ioctl command which
349 * exposes the number of filled blocks in a
350 * ZFS object.
351 */
352 zp = VTOZ(vp);
353 zfsvfs = zp->z_zfsvfs;
354 ZFS_ENTER(zfsvfs);
355 ZFS_VERIFY_ZP(zp);
356
357 /*
358 * Wait for all dirty blocks for this object
359 * to get synced out to disk, and the DMU info
360 * updated.
361 */
362 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
363 if (error) {
364 ZFS_EXIT(zfsvfs);
365 return (error);
366 }
367
368 /*
369 * Retrieve fill count from DMU object.
370 */
371 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
372 if (error) {
373 ZFS_EXIT(zfsvfs);
374 return (error);
375 }
376
377 ndata = doi.doi_fill_count;
378
379 ZFS_EXIT(zfsvfs);
380 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
381 return (SET_ERROR(EFAULT));
382 return (0);
383 }
384 }
385 return (SET_ERROR(ENOTTY));
386 }
387
388 /*
389 * Utility functions to map and unmap a single physical page. These
390 * are used to manage the mappable copies of ZFS file data, and therefore
391 * do not update ref/mod bits.
392 */
393 caddr_t
394 zfs_map_page(page_t *pp, enum seg_rw rw)
395 {
396 if (kpm_enable)
397 return (hat_kpm_mapin(pp, 0));
398 ASSERT(rw == S_READ || rw == S_WRITE);
399 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
400 (caddr_t)-1));
401 }
402
403 void
404 zfs_unmap_page(page_t *pp, caddr_t addr)
405 {
406 if (kpm_enable) {
407 hat_kpm_mapout(pp, 0, addr);
408 } else {
409 ppmapout(addr);
410 }
411 }
412
413 /*
414 * When a file is memory mapped, we must keep the IO data synchronized
415 * between the DMU cache and the memory mapped pages. What this means:
416 *
417 * On Write: If we find a memory mapped page, we write to *both*
418 * the page and the dmu buffer.
419 */
420 static void
421 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
422 {
423 int64_t off;
424
425 off = start & PAGEOFFSET;
426 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
427 page_t *pp;
428 uint64_t nbytes = MIN(PAGESIZE - off, len);
429
430 if (pp = page_lookup(vp, start, SE_SHARED)) {
431 caddr_t va;
432
433 va = zfs_map_page(pp, S_WRITE);
434 (void) dmu_read(os, oid, start+off, nbytes, va+off,
435 DMU_READ_PREFETCH);
436 zfs_unmap_page(pp, va);
437 page_unlock(pp);
438 }
439 len -= nbytes;
440 off = 0;
441 }
442 }
443
444 /*
445 * When a file is memory mapped, we must keep the IO data synchronized
446 * between the DMU cache and the memory mapped pages. What this means:
447 *
448 * On Read: We "read" preferentially from memory mapped pages,
449 * else we default from the dmu buffer.
450 *
451 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
452 * the file is memory mapped.
453 */
454 static int
455 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
456 {
457 znode_t *zp = VTOZ(vp);
458 int64_t start, off;
459 int len = nbytes;
460 int error = 0;
461
462 start = uio->uio_loffset;
463 off = start & PAGEOFFSET;
464 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
465 page_t *pp;
466 uint64_t bytes = MIN(PAGESIZE - off, len);
467
468 if (pp = page_lookup(vp, start, SE_SHARED)) {
469 caddr_t va;
470
471 va = zfs_map_page(pp, S_READ);
472 error = uiomove(va + off, bytes, UIO_READ, uio);
473 zfs_unmap_page(pp, va);
474 page_unlock(pp);
475 } else {
476 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
477 uio, bytes);
478 }
479 len -= bytes;
480 off = 0;
481 if (error)
482 break;
483 }
484 return (error);
485 }
486
487 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
488
489 /*
490 * Read bytes from specified file into supplied buffer.
491 *
492 * IN: vp - vnode of file to be read from.
493 * uio - structure supplying read location, range info,
494 * and return buffer.
495 * ioflag - SYNC flags; used to provide FRSYNC semantics.
496 * cr - credentials of caller.
497 * ct - caller context
498 *
499 * OUT: uio - updated offset and range, buffer filled.
500 *
501 * RETURN: 0 on success, error code on failure.
502 *
503 * Side Effects:
504 * vp - atime updated if byte count > 0
505 */
506 /* ARGSUSED */
507 static int
508 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
509 {
510 znode_t *zp = VTOZ(vp);
511 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
512 ssize_t n, nbytes;
513 int error = 0;
514 rl_t *rl;
515 xuio_t *xuio = NULL;
516
517 ZFS_ENTER(zfsvfs);
518 ZFS_VERIFY_ZP(zp);
519
520 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
521 ZFS_EXIT(zfsvfs);
522 return (SET_ERROR(EACCES));
523 }
524
525 /*
526 * Validate file offset
527 */
528 if (uio->uio_loffset < (offset_t)0) {
529 ZFS_EXIT(zfsvfs);
530 return (SET_ERROR(EINVAL));
531 }
532
533 /*
534 * Fasttrack empty reads
535 */
536 if (uio->uio_resid == 0) {
537 ZFS_EXIT(zfsvfs);
538 return (0);
539 }
540
541 /*
542 * Check for mandatory locks
543 */
544 if (MANDMODE(zp->z_mode)) {
545 if (error = chklock(vp, FREAD,
546 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
547 ZFS_EXIT(zfsvfs);
548 return (error);
549 }
550 }
551
552 /*
553 * If we're in FRSYNC mode, sync out this znode before reading it.
554 */
555 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
556 zil_commit(zfsvfs->z_log, zp->z_id);
557
558 /*
559 * Lock the range against changes.
560 */
561 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
562
563 /*
564 * If we are reading past end-of-file we can skip
565 * to the end; but we might still need to set atime.
566 */
567 if (uio->uio_loffset >= zp->z_size) {
568 error = 0;
569 goto out;
570 }
571
572 ASSERT(uio->uio_loffset < zp->z_size);
573 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
574
575 if ((uio->uio_extflg == UIO_XUIO) &&
576 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
577 int nblk;
578 int blksz = zp->z_blksz;
579 uint64_t offset = uio->uio_loffset;
580
581 xuio = (xuio_t *)uio;
582 if ((ISP2(blksz))) {
583 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
584 blksz)) / blksz;
585 } else {
586 ASSERT(offset + n <= blksz);
587 nblk = 1;
588 }
589 (void) dmu_xuio_init(xuio, nblk);
590
591 if (vn_has_cached_data(vp)) {
592 /*
593 * For simplicity, we always allocate a full buffer
594 * even if we only expect to read a portion of a block.
595 */
596 while (--nblk >= 0) {
597 (void) dmu_xuio_add(xuio,
598 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
599 blksz), 0, blksz);
600 }
601 }
602 }
603
604 while (n > 0) {
605 nbytes = MIN(n, zfs_read_chunk_size -
606 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
607
608 if (vn_has_cached_data(vp)) {
609 error = mappedread(vp, nbytes, uio);
610 } else {
611 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
612 uio, nbytes);
613 }
614 if (error) {
615 /* convert checksum errors into IO errors */
616 if (error == ECKSUM)
617 error = SET_ERROR(EIO);
618 break;
619 }
620
621 n -= nbytes;
622 }
623 out:
624 zfs_range_unlock(rl);
625
626 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
627 ZFS_EXIT(zfsvfs);
628 return (error);
629 }
630
631 /*
632 * Write the bytes to a file.
633 *
634 * IN: vp - vnode of file to be written to.
635 * uio - structure supplying write location, range info,
636 * and data buffer.
637 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
638 * set if in append mode.
639 * cr - credentials of caller.
640 * ct - caller context (NFS/CIFS fem monitor only)
641 *
642 * OUT: uio - updated offset and range.
643 *
644 * RETURN: 0 on success, error code on failure.
645 *
646 * Timestamps:
647 * vp - ctime|mtime updated if byte count > 0
648 */
649
650 /* ARGSUSED */
651 static int
652 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
653 {
654 znode_t *zp = VTOZ(vp);
655 rlim64_t limit = uio->uio_llimit;
656 ssize_t start_resid = uio->uio_resid;
657 ssize_t tx_bytes;
658 uint64_t end_size;
659 dmu_tx_t *tx;
660 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
661 zilog_t *zilog;
662 offset_t woff;
663 ssize_t n, nbytes;
664 rl_t *rl;
665 int max_blksz = zfsvfs->z_max_blksz;
666 int error = 0;
667 int prev_error;
668 arc_buf_t *abuf;
669 iovec_t *aiov = NULL;
670 xuio_t *xuio = NULL;
671 int i_iov = 0;
672 int iovcnt = uio->uio_iovcnt;
673 iovec_t *iovp = uio->uio_iov;
674 int write_eof;
675 int count = 0;
676 sa_bulk_attr_t bulk[4];
677 uint64_t mtime[2], ctime[2];
678
679 /*
680 * Fasttrack empty write
681 */
682 n = start_resid;
683 if (n == 0)
684 return (0);
685
686 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
687 limit = MAXOFFSET_T;
688
689 /*
690 * Pre-fault the pages to ensure slow (eg NFS) pages
691 * don't hold up txg.
692 * Skip this if uio contains loaned arc_buf.
693 */
694 if ((uio->uio_extflg == UIO_XUIO) &&
695 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
696 xuio = (xuio_t *)uio;
697 else
698 uio_prefaultpages(n, uio);
699
700 ZFS_ENTER(zfsvfs);
701 ZFS_VERIFY_ZP(zp);
702
703 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
704 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
705 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
706 &zp->z_size, 8);
707 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
708 &zp->z_pflags, 8);
709
710 /*
711 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
712 * callers might not be able to detect properly that we are read-only,
713 * so check it explicitly here.
714 */
715 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
716 ZFS_EXIT(zfsvfs);
717 return (SET_ERROR(EROFS));
718 }
719
720 /*
721 * If immutable or not appending then return EPERM
722 */
723 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
724 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
725 (uio->uio_loffset < zp->z_size))) {
726 ZFS_EXIT(zfsvfs);
727 return (SET_ERROR(EPERM));
728 }
729
730 zilog = zfsvfs->z_log;
731
732 /*
733 * Validate file offset
734 */
735 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
736 if (woff < 0) {
737 ZFS_EXIT(zfsvfs);
738 return (SET_ERROR(EINVAL));
739 }
740
741 /*
742 * Check for mandatory locks before calling zfs_range_lock()
743 * in order to prevent a deadlock with locks set via fcntl().
744 */
745 if (MANDMODE((mode_t)zp->z_mode) &&
746 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
747 ZFS_EXIT(zfsvfs);
748 return (error);
749 }
750
751 /*
752 * If in append mode, set the io offset pointer to eof.
753 */
754 if (ioflag & FAPPEND) {
755 /*
756 * Obtain an appending range lock to guarantee file append
757 * semantics. We reset the write offset once we have the lock.
758 */
759 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
760 woff = rl->r_off;
761 if (rl->r_len == UINT64_MAX) {
762 /*
763 * We overlocked the file because this write will cause
764 * the file block size to increase.
765 * Note that zp_size cannot change with this lock held.
766 */
767 woff = zp->z_size;
768 }
769 uio->uio_loffset = woff;
770 } else {
771 /*
772 * Note that if the file block size will change as a result of
773 * this write, then this range lock will lock the entire file
774 * so that we can re-write the block safely.
775 */
776 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
777 }
778
779 if (woff >= limit) {
780 zfs_range_unlock(rl);
781 ZFS_EXIT(zfsvfs);
782 return (SET_ERROR(EFBIG));
783 }
784
785 if ((woff + n) > limit || woff > (limit - n))
786 n = limit - woff;
787
788 /* Will this write extend the file length? */
789 write_eof = (woff + n > zp->z_size);
790
791 end_size = MAX(zp->z_size, woff + n);
792
793 /*
794 * Write the file in reasonable size chunks. Each chunk is written
795 * in a separate transaction; this keeps the intent log records small
796 * and allows us to do more fine-grained space accounting.
797 */
798 while (n > 0) {
799 abuf = NULL;
800 woff = uio->uio_loffset;
801 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
802 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
803 if (abuf != NULL)
804 dmu_return_arcbuf(abuf);
805 error = SET_ERROR(EDQUOT);
806 break;
807 }
808
809 if (xuio && abuf == NULL) {
810 ASSERT(i_iov < iovcnt);
811 aiov = &iovp[i_iov];
812 abuf = dmu_xuio_arcbuf(xuio, i_iov);
813 dmu_xuio_clear(xuio, i_iov);
814 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
815 iovec_t *, aiov, arc_buf_t *, abuf);
816 ASSERT((aiov->iov_base == abuf->b_data) ||
817 ((char *)aiov->iov_base - (char *)abuf->b_data +
818 aiov->iov_len == arc_buf_size(abuf)));
819 i_iov++;
820 } else if (abuf == NULL && n >= max_blksz &&
821 woff >= zp->z_size &&
822 P2PHASE(woff, max_blksz) == 0 &&
823 zp->z_blksz == max_blksz) {
824 /*
825 * This write covers a full block. "Borrow" a buffer
826 * from the dmu so that we can fill it before we enter
827 * a transaction. This avoids the possibility of
828 * holding up the transaction if the data copy hangs
829 * up on a pagefault (e.g., from an NFS server mapping).
830 */
831 size_t cbytes;
832
833 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
834 max_blksz);
835 ASSERT(abuf != NULL);
836 ASSERT(arc_buf_size(abuf) == max_blksz);
837 if (error = uiocopy(abuf->b_data, max_blksz,
838 UIO_WRITE, uio, &cbytes)) {
839 dmu_return_arcbuf(abuf);
840 break;
841 }
842 ASSERT(cbytes == max_blksz);
843 }
844
845 /*
846 * Start a transaction.
847 */
848 tx = dmu_tx_create(zfsvfs->z_os);
849 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
850 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
851 zfs_sa_upgrade_txholds(tx, zp);
852 error = dmu_tx_assign(tx, TXG_WAIT);
853 if (error) {
854 dmu_tx_abort(tx);
855 if (abuf != NULL)
856 dmu_return_arcbuf(abuf);
857 break;
858 }
859
860 /*
861 * If zfs_range_lock() over-locked we grow the blocksize
862 * and then reduce the lock range. This will only happen
863 * on the first iteration since zfs_range_reduce() will
864 * shrink down r_len to the appropriate size.
865 */
866 if (rl->r_len == UINT64_MAX) {
867 uint64_t new_blksz;
868
869 if (zp->z_blksz > max_blksz) {
870 /*
871 * File's blocksize is already larger than the
872 * "recordsize" property. Only let it grow to
873 * the next power of 2.
874 */
875 ASSERT(!ISP2(zp->z_blksz));
876 new_blksz = MIN(end_size,
877 1 << highbit64(zp->z_blksz));
878 } else {
879 new_blksz = MIN(end_size, max_blksz);
880 }
881 zfs_grow_blocksize(zp, new_blksz, tx);
882 zfs_range_reduce(rl, woff, n);
883 }
884
885 /*
886 * XXX - should we really limit each write to z_max_blksz?
887 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
888 */
889 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
890
891 if (abuf == NULL) {
892 tx_bytes = uio->uio_resid;
893 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
894 uio, nbytes, tx);
895 tx_bytes -= uio->uio_resid;
896 } else {
897 tx_bytes = nbytes;
898 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
899 /*
900 * If this is not a full block write, but we are
901 * extending the file past EOF and this data starts
902 * block-aligned, use assign_arcbuf(). Otherwise,
903 * write via dmu_write().
904 */
905 if (tx_bytes < max_blksz && (!write_eof ||
906 aiov->iov_base != abuf->b_data)) {
907 ASSERT(xuio);
908 dmu_write(zfsvfs->z_os, zp->z_id, woff,
909 aiov->iov_len, aiov->iov_base, tx);
910 dmu_return_arcbuf(abuf);
911 xuio_stat_wbuf_copied();
912 } else {
913 ASSERT(xuio || tx_bytes == max_blksz);
914 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
915 woff, abuf, tx);
916 }
917 ASSERT(tx_bytes <= uio->uio_resid);
918 uioskip(uio, tx_bytes);
919 }
920 if (tx_bytes && vn_has_cached_data(vp)) {
921 update_pages(vp, woff,
922 tx_bytes, zfsvfs->z_os, zp->z_id);
923 }
924
925 /*
926 * If we made no progress, we're done. If we made even
927 * partial progress, update the znode and ZIL accordingly.
928 */
929 if (tx_bytes == 0) {
930 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
931 (void *)&zp->z_size, sizeof (uint64_t), tx);
932 dmu_tx_commit(tx);
933 ASSERT(error != 0);
934 break;
935 }
936
937 /*
938 * Clear Set-UID/Set-GID bits on successful write if not
939 * privileged and at least one of the excute bits is set.
940 *
941 * It would be nice to to this after all writes have
942 * been done, but that would still expose the ISUID/ISGID
943 * to another app after the partial write is committed.
944 *
945 * Note: we don't call zfs_fuid_map_id() here because
946 * user 0 is not an ephemeral uid.
947 */
948 mutex_enter(&zp->z_acl_lock);
949 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
950 (S_IXUSR >> 6))) != 0 &&
951 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
952 secpolicy_vnode_setid_retain(cr,
953 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
954 uint64_t newmode;
955 zp->z_mode &= ~(S_ISUID | S_ISGID);
956 newmode = zp->z_mode;
957 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
958 (void *)&newmode, sizeof (uint64_t), tx);
959 }
960 mutex_exit(&zp->z_acl_lock);
961
962 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
963 B_TRUE);
964
965 /*
966 * Update the file size (zp_size) if it has changed;
967 * account for possible concurrent updates.
968 */
969 while ((end_size = zp->z_size) < uio->uio_loffset) {
970 (void) atomic_cas_64(&zp->z_size, end_size,
971 uio->uio_loffset);
972 }
973 /*
974 * If we are replaying and eof is non zero then force
975 * the file size to the specified eof. Note, there's no
976 * concurrency during replay.
977 */
978 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
979 zp->z_size = zfsvfs->z_replay_eof;
980
981 /*
982 * Keep track of a possible pre-existing error from a partial
983 * write via dmu_write_uio_dbuf above.
984 */
985 prev_error = error;
986 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
987
988 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
989 dmu_tx_commit(tx);
990
991 if (prev_error != 0 || error != 0)
992 break;
993 ASSERT(tx_bytes == nbytes);
994 n -= nbytes;
995 }
996
997 zfs_range_unlock(rl);
998
999 /*
1000 * If we're in replay mode, or we made no progress, return error.
1001 * Otherwise, it's at least a partial write, so it's successful.
1002 */
1003 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1004 ZFS_EXIT(zfsvfs);
1005 return (error);
1006 }
1007
1008 if (ioflag & (FSYNC | FDSYNC) ||
1009 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1010 zil_commit(zilog, zp->z_id);
1011
1012 ZFS_EXIT(zfsvfs);
1013 return (0);
1014 }
1015
1016 void
1017 zfs_get_done(zgd_t *zgd, int error)
1018 {
1019 znode_t *zp = zgd->zgd_private;
1020 objset_t *os = zp->z_zfsvfs->z_os;
1021
1022 if (zgd->zgd_db)
1023 dmu_buf_rele(zgd->zgd_db, zgd);
1024
1025 zfs_range_unlock(zgd->zgd_rl);
1026
1027 /*
1028 * Release the vnode asynchronously as we currently have the
1029 * txg stopped from syncing.
1030 */
1031 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1032
1033 if (error == 0 && zgd->zgd_bp)
1034 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1035
1036 kmem_free(zgd, sizeof (zgd_t));
1037 }
1038
1039 #ifdef DEBUG
1040 static int zil_fault_io = 0;
1041 #endif
1042
1043 /*
1044 * Get data to generate a TX_WRITE intent log record.
1045 */
1046 int
1047 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1048 {
1049 zfsvfs_t *zfsvfs = arg;
1050 objset_t *os = zfsvfs->z_os;
1051 znode_t *zp;
1052 uint64_t object = lr->lr_foid;
1053 uint64_t offset = lr->lr_offset;
1054 uint64_t size = lr->lr_length;
1055 blkptr_t *bp = &lr->lr_blkptr;
1056 dmu_buf_t *db;
1057 zgd_t *zgd;
1058 int error = 0;
1059
1060 ASSERT(zio != NULL);
1061 ASSERT(size != 0);
1062
1063 /*
1064 * Nothing to do if the file has been removed
1065 */
1066 if (zfs_zget(zfsvfs, object, &zp) != 0)
1067 return (SET_ERROR(ENOENT));
1068 if (zp->z_unlinked) {
1069 /*
1070 * Release the vnode asynchronously as we currently have the
1071 * txg stopped from syncing.
1072 */
1073 VN_RELE_ASYNC(ZTOV(zp),
1074 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1075 return (SET_ERROR(ENOENT));
1076 }
1077
1078 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1079 zgd->zgd_zilog = zfsvfs->z_log;
1080 zgd->zgd_private = zp;
1081
1082 /*
1083 * Write records come in two flavors: immediate and indirect.
1084 * For small writes it's cheaper to store the data with the
1085 * log record (immediate); for large writes it's cheaper to
1086 * sync the data and get a pointer to it (indirect) so that
1087 * we don't have to write the data twice.
1088 */
1089 if (buf != NULL) { /* immediate write */
1090 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1091 /* test for truncation needs to be done while range locked */
1092 if (offset >= zp->z_size) {
1093 error = SET_ERROR(ENOENT);
1094 } else {
1095 error = dmu_read(os, object, offset, size, buf,
1096 DMU_READ_NO_PREFETCH);
1097 }
1098 ASSERT(error == 0 || error == ENOENT);
1099 } else { /* indirect write */
1100 /*
1101 * Have to lock the whole block to ensure when it's
1102 * written out and it's checksum is being calculated
1103 * that no one can change the data. We need to re-check
1104 * blocksize after we get the lock in case it's changed!
1105 */
1106 for (;;) {
1107 uint64_t blkoff;
1108 size = zp->z_blksz;
1109 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1110 offset -= blkoff;
1111 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1112 RL_READER);
1113 if (zp->z_blksz == size)
1114 break;
1115 offset += blkoff;
1116 zfs_range_unlock(zgd->zgd_rl);
1117 }
1118 /* test for truncation needs to be done while range locked */
1119 if (lr->lr_offset >= zp->z_size)
1120 error = SET_ERROR(ENOENT);
1121 #ifdef DEBUG
1122 if (zil_fault_io) {
1123 error = SET_ERROR(EIO);
1124 zil_fault_io = 0;
1125 }
1126 #endif
1127 if (error == 0)
1128 error = dmu_buf_hold(os, object, offset, zgd, &db,
1129 DMU_READ_NO_PREFETCH);
1130
1131 if (error == 0) {
1132 blkptr_t *obp = dmu_buf_get_blkptr(db);
1133 if (obp) {
1134 ASSERT(BP_IS_HOLE(bp));
1135 *bp = *obp;
1136 }
1137
1138 zgd->zgd_db = db;
1139 zgd->zgd_bp = bp;
1140
1141 ASSERT(db->db_offset == offset);
1142 ASSERT(db->db_size == size);
1143
1144 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1145 zfs_get_done, zgd);
1146 ASSERT(error || lr->lr_length <= zp->z_blksz);
1147
1148 /*
1149 * On success, we need to wait for the write I/O
1150 * initiated by dmu_sync() to complete before we can
1151 * release this dbuf. We will finish everything up
1152 * in the zfs_get_done() callback.
1153 */
1154 if (error == 0)
1155 return (0);
1156
1157 if (error == EALREADY) {
1158 lr->lr_common.lrc_txtype = TX_WRITE2;
1159 error = 0;
1160 }
1161 }
1162 }
1163
1164 zfs_get_done(zgd, error);
1165
1166 return (error);
1167 }
1168
1169 /*ARGSUSED*/
1170 static int
1171 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1172 caller_context_t *ct)
1173 {
1174 znode_t *zp = VTOZ(vp);
1175 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1176 int error;
1177
1178 ZFS_ENTER(zfsvfs);
1179 ZFS_VERIFY_ZP(zp);
1180
1181 if (flag & V_ACE_MASK)
1182 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1183 else
1184 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1185
1186 ZFS_EXIT(zfsvfs);
1187 return (error);
1188 }
1189
1190 /*
1191 * If vnode is for a device return a specfs vnode instead.
1192 */
1193 static int
1194 specvp_check(vnode_t **vpp, cred_t *cr)
1195 {
1196 int error = 0;
1197
1198 if (IS_DEVVP(*vpp)) {
1199 struct vnode *svp;
1200
1201 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1202 VN_RELE(*vpp);
1203 if (svp == NULL)
1204 error = SET_ERROR(ENOSYS);
1205 *vpp = svp;
1206 }
1207 return (error);
1208 }
1209
1210
1211 /*
1212 * Lookup an entry in a directory, or an extended attribute directory.
1213 * If it exists, return a held vnode reference for it.
1214 *
1215 * IN: dvp - vnode of directory to search.
1216 * nm - name of entry to lookup.
1217 * pnp - full pathname to lookup [UNUSED].
1218 * flags - LOOKUP_XATTR set if looking for an attribute.
1219 * rdir - root directory vnode [UNUSED].
1220 * cr - credentials of caller.
1221 * ct - caller context
1222 * direntflags - directory lookup flags
1223 * realpnp - returned pathname.
1224 *
1225 * OUT: vpp - vnode of located entry, NULL if not found.
1226 *
1227 * RETURN: 0 on success, error code on failure.
1228 *
1229 * Timestamps:
1230 * NA
1231 */
1232 /* ARGSUSED */
1233 static int
1234 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1235 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1236 int *direntflags, pathname_t *realpnp)
1237 {
1238 znode_t *zdp = VTOZ(dvp);
1239 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1240 int error = 0;
1241
1242 /* fast path */
1243 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1244
1245 if (dvp->v_type != VDIR) {
1246 return (SET_ERROR(ENOTDIR));
1247 } else if (zdp->z_sa_hdl == NULL) {
1248 return (SET_ERROR(EIO));
1249 }
1250
1251 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1252 error = zfs_fastaccesschk_execute(zdp, cr);
1253 if (!error) {
1254 *vpp = dvp;
1255 VN_HOLD(*vpp);
1256 return (0);
1257 }
1258 return (error);
1259 } else {
1260 vnode_t *tvp = dnlc_lookup(dvp, nm);
1261
1262 if (tvp) {
1263 error = zfs_fastaccesschk_execute(zdp, cr);
1264 if (error) {
1265 VN_RELE(tvp);
1266 return (error);
1267 }
1268 if (tvp == DNLC_NO_VNODE) {
1269 VN_RELE(tvp);
1270 return (SET_ERROR(ENOENT));
1271 } else {
1272 *vpp = tvp;
1273 return (specvp_check(vpp, cr));
1274 }
1275 }
1276 }
1277 }
1278
1279 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1280
1281 ZFS_ENTER(zfsvfs);
1282 ZFS_VERIFY_ZP(zdp);
1283
1284 *vpp = NULL;
1285
1286 if (flags & LOOKUP_XATTR) {
1287 /*
1288 * If the xattr property is off, refuse the lookup request.
1289 */
1290 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1291 ZFS_EXIT(zfsvfs);
1292 return (SET_ERROR(EINVAL));
1293 }
1294
1295 /*
1296 * We don't allow recursive attributes..
1297 * Maybe someday we will.
1298 */
1299 if (zdp->z_pflags & ZFS_XATTR) {
1300 ZFS_EXIT(zfsvfs);
1301 return (SET_ERROR(EINVAL));
1302 }
1303
1304 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1305 ZFS_EXIT(zfsvfs);
1306 return (error);
1307 }
1308
1309 /*
1310 * Do we have permission to get into attribute directory?
1311 */
1312
1313 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1314 B_FALSE, cr)) {
1315 VN_RELE(*vpp);
1316 *vpp = NULL;
1317 }
1318
1319 ZFS_EXIT(zfsvfs);
1320 return (error);
1321 }
1322
1323 if (dvp->v_type != VDIR) {
1324 ZFS_EXIT(zfsvfs);
1325 return (SET_ERROR(ENOTDIR));
1326 }
1327
1328 /*
1329 * Check accessibility of directory.
1330 */
1331
1332 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1333 ZFS_EXIT(zfsvfs);
1334 return (error);
1335 }
1336
1337 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1338 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1339 ZFS_EXIT(zfsvfs);
1340 return (SET_ERROR(EILSEQ));
1341 }
1342
1343 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1344 if (error == 0)
1345 error = specvp_check(vpp, cr);
1346
1347 ZFS_EXIT(zfsvfs);
1348 return (error);
1349 }
1350
1351 /*
1352 * Attempt to create a new entry in a directory. If the entry
1353 * already exists, truncate the file if permissible, else return
1354 * an error. Return the vp of the created or trunc'd file.
1355 *
1356 * IN: dvp - vnode of directory to put new file entry in.
1357 * name - name of new file entry.
1358 * vap - attributes of new file.
1359 * excl - flag indicating exclusive or non-exclusive mode.
1360 * mode - mode to open file with.
1361 * cr - credentials of caller.
1362 * flag - large file flag [UNUSED].
1363 * ct - caller context
1364 * vsecp - ACL to be set
1365 *
1366 * OUT: vpp - vnode of created or trunc'd entry.
1367 *
1368 * RETURN: 0 on success, error code on failure.
1369 *
1370 * Timestamps:
1371 * dvp - ctime|mtime updated if new entry created
1372 * vp - ctime|mtime always, atime if new
1373 */
1374
1375 /* ARGSUSED */
1376 static int
1377 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1378 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1379 vsecattr_t *vsecp)
1380 {
1381 znode_t *zp, *dzp = VTOZ(dvp);
1382 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1383 zilog_t *zilog;
1384 objset_t *os;
1385 zfs_dirlock_t *dl;
1386 dmu_tx_t *tx;
1387 int error;
1388 ksid_t *ksid;
1389 uid_t uid;
1390 gid_t gid = crgetgid(cr);
1391 zfs_acl_ids_t acl_ids;
1392 boolean_t fuid_dirtied;
1393 boolean_t have_acl = B_FALSE;
1394 boolean_t waited = B_FALSE;
1395
1396 /*
1397 * If we have an ephemeral id, ACL, or XVATTR then
1398 * make sure file system is at proper version
1399 */
1400
1401 ksid = crgetsid(cr, KSID_OWNER);
1402 if (ksid)
1403 uid = ksid_getid(ksid);
1404 else
1405 uid = crgetuid(cr);
1406
1407 if (zfsvfs->z_use_fuids == B_FALSE &&
1408 (vsecp || (vap->va_mask & AT_XVATTR) ||
1409 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1410 return (SET_ERROR(EINVAL));
1411
1412 ZFS_ENTER(zfsvfs);
1413 ZFS_VERIFY_ZP(dzp);
1414 os = zfsvfs->z_os;
1415 zilog = zfsvfs->z_log;
1416
1417 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1418 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1419 ZFS_EXIT(zfsvfs);
1420 return (SET_ERROR(EILSEQ));
1421 }
1422
1423 if (vap->va_mask & AT_XVATTR) {
1424 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1425 crgetuid(cr), cr, vap->va_type)) != 0) {
1426 ZFS_EXIT(zfsvfs);
1427 return (error);
1428 }
1429 }
1430 top:
1431 *vpp = NULL;
1432
1433 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1434 vap->va_mode &= ~VSVTX;
1435
1436 if (*name == '\0') {
1437 /*
1438 * Null component name refers to the directory itself.
1439 */
1440 VN_HOLD(dvp);
1441 zp = dzp;
1442 dl = NULL;
1443 error = 0;
1444 } else {
1445 /* possible VN_HOLD(zp) */
1446 int zflg = 0;
1447
1448 if (flag & FIGNORECASE)
1449 zflg |= ZCILOOK;
1450
1451 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1452 NULL, NULL);
1453 if (error) {
1454 if (have_acl)
1455 zfs_acl_ids_free(&acl_ids);
1456 if (strcmp(name, "..") == 0)
1457 error = SET_ERROR(EISDIR);
1458 ZFS_EXIT(zfsvfs);
1459 return (error);
1460 }
1461 }
1462
1463 if (zp == NULL) {
1464 uint64_t txtype;
1465
1466 /*
1467 * Create a new file object and update the directory
1468 * to reference it.
1469 */
1470 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1471 if (have_acl)
1472 zfs_acl_ids_free(&acl_ids);
1473 goto out;
1474 }
1475
1476 /*
1477 * We only support the creation of regular files in
1478 * extended attribute directories.
1479 */
1480
1481 if ((dzp->z_pflags & ZFS_XATTR) &&
1482 (vap->va_type != VREG)) {
1483 if (have_acl)
1484 zfs_acl_ids_free(&acl_ids);
1485 error = SET_ERROR(EINVAL);
1486 goto out;
1487 }
1488
1489 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1490 cr, vsecp, &acl_ids)) != 0)
1491 goto out;
1492 have_acl = B_TRUE;
1493
1494 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1495 zfs_acl_ids_free(&acl_ids);
1496 error = SET_ERROR(EDQUOT);
1497 goto out;
1498 }
1499
1500 tx = dmu_tx_create(os);
1501
1502 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1503 ZFS_SA_BASE_ATTR_SIZE);
1504
1505 fuid_dirtied = zfsvfs->z_fuid_dirty;
1506 if (fuid_dirtied)
1507 zfs_fuid_txhold(zfsvfs, tx);
1508 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1509 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1510 if (!zfsvfs->z_use_sa &&
1511 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1512 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1513 0, acl_ids.z_aclp->z_acl_bytes);
1514 }
1515 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1516 if (error) {
1517 zfs_dirent_unlock(dl);
1518 if (error == ERESTART) {
1519 waited = B_TRUE;
1520 dmu_tx_wait(tx);
1521 dmu_tx_abort(tx);
1522 goto top;
1523 }
1524 zfs_acl_ids_free(&acl_ids);
1525 dmu_tx_abort(tx);
1526 ZFS_EXIT(zfsvfs);
1527 return (error);
1528 }
1529 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1530
1531 if (fuid_dirtied)
1532 zfs_fuid_sync(zfsvfs, tx);
1533
1534 (void) zfs_link_create(dl, zp, tx, ZNEW);
1535 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1536 if (flag & FIGNORECASE)
1537 txtype |= TX_CI;
1538 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1539 vsecp, acl_ids.z_fuidp, vap);
1540 zfs_acl_ids_free(&acl_ids);
1541 dmu_tx_commit(tx);
1542 } else {
1543 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1544
1545 if (have_acl)
1546 zfs_acl_ids_free(&acl_ids);
1547 have_acl = B_FALSE;
1548
1549 /*
1550 * A directory entry already exists for this name.
1551 */
1552 /*
1553 * Can't truncate an existing file if in exclusive mode.
1554 */
1555 if (excl == EXCL) {
1556 error = SET_ERROR(EEXIST);
1557 goto out;
1558 }
1559 /*
1560 * Can't open a directory for writing.
1561 */
1562 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1563 error = SET_ERROR(EISDIR);
1564 goto out;
1565 }
1566 /*
1567 * Verify requested access to file.
1568 */
1569 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1570 goto out;
1571 }
1572
1573 mutex_enter(&dzp->z_lock);
1574 dzp->z_seq++;
1575 mutex_exit(&dzp->z_lock);
1576
1577 /*
1578 * Truncate regular files if requested.
1579 */
1580 if ((ZTOV(zp)->v_type == VREG) &&
1581 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1582 /* we can't hold any locks when calling zfs_freesp() */
1583 zfs_dirent_unlock(dl);
1584 dl = NULL;
1585 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1586 if (error == 0) {
1587 vnevent_create(ZTOV(zp), ct);
1588 }
1589 }
1590 }
1591 out:
1592
1593 if (dl)
1594 zfs_dirent_unlock(dl);
1595
1596 if (error) {
1597 if (zp)
1598 VN_RELE(ZTOV(zp));
1599 } else {
1600 *vpp = ZTOV(zp);
1601 error = specvp_check(vpp, cr);
1602 }
1603
1604 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1605 zil_commit(zilog, 0);
1606
1607 ZFS_EXIT(zfsvfs);
1608 return (error);
1609 }
1610
1611 /*
1612 * Remove an entry from a directory.
1613 *
1614 * IN: dvp - vnode of directory to remove entry from.
1615 * name - name of entry to remove.
1616 * cr - credentials of caller.
1617 * ct - caller context
1618 * flags - case flags
1619 *
1620 * RETURN: 0 on success, error code on failure.
1621 *
1622 * Timestamps:
1623 * dvp - ctime|mtime
1624 * vp - ctime (if nlink > 0)
1625 */
1626
1627 uint64_t null_xattr = 0;
1628
1629 /*ARGSUSED*/
1630 static int
1631 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1632 int flags)
1633 {
1634 znode_t *zp, *dzp = VTOZ(dvp);
1635 znode_t *xzp;
1636 vnode_t *vp;
1637 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1638 zilog_t *zilog;
1639 uint64_t acl_obj, xattr_obj;
1640 uint64_t xattr_obj_unlinked = 0;
1641 uint64_t obj = 0;
1642 zfs_dirlock_t *dl;
1643 dmu_tx_t *tx;
1644 boolean_t may_delete_now, delete_now = FALSE;
1645 boolean_t unlinked, toobig = FALSE;
1646 uint64_t txtype;
1647 pathname_t *realnmp = NULL;
1648 pathname_t realnm;
1649 int error;
1650 int zflg = ZEXISTS;
1651 boolean_t waited = B_FALSE;
1652
1653 ZFS_ENTER(zfsvfs);
1654 ZFS_VERIFY_ZP(dzp);
1655 zilog = zfsvfs->z_log;
1656
1657 if (flags & FIGNORECASE) {
1658 zflg |= ZCILOOK;
1659 pn_alloc(&realnm);
1660 realnmp = &realnm;
1661 }
1662
1663 top:
1664 xattr_obj = 0;
1665 xzp = NULL;
1666 /*
1667 * Attempt to lock directory; fail if entry doesn't exist.
1668 */
1669 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1670 NULL, realnmp)) {
1671 if (realnmp)
1672 pn_free(realnmp);
1673 ZFS_EXIT(zfsvfs);
1674 return (error);
1675 }
1676
1677 vp = ZTOV(zp);
1678
1679 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1680 goto out;
1681 }
1682
1683 /*
1684 * Need to use rmdir for removing directories.
1685 */
1686 if (vp->v_type == VDIR) {
1687 error = SET_ERROR(EPERM);
1688 goto out;
1689 }
1690
1691 vnevent_remove(vp, dvp, name, ct);
1692
1693 if (realnmp)
1694 dnlc_remove(dvp, realnmp->pn_buf);
1695 else
1696 dnlc_remove(dvp, name);
1697
1698 mutex_enter(&vp->v_lock);
1699 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1700 mutex_exit(&vp->v_lock);
1701
1702 /*
1703 * We may delete the znode now, or we may put it in the unlinked set;
1704 * it depends on whether we're the last link, and on whether there are
1705 * other holds on the vnode. So we dmu_tx_hold() the right things to
1706 * allow for either case.
1707 */
1708 obj = zp->z_id;
1709 tx = dmu_tx_create(zfsvfs->z_os);
1710 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1711 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1712 zfs_sa_upgrade_txholds(tx, zp);
1713 zfs_sa_upgrade_txholds(tx, dzp);
1714 if (may_delete_now) {
1715 toobig =
1716 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1717 /* if the file is too big, only hold_free a token amount */
1718 dmu_tx_hold_free(tx, zp->z_id, 0,
1719 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1720 }
1721
1722 /* are there any extended attributes? */
1723 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1724 &xattr_obj, sizeof (xattr_obj));
1725 if (error == 0 && xattr_obj) {
1726 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1727 ASSERT0(error);
1728 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1729 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1730 }
1731
1732 mutex_enter(&zp->z_lock);
1733 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1734 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1735 mutex_exit(&zp->z_lock);
1736
1737 /* charge as an update -- would be nice not to charge at all */
1738 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1739
1740 /*
1741 * Mark this transaction as typically resulting in a net free of space
1742 */
1743 dmu_tx_mark_netfree(tx);
1744
1745 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1746 if (error) {
1747 zfs_dirent_unlock(dl);
1748 VN_RELE(vp);
1749 if (xzp)
1750 VN_RELE(ZTOV(xzp));
1751 if (error == ERESTART) {
1752 waited = B_TRUE;
1753 dmu_tx_wait(tx);
1754 dmu_tx_abort(tx);
1755 goto top;
1756 }
1757 if (realnmp)
1758 pn_free(realnmp);
1759 dmu_tx_abort(tx);
1760 ZFS_EXIT(zfsvfs);
1761 return (error);
1762 }
1763
1764 /*
1765 * Remove the directory entry.
1766 */
1767 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1768
1769 if (error) {
1770 dmu_tx_commit(tx);
1771 goto out;
1772 }
1773
1774 if (unlinked) {
1775 /*
1776 * Hold z_lock so that we can make sure that the ACL obj
1777 * hasn't changed. Could have been deleted due to
1778 * zfs_sa_upgrade().
1779 */
1780 mutex_enter(&zp->z_lock);
1781 mutex_enter(&vp->v_lock);
1782 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1783 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1784 delete_now = may_delete_now && !toobig &&
1785 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1786 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1787 acl_obj;
1788 mutex_exit(&vp->v_lock);
1789 }
1790
1791 if (delete_now) {
1792 if (xattr_obj_unlinked) {
1793 ASSERT3U(xzp->z_links, ==, 2);
1794 mutex_enter(&xzp->z_lock);
1795 xzp->z_unlinked = 1;
1796 xzp->z_links = 0;
1797 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1798 &xzp->z_links, sizeof (xzp->z_links), tx);
1799 ASSERT3U(error, ==, 0);
1800 mutex_exit(&xzp->z_lock);
1801 zfs_unlinked_add(xzp, tx);
1802
1803 if (zp->z_is_sa)
1804 error = sa_remove(zp->z_sa_hdl,
1805 SA_ZPL_XATTR(zfsvfs), tx);
1806 else
1807 error = sa_update(zp->z_sa_hdl,
1808 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1809 sizeof (uint64_t), tx);
1810 ASSERT0(error);
1811 }
1812 mutex_enter(&vp->v_lock);
1813 vp->v_count--;
1814 ASSERT0(vp->v_count);
1815 mutex_exit(&vp->v_lock);
1816 mutex_exit(&zp->z_lock);
1817 zfs_znode_delete(zp, tx);
1818 } else if (unlinked) {
1819 mutex_exit(&zp->z_lock);
1820 zfs_unlinked_add(zp, tx);
1821 }
1822
1823 txtype = TX_REMOVE;
1824 if (flags & FIGNORECASE)
1825 txtype |= TX_CI;
1826 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1827
1828 dmu_tx_commit(tx);
1829 out:
1830 if (realnmp)
1831 pn_free(realnmp);
1832
1833 zfs_dirent_unlock(dl);
1834
1835 if (!delete_now)
1836 VN_RELE(vp);
1837 if (xzp)
1838 VN_RELE(ZTOV(xzp));
1839
1840 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1841 zil_commit(zilog, 0);
1842
1843 ZFS_EXIT(zfsvfs);
1844 return (error);
1845 }
1846
1847 /*
1848 * Create a new directory and insert it into dvp using the name
1849 * provided. Return a pointer to the inserted directory.
1850 *
1851 * IN: dvp - vnode of directory to add subdir to.
1852 * dirname - name of new directory.
1853 * vap - attributes of new directory.
1854 * cr - credentials of caller.
1855 * ct - caller context
1856 * flags - case flags
1857 * vsecp - ACL to be set
1858 *
1859 * OUT: vpp - vnode of created directory.
1860 *
1861 * RETURN: 0 on success, error code on failure.
1862 *
1863 * Timestamps:
1864 * dvp - ctime|mtime updated
1865 * vp - ctime|mtime|atime updated
1866 */
1867 /*ARGSUSED*/
1868 static int
1869 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1870 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1871 {
1872 znode_t *zp, *dzp = VTOZ(dvp);
1873 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1874 zilog_t *zilog;
1875 zfs_dirlock_t *dl;
1876 uint64_t txtype;
1877 dmu_tx_t *tx;
1878 int error;
1879 int zf = ZNEW;
1880 ksid_t *ksid;
1881 uid_t uid;
1882 gid_t gid = crgetgid(cr);
1883 zfs_acl_ids_t acl_ids;
1884 boolean_t fuid_dirtied;
1885 boolean_t waited = B_FALSE;
1886
1887 ASSERT(vap->va_type == VDIR);
1888
1889 /*
1890 * If we have an ephemeral id, ACL, or XVATTR then
1891 * make sure file system is at proper version
1892 */
1893
1894 ksid = crgetsid(cr, KSID_OWNER);
1895 if (ksid)
1896 uid = ksid_getid(ksid);
1897 else
1898 uid = crgetuid(cr);
1899 if (zfsvfs->z_use_fuids == B_FALSE &&
1900 (vsecp || (vap->va_mask & AT_XVATTR) ||
1901 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1902 return (SET_ERROR(EINVAL));
1903
1904 ZFS_ENTER(zfsvfs);
1905 ZFS_VERIFY_ZP(dzp);
1906 zilog = zfsvfs->z_log;
1907
1908 if (dzp->z_pflags & ZFS_XATTR) {
1909 ZFS_EXIT(zfsvfs);
1910 return (SET_ERROR(EINVAL));
1911 }
1912
1913 if (zfsvfs->z_utf8 && u8_validate(dirname,
1914 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1915 ZFS_EXIT(zfsvfs);
1916 return (SET_ERROR(EILSEQ));
1917 }
1918 if (flags & FIGNORECASE)
1919 zf |= ZCILOOK;
1920
1921 if (vap->va_mask & AT_XVATTR) {
1922 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1923 crgetuid(cr), cr, vap->va_type)) != 0) {
1924 ZFS_EXIT(zfsvfs);
1925 return (error);
1926 }
1927 }
1928
1929 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1930 vsecp, &acl_ids)) != 0) {
1931 ZFS_EXIT(zfsvfs);
1932 return (error);
1933 }
1934 /*
1935 * First make sure the new directory doesn't exist.
1936 *
1937 * Existence is checked first to make sure we don't return
1938 * EACCES instead of EEXIST which can cause some applications
1939 * to fail.
1940 */
1941 top:
1942 *vpp = NULL;
1943
1944 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1945 NULL, NULL)) {
1946 zfs_acl_ids_free(&acl_ids);
1947 ZFS_EXIT(zfsvfs);
1948 return (error);
1949 }
1950
1951 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1952 zfs_acl_ids_free(&acl_ids);
1953 zfs_dirent_unlock(dl);
1954 ZFS_EXIT(zfsvfs);
1955 return (error);
1956 }
1957
1958 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1959 zfs_acl_ids_free(&acl_ids);
1960 zfs_dirent_unlock(dl);
1961 ZFS_EXIT(zfsvfs);
1962 return (SET_ERROR(EDQUOT));
1963 }
1964
1965 /*
1966 * Add a new entry to the directory.
1967 */
1968 tx = dmu_tx_create(zfsvfs->z_os);
1969 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1970 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1971 fuid_dirtied = zfsvfs->z_fuid_dirty;
1972 if (fuid_dirtied)
1973 zfs_fuid_txhold(zfsvfs, tx);
1974 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1975 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1976 acl_ids.z_aclp->z_acl_bytes);
1977 }
1978
1979 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1980 ZFS_SA_BASE_ATTR_SIZE);
1981
1982 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1983 if (error) {
1984 zfs_dirent_unlock(dl);
1985 if (error == ERESTART) {
1986 waited = B_TRUE;
1987 dmu_tx_wait(tx);
1988 dmu_tx_abort(tx);
1989 goto top;
1990 }
1991 zfs_acl_ids_free(&acl_ids);
1992 dmu_tx_abort(tx);
1993 ZFS_EXIT(zfsvfs);
1994 return (error);
1995 }
1996
1997 /*
1998 * Create new node.
1999 */
2000 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2001
2002 if (fuid_dirtied)
2003 zfs_fuid_sync(zfsvfs, tx);
2004
2005 /*
2006 * Now put new name in parent dir.
2007 */
2008 (void) zfs_link_create(dl, zp, tx, ZNEW);
2009
2010 *vpp = ZTOV(zp);
2011
2012 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2013 if (flags & FIGNORECASE)
2014 txtype |= TX_CI;
2015 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2016 acl_ids.z_fuidp, vap);
2017
2018 zfs_acl_ids_free(&acl_ids);
2019
2020 dmu_tx_commit(tx);
2021
2022 zfs_dirent_unlock(dl);
2023
2024 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2025 zil_commit(zilog, 0);
2026
2027 ZFS_EXIT(zfsvfs);
2028 return (0);
2029 }
2030
2031 /*
2032 * Remove a directory subdir entry. If the current working
2033 * directory is the same as the subdir to be removed, the
2034 * remove will fail.
2035 *
2036 * IN: dvp - vnode of directory to remove from.
2037 * name - name of directory to be removed.
2038 * cwd - vnode of current working directory.
2039 * cr - credentials of caller.
2040 * ct - caller context
2041 * flags - case flags
2042 *
2043 * RETURN: 0 on success, error code on failure.
2044 *
2045 * Timestamps:
2046 * dvp - ctime|mtime updated
2047 */
2048 /*ARGSUSED*/
2049 static int
2050 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2051 caller_context_t *ct, int flags)
2052 {
2053 znode_t *dzp = VTOZ(dvp);
2054 znode_t *zp;
2055 vnode_t *vp;
2056 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2057 zilog_t *zilog;
2058 zfs_dirlock_t *dl;
2059 dmu_tx_t *tx;
2060 int error;
2061 int zflg = ZEXISTS;
2062 boolean_t waited = B_FALSE;
2063
2064 ZFS_ENTER(zfsvfs);
2065 ZFS_VERIFY_ZP(dzp);
2066 zilog = zfsvfs->z_log;
2067
2068 if (flags & FIGNORECASE)
2069 zflg |= ZCILOOK;
2070 top:
2071 zp = NULL;
2072
2073 /*
2074 * Attempt to lock directory; fail if entry doesn't exist.
2075 */
2076 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2077 NULL, NULL)) {
2078 ZFS_EXIT(zfsvfs);
2079 return (error);
2080 }
2081
2082 vp = ZTOV(zp);
2083
2084 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2085 goto out;
2086 }
2087
2088 if (vp->v_type != VDIR) {
2089 error = SET_ERROR(ENOTDIR);
2090 goto out;
2091 }
2092
2093 if (vp == cwd) {
2094 error = SET_ERROR(EINVAL);
2095 goto out;
2096 }
2097
2098 vnevent_rmdir(vp, dvp, name, ct);
2099
2100 /*
2101 * Grab a lock on the directory to make sure that noone is
2102 * trying to add (or lookup) entries while we are removing it.
2103 */
2104 rw_enter(&zp->z_name_lock, RW_WRITER);
2105
2106 /*
2107 * Grab a lock on the parent pointer to make sure we play well
2108 * with the treewalk and directory rename code.
2109 */
2110 rw_enter(&zp->z_parent_lock, RW_WRITER);
2111
2112 tx = dmu_tx_create(zfsvfs->z_os);
2113 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2114 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2115 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2116 zfs_sa_upgrade_txholds(tx, zp);
2117 zfs_sa_upgrade_txholds(tx, dzp);
2118 dmu_tx_mark_netfree(tx);
2119 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2120 if (error) {
2121 rw_exit(&zp->z_parent_lock);
2122 rw_exit(&zp->z_name_lock);
2123 zfs_dirent_unlock(dl);
2124 VN_RELE(vp);
2125 if (error == ERESTART) {
2126 waited = B_TRUE;
2127 dmu_tx_wait(tx);
2128 dmu_tx_abort(tx);
2129 goto top;
2130 }
2131 dmu_tx_abort(tx);
2132 ZFS_EXIT(zfsvfs);
2133 return (error);
2134 }
2135
2136 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2137
2138 if (error == 0) {
2139 uint64_t txtype = TX_RMDIR;
2140 if (flags & FIGNORECASE)
2141 txtype |= TX_CI;
2142 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2143 }
2144
2145 dmu_tx_commit(tx);
2146
2147 rw_exit(&zp->z_parent_lock);
2148 rw_exit(&zp->z_name_lock);
2149 out:
2150 zfs_dirent_unlock(dl);
2151
2152 VN_RELE(vp);
2153
2154 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2155 zil_commit(zilog, 0);
2156
2157 ZFS_EXIT(zfsvfs);
2158 return (error);
2159 }
2160
2161 /*
2162 * Read as many directory entries as will fit into the provided
2163 * buffer from the given directory cursor position (specified in
2164 * the uio structure).
2165 *
2166 * IN: vp - vnode of directory to read.
2167 * uio - structure supplying read location, range info,
2168 * and return buffer.
2169 * cr - credentials of caller.
2170 * ct - caller context
2171 * flags - case flags
2172 *
2173 * OUT: uio - updated offset and range, buffer filled.
2174 * eofp - set to true if end-of-file detected.
2175 *
2176 * RETURN: 0 on success, error code on failure.
2177 *
2178 * Timestamps:
2179 * vp - atime updated
2180 *
2181 * Note that the low 4 bits of the cookie returned by zap is always zero.
2182 * This allows us to use the low range for "special" directory entries:
2183 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2184 * we use the offset 2 for the '.zfs' directory.
2185 */
2186 /* ARGSUSED */
2187 static int
2188 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2189 caller_context_t *ct, int flags)
2190 {
2191 znode_t *zp = VTOZ(vp);
2192 iovec_t *iovp;
2193 edirent_t *eodp;
2194 dirent64_t *odp;
2195 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2196 objset_t *os;
2197 caddr_t outbuf;
2198 size_t bufsize;
2199 zap_cursor_t zc;
2200 zap_attribute_t zap;
2201 uint_t bytes_wanted;
2202 uint64_t offset; /* must be unsigned; checks for < 1 */
2203 uint64_t parent;
2204 int local_eof;
2205 int outcount;
2206 int error;
2207 uint8_t prefetch;
2208 boolean_t check_sysattrs;
2209
2210 ZFS_ENTER(zfsvfs);
2211 ZFS_VERIFY_ZP(zp);
2212
2213 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2214 &parent, sizeof (parent))) != 0) {
2215 ZFS_EXIT(zfsvfs);
2216 return (error);
2217 }
2218
2219 /*
2220 * If we are not given an eof variable,
2221 * use a local one.
2222 */
2223 if (eofp == NULL)
2224 eofp = &local_eof;
2225
2226 /*
2227 * Check for valid iov_len.
2228 */
2229 if (uio->uio_iov->iov_len <= 0) {
2230 ZFS_EXIT(zfsvfs);
2231 return (SET_ERROR(EINVAL));
2232 }
2233
2234 /*
2235 * Quit if directory has been removed (posix)
2236 */
2237 if ((*eofp = zp->z_unlinked) != 0) {
2238 ZFS_EXIT(zfsvfs);
2239 return (0);
2240 }
2241
2242 error = 0;
2243 os = zfsvfs->z_os;
2244 offset = uio->uio_loffset;
2245 prefetch = zp->z_zn_prefetch;
2246
2247 /*
2248 * Initialize the iterator cursor.
2249 */
2250 if (offset <= 3) {
2251 /*
2252 * Start iteration from the beginning of the directory.
2253 */
2254 zap_cursor_init(&zc, os, zp->z_id);
2255 } else {
2256 /*
2257 * The offset is a serialized cursor.
2258 */
2259 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2260 }
2261
2262 /*
2263 * Get space to change directory entries into fs independent format.
2264 */
2265 iovp = uio->uio_iov;
2266 bytes_wanted = iovp->iov_len;
2267 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2268 bufsize = bytes_wanted;
2269 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2270 odp = (struct dirent64 *)outbuf;
2271 } else {
2272 bufsize = bytes_wanted;
2273 outbuf = NULL;
2274 odp = (struct dirent64 *)iovp->iov_base;
2275 }
2276 eodp = (struct edirent *)odp;
2277
2278 /*
2279 * If this VFS supports the system attribute view interface; and
2280 * we're looking at an extended attribute directory; and we care
2281 * about normalization conflicts on this vfs; then we must check
2282 * for normalization conflicts with the sysattr name space.
2283 */
2284 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2285 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2286 (flags & V_RDDIR_ENTFLAGS);
2287
2288 /*
2289 * Transform to file-system independent format
2290 */
2291 outcount = 0;
2292 while (outcount < bytes_wanted) {
2293 ino64_t objnum;
2294 ushort_t reclen;
2295 off64_t *next = NULL;
2296
2297 /*
2298 * Special case `.', `..', and `.zfs'.
2299 */
2300 if (offset == 0) {
2301 (void) strcpy(zap.za_name, ".");
2302 zap.za_normalization_conflict = 0;
2303 objnum = zp->z_id;
2304 } else if (offset == 1) {
2305 (void) strcpy(zap.za_name, "..");
2306 zap.za_normalization_conflict = 0;
2307 objnum = parent;
2308 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2309 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2310 zap.za_normalization_conflict = 0;
2311 objnum = ZFSCTL_INO_ROOT;
2312 } else {
2313 /*
2314 * Grab next entry.
2315 */
2316 if (error = zap_cursor_retrieve(&zc, &zap)) {
2317 if ((*eofp = (error == ENOENT)) != 0)
2318 break;
2319 else
2320 goto update;
2321 }
2322
2323 if (zap.za_integer_length != 8 ||
2324 zap.za_num_integers != 1) {
2325 cmn_err(CE_WARN, "zap_readdir: bad directory "
2326 "entry, obj = %lld, offset = %lld\n",
2327 (u_longlong_t)zp->z_id,
2328 (u_longlong_t)offset);
2329 error = SET_ERROR(ENXIO);
2330 goto update;
2331 }
2332
2333 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2334 /*
2335 * MacOS X can extract the object type here such as:
2336 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2337 */
2338
2339 if (check_sysattrs && !zap.za_normalization_conflict) {
2340 zap.za_normalization_conflict =
2341 xattr_sysattr_casechk(zap.za_name);
2342 }
2343 }
2344
2345 if (flags & V_RDDIR_ACCFILTER) {
2346 /*
2347 * If we have no access at all, don't include
2348 * this entry in the returned information
2349 */
2350 znode_t *ezp;
2351 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2352 goto skip_entry;
2353 if (!zfs_has_access(ezp, cr)) {
2354 VN_RELE(ZTOV(ezp));
2355 goto skip_entry;
2356 }
2357 VN_RELE(ZTOV(ezp));
2358 }
2359
2360 if (flags & V_RDDIR_ENTFLAGS)
2361 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2362 else
2363 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2364
2365 /*
2366 * Will this entry fit in the buffer?
2367 */
2368 if (outcount + reclen > bufsize) {
2369 /*
2370 * Did we manage to fit anything in the buffer?
2371 */
2372 if (!outcount) {
2373 error = SET_ERROR(EINVAL);
2374 goto update;
2375 }
2376 break;
2377 }
2378 if (flags & V_RDDIR_ENTFLAGS) {
2379 /*
2380 * Add extended flag entry:
2381 */
2382 eodp->ed_ino = objnum;
2383 eodp->ed_reclen = reclen;
2384 /* NOTE: ed_off is the offset for the *next* entry */
2385 next = &(eodp->ed_off);
2386 eodp->ed_eflags = zap.za_normalization_conflict ?
2387 ED_CASE_CONFLICT : 0;
2388 (void) strncpy(eodp->ed_name, zap.za_name,
2389 EDIRENT_NAMELEN(reclen));
2390 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2391 } else {
2392 /*
2393 * Add normal entry:
2394 */
2395 odp->d_ino = objnum;
2396 odp->d_reclen = reclen;
2397 /* NOTE: d_off is the offset for the *next* entry */
2398 next = &(odp->d_off);
2399 (void) strncpy(odp->d_name, zap.za_name,
2400 DIRENT64_NAMELEN(reclen));
2401 odp = (dirent64_t *)((intptr_t)odp + reclen);
2402 }
2403 outcount += reclen;
2404
2405 ASSERT(outcount <= bufsize);
2406
2407 /* Prefetch znode */
2408 if (prefetch)
2409 dmu_prefetch(os, objnum, 0, 0, 0,
2410 ZIO_PRIORITY_SYNC_READ);
2411
2412 skip_entry:
2413 /*
2414 * Move to the next entry, fill in the previous offset.
2415 */
2416 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2417 zap_cursor_advance(&zc);
2418 offset = zap_cursor_serialize(&zc);
2419 } else {
2420 offset += 1;
2421 }
2422 if (next)
2423 *next = offset;
2424 }
2425 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2426
2427 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2428 iovp->iov_base += outcount;
2429 iovp->iov_len -= outcount;
2430 uio->uio_resid -= outcount;
2431 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2432 /*
2433 * Reset the pointer.
2434 */
2435 offset = uio->uio_loffset;
2436 }
2437
2438 update:
2439 zap_cursor_fini(&zc);
2440 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2441 kmem_free(outbuf, bufsize);
2442
2443 if (error == ENOENT)
2444 error = 0;
2445
2446 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2447
2448 uio->uio_loffset = offset;
2449 ZFS_EXIT(zfsvfs);
2450 return (error);
2451 }
2452
2453 ulong_t zfs_fsync_sync_cnt = 4;
2454
2455 static int
2456 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2457 {
2458 znode_t *zp = VTOZ(vp);
2459 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2460
2461 /*
2462 * Regardless of whether this is required for standards conformance,
2463 * this is the logical behavior when fsync() is called on a file with
2464 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2465 * going to be pushed out as part of the zil_commit().
2466 */
2467 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2468 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2469 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2470
2471 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2472
2473 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2474 ZFS_ENTER(zfsvfs);
2475 ZFS_VERIFY_ZP(zp);
2476 zil_commit(zfsvfs->z_log, zp->z_id);
2477 ZFS_EXIT(zfsvfs);
2478 }
2479 return (0);
2480 }
2481
2482
2483 /*
2484 * Get the requested file attributes and place them in the provided
2485 * vattr structure.
2486 *
2487 * IN: vp - vnode of file.
2488 * vap - va_mask identifies requested attributes.
2489 * If AT_XVATTR set, then optional attrs are requested
2490 * flags - ATTR_NOACLCHECK (CIFS server context)
2491 * cr - credentials of caller.
2492 * ct - caller context
2493 *
2494 * OUT: vap - attribute values.
2495 *
2496 * RETURN: 0 (always succeeds).
2497 */
2498 /* ARGSUSED */
2499 static int
2500 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2501 caller_context_t *ct)
2502 {
2503 znode_t *zp = VTOZ(vp);
2504 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2505 int error = 0;
2506 uint64_t links;
2507 uint64_t mtime[2], ctime[2];
2508 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2509 xoptattr_t *xoap = NULL;
2510 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2511 sa_bulk_attr_t bulk[2];
2512 int count = 0;
2513
2514 ZFS_ENTER(zfsvfs);
2515 ZFS_VERIFY_ZP(zp);
2516
2517 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2518
2519 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2520 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2521
2522 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2523 ZFS_EXIT(zfsvfs);
2524 return (error);
2525 }
2526
2527 /*
2528 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2529 * Also, if we are the owner don't bother, since owner should
2530 * always be allowed to read basic attributes of file.
2531 */
2532 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2533 (vap->va_uid != crgetuid(cr))) {
2534 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2535 skipaclchk, cr)) {
2536 ZFS_EXIT(zfsvfs);
2537 return (error);
2538 }
2539 }
2540
2541 /*
2542 * Return all attributes. It's cheaper to provide the answer
2543 * than to determine whether we were asked the question.
2544 */
2545
2546 mutex_enter(&zp->z_lock);
2547 vap->va_type = vp->v_type;
2548 vap->va_mode = zp->z_mode & MODEMASK;
2549 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2550 vap->va_nodeid = zp->z_id;
2551 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2552 links = zp->z_links + 1;
2553 else
2554 links = zp->z_links;
2555 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2556 vap->va_size = zp->z_size;
2557 vap->va_rdev = vp->v_rdev;
2558 vap->va_seq = zp->z_seq;
2559
2560 /*
2561 * Add in any requested optional attributes and the create time.
2562 * Also set the corresponding bits in the returned attribute bitmap.
2563 */
2564 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2565 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2566 xoap->xoa_archive =
2567 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2568 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2569 }
2570
2571 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2572 xoap->xoa_readonly =
2573 ((zp->z_pflags & ZFS_READONLY) != 0);
2574 XVA_SET_RTN(xvap, XAT_READONLY);
2575 }
2576
2577 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2578 xoap->xoa_system =
2579 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2580 XVA_SET_RTN(xvap, XAT_SYSTEM);
2581 }
2582
2583 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2584 xoap->xoa_hidden =
2585 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2586 XVA_SET_RTN(xvap, XAT_HIDDEN);
2587 }
2588
2589 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2590 xoap->xoa_nounlink =
2591 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2592 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2593 }
2594
2595 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2596 xoap->xoa_immutable =
2597 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2598 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2599 }
2600
2601 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2602 xoap->xoa_appendonly =
2603 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2604 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2605 }
2606
2607 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2608 xoap->xoa_nodump =
2609 ((zp->z_pflags & ZFS_NODUMP) != 0);
2610 XVA_SET_RTN(xvap, XAT_NODUMP);
2611 }
2612
2613 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2614 xoap->xoa_opaque =
2615 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2616 XVA_SET_RTN(xvap, XAT_OPAQUE);
2617 }
2618
2619 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2620 xoap->xoa_av_quarantined =
2621 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2622 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2623 }
2624
2625 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2626 xoap->xoa_av_modified =
2627 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2628 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2629 }
2630
2631 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2632 vp->v_type == VREG) {
2633 zfs_sa_get_scanstamp(zp, xvap);
2634 }
2635
2636 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2637 uint64_t times[2];
2638
2639 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2640 times, sizeof (times));
2641 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2642 XVA_SET_RTN(xvap, XAT_CREATETIME);
2643 }
2644
2645 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2646 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2647 XVA_SET_RTN(xvap, XAT_REPARSE);
2648 }
2649 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2650 xoap->xoa_generation = zp->z_gen;
2651 XVA_SET_RTN(xvap, XAT_GEN);
2652 }
2653
2654 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2655 xoap->xoa_offline =
2656 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2657 XVA_SET_RTN(xvap, XAT_OFFLINE);
2658 }
2659
2660 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2661 xoap->xoa_sparse =
2662 ((zp->z_pflags & ZFS_SPARSE) != 0);
2663 XVA_SET_RTN(xvap, XAT_SPARSE);
2664 }
2665 }
2666
2667 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2668 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2669 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2670
2671 mutex_exit(&zp->z_lock);
2672
2673 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2674
2675 if (zp->z_blksz == 0) {
2676 /*
2677 * Block size hasn't been set; suggest maximal I/O transfers.
2678 */
2679 vap->va_blksize = zfsvfs->z_max_blksz;
2680 }
2681
2682 ZFS_EXIT(zfsvfs);
2683 return (0);
2684 }
2685
2686 /*
2687 * Set the file attributes to the values contained in the
2688 * vattr structure.
2689 *
2690 * IN: vp - vnode of file to be modified.
2691 * vap - new attribute values.
2692 * If AT_XVATTR set, then optional attrs are being set
2693 * flags - ATTR_UTIME set if non-default time values provided.
2694 * - ATTR_NOACLCHECK (CIFS context only).
2695 * cr - credentials of caller.
2696 * ct - caller context
2697 *
2698 * RETURN: 0 on success, error code on failure.
2699 *
2700 * Timestamps:
2701 * vp - ctime updated, mtime updated if size changed.
2702 */
2703 /* ARGSUSED */
2704 static int
2705 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2706 caller_context_t *ct)
2707 {
2708 znode_t *zp = VTOZ(vp);
2709 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2710 zilog_t *zilog;
2711 dmu_tx_t *tx;
2712 vattr_t oldva;
2713 xvattr_t tmpxvattr;
2714 uint_t mask = vap->va_mask;
2715 uint_t saved_mask = 0;
2716 int trim_mask = 0;
2717 uint64_t new_mode;
2718 uint64_t new_uid, new_gid;
2719 uint64_t xattr_obj;
2720 uint64_t mtime[2], ctime[2];
2721 znode_t *attrzp;
2722 int need_policy = FALSE;
2723 int err, err2;
2724 zfs_fuid_info_t *fuidp = NULL;
2725 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2726 xoptattr_t *xoap;
2727 zfs_acl_t *aclp;
2728 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2729 boolean_t fuid_dirtied = B_FALSE;
2730 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2731 int count = 0, xattr_count = 0;
2732
2733 if (mask == 0)
2734 return (0);
2735
2736 if (mask & AT_NOSET)
2737 return (SET_ERROR(EINVAL));
2738
2739 ZFS_ENTER(zfsvfs);
2740 ZFS_VERIFY_ZP(zp);
2741
2742 zilog = zfsvfs->z_log;
2743
2744 /*
2745 * Make sure that if we have ephemeral uid/gid or xvattr specified
2746 * that file system is at proper version level
2747 */
2748
2749 if (zfsvfs->z_use_fuids == B_FALSE &&
2750 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2751 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2752 (mask & AT_XVATTR))) {
2753 ZFS_EXIT(zfsvfs);
2754 return (SET_ERROR(EINVAL));
2755 }
2756
2757 if (mask & AT_SIZE && vp->v_type == VDIR) {
2758 ZFS_EXIT(zfsvfs);
2759 return (SET_ERROR(EISDIR));
2760 }
2761
2762 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2763 ZFS_EXIT(zfsvfs);
2764 return (SET_ERROR(EINVAL));
2765 }
2766
2767 /*
2768 * If this is an xvattr_t, then get a pointer to the structure of
2769 * optional attributes. If this is NULL, then we have a vattr_t.
2770 */
2771 xoap = xva_getxoptattr(xvap);
2772
2773 xva_init(&tmpxvattr);
2774
2775 /*
2776 * Immutable files can only alter immutable bit and atime
2777 */
2778 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2779 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2780 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2781 ZFS_EXIT(zfsvfs);
2782 return (SET_ERROR(EPERM));
2783 }
2784
2785 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2786 ZFS_EXIT(zfsvfs);
2787 return (SET_ERROR(EPERM));
2788 }
2789
2790 /*
2791 * Verify timestamps doesn't overflow 32 bits.
2792 * ZFS can handle large timestamps, but 32bit syscalls can't
2793 * handle times greater than 2039. This check should be removed
2794 * once large timestamps are fully supported.
2795 */
2796 if (mask & (AT_ATIME | AT_MTIME)) {
2797 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2798 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2799 ZFS_EXIT(zfsvfs);
2800 return (SET_ERROR(EOVERFLOW));
2801 }
2802 }
2803
2804 top:
2805 attrzp = NULL;
2806 aclp = NULL;
2807
2808 /* Can this be moved to before the top label? */
2809 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2810 ZFS_EXIT(zfsvfs);
2811 return (SET_ERROR(EROFS));
2812 }
2813
2814 /*
2815 * First validate permissions
2816 */
2817
2818 if (mask & AT_SIZE) {
2819 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2820 if (err) {
2821 ZFS_EXIT(zfsvfs);
2822 return (err);
2823 }
2824 /*
2825 * XXX - Note, we are not providing any open
2826 * mode flags here (like FNDELAY), so we may
2827 * block if there are locks present... this
2828 * should be addressed in openat().
2829 */
2830 /* XXX - would it be OK to generate a log record here? */
2831 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2832 if (err) {
2833 ZFS_EXIT(zfsvfs);
2834 return (err);
2835 }
2836
2837 if (vap->va_size == 0) {
2838 vnevent_truncate(ZTOV(zp), ct);
2839 } else {
2840 vnevent_resize(ZTOV(zp), ct);
2841 }
2842 }
2843
2844 if (mask & (AT_ATIME|AT_MTIME) ||
2845 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2846 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2847 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2848 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2849 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2850 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2851 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2852 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2853 skipaclchk, cr);
2854 }
2855
2856 if (mask & (AT_UID|AT_GID)) {
2857 int idmask = (mask & (AT_UID|AT_GID));
2858 int take_owner;
2859 int take_group;
2860
2861 /*
2862 * NOTE: even if a new mode is being set,
2863 * we may clear S_ISUID/S_ISGID bits.
2864 */
2865
2866 if (!(mask & AT_MODE))
2867 vap->va_mode = zp->z_mode;
2868
2869 /*
2870 * Take ownership or chgrp to group we are a member of
2871 */
2872
2873 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2874 take_group = (mask & AT_GID) &&
2875 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2876
2877 /*
2878 * If both AT_UID and AT_GID are set then take_owner and
2879 * take_group must both be set in order to allow taking
2880 * ownership.
2881 *
2882 * Otherwise, send the check through secpolicy_vnode_setattr()
2883 *
2884 */
2885
2886 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2887 ((idmask == AT_UID) && take_owner) ||
2888 ((idmask == AT_GID) && take_group)) {
2889 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2890 skipaclchk, cr) == 0) {
2891 /*
2892 * Remove setuid/setgid for non-privileged users
2893 */
2894 secpolicy_setid_clear(vap, cr);
2895 trim_mask = (mask & (AT_UID|AT_GID));
2896 } else {
2897 need_policy = TRUE;
2898 }
2899 } else {
2900 need_policy = TRUE;
2901 }
2902 }
2903
2904 mutex_enter(&zp->z_lock);
2905 oldva.va_mode = zp->z_mode;
2906 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2907 if (mask & AT_XVATTR) {
2908 /*
2909 * Update xvattr mask to include only those attributes
2910 * that are actually changing.
2911 *
2912 * the bits will be restored prior to actually setting
2913 * the attributes so the caller thinks they were set.
2914 */
2915 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2916 if (xoap->xoa_appendonly !=
2917 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2918 need_policy = TRUE;
2919 } else {
2920 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2921 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2922 }
2923 }
2924
2925 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2926 if (xoap->xoa_nounlink !=
2927 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2928 need_policy = TRUE;
2929 } else {
2930 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2931 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2932 }
2933 }
2934
2935 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2936 if (xoap->xoa_immutable !=
2937 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2938 need_policy = TRUE;
2939 } else {
2940 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2941 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2942 }
2943 }
2944
2945 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2946 if (xoap->xoa_nodump !=
2947 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2948 need_policy = TRUE;
2949 } else {
2950 XVA_CLR_REQ(xvap, XAT_NODUMP);
2951 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2952 }
2953 }
2954
2955 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2956 if (xoap->xoa_av_modified !=
2957 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2958 need_policy = TRUE;
2959 } else {
2960 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2961 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2962 }
2963 }
2964
2965 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2966 if ((vp->v_type != VREG &&
2967 xoap->xoa_av_quarantined) ||
2968 xoap->xoa_av_quarantined !=
2969 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2970 need_policy = TRUE;
2971 } else {
2972 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2973 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2974 }
2975 }
2976
2977 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2978 mutex_exit(&zp->z_lock);
2979 ZFS_EXIT(zfsvfs);
2980 return (SET_ERROR(EPERM));
2981 }
2982
2983 if (need_policy == FALSE &&
2984 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2985 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2986 need_policy = TRUE;
2987 }
2988 }
2989
2990 mutex_exit(&zp->z_lock);
2991
2992 if (mask & AT_MODE) {
2993 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2994 err = secpolicy_setid_setsticky_clear(vp, vap,
2995 &oldva, cr);
2996 if (err) {
2997 ZFS_EXIT(zfsvfs);
2998 return (err);
2999 }
3000 trim_mask |= AT_MODE;
3001 } else {
3002 need_policy = TRUE;
3003 }
3004 }
3005
3006 if (need_policy) {
3007 /*
3008 * If trim_mask is set then take ownership
3009 * has been granted or write_acl is present and user
3010 * has the ability to modify mode. In that case remove
3011 * UID|GID and or MODE from mask so that
3012 * secpolicy_vnode_setattr() doesn't revoke it.
3013 */
3014
3015 if (trim_mask) {
3016 saved_mask = vap->va_mask;
3017 vap->va_mask &= ~trim_mask;
3018 }
3019 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3020 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3021 if (err) {
3022 ZFS_EXIT(zfsvfs);
3023 return (err);
3024 }
3025
3026 if (trim_mask)
3027 vap->va_mask |= saved_mask;
3028 }
3029
3030 /*
3031 * secpolicy_vnode_setattr, or take ownership may have
3032 * changed va_mask
3033 */
3034 mask = vap->va_mask;
3035
3036 if ((mask & (AT_UID | AT_GID))) {
3037 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3038 &xattr_obj, sizeof (xattr_obj));
3039
3040 if (err == 0 && xattr_obj) {
3041 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3042 if (err)
3043 goto out2;
3044 }
3045 if (mask & AT_UID) {
3046 new_uid = zfs_fuid_create(zfsvfs,
3047 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3048 if (new_uid != zp->z_uid &&
3049 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3050 if (attrzp)
3051 VN_RELE(ZTOV(attrzp));
3052 err = SET_ERROR(EDQUOT);
3053 goto out2;
3054 }
3055 }
3056
3057 if (mask & AT_GID) {
3058 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3059 cr, ZFS_GROUP, &fuidp);
3060 if (new_gid != zp->z_gid &&
3061 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3062 if (attrzp)
3063 VN_RELE(ZTOV(attrzp));
3064 err = SET_ERROR(EDQUOT);
3065 goto out2;
3066 }
3067 }
3068 }
3069 tx = dmu_tx_create(zfsvfs->z_os);
3070
3071 if (mask & AT_MODE) {
3072 uint64_t pmode = zp->z_mode;
3073 uint64_t acl_obj;
3074 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3075
3076 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3077 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3078 err = SET_ERROR(EPERM);
3079 goto out;
3080 }
3081
3082 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3083 goto out;
3084
3085 mutex_enter(&zp->z_lock);
3086 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3087 /*
3088 * Are we upgrading ACL from old V0 format
3089 * to V1 format?
3090 */
3091 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3092 zfs_znode_acl_version(zp) ==
3093 ZFS_ACL_VERSION_INITIAL) {
3094 dmu_tx_hold_free(tx, acl_obj, 0,
3095 DMU_OBJECT_END);
3096 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3097 0, aclp->z_acl_bytes);
3098 } else {
3099 dmu_tx_hold_write(tx, acl_obj, 0,
3100 aclp->z_acl_bytes);
3101 }
3102 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3103 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3104 0, aclp->z_acl_bytes);
3105 }
3106 mutex_exit(&zp->z_lock);
3107 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3108 } else {
3109 if ((mask & AT_XVATTR) &&
3110 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3111 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3112 else
3113 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3114 }
3115
3116 if (attrzp) {
3117 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3118 }
3119
3120 fuid_dirtied = zfsvfs->z_fuid_dirty;
3121 if (fuid_dirtied)
3122 zfs_fuid_txhold(zfsvfs, tx);
3123
3124 zfs_sa_upgrade_txholds(tx, zp);
3125
3126 err = dmu_tx_assign(tx, TXG_WAIT);
3127 if (err)
3128 goto out;
3129
3130 count = 0;
3131 /*
3132 * Set each attribute requested.
3133 * We group settings according to the locks they need to acquire.
3134 *
3135 * Note: you cannot set ctime directly, although it will be
3136 * updated as a side-effect of calling this function.
3137 */
3138
3139
3140 if (mask & (AT_UID|AT_GID|AT_MODE))
3141 mutex_enter(&zp->z_acl_lock);
3142 mutex_enter(&zp->z_lock);
3143
3144 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3145 &zp->z_pflags, sizeof (zp->z_pflags));
3146
3147 if (attrzp) {
3148 if (mask & (AT_UID|AT_GID|AT_MODE))
3149 mutex_enter(&attrzp->z_acl_lock);
3150 mutex_enter(&attrzp->z_lock);
3151 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3152 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3153 sizeof (attrzp->z_pflags));
3154 }
3155
3156 if (mask & (AT_UID|AT_GID)) {
3157
3158 if (mask & AT_UID) {
3159 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3160 &new_uid, sizeof (new_uid));
3161 zp->z_uid = new_uid;
3162 if (attrzp) {
3163 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3164 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3165 sizeof (new_uid));
3166 attrzp->z_uid = new_uid;
3167 }
3168 }
3169
3170 if (mask & AT_GID) {
3171 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3172 NULL, &new_gid, sizeof (new_gid));
3173 zp->z_gid = new_gid;
3174 if (attrzp) {
3175 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3176 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3177 sizeof (new_gid));
3178 attrzp->z_gid = new_gid;
3179 }
3180 }
3181 if (!(mask & AT_MODE)) {
3182 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3183 NULL, &new_mode, sizeof (new_mode));
3184 new_mode = zp->z_mode;
3185 }
3186 err = zfs_acl_chown_setattr(zp);
3187 ASSERT(err == 0);
3188 if (attrzp) {
3189 err = zfs_acl_chown_setattr(attrzp);
3190 ASSERT(err == 0);
3191 }
3192 }
3193
3194 if (mask & AT_MODE) {
3195 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3196 &new_mode, sizeof (new_mode));
3197 zp->z_mode = new_mode;
3198 ASSERT3U((uintptr_t)aclp, !=, NULL);
3199 err = zfs_aclset_common(zp, aclp, cr, tx);
3200 ASSERT0(err);
3201 if (zp->z_acl_cached)
3202 zfs_acl_free(zp->z_acl_cached);
3203 zp->z_acl_cached = aclp;
3204 aclp = NULL;
3205 }
3206
3207
3208 if (mask & AT_ATIME) {
3209 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3210 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3211 &zp->z_atime, sizeof (zp->z_atime));
3212 }
3213
3214 if (mask & AT_MTIME) {
3215 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3216 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3217 mtime, sizeof (mtime));
3218 }
3219
3220 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3221 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3222 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3223 NULL, mtime, sizeof (mtime));
3224 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3225 &ctime, sizeof (ctime));
3226 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3227 B_TRUE);
3228 } else if (mask != 0) {
3229 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3230 &ctime, sizeof (ctime));
3231 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3232 B_TRUE);
3233 if (attrzp) {
3234 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3235 SA_ZPL_CTIME(zfsvfs), NULL,
3236 &ctime, sizeof (ctime));
3237 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3238 mtime, ctime, B_TRUE);
3239 }
3240 }
3241 /*
3242 * Do this after setting timestamps to prevent timestamp
3243 * update from toggling bit
3244 */
3245
3246 if (xoap && (mask & AT_XVATTR)) {
3247
3248 /*
3249 * restore trimmed off masks
3250 * so that return masks can be set for caller.
3251 */
3252
3253 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3254 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3255 }
3256 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3257 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3258 }
3259 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3260 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3261 }
3262 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3263 XVA_SET_REQ(xvap, XAT_NODUMP);
3264 }
3265 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3266 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3267 }
3268 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3269 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3270 }
3271
3272 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3273 ASSERT(vp->v_type == VREG);
3274
3275 zfs_xvattr_set(zp, xvap, tx);
3276 }
3277
3278 if (fuid_dirtied)
3279 zfs_fuid_sync(zfsvfs, tx);
3280
3281 if (mask != 0)
3282 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3283
3284 mutex_exit(&zp->z_lock);
3285 if (mask & (AT_UID|AT_GID|AT_MODE))
3286 mutex_exit(&zp->z_acl_lock);
3287
3288 if (attrzp) {
3289 if (mask & (AT_UID|AT_GID|AT_MODE))
3290 mutex_exit(&attrzp->z_acl_lock);
3291 mutex_exit(&attrzp->z_lock);
3292 }
3293 out:
3294 if (err == 0 && attrzp) {
3295 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3296 xattr_count, tx);
3297 ASSERT(err2 == 0);
3298 }
3299
3300 if (attrzp)
3301 VN_RELE(ZTOV(attrzp));
3302
3303 if (aclp)
3304 zfs_acl_free(aclp);
3305
3306 if (fuidp) {
3307 zfs_fuid_info_free(fuidp);
3308 fuidp = NULL;
3309 }
3310
3311 if (err) {
3312 dmu_tx_abort(tx);
3313 if (err == ERESTART)
3314 goto top;
3315 } else {
3316 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3317 dmu_tx_commit(tx);
3318 }
3319
3320 out2:
3321 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3322 zil_commit(zilog, 0);
3323
3324 ZFS_EXIT(zfsvfs);
3325 return (err);
3326 }
3327
3328 typedef struct zfs_zlock {
3329 krwlock_t *zl_rwlock; /* lock we acquired */
3330 znode_t *zl_znode; /* znode we held */
3331 struct zfs_zlock *zl_next; /* next in list */
3332 } zfs_zlock_t;
3333
3334 /*
3335 * Drop locks and release vnodes that were held by zfs_rename_lock().
3336 */
3337 static void
3338 zfs_rename_unlock(zfs_zlock_t **zlpp)
3339 {
3340 zfs_zlock_t *zl;
3341
3342 while ((zl = *zlpp) != NULL) {
3343 if (zl->zl_znode != NULL)
3344 VN_RELE(ZTOV(zl->zl_znode));
3345 rw_exit(zl->zl_rwlock);
3346 *zlpp = zl->zl_next;
3347 kmem_free(zl, sizeof (*zl));
3348 }
3349 }
3350
3351 /*
3352 * Search back through the directory tree, using the ".." entries.
3353 * Lock each directory in the chain to prevent concurrent renames.
3354 * Fail any attempt to move a directory into one of its own descendants.
3355 * XXX - z_parent_lock can overlap with map or grow locks
3356 */
3357 static int
3358 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3359 {
3360 zfs_zlock_t *zl;
3361 znode_t *zp = tdzp;
3362 uint64_t rootid = zp->z_zfsvfs->z_root;
3363 uint64_t oidp = zp->z_id;
3364 krwlock_t *rwlp = &szp->z_parent_lock;
3365 krw_t rw = RW_WRITER;
3366
3367 /*
3368 * First pass write-locks szp and compares to zp->z_id.
3369 * Later passes read-lock zp and compare to zp->z_parent.
3370 */
3371 do {
3372 if (!rw_tryenter(rwlp, rw)) {
3373 /*
3374 * Another thread is renaming in this path.
3375 * Note that if we are a WRITER, we don't have any
3376 * parent_locks held yet.
3377 */
3378 if (rw == RW_READER && zp->z_id > szp->z_id) {
3379 /*
3380 * Drop our locks and restart
3381 */
3382 zfs_rename_unlock(&zl);
3383 *zlpp = NULL;
3384 zp = tdzp;
3385 oidp = zp->z_id;
3386 rwlp = &szp->z_parent_lock;
3387 rw = RW_WRITER;
3388 continue;
3389 } else {
3390 /*
3391 * Wait for other thread to drop its locks
3392 */
3393 rw_enter(rwlp, rw);
3394 }
3395 }
3396
3397 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3398 zl->zl_rwlock = rwlp;
3399 zl->zl_znode = NULL;
3400 zl->zl_next = *zlpp;
3401 *zlpp = zl;
3402
3403 if (oidp == szp->z_id) /* We're a descendant of szp */
3404 return (SET_ERROR(EINVAL));
3405
3406 if (oidp == rootid) /* We've hit the top */
3407 return (0);
3408
3409 if (rw == RW_READER) { /* i.e. not the first pass */
3410 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3411 if (error)
3412 return (error);
3413 zl->zl_znode = zp;
3414 }
3415 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3416 &oidp, sizeof (oidp));
3417 rwlp = &zp->z_parent_lock;
3418 rw = RW_READER;
3419
3420 } while (zp->z_id != sdzp->z_id);
3421
3422 return (0);
3423 }
3424
3425 /*
3426 * Move an entry from the provided source directory to the target
3427 * directory. Change the entry name as indicated.
3428 *
3429 * IN: sdvp - Source directory containing the "old entry".
3430 * snm - Old entry name.
3431 * tdvp - Target directory to contain the "new entry".
3432 * tnm - New entry name.
3433 * cr - credentials of caller.
3434 * ct - caller context
3435 * flags - case flags
3436 *
3437 * RETURN: 0 on success, error code on failure.
3438 *
3439 * Timestamps:
3440 * sdvp,tdvp - ctime|mtime updated
3441 */
3442 /*ARGSUSED*/
3443 static int
3444 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3445 caller_context_t *ct, int flags)
3446 {
3447 znode_t *tdzp, *szp, *tzp;
3448 znode_t *sdzp = VTOZ(sdvp);
3449 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3450 zilog_t *zilog;
3451 vnode_t *realvp;
3452 zfs_dirlock_t *sdl, *tdl;
3453 dmu_tx_t *tx;
3454 zfs_zlock_t *zl;
3455 int cmp, serr, terr;
3456 int error = 0, rm_err = 0;
3457 int zflg = 0;
3458 boolean_t waited = B_FALSE;
3459
3460 ZFS_ENTER(zfsvfs);
3461 ZFS_VERIFY_ZP(sdzp);
3462 zilog = zfsvfs->z_log;
3463
3464 /*
3465 * Make sure we have the real vp for the target directory.
3466 */
3467 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3468 tdvp = realvp;
3469
3470 tdzp = VTOZ(tdvp);
3471 ZFS_VERIFY_ZP(tdzp);
3472
3473 /*
3474 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3475 * ctldir appear to have the same v_vfsp.
3476 */
3477 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3478 ZFS_EXIT(zfsvfs);
3479 return (SET_ERROR(EXDEV));
3480 }
3481
3482 if (zfsvfs->z_utf8 && u8_validate(tnm,
3483 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3484 ZFS_EXIT(zfsvfs);
3485 return (SET_ERROR(EILSEQ));
3486 }
3487
3488 if (flags & FIGNORECASE)
3489 zflg |= ZCILOOK;
3490
3491 top:
3492 szp = NULL;
3493 tzp = NULL;
3494 zl = NULL;
3495
3496 /*
3497 * This is to prevent the creation of links into attribute space
3498 * by renaming a linked file into/outof an attribute directory.
3499 * See the comment in zfs_link() for why this is considered bad.
3500 */
3501 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3502 ZFS_EXIT(zfsvfs);
3503 return (SET_ERROR(EINVAL));
3504 }
3505
3506 /*
3507 * Lock source and target directory entries. To prevent deadlock,
3508 * a lock ordering must be defined. We lock the directory with
3509 * the smallest object id first, or if it's a tie, the one with
3510 * the lexically first name.
3511 */
3512 if (sdzp->z_id < tdzp->z_id) {
3513 cmp = -1;
3514 } else if (sdzp->z_id > tdzp->z_id) {
3515 cmp = 1;
3516 } else {
3517 /*
3518 * First compare the two name arguments without
3519 * considering any case folding.
3520 */
3521 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3522
3523 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3524 ASSERT(error == 0 || !zfsvfs->z_utf8);
3525 if (cmp == 0) {
3526 /*
3527 * POSIX: "If the old argument and the new argument
3528 * both refer to links to the same existing file,
3529 * the rename() function shall return successfully
3530 * and perform no other action."
3531 */
3532 ZFS_EXIT(zfsvfs);
3533 return (0);
3534 }
3535 /*
3536 * If the file system is case-folding, then we may
3537 * have some more checking to do. A case-folding file
3538 * system is either supporting mixed case sensitivity
3539 * access or is completely case-insensitive. Note
3540 * that the file system is always case preserving.
3541 *
3542 * In mixed sensitivity mode case sensitive behavior
3543 * is the default. FIGNORECASE must be used to
3544 * explicitly request case insensitive behavior.
3545 *
3546 * If the source and target names provided differ only
3547 * by case (e.g., a request to rename 'tim' to 'Tim'),
3548 * we will treat this as a special case in the
3549 * case-insensitive mode: as long as the source name
3550 * is an exact match, we will allow this to proceed as
3551 * a name-change request.
3552 */
3553 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3554 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3555 flags & FIGNORECASE)) &&
3556 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3557 &error) == 0) {
3558 /*
3559 * case preserving rename request, require exact
3560 * name matches
3561 */
3562 zflg |= ZCIEXACT;
3563 zflg &= ~ZCILOOK;
3564 }
3565 }
3566
3567 /*
3568 * If the source and destination directories are the same, we should
3569 * grab the z_name_lock of that directory only once.
3570 */
3571 if (sdzp == tdzp) {
3572 zflg |= ZHAVELOCK;
3573 rw_enter(&sdzp->z_name_lock, RW_READER);
3574 }
3575
3576 if (cmp < 0) {
3577 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3578 ZEXISTS | zflg, NULL, NULL);
3579 terr = zfs_dirent_lock(&tdl,
3580 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3581 } else {
3582 terr = zfs_dirent_lock(&tdl,
3583 tdzp, tnm, &tzp, zflg, NULL, NULL);
3584 serr = zfs_dirent_lock(&sdl,
3585 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3586 NULL, NULL);
3587 }
3588
3589 if (serr) {
3590 /*
3591 * Source entry invalid or not there.
3592 */
3593 if (!terr) {
3594 zfs_dirent_unlock(tdl);
3595 if (tzp)
3596 VN_RELE(ZTOV(tzp));
3597 }
3598
3599 if (sdzp == tdzp)
3600 rw_exit(&sdzp->z_name_lock);
3601
3602 if (strcmp(snm, "..") == 0)
3603 serr = SET_ERROR(EINVAL);
3604 ZFS_EXIT(zfsvfs);
3605 return (serr);
3606 }
3607 if (terr) {
3608 zfs_dirent_unlock(sdl);
3609 VN_RELE(ZTOV(szp));
3610
3611 if (sdzp == tdzp)
3612 rw_exit(&sdzp->z_name_lock);
3613
3614 if (strcmp(tnm, "..") == 0)
3615 terr = SET_ERROR(EINVAL);
3616 ZFS_EXIT(zfsvfs);
3617 return (terr);
3618 }
3619
3620 /*
3621 * Must have write access at the source to remove the old entry
3622 * and write access at the target to create the new entry.
3623 * Note that if target and source are the same, this can be
3624 * done in a single check.
3625 */
3626
3627 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3628 goto out;
3629
3630 if (ZTOV(szp)->v_type == VDIR) {
3631 /*
3632 * Check to make sure rename is valid.
3633 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3634 */
3635 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3636 goto out;
3637 }
3638
3639 /*
3640 * Does target exist?
3641 */
3642 if (tzp) {
3643 /*
3644 * Source and target must be the same type.
3645 */
3646 if (ZTOV(szp)->v_type == VDIR) {
3647 if (ZTOV(tzp)->v_type != VDIR) {
3648 error = SET_ERROR(ENOTDIR);
3649 goto out;
3650 }
3651 } else {
3652 if (ZTOV(tzp)->v_type == VDIR) {
3653 error = SET_ERROR(EISDIR);
3654 goto out;
3655 }
3656 }
3657 /*
3658 * POSIX dictates that when the source and target
3659 * entries refer to the same file object, rename
3660 * must do nothing and exit without error.
3661 */
3662 if (szp->z_id == tzp->z_id) {
3663 error = 0;
3664 goto out;
3665 }
3666 }
3667
3668 vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3669 if (tzp)
3670 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3671
3672 /*
3673 * notify the target directory if it is not the same
3674 * as source directory.
3675 */
3676 if (tdvp != sdvp) {
3677 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3678 }
3679
3680 tx = dmu_tx_create(zfsvfs->z_os);
3681 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3682 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3683 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3684 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3685 if (sdzp != tdzp) {
3686 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3687 zfs_sa_upgrade_txholds(tx, tdzp);
3688 }
3689 if (tzp) {
3690 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3691 zfs_sa_upgrade_txholds(tx, tzp);
3692 }
3693
3694 zfs_sa_upgrade_txholds(tx, szp);
3695 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3696 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3697 if (error) {
3698 if (zl != NULL)
3699 zfs_rename_unlock(&zl);
3700 zfs_dirent_unlock(sdl);
3701 zfs_dirent_unlock(tdl);
3702
3703 if (sdzp == tdzp)
3704 rw_exit(&sdzp->z_name_lock);
3705
3706 VN_RELE(ZTOV(szp));
3707 if (tzp)
3708 VN_RELE(ZTOV(tzp));
3709 if (error == ERESTART) {
3710 waited = B_TRUE;
3711 dmu_tx_wait(tx);
3712 dmu_tx_abort(tx);
3713 goto top;
3714 }
3715 dmu_tx_abort(tx);
3716 ZFS_EXIT(zfsvfs);
3717 return (error);
3718 }
3719
3720 if (tzp) /* Attempt to remove the existing target */
3721 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3722
3723 if (error == 0) {
3724 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3725 if (error == 0) {
3726 szp->z_pflags |= ZFS_AV_MODIFIED;
3727
3728 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3729 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3730 ASSERT0(error);
3731
3732 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3733 if (error == 0) {
3734 zfs_log_rename(zilog, tx, TX_RENAME |
3735 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3736 sdl->dl_name, tdzp, tdl->dl_name, szp);
3737
3738 /*
3739 * Update path information for the target vnode
3740 */
3741 vn_renamepath(tdvp, ZTOV(szp), tnm,
3742 strlen(tnm));
3743 } else {
3744 /*
3745 * At this point, we have successfully created
3746 * the target name, but have failed to remove
3747 * the source name. Since the create was done
3748 * with the ZRENAMING flag, there are
3749 * complications; for one, the link count is
3750 * wrong. The easiest way to deal with this
3751 * is to remove the newly created target, and
3752 * return the original error. This must
3753 * succeed; fortunately, it is very unlikely to
3754 * fail, since we just created it.
3755 */
3756 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3757 ZRENAMING, NULL), ==, 0);
3758 }
3759 }
3760 }
3761
3762 dmu_tx_commit(tx);
3763
3764 if (tzp && rm_err == 0)
3765 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3766
3767 if (error == 0) {
3768 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3769 vnevent_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3770 }
3771 out:
3772 if (zl != NULL)
3773 zfs_rename_unlock(&zl);
3774
3775 zfs_dirent_unlock(sdl);
3776 zfs_dirent_unlock(tdl);
3777
3778 if (sdzp == tdzp)
3779 rw_exit(&sdzp->z_name_lock);
3780
3781
3782 VN_RELE(ZTOV(szp));
3783 if (tzp)
3784 VN_RELE(ZTOV(tzp));
3785
3786 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3787 zil_commit(zilog, 0);
3788
3789 ZFS_EXIT(zfsvfs);
3790 return (error);
3791 }
3792
3793 /*
3794 * Insert the indicated symbolic reference entry into the directory.
3795 *
3796 * IN: dvp - Directory to contain new symbolic link.
3797 * link - Name for new symlink entry.
3798 * vap - Attributes of new entry.
3799 * cr - credentials of caller.
3800 * ct - caller context
3801 * flags - case flags
3802 *
3803 * RETURN: 0 on success, error code on failure.
3804 *
3805 * Timestamps:
3806 * dvp - ctime|mtime updated
3807 */
3808 /*ARGSUSED*/
3809 static int
3810 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3811 caller_context_t *ct, int flags)
3812 {
3813 znode_t *zp, *dzp = VTOZ(dvp);
3814 zfs_dirlock_t *dl;
3815 dmu_tx_t *tx;
3816 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3817 zilog_t *zilog;
3818 uint64_t len = strlen(link);
3819 int error;
3820 int zflg = ZNEW;
3821 zfs_acl_ids_t acl_ids;
3822 boolean_t fuid_dirtied;
3823 uint64_t txtype = TX_SYMLINK;
3824 boolean_t waited = B_FALSE;
3825
3826 ASSERT(vap->va_type == VLNK);
3827
3828 ZFS_ENTER(zfsvfs);
3829 ZFS_VERIFY_ZP(dzp);
3830 zilog = zfsvfs->z_log;
3831
3832 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3833 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3834 ZFS_EXIT(zfsvfs);
3835 return (SET_ERROR(EILSEQ));
3836 }
3837 if (flags & FIGNORECASE)
3838 zflg |= ZCILOOK;
3839
3840 if (len > MAXPATHLEN) {
3841 ZFS_EXIT(zfsvfs);
3842 return (SET_ERROR(ENAMETOOLONG));
3843 }
3844
3845 if ((error = zfs_acl_ids_create(dzp, 0,
3846 vap, cr, NULL, &acl_ids)) != 0) {
3847 ZFS_EXIT(zfsvfs);
3848 return (error);
3849 }
3850 top:
3851 /*
3852 * Attempt to lock directory; fail if entry already exists.
3853 */
3854 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3855 if (error) {
3856 zfs_acl_ids_free(&acl_ids);
3857 ZFS_EXIT(zfsvfs);
3858 return (error);
3859 }
3860
3861 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3862 zfs_acl_ids_free(&acl_ids);
3863 zfs_dirent_unlock(dl);
3864 ZFS_EXIT(zfsvfs);
3865 return (error);
3866 }
3867
3868 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3869 zfs_acl_ids_free(&acl_ids);
3870 zfs_dirent_unlock(dl);
3871 ZFS_EXIT(zfsvfs);
3872 return (SET_ERROR(EDQUOT));
3873 }
3874 tx = dmu_tx_create(zfsvfs->z_os);
3875 fuid_dirtied = zfsvfs->z_fuid_dirty;
3876 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3877 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3878 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3879 ZFS_SA_BASE_ATTR_SIZE + len);
3880 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3881 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3882 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3883 acl_ids.z_aclp->z_acl_bytes);
3884 }
3885 if (fuid_dirtied)
3886 zfs_fuid_txhold(zfsvfs, tx);
3887 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3888 if (error) {
3889 zfs_dirent_unlock(dl);
3890 if (error == ERESTART) {
3891 waited = B_TRUE;
3892 dmu_tx_wait(tx);
3893 dmu_tx_abort(tx);
3894 goto top;
3895 }
3896 zfs_acl_ids_free(&acl_ids);
3897 dmu_tx_abort(tx);
3898 ZFS_EXIT(zfsvfs);
3899 return (error);
3900 }
3901
3902 /*
3903 * Create a new object for the symlink.
3904 * for version 4 ZPL datsets the symlink will be an SA attribute
3905 */
3906 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3907
3908 if (fuid_dirtied)
3909 zfs_fuid_sync(zfsvfs, tx);
3910
3911 mutex_enter(&zp->z_lock);
3912 if (zp->z_is_sa)
3913 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3914 link, len, tx);
3915 else
3916 zfs_sa_symlink(zp, link, len, tx);
3917 mutex_exit(&zp->z_lock);
3918
3919 zp->z_size = len;
3920 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3921 &zp->z_size, sizeof (zp->z_size), tx);
3922 /*
3923 * Insert the new object into the directory.
3924 */
3925 (void) zfs_link_create(dl, zp, tx, ZNEW);
3926
3927 if (flags & FIGNORECASE)
3928 txtype |= TX_CI;
3929 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3930
3931 zfs_acl_ids_free(&acl_ids);
3932
3933 dmu_tx_commit(tx);
3934
3935 zfs_dirent_unlock(dl);
3936
3937 VN_RELE(ZTOV(zp));
3938
3939 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3940 zil_commit(zilog, 0);
3941
3942 ZFS_EXIT(zfsvfs);
3943 return (error);
3944 }
3945
3946 /*
3947 * Return, in the buffer contained in the provided uio structure,
3948 * the symbolic path referred to by vp.
3949 *
3950 * IN: vp - vnode of symbolic link.
3951 * uio - structure to contain the link path.
3952 * cr - credentials of caller.
3953 * ct - caller context
3954 *
3955 * OUT: uio - structure containing the link path.
3956 *
3957 * RETURN: 0 on success, error code on failure.
3958 *
3959 * Timestamps:
3960 * vp - atime updated
3961 */
3962 /* ARGSUSED */
3963 static int
3964 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3965 {
3966 znode_t *zp = VTOZ(vp);
3967 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3968 int error;
3969
3970 ZFS_ENTER(zfsvfs);
3971 ZFS_VERIFY_ZP(zp);
3972
3973 mutex_enter(&zp->z_lock);
3974 if (zp->z_is_sa)
3975 error = sa_lookup_uio(zp->z_sa_hdl,
3976 SA_ZPL_SYMLINK(zfsvfs), uio);
3977 else
3978 error = zfs_sa_readlink(zp, uio);
3979 mutex_exit(&zp->z_lock);
3980
3981 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3982
3983 ZFS_EXIT(zfsvfs);
3984 return (error);
3985 }
3986
3987 /*
3988 * Insert a new entry into directory tdvp referencing svp.
3989 *
3990 * IN: tdvp - Directory to contain new entry.
3991 * svp - vnode of new entry.
3992 * name - name of new entry.
3993 * cr - credentials of caller.
3994 * ct - caller context
3995 *
3996 * RETURN: 0 on success, error code on failure.
3997 *
3998 * Timestamps:
3999 * tdvp - ctime|mtime updated
4000 * svp - ctime updated
4001 */
4002 /* ARGSUSED */
4003 static int
4004 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4005 caller_context_t *ct, int flags)
4006 {
4007 znode_t *dzp = VTOZ(tdvp);
4008 znode_t *tzp, *szp;
4009 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4010 zilog_t *zilog;
4011 zfs_dirlock_t *dl;
4012 dmu_tx_t *tx;
4013 vnode_t *realvp;
4014 int error;
4015 int zf = ZNEW;
4016 uint64_t parent;
4017 uid_t owner;
4018 boolean_t waited = B_FALSE;
4019
4020 ASSERT(tdvp->v_type == VDIR);
4021
4022 ZFS_ENTER(zfsvfs);
4023 ZFS_VERIFY_ZP(dzp);
4024 zilog = zfsvfs->z_log;
4025
4026 if (VOP_REALVP(svp, &realvp, ct) == 0)
4027 svp = realvp;
4028
4029 /*
4030 * POSIX dictates that we return EPERM here.
4031 * Better choices include ENOTSUP or EISDIR.
4032 */
4033 if (svp->v_type == VDIR) {
4034 ZFS_EXIT(zfsvfs);
4035 return (SET_ERROR(EPERM));
4036 }
4037
4038 szp = VTOZ(svp);
4039 ZFS_VERIFY_ZP(szp);
4040
4041 /*
4042 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4043 * ctldir appear to have the same v_vfsp.
4044 */
4045 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4046 ZFS_EXIT(zfsvfs);
4047 return (SET_ERROR(EXDEV));
4048 }
4049
4050 /* Prevent links to .zfs/shares files */
4051
4052 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4053 &parent, sizeof (uint64_t))) != 0) {
4054 ZFS_EXIT(zfsvfs);
4055 return (error);
4056 }
4057 if (parent == zfsvfs->z_shares_dir) {
4058 ZFS_EXIT(zfsvfs);
4059 return (SET_ERROR(EPERM));
4060 }
4061
4062 if (zfsvfs->z_utf8 && u8_validate(name,
4063 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4064 ZFS_EXIT(zfsvfs);
4065 return (SET_ERROR(EILSEQ));
4066 }
4067 if (flags & FIGNORECASE)
4068 zf |= ZCILOOK;
4069
4070 /*
4071 * We do not support links between attributes and non-attributes
4072 * because of the potential security risk of creating links
4073 * into "normal" file space in order to circumvent restrictions
4074 * imposed in attribute space.
4075 */
4076 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4077 ZFS_EXIT(zfsvfs);
4078 return (SET_ERROR(EINVAL));
4079 }
4080
4081
4082 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4083 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4084 ZFS_EXIT(zfsvfs);
4085 return (SET_ERROR(EPERM));
4086 }
4087
4088 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4089 ZFS_EXIT(zfsvfs);
4090 return (error);
4091 }
4092
4093 top:
4094 /*
4095 * Attempt to lock directory; fail if entry already exists.
4096 */
4097 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4098 if (error) {
4099 ZFS_EXIT(zfsvfs);
4100 return (error);
4101 }
4102
4103 tx = dmu_tx_create(zfsvfs->z_os);
4104 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4105 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4106 zfs_sa_upgrade_txholds(tx, szp);
4107 zfs_sa_upgrade_txholds(tx, dzp);
4108 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4109 if (error) {
4110 zfs_dirent_unlock(dl);
4111 if (error == ERESTART) {
4112 waited = B_TRUE;
4113 dmu_tx_wait(tx);
4114 dmu_tx_abort(tx);
4115 goto top;
4116 }
4117 dmu_tx_abort(tx);
4118 ZFS_EXIT(zfsvfs);
4119 return (error);
4120 }
4121
4122 error = zfs_link_create(dl, szp, tx, 0);
4123
4124 if (error == 0) {
4125 uint64_t txtype = TX_LINK;
4126 if (flags & FIGNORECASE)
4127 txtype |= TX_CI;
4128 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4129 }
4130
4131 dmu_tx_commit(tx);
4132
4133 zfs_dirent_unlock(dl);
4134
4135 if (error == 0) {
4136 vnevent_link(svp, ct);
4137 }
4138
4139 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4140 zil_commit(zilog, 0);
4141
4142 ZFS_EXIT(zfsvfs);
4143 return (error);
4144 }
4145
4146 /*
4147 * zfs_null_putapage() is used when the file system has been force
4148 * unmounted. It just drops the pages.
4149 */
4150 /* ARGSUSED */
4151 static int
4152 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4153 size_t *lenp, int flags, cred_t *cr)
4154 {
4155 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4156 return (0);
4157 }
4158
4159 /*
4160 * Push a page out to disk, klustering if possible.
4161 *
4162 * IN: vp - file to push page to.
4163 * pp - page to push.
4164 * flags - additional flags.
4165 * cr - credentials of caller.
4166 *
4167 * OUT: offp - start of range pushed.
4168 * lenp - len of range pushed.
4169 *
4170 * RETURN: 0 on success, error code on failure.
4171 *
4172 * NOTE: callers must have locked the page to be pushed. On
4173 * exit, the page (and all other pages in the kluster) must be
4174 * unlocked.
4175 */
4176 /* ARGSUSED */
4177 static int
4178 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4179 size_t *lenp, int flags, cred_t *cr)
4180 {
4181 znode_t *zp = VTOZ(vp);
4182 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4183 dmu_tx_t *tx;
4184 u_offset_t off, koff;
4185 size_t len, klen;
4186 int err;
4187
4188 off = pp->p_offset;
4189 len = PAGESIZE;
4190 /*
4191 * If our blocksize is bigger than the page size, try to kluster
4192 * multiple pages so that we write a full block (thus avoiding
4193 * a read-modify-write).
4194 */
4195 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4196 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4197 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4198 ASSERT(koff <= zp->z_size);
4199 if (koff + klen > zp->z_size)
4200 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4201 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4202 }
4203 ASSERT3U(btop(len), ==, btopr(len));
4204
4205 /*
4206 * Can't push pages past end-of-file.
4207 */
4208 if (off >= zp->z_size) {
4209 /* ignore all pages */
4210 err = 0;
4211 goto out;
4212 } else if (off + len > zp->z_size) {
4213 int npages = btopr(zp->z_size - off);
4214 page_t *trunc;
4215
4216 page_list_break(&pp, &trunc, npages);
4217 /* ignore pages past end of file */
4218 if (trunc)
4219 pvn_write_done(trunc, flags);
4220 len = zp->z_size - off;
4221 }
4222
4223 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4224 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4225 err = SET_ERROR(EDQUOT);
4226 goto out;
4227 }
4228 tx = dmu_tx_create(zfsvfs->z_os);
4229 dmu_tx_hold_write(tx, zp->z_id, off, len);
4230
4231 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4232 zfs_sa_upgrade_txholds(tx, zp);
4233 err = dmu_tx_assign(tx, TXG_WAIT);
4234 if (err != 0) {
4235 dmu_tx_abort(tx);
4236 goto out;
4237 }
4238
4239 if (zp->z_blksz <= PAGESIZE) {
4240 caddr_t va = zfs_map_page(pp, S_READ);
4241 ASSERT3U(len, <=, PAGESIZE);
4242 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4243 zfs_unmap_page(pp, va);
4244 } else {
4245 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4246 }
4247
4248 if (err == 0) {
4249 uint64_t mtime[2], ctime[2];
4250 sa_bulk_attr_t bulk[3];
4251 int count = 0;
4252
4253 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4254 &mtime, 16);
4255 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4256 &ctime, 16);
4257 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4258 &zp->z_pflags, 8);
4259 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4260 B_TRUE);
4261 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4262
4263 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4264 }
4265 dmu_tx_commit(tx);
4266
4267 out:
4268 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4269 if (offp)
4270 *offp = off;
4271 if (lenp)
4272 *lenp = len;
4273
4274 return (err);
4275 }
4276
4277 /*
4278 * Copy the portion of the file indicated from pages into the file.
4279 * The pages are stored in a page list attached to the files vnode.
4280 *
4281 * IN: vp - vnode of file to push page data to.
4282 * off - position in file to put data.
4283 * len - amount of data to write.
4284 * flags - flags to control the operation.
4285 * cr - credentials of caller.
4286 * ct - caller context.
4287 *
4288 * RETURN: 0 on success, error code on failure.
4289 *
4290 * Timestamps:
4291 * vp - ctime|mtime updated
4292 */
4293 /*ARGSUSED*/
4294 static int
4295 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4296 caller_context_t *ct)
4297 {
4298 znode_t *zp = VTOZ(vp);
4299 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4300 page_t *pp;
4301 size_t io_len;
4302 u_offset_t io_off;
4303 uint_t blksz;
4304 rl_t *rl;
4305 int error = 0;
4306
4307 ZFS_ENTER(zfsvfs);
4308 ZFS_VERIFY_ZP(zp);
4309
4310 /*
4311 * There's nothing to do if no data is cached.
4312 */
4313 if (!vn_has_cached_data(vp)) {
4314 ZFS_EXIT(zfsvfs);
4315 return (0);
4316 }
4317
4318 /*
4319 * Align this request to the file block size in case we kluster.
4320 * XXX - this can result in pretty aggresive locking, which can
4321 * impact simultanious read/write access. One option might be
4322 * to break up long requests (len == 0) into block-by-block
4323 * operations to get narrower locking.
4324 */
4325 blksz = zp->z_blksz;
4326 if (ISP2(blksz))
4327 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4328 else
4329 io_off = 0;
4330 if (len > 0 && ISP2(blksz))
4331 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4332 else
4333 io_len = 0;
4334
4335 if (io_len == 0) {
4336 /*
4337 * Search the entire vp list for pages >= io_off.
4338 */
4339 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4340 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4341 goto out;
4342 }
4343 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4344
4345 if (off > zp->z_size) {
4346 /* past end of file */
4347 zfs_range_unlock(rl);
4348 ZFS_EXIT(zfsvfs);
4349 return (0);
4350 }
4351
4352 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4353
4354 for (off = io_off; io_off < off + len; io_off += io_len) {
4355 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4356 pp = page_lookup(vp, io_off,
4357 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4358 } else {
4359 pp = page_lookup_nowait(vp, io_off,
4360 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4361 }
4362
4363 if (pp != NULL && pvn_getdirty(pp, flags)) {
4364 int err;
4365
4366 /*
4367 * Found a dirty page to push
4368 */
4369 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4370 if (err)
4371 error = err;
4372 } else {
4373 io_len = PAGESIZE;
4374 }
4375 }
4376 out:
4377 zfs_range_unlock(rl);
4378 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4379 zil_commit(zfsvfs->z_log, zp->z_id);
4380 ZFS_EXIT(zfsvfs);
4381 return (error);
4382 }
4383
4384 /*ARGSUSED*/
4385 void
4386 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4387 {
4388 znode_t *zp = VTOZ(vp);
4389 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4390 int error;
4391
4392 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4393 if (zp->z_sa_hdl == NULL) {
4394 /*
4395 * The fs has been unmounted, or we did a
4396 * suspend/resume and this file no longer exists.
4397 */
4398 if (vn_has_cached_data(vp)) {
4399 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4400 B_INVAL, cr);
4401 }
4402
4403 mutex_enter(&zp->z_lock);
4404 mutex_enter(&vp->v_lock);
4405 ASSERT(vp->v_count == 1);
4406 vp->v_count = 0;
4407 mutex_exit(&vp->v_lock);
4408 mutex_exit(&zp->z_lock);
4409 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4410 zfs_znode_free(zp);
4411 return;
4412 }
4413
4414 /*
4415 * Attempt to push any data in the page cache. If this fails
4416 * we will get kicked out later in zfs_zinactive().
4417 */
4418 if (vn_has_cached_data(vp)) {
4419 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4420 cr);
4421 }
4422
4423 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4424 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4425
4426 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4427 zfs_sa_upgrade_txholds(tx, zp);
4428 error = dmu_tx_assign(tx, TXG_WAIT);
4429 if (error) {
4430 dmu_tx_abort(tx);
4431 } else {
4432 mutex_enter(&zp->z_lock);
4433 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4434 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4435 zp->z_atime_dirty = 0;
4436 mutex_exit(&zp->z_lock);
4437 dmu_tx_commit(tx);
4438 }
4439 }
4440
4441 zfs_zinactive(zp);
4442 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4443 }
4444
4445 /*
4446 * Bounds-check the seek operation.
4447 *
4448 * IN: vp - vnode seeking within
4449 * ooff - old file offset
4450 * noffp - pointer to new file offset
4451 * ct - caller context
4452 *
4453 * RETURN: 0 on success, EINVAL if new offset invalid.
4454 */
4455 /* ARGSUSED */
4456 static int
4457 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4458 caller_context_t *ct)
4459 {
4460 if (vp->v_type == VDIR)
4461 return (0);
4462 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4463 }
4464
4465 /*
4466 * Pre-filter the generic locking function to trap attempts to place
4467 * a mandatory lock on a memory mapped file.
4468 */
4469 static int
4470 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4471 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4472 {
4473 znode_t *zp = VTOZ(vp);
4474 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4475
4476 ZFS_ENTER(zfsvfs);
4477 ZFS_VERIFY_ZP(zp);
4478
4479 /*
4480 * We are following the UFS semantics with respect to mapcnt
4481 * here: If we see that the file is mapped already, then we will
4482 * return an error, but we don't worry about races between this
4483 * function and zfs_map().
4484 */
4485 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4486 ZFS_EXIT(zfsvfs);
4487 return (SET_ERROR(EAGAIN));
4488 }
4489 ZFS_EXIT(zfsvfs);
4490 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4491 }
4492
4493 /*
4494 * If we can't find a page in the cache, we will create a new page
4495 * and fill it with file data. For efficiency, we may try to fill
4496 * multiple pages at once (klustering) to fill up the supplied page
4497 * list. Note that the pages to be filled are held with an exclusive
4498 * lock to prevent access by other threads while they are being filled.
4499 */
4500 static int
4501 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4502 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4503 {
4504 znode_t *zp = VTOZ(vp);
4505 page_t *pp, *cur_pp;
4506 objset_t *os = zp->z_zfsvfs->z_os;
4507 u_offset_t io_off, total;
4508 size_t io_len;
4509 int err;
4510
4511 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4512 /*
4513 * We only have a single page, don't bother klustering
4514 */
4515 io_off = off;
4516 io_len = PAGESIZE;
4517 pp = page_create_va(vp, io_off, io_len,
4518 PG_EXCL | PG_WAIT, seg, addr);
4519 } else {
4520 /*
4521 * Try to find enough pages to fill the page list
4522 */
4523 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4524 &io_len, off, plsz, 0);
4525 }
4526 if (pp == NULL) {
4527 /*
4528 * The page already exists, nothing to do here.
4529 */
4530 *pl = NULL;
4531 return (0);
4532 }
4533
4534 /*
4535 * Fill the pages in the kluster.
4536 */
4537 cur_pp = pp;
4538 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4539 caddr_t va;
4540
4541 ASSERT3U(io_off, ==, cur_pp->p_offset);
4542 va = zfs_map_page(cur_pp, S_WRITE);
4543 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4544 DMU_READ_PREFETCH);
4545 zfs_unmap_page(cur_pp, va);
4546 if (err) {
4547 /* On error, toss the entire kluster */
4548 pvn_read_done(pp, B_ERROR);
4549 /* convert checksum errors into IO errors */
4550 if (err == ECKSUM)
4551 err = SET_ERROR(EIO);
4552 return (err);
4553 }
4554 cur_pp = cur_pp->p_next;
4555 }
4556
4557 /*
4558 * Fill in the page list array from the kluster starting
4559 * from the desired offset `off'.
4560 * NOTE: the page list will always be null terminated.
4561 */
4562 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4563 ASSERT(pl == NULL || (*pl)->p_offset == off);
4564
4565 return (0);
4566 }
4567
4568 /*
4569 * Return pointers to the pages for the file region [off, off + len]
4570 * in the pl array. If plsz is greater than len, this function may
4571 * also return page pointers from after the specified region
4572 * (i.e. the region [off, off + plsz]). These additional pages are
4573 * only returned if they are already in the cache, or were created as
4574 * part of a klustered read.
4575 *
4576 * IN: vp - vnode of file to get data from.
4577 * off - position in file to get data from.
4578 * len - amount of data to retrieve.
4579 * plsz - length of provided page list.
4580 * seg - segment to obtain pages for.
4581 * addr - virtual address of fault.
4582 * rw - mode of created pages.
4583 * cr - credentials of caller.
4584 * ct - caller context.
4585 *
4586 * OUT: protp - protection mode of created pages.
4587 * pl - list of pages created.
4588 *
4589 * RETURN: 0 on success, error code on failure.
4590 *
4591 * Timestamps:
4592 * vp - atime updated
4593 */
4594 /* ARGSUSED */
4595 static int
4596 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4597 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4598 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4599 {
4600 znode_t *zp = VTOZ(vp);
4601 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4602 page_t **pl0 = pl;
4603 int err = 0;
4604
4605 /* we do our own caching, faultahead is unnecessary */
4606 if (pl == NULL)
4607 return (0);
4608 else if (len > plsz)
4609 len = plsz;
4610 else
4611 len = P2ROUNDUP(len, PAGESIZE);
4612 ASSERT(plsz >= len);
4613
4614 ZFS_ENTER(zfsvfs);
4615 ZFS_VERIFY_ZP(zp);
4616
4617 if (protp)
4618 *protp = PROT_ALL;
4619
4620 /*
4621 * Loop through the requested range [off, off + len) looking
4622 * for pages. If we don't find a page, we will need to create
4623 * a new page and fill it with data from the file.
4624 */
4625 while (len > 0) {
4626 if (*pl = page_lookup(vp, off, SE_SHARED))
4627 *(pl+1) = NULL;
4628 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4629 goto out;
4630 while (*pl) {
4631 ASSERT3U((*pl)->p_offset, ==, off);
4632 off += PAGESIZE;
4633 addr += PAGESIZE;
4634 if (len > 0) {
4635 ASSERT3U(len, >=, PAGESIZE);
4636 len -= PAGESIZE;
4637 }
4638 ASSERT3U(plsz, >=, PAGESIZE);
4639 plsz -= PAGESIZE;
4640 pl++;
4641 }
4642 }
4643
4644 /*
4645 * Fill out the page array with any pages already in the cache.
4646 */
4647 while (plsz > 0 &&
4648 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4649 off += PAGESIZE;
4650 plsz -= PAGESIZE;
4651 }
4652 out:
4653 if (err) {
4654 /*
4655 * Release any pages we have previously locked.
4656 */
4657 while (pl > pl0)
4658 page_unlock(*--pl);
4659 } else {
4660 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4661 }
4662
4663 *pl = NULL;
4664
4665 ZFS_EXIT(zfsvfs);
4666 return (err);
4667 }
4668
4669 /*
4670 * Request a memory map for a section of a file. This code interacts
4671 * with common code and the VM system as follows:
4672 *
4673 * - common code calls mmap(), which ends up in smmap_common()
4674 * - this calls VOP_MAP(), which takes you into (say) zfs
4675 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4676 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4677 * - zfs_addmap() updates z_mapcnt
4678 */
4679 /*ARGSUSED*/
4680 static int
4681 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4682 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4683 caller_context_t *ct)
4684 {
4685 znode_t *zp = VTOZ(vp);
4686 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4687 segvn_crargs_t vn_a;
4688 int error;
4689
4690 ZFS_ENTER(zfsvfs);
4691 ZFS_VERIFY_ZP(zp);
4692
4693 if ((prot & PROT_WRITE) && (zp->z_pflags &
4694 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4695 ZFS_EXIT(zfsvfs);
4696 return (SET_ERROR(EPERM));
4697 }
4698
4699 if ((prot & (PROT_READ | PROT_EXEC)) &&
4700 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4701 ZFS_EXIT(zfsvfs);
4702 return (SET_ERROR(EACCES));
4703 }
4704
4705 if (vp->v_flag & VNOMAP) {
4706 ZFS_EXIT(zfsvfs);
4707 return (SET_ERROR(ENOSYS));
4708 }
4709
4710 if (off < 0 || len > MAXOFFSET_T - off) {
4711 ZFS_EXIT(zfsvfs);
4712 return (SET_ERROR(ENXIO));
4713 }
4714
4715 if (vp->v_type != VREG) {
4716 ZFS_EXIT(zfsvfs);
4717 return (SET_ERROR(ENODEV));
4718 }
4719
4720 /*
4721 * If file is locked, disallow mapping.
4722 */
4723 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4724 ZFS_EXIT(zfsvfs);
4725 return (SET_ERROR(EAGAIN));
4726 }
4727
4728 as_rangelock(as);
4729 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4730 if (error != 0) {
4731 as_rangeunlock(as);
4732 ZFS_EXIT(zfsvfs);
4733 return (error);
4734 }
4735
4736 vn_a.vp = vp;
4737 vn_a.offset = (u_offset_t)off;
4738 vn_a.type = flags & MAP_TYPE;
4739 vn_a.prot = prot;
4740 vn_a.maxprot = maxprot;
4741 vn_a.cred = cr;
4742 vn_a.amp = NULL;
4743 vn_a.flags = flags & ~MAP_TYPE;
4744 vn_a.szc = 0;
4745 vn_a.lgrp_mem_policy_flags = 0;
4746
4747 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4748
4749 as_rangeunlock(as);
4750 ZFS_EXIT(zfsvfs);
4751 return (error);
4752 }
4753
4754 /* ARGSUSED */
4755 static int
4756 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4757 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4758 caller_context_t *ct)
4759 {
4760 uint64_t pages = btopr(len);
4761
4762 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4763 return (0);
4764 }
4765
4766 /*
4767 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4768 * more accurate mtime for the associated file. Since we don't have a way of
4769 * detecting when the data was actually modified, we have to resort to
4770 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4771 * last page is pushed. The problem occurs when the msync() call is omitted,
4772 * which by far the most common case:
4773 *
4774 * open()
4775 * mmap()
4776 * <modify memory>
4777 * munmap()
4778 * close()
4779 * <time lapse>
4780 * putpage() via fsflush
4781 *
4782 * If we wait until fsflush to come along, we can have a modification time that
4783 * is some arbitrary point in the future. In order to prevent this in the
4784 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4785 * torn down.
4786 */
4787 /* ARGSUSED */
4788 static int
4789 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4790 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4791 caller_context_t *ct)
4792 {
4793 uint64_t pages = btopr(len);
4794
4795 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4796 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4797
4798 return (0);
4799 }
4800
4801 /*
4802 * Free or allocate space in a file. Currently, this function only
4803 * supports the `F_FREESP' command. However, this command is somewhat
4804 * misnamed, as its functionality includes the ability to allocate as
4805 * well as free space.
4806 *
4807 * IN: vp - vnode of file to free data in.
4808 * cmd - action to take (only F_FREESP supported).
4809 * bfp - section of file to free/alloc.
4810 * flag - current file open mode flags.
4811 * offset - current file offset.
4812 * cr - credentials of caller [UNUSED].
4813 * ct - caller context.
4814 *
4815 * RETURN: 0 on success, error code on failure.
4816 *
4817 * Timestamps:
4818 * vp - ctime|mtime updated
4819 */
4820 /* ARGSUSED */
4821 static int
4822 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4823 offset_t offset, cred_t *cr, caller_context_t *ct)
4824 {
4825 znode_t *zp = VTOZ(vp);
4826 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4827 uint64_t off, len;
4828 int error;
4829
4830 ZFS_ENTER(zfsvfs);
4831 ZFS_VERIFY_ZP(zp);
4832
4833 if (cmd != F_FREESP) {
4834 ZFS_EXIT(zfsvfs);
4835 return (SET_ERROR(EINVAL));
4836 }
4837
4838 /*
4839 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4840 * callers might not be able to detect properly that we are read-only,
4841 * so check it explicitly here.
4842 */
4843 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4844 ZFS_EXIT(zfsvfs);
4845 return (SET_ERROR(EROFS));
4846 }
4847
4848 if (error = convoff(vp, bfp, 0, offset)) {
4849 ZFS_EXIT(zfsvfs);
4850 return (error);
4851 }
4852
4853 if (bfp->l_len < 0) {
4854 ZFS_EXIT(zfsvfs);
4855 return (SET_ERROR(EINVAL));
4856 }
4857
4858 off = bfp->l_start;
4859 len = bfp->l_len; /* 0 means from off to end of file */
4860
4861 error = zfs_freesp(zp, off, len, flag, TRUE);
4862
4863 if (error == 0 && len == 0) {
4864 if (off == 0) {
4865 vnevent_truncate(ZTOV(zp), ct);
4866 } else {
4867 vnevent_resize(ZTOV(zp), ct);
4868 }
4869 }
4870
4871 ZFS_EXIT(zfsvfs);
4872 return (error);
4873 }
4874
4875 /*ARGSUSED*/
4876 static int
4877 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4878 {
4879 znode_t *zp = VTOZ(vp);
4880 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4881 uint32_t gen;
4882 uint64_t gen64;
4883 uint64_t object = zp->z_id;
4884 zfid_short_t *zfid;
4885 int size, i, error;
4886
4887 ZFS_ENTER(zfsvfs);
4888 ZFS_VERIFY_ZP(zp);
4889
4890 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4891 &gen64, sizeof (uint64_t))) != 0) {
4892 ZFS_EXIT(zfsvfs);
4893 return (error);
4894 }
4895
4896 gen = (uint32_t)gen64;
4897
4898 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4899 if (fidp->fid_len < size) {
4900 fidp->fid_len = size;
4901 ZFS_EXIT(zfsvfs);
4902 return (SET_ERROR(ENOSPC));
4903 }
4904
4905 zfid = (zfid_short_t *)fidp;
4906
4907 zfid->zf_len = size;
4908
4909 for (i = 0; i < sizeof (zfid->zf_object); i++)
4910 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4911
4912 /* Must have a non-zero generation number to distinguish from .zfs */
4913 if (gen == 0)
4914 gen = 1;
4915 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4916 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4917
4918 if (size == LONG_FID_LEN) {
4919 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4920 zfid_long_t *zlfid;
4921
4922 zlfid = (zfid_long_t *)fidp;
4923
4924 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4925 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4926
4927 /* XXX - this should be the generation number for the objset */
4928 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4929 zlfid->zf_setgen[i] = 0;
4930 }
4931
4932 ZFS_EXIT(zfsvfs);
4933 return (0);
4934 }
4935
4936 static int
4937 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4938 caller_context_t *ct)
4939 {
4940 znode_t *zp, *xzp;
4941 zfsvfs_t *zfsvfs;
4942 zfs_dirlock_t *dl;
4943 int error;
4944
4945 switch (cmd) {
4946 case _PC_LINK_MAX:
4947 *valp = ULONG_MAX;
4948 return (0);
4949
4950 case _PC_FILESIZEBITS:
4951 *valp = 64;
4952 return (0);
4953
4954 case _PC_XATTR_EXISTS:
4955 zp = VTOZ(vp);
4956 zfsvfs = zp->z_zfsvfs;
4957 ZFS_ENTER(zfsvfs);
4958 ZFS_VERIFY_ZP(zp);
4959 *valp = 0;
4960 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4961 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4962 if (error == 0) {
4963 zfs_dirent_unlock(dl);
4964 if (!zfs_dirempty(xzp))
4965 *valp = 1;
4966 VN_RELE(ZTOV(xzp));
4967 } else if (error == ENOENT) {
4968 /*
4969 * If there aren't extended attributes, it's the
4970 * same as having zero of them.
4971 */
4972 error = 0;
4973 }
4974 ZFS_EXIT(zfsvfs);
4975 return (error);
4976
4977 case _PC_SATTR_ENABLED:
4978 case _PC_SATTR_EXISTS:
4979 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4980 (vp->v_type == VREG || vp->v_type == VDIR);
4981 return (0);
4982
4983 case _PC_ACCESS_FILTERING:
4984 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4985 vp->v_type == VDIR;
4986 return (0);
4987
4988 case _PC_ACL_ENABLED:
4989 *valp = _ACL_ACE_ENABLED;
4990 return (0);
4991
4992 case _PC_MIN_HOLE_SIZE:
4993 *valp = (ulong_t)SPA_MINBLOCKSIZE;
4994 return (0);
4995
4996 case _PC_TIMESTAMP_RESOLUTION:
4997 /* nanosecond timestamp resolution */
4998 *valp = 1L;
4999 return (0);
5000
5001 default:
5002 return (fs_pathconf(vp, cmd, valp, cr, ct));
5003 }
5004 }
5005
5006 /*ARGSUSED*/
5007 static int
5008 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5009 caller_context_t *ct)
5010 {
5011 znode_t *zp = VTOZ(vp);
5012 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5013 int error;
5014 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5015
5016 ZFS_ENTER(zfsvfs);
5017 ZFS_VERIFY_ZP(zp);
5018 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5019 ZFS_EXIT(zfsvfs);
5020
5021 return (error);
5022 }
5023
5024 /*ARGSUSED*/
5025 static int
5026 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5027 caller_context_t *ct)
5028 {
5029 znode_t *zp = VTOZ(vp);
5030 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5031 int error;
5032 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5033 zilog_t *zilog = zfsvfs->z_log;
5034
5035 ZFS_ENTER(zfsvfs);
5036 ZFS_VERIFY_ZP(zp);
5037
5038 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5039
5040 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5041 zil_commit(zilog, 0);
5042
5043 ZFS_EXIT(zfsvfs);
5044 return (error);
5045 }
5046
5047 /*
5048 * The smallest read we may consider to loan out an arcbuf.
5049 * This must be a power of 2.
5050 */
5051 int zcr_blksz_min = (1 << 10); /* 1K */
5052 /*
5053 * If set to less than the file block size, allow loaning out of an
5054 * arcbuf for a partial block read. This must be a power of 2.
5055 */
5056 int zcr_blksz_max = (1 << 17); /* 128K */
5057
5058 /*ARGSUSED*/
5059 static int
5060 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5061 caller_context_t *ct)
5062 {
5063 znode_t *zp = VTOZ(vp);
5064 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5065 int max_blksz = zfsvfs->z_max_blksz;
5066 uio_t *uio = &xuio->xu_uio;
5067 ssize_t size = uio->uio_resid;
5068 offset_t offset = uio->uio_loffset;
5069 int blksz;
5070 int fullblk, i;
5071 arc_buf_t *abuf;
5072 ssize_t maxsize;
5073 int preamble, postamble;
5074
5075 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5076 return (SET_ERROR(EINVAL));
5077
5078 ZFS_ENTER(zfsvfs);
5079 ZFS_VERIFY_ZP(zp);
5080 switch (ioflag) {
5081 case UIO_WRITE:
5082 /*
5083 * Loan out an arc_buf for write if write size is bigger than
5084 * max_blksz, and the file's block size is also max_blksz.
5085 */
5086 blksz = max_blksz;
5087 if (size < blksz || zp->z_blksz != blksz) {
5088 ZFS_EXIT(zfsvfs);
5089 return (SET_ERROR(EINVAL));
5090 }
5091 /*
5092 * Caller requests buffers for write before knowing where the
5093 * write offset might be (e.g. NFS TCP write).
5094 */
5095 if (offset == -1) {
5096 preamble = 0;
5097 } else {
5098 preamble = P2PHASE(offset, blksz);
5099 if (preamble) {
5100 preamble = blksz - preamble;
5101 size -= preamble;
5102 }
5103 }
5104
5105 postamble = P2PHASE(size, blksz);
5106 size -= postamble;
5107
5108 fullblk = size / blksz;
5109 (void) dmu_xuio_init(xuio,
5110 (preamble != 0) + fullblk + (postamble != 0));
5111 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5112 int, postamble, int,
5113 (preamble != 0) + fullblk + (postamble != 0));
5114
5115 /*
5116 * Have to fix iov base/len for partial buffers. They
5117 * currently represent full arc_buf's.
5118 */
5119 if (preamble) {
5120 /* data begins in the middle of the arc_buf */
5121 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5122 blksz);
5123 ASSERT(abuf);
5124 (void) dmu_xuio_add(xuio, abuf,
5125 blksz - preamble, preamble);
5126 }
5127
5128 for (i = 0; i < fullblk; i++) {
5129 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5130 blksz);
5131 ASSERT(abuf);
5132 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5133 }
5134
5135 if (postamble) {
5136 /* data ends in the middle of the arc_buf */
5137 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5138 blksz);
5139 ASSERT(abuf);
5140 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5141 }
5142 break;
5143 case UIO_READ:
5144 /*
5145 * Loan out an arc_buf for read if the read size is larger than
5146 * the current file block size. Block alignment is not
5147 * considered. Partial arc_buf will be loaned out for read.
5148 */
5149 blksz = zp->z_blksz;
5150 if (blksz < zcr_blksz_min)
5151 blksz = zcr_blksz_min;
5152 if (blksz > zcr_blksz_max)
5153 blksz = zcr_blksz_max;
5154 /* avoid potential complexity of dealing with it */
5155 if (blksz > max_blksz) {
5156 ZFS_EXIT(zfsvfs);
5157 return (SET_ERROR(EINVAL));
5158 }
5159
5160 maxsize = zp->z_size - uio->uio_loffset;
5161 if (size > maxsize)
5162 size = maxsize;
5163
5164 if (size < blksz || vn_has_cached_data(vp)) {
5165 ZFS_EXIT(zfsvfs);
5166 return (SET_ERROR(EINVAL));
5167 }
5168 break;
5169 default:
5170 ZFS_EXIT(zfsvfs);
5171 return (SET_ERROR(EINVAL));
5172 }
5173
5174 uio->uio_extflg = UIO_XUIO;
5175 XUIO_XUZC_RW(xuio) = ioflag;
5176 ZFS_EXIT(zfsvfs);
5177 return (0);
5178 }
5179
5180 /*ARGSUSED*/
5181 static int
5182 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5183 {
5184 int i;
5185 arc_buf_t *abuf;
5186 int ioflag = XUIO_XUZC_RW(xuio);
5187
5188 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5189
5190 i = dmu_xuio_cnt(xuio);
5191 while (i-- > 0) {
5192 abuf = dmu_xuio_arcbuf(xuio, i);
5193 /*
5194 * if abuf == NULL, it must be a write buffer
5195 * that has been returned in zfs_write().
5196 */
5197 if (abuf)
5198 dmu_return_arcbuf(abuf);
5199 ASSERT(abuf || ioflag == UIO_WRITE);
5200 }
5201
5202 dmu_xuio_fini(xuio);
5203 return (0);
5204 }
5205
5206 /*
5207 * Predeclare these here so that the compiler assumes that
5208 * this is an "old style" function declaration that does
5209 * not include arguments => we won't get type mismatch errors
5210 * in the initializations that follow.
5211 */
5212 static int zfs_inval();
5213 static int zfs_isdir();
5214
5215 static int
5216 zfs_inval()
5217 {
5218 return (SET_ERROR(EINVAL));
5219 }
5220
5221 static int
5222 zfs_isdir()
5223 {
5224 return (SET_ERROR(EISDIR));
5225 }
5226 /*
5227 * Directory vnode operations template
5228 */
5229 vnodeops_t *zfs_dvnodeops;
5230 const fs_operation_def_t zfs_dvnodeops_template[] = {
5231 VOPNAME_OPEN, { .vop_open = zfs_open },
5232 VOPNAME_CLOSE, { .vop_close = zfs_close },
5233 VOPNAME_READ, { .error = zfs_isdir },
5234 VOPNAME_WRITE, { .error = zfs_isdir },
5235 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5236 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5237 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5238 VOPNAME_ACCESS, { .vop_access = zfs_access },
5239 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5240 VOPNAME_CREATE, { .vop_create = zfs_create },
5241 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5242 VOPNAME_LINK, { .vop_link = zfs_link },
5243 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5244 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5245 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5246 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5247 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5248 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5249 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5250 VOPNAME_FID, { .vop_fid = zfs_fid },
5251 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5252 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5253 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5254 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5255 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5256 NULL, NULL
5257 };
5258
5259 /*
5260 * Regular file vnode operations template
5261 */
5262 vnodeops_t *zfs_fvnodeops;
5263 const fs_operation_def_t zfs_fvnodeops_template[] = {
5264 VOPNAME_OPEN, { .vop_open = zfs_open },
5265 VOPNAME_CLOSE, { .vop_close = zfs_close },
5266 VOPNAME_READ, { .vop_read = zfs_read },
5267 VOPNAME_WRITE, { .vop_write = zfs_write },
5268 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5269 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5270 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5271 VOPNAME_ACCESS, { .vop_access = zfs_access },
5272 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5273 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5274 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5275 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5276 VOPNAME_FID, { .vop_fid = zfs_fid },
5277 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5278 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5279 VOPNAME_SPACE, { .vop_space = zfs_space },
5280 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5281 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5282 VOPNAME_MAP, { .vop_map = zfs_map },
5283 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5284 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5285 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5286 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5287 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5288 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5289 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5290 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5291 NULL, NULL
5292 };
5293
5294 /*
5295 * Symbolic link vnode operations template
5296 */
5297 vnodeops_t *zfs_symvnodeops;
5298 const fs_operation_def_t zfs_symvnodeops_template[] = {
5299 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5300 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5301 VOPNAME_ACCESS, { .vop_access = zfs_access },
5302 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5303 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5304 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5305 VOPNAME_FID, { .vop_fid = zfs_fid },
5306 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5307 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5308 NULL, NULL
5309 };
5310
5311 /*
5312 * special share hidden files vnode operations template
5313 */
5314 vnodeops_t *zfs_sharevnodeops;
5315 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5316 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5317 VOPNAME_ACCESS, { .vop_access = zfs_access },
5318 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5319 VOPNAME_FID, { .vop_fid = zfs_fid },
5320 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5321 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5322 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5323 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5324 NULL, NULL
5325 };
5326
5327 /*
5328 * Extended attribute directory vnode operations template
5329 *
5330 * This template is identical to the directory vnodes
5331 * operation template except for restricted operations:
5332 * VOP_MKDIR()
5333 * VOP_SYMLINK()
5334 *
5335 * Note that there are other restrictions embedded in:
5336 * zfs_create() - restrict type to VREG
5337 * zfs_link() - no links into/out of attribute space
5338 * zfs_rename() - no moves into/out of attribute space
5339 */
5340 vnodeops_t *zfs_xdvnodeops;
5341 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5342 VOPNAME_OPEN, { .vop_open = zfs_open },
5343 VOPNAME_CLOSE, { .vop_close = zfs_close },
5344 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5345 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5346 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5347 VOPNAME_ACCESS, { .vop_access = zfs_access },
5348 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5349 VOPNAME_CREATE, { .vop_create = zfs_create },
5350 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5351 VOPNAME_LINK, { .vop_link = zfs_link },
5352 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5353 VOPNAME_MKDIR, { .error = zfs_inval },
5354 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5355 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5356 VOPNAME_SYMLINK, { .error = zfs_inval },
5357 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5358 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5359 VOPNAME_FID, { .vop_fid = zfs_fid },
5360 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5361 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5362 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5363 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5364 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5365 NULL, NULL
5366 };
5367
5368 /*
5369 * Error vnode operations template
5370 */
5371 vnodeops_t *zfs_evnodeops;
5372 const fs_operation_def_t zfs_evnodeops_template[] = {
5373 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5374 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5375 NULL, NULL
5376 };