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