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