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