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