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