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