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