1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 */
26
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2010 Robert Milkowski */
29
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
36 #include <sys/vfs.h>
37 #include <sys/vm.h>
38 #include <sys/vnode.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/kmem.h>
42 #include <sys/taskq.h>
43 #include <sys/uio.h>
44 #include <sys/atomic.h>
45 #include <sys/namei.h>
46 #include <sys/mman.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/dmu.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/spa.h>
56 #include <sys/txg.h>
57 #include <sys/dbuf.h>
58 #include <sys/zap.h>
59 #include <sys/sa.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
63 #include <sys/filio.h>
64 #include <sys/sid.h>
65 #include <sys/zfs_ctldir.h>
66 #include <sys/zfs_fuid.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/dnlc.h>
69 #include <sys/zfs_rlock.h>
70 #include <sys/extdirent.h>
71 #include <sys/kidmap.h>
72 #include <sys/bio.h>
73 #include <sys/buf.h>
74 #include <sys/sched.h>
75 #include <sys/acl.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_pageout.h>
78
79 /*
80 * Programming rules.
81 *
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
89 *
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
95 *
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
105 *
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
108 *
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
112 *
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
116 * must wait).
117 *
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
121 *
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
126 *
127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
132 * each time.
133 *
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
139 *
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
142 *
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
145 *
146 * In general, this is how things should be ordered in each vnode op:
147 *
148 * ZFS_ENTER(zfsvfs); // exit if unmounted
149 * top:
150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
155 * if (error) {
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * VN_RELE(...); // release held vnodes
159 * if (error == ERESTART) {
160 * waited = B_TRUE;
161 * dmu_tx_wait(tx);
162 * dmu_tx_abort(tx);
163 * goto top;
164 * }
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zfsvfs); // finished in zfs
167 * return (error); // really out of space
168 * }
169 * error = do_real_work(); // do whatever this VOP does
170 * if (error == 0)
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * VN_RELE(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zfsvfs); // finished in zfs
178 * return (error); // done, report error
179 */
180
181 /* ARGSUSED */
182 static int
zfs_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)183 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
184 {
185 znode_t *zp = VTOZ(*vpp);
186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
187
188 ZFS_ENTER(zfsvfs);
189 ZFS_VERIFY_ZP(zp);
190
191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & FAPPEND) == 0)) {
193 ZFS_EXIT(zfsvfs);
194 return (SET_ERROR(EPERM));
195 }
196
197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
198 ZTOV(zp)->v_type == VREG &&
199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
200 if (fs_vscan(*vpp, cr, 0) != 0) {
201 ZFS_EXIT(zfsvfs);
202 return (SET_ERROR(EACCES));
203 }
204 }
205
206 /* Keep a count of the synchronous opens in the znode */
207 if (flag & (FSYNC | FDSYNC))
208 atomic_inc_32(&zp->z_sync_cnt);
209
210 ZFS_EXIT(zfsvfs);
211 return (0);
212 }
213
214 /* ARGSUSED */
215 static int
zfs_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)216 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
217 caller_context_t *ct)
218 {
219 znode_t *zp = VTOZ(vp);
220 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
221
222 /*
223 * Clean up any locks held by this process on the vp.
224 */
225 cleanlocks(vp, ddi_get_pid(), 0);
226 cleanshares(vp, ddi_get_pid());
227
228 ZFS_ENTER(zfsvfs);
229 ZFS_VERIFY_ZP(zp);
230
231 /* Decrement the synchronous opens in the znode */
232 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
233 atomic_dec_32(&zp->z_sync_cnt);
234
235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
236 ZTOV(zp)->v_type == VREG &&
237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
238 VERIFY(fs_vscan(vp, cr, 1) == 0);
239
240 ZFS_EXIT(zfsvfs);
241 return (0);
242 }
243
244 /*
245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
247 */
248 static int
zfs_holey(vnode_t * vp,u_long cmd,offset_t * off)249 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
250 {
251 znode_t *zp = VTOZ(vp);
252 uint64_t noff = (uint64_t)*off; /* new offset */
253 uint64_t file_sz;
254 int error;
255 boolean_t hole;
256
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
260 }
261
262 if (cmd == _FIO_SEEK_HOLE)
263 hole = B_TRUE;
264 else
265 hole = B_FALSE;
266
267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
268
269 if (error == ESRCH)
270 return (SET_ERROR(ENXIO));
271
272 /*
273 * We could find a hole that begins after the logical end-of-file,
274 * because dmu_offset_next() only works on whole blocks. If the
275 * EOF falls mid-block, then indicate that the "virtual hole"
276 * at the end of the file begins at the logical EOF, rather than
277 * at the end of the last block.
278 */
279 if (noff > file_sz) {
280 ASSERT(hole);
281 noff = file_sz;
282 }
283
284 if (noff < *off)
285 return (error);
286 *off = noff;
287 return (error);
288 }
289
290 /* ARGSUSED */
291 static int
zfs_ioctl(vnode_t * vp,u_long com,intptr_t data,int flag,cred_t * cred,int * rvalp,caller_context_t * ct)292 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
293 int *rvalp, caller_context_t *ct)
294 {
295 offset_t off;
296 offset_t ndata;
297 dmu_object_info_t doi;
298 int error;
299 zfsvfs_t *zfsvfs;
300 znode_t *zp;
301
302 switch (com) {
303 case _FIOFFS:
304 {
305 return (0);
306
307 /*
308 * The following two ioctls are used by bfu. Faking out,
309 * necessary to avoid bfu errors.
310 */
311 }
312 case _FIOGDIO:
313 case _FIOSDIO:
314 {
315 return (0);
316 }
317
318 case _FIO_SEEK_DATA:
319 case _FIO_SEEK_HOLE:
320 {
321 #ifdef illumos
322 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
323 return (SET_ERROR(EFAULT));
324 #else
325 off = *(offset_t *)data;
326 #endif
327 zp = VTOZ(vp);
328 zfsvfs = zp->z_zfsvfs;
329 ZFS_ENTER(zfsvfs);
330 ZFS_VERIFY_ZP(zp);
331
332 /* offset parameter is in/out */
333 error = zfs_holey(vp, com, &off);
334 ZFS_EXIT(zfsvfs);
335 if (error)
336 return (error);
337 #ifdef illumos
338 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
339 return (SET_ERROR(EFAULT));
340 #else
341 *(offset_t *)data = off;
342 #endif
343 return (0);
344 }
345 #ifdef illumos
346 case _FIO_COUNT_FILLED:
347 {
348 /*
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
351 * ZFS object.
352 */
353 zp = VTOZ(vp);
354 zfsvfs = zp->z_zfsvfs;
355 ZFS_ENTER(zfsvfs);
356 ZFS_VERIFY_ZP(zp);
357
358 /*
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
361 * updated.
362 */
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
364 if (error) {
365 ZFS_EXIT(zfsvfs);
366 return (error);
367 }
368
369 /*
370 * Retrieve fill count from DMU object.
371 */
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
373 if (error) {
374 ZFS_EXIT(zfsvfs);
375 return (error);
376 }
377
378 ndata = doi.doi_fill_count;
379
380 ZFS_EXIT(zfsvfs);
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
383 return (0);
384 }
385 #endif
386 }
387 return (SET_ERROR(ENOTTY));
388 }
389
390 static vm_page_t
page_busy(vnode_t * vp,int64_t start,int64_t off,int64_t nbytes)391 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
392 {
393 vm_object_t obj;
394 vm_page_t pp;
395 int64_t end;
396
397 /*
398 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
399 * aligned boundaries, if the range is not aligned. As a result a
400 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
401 * It may happen that all DEV_BSIZE subranges are marked clean and thus
402 * the whole page would be considred clean despite have some dirty data.
403 * For this reason we should shrink the range to DEV_BSIZE aligned
404 * boundaries before calling vm_page_clear_dirty.
405 */
406 end = rounddown2(off + nbytes, DEV_BSIZE);
407 off = roundup2(off, DEV_BSIZE);
408 nbytes = end - off;
409
410 obj = vp->v_object;
411 zfs_vmobject_assert_wlocked(obj);
412
413 for (;;) {
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
415 pp->valid) {
416 if (vm_page_xbusied(pp)) {
417 /*
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
421 */
422 vm_page_reference(pp);
423 vm_page_lock(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb");
426 zfs_vmobject_wlock(obj);
427 continue;
428 }
429 vm_page_sbusy(pp);
430 }
431 #ifdef VM_LEGACY
432 else if (pp == NULL) {
433 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
434 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
435 VM_ALLOC_SBUSY);
436 } else {
437 ASSERT(pp != NULL && !pp->valid);
438 pp = NULL;
439 }
440 #endif
441 if (pp != NULL) {
442 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
443 vm_object_pip_add(obj, 1);
444 pmap_remove_write(pp);
445 if (nbytes != 0)
446 vm_page_clear_dirty(pp, off, nbytes);
447 }
448 break;
449 }
450 return (pp);
451 }
452
453 static void
page_unbusy(vm_page_t pp)454 page_unbusy(vm_page_t pp)
455 {
456
457 vm_page_sunbusy(pp);
458 vm_object_pip_subtract(pp->object, 1);
459 }
460
461 static vm_page_t
page_hold(vnode_t * vp,int64_t start)462 page_hold(vnode_t *vp, int64_t start)
463 {
464 vm_object_t obj;
465 vm_page_t pp;
466
467 obj = vp->v_object;
468 zfs_vmobject_assert_wlocked(obj);
469
470 for (;;) {
471 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
472 pp->valid) {
473 if (vm_page_xbusied(pp)) {
474 /*
475 * Reference the page before unlocking and
476 * sleeping so that the page daemon is less
477 * likely to reclaim it.
478 */
479 vm_page_reference(pp);
480 vm_page_lock(pp);
481 zfs_vmobject_wunlock(obj);
482 vm_page_busy_sleep(pp, "zfsmwb");
483 zfs_vmobject_wlock(obj);
484 continue;
485 }
486
487 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
488 vm_page_lock(pp);
489 vm_page_hold(pp);
490 vm_page_unlock(pp);
491
492 } else
493 pp = NULL;
494 break;
495 }
496 return (pp);
497 }
498
499 static void
page_unhold(vm_page_t pp)500 page_unhold(vm_page_t pp)
501 {
502
503 vm_page_lock(pp);
504 vm_page_unhold(pp);
505 vm_page_unlock(pp);
506 }
507
508 /*
509 * When a file is memory mapped, we must keep the IO data synchronized
510 * between the DMU cache and the memory mapped pages. What this means:
511 *
512 * On Write: If we find a memory mapped page, we write to *both*
513 * the page and the dmu buffer.
514 */
515 static void
update_pages(vnode_t * vp,int64_t start,int len,objset_t * os,uint64_t oid,int segflg,dmu_tx_t * tx)516 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
517 int segflg, dmu_tx_t *tx)
518 {
519 vm_object_t obj;
520 struct sf_buf *sf;
521 caddr_t va;
522 int off;
523
524 ASSERT(segflg != UIO_NOCOPY);
525 ASSERT(vp->v_mount != NULL);
526 obj = vp->v_object;
527 ASSERT(obj != NULL);
528
529 off = start & PAGEOFFSET;
530 zfs_vmobject_wlock(obj);
531 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
532 vm_page_t pp;
533 int nbytes = imin(PAGESIZE - off, len);
534
535 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
536 zfs_vmobject_wunlock(obj);
537
538 va = zfs_map_page(pp, &sf);
539 (void) dmu_read(os, oid, start+off, nbytes,
540 va+off, DMU_READ_PREFETCH);;
541 zfs_unmap_page(sf);
542
543 zfs_vmobject_wlock(obj);
544 page_unbusy(pp);
545 }
546 len -= nbytes;
547 off = 0;
548 }
549 vm_object_pip_wakeupn(obj, 0);
550 zfs_vmobject_wunlock(obj);
551 }
552
553 /*
554 * Read with UIO_NOCOPY flag means that sendfile(2) requests
555 * ZFS to populate a range of page cache pages with data.
556 *
557 * NOTE: this function could be optimized to pre-allocate
558 * all pages in advance, drain exclusive busy on all of them,
559 * map them into contiguous KVA region and populate them
560 * in one single dmu_read() call.
561 */
562 static int
mappedread_sf(vnode_t * vp,int nbytes,uio_t * uio)563 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
564 {
565 znode_t *zp = VTOZ(vp);
566 objset_t *os = zp->z_zfsvfs->z_os;
567 struct sf_buf *sf;
568 vm_object_t obj;
569 vm_page_t pp;
570 int64_t start;
571 caddr_t va;
572 int len = nbytes;
573 int off;
574 int error = 0;
575
576 ASSERT(uio->uio_segflg == UIO_NOCOPY);
577 ASSERT(vp->v_mount != NULL);
578 obj = vp->v_object;
579 ASSERT(obj != NULL);
580 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
581
582 zfs_vmobject_wlock(obj);
583 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
584 int bytes = MIN(PAGESIZE, len);
585
586 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
587 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
588 if (pp->valid == 0) {
589 zfs_vmobject_wunlock(obj);
590 va = zfs_map_page(pp, &sf);
591 error = dmu_read(os, zp->z_id, start, bytes, va,
592 DMU_READ_PREFETCH);
593 if (bytes != PAGESIZE && error == 0)
594 bzero(va + bytes, PAGESIZE - bytes);
595 zfs_unmap_page(sf);
596 zfs_vmobject_wlock(obj);
597 vm_page_sunbusy(pp);
598 vm_page_lock(pp);
599 if (error) {
600 if (pp->wire_count == 0 && pp->valid == 0 &&
601 !vm_page_busied(pp))
602 vm_page_free(pp);
603 } else {
604 pp->valid = VM_PAGE_BITS_ALL;
605 vm_page_activate(pp);
606 }
607 vm_page_unlock(pp);
608 } else {
609 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
610 vm_page_sunbusy(pp);
611 }
612 if (error)
613 break;
614 uio->uio_resid -= bytes;
615 uio->uio_offset += bytes;
616 len -= bytes;
617 }
618 zfs_vmobject_wunlock(obj);
619 return (error);
620 }
621
622 /*
623 * When a file is memory mapped, we must keep the IO data synchronized
624 * between the DMU cache and the memory mapped pages. What this means:
625 *
626 * On Read: We "read" preferentially from memory mapped pages,
627 * else we default from the dmu buffer.
628 *
629 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
630 * the file is memory mapped.
631 */
632 static int
mappedread(vnode_t * vp,int nbytes,uio_t * uio)633 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
634 {
635 znode_t *zp = VTOZ(vp);
636 vm_object_t obj;
637 int64_t start;
638 caddr_t va;
639 int len = nbytes;
640 int off;
641 int error = 0;
642
643 ASSERT(vp->v_mount != NULL);
644 obj = vp->v_object;
645 ASSERT(obj != NULL);
646
647 start = uio->uio_loffset;
648 off = start & PAGEOFFSET;
649 zfs_vmobject_wlock(obj);
650 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
651 vm_page_t pp;
652 uint64_t bytes = MIN(PAGESIZE - off, len);
653
654 if (pp = page_hold(vp, start)) {
655 struct sf_buf *sf;
656 caddr_t va;
657
658 zfs_vmobject_wunlock(obj);
659 va = zfs_map_page(pp, &sf);
660 error = uiomove(va + off, bytes, UIO_READ, uio);
661 zfs_unmap_page(sf);
662 zfs_vmobject_wlock(obj);
663 page_unhold(pp);
664 } else {
665 zfs_vmobject_wunlock(obj);
666 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
667 uio, bytes);
668 zfs_vmobject_wlock(obj);
669 }
670 len -= bytes;
671 off = 0;
672 if (error)
673 break;
674 }
675 zfs_vmobject_wunlock(obj);
676 return (error);
677 }
678
679 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
680
681 /*
682 * Read bytes from specified file into supplied buffer.
683 *
684 * IN: vp - vnode of file to be read from.
685 * uio - structure supplying read location, range info,
686 * and return buffer.
687 * ioflag - SYNC flags; used to provide FRSYNC semantics.
688 * cr - credentials of caller.
689 * ct - caller context
690 *
691 * OUT: uio - updated offset and range, buffer filled.
692 *
693 * RETURN: 0 on success, error code on failure.
694 *
695 * Side Effects:
696 * vp - atime updated if byte count > 0
697 */
698 /* ARGSUSED */
699 static int
zfs_read(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)700 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
701 {
702 znode_t *zp = VTOZ(vp);
703 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
704 ssize_t n, nbytes;
705 int error = 0;
706 rl_t *rl;
707 xuio_t *xuio = NULL;
708
709 ZFS_ENTER(zfsvfs);
710 ZFS_VERIFY_ZP(zp);
711
712 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
713 ZFS_EXIT(zfsvfs);
714 return (SET_ERROR(EACCES));
715 }
716
717 /*
718 * Validate file offset
719 */
720 if (uio->uio_loffset < (offset_t)0) {
721 ZFS_EXIT(zfsvfs);
722 return (SET_ERROR(EINVAL));
723 }
724
725 /*
726 * Fasttrack empty reads
727 */
728 if (uio->uio_resid == 0) {
729 ZFS_EXIT(zfsvfs);
730 return (0);
731 }
732
733 /*
734 * Check for mandatory locks
735 */
736 if (MANDMODE(zp->z_mode)) {
737 if (error = chklock(vp, FREAD,
738 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
739 ZFS_EXIT(zfsvfs);
740 return (error);
741 }
742 }
743
744 /*
745 * If we're in FRSYNC mode, sync out this znode before reading it.
746 */
747 if (zfsvfs->z_log &&
748 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
749 zil_commit(zfsvfs->z_log, zp->z_id);
750
751 /*
752 * Lock the range against changes.
753 */
754 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
755
756 /*
757 * If we are reading past end-of-file we can skip
758 * to the end; but we might still need to set atime.
759 */
760 if (uio->uio_loffset >= zp->z_size) {
761 error = 0;
762 goto out;
763 }
764
765 ASSERT(uio->uio_loffset < zp->z_size);
766 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
767
768 #ifdef illumos
769 if ((uio->uio_extflg == UIO_XUIO) &&
770 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
771 int nblk;
772 int blksz = zp->z_blksz;
773 uint64_t offset = uio->uio_loffset;
774
775 xuio = (xuio_t *)uio;
776 if ((ISP2(blksz))) {
777 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
778 blksz)) / blksz;
779 } else {
780 ASSERT(offset + n <= blksz);
781 nblk = 1;
782 }
783 (void) dmu_xuio_init(xuio, nblk);
784
785 if (vn_has_cached_data(vp)) {
786 /*
787 * For simplicity, we always allocate a full buffer
788 * even if we only expect to read a portion of a block.
789 */
790 while (--nblk >= 0) {
791 (void) dmu_xuio_add(xuio,
792 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
793 blksz), 0, blksz);
794 }
795 }
796 }
797 #endif /* illumos */
798
799 while (n > 0) {
800 nbytes = MIN(n, zfs_read_chunk_size -
801 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
802
803 #ifdef __FreeBSD__
804 if (uio->uio_segflg == UIO_NOCOPY)
805 error = mappedread_sf(vp, nbytes, uio);
806 else
807 #endif /* __FreeBSD__ */
808 if (vn_has_cached_data(vp)) {
809 error = mappedread(vp, nbytes, uio);
810 } else {
811 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
812 uio, nbytes);
813 }
814 if (error) {
815 /* convert checksum errors into IO errors */
816 if (error == ECKSUM)
817 error = SET_ERROR(EIO);
818 break;
819 }
820
821 n -= nbytes;
822 }
823 out:
824 zfs_range_unlock(rl);
825
826 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
827 ZFS_EXIT(zfsvfs);
828 return (error);
829 }
830
831 /*
832 * Write the bytes to a file.
833 *
834 * IN: vp - vnode of file to be written to.
835 * uio - structure supplying write location, range info,
836 * and data buffer.
837 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
838 * set if in append mode.
839 * cr - credentials of caller.
840 * ct - caller context (NFS/CIFS fem monitor only)
841 *
842 * OUT: uio - updated offset and range.
843 *
844 * RETURN: 0 on success, error code on failure.
845 *
846 * Timestamps:
847 * vp - ctime|mtime updated if byte count > 0
848 */
849
850 /* ARGSUSED */
851 static int
zfs_write(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)852 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
853 {
854 znode_t *zp = VTOZ(vp);
855 rlim64_t limit = MAXOFFSET_T;
856 ssize_t start_resid = uio->uio_resid;
857 ssize_t tx_bytes;
858 uint64_t end_size;
859 dmu_tx_t *tx;
860 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
861 zilog_t *zilog;
862 offset_t woff;
863 ssize_t n, nbytes;
864 rl_t *rl;
865 int max_blksz = zfsvfs->z_max_blksz;
866 int error = 0;
867 arc_buf_t *abuf;
868 iovec_t *aiov = NULL;
869 xuio_t *xuio = NULL;
870 int i_iov = 0;
871 int iovcnt = uio->uio_iovcnt;
872 iovec_t *iovp = uio->uio_iov;
873 int write_eof;
874 int count = 0;
875 sa_bulk_attr_t bulk[4];
876 uint64_t mtime[2], ctime[2];
877
878 /*
879 * Fasttrack empty write
880 */
881 n = start_resid;
882 if (n == 0)
883 return (0);
884
885 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
886 limit = MAXOFFSET_T;
887
888 ZFS_ENTER(zfsvfs);
889 ZFS_VERIFY_ZP(zp);
890
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
892 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
894 &zp->z_size, 8);
895 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
896 &zp->z_pflags, 8);
897
898 /*
899 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
900 * callers might not be able to detect properly that we are read-only,
901 * so check it explicitly here.
902 */
903 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
904 ZFS_EXIT(zfsvfs);
905 return (SET_ERROR(EROFS));
906 }
907
908 /*
909 * If immutable or not appending then return EPERM
910 */
911 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
912 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
913 (uio->uio_loffset < zp->z_size))) {
914 ZFS_EXIT(zfsvfs);
915 return (SET_ERROR(EPERM));
916 }
917
918 zilog = zfsvfs->z_log;
919
920 /*
921 * Validate file offset
922 */
923 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
924 if (woff < 0) {
925 ZFS_EXIT(zfsvfs);
926 return (SET_ERROR(EINVAL));
927 }
928
929 /*
930 * Check for mandatory locks before calling zfs_range_lock()
931 * in order to prevent a deadlock with locks set via fcntl().
932 */
933 if (MANDMODE((mode_t)zp->z_mode) &&
934 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
935 ZFS_EXIT(zfsvfs);
936 return (error);
937 }
938
939 #ifdef illumos
940 /*
941 * Pre-fault the pages to ensure slow (eg NFS) pages
942 * don't hold up txg.
943 * Skip this if uio contains loaned arc_buf.
944 */
945 if ((uio->uio_extflg == UIO_XUIO) &&
946 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
947 xuio = (xuio_t *)uio;
948 else
949 uio_prefaultpages(MIN(n, max_blksz), uio);
950 #endif
951
952 /*
953 * If in append mode, set the io offset pointer to eof.
954 */
955 if (ioflag & FAPPEND) {
956 /*
957 * Obtain an appending range lock to guarantee file append
958 * semantics. We reset the write offset once we have the lock.
959 */
960 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
961 woff = rl->r_off;
962 if (rl->r_len == UINT64_MAX) {
963 /*
964 * We overlocked the file because this write will cause
965 * the file block size to increase.
966 * Note that zp_size cannot change with this lock held.
967 */
968 woff = zp->z_size;
969 }
970 uio->uio_loffset = woff;
971 } else {
972 /*
973 * Note that if the file block size will change as a result of
974 * this write, then this range lock will lock the entire file
975 * so that we can re-write the block safely.
976 */
977 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
978 }
979
980 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
981 zfs_range_unlock(rl);
982 ZFS_EXIT(zfsvfs);
983 return (EFBIG);
984 }
985
986 if (woff >= limit) {
987 zfs_range_unlock(rl);
988 ZFS_EXIT(zfsvfs);
989 return (SET_ERROR(EFBIG));
990 }
991
992 if ((woff + n) > limit || woff > (limit - n))
993 n = limit - woff;
994
995 /* Will this write extend the file length? */
996 write_eof = (woff + n > zp->z_size);
997
998 end_size = MAX(zp->z_size, woff + n);
999
1000 /*
1001 * Write the file in reasonable size chunks. Each chunk is written
1002 * in a separate transaction; this keeps the intent log records small
1003 * and allows us to do more fine-grained space accounting.
1004 */
1005 while (n > 0) {
1006 abuf = NULL;
1007 woff = uio->uio_loffset;
1008 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1009 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1010 if (abuf != NULL)
1011 dmu_return_arcbuf(abuf);
1012 error = SET_ERROR(EDQUOT);
1013 break;
1014 }
1015
1016 if (xuio && abuf == NULL) {
1017 ASSERT(i_iov < iovcnt);
1018 aiov = &iovp[i_iov];
1019 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1020 dmu_xuio_clear(xuio, i_iov);
1021 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1022 iovec_t *, aiov, arc_buf_t *, abuf);
1023 ASSERT((aiov->iov_base == abuf->b_data) ||
1024 ((char *)aiov->iov_base - (char *)abuf->b_data +
1025 aiov->iov_len == arc_buf_size(abuf)));
1026 i_iov++;
1027 } else if (abuf == NULL && n >= max_blksz &&
1028 woff >= zp->z_size &&
1029 P2PHASE(woff, max_blksz) == 0 &&
1030 zp->z_blksz == max_blksz) {
1031 /*
1032 * This write covers a full block. "Borrow" a buffer
1033 * from the dmu so that we can fill it before we enter
1034 * a transaction. This avoids the possibility of
1035 * holding up the transaction if the data copy hangs
1036 * up on a pagefault (e.g., from an NFS server mapping).
1037 */
1038 size_t cbytes;
1039
1040 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1041 max_blksz);
1042 ASSERT(abuf != NULL);
1043 ASSERT(arc_buf_size(abuf) == max_blksz);
1044 if (error = uiocopy(abuf->b_data, max_blksz,
1045 UIO_WRITE, uio, &cbytes)) {
1046 dmu_return_arcbuf(abuf);
1047 break;
1048 }
1049 ASSERT(cbytes == max_blksz);
1050 }
1051
1052 /*
1053 * Start a transaction.
1054 */
1055 tx = dmu_tx_create(zfsvfs->z_os);
1056 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1057 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1058 zfs_sa_upgrade_txholds(tx, zp);
1059 error = dmu_tx_assign(tx, TXG_WAIT);
1060 if (error) {
1061 dmu_tx_abort(tx);
1062 if (abuf != NULL)
1063 dmu_return_arcbuf(abuf);
1064 break;
1065 }
1066
1067 /*
1068 * If zfs_range_lock() over-locked we grow the blocksize
1069 * and then reduce the lock range. This will only happen
1070 * on the first iteration since zfs_range_reduce() will
1071 * shrink down r_len to the appropriate size.
1072 */
1073 if (rl->r_len == UINT64_MAX) {
1074 uint64_t new_blksz;
1075
1076 if (zp->z_blksz > max_blksz) {
1077 /*
1078 * File's blocksize is already larger than the
1079 * "recordsize" property. Only let it grow to
1080 * the next power of 2.
1081 */
1082 ASSERT(!ISP2(zp->z_blksz));
1083 new_blksz = MIN(end_size,
1084 1 << highbit64(zp->z_blksz));
1085 } else {
1086 new_blksz = MIN(end_size, max_blksz);
1087 }
1088 zfs_grow_blocksize(zp, new_blksz, tx);
1089 zfs_range_reduce(rl, woff, n);
1090 }
1091
1092 /*
1093 * XXX - should we really limit each write to z_max_blksz?
1094 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1095 */
1096 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1097
1098 if (woff + nbytes > zp->z_size)
1099 vnode_pager_setsize(vp, woff + nbytes);
1100
1101 if (abuf == NULL) {
1102 tx_bytes = uio->uio_resid;
1103 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1104 uio, nbytes, tx);
1105 tx_bytes -= uio->uio_resid;
1106 } else {
1107 tx_bytes = nbytes;
1108 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1109 /*
1110 * If this is not a full block write, but we are
1111 * extending the file past EOF and this data starts
1112 * block-aligned, use assign_arcbuf(). Otherwise,
1113 * write via dmu_write().
1114 */
1115 if (tx_bytes < max_blksz && (!write_eof ||
1116 aiov->iov_base != abuf->b_data)) {
1117 ASSERT(xuio);
1118 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1119 aiov->iov_len, aiov->iov_base, tx);
1120 dmu_return_arcbuf(abuf);
1121 xuio_stat_wbuf_copied();
1122 } else {
1123 ASSERT(xuio || tx_bytes == max_blksz);
1124 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1125 woff, abuf, tx);
1126 }
1127 ASSERT(tx_bytes <= uio->uio_resid);
1128 uioskip(uio, tx_bytes);
1129 }
1130 if (tx_bytes && vn_has_cached_data(vp)) {
1131 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1132 zp->z_id, uio->uio_segflg, tx);
1133 }
1134
1135 /*
1136 * If we made no progress, we're done. If we made even
1137 * partial progress, update the znode and ZIL accordingly.
1138 */
1139 if (tx_bytes == 0) {
1140 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1141 (void *)&zp->z_size, sizeof (uint64_t), tx);
1142 dmu_tx_commit(tx);
1143 ASSERT(error != 0);
1144 break;
1145 }
1146
1147 /*
1148 * Clear Set-UID/Set-GID bits on successful write if not
1149 * privileged and at least one of the excute bits is set.
1150 *
1151 * It would be nice to to this after all writes have
1152 * been done, but that would still expose the ISUID/ISGID
1153 * to another app after the partial write is committed.
1154 *
1155 * Note: we don't call zfs_fuid_map_id() here because
1156 * user 0 is not an ephemeral uid.
1157 */
1158 mutex_enter(&zp->z_acl_lock);
1159 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1160 (S_IXUSR >> 6))) != 0 &&
1161 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1162 secpolicy_vnode_setid_retain(vp, cr,
1163 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1164 uint64_t newmode;
1165 zp->z_mode &= ~(S_ISUID | S_ISGID);
1166 newmode = zp->z_mode;
1167 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1168 (void *)&newmode, sizeof (uint64_t), tx);
1169 }
1170 mutex_exit(&zp->z_acl_lock);
1171
1172 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1173 B_TRUE);
1174
1175 /*
1176 * Update the file size (zp_size) if it has changed;
1177 * account for possible concurrent updates.
1178 */
1179 while ((end_size = zp->z_size) < uio->uio_loffset) {
1180 (void) atomic_cas_64(&zp->z_size, end_size,
1181 uio->uio_loffset);
1182 ASSERT(error == 0);
1183 }
1184 /*
1185 * If we are replaying and eof is non zero then force
1186 * the file size to the specified eof. Note, there's no
1187 * concurrency during replay.
1188 */
1189 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1190 zp->z_size = zfsvfs->z_replay_eof;
1191
1192 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1193
1194 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1195 dmu_tx_commit(tx);
1196
1197 if (error != 0)
1198 break;
1199 ASSERT(tx_bytes == nbytes);
1200 n -= nbytes;
1201
1202 #ifdef illumos
1203 if (!xuio && n > 0)
1204 uio_prefaultpages(MIN(n, max_blksz), uio);
1205 #endif
1206 }
1207
1208 zfs_range_unlock(rl);
1209
1210 /*
1211 * If we're in replay mode, or we made no progress, return error.
1212 * Otherwise, it's at least a partial write, so it's successful.
1213 */
1214 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1215 ZFS_EXIT(zfsvfs);
1216 return (error);
1217 }
1218
1219 if (ioflag & (FSYNC | FDSYNC) ||
1220 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1221 zil_commit(zilog, zp->z_id);
1222
1223 ZFS_EXIT(zfsvfs);
1224 return (0);
1225 }
1226
1227 void
zfs_get_done(zgd_t * zgd,int error)1228 zfs_get_done(zgd_t *zgd, int error)
1229 {
1230 znode_t *zp = zgd->zgd_private;
1231 objset_t *os = zp->z_zfsvfs->z_os;
1232
1233 if (zgd->zgd_db)
1234 dmu_buf_rele(zgd->zgd_db, zgd);
1235
1236 zfs_range_unlock(zgd->zgd_rl);
1237
1238 /*
1239 * Release the vnode asynchronously as we currently have the
1240 * txg stopped from syncing.
1241 */
1242 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1243
1244 if (error == 0 && zgd->zgd_bp)
1245 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1246
1247 kmem_free(zgd, sizeof (zgd_t));
1248 }
1249
1250 #ifdef DEBUG
1251 static int zil_fault_io = 0;
1252 #endif
1253
1254 /*
1255 * Get data to generate a TX_WRITE intent log record.
1256 */
1257 int
zfs_get_data(void * arg,lr_write_t * lr,char * buf,zio_t * zio)1258 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1259 {
1260 zfsvfs_t *zfsvfs = arg;
1261 objset_t *os = zfsvfs->z_os;
1262 znode_t *zp;
1263 uint64_t object = lr->lr_foid;
1264 uint64_t offset = lr->lr_offset;
1265 uint64_t size = lr->lr_length;
1266 blkptr_t *bp = &lr->lr_blkptr;
1267 dmu_buf_t *db;
1268 zgd_t *zgd;
1269 int error = 0;
1270
1271 ASSERT(zio != NULL);
1272 ASSERT(size != 0);
1273
1274 /*
1275 * Nothing to do if the file has been removed
1276 */
1277 if (zfs_zget(zfsvfs, object, &zp) != 0)
1278 return (SET_ERROR(ENOENT));
1279 if (zp->z_unlinked) {
1280 /*
1281 * Release the vnode asynchronously as we currently have the
1282 * txg stopped from syncing.
1283 */
1284 VN_RELE_ASYNC(ZTOV(zp),
1285 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1286 return (SET_ERROR(ENOENT));
1287 }
1288
1289 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1290 zgd->zgd_zilog = zfsvfs->z_log;
1291 zgd->zgd_private = zp;
1292
1293 /*
1294 * Write records come in two flavors: immediate and indirect.
1295 * For small writes it's cheaper to store the data with the
1296 * log record (immediate); for large writes it's cheaper to
1297 * sync the data and get a pointer to it (indirect) so that
1298 * we don't have to write the data twice.
1299 */
1300 if (buf != NULL) { /* immediate write */
1301 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1302 /* test for truncation needs to be done while range locked */
1303 if (offset >= zp->z_size) {
1304 error = SET_ERROR(ENOENT);
1305 } else {
1306 error = dmu_read(os, object, offset, size, buf,
1307 DMU_READ_NO_PREFETCH);
1308 }
1309 ASSERT(error == 0 || error == ENOENT);
1310 } else { /* indirect write */
1311 /*
1312 * Have to lock the whole block to ensure when it's
1313 * written out and it's checksum is being calculated
1314 * that no one can change the data. We need to re-check
1315 * blocksize after we get the lock in case it's changed!
1316 */
1317 for (;;) {
1318 uint64_t blkoff;
1319 size = zp->z_blksz;
1320 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1321 offset -= blkoff;
1322 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1323 RL_READER);
1324 if (zp->z_blksz == size)
1325 break;
1326 offset += blkoff;
1327 zfs_range_unlock(zgd->zgd_rl);
1328 }
1329 /* test for truncation needs to be done while range locked */
1330 if (lr->lr_offset >= zp->z_size)
1331 error = SET_ERROR(ENOENT);
1332 #ifdef DEBUG
1333 if (zil_fault_io) {
1334 error = SET_ERROR(EIO);
1335 zil_fault_io = 0;
1336 }
1337 #endif
1338 if (error == 0)
1339 error = dmu_buf_hold(os, object, offset, zgd, &db,
1340 DMU_READ_NO_PREFETCH);
1341
1342 if (error == 0) {
1343 blkptr_t *obp = dmu_buf_get_blkptr(db);
1344 if (obp) {
1345 ASSERT(BP_IS_HOLE(bp));
1346 *bp = *obp;
1347 }
1348
1349 zgd->zgd_db = db;
1350 zgd->zgd_bp = bp;
1351
1352 ASSERT(db->db_offset == offset);
1353 ASSERT(db->db_size == size);
1354
1355 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1356 zfs_get_done, zgd);
1357 ASSERT(error || lr->lr_length <= zp->z_blksz);
1358
1359 /*
1360 * On success, we need to wait for the write I/O
1361 * initiated by dmu_sync() to complete before we can
1362 * release this dbuf. We will finish everything up
1363 * in the zfs_get_done() callback.
1364 */
1365 if (error == 0)
1366 return (0);
1367
1368 if (error == EALREADY) {
1369 lr->lr_common.lrc_txtype = TX_WRITE2;
1370 error = 0;
1371 }
1372 }
1373 }
1374
1375 zfs_get_done(zgd, error);
1376
1377 return (error);
1378 }
1379
1380 /*ARGSUSED*/
1381 static int
zfs_access(vnode_t * vp,int mode,int flag,cred_t * cr,caller_context_t * ct)1382 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1383 caller_context_t *ct)
1384 {
1385 znode_t *zp = VTOZ(vp);
1386 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1387 int error;
1388
1389 ZFS_ENTER(zfsvfs);
1390 ZFS_VERIFY_ZP(zp);
1391
1392 if (flag & V_ACE_MASK)
1393 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1394 else
1395 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1396
1397 ZFS_EXIT(zfsvfs);
1398 return (error);
1399 }
1400
1401 /*
1402 * If vnode is for a device return a specfs vnode instead.
1403 */
1404 static int
specvp_check(vnode_t ** vpp,cred_t * cr)1405 specvp_check(vnode_t **vpp, cred_t *cr)
1406 {
1407 int error = 0;
1408
1409 if (IS_DEVVP(*vpp)) {
1410 struct vnode *svp;
1411
1412 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1413 VN_RELE(*vpp);
1414 if (svp == NULL)
1415 error = SET_ERROR(ENOSYS);
1416 *vpp = svp;
1417 }
1418 return (error);
1419 }
1420
1421
1422 /*
1423 * Lookup an entry in a directory, or an extended attribute directory.
1424 * If it exists, return a held vnode reference for it.
1425 *
1426 * IN: dvp - vnode of directory to search.
1427 * nm - name of entry to lookup.
1428 * pnp - full pathname to lookup [UNUSED].
1429 * flags - LOOKUP_XATTR set if looking for an attribute.
1430 * rdir - root directory vnode [UNUSED].
1431 * cr - credentials of caller.
1432 * ct - caller context
1433 * direntflags - directory lookup flags
1434 * realpnp - returned pathname.
1435 *
1436 * OUT: vpp - vnode of located entry, NULL if not found.
1437 *
1438 * RETURN: 0 on success, error code on failure.
1439 *
1440 * Timestamps:
1441 * NA
1442 */
1443 /* ARGSUSED */
1444 static int
zfs_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct componentname * cnp,int nameiop,cred_t * cr,kthread_t * td,int flags)1445 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1446 int nameiop, cred_t *cr, kthread_t *td, int flags)
1447 {
1448 znode_t *zdp = VTOZ(dvp);
1449 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1450 int error = 0;
1451 int *direntflags = NULL;
1452 void *realpnp = NULL;
1453
1454 /* fast path */
1455 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1456
1457 if (dvp->v_type != VDIR) {
1458 return (SET_ERROR(ENOTDIR));
1459 } else if (zdp->z_sa_hdl == NULL) {
1460 return (SET_ERROR(EIO));
1461 }
1462
1463 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1464 error = zfs_fastaccesschk_execute(zdp, cr);
1465 if (!error) {
1466 *vpp = dvp;
1467 VN_HOLD(*vpp);
1468 return (0);
1469 }
1470 return (error);
1471 } else {
1472 vnode_t *tvp = dnlc_lookup(dvp, nm);
1473
1474 if (tvp) {
1475 error = zfs_fastaccesschk_execute(zdp, cr);
1476 if (error) {
1477 VN_RELE(tvp);
1478 return (error);
1479 }
1480 if (tvp == DNLC_NO_VNODE) {
1481 VN_RELE(tvp);
1482 return (SET_ERROR(ENOENT));
1483 } else {
1484 *vpp = tvp;
1485 return (specvp_check(vpp, cr));
1486 }
1487 }
1488 }
1489 }
1490
1491 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1492
1493 ZFS_ENTER(zfsvfs);
1494 ZFS_VERIFY_ZP(zdp);
1495
1496 *vpp = NULL;
1497
1498 if (flags & LOOKUP_XATTR) {
1499 #ifdef TODO
1500 /*
1501 * If the xattr property is off, refuse the lookup request.
1502 */
1503 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1504 ZFS_EXIT(zfsvfs);
1505 return (SET_ERROR(EINVAL));
1506 }
1507 #endif
1508
1509 /*
1510 * We don't allow recursive attributes..
1511 * Maybe someday we will.
1512 */
1513 if (zdp->z_pflags & ZFS_XATTR) {
1514 ZFS_EXIT(zfsvfs);
1515 return (SET_ERROR(EINVAL));
1516 }
1517
1518 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1519 ZFS_EXIT(zfsvfs);
1520 return (error);
1521 }
1522
1523 /*
1524 * Do we have permission to get into attribute directory?
1525 */
1526
1527 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1528 B_FALSE, cr)) {
1529 VN_RELE(*vpp);
1530 *vpp = NULL;
1531 }
1532
1533 ZFS_EXIT(zfsvfs);
1534 return (error);
1535 }
1536
1537 if (dvp->v_type != VDIR) {
1538 ZFS_EXIT(zfsvfs);
1539 return (SET_ERROR(ENOTDIR));
1540 }
1541
1542 /*
1543 * Check accessibility of directory.
1544 */
1545
1546 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1547 ZFS_EXIT(zfsvfs);
1548 return (error);
1549 }
1550
1551 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1552 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1553 ZFS_EXIT(zfsvfs);
1554 return (SET_ERROR(EILSEQ));
1555 }
1556
1557 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1558 if (error == 0)
1559 error = specvp_check(vpp, cr);
1560
1561 /* Translate errors and add SAVENAME when needed. */
1562 if (cnp->cn_flags & ISLASTCN) {
1563 switch (nameiop) {
1564 case CREATE:
1565 case RENAME:
1566 if (error == ENOENT) {
1567 error = EJUSTRETURN;
1568 cnp->cn_flags |= SAVENAME;
1569 break;
1570 }
1571 /* FALLTHROUGH */
1572 case DELETE:
1573 if (error == 0)
1574 cnp->cn_flags |= SAVENAME;
1575 break;
1576 }
1577 }
1578 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1579 int ltype = 0;
1580
1581 if (cnp->cn_flags & ISDOTDOT) {
1582 ltype = VOP_ISLOCKED(dvp);
1583 VOP_UNLOCK(dvp, 0);
1584 }
1585 ZFS_EXIT(zfsvfs);
1586 error = vn_lock(*vpp, cnp->cn_lkflags);
1587 if (cnp->cn_flags & ISDOTDOT)
1588 vn_lock(dvp, ltype | LK_RETRY);
1589 if (error != 0) {
1590 VN_RELE(*vpp);
1591 *vpp = NULL;
1592 return (error);
1593 }
1594 } else {
1595 ZFS_EXIT(zfsvfs);
1596 }
1597
1598 #ifdef FREEBSD_NAMECACHE
1599 /*
1600 * Insert name into cache (as non-existent) if appropriate.
1601 */
1602 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1603 cache_enter(dvp, *vpp, cnp);
1604 /*
1605 * Insert name into cache if appropriate.
1606 */
1607 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1608 if (!(cnp->cn_flags & ISLASTCN) ||
1609 (nameiop != DELETE && nameiop != RENAME)) {
1610 cache_enter(dvp, *vpp, cnp);
1611 }
1612 }
1613 #endif
1614
1615 return (error);
1616 }
1617
1618 /*
1619 * Attempt to create a new entry in a directory. If the entry
1620 * already exists, truncate the file if permissible, else return
1621 * an error. Return the vp of the created or trunc'd file.
1622 *
1623 * IN: dvp - vnode of directory to put new file entry in.
1624 * name - name of new file entry.
1625 * vap - attributes of new file.
1626 * excl - flag indicating exclusive or non-exclusive mode.
1627 * mode - mode to open file with.
1628 * cr - credentials of caller.
1629 * flag - large file flag [UNUSED].
1630 * ct - caller context
1631 * vsecp - ACL to be set
1632 *
1633 * OUT: vpp - vnode of created or trunc'd entry.
1634 *
1635 * RETURN: 0 on success, error code on failure.
1636 *
1637 * Timestamps:
1638 * dvp - ctime|mtime updated if new entry created
1639 * vp - ctime|mtime always, atime if new
1640 */
1641
1642 /* ARGSUSED */
1643 static int
zfs_create(vnode_t * dvp,char * name,vattr_t * vap,int excl,int mode,vnode_t ** vpp,cred_t * cr,kthread_t * td)1644 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1645 vnode_t **vpp, cred_t *cr, kthread_t *td)
1646 {
1647 znode_t *zp, *dzp = VTOZ(dvp);
1648 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1649 zilog_t *zilog;
1650 objset_t *os;
1651 zfs_dirlock_t *dl;
1652 dmu_tx_t *tx;
1653 int error;
1654 ksid_t *ksid;
1655 uid_t uid;
1656 gid_t gid = crgetgid(cr);
1657 zfs_acl_ids_t acl_ids;
1658 boolean_t fuid_dirtied;
1659 boolean_t have_acl = B_FALSE;
1660 boolean_t waited = B_FALSE;
1661 void *vsecp = NULL;
1662 int flag = 0;
1663
1664 /*
1665 * If we have an ephemeral id, ACL, or XVATTR then
1666 * make sure file system is at proper version
1667 */
1668
1669 ksid = crgetsid(cr, KSID_OWNER);
1670 if (ksid)
1671 uid = ksid_getid(ksid);
1672 else
1673 uid = crgetuid(cr);
1674
1675 if (zfsvfs->z_use_fuids == B_FALSE &&
1676 (vsecp || (vap->va_mask & AT_XVATTR) ||
1677 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1678 return (SET_ERROR(EINVAL));
1679
1680 ZFS_ENTER(zfsvfs);
1681 ZFS_VERIFY_ZP(dzp);
1682 os = zfsvfs->z_os;
1683 zilog = zfsvfs->z_log;
1684
1685 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1686 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1687 ZFS_EXIT(zfsvfs);
1688 return (SET_ERROR(EILSEQ));
1689 }
1690
1691 if (vap->va_mask & AT_XVATTR) {
1692 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1693 crgetuid(cr), cr, vap->va_type)) != 0) {
1694 ZFS_EXIT(zfsvfs);
1695 return (error);
1696 }
1697 }
1698
1699 getnewvnode_reserve(1);
1700
1701 top:
1702 *vpp = NULL;
1703
1704 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1705 vap->va_mode &= ~S_ISVTX;
1706
1707 if (*name == '\0') {
1708 /*
1709 * Null component name refers to the directory itself.
1710 */
1711 VN_HOLD(dvp);
1712 zp = dzp;
1713 dl = NULL;
1714 error = 0;
1715 } else {
1716 /* possible VN_HOLD(zp) */
1717 int zflg = 0;
1718
1719 if (flag & FIGNORECASE)
1720 zflg |= ZCILOOK;
1721
1722 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1723 NULL, NULL);
1724 if (error) {
1725 if (have_acl)
1726 zfs_acl_ids_free(&acl_ids);
1727 if (strcmp(name, "..") == 0)
1728 error = SET_ERROR(EISDIR);
1729 getnewvnode_drop_reserve();
1730 ZFS_EXIT(zfsvfs);
1731 return (error);
1732 }
1733 }
1734
1735 if (zp == NULL) {
1736 uint64_t txtype;
1737
1738 /*
1739 * Create a new file object and update the directory
1740 * to reference it.
1741 */
1742 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1743 if (have_acl)
1744 zfs_acl_ids_free(&acl_ids);
1745 goto out;
1746 }
1747
1748 /*
1749 * We only support the creation of regular files in
1750 * extended attribute directories.
1751 */
1752
1753 if ((dzp->z_pflags & ZFS_XATTR) &&
1754 (vap->va_type != VREG)) {
1755 if (have_acl)
1756 zfs_acl_ids_free(&acl_ids);
1757 error = SET_ERROR(EINVAL);
1758 goto out;
1759 }
1760
1761 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1762 cr, vsecp, &acl_ids)) != 0)
1763 goto out;
1764 have_acl = B_TRUE;
1765
1766 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1767 zfs_acl_ids_free(&acl_ids);
1768 error = SET_ERROR(EDQUOT);
1769 goto out;
1770 }
1771
1772 tx = dmu_tx_create(os);
1773
1774 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1775 ZFS_SA_BASE_ATTR_SIZE);
1776
1777 fuid_dirtied = zfsvfs->z_fuid_dirty;
1778 if (fuid_dirtied)
1779 zfs_fuid_txhold(zfsvfs, tx);
1780 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1781 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1782 if (!zfsvfs->z_use_sa &&
1783 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1784 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1785 0, acl_ids.z_aclp->z_acl_bytes);
1786 }
1787 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1788 if (error) {
1789 zfs_dirent_unlock(dl);
1790 if (error == ERESTART) {
1791 waited = B_TRUE;
1792 dmu_tx_wait(tx);
1793 dmu_tx_abort(tx);
1794 goto top;
1795 }
1796 zfs_acl_ids_free(&acl_ids);
1797 dmu_tx_abort(tx);
1798 getnewvnode_drop_reserve();
1799 ZFS_EXIT(zfsvfs);
1800 return (error);
1801 }
1802 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1803
1804 if (fuid_dirtied)
1805 zfs_fuid_sync(zfsvfs, tx);
1806
1807 (void) zfs_link_create(dl, zp, tx, ZNEW);
1808 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1809 if (flag & FIGNORECASE)
1810 txtype |= TX_CI;
1811 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1812 vsecp, acl_ids.z_fuidp, vap);
1813 zfs_acl_ids_free(&acl_ids);
1814 dmu_tx_commit(tx);
1815 } else {
1816 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1817
1818 if (have_acl)
1819 zfs_acl_ids_free(&acl_ids);
1820 have_acl = B_FALSE;
1821
1822 /*
1823 * A directory entry already exists for this name.
1824 */
1825 /*
1826 * Can't truncate an existing file if in exclusive mode.
1827 */
1828 if (excl == EXCL) {
1829 error = SET_ERROR(EEXIST);
1830 goto out;
1831 }
1832 /*
1833 * Can't open a directory for writing.
1834 */
1835 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1836 error = SET_ERROR(EISDIR);
1837 goto out;
1838 }
1839 /*
1840 * Verify requested access to file.
1841 */
1842 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1843 goto out;
1844 }
1845
1846 mutex_enter(&dzp->z_lock);
1847 dzp->z_seq++;
1848 mutex_exit(&dzp->z_lock);
1849
1850 /*
1851 * Truncate regular files if requested.
1852 */
1853 if ((ZTOV(zp)->v_type == VREG) &&
1854 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1855 /* we can't hold any locks when calling zfs_freesp() */
1856 zfs_dirent_unlock(dl);
1857 dl = NULL;
1858 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1859 if (error == 0) {
1860 vnevent_create(ZTOV(zp), ct);
1861 }
1862 }
1863 }
1864 out:
1865 getnewvnode_drop_reserve();
1866 if (dl)
1867 zfs_dirent_unlock(dl);
1868
1869 if (error) {
1870 if (zp)
1871 VN_RELE(ZTOV(zp));
1872 } else {
1873 *vpp = ZTOV(zp);
1874 error = specvp_check(vpp, cr);
1875 }
1876
1877 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1878 zil_commit(zilog, 0);
1879
1880 ZFS_EXIT(zfsvfs);
1881 return (error);
1882 }
1883
1884 /*
1885 * Remove an entry from a directory.
1886 *
1887 * IN: dvp - vnode of directory to remove entry from.
1888 * name - name of entry to remove.
1889 * cr - credentials of caller.
1890 * ct - caller context
1891 * flags - case flags
1892 *
1893 * RETURN: 0 on success, error code on failure.
1894 *
1895 * Timestamps:
1896 * dvp - ctime|mtime
1897 * vp - ctime (if nlink > 0)
1898 */
1899
1900 uint64_t null_xattr = 0;
1901
1902 /*ARGSUSED*/
1903 static int
zfs_remove(vnode_t * dvp,char * name,cred_t * cr,caller_context_t * ct,int flags)1904 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1905 int flags)
1906 {
1907 znode_t *zp, *dzp = VTOZ(dvp);
1908 znode_t *xzp;
1909 vnode_t *vp;
1910 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1911 zilog_t *zilog;
1912 uint64_t acl_obj, xattr_obj;
1913 uint64_t xattr_obj_unlinked = 0;
1914 uint64_t obj = 0;
1915 zfs_dirlock_t *dl;
1916 dmu_tx_t *tx;
1917 boolean_t may_delete_now, delete_now = FALSE;
1918 boolean_t unlinked, toobig = FALSE;
1919 uint64_t txtype;
1920 pathname_t *realnmp = NULL;
1921 pathname_t realnm;
1922 int error;
1923 int zflg = ZEXISTS;
1924 boolean_t waited = B_FALSE;
1925
1926 ZFS_ENTER(zfsvfs);
1927 ZFS_VERIFY_ZP(dzp);
1928 zilog = zfsvfs->z_log;
1929
1930 if (flags & FIGNORECASE) {
1931 zflg |= ZCILOOK;
1932 pn_alloc(&realnm);
1933 realnmp = &realnm;
1934 }
1935
1936 top:
1937 xattr_obj = 0;
1938 xzp = NULL;
1939 /*
1940 * Attempt to lock directory; fail if entry doesn't exist.
1941 */
1942 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1943 NULL, realnmp)) {
1944 if (realnmp)
1945 pn_free(realnmp);
1946 ZFS_EXIT(zfsvfs);
1947 return (error);
1948 }
1949
1950 vp = ZTOV(zp);
1951
1952 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1953 goto out;
1954 }
1955
1956 /*
1957 * Need to use rmdir for removing directories.
1958 */
1959 if (vp->v_type == VDIR) {
1960 error = SET_ERROR(EPERM);
1961 goto out;
1962 }
1963
1964 vnevent_remove(vp, dvp, name, ct);
1965
1966 if (realnmp)
1967 dnlc_remove(dvp, realnmp->pn_buf);
1968 else
1969 dnlc_remove(dvp, name);
1970
1971 VI_LOCK(vp);
1972 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1973 VI_UNLOCK(vp);
1974
1975 /*
1976 * We may delete the znode now, or we may put it in the unlinked set;
1977 * it depends on whether we're the last link, and on whether there are
1978 * other holds on the vnode. So we dmu_tx_hold() the right things to
1979 * allow for either case.
1980 */
1981 obj = zp->z_id;
1982 tx = dmu_tx_create(zfsvfs->z_os);
1983 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1984 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1985 zfs_sa_upgrade_txholds(tx, zp);
1986 zfs_sa_upgrade_txholds(tx, dzp);
1987 if (may_delete_now) {
1988 toobig =
1989 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1990 /* if the file is too big, only hold_free a token amount */
1991 dmu_tx_hold_free(tx, zp->z_id, 0,
1992 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1993 }
1994
1995 /* are there any extended attributes? */
1996 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1997 &xattr_obj, sizeof (xattr_obj));
1998 if (error == 0 && xattr_obj) {
1999 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
2000 ASSERT0(error);
2001 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2002 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
2003 }
2004
2005 mutex_enter(&zp->z_lock);
2006 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
2007 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
2008 mutex_exit(&zp->z_lock);
2009
2010 /* charge as an update -- would be nice not to charge at all */
2011 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2012
2013 /*
2014 * Mark this transaction as typically resulting in a net free of
2015 * space, unless object removal will be delayed indefinitely
2016 * (due to active holds on the vnode due to the file being open).
2017 */
2018 if (may_delete_now)
2019 dmu_tx_mark_netfree(tx);
2020
2021 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2022 if (error) {
2023 zfs_dirent_unlock(dl);
2024 VN_RELE(vp);
2025 if (xzp)
2026 VN_RELE(ZTOV(xzp));
2027 if (error == ERESTART) {
2028 waited = B_TRUE;
2029 dmu_tx_wait(tx);
2030 dmu_tx_abort(tx);
2031 goto top;
2032 }
2033 if (realnmp)
2034 pn_free(realnmp);
2035 dmu_tx_abort(tx);
2036 ZFS_EXIT(zfsvfs);
2037 return (error);
2038 }
2039
2040 /*
2041 * Remove the directory entry.
2042 */
2043 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
2044
2045 if (error) {
2046 dmu_tx_commit(tx);
2047 goto out;
2048 }
2049
2050 if (unlinked) {
2051 /*
2052 * Hold z_lock so that we can make sure that the ACL obj
2053 * hasn't changed. Could have been deleted due to
2054 * zfs_sa_upgrade().
2055 */
2056 mutex_enter(&zp->z_lock);
2057 VI_LOCK(vp);
2058 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2059 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
2060 delete_now = may_delete_now && !toobig &&
2061 vp->v_count == 1 && !vn_has_cached_data(vp) &&
2062 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2063 acl_obj;
2064 VI_UNLOCK(vp);
2065 }
2066
2067 if (delete_now) {
2068 #ifdef __FreeBSD__
2069 panic("zfs_remove: delete_now branch taken");
2070 #endif
2071 if (xattr_obj_unlinked) {
2072 ASSERT3U(xzp->z_links, ==, 2);
2073 mutex_enter(&xzp->z_lock);
2074 xzp->z_unlinked = 1;
2075 xzp->z_links = 0;
2076 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2077 &xzp->z_links, sizeof (xzp->z_links), tx);
2078 ASSERT3U(error, ==, 0);
2079 mutex_exit(&xzp->z_lock);
2080 zfs_unlinked_add(xzp, tx);
2081
2082 if (zp->z_is_sa)
2083 error = sa_remove(zp->z_sa_hdl,
2084 SA_ZPL_XATTR(zfsvfs), tx);
2085 else
2086 error = sa_update(zp->z_sa_hdl,
2087 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2088 sizeof (uint64_t), tx);
2089 ASSERT0(error);
2090 }
2091 VI_LOCK(vp);
2092 vp->v_count--;
2093 ASSERT0(vp->v_count);
2094 VI_UNLOCK(vp);
2095 mutex_exit(&zp->z_lock);
2096 zfs_znode_delete(zp, tx);
2097 } else if (unlinked) {
2098 mutex_exit(&zp->z_lock);
2099 zfs_unlinked_add(zp, tx);
2100 #ifdef __FreeBSD__
2101 vp->v_vflag |= VV_NOSYNC;
2102 #endif
2103 }
2104
2105 txtype = TX_REMOVE;
2106 if (flags & FIGNORECASE)
2107 txtype |= TX_CI;
2108 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2109
2110 dmu_tx_commit(tx);
2111 out:
2112 if (realnmp)
2113 pn_free(realnmp);
2114
2115 zfs_dirent_unlock(dl);
2116
2117 if (!delete_now)
2118 VN_RELE(vp);
2119 if (xzp)
2120 VN_RELE(ZTOV(xzp));
2121
2122 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2123 zil_commit(zilog, 0);
2124
2125 ZFS_EXIT(zfsvfs);
2126 return (error);
2127 }
2128
2129 /*
2130 * Create a new directory and insert it into dvp using the name
2131 * provided. Return a pointer to the inserted directory.
2132 *
2133 * IN: dvp - vnode of directory to add subdir to.
2134 * dirname - name of new directory.
2135 * vap - attributes of new directory.
2136 * cr - credentials of caller.
2137 * ct - caller context
2138 * flags - case flags
2139 * vsecp - ACL to be set
2140 *
2141 * OUT: vpp - vnode of created directory.
2142 *
2143 * RETURN: 0 on success, error code on failure.
2144 *
2145 * Timestamps:
2146 * dvp - ctime|mtime updated
2147 * vp - ctime|mtime|atime updated
2148 */
2149 /*ARGSUSED*/
2150 static int
zfs_mkdir(vnode_t * dvp,char * dirname,vattr_t * vap,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)2151 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2152 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2153 {
2154 znode_t *zp, *dzp = VTOZ(dvp);
2155 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2156 zilog_t *zilog;
2157 zfs_dirlock_t *dl;
2158 uint64_t txtype;
2159 dmu_tx_t *tx;
2160 int error;
2161 int zf = ZNEW;
2162 ksid_t *ksid;
2163 uid_t uid;
2164 gid_t gid = crgetgid(cr);
2165 zfs_acl_ids_t acl_ids;
2166 boolean_t fuid_dirtied;
2167 boolean_t waited = B_FALSE;
2168
2169 ASSERT(vap->va_type == VDIR);
2170
2171 /*
2172 * If we have an ephemeral id, ACL, or XVATTR then
2173 * make sure file system is at proper version
2174 */
2175
2176 ksid = crgetsid(cr, KSID_OWNER);
2177 if (ksid)
2178 uid = ksid_getid(ksid);
2179 else
2180 uid = crgetuid(cr);
2181 if (zfsvfs->z_use_fuids == B_FALSE &&
2182 (vsecp || (vap->va_mask & AT_XVATTR) ||
2183 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2184 return (SET_ERROR(EINVAL));
2185
2186 ZFS_ENTER(zfsvfs);
2187 ZFS_VERIFY_ZP(dzp);
2188 zilog = zfsvfs->z_log;
2189
2190 if (dzp->z_pflags & ZFS_XATTR) {
2191 ZFS_EXIT(zfsvfs);
2192 return (SET_ERROR(EINVAL));
2193 }
2194
2195 if (zfsvfs->z_utf8 && u8_validate(dirname,
2196 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2197 ZFS_EXIT(zfsvfs);
2198 return (SET_ERROR(EILSEQ));
2199 }
2200 if (flags & FIGNORECASE)
2201 zf |= ZCILOOK;
2202
2203 if (vap->va_mask & AT_XVATTR) {
2204 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2205 crgetuid(cr), cr, vap->va_type)) != 0) {
2206 ZFS_EXIT(zfsvfs);
2207 return (error);
2208 }
2209 }
2210
2211 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2212 vsecp, &acl_ids)) != 0) {
2213 ZFS_EXIT(zfsvfs);
2214 return (error);
2215 }
2216
2217 getnewvnode_reserve(1);
2218
2219 /*
2220 * First make sure the new directory doesn't exist.
2221 *
2222 * Existence is checked first to make sure we don't return
2223 * EACCES instead of EEXIST which can cause some applications
2224 * to fail.
2225 */
2226 top:
2227 *vpp = NULL;
2228
2229 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2230 NULL, NULL)) {
2231 zfs_acl_ids_free(&acl_ids);
2232 getnewvnode_drop_reserve();
2233 ZFS_EXIT(zfsvfs);
2234 return (error);
2235 }
2236
2237 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2238 zfs_acl_ids_free(&acl_ids);
2239 zfs_dirent_unlock(dl);
2240 getnewvnode_drop_reserve();
2241 ZFS_EXIT(zfsvfs);
2242 return (error);
2243 }
2244
2245 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2246 zfs_acl_ids_free(&acl_ids);
2247 zfs_dirent_unlock(dl);
2248 getnewvnode_drop_reserve();
2249 ZFS_EXIT(zfsvfs);
2250 return (SET_ERROR(EDQUOT));
2251 }
2252
2253 /*
2254 * Add a new entry to the directory.
2255 */
2256 tx = dmu_tx_create(zfsvfs->z_os);
2257 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2258 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2259 fuid_dirtied = zfsvfs->z_fuid_dirty;
2260 if (fuid_dirtied)
2261 zfs_fuid_txhold(zfsvfs, tx);
2262 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2263 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2264 acl_ids.z_aclp->z_acl_bytes);
2265 }
2266
2267 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2268 ZFS_SA_BASE_ATTR_SIZE);
2269
2270 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2271 if (error) {
2272 zfs_dirent_unlock(dl);
2273 if (error == ERESTART) {
2274 waited = B_TRUE;
2275 dmu_tx_wait(tx);
2276 dmu_tx_abort(tx);
2277 goto top;
2278 }
2279 zfs_acl_ids_free(&acl_ids);
2280 dmu_tx_abort(tx);
2281 getnewvnode_drop_reserve();
2282 ZFS_EXIT(zfsvfs);
2283 return (error);
2284 }
2285
2286 /*
2287 * Create new node.
2288 */
2289 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2290
2291 if (fuid_dirtied)
2292 zfs_fuid_sync(zfsvfs, tx);
2293
2294 /*
2295 * Now put new name in parent dir.
2296 */
2297 (void) zfs_link_create(dl, zp, tx, ZNEW);
2298
2299 *vpp = ZTOV(zp);
2300
2301 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2302 if (flags & FIGNORECASE)
2303 txtype |= TX_CI;
2304 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2305 acl_ids.z_fuidp, vap);
2306
2307 zfs_acl_ids_free(&acl_ids);
2308
2309 dmu_tx_commit(tx);
2310
2311 getnewvnode_drop_reserve();
2312
2313 zfs_dirent_unlock(dl);
2314
2315 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2316 zil_commit(zilog, 0);
2317
2318 ZFS_EXIT(zfsvfs);
2319 return (0);
2320 }
2321
2322 /*
2323 * Remove a directory subdir entry. If the current working
2324 * directory is the same as the subdir to be removed, the
2325 * remove will fail.
2326 *
2327 * IN: dvp - vnode of directory to remove from.
2328 * name - name of directory to be removed.
2329 * cwd - vnode of current working directory.
2330 * cr - credentials of caller.
2331 * ct - caller context
2332 * flags - case flags
2333 *
2334 * RETURN: 0 on success, error code on failure.
2335 *
2336 * Timestamps:
2337 * dvp - ctime|mtime updated
2338 */
2339 /*ARGSUSED*/
2340 static int
zfs_rmdir(vnode_t * dvp,char * name,vnode_t * cwd,cred_t * cr,caller_context_t * ct,int flags)2341 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2342 caller_context_t *ct, int flags)
2343 {
2344 znode_t *dzp = VTOZ(dvp);
2345 znode_t *zp;
2346 vnode_t *vp;
2347 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2348 zilog_t *zilog;
2349 zfs_dirlock_t *dl;
2350 dmu_tx_t *tx;
2351 int error;
2352 int zflg = ZEXISTS;
2353 boolean_t waited = B_FALSE;
2354
2355 ZFS_ENTER(zfsvfs);
2356 ZFS_VERIFY_ZP(dzp);
2357 zilog = zfsvfs->z_log;
2358
2359 if (flags & FIGNORECASE)
2360 zflg |= ZCILOOK;
2361 top:
2362 zp = NULL;
2363
2364 /*
2365 * Attempt to lock directory; fail if entry doesn't exist.
2366 */
2367 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2368 NULL, NULL)) {
2369 ZFS_EXIT(zfsvfs);
2370 return (error);
2371 }
2372
2373 vp = ZTOV(zp);
2374
2375 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2376 goto out;
2377 }
2378
2379 if (vp->v_type != VDIR) {
2380 error = SET_ERROR(ENOTDIR);
2381 goto out;
2382 }
2383
2384 if (vp == cwd) {
2385 error = SET_ERROR(EINVAL);
2386 goto out;
2387 }
2388
2389 vnevent_rmdir(vp, dvp, name, ct);
2390
2391 /*
2392 * Grab a lock on the directory to make sure that noone is
2393 * trying to add (or lookup) entries while we are removing it.
2394 */
2395 rw_enter(&zp->z_name_lock, RW_WRITER);
2396
2397 /*
2398 * Grab a lock on the parent pointer to make sure we play well
2399 * with the treewalk and directory rename code.
2400 */
2401 rw_enter(&zp->z_parent_lock, RW_WRITER);
2402
2403 tx = dmu_tx_create(zfsvfs->z_os);
2404 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2405 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2406 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2407 zfs_sa_upgrade_txholds(tx, zp);
2408 zfs_sa_upgrade_txholds(tx, dzp);
2409 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2410 if (error) {
2411 rw_exit(&zp->z_parent_lock);
2412 rw_exit(&zp->z_name_lock);
2413 zfs_dirent_unlock(dl);
2414 VN_RELE(vp);
2415 if (error == ERESTART) {
2416 waited = B_TRUE;
2417 dmu_tx_wait(tx);
2418 dmu_tx_abort(tx);
2419 goto top;
2420 }
2421 dmu_tx_abort(tx);
2422 ZFS_EXIT(zfsvfs);
2423 return (error);
2424 }
2425
2426 #ifdef FREEBSD_NAMECACHE
2427 cache_purge(dvp);
2428 #endif
2429
2430 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2431
2432 if (error == 0) {
2433 uint64_t txtype = TX_RMDIR;
2434 if (flags & FIGNORECASE)
2435 txtype |= TX_CI;
2436 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2437 }
2438
2439 dmu_tx_commit(tx);
2440
2441 rw_exit(&zp->z_parent_lock);
2442 rw_exit(&zp->z_name_lock);
2443 #ifdef FREEBSD_NAMECACHE
2444 cache_purge(vp);
2445 #endif
2446 out:
2447 zfs_dirent_unlock(dl);
2448
2449 VN_RELE(vp);
2450
2451 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2452 zil_commit(zilog, 0);
2453
2454 ZFS_EXIT(zfsvfs);
2455 return (error);
2456 }
2457
2458 /*
2459 * Read as many directory entries as will fit into the provided
2460 * buffer from the given directory cursor position (specified in
2461 * the uio structure).
2462 *
2463 * IN: vp - vnode of directory to read.
2464 * uio - structure supplying read location, range info,
2465 * and return buffer.
2466 * cr - credentials of caller.
2467 * ct - caller context
2468 * flags - case flags
2469 *
2470 * OUT: uio - updated offset and range, buffer filled.
2471 * eofp - set to true if end-of-file detected.
2472 *
2473 * RETURN: 0 on success, error code on failure.
2474 *
2475 * Timestamps:
2476 * vp - atime updated
2477 *
2478 * Note that the low 4 bits of the cookie returned by zap is always zero.
2479 * This allows us to use the low range for "special" directory entries:
2480 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2481 * we use the offset 2 for the '.zfs' directory.
2482 */
2483 /* ARGSUSED */
2484 static int
zfs_readdir(vnode_t * vp,uio_t * uio,cred_t * cr,int * eofp,int * ncookies,u_long ** cookies)2485 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2486 {
2487 znode_t *zp = VTOZ(vp);
2488 iovec_t *iovp;
2489 edirent_t *eodp;
2490 dirent64_t *odp;
2491 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2492 objset_t *os;
2493 caddr_t outbuf;
2494 size_t bufsize;
2495 zap_cursor_t zc;
2496 zap_attribute_t zap;
2497 uint_t bytes_wanted;
2498 uint64_t offset; /* must be unsigned; checks for < 1 */
2499 uint64_t parent;
2500 int local_eof;
2501 int outcount;
2502 int error;
2503 uint8_t prefetch;
2504 boolean_t check_sysattrs;
2505 uint8_t type;
2506 int ncooks;
2507 u_long *cooks = NULL;
2508 int flags = 0;
2509
2510 ZFS_ENTER(zfsvfs);
2511 ZFS_VERIFY_ZP(zp);
2512
2513 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2514 &parent, sizeof (parent))) != 0) {
2515 ZFS_EXIT(zfsvfs);
2516 return (error);
2517 }
2518
2519 /*
2520 * If we are not given an eof variable,
2521 * use a local one.
2522 */
2523 if (eofp == NULL)
2524 eofp = &local_eof;
2525
2526 /*
2527 * Check for valid iov_len.
2528 */
2529 if (uio->uio_iov->iov_len <= 0) {
2530 ZFS_EXIT(zfsvfs);
2531 return (SET_ERROR(EINVAL));
2532 }
2533
2534 /*
2535 * Quit if directory has been removed (posix)
2536 */
2537 if ((*eofp = zp->z_unlinked) != 0) {
2538 ZFS_EXIT(zfsvfs);
2539 return (0);
2540 }
2541
2542 error = 0;
2543 os = zfsvfs->z_os;
2544 offset = uio->uio_loffset;
2545 prefetch = zp->z_zn_prefetch;
2546
2547 /*
2548 * Initialize the iterator cursor.
2549 */
2550 if (offset <= 3) {
2551 /*
2552 * Start iteration from the beginning of the directory.
2553 */
2554 zap_cursor_init(&zc, os, zp->z_id);
2555 } else {
2556 /*
2557 * The offset is a serialized cursor.
2558 */
2559 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2560 }
2561
2562 /*
2563 * Get space to change directory entries into fs independent format.
2564 */
2565 iovp = uio->uio_iov;
2566 bytes_wanted = iovp->iov_len;
2567 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2568 bufsize = bytes_wanted;
2569 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2570 odp = (struct dirent64 *)outbuf;
2571 } else {
2572 bufsize = bytes_wanted;
2573 outbuf = NULL;
2574 odp = (struct dirent64 *)iovp->iov_base;
2575 }
2576 eodp = (struct edirent *)odp;
2577
2578 if (ncookies != NULL) {
2579 /*
2580 * Minimum entry size is dirent size and 1 byte for a file name.
2581 */
2582 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2583 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2584 *cookies = cooks;
2585 *ncookies = ncooks;
2586 }
2587 /*
2588 * If this VFS supports the system attribute view interface; and
2589 * we're looking at an extended attribute directory; and we care
2590 * about normalization conflicts on this vfs; then we must check
2591 * for normalization conflicts with the sysattr name space.
2592 */
2593 #ifdef TODO
2594 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2595 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2596 (flags & V_RDDIR_ENTFLAGS);
2597 #else
2598 check_sysattrs = 0;
2599 #endif
2600
2601 /*
2602 * Transform to file-system independent format
2603 */
2604 outcount = 0;
2605 while (outcount < bytes_wanted) {
2606 ino64_t objnum;
2607 ushort_t reclen;
2608 off64_t *next = NULL;
2609
2610 /*
2611 * Special case `.', `..', and `.zfs'.
2612 */
2613 if (offset == 0) {
2614 (void) strcpy(zap.za_name, ".");
2615 zap.za_normalization_conflict = 0;
2616 objnum = zp->z_id;
2617 type = DT_DIR;
2618 } else if (offset == 1) {
2619 (void) strcpy(zap.za_name, "..");
2620 zap.za_normalization_conflict = 0;
2621 objnum = parent;
2622 type = DT_DIR;
2623 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2624 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2625 zap.za_normalization_conflict = 0;
2626 objnum = ZFSCTL_INO_ROOT;
2627 type = DT_DIR;
2628 } else {
2629 /*
2630 * Grab next entry.
2631 */
2632 if (error = zap_cursor_retrieve(&zc, &zap)) {
2633 if ((*eofp = (error == ENOENT)) != 0)
2634 break;
2635 else
2636 goto update;
2637 }
2638
2639 if (zap.za_integer_length != 8 ||
2640 zap.za_num_integers != 1) {
2641 cmn_err(CE_WARN, "zap_readdir: bad directory "
2642 "entry, obj = %lld, offset = %lld\n",
2643 (u_longlong_t)zp->z_id,
2644 (u_longlong_t)offset);
2645 error = SET_ERROR(ENXIO);
2646 goto update;
2647 }
2648
2649 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2650 /*
2651 * MacOS X can extract the object type here such as:
2652 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2653 */
2654 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2655
2656 if (check_sysattrs && !zap.za_normalization_conflict) {
2657 #ifdef TODO
2658 zap.za_normalization_conflict =
2659 xattr_sysattr_casechk(zap.za_name);
2660 #else
2661 panic("%s:%u: TODO", __func__, __LINE__);
2662 #endif
2663 }
2664 }
2665
2666 if (flags & V_RDDIR_ACCFILTER) {
2667 /*
2668 * If we have no access at all, don't include
2669 * this entry in the returned information
2670 */
2671 znode_t *ezp;
2672 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2673 goto skip_entry;
2674 if (!zfs_has_access(ezp, cr)) {
2675 VN_RELE(ZTOV(ezp));
2676 goto skip_entry;
2677 }
2678 VN_RELE(ZTOV(ezp));
2679 }
2680
2681 if (flags & V_RDDIR_ENTFLAGS)
2682 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2683 else
2684 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2685
2686 /*
2687 * Will this entry fit in the buffer?
2688 */
2689 if (outcount + reclen > bufsize) {
2690 /*
2691 * Did we manage to fit anything in the buffer?
2692 */
2693 if (!outcount) {
2694 error = SET_ERROR(EINVAL);
2695 goto update;
2696 }
2697 break;
2698 }
2699 if (flags & V_RDDIR_ENTFLAGS) {
2700 /*
2701 * Add extended flag entry:
2702 */
2703 eodp->ed_ino = objnum;
2704 eodp->ed_reclen = reclen;
2705 /* NOTE: ed_off is the offset for the *next* entry */
2706 next = &(eodp->ed_off);
2707 eodp->ed_eflags = zap.za_normalization_conflict ?
2708 ED_CASE_CONFLICT : 0;
2709 (void) strncpy(eodp->ed_name, zap.za_name,
2710 EDIRENT_NAMELEN(reclen));
2711 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2712 } else {
2713 /*
2714 * Add normal entry:
2715 */
2716 odp->d_ino = objnum;
2717 odp->d_reclen = reclen;
2718 odp->d_namlen = strlen(zap.za_name);
2719 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2720 odp->d_type = type;
2721 odp = (dirent64_t *)((intptr_t)odp + reclen);
2722 }
2723 outcount += reclen;
2724
2725 ASSERT(outcount <= bufsize);
2726
2727 /* Prefetch znode */
2728 if (prefetch)
2729 dmu_prefetch(os, objnum, 0, 0, 0,
2730 ZIO_PRIORITY_SYNC_READ);
2731
2732 skip_entry:
2733 /*
2734 * Move to the next entry, fill in the previous offset.
2735 */
2736 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2737 zap_cursor_advance(&zc);
2738 offset = zap_cursor_serialize(&zc);
2739 } else {
2740 offset += 1;
2741 }
2742
2743 if (cooks != NULL) {
2744 *cooks++ = offset;
2745 ncooks--;
2746 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2747 }
2748 }
2749 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2750
2751 /* Subtract unused cookies */
2752 if (ncookies != NULL)
2753 *ncookies -= ncooks;
2754
2755 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2756 iovp->iov_base += outcount;
2757 iovp->iov_len -= outcount;
2758 uio->uio_resid -= outcount;
2759 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2760 /*
2761 * Reset the pointer.
2762 */
2763 offset = uio->uio_loffset;
2764 }
2765
2766 update:
2767 zap_cursor_fini(&zc);
2768 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2769 kmem_free(outbuf, bufsize);
2770
2771 if (error == ENOENT)
2772 error = 0;
2773
2774 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2775
2776 uio->uio_loffset = offset;
2777 ZFS_EXIT(zfsvfs);
2778 if (error != 0 && cookies != NULL) {
2779 free(*cookies, M_TEMP);
2780 *cookies = NULL;
2781 *ncookies = 0;
2782 }
2783 return (error);
2784 }
2785
2786 ulong_t zfs_fsync_sync_cnt = 4;
2787
2788 static int
zfs_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)2789 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2790 {
2791 znode_t *zp = VTOZ(vp);
2792 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2793
2794 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2795
2796 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2797 ZFS_ENTER(zfsvfs);
2798 ZFS_VERIFY_ZP(zp);
2799 zil_commit(zfsvfs->z_log, zp->z_id);
2800 ZFS_EXIT(zfsvfs);
2801 }
2802 return (0);
2803 }
2804
2805
2806 /*
2807 * Get the requested file attributes and place them in the provided
2808 * vattr structure.
2809 *
2810 * IN: vp - vnode of file.
2811 * vap - va_mask identifies requested attributes.
2812 * If AT_XVATTR set, then optional attrs are requested
2813 * flags - ATTR_NOACLCHECK (CIFS server context)
2814 * cr - credentials of caller.
2815 * ct - caller context
2816 *
2817 * OUT: vap - attribute values.
2818 *
2819 * RETURN: 0 (always succeeds).
2820 */
2821 /* ARGSUSED */
2822 static int
zfs_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2823 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2824 caller_context_t *ct)
2825 {
2826 znode_t *zp = VTOZ(vp);
2827 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2828 int error = 0;
2829 uint32_t blksize;
2830 u_longlong_t nblocks;
2831 uint64_t links;
2832 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2833 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2834 xoptattr_t *xoap = NULL;
2835 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2836 sa_bulk_attr_t bulk[4];
2837 int count = 0;
2838
2839 ZFS_ENTER(zfsvfs);
2840 ZFS_VERIFY_ZP(zp);
2841
2842 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2843
2844 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2845 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2846 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2847 if (vp->v_type == VBLK || vp->v_type == VCHR)
2848 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2849 &rdev, 8);
2850
2851 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2852 ZFS_EXIT(zfsvfs);
2853 return (error);
2854 }
2855
2856 /*
2857 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2858 * Also, if we are the owner don't bother, since owner should
2859 * always be allowed to read basic attributes of file.
2860 */
2861 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2862 (vap->va_uid != crgetuid(cr))) {
2863 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2864 skipaclchk, cr)) {
2865 ZFS_EXIT(zfsvfs);
2866 return (error);
2867 }
2868 }
2869
2870 /*
2871 * Return all attributes. It's cheaper to provide the answer
2872 * than to determine whether we were asked the question.
2873 */
2874
2875 mutex_enter(&zp->z_lock);
2876 vap->va_type = IFTOVT(zp->z_mode);
2877 vap->va_mode = zp->z_mode & ~S_IFMT;
2878 #ifdef illumos
2879 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2880 #else
2881 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2882 #endif
2883 vap->va_nodeid = zp->z_id;
2884 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2885 links = zp->z_links + 1;
2886 else
2887 links = zp->z_links;
2888 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2889 vap->va_size = zp->z_size;
2890 #ifdef illumos
2891 vap->va_rdev = vp->v_rdev;
2892 #else
2893 if (vp->v_type == VBLK || vp->v_type == VCHR)
2894 vap->va_rdev = zfs_cmpldev(rdev);
2895 #endif
2896 vap->va_seq = zp->z_seq;
2897 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2898 vap->va_filerev = zp->z_seq;
2899
2900 /*
2901 * Add in any requested optional attributes and the create time.
2902 * Also set the corresponding bits in the returned attribute bitmap.
2903 */
2904 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2905 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2906 xoap->xoa_archive =
2907 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2908 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2909 }
2910
2911 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2912 xoap->xoa_readonly =
2913 ((zp->z_pflags & ZFS_READONLY) != 0);
2914 XVA_SET_RTN(xvap, XAT_READONLY);
2915 }
2916
2917 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2918 xoap->xoa_system =
2919 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2920 XVA_SET_RTN(xvap, XAT_SYSTEM);
2921 }
2922
2923 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2924 xoap->xoa_hidden =
2925 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2926 XVA_SET_RTN(xvap, XAT_HIDDEN);
2927 }
2928
2929 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2930 xoap->xoa_nounlink =
2931 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2932 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2933 }
2934
2935 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2936 xoap->xoa_immutable =
2937 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2938 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2939 }
2940
2941 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2942 xoap->xoa_appendonly =
2943 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2944 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2945 }
2946
2947 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2948 xoap->xoa_nodump =
2949 ((zp->z_pflags & ZFS_NODUMP) != 0);
2950 XVA_SET_RTN(xvap, XAT_NODUMP);
2951 }
2952
2953 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2954 xoap->xoa_opaque =
2955 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2956 XVA_SET_RTN(xvap, XAT_OPAQUE);
2957 }
2958
2959 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2960 xoap->xoa_av_quarantined =
2961 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2962 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2963 }
2964
2965 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2966 xoap->xoa_av_modified =
2967 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2968 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2969 }
2970
2971 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2972 vp->v_type == VREG) {
2973 zfs_sa_get_scanstamp(zp, xvap);
2974 }
2975
2976 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2977 uint64_t times[2];
2978
2979 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2980 times, sizeof (times));
2981 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2982 XVA_SET_RTN(xvap, XAT_CREATETIME);
2983 }
2984
2985 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2986 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2987 XVA_SET_RTN(xvap, XAT_REPARSE);
2988 }
2989 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2990 xoap->xoa_generation = zp->z_gen;
2991 XVA_SET_RTN(xvap, XAT_GEN);
2992 }
2993
2994 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2995 xoap->xoa_offline =
2996 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2997 XVA_SET_RTN(xvap, XAT_OFFLINE);
2998 }
2999
3000 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
3001 xoap->xoa_sparse =
3002 ((zp->z_pflags & ZFS_SPARSE) != 0);
3003 XVA_SET_RTN(xvap, XAT_SPARSE);
3004 }
3005 }
3006
3007 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
3008 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
3009 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
3010 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
3011
3012 mutex_exit(&zp->z_lock);
3013
3014 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
3015 vap->va_blksize = blksize;
3016 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
3017
3018 if (zp->z_blksz == 0) {
3019 /*
3020 * Block size hasn't been set; suggest maximal I/O transfers.
3021 */
3022 vap->va_blksize = zfsvfs->z_max_blksz;
3023 }
3024
3025 ZFS_EXIT(zfsvfs);
3026 return (0);
3027 }
3028
3029 /*
3030 * Set the file attributes to the values contained in the
3031 * vattr structure.
3032 *
3033 * IN: vp - vnode of file to be modified.
3034 * vap - new attribute values.
3035 * If AT_XVATTR set, then optional attrs are being set
3036 * flags - ATTR_UTIME set if non-default time values provided.
3037 * - ATTR_NOACLCHECK (CIFS context only).
3038 * cr - credentials of caller.
3039 * ct - caller context
3040 *
3041 * RETURN: 0 on success, error code on failure.
3042 *
3043 * Timestamps:
3044 * vp - ctime updated, mtime updated if size changed.
3045 */
3046 /* ARGSUSED */
3047 static int
zfs_setattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)3048 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
3049 caller_context_t *ct)
3050 {
3051 znode_t *zp = VTOZ(vp);
3052 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3053 zilog_t *zilog;
3054 dmu_tx_t *tx;
3055 vattr_t oldva;
3056 xvattr_t tmpxvattr;
3057 uint_t mask = vap->va_mask;
3058 uint_t saved_mask = 0;
3059 uint64_t saved_mode;
3060 int trim_mask = 0;
3061 uint64_t new_mode;
3062 uint64_t new_uid, new_gid;
3063 uint64_t xattr_obj;
3064 uint64_t mtime[2], ctime[2];
3065 znode_t *attrzp;
3066 int need_policy = FALSE;
3067 int err, err2;
3068 zfs_fuid_info_t *fuidp = NULL;
3069 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
3070 xoptattr_t *xoap;
3071 zfs_acl_t *aclp;
3072 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3073 boolean_t fuid_dirtied = B_FALSE;
3074 sa_bulk_attr_t bulk[7], xattr_bulk[7];
3075 int count = 0, xattr_count = 0;
3076
3077 if (mask == 0)
3078 return (0);
3079
3080 if (mask & AT_NOSET)
3081 return (SET_ERROR(EINVAL));
3082
3083 ZFS_ENTER(zfsvfs);
3084 ZFS_VERIFY_ZP(zp);
3085
3086 zilog = zfsvfs->z_log;
3087
3088 /*
3089 * Make sure that if we have ephemeral uid/gid or xvattr specified
3090 * that file system is at proper version level
3091 */
3092
3093 if (zfsvfs->z_use_fuids == B_FALSE &&
3094 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3095 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3096 (mask & AT_XVATTR))) {
3097 ZFS_EXIT(zfsvfs);
3098 return (SET_ERROR(EINVAL));
3099 }
3100
3101 if (mask & AT_SIZE && vp->v_type == VDIR) {
3102 ZFS_EXIT(zfsvfs);
3103 return (SET_ERROR(EISDIR));
3104 }
3105
3106 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3107 ZFS_EXIT(zfsvfs);
3108 return (SET_ERROR(EINVAL));
3109 }
3110
3111 /*
3112 * If this is an xvattr_t, then get a pointer to the structure of
3113 * optional attributes. If this is NULL, then we have a vattr_t.
3114 */
3115 xoap = xva_getxoptattr(xvap);
3116
3117 xva_init(&tmpxvattr);
3118
3119 /*
3120 * Immutable files can only alter immutable bit and atime
3121 */
3122 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3123 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3124 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3125 ZFS_EXIT(zfsvfs);
3126 return (SET_ERROR(EPERM));
3127 }
3128
3129 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3130 ZFS_EXIT(zfsvfs);
3131 return (SET_ERROR(EPERM));
3132 }
3133
3134 /*
3135 * Verify timestamps doesn't overflow 32 bits.
3136 * ZFS can handle large timestamps, but 32bit syscalls can't
3137 * handle times greater than 2039. This check should be removed
3138 * once large timestamps are fully supported.
3139 */
3140 if (mask & (AT_ATIME | AT_MTIME)) {
3141 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3142 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3143 ZFS_EXIT(zfsvfs);
3144 return (SET_ERROR(EOVERFLOW));
3145 }
3146 }
3147
3148 top:
3149 attrzp = NULL;
3150 aclp = NULL;
3151
3152 /* Can this be moved to before the top label? */
3153 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3154 ZFS_EXIT(zfsvfs);
3155 return (SET_ERROR(EROFS));
3156 }
3157
3158 /*
3159 * First validate permissions
3160 */
3161
3162 if (mask & AT_SIZE) {
3163 /*
3164 * XXX - Note, we are not providing any open
3165 * mode flags here (like FNDELAY), so we may
3166 * block if there are locks present... this
3167 * should be addressed in openat().
3168 */
3169 /* XXX - would it be OK to generate a log record here? */
3170 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3171 if (err) {
3172 ZFS_EXIT(zfsvfs);
3173 return (err);
3174 }
3175 }
3176
3177 if (mask & (AT_ATIME|AT_MTIME) ||
3178 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3179 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3180 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3181 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3182 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3183 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3184 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3185 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3186 skipaclchk, cr);
3187 }
3188
3189 if (mask & (AT_UID|AT_GID)) {
3190 int idmask = (mask & (AT_UID|AT_GID));
3191 int take_owner;
3192 int take_group;
3193
3194 /*
3195 * NOTE: even if a new mode is being set,
3196 * we may clear S_ISUID/S_ISGID bits.
3197 */
3198
3199 if (!(mask & AT_MODE))
3200 vap->va_mode = zp->z_mode;
3201
3202 /*
3203 * Take ownership or chgrp to group we are a member of
3204 */
3205
3206 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3207 take_group = (mask & AT_GID) &&
3208 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3209
3210 /*
3211 * If both AT_UID and AT_GID are set then take_owner and
3212 * take_group must both be set in order to allow taking
3213 * ownership.
3214 *
3215 * Otherwise, send the check through secpolicy_vnode_setattr()
3216 *
3217 */
3218
3219 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3220 ((idmask == AT_UID) && take_owner) ||
3221 ((idmask == AT_GID) && take_group)) {
3222 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3223 skipaclchk, cr) == 0) {
3224 /*
3225 * Remove setuid/setgid for non-privileged users
3226 */
3227 secpolicy_setid_clear(vap, vp, cr);
3228 trim_mask = (mask & (AT_UID|AT_GID));
3229 } else {
3230 need_policy = TRUE;
3231 }
3232 } else {
3233 need_policy = TRUE;
3234 }
3235 }
3236
3237 mutex_enter(&zp->z_lock);
3238 oldva.va_mode = zp->z_mode;
3239 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3240 if (mask & AT_XVATTR) {
3241 /*
3242 * Update xvattr mask to include only those attributes
3243 * that are actually changing.
3244 *
3245 * the bits will be restored prior to actually setting
3246 * the attributes so the caller thinks they were set.
3247 */
3248 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3249 if (xoap->xoa_appendonly !=
3250 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3251 need_policy = TRUE;
3252 } else {
3253 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3254 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3255 }
3256 }
3257
3258 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3259 if (xoap->xoa_nounlink !=
3260 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3261 need_policy = TRUE;
3262 } else {
3263 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3264 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3265 }
3266 }
3267
3268 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3269 if (xoap->xoa_immutable !=
3270 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3271 need_policy = TRUE;
3272 } else {
3273 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3274 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3275 }
3276 }
3277
3278 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3279 if (xoap->xoa_nodump !=
3280 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3281 need_policy = TRUE;
3282 } else {
3283 XVA_CLR_REQ(xvap, XAT_NODUMP);
3284 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3285 }
3286 }
3287
3288 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3289 if (xoap->xoa_av_modified !=
3290 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3291 need_policy = TRUE;
3292 } else {
3293 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3294 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3295 }
3296 }
3297
3298 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3299 if ((vp->v_type != VREG &&
3300 xoap->xoa_av_quarantined) ||
3301 xoap->xoa_av_quarantined !=
3302 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3303 need_policy = TRUE;
3304 } else {
3305 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3306 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3307 }
3308 }
3309
3310 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3311 mutex_exit(&zp->z_lock);
3312 ZFS_EXIT(zfsvfs);
3313 return (SET_ERROR(EPERM));
3314 }
3315
3316 if (need_policy == FALSE &&
3317 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3318 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3319 need_policy = TRUE;
3320 }
3321 }
3322
3323 mutex_exit(&zp->z_lock);
3324
3325 if (mask & AT_MODE) {
3326 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3327 err = secpolicy_setid_setsticky_clear(vp, vap,
3328 &oldva, cr);
3329 if (err) {
3330 ZFS_EXIT(zfsvfs);
3331 return (err);
3332 }
3333 trim_mask |= AT_MODE;
3334 } else {
3335 need_policy = TRUE;
3336 }
3337 }
3338
3339 if (need_policy) {
3340 /*
3341 * If trim_mask is set then take ownership
3342 * has been granted or write_acl is present and user
3343 * has the ability to modify mode. In that case remove
3344 * UID|GID and or MODE from mask so that
3345 * secpolicy_vnode_setattr() doesn't revoke it.
3346 */
3347
3348 if (trim_mask) {
3349 saved_mask = vap->va_mask;
3350 vap->va_mask &= ~trim_mask;
3351 if (trim_mask & AT_MODE) {
3352 /*
3353 * Save the mode, as secpolicy_vnode_setattr()
3354 * will overwrite it with ova.va_mode.
3355 */
3356 saved_mode = vap->va_mode;
3357 }
3358 }
3359 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3360 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3361 if (err) {
3362 ZFS_EXIT(zfsvfs);
3363 return (err);
3364 }
3365
3366 if (trim_mask) {
3367 vap->va_mask |= saved_mask;
3368 if (trim_mask & AT_MODE) {
3369 /*
3370 * Recover the mode after
3371 * secpolicy_vnode_setattr().
3372 */
3373 vap->va_mode = saved_mode;
3374 }
3375 }
3376 }
3377
3378 /*
3379 * secpolicy_vnode_setattr, or take ownership may have
3380 * changed va_mask
3381 */
3382 mask = vap->va_mask;
3383
3384 if ((mask & (AT_UID | AT_GID))) {
3385 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3386 &xattr_obj, sizeof (xattr_obj));
3387
3388 if (err == 0 && xattr_obj) {
3389 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3390 if (err)
3391 goto out2;
3392 }
3393 if (mask & AT_UID) {
3394 new_uid = zfs_fuid_create(zfsvfs,
3395 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3396 if (new_uid != zp->z_uid &&
3397 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3398 if (attrzp)
3399 VN_RELE(ZTOV(attrzp));
3400 err = SET_ERROR(EDQUOT);
3401 goto out2;
3402 }
3403 }
3404
3405 if (mask & AT_GID) {
3406 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3407 cr, ZFS_GROUP, &fuidp);
3408 if (new_gid != zp->z_gid &&
3409 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3410 if (attrzp)
3411 VN_RELE(ZTOV(attrzp));
3412 err = SET_ERROR(EDQUOT);
3413 goto out2;
3414 }
3415 }
3416 }
3417 tx = dmu_tx_create(zfsvfs->z_os);
3418
3419 if (mask & AT_MODE) {
3420 uint64_t pmode = zp->z_mode;
3421 uint64_t acl_obj;
3422 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3423
3424 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3425 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3426 err = SET_ERROR(EPERM);
3427 goto out;
3428 }
3429
3430 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3431 goto out;
3432
3433 mutex_enter(&zp->z_lock);
3434 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3435 /*
3436 * Are we upgrading ACL from old V0 format
3437 * to V1 format?
3438 */
3439 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3440 zfs_znode_acl_version(zp) ==
3441 ZFS_ACL_VERSION_INITIAL) {
3442 dmu_tx_hold_free(tx, acl_obj, 0,
3443 DMU_OBJECT_END);
3444 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3445 0, aclp->z_acl_bytes);
3446 } else {
3447 dmu_tx_hold_write(tx, acl_obj, 0,
3448 aclp->z_acl_bytes);
3449 }
3450 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3451 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3452 0, aclp->z_acl_bytes);
3453 }
3454 mutex_exit(&zp->z_lock);
3455 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3456 } else {
3457 if ((mask & AT_XVATTR) &&
3458 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3459 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3460 else
3461 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3462 }
3463
3464 if (attrzp) {
3465 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3466 }
3467
3468 fuid_dirtied = zfsvfs->z_fuid_dirty;
3469 if (fuid_dirtied)
3470 zfs_fuid_txhold(zfsvfs, tx);
3471
3472 zfs_sa_upgrade_txholds(tx, zp);
3473
3474 err = dmu_tx_assign(tx, TXG_WAIT);
3475 if (err)
3476 goto out;
3477
3478 count = 0;
3479 /*
3480 * Set each attribute requested.
3481 * We group settings according to the locks they need to acquire.
3482 *
3483 * Note: you cannot set ctime directly, although it will be
3484 * updated as a side-effect of calling this function.
3485 */
3486
3487
3488 if (mask & (AT_UID|AT_GID|AT_MODE))
3489 mutex_enter(&zp->z_acl_lock);
3490 mutex_enter(&zp->z_lock);
3491
3492 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3493 &zp->z_pflags, sizeof (zp->z_pflags));
3494
3495 if (attrzp) {
3496 if (mask & (AT_UID|AT_GID|AT_MODE))
3497 mutex_enter(&attrzp->z_acl_lock);
3498 mutex_enter(&attrzp->z_lock);
3499 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3500 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3501 sizeof (attrzp->z_pflags));
3502 }
3503
3504 if (mask & (AT_UID|AT_GID)) {
3505
3506 if (mask & AT_UID) {
3507 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3508 &new_uid, sizeof (new_uid));
3509 zp->z_uid = new_uid;
3510 if (attrzp) {
3511 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3512 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3513 sizeof (new_uid));
3514 attrzp->z_uid = new_uid;
3515 }
3516 }
3517
3518 if (mask & AT_GID) {
3519 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3520 NULL, &new_gid, sizeof (new_gid));
3521 zp->z_gid = new_gid;
3522 if (attrzp) {
3523 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3524 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3525 sizeof (new_gid));
3526 attrzp->z_gid = new_gid;
3527 }
3528 }
3529 if (!(mask & AT_MODE)) {
3530 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3531 NULL, &new_mode, sizeof (new_mode));
3532 new_mode = zp->z_mode;
3533 }
3534 err = zfs_acl_chown_setattr(zp);
3535 ASSERT(err == 0);
3536 if (attrzp) {
3537 err = zfs_acl_chown_setattr(attrzp);
3538 ASSERT(err == 0);
3539 }
3540 }
3541
3542 if (mask & AT_MODE) {
3543 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3544 &new_mode, sizeof (new_mode));
3545 zp->z_mode = new_mode;
3546 ASSERT3U((uintptr_t)aclp, !=, 0);
3547 err = zfs_aclset_common(zp, aclp, cr, tx);
3548 ASSERT0(err);
3549 if (zp->z_acl_cached)
3550 zfs_acl_free(zp->z_acl_cached);
3551 zp->z_acl_cached = aclp;
3552 aclp = NULL;
3553 }
3554
3555
3556 if (mask & AT_ATIME) {
3557 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3559 &zp->z_atime, sizeof (zp->z_atime));
3560 }
3561
3562 if (mask & AT_MTIME) {
3563 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3564 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3565 mtime, sizeof (mtime));
3566 }
3567
3568 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3569 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3570 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3571 NULL, mtime, sizeof (mtime));
3572 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3573 &ctime, sizeof (ctime));
3574 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3575 B_TRUE);
3576 } else if (mask != 0) {
3577 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3578 &ctime, sizeof (ctime));
3579 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3580 B_TRUE);
3581 if (attrzp) {
3582 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3583 SA_ZPL_CTIME(zfsvfs), NULL,
3584 &ctime, sizeof (ctime));
3585 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3586 mtime, ctime, B_TRUE);
3587 }
3588 }
3589 /*
3590 * Do this after setting timestamps to prevent timestamp
3591 * update from toggling bit
3592 */
3593
3594 if (xoap && (mask & AT_XVATTR)) {
3595
3596 /*
3597 * restore trimmed off masks
3598 * so that return masks can be set for caller.
3599 */
3600
3601 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3602 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3603 }
3604 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3605 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3606 }
3607 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3608 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3609 }
3610 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3611 XVA_SET_REQ(xvap, XAT_NODUMP);
3612 }
3613 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3614 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3615 }
3616 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3617 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3618 }
3619
3620 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3621 ASSERT(vp->v_type == VREG);
3622
3623 zfs_xvattr_set(zp, xvap, tx);
3624 }
3625
3626 if (fuid_dirtied)
3627 zfs_fuid_sync(zfsvfs, tx);
3628
3629 if (mask != 0)
3630 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3631
3632 mutex_exit(&zp->z_lock);
3633 if (mask & (AT_UID|AT_GID|AT_MODE))
3634 mutex_exit(&zp->z_acl_lock);
3635
3636 if (attrzp) {
3637 if (mask & (AT_UID|AT_GID|AT_MODE))
3638 mutex_exit(&attrzp->z_acl_lock);
3639 mutex_exit(&attrzp->z_lock);
3640 }
3641 out:
3642 if (err == 0 && attrzp) {
3643 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3644 xattr_count, tx);
3645 ASSERT(err2 == 0);
3646 }
3647
3648 if (attrzp)
3649 VN_RELE(ZTOV(attrzp));
3650
3651 if (aclp)
3652 zfs_acl_free(aclp);
3653
3654 if (fuidp) {
3655 zfs_fuid_info_free(fuidp);
3656 fuidp = NULL;
3657 }
3658
3659 if (err) {
3660 dmu_tx_abort(tx);
3661 if (err == ERESTART)
3662 goto top;
3663 } else {
3664 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3665 dmu_tx_commit(tx);
3666 }
3667
3668 out2:
3669 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3670 zil_commit(zilog, 0);
3671
3672 ZFS_EXIT(zfsvfs);
3673 return (err);
3674 }
3675
3676 typedef struct zfs_zlock {
3677 krwlock_t *zl_rwlock; /* lock we acquired */
3678 znode_t *zl_znode; /* znode we held */
3679 struct zfs_zlock *zl_next; /* next in list */
3680 } zfs_zlock_t;
3681
3682 /*
3683 * Drop locks and release vnodes that were held by zfs_rename_lock().
3684 */
3685 static void
zfs_rename_unlock(zfs_zlock_t ** zlpp)3686 zfs_rename_unlock(zfs_zlock_t **zlpp)
3687 {
3688 zfs_zlock_t *zl;
3689
3690 while ((zl = *zlpp) != NULL) {
3691 if (zl->zl_znode != NULL)
3692 VN_RELE(ZTOV(zl->zl_znode));
3693 rw_exit(zl->zl_rwlock);
3694 *zlpp = zl->zl_next;
3695 kmem_free(zl, sizeof (*zl));
3696 }
3697 }
3698
3699 /*
3700 * Search back through the directory tree, using the ".." entries.
3701 * Lock each directory in the chain to prevent concurrent renames.
3702 * Fail any attempt to move a directory into one of its own descendants.
3703 * XXX - z_parent_lock can overlap with map or grow locks
3704 */
3705 static int
zfs_rename_lock(znode_t * szp,znode_t * tdzp,znode_t * sdzp,zfs_zlock_t ** zlpp)3706 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3707 {
3708 zfs_zlock_t *zl;
3709 znode_t *zp = tdzp;
3710 uint64_t rootid = zp->z_zfsvfs->z_root;
3711 uint64_t oidp = zp->z_id;
3712 krwlock_t *rwlp = &szp->z_parent_lock;
3713 krw_t rw = RW_WRITER;
3714
3715 /*
3716 * First pass write-locks szp and compares to zp->z_id.
3717 * Later passes read-lock zp and compare to zp->z_parent.
3718 */
3719 do {
3720 if (!rw_tryenter(rwlp, rw)) {
3721 /*
3722 * Another thread is renaming in this path.
3723 * Note that if we are a WRITER, we don't have any
3724 * parent_locks held yet.
3725 */
3726 if (rw == RW_READER && zp->z_id > szp->z_id) {
3727 /*
3728 * Drop our locks and restart
3729 */
3730 zfs_rename_unlock(&zl);
3731 *zlpp = NULL;
3732 zp = tdzp;
3733 oidp = zp->z_id;
3734 rwlp = &szp->z_parent_lock;
3735 rw = RW_WRITER;
3736 continue;
3737 } else {
3738 /*
3739 * Wait for other thread to drop its locks
3740 */
3741 rw_enter(rwlp, rw);
3742 }
3743 }
3744
3745 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3746 zl->zl_rwlock = rwlp;
3747 zl->zl_znode = NULL;
3748 zl->zl_next = *zlpp;
3749 *zlpp = zl;
3750
3751 if (oidp == szp->z_id) /* We're a descendant of szp */
3752 return (SET_ERROR(EINVAL));
3753
3754 if (oidp == rootid) /* We've hit the top */
3755 return (0);
3756
3757 if (rw == RW_READER) { /* i.e. not the first pass */
3758 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3759 if (error)
3760 return (error);
3761 zl->zl_znode = zp;
3762 }
3763 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3764 &oidp, sizeof (oidp));
3765 rwlp = &zp->z_parent_lock;
3766 rw = RW_READER;
3767
3768 } while (zp->z_id != sdzp->z_id);
3769
3770 return (0);
3771 }
3772
3773 /*
3774 * Move an entry from the provided source directory to the target
3775 * directory. Change the entry name as indicated.
3776 *
3777 * IN: sdvp - Source directory containing the "old entry".
3778 * snm - Old entry name.
3779 * tdvp - Target directory to contain the "new entry".
3780 * tnm - New entry name.
3781 * cr - credentials of caller.
3782 * ct - caller context
3783 * flags - case flags
3784 *
3785 * RETURN: 0 on success, error code on failure.
3786 *
3787 * Timestamps:
3788 * sdvp,tdvp - ctime|mtime updated
3789 */
3790 /*ARGSUSED*/
3791 static int
zfs_rename(vnode_t * sdvp,char * snm,vnode_t * tdvp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)3792 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3793 caller_context_t *ct, int flags)
3794 {
3795 znode_t *tdzp, *sdzp, *szp, *tzp;
3796 zfsvfs_t *zfsvfs;
3797 zilog_t *zilog;
3798 vnode_t *realvp;
3799 zfs_dirlock_t *sdl, *tdl;
3800 dmu_tx_t *tx;
3801 zfs_zlock_t *zl;
3802 int cmp, serr, terr;
3803 int error = 0;
3804 int zflg = 0;
3805 boolean_t waited = B_FALSE;
3806
3807 tdzp = VTOZ(tdvp);
3808 ZFS_VERIFY_ZP(tdzp);
3809 zfsvfs = tdzp->z_zfsvfs;
3810 ZFS_ENTER(zfsvfs);
3811 zilog = zfsvfs->z_log;
3812 sdzp = VTOZ(sdvp);
3813
3814 /*
3815 * In case sdzp is not valid, let's be sure to exit from the right
3816 * zfsvfs_t.
3817 */
3818 if (sdzp->z_sa_hdl == NULL) {
3819 ZFS_EXIT(zfsvfs);
3820 return (SET_ERROR(EIO));
3821 }
3822
3823 /*
3824 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3825 * ctldir appear to have the same v_vfsp.
3826 */
3827 if (sdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3828 ZFS_EXIT(zfsvfs);
3829 return (SET_ERROR(EXDEV));
3830 }
3831
3832 if (zfsvfs->z_utf8 && u8_validate(tnm,
3833 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3834 ZFS_EXIT(zfsvfs);
3835 return (SET_ERROR(EILSEQ));
3836 }
3837
3838 if (flags & FIGNORECASE)
3839 zflg |= ZCILOOK;
3840
3841 top:
3842 szp = NULL;
3843 tzp = NULL;
3844 zl = NULL;
3845
3846 /*
3847 * This is to prevent the creation of links into attribute space
3848 * by renaming a linked file into/outof an attribute directory.
3849 * See the comment in zfs_link() for why this is considered bad.
3850 */
3851 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3852 ZFS_EXIT(zfsvfs);
3853 return (SET_ERROR(EINVAL));
3854 }
3855
3856 /*
3857 * Lock source and target directory entries. To prevent deadlock,
3858 * a lock ordering must be defined. We lock the directory with
3859 * the smallest object id first, or if it's a tie, the one with
3860 * the lexically first name.
3861 */
3862 if (sdzp->z_id < tdzp->z_id) {
3863 cmp = -1;
3864 } else if (sdzp->z_id > tdzp->z_id) {
3865 cmp = 1;
3866 } else {
3867 /*
3868 * First compare the two name arguments without
3869 * considering any case folding.
3870 */
3871 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3872
3873 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3874 ASSERT(error == 0 || !zfsvfs->z_utf8);
3875 if (cmp == 0) {
3876 /*
3877 * POSIX: "If the old argument and the new argument
3878 * both refer to links to the same existing file,
3879 * the rename() function shall return successfully
3880 * and perform no other action."
3881 */
3882 ZFS_EXIT(zfsvfs);
3883 return (0);
3884 }
3885 /*
3886 * If the file system is case-folding, then we may
3887 * have some more checking to do. A case-folding file
3888 * system is either supporting mixed case sensitivity
3889 * access or is completely case-insensitive. Note
3890 * that the file system is always case preserving.
3891 *
3892 * In mixed sensitivity mode case sensitive behavior
3893 * is the default. FIGNORECASE must be used to
3894 * explicitly request case insensitive behavior.
3895 *
3896 * If the source and target names provided differ only
3897 * by case (e.g., a request to rename 'tim' to 'Tim'),
3898 * we will treat this as a special case in the
3899 * case-insensitive mode: as long as the source name
3900 * is an exact match, we will allow this to proceed as
3901 * a name-change request.
3902 */
3903 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3904 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3905 flags & FIGNORECASE)) &&
3906 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3907 &error) == 0) {
3908 /*
3909 * case preserving rename request, require exact
3910 * name matches
3911 */
3912 zflg |= ZCIEXACT;
3913 zflg &= ~ZCILOOK;
3914 }
3915 }
3916
3917 /*
3918 * If the source and destination directories are the same, we should
3919 * grab the z_name_lock of that directory only once.
3920 */
3921 if (sdzp == tdzp) {
3922 zflg |= ZHAVELOCK;
3923 rw_enter(&sdzp->z_name_lock, RW_READER);
3924 }
3925
3926 if (cmp < 0) {
3927 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3928 ZEXISTS | zflg, NULL, NULL);
3929 terr = zfs_dirent_lock(&tdl,
3930 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3931 } else {
3932 terr = zfs_dirent_lock(&tdl,
3933 tdzp, tnm, &tzp, zflg, NULL, NULL);
3934 serr = zfs_dirent_lock(&sdl,
3935 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3936 NULL, NULL);
3937 }
3938
3939 if (serr) {
3940 /*
3941 * Source entry invalid or not there.
3942 */
3943 if (!terr) {
3944 zfs_dirent_unlock(tdl);
3945 if (tzp)
3946 VN_RELE(ZTOV(tzp));
3947 }
3948
3949 if (sdzp == tdzp)
3950 rw_exit(&sdzp->z_name_lock);
3951
3952 /*
3953 * FreeBSD: In OpenSolaris they only check if rename source is
3954 * ".." here, because "." is handled in their lookup. This is
3955 * not the case for FreeBSD, so we check for "." explicitly.
3956 */
3957 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3958 serr = SET_ERROR(EINVAL);
3959 ZFS_EXIT(zfsvfs);
3960 return (serr);
3961 }
3962 if (terr) {
3963 zfs_dirent_unlock(sdl);
3964 VN_RELE(ZTOV(szp));
3965
3966 if (sdzp == tdzp)
3967 rw_exit(&sdzp->z_name_lock);
3968
3969 if (strcmp(tnm, "..") == 0)
3970 terr = SET_ERROR(EINVAL);
3971 ZFS_EXIT(zfsvfs);
3972 return (terr);
3973 }
3974
3975 /*
3976 * Must have write access at the source to remove the old entry
3977 * and write access at the target to create the new entry.
3978 * Note that if target and source are the same, this can be
3979 * done in a single check.
3980 */
3981
3982 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3983 goto out;
3984
3985 if (ZTOV(szp)->v_type == VDIR) {
3986 /*
3987 * Check to make sure rename is valid.
3988 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3989 */
3990 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3991 goto out;
3992 }
3993
3994 /*
3995 * Does target exist?
3996 */
3997 if (tzp) {
3998 /*
3999 * Source and target must be the same type.
4000 */
4001 if (ZTOV(szp)->v_type == VDIR) {
4002 if (ZTOV(tzp)->v_type != VDIR) {
4003 error = SET_ERROR(ENOTDIR);
4004 goto out;
4005 }
4006 } else {
4007 if (ZTOV(tzp)->v_type == VDIR) {
4008 error = SET_ERROR(EISDIR);
4009 goto out;
4010 }
4011 }
4012 /*
4013 * POSIX dictates that when the source and target
4014 * entries refer to the same file object, rename
4015 * must do nothing and exit without error.
4016 */
4017 if (szp->z_id == tzp->z_id) {
4018 error = 0;
4019 goto out;
4020 }
4021 }
4022
4023 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
4024 if (tzp)
4025 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
4026
4027 /*
4028 * notify the target directory if it is not the same
4029 * as source directory.
4030 */
4031 if (tdvp != sdvp) {
4032 vnevent_rename_dest_dir(tdvp, ct);
4033 }
4034
4035 tx = dmu_tx_create(zfsvfs->z_os);
4036 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4037 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
4038 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
4039 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
4040 if (sdzp != tdzp) {
4041 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
4042 zfs_sa_upgrade_txholds(tx, tdzp);
4043 }
4044 if (tzp) {
4045 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
4046 zfs_sa_upgrade_txholds(tx, tzp);
4047 }
4048
4049 zfs_sa_upgrade_txholds(tx, szp);
4050 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4051 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4052 if (error) {
4053 if (zl != NULL)
4054 zfs_rename_unlock(&zl);
4055 zfs_dirent_unlock(sdl);
4056 zfs_dirent_unlock(tdl);
4057
4058 if (sdzp == tdzp)
4059 rw_exit(&sdzp->z_name_lock);
4060
4061 VN_RELE(ZTOV(szp));
4062 if (tzp)
4063 VN_RELE(ZTOV(tzp));
4064 if (error == ERESTART) {
4065 waited = B_TRUE;
4066 dmu_tx_wait(tx);
4067 dmu_tx_abort(tx);
4068 goto top;
4069 }
4070 dmu_tx_abort(tx);
4071 ZFS_EXIT(zfsvfs);
4072 return (error);
4073 }
4074
4075 if (tzp) /* Attempt to remove the existing target */
4076 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
4077
4078 if (error == 0) {
4079 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
4080 if (error == 0) {
4081 szp->z_pflags |= ZFS_AV_MODIFIED;
4082
4083 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4084 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4085 ASSERT0(error);
4086
4087 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4088 if (error == 0) {
4089 zfs_log_rename(zilog, tx, TX_RENAME |
4090 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4091 sdl->dl_name, tdzp, tdl->dl_name, szp);
4092
4093 /*
4094 * Update path information for the target vnode
4095 */
4096 vn_renamepath(tdvp, ZTOV(szp), tnm,
4097 strlen(tnm));
4098 } else {
4099 /*
4100 * At this point, we have successfully created
4101 * the target name, but have failed to remove
4102 * the source name. Since the create was done
4103 * with the ZRENAMING flag, there are
4104 * complications; for one, the link count is
4105 * wrong. The easiest way to deal with this
4106 * is to remove the newly created target, and
4107 * return the original error. This must
4108 * succeed; fortunately, it is very unlikely to
4109 * fail, since we just created it.
4110 */
4111 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4112 ZRENAMING, NULL), ==, 0);
4113 }
4114 }
4115 #ifdef FREEBSD_NAMECACHE
4116 if (error == 0) {
4117 cache_purge(sdvp);
4118 cache_purge(tdvp);
4119 cache_purge(ZTOV(szp));
4120 if (tzp)
4121 cache_purge(ZTOV(tzp));
4122 }
4123 #endif
4124 }
4125
4126 dmu_tx_commit(tx);
4127 out:
4128 if (zl != NULL)
4129 zfs_rename_unlock(&zl);
4130
4131 zfs_dirent_unlock(sdl);
4132 zfs_dirent_unlock(tdl);
4133
4134 if (sdzp == tdzp)
4135 rw_exit(&sdzp->z_name_lock);
4136
4137
4138 VN_RELE(ZTOV(szp));
4139 if (tzp)
4140 VN_RELE(ZTOV(tzp));
4141
4142 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4143 zil_commit(zilog, 0);
4144
4145 ZFS_EXIT(zfsvfs);
4146
4147 return (error);
4148 }
4149
4150 /*
4151 * Insert the indicated symbolic reference entry into the directory.
4152 *
4153 * IN: dvp - Directory to contain new symbolic link.
4154 * link - Name for new symlink entry.
4155 * vap - Attributes of new entry.
4156 * cr - credentials of caller.
4157 * ct - caller context
4158 * flags - case flags
4159 *
4160 * RETURN: 0 on success, error code on failure.
4161 *
4162 * Timestamps:
4163 * dvp - ctime|mtime updated
4164 */
4165 /*ARGSUSED*/
4166 static int
zfs_symlink(vnode_t * dvp,vnode_t ** vpp,char * name,vattr_t * vap,char * link,cred_t * cr,kthread_t * td)4167 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4168 cred_t *cr, kthread_t *td)
4169 {
4170 znode_t *zp, *dzp = VTOZ(dvp);
4171 zfs_dirlock_t *dl;
4172 dmu_tx_t *tx;
4173 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4174 zilog_t *zilog;
4175 uint64_t len = strlen(link);
4176 int error;
4177 int zflg = ZNEW;
4178 zfs_acl_ids_t acl_ids;
4179 boolean_t fuid_dirtied;
4180 uint64_t txtype = TX_SYMLINK;
4181 boolean_t waited = B_FALSE;
4182 int flags = 0;
4183
4184 ASSERT(vap->va_type == VLNK);
4185
4186 ZFS_ENTER(zfsvfs);
4187 ZFS_VERIFY_ZP(dzp);
4188 zilog = zfsvfs->z_log;
4189
4190 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4191 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4192 ZFS_EXIT(zfsvfs);
4193 return (SET_ERROR(EILSEQ));
4194 }
4195 if (flags & FIGNORECASE)
4196 zflg |= ZCILOOK;
4197
4198 if (len > MAXPATHLEN) {
4199 ZFS_EXIT(zfsvfs);
4200 return (SET_ERROR(ENAMETOOLONG));
4201 }
4202
4203 if ((error = zfs_acl_ids_create(dzp, 0,
4204 vap, cr, NULL, &acl_ids)) != 0) {
4205 ZFS_EXIT(zfsvfs);
4206 return (error);
4207 }
4208
4209 getnewvnode_reserve(1);
4210
4211 top:
4212 /*
4213 * Attempt to lock directory; fail if entry already exists.
4214 */
4215 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4216 if (error) {
4217 zfs_acl_ids_free(&acl_ids);
4218 getnewvnode_drop_reserve();
4219 ZFS_EXIT(zfsvfs);
4220 return (error);
4221 }
4222
4223 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4224 zfs_acl_ids_free(&acl_ids);
4225 zfs_dirent_unlock(dl);
4226 getnewvnode_drop_reserve();
4227 ZFS_EXIT(zfsvfs);
4228 return (error);
4229 }
4230
4231 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4232 zfs_acl_ids_free(&acl_ids);
4233 zfs_dirent_unlock(dl);
4234 getnewvnode_drop_reserve();
4235 ZFS_EXIT(zfsvfs);
4236 return (SET_ERROR(EDQUOT));
4237 }
4238 tx = dmu_tx_create(zfsvfs->z_os);
4239 fuid_dirtied = zfsvfs->z_fuid_dirty;
4240 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4241 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4242 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4243 ZFS_SA_BASE_ATTR_SIZE + len);
4244 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4245 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4246 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4247 acl_ids.z_aclp->z_acl_bytes);
4248 }
4249 if (fuid_dirtied)
4250 zfs_fuid_txhold(zfsvfs, tx);
4251 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4252 if (error) {
4253 zfs_dirent_unlock(dl);
4254 if (error == ERESTART) {
4255 waited = B_TRUE;
4256 dmu_tx_wait(tx);
4257 dmu_tx_abort(tx);
4258 goto top;
4259 }
4260 zfs_acl_ids_free(&acl_ids);
4261 dmu_tx_abort(tx);
4262 getnewvnode_drop_reserve();
4263 ZFS_EXIT(zfsvfs);
4264 return (error);
4265 }
4266
4267 /*
4268 * Create a new object for the symlink.
4269 * for version 4 ZPL datsets the symlink will be an SA attribute
4270 */
4271 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4272
4273 if (fuid_dirtied)
4274 zfs_fuid_sync(zfsvfs, tx);
4275
4276 mutex_enter(&zp->z_lock);
4277 if (zp->z_is_sa)
4278 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4279 link, len, tx);
4280 else
4281 zfs_sa_symlink(zp, link, len, tx);
4282 mutex_exit(&zp->z_lock);
4283
4284 zp->z_size = len;
4285 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4286 &zp->z_size, sizeof (zp->z_size), tx);
4287 /*
4288 * Insert the new object into the directory.
4289 */
4290 (void) zfs_link_create(dl, zp, tx, ZNEW);
4291
4292 if (flags & FIGNORECASE)
4293 txtype |= TX_CI;
4294 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4295 *vpp = ZTOV(zp);
4296
4297 zfs_acl_ids_free(&acl_ids);
4298
4299 dmu_tx_commit(tx);
4300
4301 getnewvnode_drop_reserve();
4302
4303 zfs_dirent_unlock(dl);
4304
4305 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4306 zil_commit(zilog, 0);
4307
4308 ZFS_EXIT(zfsvfs);
4309 return (error);
4310 }
4311
4312 /*
4313 * Return, in the buffer contained in the provided uio structure,
4314 * the symbolic path referred to by vp.
4315 *
4316 * IN: vp - vnode of symbolic link.
4317 * uio - structure to contain the link path.
4318 * cr - credentials of caller.
4319 * ct - caller context
4320 *
4321 * OUT: uio - structure containing the link path.
4322 *
4323 * RETURN: 0 on success, error code on failure.
4324 *
4325 * Timestamps:
4326 * vp - atime updated
4327 */
4328 /* ARGSUSED */
4329 static int
zfs_readlink(vnode_t * vp,uio_t * uio,cred_t * cr,caller_context_t * ct)4330 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4331 {
4332 znode_t *zp = VTOZ(vp);
4333 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4334 int error;
4335
4336 ZFS_ENTER(zfsvfs);
4337 ZFS_VERIFY_ZP(zp);
4338
4339 mutex_enter(&zp->z_lock);
4340 if (zp->z_is_sa)
4341 error = sa_lookup_uio(zp->z_sa_hdl,
4342 SA_ZPL_SYMLINK(zfsvfs), uio);
4343 else
4344 error = zfs_sa_readlink(zp, uio);
4345 mutex_exit(&zp->z_lock);
4346
4347 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4348
4349 ZFS_EXIT(zfsvfs);
4350 return (error);
4351 }
4352
4353 /*
4354 * Insert a new entry into directory tdvp referencing svp.
4355 *
4356 * IN: tdvp - Directory to contain new entry.
4357 * svp - vnode of new entry.
4358 * name - name of new entry.
4359 * cr - credentials of caller.
4360 * ct - caller context
4361 *
4362 * RETURN: 0 on success, error code on failure.
4363 *
4364 * Timestamps:
4365 * tdvp - ctime|mtime updated
4366 * svp - ctime updated
4367 */
4368 /* ARGSUSED */
4369 static int
zfs_link(vnode_t * tdvp,vnode_t * svp,char * name,cred_t * cr,caller_context_t * ct,int flags)4370 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4371 caller_context_t *ct, int flags)
4372 {
4373 znode_t *dzp = VTOZ(tdvp);
4374 znode_t *tzp, *szp;
4375 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4376 zilog_t *zilog;
4377 zfs_dirlock_t *dl;
4378 dmu_tx_t *tx;
4379 vnode_t *realvp;
4380 int error;
4381 int zf = ZNEW;
4382 uint64_t parent;
4383 uid_t owner;
4384 boolean_t waited = B_FALSE;
4385
4386 ASSERT(tdvp->v_type == VDIR);
4387
4388 ZFS_ENTER(zfsvfs);
4389 ZFS_VERIFY_ZP(dzp);
4390 zilog = zfsvfs->z_log;
4391
4392 if (VOP_REALVP(svp, &realvp, ct) == 0)
4393 svp = realvp;
4394
4395 /*
4396 * POSIX dictates that we return EPERM here.
4397 * Better choices include ENOTSUP or EISDIR.
4398 */
4399 if (svp->v_type == VDIR) {
4400 ZFS_EXIT(zfsvfs);
4401 return (SET_ERROR(EPERM));
4402 }
4403
4404 szp = VTOZ(svp);
4405 ZFS_VERIFY_ZP(szp);
4406
4407 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4408 ZFS_EXIT(zfsvfs);
4409 return (SET_ERROR(EPERM));
4410 }
4411
4412 /*
4413 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4414 * ctldir appear to have the same v_vfsp.
4415 */
4416 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4417 ZFS_EXIT(zfsvfs);
4418 return (SET_ERROR(EXDEV));
4419 }
4420
4421 /* Prevent links to .zfs/shares files */
4422
4423 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4424 &parent, sizeof (uint64_t))) != 0) {
4425 ZFS_EXIT(zfsvfs);
4426 return (error);
4427 }
4428 if (parent == zfsvfs->z_shares_dir) {
4429 ZFS_EXIT(zfsvfs);
4430 return (SET_ERROR(EPERM));
4431 }
4432
4433 if (zfsvfs->z_utf8 && u8_validate(name,
4434 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4435 ZFS_EXIT(zfsvfs);
4436 return (SET_ERROR(EILSEQ));
4437 }
4438 if (flags & FIGNORECASE)
4439 zf |= ZCILOOK;
4440
4441 /*
4442 * We do not support links between attributes and non-attributes
4443 * because of the potential security risk of creating links
4444 * into "normal" file space in order to circumvent restrictions
4445 * imposed in attribute space.
4446 */
4447 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4448 ZFS_EXIT(zfsvfs);
4449 return (SET_ERROR(EINVAL));
4450 }
4451
4452
4453 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4454 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4455 ZFS_EXIT(zfsvfs);
4456 return (SET_ERROR(EPERM));
4457 }
4458
4459 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4460 ZFS_EXIT(zfsvfs);
4461 return (error);
4462 }
4463
4464 top:
4465 /*
4466 * Attempt to lock directory; fail if entry already exists.
4467 */
4468 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4469 if (error) {
4470 ZFS_EXIT(zfsvfs);
4471 return (error);
4472 }
4473
4474 tx = dmu_tx_create(zfsvfs->z_os);
4475 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4476 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4477 zfs_sa_upgrade_txholds(tx, szp);
4478 zfs_sa_upgrade_txholds(tx, dzp);
4479 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4480 if (error) {
4481 zfs_dirent_unlock(dl);
4482 if (error == ERESTART) {
4483 waited = B_TRUE;
4484 dmu_tx_wait(tx);
4485 dmu_tx_abort(tx);
4486 goto top;
4487 }
4488 dmu_tx_abort(tx);
4489 ZFS_EXIT(zfsvfs);
4490 return (error);
4491 }
4492
4493 error = zfs_link_create(dl, szp, tx, 0);
4494
4495 if (error == 0) {
4496 uint64_t txtype = TX_LINK;
4497 if (flags & FIGNORECASE)
4498 txtype |= TX_CI;
4499 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4500 }
4501
4502 dmu_tx_commit(tx);
4503
4504 zfs_dirent_unlock(dl);
4505
4506 if (error == 0) {
4507 vnevent_link(svp, ct);
4508 }
4509
4510 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4511 zil_commit(zilog, 0);
4512
4513 ZFS_EXIT(zfsvfs);
4514 return (error);
4515 }
4516
4517 #ifdef illumos
4518 /*
4519 * zfs_null_putapage() is used when the file system has been force
4520 * unmounted. It just drops the pages.
4521 */
4522 /* ARGSUSED */
4523 static int
zfs_null_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)4524 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4525 size_t *lenp, int flags, cred_t *cr)
4526 {
4527 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4528 return (0);
4529 }
4530
4531 /*
4532 * Push a page out to disk, klustering if possible.
4533 *
4534 * IN: vp - file to push page to.
4535 * pp - page to push.
4536 * flags - additional flags.
4537 * cr - credentials of caller.
4538 *
4539 * OUT: offp - start of range pushed.
4540 * lenp - len of range pushed.
4541 *
4542 * RETURN: 0 on success, error code on failure.
4543 *
4544 * NOTE: callers must have locked the page to be pushed. On
4545 * exit, the page (and all other pages in the kluster) must be
4546 * unlocked.
4547 */
4548 /* ARGSUSED */
4549 static int
zfs_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)4550 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4551 size_t *lenp, int flags, cred_t *cr)
4552 {
4553 znode_t *zp = VTOZ(vp);
4554 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4555 dmu_tx_t *tx;
4556 u_offset_t off, koff;
4557 size_t len, klen;
4558 int err;
4559
4560 off = pp->p_offset;
4561 len = PAGESIZE;
4562 /*
4563 * If our blocksize is bigger than the page size, try to kluster
4564 * multiple pages so that we write a full block (thus avoiding
4565 * a read-modify-write).
4566 */
4567 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4568 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4569 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4570 ASSERT(koff <= zp->z_size);
4571 if (koff + klen > zp->z_size)
4572 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4573 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4574 }
4575 ASSERT3U(btop(len), ==, btopr(len));
4576
4577 /*
4578 * Can't push pages past end-of-file.
4579 */
4580 if (off >= zp->z_size) {
4581 /* ignore all pages */
4582 err = 0;
4583 goto out;
4584 } else if (off + len > zp->z_size) {
4585 int npages = btopr(zp->z_size - off);
4586 page_t *trunc;
4587
4588 page_list_break(&pp, &trunc, npages);
4589 /* ignore pages past end of file */
4590 if (trunc)
4591 pvn_write_done(trunc, flags);
4592 len = zp->z_size - off;
4593 }
4594
4595 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4596 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4597 err = SET_ERROR(EDQUOT);
4598 goto out;
4599 }
4600 tx = dmu_tx_create(zfsvfs->z_os);
4601 dmu_tx_hold_write(tx, zp->z_id, off, len);
4602
4603 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4604 zfs_sa_upgrade_txholds(tx, zp);
4605 err = dmu_tx_assign(tx, TXG_WAIT);
4606 if (err != 0) {
4607 dmu_tx_abort(tx);
4608 goto out;
4609 }
4610
4611 if (zp->z_blksz <= PAGESIZE) {
4612 caddr_t va = zfs_map_page(pp, S_READ);
4613 ASSERT3U(len, <=, PAGESIZE);
4614 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4615 zfs_unmap_page(pp, va);
4616 } else {
4617 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4618 }
4619
4620 if (err == 0) {
4621 uint64_t mtime[2], ctime[2];
4622 sa_bulk_attr_t bulk[3];
4623 int count = 0;
4624
4625 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4626 &mtime, 16);
4627 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4628 &ctime, 16);
4629 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4630 &zp->z_pflags, 8);
4631 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4632 B_TRUE);
4633 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4634 }
4635 dmu_tx_commit(tx);
4636
4637 out:
4638 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4639 if (offp)
4640 *offp = off;
4641 if (lenp)
4642 *lenp = len;
4643
4644 return (err);
4645 }
4646
4647 /*
4648 * Copy the portion of the file indicated from pages into the file.
4649 * The pages are stored in a page list attached to the files vnode.
4650 *
4651 * IN: vp - vnode of file to push page data to.
4652 * off - position in file to put data.
4653 * len - amount of data to write.
4654 * flags - flags to control the operation.
4655 * cr - credentials of caller.
4656 * ct - caller context.
4657 *
4658 * RETURN: 0 on success, error code on failure.
4659 *
4660 * Timestamps:
4661 * vp - ctime|mtime updated
4662 */
4663 /*ARGSUSED*/
4664 static int
zfs_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)4665 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4666 caller_context_t *ct)
4667 {
4668 znode_t *zp = VTOZ(vp);
4669 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4670 page_t *pp;
4671 size_t io_len;
4672 u_offset_t io_off;
4673 uint_t blksz;
4674 rl_t *rl;
4675 int error = 0;
4676
4677 ZFS_ENTER(zfsvfs);
4678 ZFS_VERIFY_ZP(zp);
4679
4680 /*
4681 * Align this request to the file block size in case we kluster.
4682 * XXX - this can result in pretty aggresive locking, which can
4683 * impact simultanious read/write access. One option might be
4684 * to break up long requests (len == 0) into block-by-block
4685 * operations to get narrower locking.
4686 */
4687 blksz = zp->z_blksz;
4688 if (ISP2(blksz))
4689 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4690 else
4691 io_off = 0;
4692 if (len > 0 && ISP2(blksz))
4693 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4694 else
4695 io_len = 0;
4696
4697 if (io_len == 0) {
4698 /*
4699 * Search the entire vp list for pages >= io_off.
4700 */
4701 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4702 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4703 goto out;
4704 }
4705 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4706
4707 if (off > zp->z_size) {
4708 /* past end of file */
4709 zfs_range_unlock(rl);
4710 ZFS_EXIT(zfsvfs);
4711 return (0);
4712 }
4713
4714 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4715
4716 for (off = io_off; io_off < off + len; io_off += io_len) {
4717 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4718 pp = page_lookup(vp, io_off,
4719 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4720 } else {
4721 pp = page_lookup_nowait(vp, io_off,
4722 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4723 }
4724
4725 if (pp != NULL && pvn_getdirty(pp, flags)) {
4726 int err;
4727
4728 /*
4729 * Found a dirty page to push
4730 */
4731 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4732 if (err)
4733 error = err;
4734 } else {
4735 io_len = PAGESIZE;
4736 }
4737 }
4738 out:
4739 zfs_range_unlock(rl);
4740 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4741 zil_commit(zfsvfs->z_log, zp->z_id);
4742 ZFS_EXIT(zfsvfs);
4743 return (error);
4744 }
4745 #endif /* illumos */
4746
4747 /*ARGSUSED*/
4748 void
zfs_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)4749 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4750 {
4751 znode_t *zp = VTOZ(vp);
4752 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4753 int error;
4754
4755 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4756 if (zp->z_sa_hdl == NULL) {
4757 /*
4758 * The fs has been unmounted, or we did a
4759 * suspend/resume and this file no longer exists.
4760 */
4761 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4762 vrecycle(vp);
4763 return;
4764 }
4765
4766 mutex_enter(&zp->z_lock);
4767 if (zp->z_unlinked) {
4768 /*
4769 * Fast path to recycle a vnode of a removed file.
4770 */
4771 mutex_exit(&zp->z_lock);
4772 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4773 vrecycle(vp);
4774 return;
4775 }
4776 mutex_exit(&zp->z_lock);
4777
4778 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4779 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4780
4781 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4782 zfs_sa_upgrade_txholds(tx, zp);
4783 error = dmu_tx_assign(tx, TXG_WAIT);
4784 if (error) {
4785 dmu_tx_abort(tx);
4786 } else {
4787 mutex_enter(&zp->z_lock);
4788 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4789 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4790 zp->z_atime_dirty = 0;
4791 mutex_exit(&zp->z_lock);
4792 dmu_tx_commit(tx);
4793 }
4794 }
4795 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4796 }
4797
4798 #ifdef illumos
4799 /*
4800 * Bounds-check the seek operation.
4801 *
4802 * IN: vp - vnode seeking within
4803 * ooff - old file offset
4804 * noffp - pointer to new file offset
4805 * ct - caller context
4806 *
4807 * RETURN: 0 on success, EINVAL if new offset invalid.
4808 */
4809 /* ARGSUSED */
4810 static int
zfs_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)4811 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4812 caller_context_t *ct)
4813 {
4814 if (vp->v_type == VDIR)
4815 return (0);
4816 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4817 }
4818
4819 /*
4820 * Pre-filter the generic locking function to trap attempts to place
4821 * a mandatory lock on a memory mapped file.
4822 */
4823 static int
zfs_frlock(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,flk_callback_t * flk_cbp,cred_t * cr,caller_context_t * ct)4824 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4825 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4826 {
4827 znode_t *zp = VTOZ(vp);
4828 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4829
4830 ZFS_ENTER(zfsvfs);
4831 ZFS_VERIFY_ZP(zp);
4832
4833 /*
4834 * We are following the UFS semantics with respect to mapcnt
4835 * here: If we see that the file is mapped already, then we will
4836 * return an error, but we don't worry about races between this
4837 * function and zfs_map().
4838 */
4839 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4840 ZFS_EXIT(zfsvfs);
4841 return (SET_ERROR(EAGAIN));
4842 }
4843 ZFS_EXIT(zfsvfs);
4844 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4845 }
4846
4847 /*
4848 * If we can't find a page in the cache, we will create a new page
4849 * and fill it with file data. For efficiency, we may try to fill
4850 * multiple pages at once (klustering) to fill up the supplied page
4851 * list. Note that the pages to be filled are held with an exclusive
4852 * lock to prevent access by other threads while they are being filled.
4853 */
4854 static int
zfs_fillpage(vnode_t * vp,u_offset_t off,struct seg * seg,caddr_t addr,page_t * pl[],size_t plsz,enum seg_rw rw)4855 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4856 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4857 {
4858 znode_t *zp = VTOZ(vp);
4859 page_t *pp, *cur_pp;
4860 objset_t *os = zp->z_zfsvfs->z_os;
4861 u_offset_t io_off, total;
4862 size_t io_len;
4863 int err;
4864
4865 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4866 /*
4867 * We only have a single page, don't bother klustering
4868 */
4869 io_off = off;
4870 io_len = PAGESIZE;
4871 pp = page_create_va(vp, io_off, io_len,
4872 PG_EXCL | PG_WAIT, seg, addr);
4873 } else {
4874 /*
4875 * Try to find enough pages to fill the page list
4876 */
4877 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4878 &io_len, off, plsz, 0);
4879 }
4880 if (pp == NULL) {
4881 /*
4882 * The page already exists, nothing to do here.
4883 */
4884 *pl = NULL;
4885 return (0);
4886 }
4887
4888 /*
4889 * Fill the pages in the kluster.
4890 */
4891 cur_pp = pp;
4892 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4893 caddr_t va;
4894
4895 ASSERT3U(io_off, ==, cur_pp->p_offset);
4896 va = zfs_map_page(cur_pp, S_WRITE);
4897 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4898 DMU_READ_PREFETCH);
4899 zfs_unmap_page(cur_pp, va);
4900 if (err) {
4901 /* On error, toss the entire kluster */
4902 pvn_read_done(pp, B_ERROR);
4903 /* convert checksum errors into IO errors */
4904 if (err == ECKSUM)
4905 err = SET_ERROR(EIO);
4906 return (err);
4907 }
4908 cur_pp = cur_pp->p_next;
4909 }
4910
4911 /*
4912 * Fill in the page list array from the kluster starting
4913 * from the desired offset `off'.
4914 * NOTE: the page list will always be null terminated.
4915 */
4916 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4917 ASSERT(pl == NULL || (*pl)->p_offset == off);
4918
4919 return (0);
4920 }
4921
4922 /*
4923 * Return pointers to the pages for the file region [off, off + len]
4924 * in the pl array. If plsz is greater than len, this function may
4925 * also return page pointers from after the specified region
4926 * (i.e. the region [off, off + plsz]). These additional pages are
4927 * only returned if they are already in the cache, or were created as
4928 * part of a klustered read.
4929 *
4930 * IN: vp - vnode of file to get data from.
4931 * off - position in file to get data from.
4932 * len - amount of data to retrieve.
4933 * plsz - length of provided page list.
4934 * seg - segment to obtain pages for.
4935 * addr - virtual address of fault.
4936 * rw - mode of created pages.
4937 * cr - credentials of caller.
4938 * ct - caller context.
4939 *
4940 * OUT: protp - protection mode of created pages.
4941 * pl - list of pages created.
4942 *
4943 * RETURN: 0 on success, error code on failure.
4944 *
4945 * Timestamps:
4946 * vp - atime updated
4947 */
4948 /* ARGSUSED */
4949 static int
zfs_getpage(vnode_t * vp,offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr,caller_context_t * ct)4950 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4951 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4952 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4953 {
4954 znode_t *zp = VTOZ(vp);
4955 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4956 page_t **pl0 = pl;
4957 int err = 0;
4958
4959 /* we do our own caching, faultahead is unnecessary */
4960 if (pl == NULL)
4961 return (0);
4962 else if (len > plsz)
4963 len = plsz;
4964 else
4965 len = P2ROUNDUP(len, PAGESIZE);
4966 ASSERT(plsz >= len);
4967
4968 ZFS_ENTER(zfsvfs);
4969 ZFS_VERIFY_ZP(zp);
4970
4971 if (protp)
4972 *protp = PROT_ALL;
4973
4974 /*
4975 * Loop through the requested range [off, off + len) looking
4976 * for pages. If we don't find a page, we will need to create
4977 * a new page and fill it with data from the file.
4978 */
4979 while (len > 0) {
4980 if (*pl = page_lookup(vp, off, SE_SHARED))
4981 *(pl+1) = NULL;
4982 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4983 goto out;
4984 while (*pl) {
4985 ASSERT3U((*pl)->p_offset, ==, off);
4986 off += PAGESIZE;
4987 addr += PAGESIZE;
4988 if (len > 0) {
4989 ASSERT3U(len, >=, PAGESIZE);
4990 len -= PAGESIZE;
4991 }
4992 ASSERT3U(plsz, >=, PAGESIZE);
4993 plsz -= PAGESIZE;
4994 pl++;
4995 }
4996 }
4997
4998 /*
4999 * Fill out the page array with any pages already in the cache.
5000 */
5001 while (plsz > 0 &&
5002 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
5003 off += PAGESIZE;
5004 plsz -= PAGESIZE;
5005 }
5006 out:
5007 if (err) {
5008 /*
5009 * Release any pages we have previously locked.
5010 */
5011 while (pl > pl0)
5012 page_unlock(*--pl);
5013 } else {
5014 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5015 }
5016
5017 *pl = NULL;
5018
5019 ZFS_EXIT(zfsvfs);
5020 return (err);
5021 }
5022
5023 /*
5024 * Request a memory map for a section of a file. This code interacts
5025 * with common code and the VM system as follows:
5026 *
5027 * - common code calls mmap(), which ends up in smmap_common()
5028 * - this calls VOP_MAP(), which takes you into (say) zfs
5029 * - zfs_map() calls as_map(), passing segvn_create() as the callback
5030 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
5031 * - zfs_addmap() updates z_mapcnt
5032 */
5033 /*ARGSUSED*/
5034 static int
zfs_map(vnode_t * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)5035 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5036 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5037 caller_context_t *ct)
5038 {
5039 znode_t *zp = VTOZ(vp);
5040 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5041 segvn_crargs_t vn_a;
5042 int error;
5043
5044 ZFS_ENTER(zfsvfs);
5045 ZFS_VERIFY_ZP(zp);
5046
5047 if ((prot & PROT_WRITE) && (zp->z_pflags &
5048 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
5049 ZFS_EXIT(zfsvfs);
5050 return (SET_ERROR(EPERM));
5051 }
5052
5053 if ((prot & (PROT_READ | PROT_EXEC)) &&
5054 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
5055 ZFS_EXIT(zfsvfs);
5056 return (SET_ERROR(EACCES));
5057 }
5058
5059 if (vp->v_flag & VNOMAP) {
5060 ZFS_EXIT(zfsvfs);
5061 return (SET_ERROR(ENOSYS));
5062 }
5063
5064 if (off < 0 || len > MAXOFFSET_T - off) {
5065 ZFS_EXIT(zfsvfs);
5066 return (SET_ERROR(ENXIO));
5067 }
5068
5069 if (vp->v_type != VREG) {
5070 ZFS_EXIT(zfsvfs);
5071 return (SET_ERROR(ENODEV));
5072 }
5073
5074 /*
5075 * If file is locked, disallow mapping.
5076 */
5077 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
5078 ZFS_EXIT(zfsvfs);
5079 return (SET_ERROR(EAGAIN));
5080 }
5081
5082 as_rangelock(as);
5083 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5084 if (error != 0) {
5085 as_rangeunlock(as);
5086 ZFS_EXIT(zfsvfs);
5087 return (error);
5088 }
5089
5090 vn_a.vp = vp;
5091 vn_a.offset = (u_offset_t)off;
5092 vn_a.type = flags & MAP_TYPE;
5093 vn_a.prot = prot;
5094 vn_a.maxprot = maxprot;
5095 vn_a.cred = cr;
5096 vn_a.amp = NULL;
5097 vn_a.flags = flags & ~MAP_TYPE;
5098 vn_a.szc = 0;
5099 vn_a.lgrp_mem_policy_flags = 0;
5100
5101 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5102
5103 as_rangeunlock(as);
5104 ZFS_EXIT(zfsvfs);
5105 return (error);
5106 }
5107
5108 /* ARGSUSED */
5109 static int
zfs_addmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)5110 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5111 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5112 caller_context_t *ct)
5113 {
5114 uint64_t pages = btopr(len);
5115
5116 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5117 return (0);
5118 }
5119
5120 /*
5121 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5122 * more accurate mtime for the associated file. Since we don't have a way of
5123 * detecting when the data was actually modified, we have to resort to
5124 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5125 * last page is pushed. The problem occurs when the msync() call is omitted,
5126 * which by far the most common case:
5127 *
5128 * open()
5129 * mmap()
5130 * <modify memory>
5131 * munmap()
5132 * close()
5133 * <time lapse>
5134 * putpage() via fsflush
5135 *
5136 * If we wait until fsflush to come along, we can have a modification time that
5137 * is some arbitrary point in the future. In order to prevent this in the
5138 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5139 * torn down.
5140 */
5141 /* ARGSUSED */
5142 static int
zfs_delmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)5143 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5144 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5145 caller_context_t *ct)
5146 {
5147 uint64_t pages = btopr(len);
5148
5149 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5150 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5151
5152 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5153 vn_has_cached_data(vp))
5154 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5155
5156 return (0);
5157 }
5158
5159 /*
5160 * Free or allocate space in a file. Currently, this function only
5161 * supports the `F_FREESP' command. However, this command is somewhat
5162 * misnamed, as its functionality includes the ability to allocate as
5163 * well as free space.
5164 *
5165 * IN: vp - vnode of file to free data in.
5166 * cmd - action to take (only F_FREESP supported).
5167 * bfp - section of file to free/alloc.
5168 * flag - current file open mode flags.
5169 * offset - current file offset.
5170 * cr - credentials of caller [UNUSED].
5171 * ct - caller context.
5172 *
5173 * RETURN: 0 on success, error code on failure.
5174 *
5175 * Timestamps:
5176 * vp - ctime|mtime updated
5177 */
5178 /* ARGSUSED */
5179 static int
zfs_space(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)5180 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5181 offset_t offset, cred_t *cr, caller_context_t *ct)
5182 {
5183 znode_t *zp = VTOZ(vp);
5184 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5185 uint64_t off, len;
5186 int error;
5187
5188 ZFS_ENTER(zfsvfs);
5189 ZFS_VERIFY_ZP(zp);
5190
5191 if (cmd != F_FREESP) {
5192 ZFS_EXIT(zfsvfs);
5193 return (SET_ERROR(EINVAL));
5194 }
5195
5196 /*
5197 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
5198 * callers might not be able to detect properly that we are read-only,
5199 * so check it explicitly here.
5200 */
5201 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
5202 ZFS_EXIT(zfsvfs);
5203 return (SET_ERROR(EROFS));
5204 }
5205
5206 if (error = convoff(vp, bfp, 0, offset)) {
5207 ZFS_EXIT(zfsvfs);
5208 return (error);
5209 }
5210
5211 if (bfp->l_len < 0) {
5212 ZFS_EXIT(zfsvfs);
5213 return (SET_ERROR(EINVAL));
5214 }
5215
5216 off = bfp->l_start;
5217 len = bfp->l_len; /* 0 means from off to end of file */
5218
5219 error = zfs_freesp(zp, off, len, flag, TRUE);
5220
5221 ZFS_EXIT(zfsvfs);
5222 return (error);
5223 }
5224 #endif /* illumos */
5225
5226 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5227 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5228
5229 /*ARGSUSED*/
5230 static int
zfs_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)5231 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5232 {
5233 znode_t *zp = VTOZ(vp);
5234 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5235 uint32_t gen;
5236 uint64_t gen64;
5237 uint64_t object = zp->z_id;
5238 zfid_short_t *zfid;
5239 int size, i, error;
5240
5241 ZFS_ENTER(zfsvfs);
5242 ZFS_VERIFY_ZP(zp);
5243
5244 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5245 &gen64, sizeof (uint64_t))) != 0) {
5246 ZFS_EXIT(zfsvfs);
5247 return (error);
5248 }
5249
5250 gen = (uint32_t)gen64;
5251
5252 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5253
5254 #ifdef illumos
5255 if (fidp->fid_len < size) {
5256 fidp->fid_len = size;
5257 ZFS_EXIT(zfsvfs);
5258 return (SET_ERROR(ENOSPC));
5259 }
5260 #else
5261 fidp->fid_len = size;
5262 #endif
5263
5264 zfid = (zfid_short_t *)fidp;
5265
5266 zfid->zf_len = size;
5267
5268 for (i = 0; i < sizeof (zfid->zf_object); i++)
5269 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5270
5271 /* Must have a non-zero generation number to distinguish from .zfs */
5272 if (gen == 0)
5273 gen = 1;
5274 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5275 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5276
5277 if (size == LONG_FID_LEN) {
5278 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5279 zfid_long_t *zlfid;
5280
5281 zlfid = (zfid_long_t *)fidp;
5282
5283 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5284 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5285
5286 /* XXX - this should be the generation number for the objset */
5287 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5288 zlfid->zf_setgen[i] = 0;
5289 }
5290
5291 ZFS_EXIT(zfsvfs);
5292 return (0);
5293 }
5294
5295 static int
zfs_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)5296 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5297 caller_context_t *ct)
5298 {
5299 znode_t *zp, *xzp;
5300 zfsvfs_t *zfsvfs;
5301 zfs_dirlock_t *dl;
5302 int error;
5303
5304 switch (cmd) {
5305 case _PC_LINK_MAX:
5306 *valp = INT_MAX;
5307 return (0);
5308
5309 case _PC_FILESIZEBITS:
5310 *valp = 64;
5311 return (0);
5312 #ifdef illumos
5313 case _PC_XATTR_EXISTS:
5314 zp = VTOZ(vp);
5315 zfsvfs = zp->z_zfsvfs;
5316 ZFS_ENTER(zfsvfs);
5317 ZFS_VERIFY_ZP(zp);
5318 *valp = 0;
5319 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5320 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5321 if (error == 0) {
5322 zfs_dirent_unlock(dl);
5323 if (!zfs_dirempty(xzp))
5324 *valp = 1;
5325 VN_RELE(ZTOV(xzp));
5326 } else if (error == ENOENT) {
5327 /*
5328 * If there aren't extended attributes, it's the
5329 * same as having zero of them.
5330 */
5331 error = 0;
5332 }
5333 ZFS_EXIT(zfsvfs);
5334 return (error);
5335
5336 case _PC_SATTR_ENABLED:
5337 case _PC_SATTR_EXISTS:
5338 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5339 (vp->v_type == VREG || vp->v_type == VDIR);
5340 return (0);
5341
5342 case _PC_ACCESS_FILTERING:
5343 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5344 vp->v_type == VDIR;
5345 return (0);
5346
5347 case _PC_ACL_ENABLED:
5348 *valp = _ACL_ACE_ENABLED;
5349 return (0);
5350 #endif /* illumos */
5351 case _PC_MIN_HOLE_SIZE:
5352 *valp = (int)SPA_MINBLOCKSIZE;
5353 return (0);
5354 #ifdef illumos
5355 case _PC_TIMESTAMP_RESOLUTION:
5356 /* nanosecond timestamp resolution */
5357 *valp = 1L;
5358 return (0);
5359 #endif
5360 case _PC_ACL_EXTENDED:
5361 *valp = 0;
5362 return (0);
5363
5364 case _PC_ACL_NFS4:
5365 *valp = 1;
5366 return (0);
5367
5368 case _PC_ACL_PATH_MAX:
5369 *valp = ACL_MAX_ENTRIES;
5370 return (0);
5371
5372 default:
5373 return (EOPNOTSUPP);
5374 }
5375 }
5376
5377 /*ARGSUSED*/
5378 static int
zfs_getsecattr(vnode_t * vp,vsecattr_t * vsecp,int flag,cred_t * cr,caller_context_t * ct)5379 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5380 caller_context_t *ct)
5381 {
5382 znode_t *zp = VTOZ(vp);
5383 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5384 int error;
5385 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5386
5387 ZFS_ENTER(zfsvfs);
5388 ZFS_VERIFY_ZP(zp);
5389 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5390 ZFS_EXIT(zfsvfs);
5391
5392 return (error);
5393 }
5394
5395 /*ARGSUSED*/
5396 int
zfs_setsecattr(vnode_t * vp,vsecattr_t * vsecp,int flag,cred_t * cr,caller_context_t * ct)5397 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5398 caller_context_t *ct)
5399 {
5400 znode_t *zp = VTOZ(vp);
5401 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5402 int error;
5403 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5404 zilog_t *zilog = zfsvfs->z_log;
5405
5406 ZFS_ENTER(zfsvfs);
5407 ZFS_VERIFY_ZP(zp);
5408
5409 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5410
5411 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5412 zil_commit(zilog, 0);
5413
5414 ZFS_EXIT(zfsvfs);
5415 return (error);
5416 }
5417
5418 #ifdef illumos
5419 /*
5420 * The smallest read we may consider to loan out an arcbuf.
5421 * This must be a power of 2.
5422 */
5423 int zcr_blksz_min = (1 << 10); /* 1K */
5424 /*
5425 * If set to less than the file block size, allow loaning out of an
5426 * arcbuf for a partial block read. This must be a power of 2.
5427 */
5428 int zcr_blksz_max = (1 << 17); /* 128K */
5429
5430 /*ARGSUSED*/
5431 static int
zfs_reqzcbuf(vnode_t * vp,enum uio_rw ioflag,xuio_t * xuio,cred_t * cr,caller_context_t * ct)5432 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5433 caller_context_t *ct)
5434 {
5435 znode_t *zp = VTOZ(vp);
5436 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5437 int max_blksz = zfsvfs->z_max_blksz;
5438 uio_t *uio = &xuio->xu_uio;
5439 ssize_t size = uio->uio_resid;
5440 offset_t offset = uio->uio_loffset;
5441 int blksz;
5442 int fullblk, i;
5443 arc_buf_t *abuf;
5444 ssize_t maxsize;
5445 int preamble, postamble;
5446
5447 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5448 return (SET_ERROR(EINVAL));
5449
5450 ZFS_ENTER(zfsvfs);
5451 ZFS_VERIFY_ZP(zp);
5452 switch (ioflag) {
5453 case UIO_WRITE:
5454 /*
5455 * Loan out an arc_buf for write if write size is bigger than
5456 * max_blksz, and the file's block size is also max_blksz.
5457 */
5458 blksz = max_blksz;
5459 if (size < blksz || zp->z_blksz != blksz) {
5460 ZFS_EXIT(zfsvfs);
5461 return (SET_ERROR(EINVAL));
5462 }
5463 /*
5464 * Caller requests buffers for write before knowing where the
5465 * write offset might be (e.g. NFS TCP write).
5466 */
5467 if (offset == -1) {
5468 preamble = 0;
5469 } else {
5470 preamble = P2PHASE(offset, blksz);
5471 if (preamble) {
5472 preamble = blksz - preamble;
5473 size -= preamble;
5474 }
5475 }
5476
5477 postamble = P2PHASE(size, blksz);
5478 size -= postamble;
5479
5480 fullblk = size / blksz;
5481 (void) dmu_xuio_init(xuio,
5482 (preamble != 0) + fullblk + (postamble != 0));
5483 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5484 int, postamble, int,
5485 (preamble != 0) + fullblk + (postamble != 0));
5486
5487 /*
5488 * Have to fix iov base/len for partial buffers. They
5489 * currently represent full arc_buf's.
5490 */
5491 if (preamble) {
5492 /* data begins in the middle of the arc_buf */
5493 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5494 blksz);
5495 ASSERT(abuf);
5496 (void) dmu_xuio_add(xuio, abuf,
5497 blksz - preamble, preamble);
5498 }
5499
5500 for (i = 0; i < fullblk; i++) {
5501 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5502 blksz);
5503 ASSERT(abuf);
5504 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5505 }
5506
5507 if (postamble) {
5508 /* data ends in the middle of the arc_buf */
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, postamble);
5513 }
5514 break;
5515 case UIO_READ:
5516 /*
5517 * Loan out an arc_buf for read if the read size is larger than
5518 * the current file block size. Block alignment is not
5519 * considered. Partial arc_buf will be loaned out for read.
5520 */
5521 blksz = zp->z_blksz;
5522 if (blksz < zcr_blksz_min)
5523 blksz = zcr_blksz_min;
5524 if (blksz > zcr_blksz_max)
5525 blksz = zcr_blksz_max;
5526 /* avoid potential complexity of dealing with it */
5527 if (blksz > max_blksz) {
5528 ZFS_EXIT(zfsvfs);
5529 return (SET_ERROR(EINVAL));
5530 }
5531
5532 maxsize = zp->z_size - uio->uio_loffset;
5533 if (size > maxsize)
5534 size = maxsize;
5535
5536 if (size < blksz || vn_has_cached_data(vp)) {
5537 ZFS_EXIT(zfsvfs);
5538 return (SET_ERROR(EINVAL));
5539 }
5540 break;
5541 default:
5542 ZFS_EXIT(zfsvfs);
5543 return (SET_ERROR(EINVAL));
5544 }
5545
5546 uio->uio_extflg = UIO_XUIO;
5547 XUIO_XUZC_RW(xuio) = ioflag;
5548 ZFS_EXIT(zfsvfs);
5549 return (0);
5550 }
5551
5552 /*ARGSUSED*/
5553 static int
zfs_retzcbuf(vnode_t * vp,xuio_t * xuio,cred_t * cr,caller_context_t * ct)5554 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5555 {
5556 int i;
5557 arc_buf_t *abuf;
5558 int ioflag = XUIO_XUZC_RW(xuio);
5559
5560 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5561
5562 i = dmu_xuio_cnt(xuio);
5563 while (i-- > 0) {
5564 abuf = dmu_xuio_arcbuf(xuio, i);
5565 /*
5566 * if abuf == NULL, it must be a write buffer
5567 * that has been returned in zfs_write().
5568 */
5569 if (abuf)
5570 dmu_return_arcbuf(abuf);
5571 ASSERT(abuf || ioflag == UIO_WRITE);
5572 }
5573
5574 dmu_xuio_fini(xuio);
5575 return (0);
5576 }
5577
5578 /*
5579 * Predeclare these here so that the compiler assumes that
5580 * this is an "old style" function declaration that does
5581 * not include arguments => we won't get type mismatch errors
5582 * in the initializations that follow.
5583 */
5584 static int zfs_inval();
5585 static int zfs_isdir();
5586
5587 static int
zfs_inval()5588 zfs_inval()
5589 {
5590 return (SET_ERROR(EINVAL));
5591 }
5592
5593 static int
zfs_isdir()5594 zfs_isdir()
5595 {
5596 return (SET_ERROR(EISDIR));
5597 }
5598 /*
5599 * Directory vnode operations template
5600 */
5601 vnodeops_t *zfs_dvnodeops;
5602 const fs_operation_def_t zfs_dvnodeops_template[] = {
5603 VOPNAME_OPEN, { .vop_open = zfs_open },
5604 VOPNAME_CLOSE, { .vop_close = zfs_close },
5605 VOPNAME_READ, { .error = zfs_isdir },
5606 VOPNAME_WRITE, { .error = zfs_isdir },
5607 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5608 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5609 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5610 VOPNAME_ACCESS, { .vop_access = zfs_access },
5611 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5612 VOPNAME_CREATE, { .vop_create = zfs_create },
5613 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5614 VOPNAME_LINK, { .vop_link = zfs_link },
5615 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5616 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5617 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5618 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5619 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5620 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5621 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5622 VOPNAME_FID, { .vop_fid = zfs_fid },
5623 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5624 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5625 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5626 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5627 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5628 NULL, NULL
5629 };
5630
5631 /*
5632 * Regular file vnode operations template
5633 */
5634 vnodeops_t *zfs_fvnodeops;
5635 const fs_operation_def_t zfs_fvnodeops_template[] = {
5636 VOPNAME_OPEN, { .vop_open = zfs_open },
5637 VOPNAME_CLOSE, { .vop_close = zfs_close },
5638 VOPNAME_READ, { .vop_read = zfs_read },
5639 VOPNAME_WRITE, { .vop_write = zfs_write },
5640 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5641 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5642 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5643 VOPNAME_ACCESS, { .vop_access = zfs_access },
5644 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5645 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5646 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5647 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5648 VOPNAME_FID, { .vop_fid = zfs_fid },
5649 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5650 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5651 VOPNAME_SPACE, { .vop_space = zfs_space },
5652 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5653 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5654 VOPNAME_MAP, { .vop_map = zfs_map },
5655 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5656 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5657 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5658 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5659 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5660 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5661 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5662 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5663 NULL, NULL
5664 };
5665
5666 /*
5667 * Symbolic link vnode operations template
5668 */
5669 vnodeops_t *zfs_symvnodeops;
5670 const fs_operation_def_t zfs_symvnodeops_template[] = {
5671 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5672 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5673 VOPNAME_ACCESS, { .vop_access = zfs_access },
5674 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5675 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5676 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5677 VOPNAME_FID, { .vop_fid = zfs_fid },
5678 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5679 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5680 NULL, NULL
5681 };
5682
5683 /*
5684 * special share hidden files vnode operations template
5685 */
5686 vnodeops_t *zfs_sharevnodeops;
5687 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5688 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5689 VOPNAME_ACCESS, { .vop_access = zfs_access },
5690 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5691 VOPNAME_FID, { .vop_fid = zfs_fid },
5692 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5693 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5694 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5695 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5696 NULL, NULL
5697 };
5698
5699 /*
5700 * Extended attribute directory vnode operations template
5701 *
5702 * This template is identical to the directory vnodes
5703 * operation template except for restricted operations:
5704 * VOP_MKDIR()
5705 * VOP_SYMLINK()
5706 *
5707 * Note that there are other restrictions embedded in:
5708 * zfs_create() - restrict type to VREG
5709 * zfs_link() - no links into/out of attribute space
5710 * zfs_rename() - no moves into/out of attribute space
5711 */
5712 vnodeops_t *zfs_xdvnodeops;
5713 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5714 VOPNAME_OPEN, { .vop_open = zfs_open },
5715 VOPNAME_CLOSE, { .vop_close = zfs_close },
5716 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5717 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5718 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5719 VOPNAME_ACCESS, { .vop_access = zfs_access },
5720 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5721 VOPNAME_CREATE, { .vop_create = zfs_create },
5722 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5723 VOPNAME_LINK, { .vop_link = zfs_link },
5724 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5725 VOPNAME_MKDIR, { .error = zfs_inval },
5726 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5727 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5728 VOPNAME_SYMLINK, { .error = zfs_inval },
5729 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5730 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5731 VOPNAME_FID, { .vop_fid = zfs_fid },
5732 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5733 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5734 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5735 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5736 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5737 NULL, NULL
5738 };
5739
5740 /*
5741 * Error vnode operations template
5742 */
5743 vnodeops_t *zfs_evnodeops;
5744 const fs_operation_def_t zfs_evnodeops_template[] = {
5745 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5746 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5747 NULL, NULL
5748 };
5749 #endif /* illumos */
5750
5751 static int
ioflags(int ioflags)5752 ioflags(int ioflags)
5753 {
5754 int flags = 0;
5755
5756 if (ioflags & IO_APPEND)
5757 flags |= FAPPEND;
5758 if (ioflags & IO_NDELAY)
5759 flags |= FNONBLOCK;
5760 if (ioflags & IO_SYNC)
5761 flags |= (FSYNC | FDSYNC | FRSYNC);
5762
5763 return (flags);
5764 }
5765
5766 static int
zfs_getpages(struct vnode * vp,vm_page_t * m,int count,int * rbehind,int * rahead)5767 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int *rbehind,
5768 int *rahead)
5769 {
5770 znode_t *zp = VTOZ(vp);
5771 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5772 objset_t *os = zp->z_zfsvfs->z_os;
5773 vm_page_t mlast;
5774 vm_object_t object;
5775 caddr_t va;
5776 struct sf_buf *sf;
5777 off_t startoff, endoff;
5778 int i, error;
5779 vm_pindex_t reqstart, reqend;
5780 int lsize, reqsize, size;
5781
5782 object = m[0]->object;
5783 error = 0;
5784
5785 ZFS_ENTER(zfsvfs);
5786 ZFS_VERIFY_ZP(zp);
5787
5788 zfs_vmobject_wlock(object);
5789 if (m[count - 1]->valid != 0 && --count == 0) {
5790 zfs_vmobject_wunlock(object);
5791 goto out;
5792 }
5793
5794 mlast = m[count - 1];
5795
5796 if (IDX_TO_OFF(mlast->pindex) >=
5797 object->un_pager.vnp.vnp_size) {
5798 zfs_vmobject_wunlock(object);
5799 ZFS_EXIT(zfsvfs);
5800 return (zfs_vm_pagerret_bad);
5801 }
5802
5803 PCPU_INC(cnt.v_vnodein);
5804 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5805
5806 lsize = PAGE_SIZE;
5807 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5808 lsize = object->un_pager.vnp.vnp_size -
5809 IDX_TO_OFF(mlast->pindex);
5810 zfs_vmobject_wunlock(object);
5811
5812 for (i = 0; i < count; i++) {
5813 size = PAGE_SIZE;
5814 if (i == count - 1)
5815 size = lsize;
5816 va = zfs_map_page(m[i], &sf);
5817 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5818 size, va, DMU_READ_PREFETCH);
5819 if (size != PAGE_SIZE)
5820 bzero(va + size, PAGE_SIZE - size);
5821 zfs_unmap_page(sf);
5822 if (error != 0)
5823 goto out;
5824 }
5825
5826 zfs_vmobject_wlock(object);
5827 for (i = 0; i < count; i++)
5828 m[i]->valid = VM_PAGE_BITS_ALL;
5829 zfs_vmobject_wunlock(object);
5830
5831 out:
5832 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5833 ZFS_EXIT(zfsvfs);
5834 if (error == 0) {
5835 if (rbehind)
5836 *rbehind = 0;
5837 if (rahead)
5838 *rahead = 0;
5839 return (zfs_vm_pagerret_ok);
5840 } else
5841 return (zfs_vm_pagerret_error);
5842 }
5843
5844 static int
zfs_freebsd_getpages(ap)5845 zfs_freebsd_getpages(ap)
5846 struct vop_getpages_args /* {
5847 struct vnode *a_vp;
5848 vm_page_t *a_m;
5849 int a_count;
5850 int *a_rbehind;
5851 int *a_rahead;
5852 } */ *ap;
5853 {
5854
5855 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
5856 ap->a_rahead));
5857 }
5858
5859 static int
zfs_putpages(struct vnode * vp,vm_page_t * ma,size_t len,int flags,int * rtvals)5860 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5861 int *rtvals)
5862 {
5863 znode_t *zp = VTOZ(vp);
5864 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5865 rl_t *rl;
5866 dmu_tx_t *tx;
5867 struct sf_buf *sf;
5868 vm_object_t object;
5869 vm_page_t m;
5870 caddr_t va;
5871 size_t tocopy;
5872 size_t lo_len;
5873 vm_ooffset_t lo_off;
5874 vm_ooffset_t off;
5875 uint_t blksz;
5876 int ncount;
5877 int pcount;
5878 int err;
5879 int i;
5880
5881 ZFS_ENTER(zfsvfs);
5882 ZFS_VERIFY_ZP(zp);
5883
5884 object = vp->v_object;
5885 pcount = btoc(len);
5886 ncount = pcount;
5887
5888 KASSERT(ma[0]->object == object, ("mismatching object"));
5889 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5890
5891 for (i = 0; i < pcount; i++)
5892 rtvals[i] = zfs_vm_pagerret_error;
5893
5894 off = IDX_TO_OFF(ma[0]->pindex);
5895 blksz = zp->z_blksz;
5896 lo_off = rounddown(off, blksz);
5897 lo_len = roundup(len + (off - lo_off), blksz);
5898 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5899
5900 zfs_vmobject_wlock(object);
5901 if (len + off > object->un_pager.vnp.vnp_size) {
5902 if (object->un_pager.vnp.vnp_size > off) {
5903 int pgoff;
5904
5905 len = object->un_pager.vnp.vnp_size - off;
5906 ncount = btoc(len);
5907 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5908 /*
5909 * If the object is locked and the following
5910 * conditions hold, then the page's dirty
5911 * field cannot be concurrently changed by a
5912 * pmap operation.
5913 */
5914 m = ma[ncount - 1];
5915 vm_page_assert_sbusied(m);
5916 KASSERT(!pmap_page_is_write_mapped(m),
5917 ("zfs_putpages: page %p is not read-only", m));
5918 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5919 pgoff);
5920 }
5921 } else {
5922 len = 0;
5923 ncount = 0;
5924 }
5925 if (ncount < pcount) {
5926 for (i = ncount; i < pcount; i++) {
5927 rtvals[i] = zfs_vm_pagerret_bad;
5928 }
5929 }
5930 }
5931 zfs_vmobject_wunlock(object);
5932
5933 if (ncount == 0)
5934 goto out;
5935
5936 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5937 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5938 goto out;
5939 }
5940
5941 top:
5942 tx = dmu_tx_create(zfsvfs->z_os);
5943 dmu_tx_hold_write(tx, zp->z_id, off, len);
5944
5945 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5946 zfs_sa_upgrade_txholds(tx, zp);
5947 err = dmu_tx_assign(tx, TXG_NOWAIT);
5948 if (err != 0) {
5949 if (err == ERESTART) {
5950 dmu_tx_wait(tx);
5951 dmu_tx_abort(tx);
5952 goto top;
5953 }
5954 dmu_tx_abort(tx);
5955 goto out;
5956 }
5957
5958 if (zp->z_blksz < PAGE_SIZE) {
5959 i = 0;
5960 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5961 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5962 va = zfs_map_page(ma[i], &sf);
5963 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5964 zfs_unmap_page(sf);
5965 }
5966 } else {
5967 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5968 }
5969
5970 if (err == 0) {
5971 uint64_t mtime[2], ctime[2];
5972 sa_bulk_attr_t bulk[3];
5973 int count = 0;
5974
5975 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5976 &mtime, 16);
5977 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5978 &ctime, 16);
5979 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5980 &zp->z_pflags, 8);
5981 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
5982 B_TRUE);
5983 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
5984 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
5985
5986 zfs_vmobject_wlock(object);
5987 for (i = 0; i < ncount; i++) {
5988 rtvals[i] = zfs_vm_pagerret_ok;
5989 vm_page_undirty(ma[i]);
5990 }
5991 zfs_vmobject_wunlock(object);
5992 PCPU_INC(cnt.v_vnodeout);
5993 PCPU_ADD(cnt.v_vnodepgsout, ncount);
5994 }
5995 dmu_tx_commit(tx);
5996
5997 out:
5998 zfs_range_unlock(rl);
5999 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
6000 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
6001 zil_commit(zfsvfs->z_log, zp->z_id);
6002 ZFS_EXIT(zfsvfs);
6003 return (rtvals[0]);
6004 }
6005
6006 int
zfs_freebsd_putpages(ap)6007 zfs_freebsd_putpages(ap)
6008 struct vop_putpages_args /* {
6009 struct vnode *a_vp;
6010 vm_page_t *a_m;
6011 int a_count;
6012 int a_sync;
6013 int *a_rtvals;
6014 } */ *ap;
6015 {
6016
6017 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
6018 ap->a_rtvals));
6019 }
6020
6021 static int
zfs_freebsd_bmap(ap)6022 zfs_freebsd_bmap(ap)
6023 struct vop_bmap_args /* {
6024 struct vnode *a_vp;
6025 daddr_t a_bn;
6026 struct bufobj **a_bop;
6027 daddr_t *a_bnp;
6028 int *a_runp;
6029 int *a_runb;
6030 } */ *ap;
6031 {
6032
6033 if (ap->a_bop != NULL)
6034 *ap->a_bop = &ap->a_vp->v_bufobj;
6035 if (ap->a_bnp != NULL)
6036 *ap->a_bnp = ap->a_bn;
6037 if (ap->a_runp != NULL)
6038 *ap->a_runp = 0;
6039 if (ap->a_runb != NULL)
6040 *ap->a_runb = 0;
6041
6042 return (0);
6043 }
6044
6045 static int
zfs_freebsd_open(ap)6046 zfs_freebsd_open(ap)
6047 struct vop_open_args /* {
6048 struct vnode *a_vp;
6049 int a_mode;
6050 struct ucred *a_cred;
6051 struct thread *a_td;
6052 } */ *ap;
6053 {
6054 vnode_t *vp = ap->a_vp;
6055 znode_t *zp = VTOZ(vp);
6056 int error;
6057
6058 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
6059 if (error == 0)
6060 vnode_create_vobject(vp, zp->z_size, ap->a_td);
6061 return (error);
6062 }
6063
6064 static int
zfs_freebsd_close(ap)6065 zfs_freebsd_close(ap)
6066 struct vop_close_args /* {
6067 struct vnode *a_vp;
6068 int a_fflag;
6069 struct ucred *a_cred;
6070 struct thread *a_td;
6071 } */ *ap;
6072 {
6073
6074 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6075 }
6076
6077 static int
zfs_freebsd_ioctl(ap)6078 zfs_freebsd_ioctl(ap)
6079 struct vop_ioctl_args /* {
6080 struct vnode *a_vp;
6081 u_long a_command;
6082 caddr_t a_data;
6083 int a_fflag;
6084 struct ucred *cred;
6085 struct thread *td;
6086 } */ *ap;
6087 {
6088
6089 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6090 ap->a_fflag, ap->a_cred, NULL, NULL));
6091 }
6092
6093 static int
zfs_freebsd_read(ap)6094 zfs_freebsd_read(ap)
6095 struct vop_read_args /* {
6096 struct vnode *a_vp;
6097 struct uio *a_uio;
6098 int a_ioflag;
6099 struct ucred *a_cred;
6100 } */ *ap;
6101 {
6102
6103 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6104 ap->a_cred, NULL));
6105 }
6106
6107 static int
zfs_freebsd_write(ap)6108 zfs_freebsd_write(ap)
6109 struct vop_write_args /* {
6110 struct vnode *a_vp;
6111 struct uio *a_uio;
6112 int a_ioflag;
6113 struct ucred *a_cred;
6114 } */ *ap;
6115 {
6116
6117 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6118 ap->a_cred, NULL));
6119 }
6120
6121 static int
zfs_freebsd_access(ap)6122 zfs_freebsd_access(ap)
6123 struct vop_access_args /* {
6124 struct vnode *a_vp;
6125 accmode_t a_accmode;
6126 struct ucred *a_cred;
6127 struct thread *a_td;
6128 } */ *ap;
6129 {
6130 vnode_t *vp = ap->a_vp;
6131 znode_t *zp = VTOZ(vp);
6132 accmode_t accmode;
6133 int error = 0;
6134
6135 /*
6136 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6137 */
6138 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6139 if (accmode != 0)
6140 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6141
6142 /*
6143 * VADMIN has to be handled by vaccess().
6144 */
6145 if (error == 0) {
6146 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6147 if (accmode != 0) {
6148 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6149 zp->z_gid, accmode, ap->a_cred, NULL);
6150 }
6151 }
6152
6153 /*
6154 * For VEXEC, ensure that at least one execute bit is set for
6155 * non-directories.
6156 */
6157 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6158 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6159 error = EACCES;
6160 }
6161
6162 return (error);
6163 }
6164
6165 static int
zfs_freebsd_lookup(ap)6166 zfs_freebsd_lookup(ap)
6167 struct vop_lookup_args /* {
6168 struct vnode *a_dvp;
6169 struct vnode **a_vpp;
6170 struct componentname *a_cnp;
6171 } */ *ap;
6172 {
6173 struct componentname *cnp = ap->a_cnp;
6174 char nm[NAME_MAX + 1];
6175
6176 ASSERT(cnp->cn_namelen < sizeof(nm));
6177 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6178
6179 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6180 cnp->cn_cred, cnp->cn_thread, 0));
6181 }
6182
6183 static int
zfs_freebsd_create(ap)6184 zfs_freebsd_create(ap)
6185 struct vop_create_args /* {
6186 struct vnode *a_dvp;
6187 struct vnode **a_vpp;
6188 struct componentname *a_cnp;
6189 struct vattr *a_vap;
6190 } */ *ap;
6191 {
6192 struct componentname *cnp = ap->a_cnp;
6193 vattr_t *vap = ap->a_vap;
6194 int error, mode;
6195
6196 ASSERT(cnp->cn_flags & SAVENAME);
6197
6198 vattr_init_mask(vap);
6199 mode = vap->va_mode & ALLPERMS;
6200
6201 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6202 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
6203 #ifdef FREEBSD_NAMECACHE
6204 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
6205 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
6206 #endif
6207 return (error);
6208 }
6209
6210 static int
zfs_freebsd_remove(ap)6211 zfs_freebsd_remove(ap)
6212 struct vop_remove_args /* {
6213 struct vnode *a_dvp;
6214 struct vnode *a_vp;
6215 struct componentname *a_cnp;
6216 } */ *ap;
6217 {
6218
6219 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6220
6221 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6222 ap->a_cnp->cn_cred, NULL, 0));
6223 }
6224
6225 static int
zfs_freebsd_mkdir(ap)6226 zfs_freebsd_mkdir(ap)
6227 struct vop_mkdir_args /* {
6228 struct vnode *a_dvp;
6229 struct vnode **a_vpp;
6230 struct componentname *a_cnp;
6231 struct vattr *a_vap;
6232 } */ *ap;
6233 {
6234 vattr_t *vap = ap->a_vap;
6235
6236 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6237
6238 vattr_init_mask(vap);
6239
6240 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6241 ap->a_cnp->cn_cred, NULL, 0, NULL));
6242 }
6243
6244 static int
zfs_freebsd_rmdir(ap)6245 zfs_freebsd_rmdir(ap)
6246 struct vop_rmdir_args /* {
6247 struct vnode *a_dvp;
6248 struct vnode *a_vp;
6249 struct componentname *a_cnp;
6250 } */ *ap;
6251 {
6252 struct componentname *cnp = ap->a_cnp;
6253
6254 ASSERT(cnp->cn_flags & SAVENAME);
6255
6256 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6257 }
6258
6259 static int
zfs_freebsd_readdir(ap)6260 zfs_freebsd_readdir(ap)
6261 struct vop_readdir_args /* {
6262 struct vnode *a_vp;
6263 struct uio *a_uio;
6264 struct ucred *a_cred;
6265 int *a_eofflag;
6266 int *a_ncookies;
6267 u_long **a_cookies;
6268 } */ *ap;
6269 {
6270
6271 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6272 ap->a_ncookies, ap->a_cookies));
6273 }
6274
6275 static int
zfs_freebsd_fsync(ap)6276 zfs_freebsd_fsync(ap)
6277 struct vop_fsync_args /* {
6278 struct vnode *a_vp;
6279 int a_waitfor;
6280 struct thread *a_td;
6281 } */ *ap;
6282 {
6283
6284 vop_stdfsync(ap);
6285 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6286 }
6287
6288 static int
zfs_freebsd_getattr(ap)6289 zfs_freebsd_getattr(ap)
6290 struct vop_getattr_args /* {
6291 struct vnode *a_vp;
6292 struct vattr *a_vap;
6293 struct ucred *a_cred;
6294 } */ *ap;
6295 {
6296 vattr_t *vap = ap->a_vap;
6297 xvattr_t xvap;
6298 u_long fflags = 0;
6299 int error;
6300
6301 xva_init(&xvap);
6302 xvap.xva_vattr = *vap;
6303 xvap.xva_vattr.va_mask |= AT_XVATTR;
6304
6305 /* Convert chflags into ZFS-type flags. */
6306 /* XXX: what about SF_SETTABLE?. */
6307 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6308 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6309 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6310 XVA_SET_REQ(&xvap, XAT_NODUMP);
6311 XVA_SET_REQ(&xvap, XAT_READONLY);
6312 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
6313 XVA_SET_REQ(&xvap, XAT_SYSTEM);
6314 XVA_SET_REQ(&xvap, XAT_HIDDEN);
6315 XVA_SET_REQ(&xvap, XAT_REPARSE);
6316 XVA_SET_REQ(&xvap, XAT_OFFLINE);
6317 XVA_SET_REQ(&xvap, XAT_SPARSE);
6318
6319 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6320 if (error != 0)
6321 return (error);
6322
6323 /* Convert ZFS xattr into chflags. */
6324 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6325 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6326 fflags |= (fflag); \
6327 } while (0)
6328 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6329 xvap.xva_xoptattrs.xoa_immutable);
6330 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6331 xvap.xva_xoptattrs.xoa_appendonly);
6332 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6333 xvap.xva_xoptattrs.xoa_nounlink);
6334 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
6335 xvap.xva_xoptattrs.xoa_archive);
6336 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6337 xvap.xva_xoptattrs.xoa_nodump);
6338 FLAG_CHECK(UF_READONLY, XAT_READONLY,
6339 xvap.xva_xoptattrs.xoa_readonly);
6340 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
6341 xvap.xva_xoptattrs.xoa_system);
6342 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
6343 xvap.xva_xoptattrs.xoa_hidden);
6344 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
6345 xvap.xva_xoptattrs.xoa_reparse);
6346 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
6347 xvap.xva_xoptattrs.xoa_offline);
6348 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
6349 xvap.xva_xoptattrs.xoa_sparse);
6350
6351 #undef FLAG_CHECK
6352 *vap = xvap.xva_vattr;
6353 vap->va_flags = fflags;
6354 return (0);
6355 }
6356
6357 static int
zfs_freebsd_setattr(ap)6358 zfs_freebsd_setattr(ap)
6359 struct vop_setattr_args /* {
6360 struct vnode *a_vp;
6361 struct vattr *a_vap;
6362 struct ucred *a_cred;
6363 } */ *ap;
6364 {
6365 vnode_t *vp = ap->a_vp;
6366 vattr_t *vap = ap->a_vap;
6367 cred_t *cred = ap->a_cred;
6368 xvattr_t xvap;
6369 u_long fflags;
6370 uint64_t zflags;
6371
6372 vattr_init_mask(vap);
6373 vap->va_mask &= ~AT_NOSET;
6374
6375 xva_init(&xvap);
6376 xvap.xva_vattr = *vap;
6377
6378 zflags = VTOZ(vp)->z_pflags;
6379
6380 if (vap->va_flags != VNOVAL) {
6381 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6382 int error;
6383
6384 if (zfsvfs->z_use_fuids == B_FALSE)
6385 return (EOPNOTSUPP);
6386
6387 fflags = vap->va_flags;
6388 /*
6389 * XXX KDM
6390 * We need to figure out whether it makes sense to allow
6391 * UF_REPARSE through, since we don't really have other
6392 * facilities to handle reparse points and zfs_setattr()
6393 * doesn't currently allow setting that attribute anyway.
6394 */
6395 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
6396 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
6397 UF_OFFLINE|UF_SPARSE)) != 0)
6398 return (EOPNOTSUPP);
6399 /*
6400 * Unprivileged processes are not permitted to unset system
6401 * flags, or modify flags if any system flags are set.
6402 * Privileged non-jail processes may not modify system flags
6403 * if securelevel > 0 and any existing system flags are set.
6404 * Privileged jail processes behave like privileged non-jail
6405 * processes if the security.jail.chflags_allowed sysctl is
6406 * is non-zero; otherwise, they behave like unprivileged
6407 * processes.
6408 */
6409 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6410 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6411 if (zflags &
6412 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6413 error = securelevel_gt(cred, 0);
6414 if (error != 0)
6415 return (error);
6416 }
6417 } else {
6418 /*
6419 * Callers may only modify the file flags on objects they
6420 * have VADMIN rights for.
6421 */
6422 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6423 return (error);
6424 if (zflags &
6425 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6426 return (EPERM);
6427 }
6428 if (fflags &
6429 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6430 return (EPERM);
6431 }
6432 }
6433
6434 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6435 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6436 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6437 XVA_SET_REQ(&xvap, (xflag)); \
6438 (xfield) = ((fflags & (fflag)) != 0); \
6439 } \
6440 } while (0)
6441 /* Convert chflags into ZFS-type flags. */
6442 /* XXX: what about SF_SETTABLE?. */
6443 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6444 xvap.xva_xoptattrs.xoa_immutable);
6445 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6446 xvap.xva_xoptattrs.xoa_appendonly);
6447 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6448 xvap.xva_xoptattrs.xoa_nounlink);
6449 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
6450 xvap.xva_xoptattrs.xoa_archive);
6451 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6452 xvap.xva_xoptattrs.xoa_nodump);
6453 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
6454 xvap.xva_xoptattrs.xoa_readonly);
6455 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
6456 xvap.xva_xoptattrs.xoa_system);
6457 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
6458 xvap.xva_xoptattrs.xoa_hidden);
6459 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
6460 xvap.xva_xoptattrs.xoa_hidden);
6461 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
6462 xvap.xva_xoptattrs.xoa_offline);
6463 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
6464 xvap.xva_xoptattrs.xoa_sparse);
6465 #undef FLAG_CHANGE
6466 }
6467 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6468 }
6469
6470 static int
zfs_freebsd_rename(ap)6471 zfs_freebsd_rename(ap)
6472 struct vop_rename_args /* {
6473 struct vnode *a_fdvp;
6474 struct vnode *a_fvp;
6475 struct componentname *a_fcnp;
6476 struct vnode *a_tdvp;
6477 struct vnode *a_tvp;
6478 struct componentname *a_tcnp;
6479 } */ *ap;
6480 {
6481 vnode_t *fdvp = ap->a_fdvp;
6482 vnode_t *fvp = ap->a_fvp;
6483 vnode_t *tdvp = ap->a_tdvp;
6484 vnode_t *tvp = ap->a_tvp;
6485 int error;
6486
6487 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6488 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6489
6490 /*
6491 * Check for cross-device rename.
6492 */
6493 if ((fdvp->v_mount != tdvp->v_mount) ||
6494 (tvp && (fdvp->v_mount != tvp->v_mount)))
6495 error = EXDEV;
6496 else
6497 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6498 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6499 if (tdvp == tvp)
6500 VN_RELE(tdvp);
6501 else
6502 VN_URELE(tdvp);
6503 if (tvp)
6504 VN_URELE(tvp);
6505 VN_RELE(fdvp);
6506 VN_RELE(fvp);
6507
6508 return (error);
6509 }
6510
6511 static int
zfs_freebsd_symlink(ap)6512 zfs_freebsd_symlink(ap)
6513 struct vop_symlink_args /* {
6514 struct vnode *a_dvp;
6515 struct vnode **a_vpp;
6516 struct componentname *a_cnp;
6517 struct vattr *a_vap;
6518 char *a_target;
6519 } */ *ap;
6520 {
6521 struct componentname *cnp = ap->a_cnp;
6522 vattr_t *vap = ap->a_vap;
6523
6524 ASSERT(cnp->cn_flags & SAVENAME);
6525
6526 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6527 vattr_init_mask(vap);
6528
6529 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6530 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6531 }
6532
6533 static int
zfs_freebsd_readlink(ap)6534 zfs_freebsd_readlink(ap)
6535 struct vop_readlink_args /* {
6536 struct vnode *a_vp;
6537 struct uio *a_uio;
6538 struct ucred *a_cred;
6539 } */ *ap;
6540 {
6541
6542 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6543 }
6544
6545 static int
zfs_freebsd_link(ap)6546 zfs_freebsd_link(ap)
6547 struct vop_link_args /* {
6548 struct vnode *a_tdvp;
6549 struct vnode *a_vp;
6550 struct componentname *a_cnp;
6551 } */ *ap;
6552 {
6553 struct componentname *cnp = ap->a_cnp;
6554 vnode_t *vp = ap->a_vp;
6555 vnode_t *tdvp = ap->a_tdvp;
6556
6557 if (tdvp->v_mount != vp->v_mount)
6558 return (EXDEV);
6559
6560 ASSERT(cnp->cn_flags & SAVENAME);
6561
6562 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6563 }
6564
6565 static int
zfs_freebsd_inactive(ap)6566 zfs_freebsd_inactive(ap)
6567 struct vop_inactive_args /* {
6568 struct vnode *a_vp;
6569 struct thread *a_td;
6570 } */ *ap;
6571 {
6572 vnode_t *vp = ap->a_vp;
6573
6574 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6575 return (0);
6576 }
6577
6578 static int
zfs_freebsd_reclaim(ap)6579 zfs_freebsd_reclaim(ap)
6580 struct vop_reclaim_args /* {
6581 struct vnode *a_vp;
6582 struct thread *a_td;
6583 } */ *ap;
6584 {
6585 vnode_t *vp = ap->a_vp;
6586 znode_t *zp = VTOZ(vp);
6587 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6588
6589 ASSERT(zp != NULL);
6590
6591 /* Destroy the vm object and flush associated pages. */
6592 vnode_destroy_vobject(vp);
6593
6594 /*
6595 * z_teardown_inactive_lock protects from a race with
6596 * zfs_znode_dmu_fini in zfsvfs_teardown during
6597 * force unmount.
6598 */
6599 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6600 if (zp->z_sa_hdl == NULL)
6601 zfs_znode_free(zp);
6602 else
6603 zfs_zinactive(zp);
6604 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6605
6606 vp->v_data = NULL;
6607 return (0);
6608 }
6609
6610 static int
zfs_freebsd_fid(ap)6611 zfs_freebsd_fid(ap)
6612 struct vop_fid_args /* {
6613 struct vnode *a_vp;
6614 struct fid *a_fid;
6615 } */ *ap;
6616 {
6617
6618 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6619 }
6620
6621 static int
zfs_freebsd_pathconf(ap)6622 zfs_freebsd_pathconf(ap)
6623 struct vop_pathconf_args /* {
6624 struct vnode *a_vp;
6625 int a_name;
6626 register_t *a_retval;
6627 } */ *ap;
6628 {
6629 ulong_t val;
6630 int error;
6631
6632 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6633 if (error == 0)
6634 *ap->a_retval = val;
6635 else if (error == EOPNOTSUPP)
6636 error = vop_stdpathconf(ap);
6637 return (error);
6638 }
6639
6640 static int
zfs_freebsd_fifo_pathconf(ap)6641 zfs_freebsd_fifo_pathconf(ap)
6642 struct vop_pathconf_args /* {
6643 struct vnode *a_vp;
6644 int a_name;
6645 register_t *a_retval;
6646 } */ *ap;
6647 {
6648
6649 switch (ap->a_name) {
6650 case _PC_ACL_EXTENDED:
6651 case _PC_ACL_NFS4:
6652 case _PC_ACL_PATH_MAX:
6653 case _PC_MAC_PRESENT:
6654 return (zfs_freebsd_pathconf(ap));
6655 default:
6656 return (fifo_specops.vop_pathconf(ap));
6657 }
6658 }
6659
6660 /*
6661 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6662 * extended attribute name:
6663 *
6664 * NAMESPACE PREFIX
6665 * system freebsd:system:
6666 * user (none, can be used to access ZFS fsattr(5) attributes
6667 * created on Solaris)
6668 */
6669 static int
zfs_create_attrname(int attrnamespace,const char * name,char * attrname,size_t size)6670 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6671 size_t size)
6672 {
6673 const char *namespace, *prefix, *suffix;
6674
6675 /* We don't allow '/' character in attribute name. */
6676 if (strchr(name, '/') != NULL)
6677 return (EINVAL);
6678 /* We don't allow attribute names that start with "freebsd:" string. */
6679 if (strncmp(name, "freebsd:", 8) == 0)
6680 return (EINVAL);
6681
6682 bzero(attrname, size);
6683
6684 switch (attrnamespace) {
6685 case EXTATTR_NAMESPACE_USER:
6686 #if 0
6687 prefix = "freebsd:";
6688 namespace = EXTATTR_NAMESPACE_USER_STRING;
6689 suffix = ":";
6690 #else
6691 /*
6692 * This is the default namespace by which we can access all
6693 * attributes created on Solaris.
6694 */
6695 prefix = namespace = suffix = "";
6696 #endif
6697 break;
6698 case EXTATTR_NAMESPACE_SYSTEM:
6699 prefix = "freebsd:";
6700 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6701 suffix = ":";
6702 break;
6703 case EXTATTR_NAMESPACE_EMPTY:
6704 default:
6705 return (EINVAL);
6706 }
6707 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6708 name) >= size) {
6709 return (ENAMETOOLONG);
6710 }
6711 return (0);
6712 }
6713
6714 /*
6715 * Vnode operating to retrieve a named extended attribute.
6716 */
6717 static int
zfs_getextattr(struct vop_getextattr_args * ap)6718 zfs_getextattr(struct vop_getextattr_args *ap)
6719 /*
6720 vop_getextattr {
6721 IN struct vnode *a_vp;
6722 IN int a_attrnamespace;
6723 IN const char *a_name;
6724 INOUT struct uio *a_uio;
6725 OUT size_t *a_size;
6726 IN struct ucred *a_cred;
6727 IN struct thread *a_td;
6728 };
6729 */
6730 {
6731 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6732 struct thread *td = ap->a_td;
6733 struct nameidata nd;
6734 char attrname[255];
6735 struct vattr va;
6736 vnode_t *xvp = NULL, *vp;
6737 int error, flags;
6738
6739 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6740 ap->a_cred, ap->a_td, VREAD);
6741 if (error != 0)
6742 return (error);
6743
6744 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6745 sizeof(attrname));
6746 if (error != 0)
6747 return (error);
6748
6749 ZFS_ENTER(zfsvfs);
6750
6751 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6752 LOOKUP_XATTR);
6753 if (error != 0) {
6754 ZFS_EXIT(zfsvfs);
6755 return (error);
6756 }
6757
6758 flags = FREAD;
6759 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6760 xvp, td);
6761 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6762 vp = nd.ni_vp;
6763 NDFREE(&nd, NDF_ONLY_PNBUF);
6764 if (error != 0) {
6765 ZFS_EXIT(zfsvfs);
6766 if (error == ENOENT)
6767 error = ENOATTR;
6768 return (error);
6769 }
6770
6771 if (ap->a_size != NULL) {
6772 error = VOP_GETATTR(vp, &va, ap->a_cred);
6773 if (error == 0)
6774 *ap->a_size = (size_t)va.va_size;
6775 } else if (ap->a_uio != NULL)
6776 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6777
6778 VOP_UNLOCK(vp, 0);
6779 vn_close(vp, flags, ap->a_cred, td);
6780 ZFS_EXIT(zfsvfs);
6781
6782 return (error);
6783 }
6784
6785 /*
6786 * Vnode operation to remove a named attribute.
6787 */
6788 int
zfs_deleteextattr(struct vop_deleteextattr_args * ap)6789 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6790 /*
6791 vop_deleteextattr {
6792 IN struct vnode *a_vp;
6793 IN int a_attrnamespace;
6794 IN const char *a_name;
6795 IN struct ucred *a_cred;
6796 IN struct thread *a_td;
6797 };
6798 */
6799 {
6800 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6801 struct thread *td = ap->a_td;
6802 struct nameidata nd;
6803 char attrname[255];
6804 struct vattr va;
6805 vnode_t *xvp = NULL, *vp;
6806 int error, flags;
6807
6808 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6809 ap->a_cred, ap->a_td, VWRITE);
6810 if (error != 0)
6811 return (error);
6812
6813 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6814 sizeof(attrname));
6815 if (error != 0)
6816 return (error);
6817
6818 ZFS_ENTER(zfsvfs);
6819
6820 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6821 LOOKUP_XATTR);
6822 if (error != 0) {
6823 ZFS_EXIT(zfsvfs);
6824 return (error);
6825 }
6826
6827 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
6828 UIO_SYSSPACE, attrname, xvp, td);
6829 error = namei(&nd);
6830 vp = nd.ni_vp;
6831 if (error != 0) {
6832 ZFS_EXIT(zfsvfs);
6833 NDFREE(&nd, NDF_ONLY_PNBUF);
6834 if (error == ENOENT)
6835 error = ENOATTR;
6836 return (error);
6837 }
6838
6839 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6840 NDFREE(&nd, NDF_ONLY_PNBUF);
6841
6842 vput(nd.ni_dvp);
6843 if (vp == nd.ni_dvp)
6844 vrele(vp);
6845 else
6846 vput(vp);
6847 ZFS_EXIT(zfsvfs);
6848
6849 return (error);
6850 }
6851
6852 /*
6853 * Vnode operation to set a named attribute.
6854 */
6855 static int
zfs_setextattr(struct vop_setextattr_args * ap)6856 zfs_setextattr(struct vop_setextattr_args *ap)
6857 /*
6858 vop_setextattr {
6859 IN struct vnode *a_vp;
6860 IN int a_attrnamespace;
6861 IN const char *a_name;
6862 INOUT struct uio *a_uio;
6863 IN struct ucred *a_cred;
6864 IN struct thread *a_td;
6865 };
6866 */
6867 {
6868 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6869 struct thread *td = ap->a_td;
6870 struct nameidata nd;
6871 char attrname[255];
6872 struct vattr va;
6873 vnode_t *xvp = NULL, *vp;
6874 int error, flags;
6875
6876 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6877 ap->a_cred, ap->a_td, VWRITE);
6878 if (error != 0)
6879 return (error);
6880
6881 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6882 sizeof(attrname));
6883 if (error != 0)
6884 return (error);
6885
6886 ZFS_ENTER(zfsvfs);
6887
6888 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6889 LOOKUP_XATTR | CREATE_XATTR_DIR);
6890 if (error != 0) {
6891 ZFS_EXIT(zfsvfs);
6892 return (error);
6893 }
6894
6895 flags = FFLAGS(O_WRONLY | O_CREAT);
6896 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6897 xvp, td);
6898 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6899 vp = nd.ni_vp;
6900 NDFREE(&nd, NDF_ONLY_PNBUF);
6901 if (error != 0) {
6902 ZFS_EXIT(zfsvfs);
6903 return (error);
6904 }
6905
6906 VATTR_NULL(&va);
6907 va.va_size = 0;
6908 error = VOP_SETATTR(vp, &va, ap->a_cred);
6909 if (error == 0)
6910 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6911
6912 VOP_UNLOCK(vp, 0);
6913 vn_close(vp, flags, ap->a_cred, td);
6914 ZFS_EXIT(zfsvfs);
6915
6916 return (error);
6917 }
6918
6919 /*
6920 * Vnode operation to retrieve extended attributes on a vnode.
6921 */
6922 static int
zfs_listextattr(struct vop_listextattr_args * ap)6923 zfs_listextattr(struct vop_listextattr_args *ap)
6924 /*
6925 vop_listextattr {
6926 IN struct vnode *a_vp;
6927 IN int a_attrnamespace;
6928 INOUT struct uio *a_uio;
6929 OUT size_t *a_size;
6930 IN struct ucred *a_cred;
6931 IN struct thread *a_td;
6932 };
6933 */
6934 {
6935 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6936 struct thread *td = ap->a_td;
6937 struct nameidata nd;
6938 char attrprefix[16];
6939 u_char dirbuf[sizeof(struct dirent)];
6940 struct dirent *dp;
6941 struct iovec aiov;
6942 struct uio auio, *uio = ap->a_uio;
6943 size_t *sizep = ap->a_size;
6944 size_t plen;
6945 vnode_t *xvp = NULL, *vp;
6946 int done, error, eof, pos;
6947
6948 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6949 ap->a_cred, ap->a_td, VREAD);
6950 if (error != 0)
6951 return (error);
6952
6953 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6954 sizeof(attrprefix));
6955 if (error != 0)
6956 return (error);
6957 plen = strlen(attrprefix);
6958
6959 ZFS_ENTER(zfsvfs);
6960
6961 if (sizep != NULL)
6962 *sizep = 0;
6963
6964 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6965 LOOKUP_XATTR);
6966 if (error != 0) {
6967 ZFS_EXIT(zfsvfs);
6968 /*
6969 * ENOATTR means that the EA directory does not yet exist,
6970 * i.e. there are no extended attributes there.
6971 */
6972 if (error == ENOATTR)
6973 error = 0;
6974 return (error);
6975 }
6976
6977 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
6978 UIO_SYSSPACE, ".", xvp, td);
6979 error = namei(&nd);
6980 vp = nd.ni_vp;
6981 NDFREE(&nd, NDF_ONLY_PNBUF);
6982 if (error != 0) {
6983 ZFS_EXIT(zfsvfs);
6984 return (error);
6985 }
6986
6987 auio.uio_iov = &aiov;
6988 auio.uio_iovcnt = 1;
6989 auio.uio_segflg = UIO_SYSSPACE;
6990 auio.uio_td = td;
6991 auio.uio_rw = UIO_READ;
6992 auio.uio_offset = 0;
6993
6994 do {
6995 u_char nlen;
6996
6997 aiov.iov_base = (void *)dirbuf;
6998 aiov.iov_len = sizeof(dirbuf);
6999 auio.uio_resid = sizeof(dirbuf);
7000 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
7001 done = sizeof(dirbuf) - auio.uio_resid;
7002 if (error != 0)
7003 break;
7004 for (pos = 0; pos < done;) {
7005 dp = (struct dirent *)(dirbuf + pos);
7006 pos += dp->d_reclen;
7007 /*
7008 * XXX: Temporarily we also accept DT_UNKNOWN, as this
7009 * is what we get when attribute was created on Solaris.
7010 */
7011 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
7012 continue;
7013 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
7014 continue;
7015 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
7016 continue;
7017 nlen = dp->d_namlen - plen;
7018 if (sizep != NULL)
7019 *sizep += 1 + nlen;
7020 else if (uio != NULL) {
7021 /*
7022 * Format of extattr name entry is one byte for
7023 * length and the rest for name.
7024 */
7025 error = uiomove(&nlen, 1, uio->uio_rw, uio);
7026 if (error == 0) {
7027 error = uiomove(dp->d_name + plen, nlen,
7028 uio->uio_rw, uio);
7029 }
7030 if (error != 0)
7031 break;
7032 }
7033 }
7034 } while (!eof && error == 0);
7035
7036 vput(vp);
7037 ZFS_EXIT(zfsvfs);
7038
7039 return (error);
7040 }
7041
7042 int
zfs_freebsd_getacl(ap)7043 zfs_freebsd_getacl(ap)
7044 struct vop_getacl_args /* {
7045 struct vnode *vp;
7046 acl_type_t type;
7047 struct acl *aclp;
7048 struct ucred *cred;
7049 struct thread *td;
7050 } */ *ap;
7051 {
7052 int error;
7053 vsecattr_t vsecattr;
7054
7055 if (ap->a_type != ACL_TYPE_NFS4)
7056 return (EINVAL);
7057
7058 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
7059 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
7060 return (error);
7061
7062 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
7063 if (vsecattr.vsa_aclentp != NULL)
7064 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
7065
7066 return (error);
7067 }
7068
7069 int
zfs_freebsd_setacl(ap)7070 zfs_freebsd_setacl(ap)
7071 struct vop_setacl_args /* {
7072 struct vnode *vp;
7073 acl_type_t type;
7074 struct acl *aclp;
7075 struct ucred *cred;
7076 struct thread *td;
7077 } */ *ap;
7078 {
7079 int error;
7080 vsecattr_t vsecattr;
7081 int aclbsize; /* size of acl list in bytes */
7082 aclent_t *aaclp;
7083
7084 if (ap->a_type != ACL_TYPE_NFS4)
7085 return (EINVAL);
7086
7087 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
7088 return (EINVAL);
7089
7090 /*
7091 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
7092 * splitting every entry into two and appending "canonical six"
7093 * entries at the end. Don't allow for setting an ACL that would
7094 * cause chmod(2) to run out of ACL entries.
7095 */
7096 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
7097 return (ENOSPC);
7098
7099 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
7100 if (error != 0)
7101 return (error);
7102
7103 vsecattr.vsa_mask = VSA_ACE;
7104 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
7105 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
7106 aaclp = vsecattr.vsa_aclentp;
7107 vsecattr.vsa_aclentsz = aclbsize;
7108
7109 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
7110 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
7111 kmem_free(aaclp, aclbsize);
7112
7113 return (error);
7114 }
7115
7116 int
zfs_freebsd_aclcheck(ap)7117 zfs_freebsd_aclcheck(ap)
7118 struct vop_aclcheck_args /* {
7119 struct vnode *vp;
7120 acl_type_t type;
7121 struct acl *aclp;
7122 struct ucred *cred;
7123 struct thread *td;
7124 } */ *ap;
7125 {
7126
7127 return (EOPNOTSUPP);
7128 }
7129
7130 struct vop_vector zfs_vnodeops;
7131 struct vop_vector zfs_fifoops;
7132 struct vop_vector zfs_shareops;
7133
7134 struct vop_vector zfs_vnodeops = {
7135 .vop_default = &default_vnodeops,
7136 .vop_inactive = zfs_freebsd_inactive,
7137 .vop_reclaim = zfs_freebsd_reclaim,
7138 .vop_access = zfs_freebsd_access,
7139 #ifdef FREEBSD_NAMECACHE
7140 .vop_lookup = vfs_cache_lookup,
7141 .vop_cachedlookup = zfs_freebsd_lookup,
7142 #else
7143 .vop_lookup = zfs_freebsd_lookup,
7144 #endif
7145 .vop_getattr = zfs_freebsd_getattr,
7146 .vop_setattr = zfs_freebsd_setattr,
7147 .vop_create = zfs_freebsd_create,
7148 .vop_mknod = zfs_freebsd_create,
7149 .vop_mkdir = zfs_freebsd_mkdir,
7150 .vop_readdir = zfs_freebsd_readdir,
7151 .vop_fsync = zfs_freebsd_fsync,
7152 .vop_open = zfs_freebsd_open,
7153 .vop_close = zfs_freebsd_close,
7154 .vop_rmdir = zfs_freebsd_rmdir,
7155 .vop_ioctl = zfs_freebsd_ioctl,
7156 .vop_link = zfs_freebsd_link,
7157 .vop_symlink = zfs_freebsd_symlink,
7158 .vop_readlink = zfs_freebsd_readlink,
7159 .vop_read = zfs_freebsd_read,
7160 .vop_write = zfs_freebsd_write,
7161 .vop_remove = zfs_freebsd_remove,
7162 .vop_rename = zfs_freebsd_rename,
7163 .vop_pathconf = zfs_freebsd_pathconf,
7164 .vop_bmap = zfs_freebsd_bmap,
7165 .vop_fid = zfs_freebsd_fid,
7166 .vop_getextattr = zfs_getextattr,
7167 .vop_deleteextattr = zfs_deleteextattr,
7168 .vop_setextattr = zfs_setextattr,
7169 .vop_listextattr = zfs_listextattr,
7170 .vop_getacl = zfs_freebsd_getacl,
7171 .vop_setacl = zfs_freebsd_setacl,
7172 .vop_aclcheck = zfs_freebsd_aclcheck,
7173 .vop_getpages = zfs_freebsd_getpages,
7174 .vop_putpages = zfs_freebsd_putpages,
7175 };
7176
7177 struct vop_vector zfs_fifoops = {
7178 .vop_default = &fifo_specops,
7179 .vop_fsync = zfs_freebsd_fsync,
7180 .vop_access = zfs_freebsd_access,
7181 .vop_getattr = zfs_freebsd_getattr,
7182 .vop_inactive = zfs_freebsd_inactive,
7183 .vop_read = VOP_PANIC,
7184 .vop_reclaim = zfs_freebsd_reclaim,
7185 .vop_setattr = zfs_freebsd_setattr,
7186 .vop_write = VOP_PANIC,
7187 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7188 .vop_fid = zfs_freebsd_fid,
7189 .vop_getacl = zfs_freebsd_getacl,
7190 .vop_setacl = zfs_freebsd_setacl,
7191 .vop_aclcheck = zfs_freebsd_aclcheck,
7192 };
7193
7194 /*
7195 * special share hidden files vnode operations template
7196 */
7197 struct vop_vector zfs_shareops = {
7198 .vop_default = &default_vnodeops,
7199 .vop_access = zfs_freebsd_access,
7200 .vop_inactive = zfs_freebsd_inactive,
7201 .vop_reclaim = zfs_freebsd_reclaim,
7202 .vop_fid = zfs_freebsd_fid,
7203 .vop_pathconf = zfs_freebsd_pathconf,
7204 };
7205