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