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