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