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