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