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