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