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