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