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