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