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