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