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