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