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