]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_vnops.c
Reintroduce zfs_remove() synchronous deletes
[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_CACHE_SIZE-1);
337 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
338 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
339
340 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
341 if (pp) {
342 if (mapping_writably_mapped(mp))
343 flush_dcache_page(pp);
344
345 pb = kmap(pp);
346 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
347 DMU_READ_PREFETCH);
348 kunmap(pp);
349
350 if (mapping_writably_mapped(mp))
351 flush_dcache_page(pp);
352
353 mark_page_accessed(pp);
354 SetPageUptodate(pp);
355 ClearPageError(pp);
356 unlock_page(pp);
357 page_cache_release(pp);
358 }
359
360 len -= nbytes;
361 off = 0;
362 }
363 }
364
365 /*
366 * When a file is memory mapped, we must keep the IO data synchronized
367 * between the DMU cache and the memory mapped pages. What this means:
368 *
369 * On Read: We "read" preferentially from memory mapped pages,
370 * else we default from the dmu buffer.
371 *
372 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
373 * the file is memory mapped.
374 */
375 static int
376 mappedread(struct inode *ip, int nbytes, uio_t *uio)
377 {
378 struct address_space *mp = ip->i_mapping;
379 struct page *pp;
380 znode_t *zp = ITOZ(ip);
381 int64_t start, off;
382 uint64_t bytes;
383 int len = nbytes;
384 int error = 0;
385 void *pb;
386
387 start = uio->uio_loffset;
388 off = start & (PAGE_CACHE_SIZE-1);
389 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
390 bytes = MIN(PAGE_CACHE_SIZE - off, len);
391
392 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
393 if (pp) {
394 ASSERT(PageUptodate(pp));
395
396 pb = kmap(pp);
397 error = uiomove(pb + off, bytes, UIO_READ, uio);
398 kunmap(pp);
399
400 if (mapping_writably_mapped(mp))
401 flush_dcache_page(pp);
402
403 mark_page_accessed(pp);
404 unlock_page(pp);
405 page_cache_release(pp);
406 } else {
407 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
408 uio, bytes);
409 }
410
411 len -= bytes;
412 off = 0;
413 if (error)
414 break;
415 }
416 return (error);
417 }
418 #endif /* _KERNEL */
419
420 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
421 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, uio->uio_loffset, uio->uio_resid, RL_READER);
487
488 /*
489 * If we are reading past end-of-file we can skip
490 * to the end; but we might still need to set atime.
491 */
492 if (uio->uio_loffset >= zp->z_size) {
493 error = 0;
494 goto out;
495 }
496
497 ASSERT(uio->uio_loffset < zp->z_size);
498 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
499
500 #ifdef HAVE_UIO_ZEROCOPY
501 if ((uio->uio_extflg == UIO_XUIO) &&
502 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
503 int nblk;
504 int blksz = zp->z_blksz;
505 uint64_t offset = uio->uio_loffset;
506
507 xuio = (xuio_t *)uio;
508 if ((ISP2(blksz))) {
509 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
510 blksz)) / blksz;
511 } else {
512 ASSERT(offset + n <= blksz);
513 nblk = 1;
514 }
515 (void) dmu_xuio_init(xuio, nblk);
516
517 if (vn_has_cached_data(ip)) {
518 /*
519 * For simplicity, we always allocate a full buffer
520 * even if we only expect to read a portion of a block.
521 */
522 while (--nblk >= 0) {
523 (void) dmu_xuio_add(xuio,
524 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
525 blksz), 0, blksz);
526 }
527 }
528 }
529 #endif /* HAVE_UIO_ZEROCOPY */
530
531 while (n > 0) {
532 nbytes = MIN(n, zfs_read_chunk_size -
533 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
534
535 if (zp->z_is_mapped && !(ioflag & O_DIRECT)) {
536 error = mappedread(ip, nbytes, uio);
537 } else {
538 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
539 uio, nbytes);
540 }
541
542 if (error) {
543 /* convert checksum errors into IO errors */
544 if (error == ECKSUM)
545 error = SET_ERROR(EIO);
546 break;
547 }
548
549 n -= nbytes;
550 }
551 out:
552 zfs_range_unlock(rl);
553
554 ZFS_ACCESSTIME_STAMP(zsb, zp);
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, 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, 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 B_TRUE);
880
881 /*
882 * Update the file size (zp_size) if it has changed;
883 * account for possible concurrent updates.
884 */
885 while ((end_size = zp->z_size) < uio->uio_loffset) {
886 (void) atomic_cas_64(&zp->z_size, end_size,
887 uio->uio_loffset);
888 ASSERT(error == 0);
889 }
890 /*
891 * If we are replaying and eof is non zero then force
892 * the file size to the specified eof. Note, there's no
893 * concurrency during replay.
894 */
895 if (zsb->z_replay && zsb->z_replay_eof != 0)
896 zp->z_size = zsb->z_replay_eof;
897
898 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
899
900 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
901 NULL, NULL);
902 dmu_tx_commit(tx);
903
904 if (error != 0)
905 break;
906 ASSERT(tx_bytes == nbytes);
907 n -= nbytes;
908
909 if (!xuio && n > 0)
910 uio_prefaultpages(MIN(n, max_blksz), uio);
911 }
912
913 zfs_inode_update(zp);
914 zfs_range_unlock(rl);
915
916 /*
917 * If we're in replay mode, or we made no progress, return error.
918 * Otherwise, it's at least a partial write, so it's successful.
919 */
920 if (zsb->z_replay || uio->uio_resid == start_resid) {
921 ZFS_EXIT(zsb);
922 return (error);
923 }
924
925 if (ioflag & (FSYNC | FDSYNC) ||
926 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
927 zil_commit(zilog, zp->z_id);
928
929 ZFS_EXIT(zsb);
930 return (0);
931 }
932 EXPORT_SYMBOL(zfs_write);
933
934 void
935 zfs_iput_async(struct inode *ip)
936 {
937 objset_t *os = ITOZSB(ip)->z_os;
938
939 ASSERT(atomic_read(&ip->i_count) > 0);
940 ASSERT(os != NULL);
941
942 if (atomic_read(&ip->i_count) == 1)
943 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
944 (task_func_t *)iput, ip, TQ_SLEEP);
945 else
946 iput(ip);
947 }
948
949 void
950 zfs_get_done(zgd_t *zgd, int error)
951 {
952 znode_t *zp = zgd->zgd_private;
953
954 if (zgd->zgd_db)
955 dmu_buf_rele(zgd->zgd_db, zgd);
956
957 zfs_range_unlock(zgd->zgd_rl);
958
959 /*
960 * Release the vnode asynchronously as we currently have the
961 * txg stopped from syncing.
962 */
963 zfs_iput_async(ZTOI(zp));
964
965 if (error == 0 && zgd->zgd_bp)
966 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
967
968 kmem_free(zgd, sizeof (zgd_t));
969 }
970
971 #ifdef DEBUG
972 static int zil_fault_io = 0;
973 #endif
974
975 /*
976 * Get data to generate a TX_WRITE intent log record.
977 */
978 int
979 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
980 {
981 zfs_sb_t *zsb = arg;
982 objset_t *os = zsb->z_os;
983 znode_t *zp;
984 uint64_t object = lr->lr_foid;
985 uint64_t offset = lr->lr_offset;
986 uint64_t size = lr->lr_length;
987 blkptr_t *bp = &lr->lr_blkptr;
988 dmu_buf_t *db;
989 zgd_t *zgd;
990 int error = 0;
991
992 ASSERT(zio != NULL);
993 ASSERT(size != 0);
994
995 /*
996 * Nothing to do if the file has been removed
997 */
998 if (zfs_zget(zsb, object, &zp) != 0)
999 return (SET_ERROR(ENOENT));
1000 if (zp->z_unlinked) {
1001 /*
1002 * Release the vnode asynchronously as we currently have the
1003 * txg stopped from syncing.
1004 */
1005 zfs_iput_async(ZTOI(zp));
1006 return (SET_ERROR(ENOENT));
1007 }
1008
1009 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1010 zgd->zgd_zilog = zsb->z_log;
1011 zgd->zgd_private = zp;
1012
1013 /*
1014 * Write records come in two flavors: immediate and indirect.
1015 * For small writes it's cheaper to store the data with the
1016 * log record (immediate); for large writes it's cheaper to
1017 * sync the data and get a pointer to it (indirect) so that
1018 * we don't have to write the data twice.
1019 */
1020 if (buf != NULL) { /* immediate write */
1021 zgd->zgd_rl = zfs_range_lock(zp, offset, size, 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, offset, size,
1043 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)
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 zfs_dirlock_t *dl;
1528 dmu_tx_t *tx;
1529 boolean_t may_delete_now, delete_now = FALSE;
1530 boolean_t unlinked, toobig = FALSE;
1531 uint64_t txtype;
1532 pathname_t *realnmp = NULL;
1533 #ifdef HAVE_PN_UTILS
1534 pathname_t realnm;
1535 #endif /* HAVE_PN_UTILS */
1536 int error;
1537 int zflg = ZEXISTS;
1538 boolean_t waited = B_FALSE;
1539
1540 ZFS_ENTER(zsb);
1541 ZFS_VERIFY_ZP(dzp);
1542 zilog = zsb->z_log;
1543
1544 #ifdef HAVE_PN_UTILS
1545 if (flags & FIGNORECASE) {
1546 zflg |= ZCILOOK;
1547 pn_alloc(&realnm);
1548 realnmp = &realnm;
1549 }
1550 #endif /* HAVE_PN_UTILS */
1551
1552 top:
1553 xattr_obj = 0;
1554 xzp = NULL;
1555 /*
1556 * Attempt to lock directory; fail if entry doesn't exist.
1557 */
1558 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1559 NULL, realnmp))) {
1560 #ifdef HAVE_PN_UTILS
1561 if (realnmp)
1562 pn_free(realnmp);
1563 #endif /* HAVE_PN_UTILS */
1564 ZFS_EXIT(zsb);
1565 return (error);
1566 }
1567
1568 ip = ZTOI(zp);
1569
1570 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1571 goto out;
1572 }
1573
1574 /*
1575 * Need to use rmdir for removing directories.
1576 */
1577 if (S_ISDIR(ip->i_mode)) {
1578 error = SET_ERROR(EPERM);
1579 goto out;
1580 }
1581
1582 #ifdef HAVE_DNLC
1583 if (realnmp)
1584 dnlc_remove(dvp, realnmp->pn_buf);
1585 else
1586 dnlc_remove(dvp, name);
1587 #endif /* HAVE_DNLC */
1588
1589 mutex_enter(&zp->z_lock);
1590 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1591 mutex_exit(&zp->z_lock);
1592
1593 /*
1594 * We may delete the znode now, or we may put it in the unlinked set;
1595 * it depends on whether we're the last link, and on whether there are
1596 * other holds on the inode. So we dmu_tx_hold() the right things to
1597 * allow for either case.
1598 */
1599 obj = zp->z_id;
1600 tx = dmu_tx_create(zsb->z_os);
1601 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1602 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1603 zfs_sa_upgrade_txholds(tx, zp);
1604 zfs_sa_upgrade_txholds(tx, dzp);
1605 if (may_delete_now) {
1606 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1607 /* if the file is too big, only hold_free a token amount */
1608 dmu_tx_hold_free(tx, zp->z_id, 0,
1609 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1610 }
1611
1612 /* are there any extended attributes? */
1613 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1614 &xattr_obj, sizeof (xattr_obj));
1615 if (error == 0 && xattr_obj) {
1616 error = zfs_zget(zsb, xattr_obj, &xzp);
1617 ASSERT0(error);
1618 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1619 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1620 }
1621
1622 mutex_enter(&zp->z_lock);
1623 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1624 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1625 mutex_exit(&zp->z_lock);
1626
1627 /* charge as an update -- would be nice not to charge at all */
1628 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1629
1630 /*
1631 * Mark this transaction as typically resulting in a net free of
1632 * space, unless object removal will be delayed indefinitely
1633 * (due to active holds on the vnode due to the file being open).
1634 */
1635 if (may_delete_now)
1636 dmu_tx_mark_netfree(tx);
1637
1638 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1639 if (error) {
1640 zfs_dirent_unlock(dl);
1641 iput(ip);
1642 if (xzp)
1643 iput(ZTOI(xzp));
1644 if (error == ERESTART) {
1645 waited = B_TRUE;
1646 dmu_tx_wait(tx);
1647 dmu_tx_abort(tx);
1648 goto top;
1649 }
1650 #ifdef HAVE_PN_UTILS
1651 if (realnmp)
1652 pn_free(realnmp);
1653 #endif /* HAVE_PN_UTILS */
1654 dmu_tx_abort(tx);
1655 ZFS_EXIT(zsb);
1656 return (error);
1657 }
1658
1659 /*
1660 * Remove the directory entry.
1661 */
1662 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1663
1664 if (error) {
1665 dmu_tx_commit(tx);
1666 goto out;
1667 }
1668
1669 if (unlinked) {
1670 /*
1671 * Hold z_lock so that we can make sure that the ACL obj
1672 * hasn't changed. Could have been deleted due to
1673 * zfs_sa_upgrade().
1674 */
1675 mutex_enter(&zp->z_lock);
1676 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1677 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1678 delete_now = may_delete_now && !toobig &&
1679 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1680 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1681 acl_obj;
1682 }
1683
1684 if (delete_now) {
1685 if (xattr_obj_unlinked) {
1686 ASSERT3U(xzp->z_links, ==, 2);
1687 mutex_enter(&xzp->z_lock);
1688 xzp->z_unlinked = 1;
1689 xzp->z_links = 0;
1690 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zsb),
1691 &xzp->z_links, sizeof (xzp->z_links), tx);
1692 ASSERT3U(error, ==, 0);
1693 mutex_exit(&xzp->z_lock);
1694 zfs_unlinked_add(xzp, tx);
1695
1696 if (zp->z_is_sa)
1697 error = sa_remove(zp->z_sa_hdl,
1698 SA_ZPL_XATTR(zsb), tx);
1699 else
1700 error = sa_update(zp->z_sa_hdl,
1701 SA_ZPL_XATTR(zsb), &null_xattr,
1702 sizeof (uint64_t), tx);
1703 ASSERT0(error);
1704 }
1705 /*
1706 * Add to the unlinked set because a new reference could be
1707 * taken concurrently resulting in a deferred destruction.
1708 */
1709 zfs_unlinked_add(zp, tx);
1710 mutex_exit(&zp->z_lock);
1711 zfs_inode_update(zp);
1712 iput(ip);
1713 } else if (unlinked) {
1714 mutex_exit(&zp->z_lock);
1715 zfs_unlinked_add(zp, tx);
1716 }
1717
1718 txtype = TX_REMOVE;
1719 #ifdef HAVE_PN_UTILS
1720 if (flags & FIGNORECASE)
1721 txtype |= TX_CI;
1722 #endif /* HAVE_PN_UTILS */
1723 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1724
1725 dmu_tx_commit(tx);
1726 out:
1727 #ifdef HAVE_PN_UTILS
1728 if (realnmp)
1729 pn_free(realnmp);
1730 #endif /* HAVE_PN_UTILS */
1731
1732 zfs_dirent_unlock(dl);
1733 zfs_inode_update(dzp);
1734
1735 if (!delete_now) {
1736 zfs_inode_update(zp);
1737 zfs_iput_async(ip);
1738 }
1739
1740 if (xzp) {
1741 zfs_inode_update(xzp);
1742 zfs_iput_async(ZTOI(xzp));
1743 }
1744
1745 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1746 zil_commit(zilog, 0);
1747
1748 ZFS_EXIT(zsb);
1749 return (error);
1750 }
1751 EXPORT_SYMBOL(zfs_remove);
1752
1753 /*
1754 * Create a new directory and insert it into dip using the name
1755 * provided. Return a pointer to the inserted directory.
1756 *
1757 * IN: dip - inode of directory to add subdir to.
1758 * dirname - name of new directory.
1759 * vap - attributes of new directory.
1760 * cr - credentials of caller.
1761 * vsecp - ACL to be set
1762 *
1763 * OUT: ipp - inode of created directory.
1764 *
1765 * RETURN: 0 if success
1766 * error code if failure
1767 *
1768 * Timestamps:
1769 * dip - ctime|mtime updated
1770 * ipp - ctime|mtime|atime updated
1771 */
1772 /*ARGSUSED*/
1773 int
1774 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1775 cred_t *cr, int flags, vsecattr_t *vsecp)
1776 {
1777 znode_t *zp, *dzp = ITOZ(dip);
1778 zfs_sb_t *zsb = ITOZSB(dip);
1779 zilog_t *zilog;
1780 zfs_dirlock_t *dl;
1781 uint64_t txtype;
1782 dmu_tx_t *tx;
1783 int error;
1784 int zf = ZNEW;
1785 uid_t uid;
1786 gid_t gid = crgetgid(cr);
1787 zfs_acl_ids_t acl_ids;
1788 boolean_t fuid_dirtied;
1789 boolean_t waited = B_FALSE;
1790
1791 ASSERT(S_ISDIR(vap->va_mode));
1792
1793 /*
1794 * If we have an ephemeral id, ACL, or XVATTR then
1795 * make sure file system is at proper version
1796 */
1797
1798 uid = crgetuid(cr);
1799 if (zsb->z_use_fuids == B_FALSE &&
1800 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1801 return (SET_ERROR(EINVAL));
1802
1803 ZFS_ENTER(zsb);
1804 ZFS_VERIFY_ZP(dzp);
1805 zilog = zsb->z_log;
1806
1807 if (dzp->z_pflags & ZFS_XATTR) {
1808 ZFS_EXIT(zsb);
1809 return (SET_ERROR(EINVAL));
1810 }
1811
1812 if (zsb->z_utf8 && u8_validate(dirname,
1813 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1814 ZFS_EXIT(zsb);
1815 return (SET_ERROR(EILSEQ));
1816 }
1817 if (flags & FIGNORECASE)
1818 zf |= ZCILOOK;
1819
1820 if (vap->va_mask & ATTR_XVATTR) {
1821 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1822 crgetuid(cr), cr, vap->va_mode)) != 0) {
1823 ZFS_EXIT(zsb);
1824 return (error);
1825 }
1826 }
1827
1828 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1829 vsecp, &acl_ids)) != 0) {
1830 ZFS_EXIT(zsb);
1831 return (error);
1832 }
1833 /*
1834 * First make sure the new directory doesn't exist.
1835 *
1836 * Existence is checked first to make sure we don't return
1837 * EACCES instead of EEXIST which can cause some applications
1838 * to fail.
1839 */
1840 top:
1841 *ipp = NULL;
1842
1843 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1844 NULL, NULL))) {
1845 zfs_acl_ids_free(&acl_ids);
1846 ZFS_EXIT(zsb);
1847 return (error);
1848 }
1849
1850 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1851 zfs_acl_ids_free(&acl_ids);
1852 zfs_dirent_unlock(dl);
1853 ZFS_EXIT(zsb);
1854 return (error);
1855 }
1856
1857 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1858 zfs_acl_ids_free(&acl_ids);
1859 zfs_dirent_unlock(dl);
1860 ZFS_EXIT(zsb);
1861 return (SET_ERROR(EDQUOT));
1862 }
1863
1864 /*
1865 * Add a new entry to the directory.
1866 */
1867 tx = dmu_tx_create(zsb->z_os);
1868 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1869 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1870 fuid_dirtied = zsb->z_fuid_dirty;
1871 if (fuid_dirtied)
1872 zfs_fuid_txhold(zsb, tx);
1873 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1874 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1875 acl_ids.z_aclp->z_acl_bytes);
1876 }
1877
1878 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1879 ZFS_SA_BASE_ATTR_SIZE);
1880
1881 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1882 if (error) {
1883 zfs_dirent_unlock(dl);
1884 if (error == ERESTART) {
1885 waited = B_TRUE;
1886 dmu_tx_wait(tx);
1887 dmu_tx_abort(tx);
1888 goto top;
1889 }
1890 zfs_acl_ids_free(&acl_ids);
1891 dmu_tx_abort(tx);
1892 ZFS_EXIT(zsb);
1893 return (error);
1894 }
1895
1896 /*
1897 * Create new node.
1898 */
1899 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1900
1901 if (fuid_dirtied)
1902 zfs_fuid_sync(zsb, tx);
1903
1904 /*
1905 * Now put new name in parent dir.
1906 */
1907 (void) zfs_link_create(dl, zp, tx, ZNEW);
1908
1909 *ipp = ZTOI(zp);
1910
1911 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1912 if (flags & FIGNORECASE)
1913 txtype |= TX_CI;
1914 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1915 acl_ids.z_fuidp, vap);
1916
1917 zfs_acl_ids_free(&acl_ids);
1918
1919 dmu_tx_commit(tx);
1920
1921 zfs_dirent_unlock(dl);
1922
1923 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1924 zil_commit(zilog, 0);
1925
1926 zfs_inode_update(dzp);
1927 zfs_inode_update(zp);
1928 ZFS_EXIT(zsb);
1929 return (0);
1930 }
1931 EXPORT_SYMBOL(zfs_mkdir);
1932
1933 /*
1934 * Remove a directory subdir entry. If the current working
1935 * directory is the same as the subdir to be removed, the
1936 * remove will fail.
1937 *
1938 * IN: dip - inode of directory to remove from.
1939 * name - name of directory to be removed.
1940 * cwd - inode of current working directory.
1941 * cr - credentials of caller.
1942 * flags - case flags
1943 *
1944 * RETURN: 0 on success, error code on failure.
1945 *
1946 * Timestamps:
1947 * dip - ctime|mtime updated
1948 */
1949 /*ARGSUSED*/
1950 int
1951 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1952 int flags)
1953 {
1954 znode_t *dzp = ITOZ(dip);
1955 znode_t *zp;
1956 struct inode *ip;
1957 zfs_sb_t *zsb = ITOZSB(dip);
1958 zilog_t *zilog;
1959 zfs_dirlock_t *dl;
1960 dmu_tx_t *tx;
1961 int error;
1962 int zflg = ZEXISTS;
1963 boolean_t waited = B_FALSE;
1964
1965 ZFS_ENTER(zsb);
1966 ZFS_VERIFY_ZP(dzp);
1967 zilog = zsb->z_log;
1968
1969 if (flags & FIGNORECASE)
1970 zflg |= ZCILOOK;
1971 top:
1972 zp = NULL;
1973
1974 /*
1975 * Attempt to lock directory; fail if entry doesn't exist.
1976 */
1977 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1978 NULL, NULL))) {
1979 ZFS_EXIT(zsb);
1980 return (error);
1981 }
1982
1983 ip = ZTOI(zp);
1984
1985 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1986 goto out;
1987 }
1988
1989 if (!S_ISDIR(ip->i_mode)) {
1990 error = SET_ERROR(ENOTDIR);
1991 goto out;
1992 }
1993
1994 if (ip == cwd) {
1995 error = SET_ERROR(EINVAL);
1996 goto out;
1997 }
1998
1999 /*
2000 * Grab a lock on the directory to make sure that noone is
2001 * trying to add (or lookup) entries while we are removing it.
2002 */
2003 rw_enter(&zp->z_name_lock, RW_WRITER);
2004
2005 /*
2006 * Grab a lock on the parent pointer to make sure we play well
2007 * with the treewalk and directory rename code.
2008 */
2009 rw_enter(&zp->z_parent_lock, RW_WRITER);
2010
2011 tx = dmu_tx_create(zsb->z_os);
2012 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2013 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2014 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
2015 zfs_sa_upgrade_txholds(tx, zp);
2016 zfs_sa_upgrade_txholds(tx, dzp);
2017 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2018 if (error) {
2019 rw_exit(&zp->z_parent_lock);
2020 rw_exit(&zp->z_name_lock);
2021 zfs_dirent_unlock(dl);
2022 iput(ip);
2023 if (error == ERESTART) {
2024 waited = B_TRUE;
2025 dmu_tx_wait(tx);
2026 dmu_tx_abort(tx);
2027 goto top;
2028 }
2029 dmu_tx_abort(tx);
2030 ZFS_EXIT(zsb);
2031 return (error);
2032 }
2033
2034 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2035
2036 if (error == 0) {
2037 uint64_t txtype = TX_RMDIR;
2038 if (flags & FIGNORECASE)
2039 txtype |= TX_CI;
2040 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2041 }
2042
2043 dmu_tx_commit(tx);
2044
2045 rw_exit(&zp->z_parent_lock);
2046 rw_exit(&zp->z_name_lock);
2047 out:
2048 zfs_dirent_unlock(dl);
2049
2050 zfs_inode_update(dzp);
2051 zfs_inode_update(zp);
2052 iput(ip);
2053
2054 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2055 zil_commit(zilog, 0);
2056
2057 ZFS_EXIT(zsb);
2058 return (error);
2059 }
2060 EXPORT_SYMBOL(zfs_rmdir);
2061
2062 /*
2063 * Read as many directory entries as will fit into the provided
2064 * dirent buffer from the given directory cursor position.
2065 *
2066 * IN: ip - inode of directory to read.
2067 * dirent - buffer for directory entries.
2068 *
2069 * OUT: dirent - filler buffer of directory entries.
2070 *
2071 * RETURN: 0 if success
2072 * error code if failure
2073 *
2074 * Timestamps:
2075 * ip - atime updated
2076 *
2077 * Note that the low 4 bits of the cookie returned by zap is always zero.
2078 * This allows us to use the low range for "special" directory entries:
2079 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2080 * we use the offset 2 for the '.zfs' directory.
2081 */
2082 /* ARGSUSED */
2083 int
2084 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2085 {
2086 znode_t *zp = ITOZ(ip);
2087 zfs_sb_t *zsb = ITOZSB(ip);
2088 objset_t *os;
2089 zap_cursor_t zc;
2090 zap_attribute_t zap;
2091 int error;
2092 uint8_t prefetch;
2093 uint8_t type;
2094 int done = 0;
2095 uint64_t parent;
2096 uint64_t offset; /* must be unsigned; checks for < 1 */
2097
2098 ZFS_ENTER(zsb);
2099 ZFS_VERIFY_ZP(zp);
2100
2101 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
2102 &parent, sizeof (parent))) != 0)
2103 goto out;
2104
2105 /*
2106 * Quit if directory has been removed (posix)
2107 */
2108 if (zp->z_unlinked)
2109 goto out;
2110
2111 error = 0;
2112 os = zsb->z_os;
2113 offset = ctx->pos;
2114 prefetch = zp->z_zn_prefetch;
2115
2116 /*
2117 * Initialize the iterator cursor.
2118 */
2119 if (offset <= 3) {
2120 /*
2121 * Start iteration from the beginning of the directory.
2122 */
2123 zap_cursor_init(&zc, os, zp->z_id);
2124 } else {
2125 /*
2126 * The offset is a serialized cursor.
2127 */
2128 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2129 }
2130
2131 /*
2132 * Transform to file-system independent format
2133 */
2134 while (!done) {
2135 uint64_t objnum;
2136 /*
2137 * Special case `.', `..', and `.zfs'.
2138 */
2139 if (offset == 0) {
2140 (void) strcpy(zap.za_name, ".");
2141 zap.za_normalization_conflict = 0;
2142 objnum = zp->z_id;
2143 type = DT_DIR;
2144 } else if (offset == 1) {
2145 (void) strcpy(zap.za_name, "..");
2146 zap.za_normalization_conflict = 0;
2147 objnum = parent;
2148 type = DT_DIR;
2149 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2150 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2151 zap.za_normalization_conflict = 0;
2152 objnum = ZFSCTL_INO_ROOT;
2153 type = DT_DIR;
2154 } else {
2155 /*
2156 * Grab next entry.
2157 */
2158 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2159 if (error == ENOENT)
2160 break;
2161 else
2162 goto update;
2163 }
2164
2165 /*
2166 * Allow multiple entries provided the first entry is
2167 * the object id. Non-zpl consumers may safely make
2168 * use of the additional space.
2169 *
2170 * XXX: This should be a feature flag for compatibility
2171 */
2172 if (zap.za_integer_length != 8 ||
2173 zap.za_num_integers == 0) {
2174 cmn_err(CE_WARN, "zap_readdir: bad directory "
2175 "entry, obj = %lld, offset = %lld, "
2176 "length = %d, num = %lld\n",
2177 (u_longlong_t)zp->z_id,
2178 (u_longlong_t)offset,
2179 zap.za_integer_length,
2180 (u_longlong_t)zap.za_num_integers);
2181 error = SET_ERROR(ENXIO);
2182 goto update;
2183 }
2184
2185 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2186 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2187 }
2188
2189 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2190 objnum, type);
2191 if (done)
2192 break;
2193
2194 /* Prefetch znode */
2195 if (prefetch) {
2196 dmu_prefetch(os, objnum, 0, 0, 0,
2197 ZIO_PRIORITY_SYNC_READ);
2198 }
2199
2200 /*
2201 * Move to the next entry, fill in the previous offset.
2202 */
2203 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2204 zap_cursor_advance(&zc);
2205 offset = zap_cursor_serialize(&zc);
2206 } else {
2207 offset += 1;
2208 }
2209 ctx->pos = offset;
2210 }
2211 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2212
2213 update:
2214 zap_cursor_fini(&zc);
2215 if (error == ENOENT)
2216 error = 0;
2217
2218 ZFS_ACCESSTIME_STAMP(zsb, zp);
2219
2220 out:
2221 ZFS_EXIT(zsb);
2222
2223 return (error);
2224 }
2225 EXPORT_SYMBOL(zfs_readdir);
2226
2227 ulong_t zfs_fsync_sync_cnt = 4;
2228
2229 int
2230 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2231 {
2232 znode_t *zp = ITOZ(ip);
2233 zfs_sb_t *zsb = ITOZSB(ip);
2234
2235 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2236
2237 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2238 ZFS_ENTER(zsb);
2239 ZFS_VERIFY_ZP(zp);
2240 zil_commit(zsb->z_log, zp->z_id);
2241 ZFS_EXIT(zsb);
2242 }
2243 tsd_set(zfs_fsyncer_key, NULL);
2244
2245 return (0);
2246 }
2247 EXPORT_SYMBOL(zfs_fsync);
2248
2249
2250 /*
2251 * Get the requested file attributes and place them in the provided
2252 * vattr structure.
2253 *
2254 * IN: ip - inode of file.
2255 * vap - va_mask identifies requested attributes.
2256 * If ATTR_XVATTR set, then optional attrs are requested
2257 * flags - ATTR_NOACLCHECK (CIFS server context)
2258 * cr - credentials of caller.
2259 *
2260 * OUT: vap - attribute values.
2261 *
2262 * RETURN: 0 (always succeeds)
2263 */
2264 /* ARGSUSED */
2265 int
2266 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2267 {
2268 znode_t *zp = ITOZ(ip);
2269 zfs_sb_t *zsb = ITOZSB(ip);
2270 int error = 0;
2271 uint64_t links;
2272 uint64_t mtime[2], ctime[2];
2273 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2274 xoptattr_t *xoap = NULL;
2275 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2276 sa_bulk_attr_t bulk[2];
2277 int count = 0;
2278
2279 ZFS_ENTER(zsb);
2280 ZFS_VERIFY_ZP(zp);
2281
2282 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2283
2284 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2285 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2286
2287 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2288 ZFS_EXIT(zsb);
2289 return (error);
2290 }
2291
2292 /*
2293 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2294 * Also, if we are the owner don't bother, since owner should
2295 * always be allowed to read basic attributes of file.
2296 */
2297 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2298 (vap->va_uid != crgetuid(cr))) {
2299 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2300 skipaclchk, cr))) {
2301 ZFS_EXIT(zsb);
2302 return (error);
2303 }
2304 }
2305
2306 /*
2307 * Return all attributes. It's cheaper to provide the answer
2308 * than to determine whether we were asked the question.
2309 */
2310
2311 mutex_enter(&zp->z_lock);
2312 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2313 vap->va_mode = zp->z_mode;
2314 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2315 vap->va_nodeid = zp->z_id;
2316 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2317 links = zp->z_links + 1;
2318 else
2319 links = zp->z_links;
2320 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2321 vap->va_size = i_size_read(ip);
2322 vap->va_rdev = ip->i_rdev;
2323 vap->va_seq = ip->i_generation;
2324
2325 /*
2326 * Add in any requested optional attributes and the create time.
2327 * Also set the corresponding bits in the returned attribute bitmap.
2328 */
2329 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2330 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2331 xoap->xoa_archive =
2332 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2333 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2334 }
2335
2336 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2337 xoap->xoa_readonly =
2338 ((zp->z_pflags & ZFS_READONLY) != 0);
2339 XVA_SET_RTN(xvap, XAT_READONLY);
2340 }
2341
2342 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2343 xoap->xoa_system =
2344 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2345 XVA_SET_RTN(xvap, XAT_SYSTEM);
2346 }
2347
2348 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2349 xoap->xoa_hidden =
2350 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2351 XVA_SET_RTN(xvap, XAT_HIDDEN);
2352 }
2353
2354 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2355 xoap->xoa_nounlink =
2356 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2357 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2358 }
2359
2360 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2361 xoap->xoa_immutable =
2362 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2363 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2364 }
2365
2366 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2367 xoap->xoa_appendonly =
2368 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2369 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2370 }
2371
2372 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2373 xoap->xoa_nodump =
2374 ((zp->z_pflags & ZFS_NODUMP) != 0);
2375 XVA_SET_RTN(xvap, XAT_NODUMP);
2376 }
2377
2378 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2379 xoap->xoa_opaque =
2380 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2381 XVA_SET_RTN(xvap, XAT_OPAQUE);
2382 }
2383
2384 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2385 xoap->xoa_av_quarantined =
2386 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2387 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2388 }
2389
2390 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2391 xoap->xoa_av_modified =
2392 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2393 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2394 }
2395
2396 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2397 S_ISREG(ip->i_mode)) {
2398 zfs_sa_get_scanstamp(zp, xvap);
2399 }
2400
2401 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2402 uint64_t times[2];
2403
2404 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2405 times, sizeof (times));
2406 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2407 XVA_SET_RTN(xvap, XAT_CREATETIME);
2408 }
2409
2410 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2411 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2412 XVA_SET_RTN(xvap, XAT_REPARSE);
2413 }
2414 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2415 xoap->xoa_generation = zp->z_gen;
2416 XVA_SET_RTN(xvap, XAT_GEN);
2417 }
2418
2419 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2420 xoap->xoa_offline =
2421 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2422 XVA_SET_RTN(xvap, XAT_OFFLINE);
2423 }
2424
2425 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2426 xoap->xoa_sparse =
2427 ((zp->z_pflags & ZFS_SPARSE) != 0);
2428 XVA_SET_RTN(xvap, XAT_SPARSE);
2429 }
2430 }
2431
2432 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2433 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2434 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2435
2436 mutex_exit(&zp->z_lock);
2437
2438 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2439
2440 if (zp->z_blksz == 0) {
2441 /*
2442 * Block size hasn't been set; suggest maximal I/O transfers.
2443 */
2444 vap->va_blksize = zsb->z_max_blksz;
2445 }
2446
2447 ZFS_EXIT(zsb);
2448 return (0);
2449 }
2450 EXPORT_SYMBOL(zfs_getattr);
2451
2452 /*
2453 * Get the basic file attributes and place them in the provided kstat
2454 * structure. The inode is assumed to be the authoritative source
2455 * for most of the attributes. However, the znode currently has the
2456 * authoritative atime, blksize, and block count.
2457 *
2458 * IN: ip - inode of file.
2459 *
2460 * OUT: sp - kstat values.
2461 *
2462 * RETURN: 0 (always succeeds)
2463 */
2464 /* ARGSUSED */
2465 int
2466 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2467 {
2468 znode_t *zp = ITOZ(ip);
2469 zfs_sb_t *zsb = ITOZSB(ip);
2470 uint32_t blksize;
2471 u_longlong_t nblocks;
2472
2473 ZFS_ENTER(zsb);
2474 ZFS_VERIFY_ZP(zp);
2475
2476 mutex_enter(&zp->z_lock);
2477
2478 generic_fillattr(ip, sp);
2479 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2480
2481 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2482 sp->blksize = blksize;
2483 sp->blocks = nblocks;
2484
2485 if (unlikely(zp->z_blksz == 0)) {
2486 /*
2487 * Block size hasn't been set; suggest maximal I/O transfers.
2488 */
2489 sp->blksize = zsb->z_max_blksz;
2490 }
2491
2492 mutex_exit(&zp->z_lock);
2493
2494 /*
2495 * Required to prevent NFS client from detecting different inode
2496 * numbers of snapshot root dentry before and after snapshot mount.
2497 */
2498 if (zsb->z_issnap) {
2499 if (ip->i_sb->s_root->d_inode == ip)
2500 sp->ino = ZFSCTL_INO_SNAPDIRS -
2501 dmu_objset_id(zsb->z_os);
2502 }
2503
2504 ZFS_EXIT(zsb);
2505
2506 return (0);
2507 }
2508 EXPORT_SYMBOL(zfs_getattr_fast);
2509
2510 /*
2511 * Set the file attributes to the values contained in the
2512 * vattr structure.
2513 *
2514 * IN: ip - inode of file to be modified.
2515 * vap - new attribute values.
2516 * If ATTR_XVATTR set, then optional attrs are being set
2517 * flags - ATTR_UTIME set if non-default time values provided.
2518 * - ATTR_NOACLCHECK (CIFS context only).
2519 * cr - credentials of caller.
2520 *
2521 * RETURN: 0 if success
2522 * error code if failure
2523 *
2524 * Timestamps:
2525 * ip - ctime updated, mtime updated if size changed.
2526 */
2527 /* ARGSUSED */
2528 int
2529 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2530 {
2531 znode_t *zp = ITOZ(ip);
2532 zfs_sb_t *zsb = ITOZSB(ip);
2533 zilog_t *zilog;
2534 dmu_tx_t *tx;
2535 vattr_t oldva;
2536 xvattr_t *tmpxvattr;
2537 uint_t mask = vap->va_mask;
2538 uint_t saved_mask = 0;
2539 int trim_mask = 0;
2540 uint64_t new_mode;
2541 uint64_t new_uid, new_gid;
2542 uint64_t xattr_obj;
2543 uint64_t mtime[2], ctime[2];
2544 znode_t *attrzp;
2545 int need_policy = FALSE;
2546 int err, err2;
2547 zfs_fuid_info_t *fuidp = NULL;
2548 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2549 xoptattr_t *xoap;
2550 zfs_acl_t *aclp;
2551 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2552 boolean_t fuid_dirtied = B_FALSE;
2553 sa_bulk_attr_t *bulk, *xattr_bulk;
2554 int count = 0, xattr_count = 0;
2555
2556 if (mask == 0)
2557 return (0);
2558
2559 ZFS_ENTER(zsb);
2560 ZFS_VERIFY_ZP(zp);
2561
2562 zilog = zsb->z_log;
2563
2564 /*
2565 * Make sure that if we have ephemeral uid/gid or xvattr specified
2566 * that file system is at proper version level
2567 */
2568
2569 if (zsb->z_use_fuids == B_FALSE &&
2570 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2571 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2572 (mask & ATTR_XVATTR))) {
2573 ZFS_EXIT(zsb);
2574 return (SET_ERROR(EINVAL));
2575 }
2576
2577 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2578 ZFS_EXIT(zsb);
2579 return (SET_ERROR(EISDIR));
2580 }
2581
2582 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2583 ZFS_EXIT(zsb);
2584 return (SET_ERROR(EINVAL));
2585 }
2586
2587 /*
2588 * If this is an xvattr_t, then get a pointer to the structure of
2589 * optional attributes. If this is NULL, then we have a vattr_t.
2590 */
2591 xoap = xva_getxoptattr(xvap);
2592
2593 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2594 xva_init(tmpxvattr);
2595
2596 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2597 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2598
2599 /*
2600 * Immutable files can only alter immutable bit and atime
2601 */
2602 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2603 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2604 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2605 err = EPERM;
2606 goto out3;
2607 }
2608
2609 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2610 err = EPERM;
2611 goto out3;
2612 }
2613
2614 /*
2615 * Verify timestamps doesn't overflow 32 bits.
2616 * ZFS can handle large timestamps, but 32bit syscalls can't
2617 * handle times greater than 2039. This check should be removed
2618 * once large timestamps are fully supported.
2619 */
2620 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2621 if (((mask & ATTR_ATIME) &&
2622 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2623 ((mask & ATTR_MTIME) &&
2624 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2625 err = EOVERFLOW;
2626 goto out3;
2627 }
2628 }
2629
2630 top:
2631 attrzp = NULL;
2632 aclp = NULL;
2633
2634 /* Can this be moved to before the top label? */
2635 if (zfs_is_readonly(zsb)) {
2636 err = EROFS;
2637 goto out3;
2638 }
2639
2640 /*
2641 * First validate permissions
2642 */
2643
2644 if (mask & ATTR_SIZE) {
2645 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2646 if (err)
2647 goto out3;
2648
2649 /*
2650 * XXX - Note, we are not providing any open
2651 * mode flags here (like FNDELAY), so we may
2652 * block if there are locks present... this
2653 * should be addressed in openat().
2654 */
2655 /* XXX - would it be OK to generate a log record here? */
2656 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2657 if (err)
2658 goto out3;
2659 }
2660
2661 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2662 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2663 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2664 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2665 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2666 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2667 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2668 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2669 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2670 skipaclchk, cr);
2671 }
2672
2673 if (mask & (ATTR_UID|ATTR_GID)) {
2674 int idmask = (mask & (ATTR_UID|ATTR_GID));
2675 int take_owner;
2676 int take_group;
2677
2678 /*
2679 * NOTE: even if a new mode is being set,
2680 * we may clear S_ISUID/S_ISGID bits.
2681 */
2682
2683 if (!(mask & ATTR_MODE))
2684 vap->va_mode = zp->z_mode;
2685
2686 /*
2687 * Take ownership or chgrp to group we are a member of
2688 */
2689
2690 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2691 take_group = (mask & ATTR_GID) &&
2692 zfs_groupmember(zsb, vap->va_gid, cr);
2693
2694 /*
2695 * If both ATTR_UID and ATTR_GID are set then take_owner and
2696 * take_group must both be set in order to allow taking
2697 * ownership.
2698 *
2699 * Otherwise, send the check through secpolicy_vnode_setattr()
2700 *
2701 */
2702
2703 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2704 take_owner && take_group) ||
2705 ((idmask == ATTR_UID) && take_owner) ||
2706 ((idmask == ATTR_GID) && take_group)) {
2707 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2708 skipaclchk, cr) == 0) {
2709 /*
2710 * Remove setuid/setgid for non-privileged users
2711 */
2712 (void) secpolicy_setid_clear(vap, cr);
2713 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2714 } else {
2715 need_policy = TRUE;
2716 }
2717 } else {
2718 need_policy = TRUE;
2719 }
2720 }
2721
2722 mutex_enter(&zp->z_lock);
2723 oldva.va_mode = zp->z_mode;
2724 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2725 if (mask & ATTR_XVATTR) {
2726 /*
2727 * Update xvattr mask to include only those attributes
2728 * that are actually changing.
2729 *
2730 * the bits will be restored prior to actually setting
2731 * the attributes so the caller thinks they were set.
2732 */
2733 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2734 if (xoap->xoa_appendonly !=
2735 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2736 need_policy = TRUE;
2737 } else {
2738 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2739 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2740 }
2741 }
2742
2743 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2744 if (xoap->xoa_nounlink !=
2745 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2746 need_policy = TRUE;
2747 } else {
2748 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2749 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2750 }
2751 }
2752
2753 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2754 if (xoap->xoa_immutable !=
2755 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2756 need_policy = TRUE;
2757 } else {
2758 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2759 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2760 }
2761 }
2762
2763 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2764 if (xoap->xoa_nodump !=
2765 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2766 need_policy = TRUE;
2767 } else {
2768 XVA_CLR_REQ(xvap, XAT_NODUMP);
2769 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2770 }
2771 }
2772
2773 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2774 if (xoap->xoa_av_modified !=
2775 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2776 need_policy = TRUE;
2777 } else {
2778 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2779 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2780 }
2781 }
2782
2783 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2784 if ((!S_ISREG(ip->i_mode) &&
2785 xoap->xoa_av_quarantined) ||
2786 xoap->xoa_av_quarantined !=
2787 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2788 need_policy = TRUE;
2789 } else {
2790 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2791 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2792 }
2793 }
2794
2795 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2796 mutex_exit(&zp->z_lock);
2797 err = EPERM;
2798 goto out3;
2799 }
2800
2801 if (need_policy == FALSE &&
2802 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2803 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2804 need_policy = TRUE;
2805 }
2806 }
2807
2808 mutex_exit(&zp->z_lock);
2809
2810 if (mask & ATTR_MODE) {
2811 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2812 err = secpolicy_setid_setsticky_clear(ip, vap,
2813 &oldva, cr);
2814 if (err)
2815 goto out3;
2816
2817 trim_mask |= ATTR_MODE;
2818 } else {
2819 need_policy = TRUE;
2820 }
2821 }
2822
2823 if (need_policy) {
2824 /*
2825 * If trim_mask is set then take ownership
2826 * has been granted or write_acl is present and user
2827 * has the ability to modify mode. In that case remove
2828 * UID|GID and or MODE from mask so that
2829 * secpolicy_vnode_setattr() doesn't revoke it.
2830 */
2831
2832 if (trim_mask) {
2833 saved_mask = vap->va_mask;
2834 vap->va_mask &= ~trim_mask;
2835 }
2836 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2837 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2838 if (err)
2839 goto out3;
2840
2841 if (trim_mask)
2842 vap->va_mask |= saved_mask;
2843 }
2844
2845 /*
2846 * secpolicy_vnode_setattr, or take ownership may have
2847 * changed va_mask
2848 */
2849 mask = vap->va_mask;
2850
2851 if ((mask & (ATTR_UID | ATTR_GID))) {
2852 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2853 &xattr_obj, sizeof (xattr_obj));
2854
2855 if (err == 0 && xattr_obj) {
2856 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2857 if (err)
2858 goto out2;
2859 }
2860 if (mask & ATTR_UID) {
2861 new_uid = zfs_fuid_create(zsb,
2862 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2863 if (new_uid != zp->z_uid &&
2864 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2865 if (attrzp)
2866 iput(ZTOI(attrzp));
2867 err = EDQUOT;
2868 goto out2;
2869 }
2870 }
2871
2872 if (mask & ATTR_GID) {
2873 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2874 cr, ZFS_GROUP, &fuidp);
2875 if (new_gid != zp->z_gid &&
2876 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2877 if (attrzp)
2878 iput(ZTOI(attrzp));
2879 err = EDQUOT;
2880 goto out2;
2881 }
2882 }
2883 }
2884 tx = dmu_tx_create(zsb->z_os);
2885
2886 if (mask & ATTR_MODE) {
2887 uint64_t pmode = zp->z_mode;
2888 uint64_t acl_obj;
2889 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2890
2891 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2892
2893 mutex_enter(&zp->z_lock);
2894 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2895 /*
2896 * Are we upgrading ACL from old V0 format
2897 * to V1 format?
2898 */
2899 if (zsb->z_version >= ZPL_VERSION_FUID &&
2900 zfs_znode_acl_version(zp) ==
2901 ZFS_ACL_VERSION_INITIAL) {
2902 dmu_tx_hold_free(tx, acl_obj, 0,
2903 DMU_OBJECT_END);
2904 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2905 0, aclp->z_acl_bytes);
2906 } else {
2907 dmu_tx_hold_write(tx, acl_obj, 0,
2908 aclp->z_acl_bytes);
2909 }
2910 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2911 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2912 0, aclp->z_acl_bytes);
2913 }
2914 mutex_exit(&zp->z_lock);
2915 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2916 } else {
2917 if ((mask & ATTR_XVATTR) &&
2918 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2919 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2920 else
2921 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2922 }
2923
2924 if (attrzp) {
2925 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2926 }
2927
2928 fuid_dirtied = zsb->z_fuid_dirty;
2929 if (fuid_dirtied)
2930 zfs_fuid_txhold(zsb, tx);
2931
2932 zfs_sa_upgrade_txholds(tx, zp);
2933
2934 err = dmu_tx_assign(tx, TXG_WAIT);
2935 if (err)
2936 goto out;
2937
2938 count = 0;
2939 /*
2940 * Set each attribute requested.
2941 * We group settings according to the locks they need to acquire.
2942 *
2943 * Note: you cannot set ctime directly, although it will be
2944 * updated as a side-effect of calling this function.
2945 */
2946
2947
2948 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2949 mutex_enter(&zp->z_acl_lock);
2950 mutex_enter(&zp->z_lock);
2951
2952 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2953 &zp->z_pflags, sizeof (zp->z_pflags));
2954
2955 if (attrzp) {
2956 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2957 mutex_enter(&attrzp->z_acl_lock);
2958 mutex_enter(&attrzp->z_lock);
2959 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2960 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2961 sizeof (attrzp->z_pflags));
2962 }
2963
2964 if (mask & (ATTR_UID|ATTR_GID)) {
2965
2966 if (mask & ATTR_UID) {
2967 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2968 &new_uid, sizeof (new_uid));
2969 zp->z_uid = new_uid;
2970 if (attrzp) {
2971 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2972 SA_ZPL_UID(zsb), NULL, &new_uid,
2973 sizeof (new_uid));
2974 attrzp->z_uid = new_uid;
2975 }
2976 }
2977
2978 if (mask & ATTR_GID) {
2979 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2980 NULL, &new_gid, sizeof (new_gid));
2981 zp->z_gid = new_gid;
2982 if (attrzp) {
2983 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2984 SA_ZPL_GID(zsb), NULL, &new_gid,
2985 sizeof (new_gid));
2986 attrzp->z_gid = new_gid;
2987 }
2988 }
2989 if (!(mask & ATTR_MODE)) {
2990 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2991 NULL, &new_mode, sizeof (new_mode));
2992 new_mode = zp->z_mode;
2993 }
2994 err = zfs_acl_chown_setattr(zp);
2995 ASSERT(err == 0);
2996 if (attrzp) {
2997 err = zfs_acl_chown_setattr(attrzp);
2998 ASSERT(err == 0);
2999 }
3000 }
3001
3002 if (mask & ATTR_MODE) {
3003 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
3004 &new_mode, sizeof (new_mode));
3005 zp->z_mode = new_mode;
3006 ASSERT3P(aclp, !=, NULL);
3007 err = zfs_aclset_common(zp, aclp, cr, tx);
3008 ASSERT0(err);
3009 if (zp->z_acl_cached)
3010 zfs_acl_free(zp->z_acl_cached);
3011 zp->z_acl_cached = aclp;
3012 aclp = NULL;
3013 }
3014
3015
3016 if (mask & ATTR_ATIME) {
3017 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3018 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
3019 &zp->z_atime, sizeof (zp->z_atime));
3020 }
3021
3022 if (mask & ATTR_MTIME) {
3023 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3024 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
3025 mtime, sizeof (mtime));
3026 }
3027
3028 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3029 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
3030 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
3031 NULL, mtime, sizeof (mtime));
3032 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
3033 &ctime, sizeof (ctime));
3034 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3035 B_TRUE);
3036 } else if (mask != 0) {
3037 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
3038 &ctime, sizeof (ctime));
3039 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3040 B_TRUE);
3041 if (attrzp) {
3042 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3043 SA_ZPL_CTIME(zsb), NULL,
3044 &ctime, sizeof (ctime));
3045 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3046 mtime, ctime, B_TRUE);
3047 }
3048 }
3049 /*
3050 * Do this after setting timestamps to prevent timestamp
3051 * update from toggling bit
3052 */
3053
3054 if (xoap && (mask & ATTR_XVATTR)) {
3055
3056 /*
3057 * restore trimmed off masks
3058 * so that return masks can be set for caller.
3059 */
3060
3061 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3062 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3063 }
3064 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3065 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3066 }
3067 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3068 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3069 }
3070 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3071 XVA_SET_REQ(xvap, XAT_NODUMP);
3072 }
3073 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3074 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3075 }
3076 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3077 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3078 }
3079
3080 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3081 ASSERT(S_ISREG(ip->i_mode));
3082
3083 zfs_xvattr_set(zp, xvap, tx);
3084 }
3085
3086 if (fuid_dirtied)
3087 zfs_fuid_sync(zsb, tx);
3088
3089 if (mask != 0)
3090 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3091
3092 mutex_exit(&zp->z_lock);
3093 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3094 mutex_exit(&zp->z_acl_lock);
3095
3096 if (attrzp) {
3097 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3098 mutex_exit(&attrzp->z_acl_lock);
3099 mutex_exit(&attrzp->z_lock);
3100 }
3101 out:
3102 if (err == 0 && attrzp) {
3103 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3104 xattr_count, tx);
3105 ASSERT(err2 == 0);
3106 }
3107
3108 if (attrzp)
3109 iput(ZTOI(attrzp));
3110 if (aclp)
3111 zfs_acl_free(aclp);
3112
3113 if (fuidp) {
3114 zfs_fuid_info_free(fuidp);
3115 fuidp = NULL;
3116 }
3117
3118 if (err) {
3119 dmu_tx_abort(tx);
3120 if (err == ERESTART)
3121 goto top;
3122 } else {
3123 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3124 dmu_tx_commit(tx);
3125 zfs_inode_update(zp);
3126 }
3127
3128 out2:
3129 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3130 zil_commit(zilog, 0);
3131
3132 out3:
3133 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3134 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3135 kmem_free(tmpxvattr, sizeof (xvattr_t));
3136 ZFS_EXIT(zsb);
3137 return (err);
3138 }
3139 EXPORT_SYMBOL(zfs_setattr);
3140
3141 typedef struct zfs_zlock {
3142 krwlock_t *zl_rwlock; /* lock we acquired */
3143 znode_t *zl_znode; /* znode we held */
3144 struct zfs_zlock *zl_next; /* next in list */
3145 } zfs_zlock_t;
3146
3147 /*
3148 * Drop locks and release vnodes that were held by zfs_rename_lock().
3149 */
3150 static void
3151 zfs_rename_unlock(zfs_zlock_t **zlpp)
3152 {
3153 zfs_zlock_t *zl;
3154
3155 while ((zl = *zlpp) != NULL) {
3156 if (zl->zl_znode != NULL)
3157 iput(ZTOI(zl->zl_znode));
3158 rw_exit(zl->zl_rwlock);
3159 *zlpp = zl->zl_next;
3160 kmem_free(zl, sizeof (*zl));
3161 }
3162 }
3163
3164 /*
3165 * Search back through the directory tree, using the ".." entries.
3166 * Lock each directory in the chain to prevent concurrent renames.
3167 * Fail any attempt to move a directory into one of its own descendants.
3168 * XXX - z_parent_lock can overlap with map or grow locks
3169 */
3170 static int
3171 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3172 {
3173 zfs_zlock_t *zl;
3174 znode_t *zp = tdzp;
3175 uint64_t rootid = ZTOZSB(zp)->z_root;
3176 uint64_t oidp = zp->z_id;
3177 krwlock_t *rwlp = &szp->z_parent_lock;
3178 krw_t rw = RW_WRITER;
3179
3180 /*
3181 * First pass write-locks szp and compares to zp->z_id.
3182 * Later passes read-lock zp and compare to zp->z_parent.
3183 */
3184 do {
3185 if (!rw_tryenter(rwlp, rw)) {
3186 /*
3187 * Another thread is renaming in this path.
3188 * Note that if we are a WRITER, we don't have any
3189 * parent_locks held yet.
3190 */
3191 if (rw == RW_READER && zp->z_id > szp->z_id) {
3192 /*
3193 * Drop our locks and restart
3194 */
3195 zfs_rename_unlock(&zl);
3196 *zlpp = NULL;
3197 zp = tdzp;
3198 oidp = zp->z_id;
3199 rwlp = &szp->z_parent_lock;
3200 rw = RW_WRITER;
3201 continue;
3202 } else {
3203 /*
3204 * Wait for other thread to drop its locks
3205 */
3206 rw_enter(rwlp, rw);
3207 }
3208 }
3209
3210 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3211 zl->zl_rwlock = rwlp;
3212 zl->zl_znode = NULL;
3213 zl->zl_next = *zlpp;
3214 *zlpp = zl;
3215
3216 if (oidp == szp->z_id) /* We're a descendant of szp */
3217 return (SET_ERROR(EINVAL));
3218
3219 if (oidp == rootid) /* We've hit the top */
3220 return (0);
3221
3222 if (rw == RW_READER) { /* i.e. not the first pass */
3223 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3224 if (error)
3225 return (error);
3226 zl->zl_znode = zp;
3227 }
3228 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3229 &oidp, sizeof (oidp));
3230 rwlp = &zp->z_parent_lock;
3231 rw = RW_READER;
3232
3233 } while (zp->z_id != sdzp->z_id);
3234
3235 return (0);
3236 }
3237
3238 /*
3239 * Move an entry from the provided source directory to the target
3240 * directory. Change the entry name as indicated.
3241 *
3242 * IN: sdip - Source directory containing the "old entry".
3243 * snm - Old entry name.
3244 * tdip - Target directory to contain the "new entry".
3245 * tnm - New entry name.
3246 * cr - credentials of caller.
3247 * flags - case flags
3248 *
3249 * RETURN: 0 on success, error code on failure.
3250 *
3251 * Timestamps:
3252 * sdip,tdip - ctime|mtime updated
3253 */
3254 /*ARGSUSED*/
3255 int
3256 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3257 cred_t *cr, int flags)
3258 {
3259 znode_t *tdzp, *szp, *tzp;
3260 znode_t *sdzp = ITOZ(sdip);
3261 zfs_sb_t *zsb = ITOZSB(sdip);
3262 zilog_t *zilog;
3263 zfs_dirlock_t *sdl, *tdl;
3264 dmu_tx_t *tx;
3265 zfs_zlock_t *zl;
3266 int cmp, serr, terr;
3267 int error = 0;
3268 int zflg = 0;
3269 boolean_t waited = B_FALSE;
3270
3271 ZFS_ENTER(zsb);
3272 ZFS_VERIFY_ZP(sdzp);
3273 zilog = zsb->z_log;
3274
3275 tdzp = ITOZ(tdip);
3276 ZFS_VERIFY_ZP(tdzp);
3277
3278 /*
3279 * We check i_sb because snapshots and the ctldir must have different
3280 * super blocks.
3281 */
3282 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3283 ZFS_EXIT(zsb);
3284 return (SET_ERROR(EXDEV));
3285 }
3286
3287 if (zsb->z_utf8 && u8_validate(tnm,
3288 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3289 ZFS_EXIT(zsb);
3290 return (SET_ERROR(EILSEQ));
3291 }
3292
3293 if (flags & FIGNORECASE)
3294 zflg |= ZCILOOK;
3295
3296 top:
3297 szp = NULL;
3298 tzp = NULL;
3299 zl = NULL;
3300
3301 /*
3302 * This is to prevent the creation of links into attribute space
3303 * by renaming a linked file into/outof an attribute directory.
3304 * See the comment in zfs_link() for why this is considered bad.
3305 */
3306 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3307 ZFS_EXIT(zsb);
3308 return (SET_ERROR(EINVAL));
3309 }
3310
3311 /*
3312 * Lock source and target directory entries. To prevent deadlock,
3313 * a lock ordering must be defined. We lock the directory with
3314 * the smallest object id first, or if it's a tie, the one with
3315 * the lexically first name.
3316 */
3317 if (sdzp->z_id < tdzp->z_id) {
3318 cmp = -1;
3319 } else if (sdzp->z_id > tdzp->z_id) {
3320 cmp = 1;
3321 } else {
3322 /*
3323 * First compare the two name arguments without
3324 * considering any case folding.
3325 */
3326 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3327
3328 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3329 ASSERT(error == 0 || !zsb->z_utf8);
3330 if (cmp == 0) {
3331 /*
3332 * POSIX: "If the old argument and the new argument
3333 * both refer to links to the same existing file,
3334 * the rename() function shall return successfully
3335 * and perform no other action."
3336 */
3337 ZFS_EXIT(zsb);
3338 return (0);
3339 }
3340 /*
3341 * If the file system is case-folding, then we may
3342 * have some more checking to do. A case-folding file
3343 * system is either supporting mixed case sensitivity
3344 * access or is completely case-insensitive. Note
3345 * that the file system is always case preserving.
3346 *
3347 * In mixed sensitivity mode case sensitive behavior
3348 * is the default. FIGNORECASE must be used to
3349 * explicitly request case insensitive behavior.
3350 *
3351 * If the source and target names provided differ only
3352 * by case (e.g., a request to rename 'tim' to 'Tim'),
3353 * we will treat this as a special case in the
3354 * case-insensitive mode: as long as the source name
3355 * is an exact match, we will allow this to proceed as
3356 * a name-change request.
3357 */
3358 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3359 (zsb->z_case == ZFS_CASE_MIXED &&
3360 flags & FIGNORECASE)) &&
3361 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3362 &error) == 0) {
3363 /*
3364 * case preserving rename request, require exact
3365 * name matches
3366 */
3367 zflg |= ZCIEXACT;
3368 zflg &= ~ZCILOOK;
3369 }
3370 }
3371
3372 /*
3373 * If the source and destination directories are the same, we should
3374 * grab the z_name_lock of that directory only once.
3375 */
3376 if (sdzp == tdzp) {
3377 zflg |= ZHAVELOCK;
3378 rw_enter(&sdzp->z_name_lock, RW_READER);
3379 }
3380
3381 if (cmp < 0) {
3382 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3383 ZEXISTS | zflg, NULL, NULL);
3384 terr = zfs_dirent_lock(&tdl,
3385 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3386 } else {
3387 terr = zfs_dirent_lock(&tdl,
3388 tdzp, tnm, &tzp, zflg, NULL, NULL);
3389 serr = zfs_dirent_lock(&sdl,
3390 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3391 NULL, NULL);
3392 }
3393
3394 if (serr) {
3395 /*
3396 * Source entry invalid or not there.
3397 */
3398 if (!terr) {
3399 zfs_dirent_unlock(tdl);
3400 if (tzp)
3401 iput(ZTOI(tzp));
3402 }
3403
3404 if (sdzp == tdzp)
3405 rw_exit(&sdzp->z_name_lock);
3406
3407 if (strcmp(snm, "..") == 0)
3408 serr = EINVAL;
3409 ZFS_EXIT(zsb);
3410 return (serr);
3411 }
3412 if (terr) {
3413 zfs_dirent_unlock(sdl);
3414 iput(ZTOI(szp));
3415
3416 if (sdzp == tdzp)
3417 rw_exit(&sdzp->z_name_lock);
3418
3419 if (strcmp(tnm, "..") == 0)
3420 terr = EINVAL;
3421 ZFS_EXIT(zsb);
3422 return (terr);
3423 }
3424
3425 /*
3426 * Must have write access at the source to remove the old entry
3427 * and write access at the target to create the new entry.
3428 * Note that if target and source are the same, this can be
3429 * done in a single check.
3430 */
3431
3432 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3433 goto out;
3434
3435 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3436 /*
3437 * Check to make sure rename is valid.
3438 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3439 */
3440 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3441 goto out;
3442 }
3443
3444 /*
3445 * Does target exist?
3446 */
3447 if (tzp) {
3448 /*
3449 * Source and target must be the same type.
3450 */
3451 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3452 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3453 error = SET_ERROR(ENOTDIR);
3454 goto out;
3455 }
3456 } else {
3457 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3458 error = SET_ERROR(EISDIR);
3459 goto out;
3460 }
3461 }
3462 /*
3463 * POSIX dictates that when the source and target
3464 * entries refer to the same file object, rename
3465 * must do nothing and exit without error.
3466 */
3467 if (szp->z_id == tzp->z_id) {
3468 error = 0;
3469 goto out;
3470 }
3471 }
3472
3473 tx = dmu_tx_create(zsb->z_os);
3474 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3475 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3476 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3477 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3478 if (sdzp != tdzp) {
3479 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3480 zfs_sa_upgrade_txholds(tx, tdzp);
3481 }
3482 if (tzp) {
3483 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3484 zfs_sa_upgrade_txholds(tx, tzp);
3485 }
3486
3487 zfs_sa_upgrade_txholds(tx, szp);
3488 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3489 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3490 if (error) {
3491 if (zl != NULL)
3492 zfs_rename_unlock(&zl);
3493 zfs_dirent_unlock(sdl);
3494 zfs_dirent_unlock(tdl);
3495
3496 if (sdzp == tdzp)
3497 rw_exit(&sdzp->z_name_lock);
3498
3499 iput(ZTOI(szp));
3500 if (tzp)
3501 iput(ZTOI(tzp));
3502 if (error == ERESTART) {
3503 waited = B_TRUE;
3504 dmu_tx_wait(tx);
3505 dmu_tx_abort(tx);
3506 goto top;
3507 }
3508 dmu_tx_abort(tx);
3509 ZFS_EXIT(zsb);
3510 return (error);
3511 }
3512
3513 if (tzp) /* Attempt to remove the existing target */
3514 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3515
3516 if (error == 0) {
3517 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3518 if (error == 0) {
3519 szp->z_pflags |= ZFS_AV_MODIFIED;
3520
3521 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3522 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3523 ASSERT0(error);
3524
3525 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3526 if (error == 0) {
3527 zfs_log_rename(zilog, tx, TX_RENAME |
3528 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3529 sdl->dl_name, tdzp, tdl->dl_name, szp);
3530 } else {
3531 /*
3532 * At this point, we have successfully created
3533 * the target name, but have failed to remove
3534 * the source name. Since the create was done
3535 * with the ZRENAMING flag, there are
3536 * complications; for one, the link count is
3537 * wrong. The easiest way to deal with this
3538 * is to remove the newly created target, and
3539 * return the original error. This must
3540 * succeed; fortunately, it is very unlikely to
3541 * fail, since we just created it.
3542 */
3543 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3544 ZRENAMING, NULL), ==, 0);
3545 }
3546 }
3547 }
3548
3549 dmu_tx_commit(tx);
3550 out:
3551 if (zl != NULL)
3552 zfs_rename_unlock(&zl);
3553
3554 zfs_dirent_unlock(sdl);
3555 zfs_dirent_unlock(tdl);
3556
3557 zfs_inode_update(sdzp);
3558 if (sdzp == tdzp)
3559 rw_exit(&sdzp->z_name_lock);
3560
3561 if (sdzp != tdzp)
3562 zfs_inode_update(tdzp);
3563
3564 zfs_inode_update(szp);
3565 iput(ZTOI(szp));
3566 if (tzp) {
3567 zfs_inode_update(tzp);
3568 iput(ZTOI(tzp));
3569 }
3570
3571 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3572 zil_commit(zilog, 0);
3573
3574 ZFS_EXIT(zsb);
3575 return (error);
3576 }
3577 EXPORT_SYMBOL(zfs_rename);
3578
3579 /*
3580 * Insert the indicated symbolic reference entry into the directory.
3581 *
3582 * IN: dip - Directory to contain new symbolic link.
3583 * link - Name for new symlink entry.
3584 * vap - Attributes of new entry.
3585 * target - Target path of new symlink.
3586 *
3587 * cr - credentials of caller.
3588 * flags - case flags
3589 *
3590 * RETURN: 0 on success, error code on failure.
3591 *
3592 * Timestamps:
3593 * dip - ctime|mtime updated
3594 */
3595 /*ARGSUSED*/
3596 int
3597 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3598 struct inode **ipp, cred_t *cr, int flags)
3599 {
3600 znode_t *zp, *dzp = ITOZ(dip);
3601 zfs_dirlock_t *dl;
3602 dmu_tx_t *tx;
3603 zfs_sb_t *zsb = ITOZSB(dip);
3604 zilog_t *zilog;
3605 uint64_t len = strlen(link);
3606 int error;
3607 int zflg = ZNEW;
3608 zfs_acl_ids_t acl_ids;
3609 boolean_t fuid_dirtied;
3610 uint64_t txtype = TX_SYMLINK;
3611 boolean_t waited = B_FALSE;
3612
3613 ASSERT(S_ISLNK(vap->va_mode));
3614
3615 ZFS_ENTER(zsb);
3616 ZFS_VERIFY_ZP(dzp);
3617 zilog = zsb->z_log;
3618
3619 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3620 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3621 ZFS_EXIT(zsb);
3622 return (SET_ERROR(EILSEQ));
3623 }
3624 if (flags & FIGNORECASE)
3625 zflg |= ZCILOOK;
3626
3627 if (len > MAXPATHLEN) {
3628 ZFS_EXIT(zsb);
3629 return (SET_ERROR(ENAMETOOLONG));
3630 }
3631
3632 if ((error = zfs_acl_ids_create(dzp, 0,
3633 vap, cr, NULL, &acl_ids)) != 0) {
3634 ZFS_EXIT(zsb);
3635 return (error);
3636 }
3637 top:
3638 *ipp = NULL;
3639
3640 /*
3641 * Attempt to lock directory; fail if entry already exists.
3642 */
3643 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3644 if (error) {
3645 zfs_acl_ids_free(&acl_ids);
3646 ZFS_EXIT(zsb);
3647 return (error);
3648 }
3649
3650 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3651 zfs_acl_ids_free(&acl_ids);
3652 zfs_dirent_unlock(dl);
3653 ZFS_EXIT(zsb);
3654 return (error);
3655 }
3656
3657 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3658 zfs_acl_ids_free(&acl_ids);
3659 zfs_dirent_unlock(dl);
3660 ZFS_EXIT(zsb);
3661 return (SET_ERROR(EDQUOT));
3662 }
3663 tx = dmu_tx_create(zsb->z_os);
3664 fuid_dirtied = zsb->z_fuid_dirty;
3665 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3666 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3667 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3668 ZFS_SA_BASE_ATTR_SIZE + len);
3669 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3670 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3671 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3672 acl_ids.z_aclp->z_acl_bytes);
3673 }
3674 if (fuid_dirtied)
3675 zfs_fuid_txhold(zsb, tx);
3676 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3677 if (error) {
3678 zfs_dirent_unlock(dl);
3679 if (error == ERESTART) {
3680 waited = B_TRUE;
3681 dmu_tx_wait(tx);
3682 dmu_tx_abort(tx);
3683 goto top;
3684 }
3685 zfs_acl_ids_free(&acl_ids);
3686 dmu_tx_abort(tx);
3687 ZFS_EXIT(zsb);
3688 return (error);
3689 }
3690
3691 /*
3692 * Create a new object for the symlink.
3693 * for version 4 ZPL datsets the symlink will be an SA attribute
3694 */
3695 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3696
3697 if (fuid_dirtied)
3698 zfs_fuid_sync(zsb, tx);
3699
3700 mutex_enter(&zp->z_lock);
3701 if (zp->z_is_sa)
3702 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3703 link, len, tx);
3704 else
3705 zfs_sa_symlink(zp, link, len, tx);
3706 mutex_exit(&zp->z_lock);
3707
3708 zp->z_size = len;
3709 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3710 &zp->z_size, sizeof (zp->z_size), tx);
3711 /*
3712 * Insert the new object into the directory.
3713 */
3714 (void) zfs_link_create(dl, zp, tx, ZNEW);
3715
3716 if (flags & FIGNORECASE)
3717 txtype |= TX_CI;
3718 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3719
3720 zfs_inode_update(dzp);
3721 zfs_inode_update(zp);
3722
3723 zfs_acl_ids_free(&acl_ids);
3724
3725 dmu_tx_commit(tx);
3726
3727 zfs_dirent_unlock(dl);
3728
3729 *ipp = ZTOI(zp);
3730
3731 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3732 zil_commit(zilog, 0);
3733
3734 ZFS_EXIT(zsb);
3735 return (error);
3736 }
3737 EXPORT_SYMBOL(zfs_symlink);
3738
3739 /*
3740 * Return, in the buffer contained in the provided uio structure,
3741 * the symbolic path referred to by ip.
3742 *
3743 * IN: ip - inode of symbolic link
3744 * uio - structure to contain the link path.
3745 * cr - credentials of caller.
3746 *
3747 * RETURN: 0 if success
3748 * error code if failure
3749 *
3750 * Timestamps:
3751 * ip - atime updated
3752 */
3753 /* ARGSUSED */
3754 int
3755 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3756 {
3757 znode_t *zp = ITOZ(ip);
3758 zfs_sb_t *zsb = ITOZSB(ip);
3759 int error;
3760
3761 ZFS_ENTER(zsb);
3762 ZFS_VERIFY_ZP(zp);
3763
3764 mutex_enter(&zp->z_lock);
3765 if (zp->z_is_sa)
3766 error = sa_lookup_uio(zp->z_sa_hdl,
3767 SA_ZPL_SYMLINK(zsb), uio);
3768 else
3769 error = zfs_sa_readlink(zp, uio);
3770 mutex_exit(&zp->z_lock);
3771
3772 ZFS_ACCESSTIME_STAMP(zsb, zp);
3773 ZFS_EXIT(zsb);
3774 return (error);
3775 }
3776 EXPORT_SYMBOL(zfs_readlink);
3777
3778 /*
3779 * Insert a new entry into directory tdip referencing sip.
3780 *
3781 * IN: tdip - Directory to contain new entry.
3782 * sip - inode of new entry.
3783 * name - name of new entry.
3784 * cr - credentials of caller.
3785 *
3786 * RETURN: 0 if success
3787 * error code if failure
3788 *
3789 * Timestamps:
3790 * tdip - ctime|mtime updated
3791 * sip - ctime updated
3792 */
3793 /* ARGSUSED */
3794 int
3795 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3796 {
3797 znode_t *dzp = ITOZ(tdip);
3798 znode_t *tzp, *szp;
3799 zfs_sb_t *zsb = ITOZSB(tdip);
3800 zilog_t *zilog;
3801 zfs_dirlock_t *dl;
3802 dmu_tx_t *tx;
3803 int error;
3804 int zf = ZNEW;
3805 uint64_t parent;
3806 uid_t owner;
3807 boolean_t waited = B_FALSE;
3808
3809 ASSERT(S_ISDIR(tdip->i_mode));
3810
3811 ZFS_ENTER(zsb);
3812 ZFS_VERIFY_ZP(dzp);
3813 zilog = zsb->z_log;
3814
3815 /*
3816 * POSIX dictates that we return EPERM here.
3817 * Better choices include ENOTSUP or EISDIR.
3818 */
3819 if (S_ISDIR(sip->i_mode)) {
3820 ZFS_EXIT(zsb);
3821 return (SET_ERROR(EPERM));
3822 }
3823
3824 szp = ITOZ(sip);
3825 ZFS_VERIFY_ZP(szp);
3826
3827 /*
3828 * We check i_sb because snapshots and the ctldir must have different
3829 * super blocks.
3830 */
3831 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3832 ZFS_EXIT(zsb);
3833 return (SET_ERROR(EXDEV));
3834 }
3835
3836 /* Prevent links to .zfs/shares files */
3837
3838 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3839 &parent, sizeof (uint64_t))) != 0) {
3840 ZFS_EXIT(zsb);
3841 return (error);
3842 }
3843 if (parent == zsb->z_shares_dir) {
3844 ZFS_EXIT(zsb);
3845 return (SET_ERROR(EPERM));
3846 }
3847
3848 if (zsb->z_utf8 && u8_validate(name,
3849 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3850 ZFS_EXIT(zsb);
3851 return (SET_ERROR(EILSEQ));
3852 }
3853 #ifdef HAVE_PN_UTILS
3854 if (flags & FIGNORECASE)
3855 zf |= ZCILOOK;
3856 #endif /* HAVE_PN_UTILS */
3857
3858 /*
3859 * We do not support links between attributes and non-attributes
3860 * because of the potential security risk of creating links
3861 * into "normal" file space in order to circumvent restrictions
3862 * imposed in attribute space.
3863 */
3864 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3865 ZFS_EXIT(zsb);
3866 return (SET_ERROR(EINVAL));
3867 }
3868
3869 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3870 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3871 ZFS_EXIT(zsb);
3872 return (SET_ERROR(EPERM));
3873 }
3874
3875 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3876 ZFS_EXIT(zsb);
3877 return (error);
3878 }
3879
3880 top:
3881 /*
3882 * Attempt to lock directory; fail if entry already exists.
3883 */
3884 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3885 if (error) {
3886 ZFS_EXIT(zsb);
3887 return (error);
3888 }
3889
3890 tx = dmu_tx_create(zsb->z_os);
3891 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3892 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3893 zfs_sa_upgrade_txholds(tx, szp);
3894 zfs_sa_upgrade_txholds(tx, dzp);
3895 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3896 if (error) {
3897 zfs_dirent_unlock(dl);
3898 if (error == ERESTART) {
3899 waited = B_TRUE;
3900 dmu_tx_wait(tx);
3901 dmu_tx_abort(tx);
3902 goto top;
3903 }
3904 dmu_tx_abort(tx);
3905 ZFS_EXIT(zsb);
3906 return (error);
3907 }
3908
3909 error = zfs_link_create(dl, szp, tx, 0);
3910
3911 if (error == 0) {
3912 uint64_t txtype = TX_LINK;
3913 #ifdef HAVE_PN_UTILS
3914 if (flags & FIGNORECASE)
3915 txtype |= TX_CI;
3916 #endif /* HAVE_PN_UTILS */
3917 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3918 }
3919
3920 dmu_tx_commit(tx);
3921
3922 zfs_dirent_unlock(dl);
3923
3924 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3925 zil_commit(zilog, 0);
3926
3927 zfs_inode_update(dzp);
3928 zfs_inode_update(szp);
3929 ZFS_EXIT(zsb);
3930 return (error);
3931 }
3932 EXPORT_SYMBOL(zfs_link);
3933
3934 static void
3935 zfs_putpage_commit_cb(void *arg)
3936 {
3937 struct page *pp = arg;
3938
3939 ClearPageError(pp);
3940 end_page_writeback(pp);
3941 }
3942
3943 /*
3944 * Push a page out to disk, once the page is on stable storage the
3945 * registered commit callback will be run as notification of completion.
3946 *
3947 * IN: ip - page mapped for inode.
3948 * pp - page to push (page is locked)
3949 * wbc - writeback control data
3950 *
3951 * RETURN: 0 if success
3952 * error code if failure
3953 *
3954 * Timestamps:
3955 * ip - ctime|mtime updated
3956 */
3957 /* ARGSUSED */
3958 int
3959 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3960 {
3961 znode_t *zp = ITOZ(ip);
3962 zfs_sb_t *zsb = ITOZSB(ip);
3963 loff_t offset;
3964 loff_t pgoff;
3965 unsigned int pglen;
3966 rl_t *rl;
3967 dmu_tx_t *tx;
3968 caddr_t va;
3969 int err = 0;
3970 uint64_t mtime[2], ctime[2];
3971 sa_bulk_attr_t bulk[3];
3972 int cnt = 0;
3973 struct address_space *mapping;
3974
3975 ZFS_ENTER(zsb);
3976 ZFS_VERIFY_ZP(zp);
3977
3978 ASSERT(PageLocked(pp));
3979
3980 pgoff = page_offset(pp); /* Page byte-offset in file */
3981 offset = i_size_read(ip); /* File length in bytes */
3982 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3983 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3984
3985 /* Page is beyond end of file */
3986 if (pgoff >= offset) {
3987 unlock_page(pp);
3988 ZFS_EXIT(zsb);
3989 return (0);
3990 }
3991
3992 /* Truncate page length to end of file */
3993 if (pgoff + pglen > offset)
3994 pglen = offset - pgoff;
3995
3996 #if 0
3997 /*
3998 * FIXME: Allow mmap writes past its quota. The correct fix
3999 * is to register a page_mkwrite() handler to count the page
4000 * against its quota when it is about to be dirtied.
4001 */
4002 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
4003 zfs_owner_overquota(zsb, zp, B_TRUE)) {
4004 err = EDQUOT;
4005 }
4006 #endif
4007
4008 /*
4009 * The ordering here is critical and must adhere to the following
4010 * rules in order to avoid deadlocking in either zfs_read() or
4011 * zfs_free_range() due to a lock inversion.
4012 *
4013 * 1) The page must be unlocked prior to acquiring the range lock.
4014 * This is critical because zfs_read() calls find_lock_page()
4015 * which may block on the page lock while holding the range lock.
4016 *
4017 * 2) Before setting or clearing write back on a page the range lock
4018 * must be held in order to prevent a lock inversion with the
4019 * zfs_free_range() function.
4020 *
4021 * This presents a problem because upon entering this function the
4022 * page lock is already held. To safely acquire the range lock the
4023 * page lock must be dropped. This creates a window where another
4024 * process could truncate, invalidate, dirty, or write out the page.
4025 *
4026 * Therefore, after successfully reacquiring the range and page locks
4027 * the current page state is checked. In the common case everything
4028 * will be as is expected and it can be written out. However, if
4029 * the page state has changed it must be handled accordingly.
4030 */
4031 mapping = pp->mapping;
4032 redirty_page_for_writepage(wbc, pp);
4033 unlock_page(pp);
4034
4035 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
4036 lock_page(pp);
4037
4038 /* Page mapping changed or it was no longer dirty, we're done */
4039 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4040 unlock_page(pp);
4041 zfs_range_unlock(rl);
4042 ZFS_EXIT(zsb);
4043 return (0);
4044 }
4045
4046 /* Another process started write block if required */
4047 if (PageWriteback(pp)) {
4048 unlock_page(pp);
4049 zfs_range_unlock(rl);
4050
4051 if (wbc->sync_mode != WB_SYNC_NONE)
4052 wait_on_page_writeback(pp);
4053
4054 ZFS_EXIT(zsb);
4055 return (0);
4056 }
4057
4058 /* Clear the dirty flag the required locks are held */
4059 if (!clear_page_dirty_for_io(pp)) {
4060 unlock_page(pp);
4061 zfs_range_unlock(rl);
4062 ZFS_EXIT(zsb);
4063 return (0);
4064 }
4065
4066 /*
4067 * Counterpart for redirty_page_for_writepage() above. This page
4068 * was in fact not skipped and should not be counted as if it were.
4069 */
4070 wbc->pages_skipped--;
4071 set_page_writeback(pp);
4072 unlock_page(pp);
4073
4074 tx = dmu_tx_create(zsb->z_os);
4075 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4076 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4077 zfs_sa_upgrade_txholds(tx, zp);
4078
4079 err = dmu_tx_assign(tx, TXG_NOWAIT);
4080 if (err != 0) {
4081 if (err == ERESTART)
4082 dmu_tx_wait(tx);
4083
4084 dmu_tx_abort(tx);
4085 __set_page_dirty_nobuffers(pp);
4086 ClearPageError(pp);
4087 end_page_writeback(pp);
4088 zfs_range_unlock(rl);
4089 ZFS_EXIT(zsb);
4090 return (err);
4091 }
4092
4093 va = kmap(pp);
4094 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
4095 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
4096 kunmap(pp);
4097
4098 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4099 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4100 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
4101
4102 /* Preserve the mtime and ctime provided by the inode */
4103 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4104 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4105 zp->z_atime_dirty = 0;
4106 zp->z_seq++;
4107
4108 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4109
4110 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4111 zfs_putpage_commit_cb, pp);
4112 dmu_tx_commit(tx);
4113
4114 zfs_range_unlock(rl);
4115
4116 if (wbc->sync_mode != WB_SYNC_NONE) {
4117 /*
4118 * Note that this is rarely called under writepages(), because
4119 * writepages() normally handles the entire commit for
4120 * performance reasons.
4121 */
4122 if (zsb->z_log != NULL)
4123 zil_commit(zsb->z_log, zp->z_id);
4124 }
4125
4126 ZFS_EXIT(zsb);
4127 return (err);
4128 }
4129
4130 /*
4131 * Update the system attributes when the inode has been dirtied. For the
4132 * moment we only update the mode, atime, mtime, and ctime.
4133 */
4134 int
4135 zfs_dirty_inode(struct inode *ip, int flags)
4136 {
4137 znode_t *zp = ITOZ(ip);
4138 zfs_sb_t *zsb = ITOZSB(ip);
4139 dmu_tx_t *tx;
4140 uint64_t mode, atime[2], mtime[2], ctime[2];
4141 sa_bulk_attr_t bulk[4];
4142 int error;
4143 int cnt = 0;
4144
4145 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
4146 return (0);
4147
4148 ZFS_ENTER(zsb);
4149 ZFS_VERIFY_ZP(zp);
4150
4151 tx = dmu_tx_create(zsb->z_os);
4152
4153 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4154 zfs_sa_upgrade_txholds(tx, zp);
4155
4156 error = dmu_tx_assign(tx, TXG_WAIT);
4157 if (error) {
4158 dmu_tx_abort(tx);
4159 goto out;
4160 }
4161
4162 mutex_enter(&zp->z_lock);
4163 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zsb), NULL, &mode, 8);
4164 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
4165 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4166 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4167
4168 /* Preserve the mode, mtime and ctime provided by the inode */
4169 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4170 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4171 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4172 mode = ip->i_mode;
4173
4174 zp->z_mode = mode;
4175 zp->z_atime_dirty = 0;
4176
4177 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4178 mutex_exit(&zp->z_lock);
4179
4180 dmu_tx_commit(tx);
4181 out:
4182 ZFS_EXIT(zsb);
4183 return (error);
4184 }
4185 EXPORT_SYMBOL(zfs_dirty_inode);
4186
4187 /*ARGSUSED*/
4188 void
4189 zfs_inactive(struct inode *ip)
4190 {
4191 znode_t *zp = ITOZ(ip);
4192 zfs_sb_t *zsb = ITOZSB(ip);
4193 int error;
4194 int need_unlock = 0;
4195
4196 /* Only read lock if we haven't already write locked, e.g. rollback */
4197 if (!RW_WRITE_HELD(&zsb->z_teardown_inactive_lock)) {
4198 need_unlock = 1;
4199 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
4200 }
4201 if (zp->z_sa_hdl == NULL) {
4202 if (need_unlock)
4203 rw_exit(&zsb->z_teardown_inactive_lock);
4204 return;
4205 }
4206
4207 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4208 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
4209
4210 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4211 zfs_sa_upgrade_txholds(tx, zp);
4212 error = dmu_tx_assign(tx, TXG_WAIT);
4213 if (error) {
4214 dmu_tx_abort(tx);
4215 } else {
4216 mutex_enter(&zp->z_lock);
4217 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
4218 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4219 zp->z_atime_dirty = 0;
4220 mutex_exit(&zp->z_lock);
4221 dmu_tx_commit(tx);
4222 }
4223 }
4224
4225 zfs_zinactive(zp);
4226 if (need_unlock)
4227 rw_exit(&zsb->z_teardown_inactive_lock);
4228 }
4229 EXPORT_SYMBOL(zfs_inactive);
4230
4231 /*
4232 * Bounds-check the seek operation.
4233 *
4234 * IN: ip - inode seeking within
4235 * ooff - old file offset
4236 * noffp - pointer to new file offset
4237 * ct - caller context
4238 *
4239 * RETURN: 0 if success
4240 * EINVAL if new offset invalid
4241 */
4242 /* ARGSUSED */
4243 int
4244 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4245 {
4246 if (S_ISDIR(ip->i_mode))
4247 return (0);
4248 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4249 }
4250 EXPORT_SYMBOL(zfs_seek);
4251
4252 /*
4253 * Fill pages with data from the disk.
4254 */
4255 static int
4256 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4257 {
4258 znode_t *zp = ITOZ(ip);
4259 zfs_sb_t *zsb = ITOZSB(ip);
4260 objset_t *os;
4261 struct page *cur_pp;
4262 u_offset_t io_off, total;
4263 size_t io_len;
4264 loff_t i_size;
4265 unsigned page_idx;
4266 int err;
4267
4268 os = zsb->z_os;
4269 io_len = nr_pages << PAGE_CACHE_SHIFT;
4270 i_size = i_size_read(ip);
4271 io_off = page_offset(pl[0]);
4272
4273 if (io_off + io_len > i_size)
4274 io_len = i_size - io_off;
4275
4276 /*
4277 * Iterate over list of pages and read each page individually.
4278 */
4279 page_idx = 0;
4280 cur_pp = pl[0];
4281 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4282 caddr_t va;
4283
4284 va = kmap(cur_pp);
4285 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4286 DMU_READ_PREFETCH);
4287 kunmap(cur_pp);
4288 if (err) {
4289 /* convert checksum errors into IO errors */
4290 if (err == ECKSUM)
4291 err = SET_ERROR(EIO);
4292 return (err);
4293 }
4294 cur_pp = pl[++page_idx];
4295 }
4296
4297 return (0);
4298 }
4299
4300 /*
4301 * Uses zfs_fillpage to read data from the file and fill the pages.
4302 *
4303 * IN: ip - inode of file to get data from.
4304 * pl - list of pages to read
4305 * nr_pages - number of pages to read
4306 *
4307 * RETURN: 0 on success, error code on failure.
4308 *
4309 * Timestamps:
4310 * vp - atime updated
4311 */
4312 /* ARGSUSED */
4313 int
4314 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4315 {
4316 znode_t *zp = ITOZ(ip);
4317 zfs_sb_t *zsb = ITOZSB(ip);
4318 int err;
4319
4320 if (pl == NULL)
4321 return (0);
4322
4323 ZFS_ENTER(zsb);
4324 ZFS_VERIFY_ZP(zp);
4325
4326 err = zfs_fillpage(ip, pl, nr_pages);
4327
4328 if (!err)
4329 ZFS_ACCESSTIME_STAMP(zsb, zp);
4330
4331 ZFS_EXIT(zsb);
4332 return (err);
4333 }
4334 EXPORT_SYMBOL(zfs_getpage);
4335
4336 /*
4337 * Check ZFS specific permissions to memory map a section of a file.
4338 *
4339 * IN: ip - inode of the file to mmap
4340 * off - file offset
4341 * addrp - start address in memory region
4342 * len - length of memory region
4343 * vm_flags- address flags
4344 *
4345 * RETURN: 0 if success
4346 * error code if failure
4347 */
4348 /*ARGSUSED*/
4349 int
4350 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4351 unsigned long vm_flags)
4352 {
4353 znode_t *zp = ITOZ(ip);
4354 zfs_sb_t *zsb = ITOZSB(ip);
4355
4356 ZFS_ENTER(zsb);
4357 ZFS_VERIFY_ZP(zp);
4358
4359 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4360 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4361 ZFS_EXIT(zsb);
4362 return (SET_ERROR(EPERM));
4363 }
4364
4365 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4366 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4367 ZFS_EXIT(zsb);
4368 return (SET_ERROR(EACCES));
4369 }
4370
4371 if (off < 0 || len > MAXOFFSET_T - off) {
4372 ZFS_EXIT(zsb);
4373 return (SET_ERROR(ENXIO));
4374 }
4375
4376 ZFS_EXIT(zsb);
4377 return (0);
4378 }
4379 EXPORT_SYMBOL(zfs_map);
4380
4381 /*
4382 * convoff - converts the given data (start, whence) to the
4383 * given whence.
4384 */
4385 int
4386 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4387 {
4388 vattr_t vap;
4389 int error;
4390
4391 if ((lckdat->l_whence == 2) || (whence == 2)) {
4392 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4393 return (error);
4394 }
4395
4396 switch (lckdat->l_whence) {
4397 case 1:
4398 lckdat->l_start += offset;
4399 break;
4400 case 2:
4401 lckdat->l_start += vap.va_size;
4402 /* FALLTHRU */
4403 case 0:
4404 break;
4405 default:
4406 return (SET_ERROR(EINVAL));
4407 }
4408
4409 if (lckdat->l_start < 0)
4410 return (SET_ERROR(EINVAL));
4411
4412 switch (whence) {
4413 case 1:
4414 lckdat->l_start -= offset;
4415 break;
4416 case 2:
4417 lckdat->l_start -= vap.va_size;
4418 /* FALLTHRU */
4419 case 0:
4420 break;
4421 default:
4422 return (SET_ERROR(EINVAL));
4423 }
4424
4425 lckdat->l_whence = (short)whence;
4426 return (0);
4427 }
4428
4429 /*
4430 * Free or allocate space in a file. Currently, this function only
4431 * supports the `F_FREESP' command. However, this command is somewhat
4432 * misnamed, as its functionality includes the ability to allocate as
4433 * well as free space.
4434 *
4435 * IN: ip - inode of file to free data in.
4436 * cmd - action to take (only F_FREESP supported).
4437 * bfp - section of file to free/alloc.
4438 * flag - current file open mode flags.
4439 * offset - current file offset.
4440 * cr - credentials of caller [UNUSED].
4441 *
4442 * RETURN: 0 on success, error code on failure.
4443 *
4444 * Timestamps:
4445 * ip - ctime|mtime updated
4446 */
4447 /* ARGSUSED */
4448 int
4449 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4450 offset_t offset, cred_t *cr)
4451 {
4452 znode_t *zp = ITOZ(ip);
4453 zfs_sb_t *zsb = ITOZSB(ip);
4454 uint64_t off, len;
4455 int error;
4456
4457 ZFS_ENTER(zsb);
4458 ZFS_VERIFY_ZP(zp);
4459
4460 if (cmd != F_FREESP) {
4461 ZFS_EXIT(zsb);
4462 return (SET_ERROR(EINVAL));
4463 }
4464
4465 /*
4466 * Callers might not be able to detect properly that we are read-only,
4467 * so check it explicitly here.
4468 */
4469 if (zfs_is_readonly(zsb)) {
4470 ZFS_EXIT(zsb);
4471 return (SET_ERROR(EROFS));
4472 }
4473
4474 if ((error = convoff(ip, bfp, 0, offset))) {
4475 ZFS_EXIT(zsb);
4476 return (error);
4477 }
4478
4479 if (bfp->l_len < 0) {
4480 ZFS_EXIT(zsb);
4481 return (SET_ERROR(EINVAL));
4482 }
4483
4484 /*
4485 * Permissions aren't checked on Solaris because on this OS
4486 * zfs_space() can only be called with an opened file handle.
4487 * On Linux we can get here through truncate_range() which
4488 * operates directly on inodes, so we need to check access rights.
4489 */
4490 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4491 ZFS_EXIT(zsb);
4492 return (error);
4493 }
4494
4495 off = bfp->l_start;
4496 len = bfp->l_len; /* 0 means from off to end of file */
4497
4498 error = zfs_freesp(zp, off, len, flag, TRUE);
4499
4500 ZFS_EXIT(zsb);
4501 return (error);
4502 }
4503 EXPORT_SYMBOL(zfs_space);
4504
4505 /*ARGSUSED*/
4506 int
4507 zfs_fid(struct inode *ip, fid_t *fidp)
4508 {
4509 znode_t *zp = ITOZ(ip);
4510 zfs_sb_t *zsb = ITOZSB(ip);
4511 uint32_t gen;
4512 uint64_t gen64;
4513 uint64_t object = zp->z_id;
4514 zfid_short_t *zfid;
4515 int size, i, error;
4516
4517 ZFS_ENTER(zsb);
4518 ZFS_VERIFY_ZP(zp);
4519
4520 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4521 &gen64, sizeof (uint64_t))) != 0) {
4522 ZFS_EXIT(zsb);
4523 return (error);
4524 }
4525
4526 gen = (uint32_t)gen64;
4527
4528 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4529 if (fidp->fid_len < size) {
4530 fidp->fid_len = size;
4531 ZFS_EXIT(zsb);
4532 return (SET_ERROR(ENOSPC));
4533 }
4534
4535 zfid = (zfid_short_t *)fidp;
4536
4537 zfid->zf_len = size;
4538
4539 for (i = 0; i < sizeof (zfid->zf_object); i++)
4540 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4541
4542 /* Must have a non-zero generation number to distinguish from .zfs */
4543 if (gen == 0)
4544 gen = 1;
4545 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4546 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4547
4548 if (size == LONG_FID_LEN) {
4549 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4550 zfid_long_t *zlfid;
4551
4552 zlfid = (zfid_long_t *)fidp;
4553
4554 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4555 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4556
4557 /* XXX - this should be the generation number for the objset */
4558 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4559 zlfid->zf_setgen[i] = 0;
4560 }
4561
4562 ZFS_EXIT(zsb);
4563 return (0);
4564 }
4565 EXPORT_SYMBOL(zfs_fid);
4566
4567 /*ARGSUSED*/
4568 int
4569 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4570 {
4571 znode_t *zp = ITOZ(ip);
4572 zfs_sb_t *zsb = ITOZSB(ip);
4573 int error;
4574 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4575
4576 ZFS_ENTER(zsb);
4577 ZFS_VERIFY_ZP(zp);
4578 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4579 ZFS_EXIT(zsb);
4580
4581 return (error);
4582 }
4583 EXPORT_SYMBOL(zfs_getsecattr);
4584
4585 /*ARGSUSED*/
4586 int
4587 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4588 {
4589 znode_t *zp = ITOZ(ip);
4590 zfs_sb_t *zsb = ITOZSB(ip);
4591 int error;
4592 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4593 zilog_t *zilog = zsb->z_log;
4594
4595 ZFS_ENTER(zsb);
4596 ZFS_VERIFY_ZP(zp);
4597
4598 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4599
4600 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4601 zil_commit(zilog, 0);
4602
4603 ZFS_EXIT(zsb);
4604 return (error);
4605 }
4606 EXPORT_SYMBOL(zfs_setsecattr);
4607
4608 #ifdef HAVE_UIO_ZEROCOPY
4609 /*
4610 * Tunable, both must be a power of 2.
4611 *
4612 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4613 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4614 * an arcbuf for a partial block read
4615 */
4616 int zcr_blksz_min = (1 << 10); /* 1K */
4617 int zcr_blksz_max = (1 << 17); /* 128K */
4618
4619 /*ARGSUSED*/
4620 static int
4621 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4622 {
4623 znode_t *zp = ITOZ(ip);
4624 zfs_sb_t *zsb = ITOZSB(ip);
4625 int max_blksz = zsb->z_max_blksz;
4626 uio_t *uio = &xuio->xu_uio;
4627 ssize_t size = uio->uio_resid;
4628 offset_t offset = uio->uio_loffset;
4629 int blksz;
4630 int fullblk, i;
4631 arc_buf_t *abuf;
4632 ssize_t maxsize;
4633 int preamble, postamble;
4634
4635 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4636 return (SET_ERROR(EINVAL));
4637
4638 ZFS_ENTER(zsb);
4639 ZFS_VERIFY_ZP(zp);
4640 switch (ioflag) {
4641 case UIO_WRITE:
4642 /*
4643 * Loan out an arc_buf for write if write size is bigger than
4644 * max_blksz, and the file's block size is also max_blksz.
4645 */
4646 blksz = max_blksz;
4647 if (size < blksz || zp->z_blksz != blksz) {
4648 ZFS_EXIT(zsb);
4649 return (SET_ERROR(EINVAL));
4650 }
4651 /*
4652 * Caller requests buffers for write before knowing where the
4653 * write offset might be (e.g. NFS TCP write).
4654 */
4655 if (offset == -1) {
4656 preamble = 0;
4657 } else {
4658 preamble = P2PHASE(offset, blksz);
4659 if (preamble) {
4660 preamble = blksz - preamble;
4661 size -= preamble;
4662 }
4663 }
4664
4665 postamble = P2PHASE(size, blksz);
4666 size -= postamble;
4667
4668 fullblk = size / blksz;
4669 (void) dmu_xuio_init(xuio,
4670 (preamble != 0) + fullblk + (postamble != 0));
4671
4672 /*
4673 * Have to fix iov base/len for partial buffers. They
4674 * currently represent full arc_buf's.
4675 */
4676 if (preamble) {
4677 /* data begins in the middle of the arc_buf */
4678 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4679 blksz);
4680 ASSERT(abuf);
4681 (void) dmu_xuio_add(xuio, abuf,
4682 blksz - preamble, preamble);
4683 }
4684
4685 for (i = 0; i < fullblk; i++) {
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, blksz);
4690 }
4691
4692 if (postamble) {
4693 /* data ends in the middle of the arc_buf */
4694 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4695 blksz);
4696 ASSERT(abuf);
4697 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4698 }
4699 break;
4700 case UIO_READ:
4701 /*
4702 * Loan out an arc_buf for read if the read size is larger than
4703 * the current file block size. Block alignment is not
4704 * considered. Partial arc_buf will be loaned out for read.
4705 */
4706 blksz = zp->z_blksz;
4707 if (blksz < zcr_blksz_min)
4708 blksz = zcr_blksz_min;
4709 if (blksz > zcr_blksz_max)
4710 blksz = zcr_blksz_max;
4711 /* avoid potential complexity of dealing with it */
4712 if (blksz > max_blksz) {
4713 ZFS_EXIT(zsb);
4714 return (SET_ERROR(EINVAL));
4715 }
4716
4717 maxsize = zp->z_size - uio->uio_loffset;
4718 if (size > maxsize)
4719 size = maxsize;
4720
4721 if (size < blksz) {
4722 ZFS_EXIT(zsb);
4723 return (SET_ERROR(EINVAL));
4724 }
4725 break;
4726 default:
4727 ZFS_EXIT(zsb);
4728 return (SET_ERROR(EINVAL));
4729 }
4730
4731 uio->uio_extflg = UIO_XUIO;
4732 XUIO_XUZC_RW(xuio) = ioflag;
4733 ZFS_EXIT(zsb);
4734 return (0);
4735 }
4736
4737 /*ARGSUSED*/
4738 static int
4739 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4740 {
4741 int i;
4742 arc_buf_t *abuf;
4743 int ioflag = XUIO_XUZC_RW(xuio);
4744
4745 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4746
4747 i = dmu_xuio_cnt(xuio);
4748 while (i-- > 0) {
4749 abuf = dmu_xuio_arcbuf(xuio, i);
4750 /*
4751 * if abuf == NULL, it must be a write buffer
4752 * that has been returned in zfs_write().
4753 */
4754 if (abuf)
4755 dmu_return_arcbuf(abuf);
4756 ASSERT(abuf || ioflag == UIO_WRITE);
4757 }
4758
4759 dmu_xuio_fini(xuio);
4760 return (0);
4761 }
4762 #endif /* HAVE_UIO_ZEROCOPY */
4763
4764 #if defined(_KERNEL) && defined(HAVE_SPL)
4765 module_param(zfs_delete_blocks, ulong, 0644);
4766 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
4767 module_param(zfs_read_chunk_size, long, 0644);
4768 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");
4769 #endif