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