]> git.proxmox.com Git - mirror_zfs.git/blame - module/os/linux/zfs/zfs_vnops_os.c
Fix corruption caused by mmap flushing problems
[mirror_zfs.git] / module / os / linux / zfs / zfs_vnops_os.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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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 */
9b7b9cd3 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
a448a255 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
5475aada 25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
9b7b9cd3 26 * Copyright 2017 Nexenta Systems, Inc.
34dc7c2f
BB
27 */
28
29/* Portions Copyright 2007 Jeremy Teo */
428870ff 30/* Portions Copyright 2010 Robert Milkowski */
34dc7c2f 31
60101509 32
34dc7c2f
BB
33#include <sys/types.h>
34#include <sys/param.h>
35#include <sys/time.h>
34dc7c2f 36#include <sys/sysmacros.h>
34dc7c2f 37#include <sys/vfs.h>
34dc7c2f
BB
38#include <sys/file.h>
39#include <sys/stat.h>
40#include <sys/kmem.h>
41#include <sys/taskq.h>
42#include <sys/uio.h>
43#include <sys/vmsystm.h>
44#include <sys/atomic.h>
34dc7c2f
BB
45#include <sys/pathname.h>
46#include <sys/cmn_err.h>
47#include <sys/errno.h>
34dc7c2f
BB
48#include <sys/zfs_dir.h>
49#include <sys/zfs_acl.h>
50#include <sys/zfs_ioctl.h>
51#include <sys/fs/zfs.h>
52#include <sys/dmu.h>
428870ff 53#include <sys/dmu_objset.h>
34dc7c2f
BB
54#include <sys/spa.h>
55#include <sys/txg.h>
56#include <sys/dbuf.h>
57#include <sys/zap.h>
428870ff 58#include <sys/sa.h>
34dc7c2f
BB
59#include <sys/policy.h>
60#include <sys/sunddi.h>
b128c09f 61#include <sys/sid.h>
ebe7e575 62#include <sys/zfs_ctldir.h>
34dc7c2f 63#include <sys/zfs_fuid.h>
957c7aa2 64#include <sys/zfs_quota.h>
428870ff 65#include <sys/zfs_sa.h>
e5c39b95 66#include <sys/zfs_vnops.h>
34dc7c2f 67#include <sys/zfs_rlock.h>
428870ff 68#include <sys/cred.h>
218b8eaf 69#include <sys/zpl.h>
1ce23dca 70#include <sys/zil.h>
9c5167d1 71#include <sys/sa_impl.h>
34dc7c2f
BB
72
73/*
74 * Programming rules.
75 *
76 * Each vnode op performs some logical unit of work. To do this, the ZPL must
77 * properly lock its in-core state, create a DMU transaction, do the work,
78 * record this work in the intent log (ZIL), commit the DMU transaction,
79 * and wait for the intent log to commit if it is a synchronous operation.
80 * Moreover, the vnode ops must work in both normal and log replay context.
81 * The ordering of events is important to avoid deadlocks and references
82 * to freed memory. The example below illustrates the following Big Rules:
83 *
84 * (1) A check must be made in each zfs thread for a mounted file system.
768eaced
CC
85 * This is done avoiding races using zfs_enter(zfsvfs).
86 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
87 * must be checked with zfs_verify_zp(zp). Both of these macros
34dc7c2f
BB
88 * can return EIO from the calling function.
89 *
2921ad6c 90 * (2) zrele() should always be the last thing except for zil_commit() (if
768eaced 91 * necessary) and zfs_exit(). This is for 3 reasons: First, if it's the
2921ad6c
PD
92 * last reference, the vnode/znode can be freed, so the zp may point to
93 * freed memory. Second, the last reference will call zfs_zinactive(),
94 * which may induce a lot of work -- pushing cached pages (which acquires
95 * range locks) and syncing out cached atime changes. Third,
96 * zfs_zinactive() may require a new tx, which could deadlock the system
97 * if you were already holding one. This deadlock occurs because the tx
98 * currently being operated on prevents a txg from syncing, which
99 * prevents the new tx from progressing, resulting in a deadlock. If you
100 * must call zrele() within a tx, use zfs_zrele_async(). Note that iput()
101 * is a synonym for zrele().
34dc7c2f
BB
102 *
103 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
104 * as they can span dmu_tx_assign() calls.
105 *
384f8a09
MA
106 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
107 * dmu_tx_assign(). This is critical because we don't want to block
108 * while holding locks.
109 *
768eaced 110 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
384f8a09
MA
111 * reduces lock contention and CPU usage when we must wait (note that if
112 * throughput is constrained by the storage, nearly every transaction
113 * must wait).
114 *
115 * Note, in particular, that if a lock is sometimes acquired before
116 * the tx assigns, and sometimes after (e.g. z_lock), then failing
117 * to use a non-blocking assign can deadlock the system. The scenario:
34dc7c2f
BB
118 *
119 * Thread A has grabbed a lock before calling dmu_tx_assign().
120 * Thread B is in an already-assigned tx, and blocks for this lock.
121 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
122 * forever, because the previous txg can't quiesce until B's tx commits.
123 *
0037b49e 124 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
e8b96c60 125 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
0735ecb3 126 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
e8b96c60
MA
127 * to indicate that this operation has already called dmu_tx_wait().
128 * This will ensure that we don't retry forever, waiting a short bit
129 * each time.
34dc7c2f
BB
130 *
131 * (5) If the operation succeeded, generate the intent log entry for it
132 * before dropping locks. This ensures that the ordering of events
133 * in the intent log matches the order in which they actually occurred.
d3cc8b15 134 * During ZIL replay the zfs_log_* functions will update the sequence
fb5f0bc8 135 * number to indicate the zil transaction has replayed.
34dc7c2f
BB
136 *
137 * (6) At the end of each vnode op, the DMU tx must always commit,
138 * regardless of whether there were any errors.
139 *
572e2857 140 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
34dc7c2f
BB
141 * to ensure that synchronous semantics are provided when necessary.
142 *
143 * In general, this is how things should be ordered in each vnode op:
144 *
768eaced 145 * zfs_enter(zfsvfs); // exit if unmounted
34dc7c2f 146 * top:
3558fd73 147 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
34dc7c2f
BB
148 * rw_enter(...); // grab any other locks you need
149 * tx = dmu_tx_create(...); // get DMU tx
150 * dmu_tx_hold_*(); // hold each object you might modify
0735ecb3 151 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
152 * if (error) {
153 * rw_exit(...); // drop locks
154 * zfs_dirent_unlock(dl); // unlock directory entry
657ce253 155 * zrele(...); // release held znodes
fb5f0bc8 156 * if (error == ERESTART) {
e8b96c60 157 * waited = B_TRUE;
34dc7c2f
BB
158 * dmu_tx_wait(tx);
159 * dmu_tx_abort(tx);
160 * goto top;
161 * }
162 * dmu_tx_abort(tx); // abort DMU tx
768eaced 163 * zfs_exit(zfsvfs); // finished in zfs
34dc7c2f
BB
164 * return (error); // really out of space
165 * }
166 * error = do_real_work(); // do whatever this VOP does
167 * if (error == 0)
168 * zfs_log_*(...); // on success, make ZIL entry
169 * dmu_tx_commit(tx); // commit DMU tx -- error or not
170 * rw_exit(...); // drop locks
171 * zfs_dirent_unlock(dl); // unlock directory entry
657ce253 172 * zrele(...); // release held znodes
572e2857 173 * zil_commit(zilog, foid); // synchronous when necessary
768eaced 174 * zfs_exit(zfsvfs); // finished in zfs
34dc7c2f
BB
175 * return (error); // done, report error
176 */
126400a1
BB
177int
178zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
179{
ef70eff1 180 (void) cr;
126400a1 181 znode_t *zp = ITOZ(ip);
0037b49e 182 zfsvfs_t *zfsvfs = ITOZSB(ip);
768eaced 183 int error;
126400a1 184
768eaced
CC
185 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
186 return (error);
126400a1
BB
187
188 /* Honor ZFS_APPENDONLY file attribute */
43e8f6e3 189 if (blk_mode_is_open_write(mode) && (zp->z_pflags & ZFS_APPENDONLY) &&
126400a1 190 ((flag & O_APPEND) == 0)) {
768eaced 191 zfs_exit(zfsvfs, FTAG);
2e528b49 192 return (SET_ERROR(EPERM));
126400a1
BB
193 }
194
c3773de1
AM
195 /*
196 * Keep a count of the synchronous opens in the znode. On first
197 * synchronous open we must convert all previous async transactions
198 * into sync to keep correct ordering.
199 */
200 if (flag & O_SYNC) {
201 if (atomic_inc_32_nv(&zp->z_sync_cnt) == 1)
202 zil_async_to_sync(zfsvfs->z_log, zp->z_id);
203 }
126400a1 204
768eaced 205 zfs_exit(zfsvfs, FTAG);
126400a1
BB
206 return (0);
207}
126400a1 208
126400a1
BB
209int
210zfs_close(struct inode *ip, int flag, cred_t *cr)
211{
ef70eff1 212 (void) cr;
126400a1 213 znode_t *zp = ITOZ(ip);
0037b49e 214 zfsvfs_t *zfsvfs = ITOZSB(ip);
768eaced 215 int error;
126400a1 216
768eaced
CC
217 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
218 return (error);
126400a1 219
7dc71949 220 /* Decrement the synchronous opens in the znode */
126400a1 221 if (flag & O_SYNC)
7dc71949 222 atomic_dec_32(&zp->z_sync_cnt);
126400a1 223
768eaced 224 zfs_exit(zfsvfs, FTAG);
8780c539 225 return (0);
126400a1 226}
126400a1 227
c0d35759 228#if defined(_KERNEL)
89cd2197
BB
229
230static int zfs_fillpage(struct inode *ip, struct page *pp);
231
34dc7c2f
BB
232/*
233 * When a file is memory mapped, we must keep the IO data synchronized
89cd2197
BB
234 * between the DMU cache and the memory mapped pages. Update all mapped
235 * pages with the contents of the coresponding dmu buffer.
34dc7c2f 236 */
e53d678d 237void
8a9634e2 238update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
34dc7c2f 239{
89cd2197
BB
240 struct address_space *mp = ZTOI(zp)->i_mapping;
241 int64_t off = start & (PAGE_SIZE - 1);
34dc7c2f 242
8b1899d3 243 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
89cd2197 244 uint64_t nbytes = MIN(PAGE_SIZE - off, len);
34dc7c2f 245
89cd2197 246 struct page *pp = find_lock_page(mp, start >> PAGE_SHIFT);
c0d35759
BB
247 if (pp) {
248 if (mapping_writably_mapped(mp))
249 flush_dcache_page(pp);
34dc7c2f 250
89cd2197
BB
251 void *pb = kmap(pp);
252 int error = dmu_read(os, zp->z_id, start + off,
253 nbytes, pb + off, DMU_READ_PREFETCH);
c0d35759
BB
254 kunmap(pp);
255
89cd2197
BB
256 if (error) {
257 SetPageError(pp);
258 ClearPageUptodate(pp);
259 } else {
260 ClearPageError(pp);
261 SetPageUptodate(pp);
262
263 if (mapping_writably_mapped(mp))
264 flush_dcache_page(pp);
265
266 mark_page_accessed(pp);
267 }
c0d35759 268
c0d35759 269 unlock_page(pp);
8b1899d3 270 put_page(pp);
34dc7c2f 271 }
c0d35759 272
d164b209 273 len -= nbytes;
34dc7c2f 274 off = 0;
34dc7c2f 275 }
34dc7c2f
BB
276}
277
278/*
89cd2197
BB
279 * When a file is memory mapped, we must keep the I/O data synchronized
280 * between the DMU cache and the memory mapped pages. Preferentially read
281 * from memory mapped pages, otherwise fallback to reading through the dmu.
34dc7c2f 282 */
e53d678d 283int
d0cd9a5c 284mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
34dc7c2f 285{
e53d678d 286 struct inode *ip = ZTOI(zp);
c0d35759 287 struct address_space *mp = ip->i_mapping;
89cd2197
BB
288 int64_t start = uio->uio_loffset;
289 int64_t off = start & (PAGE_SIZE - 1);
34dc7c2f
BB
290 int len = nbytes;
291 int error = 0;
292
8b1899d3 293 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
89cd2197 294 uint64_t bytes = MIN(PAGE_SIZE - off, len);
c0d35759 295
89cd2197 296 struct page *pp = find_lock_page(mp, start >> PAGE_SHIFT);
c0d35759 297 if (pp) {
89cd2197
BB
298 /*
299 * If filemap_fault() retries there exists a window
300 * where the page will be unlocked and not up to date.
301 * In this case we must try and fill the page.
302 */
303 if (unlikely(!PageUptodate(pp))) {
304 error = zfs_fillpage(ip, pp);
305 if (error) {
306 unlock_page(pp);
307 put_page(pp);
308 return (error);
309 }
310 }
311
312 ASSERT(PageUptodate(pp) || PageDirty(pp));
313
b2ab468d 314 unlock_page(pp);
c0d35759 315
89cd2197 316 void *pb = kmap(pp);
d0cd9a5c 317 error = zfs_uiomove(pb + off, bytes, UIO_READ, uio);
c0d35759
BB
318 kunmap(pp);
319
320 if (mapping_writably_mapped(mp))
321 flush_dcache_page(pp);
322
323 mark_page_accessed(pp);
8b1899d3 324 put_page(pp);
34dc7c2f 325 } else {
804e0504
MA
326 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
327 uio, bytes);
34dc7c2f 328 }
c0d35759 329
34dc7c2f
BB
330 len -= bytes;
331 off = 0;
89cd2197 332
34dc7c2f
BB
333 if (error)
334 break;
335 }
89cd2197 336
34dc7c2f
BB
337 return (error);
338}
c0d35759 339#endif /* _KERNEL */
34dc7c2f 340
18168da7 341static unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT;
34dc7c2f 342
13a9a6f5
MM
343/*
344 * Write the bytes to a file.
345 *
346 * IN: zp - znode of file to be written to
347 * data - bytes to write
348 * len - number of bytes to write
349 * pos - offset to start writing at
350 *
351 * OUT: resid - remaining bytes to write
352 *
353 * RETURN: 0 if success
1c2358c1
BB
354 * positive error code if failure. EIO is returned
355 * for a short write when residp isn't provided.
13a9a6f5
MM
356 *
357 * Timestamps:
358 * zp - ctime|mtime updated if byte count > 0
359 */
360int
361zfs_write_simple(znode_t *zp, const void *data, size_t len,
1c2358c1 362 loff_t pos, size_t *residp)
13a9a6f5 363{
1c2358c1
BB
364 fstrans_cookie_t cookie;
365 int error;
366
367 struct iovec iov;
368 iov.iov_base = (void *)data;
369 iov.iov_len = len;
370
d0cd9a5c
BA
371 zfs_uio_t uio;
372 zfs_uio_iovec_init(&uio, &iov, 1, pos, UIO_SYSSPACE, len, 0);
13a9a6f5 373
1c2358c1
BB
374 cookie = spl_fstrans_mark();
375 error = zfs_write(zp, &uio, 0, kcred);
376 spl_fstrans_unmark(cookie);
377
378 if (error == 0) {
379 if (residp != NULL)
d0cd9a5c
BA
380 *residp = zfs_uio_resid(&uio);
381 else if (zfs_uio_resid(&uio) != 0)
1c2358c1 382 error = SET_ERROR(EIO);
13a9a6f5 383 }
1c2358c1 384
13a9a6f5
MM
385 return (error);
386}
387
23c13c7e
AL
388static void
389zfs_rele_async_task(void *arg)
390{
391 iput(arg);
392}
393
0a50679c 394void
657ce253 395zfs_zrele_async(znode_t *zp)
3558fd73 396{
657ce253 397 struct inode *ip = ZTOI(zp);
0a50679c
BB
398 objset_t *os = ITOZSB(ip)->z_os;
399
3558fd73 400 ASSERT(atomic_read(&ip->i_count) > 0);
0a50679c
BB
401 ASSERT(os != NULL);
402
2921ad6c
PD
403 /*
404 * If decrementing the count would put us at 0, we can't do it inline
405 * here, because that would be synchronous. Instead, dispatch an iput
406 * to run later.
407 *
408 * For more information on the dangers of a synchronous iput, see the
409 * header comment of this file.
410 */
411 if (!atomic_add_unless(&ip->i_count, -1, 1)) {
657ce253 412 VERIFY(taskq_dispatch(dsl_pool_zrele_taskq(dmu_objset_pool(os)),
23c13c7e 413 zfs_rele_async_task, ip, TQ_SLEEP) != TASKQID_INVALID);
2921ad6c 414 }
3558fd73
BB
415}
416
34dc7c2f 417
34dc7c2f
BB
418/*
419 * Lookup an entry in a directory, or an extended attribute directory.
3558fd73 420 * If it exists, return a held inode reference for it.
34dc7c2f 421 *
657ce253 422 * IN: zdp - znode of directory to search.
34dc7c2f 423 * nm - name of entry to lookup.
34dc7c2f 424 * flags - LOOKUP_XATTR set if looking for an attribute.
34dc7c2f 425 * cr - credentials of caller.
34dc7c2f
BB
426 * direntflags - directory lookup flags
427 * realpnp - returned pathname.
428 *
657ce253 429 * OUT: zpp - znode of located entry, NULL if not found.
34dc7c2f 430 *
d3cc8b15 431 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
432 *
433 * Timestamps:
434 * NA
435 */
e5c39b95 436int
4d55ea81
RM
437zfs_lookup(znode_t *zdp, char *nm, znode_t **zpp, int flags, cred_t *cr,
438 int *direntflags, pathname_t *realpnp)
34dc7c2f 439{
657ce253 440 zfsvfs_t *zfsvfs = ZTOZSB(zdp);
3558fd73 441 int error = 0;
45d1cae3 442
9b7b9cd3
GM
443 /*
444 * Fast path lookup, however we must skip DNLC lookup
445 * for case folding or normalizing lookups because the
446 * DNLC code only stores the passed in name. This means
447 * creating 'a' and removing 'A' on a case insensitive
448 * file system would work, but DNLC still thinks 'a'
449 * exists and won't let you create it again on the next
450 * pass through fast path.
451 */
45d1cae3
BB
452 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
453
657ce253 454 if (!S_ISDIR(ZTOI(zdp)->i_mode)) {
2e528b49 455 return (SET_ERROR(ENOTDIR));
428870ff 456 } else if (zdp->z_sa_hdl == NULL) {
2e528b49 457 return (SET_ERROR(EIO));
45d1cae3
BB
458 }
459
460 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
461 error = zfs_fastaccesschk_execute(zdp, cr);
462 if (!error) {
657ce253
MM
463 *zpp = zdp;
464 zhold(*zpp);
45d1cae3
BB
465 return (0);
466 }
467 return (error);
45d1cae3
BB
468 }
469 }
470
768eaced
CC
471 if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0)
472 return (error);
34dc7c2f 473
657ce253 474 *zpp = NULL;
34dc7c2f
BB
475
476 if (flags & LOOKUP_XATTR) {
34dc7c2f
BB
477 /*
478 * We don't allow recursive attributes..
479 * Maybe someday we will.
480 */
428870ff 481 if (zdp->z_pflags & ZFS_XATTR) {
768eaced 482 zfs_exit(zfsvfs, FTAG);
2e528b49 483 return (SET_ERROR(EINVAL));
34dc7c2f
BB
484 }
485
657ce253 486 if ((error = zfs_get_xattrdir(zdp, zpp, cr, flags))) {
768eaced 487 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
488 return (error);
489 }
490
491 /*
492 * Do we have permission to get into attribute directory?
493 */
494
657ce253 495 if ((error = zfs_zaccess(*zpp, ACE_EXECUTE, 0,
d4dc53da 496 B_TRUE, cr, zfs_init_idmap))) {
657ce253
MM
497 zrele(*zpp);
498 *zpp = NULL;
34dc7c2f
BB
499 }
500
768eaced 501 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
502 return (error);
503 }
504
657ce253 505 if (!S_ISDIR(ZTOI(zdp)->i_mode)) {
768eaced 506 zfs_exit(zfsvfs, FTAG);
2e528b49 507 return (SET_ERROR(ENOTDIR));
34dc7c2f
BB
508 }
509
510 /*
511 * Check accessibility of directory.
512 */
513
f224eddf 514 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr,
d4dc53da 515 zfs_init_idmap))) {
768eaced 516 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
517 return (error);
518 }
519
0037b49e 520 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
34dc7c2f 521 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 522 zfs_exit(zfsvfs, FTAG);
2e528b49 523 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
524 }
525
657ce253
MM
526 error = zfs_dirlook(zdp, nm, zpp, flags, direntflags, realpnp);
527 if ((error == 0) && (*zpp))
fc273894 528 zfs_znode_update_vfs(*zpp);
34dc7c2f 529
768eaced 530 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
531 return (error);
532}
533
534/*
535 * Attempt to create a new entry in a directory. If the entry
536 * already exists, truncate the file if permissible, else return
3558fd73 537 * an error. Return the ip of the created or trunc'd file.
34dc7c2f 538 *
657ce253 539 * IN: dzp - znode of directory to put new file entry in.
34dc7c2f
BB
540 * name - name of new file entry.
541 * vap - attributes of new file.
542 * excl - flag indicating exclusive or non-exclusive mode.
543 * mode - mode to open file with.
544 * cr - credentials of caller.
a35c1207 545 * flag - file flag.
3558fd73 546 * vsecp - ACL to be set
2a068a13 547 * mnt_ns - user namespace of the mount
34dc7c2f 548 *
657ce253 549 * OUT: zpp - znode of created or trunc'd entry.
34dc7c2f 550 *
d3cc8b15 551 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
552 *
553 * Timestamps:
657ce253
MM
554 * dzp - ctime|mtime updated if new entry created
555 * zp - ctime|mtime always, atime if new
34dc7c2f 556 */
e5c39b95 557int
657ce253 558zfs_create(znode_t *dzp, char *name, vattr_t *vap, int excl,
2a068a13 559 int mode, znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp,
d4dc53da 560 zidmap_t *mnt_ns)
34dc7c2f 561{
657ce253
MM
562 znode_t *zp;
563 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f
BB
564 zilog_t *zilog;
565 objset_t *os;
566 zfs_dirlock_t *dl;
567 dmu_tx_t *tx;
568 int error;
b128c09f 569 uid_t uid;
149e873a 570 gid_t gid;
428870ff 571 zfs_acl_ids_t acl_ids;
9babb374 572 boolean_t fuid_dirtied;
428870ff 573 boolean_t have_acl = B_FALSE;
e8b96c60 574 boolean_t waited = B_FALSE;
e3785718 575 boolean_t skip_acl = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
34dc7c2f
BB
576
577 /*
578 * If we have an ephemeral id, ACL, or XVATTR then
579 * make sure file system is at proper version
580 */
581
149e873a 582 gid = crgetgid(cr);
3558fd73 583 uid = crgetuid(cr);
b128c09f 584
0037b49e 585 if (zfsvfs->z_use_fuids == B_FALSE &&
3558fd73 586 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2e528b49 587 return (SET_ERROR(EINVAL));
34dc7c2f 588
32dec7bd 589 if (name == NULL)
590 return (SET_ERROR(EINVAL));
591
768eaced
CC
592 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
593 return (error);
0037b49e
BB
594 os = zfsvfs->z_os;
595 zilog = zfsvfs->z_log;
34dc7c2f 596
0037b49e 597 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
34dc7c2f 598 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 599 zfs_exit(zfsvfs, FTAG);
2e528b49 600 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
601 }
602
5484965a 603 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f 604 if ((error = secpolicy_xvattr((xvattr_t *)vap,
3558fd73 605 crgetuid(cr), cr, vap->va_mode)) != 0) {
768eaced 606 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
607 return (error);
608 }
609 }
34dc7c2f 610
3558fd73 611top:
657ce253 612 *zpp = NULL;
34dc7c2f
BB
613 if (*name == '\0') {
614 /*
615 * Null component name refers to the directory itself.
616 */
657ce253 617 zhold(dzp);
34dc7c2f
BB
618 zp = dzp;
619 dl = NULL;
620 error = 0;
621 } else {
3558fd73 622 /* possible igrab(zp) */
34dc7c2f
BB
623 int zflg = 0;
624
625 if (flag & FIGNORECASE)
626 zflg |= ZCILOOK;
627
628 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
629 NULL, NULL);
630 if (error) {
572e2857
BB
631 if (have_acl)
632 zfs_acl_ids_free(&acl_ids);
34dc7c2f 633 if (strcmp(name, "..") == 0)
2e528b49 634 error = SET_ERROR(EISDIR);
768eaced 635 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
636 return (error);
637 }
638 }
428870ff 639
34dc7c2f
BB
640 if (zp == NULL) {
641 uint64_t txtype;
9c5167d1 642 uint64_t projid = ZFS_DEFAULT_PROJID;
34dc7c2f
BB
643
644 /*
645 * Create a new file object and update the directory
646 * to reference it.
647 */
e3785718 648 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, skip_acl, cr,
2a068a13 649 mnt_ns))) {
572e2857
BB
650 if (have_acl)
651 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
652 goto out;
653 }
654
655 /*
656 * We only support the creation of regular files in
657 * extended attribute directories.
658 */
428870ff 659
3558fd73 660 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
572e2857
BB
661 if (have_acl)
662 zfs_acl_ids_free(&acl_ids);
2e528b49 663 error = SET_ERROR(EINVAL);
34dc7c2f
BB
664 goto out;
665 }
666
428870ff 667 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
2a068a13 668 cr, vsecp, &acl_ids, mnt_ns)) != 0)
9babb374 669 goto out;
428870ff
BB
670 have_acl = B_TRUE;
671
9c5167d1
NF
672 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
673 projid = zfs_inherit_projid(dzp);
674 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
45d1cae3 675 zfs_acl_ids_free(&acl_ids);
2e528b49 676 error = SET_ERROR(EDQUOT);
9babb374
BB
677 goto out;
678 }
679
34dc7c2f 680 tx = dmu_tx_create(os);
428870ff
BB
681
682 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
683 ZFS_SA_BASE_ATTR_SIZE);
684
0037b49e 685 fuid_dirtied = zfsvfs->z_fuid_dirty;
9babb374 686 if (fuid_dirtied)
0037b49e 687 zfs_fuid_txhold(zfsvfs, tx);
34dc7c2f 688 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
428870ff 689 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
0037b49e 690 if (!zfsvfs->z_use_sa &&
428870ff 691 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
34dc7c2f 692 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
428870ff 693 0, acl_ids.z_aclp->z_acl_bytes);
34dc7c2f 694 }
599b8648 695
0735ecb3
PS
696 error = dmu_tx_assign(tx,
697 (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
698 if (error) {
699 zfs_dirent_unlock(dl);
fb5f0bc8 700 if (error == ERESTART) {
e8b96c60 701 waited = B_TRUE;
34dc7c2f
BB
702 dmu_tx_wait(tx);
703 dmu_tx_abort(tx);
704 goto top;
705 }
428870ff 706 zfs_acl_ids_free(&acl_ids);
34dc7c2f 707 dmu_tx_abort(tx);
768eaced 708 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
709 return (error);
710 }
428870ff 711 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
9babb374 712
599b8648
CC
713 error = zfs_link_create(dl, zp, tx, ZNEW);
714 if (error != 0) {
715 /*
716 * Since, we failed to add the directory entry for it,
717 * delete the newly created dnode.
718 */
719 zfs_znode_delete(zp, tx);
720 remove_inode_hash(ZTOI(zp));
721 zfs_acl_ids_free(&acl_ids);
722 dmu_tx_commit(tx);
723 goto out;
724 }
725
9babb374 726 if (fuid_dirtied)
0037b49e 727 zfs_fuid_sync(zfsvfs, tx);
9babb374 728
34dc7c2f
BB
729 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
730 if (flag & FIGNORECASE)
731 txtype |= TX_CI;
732 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
9babb374
BB
733 vsecp, acl_ids.z_fuidp, vap);
734 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
735 dmu_tx_commit(tx);
736 } else {
da92d5cb 737 int aflags = (flag & O_APPEND) ? V_APPEND : 0;
34dc7c2f 738
572e2857
BB
739 if (have_acl)
740 zfs_acl_ids_free(&acl_ids);
572e2857 741
34dc7c2f
BB
742 /*
743 * A directory entry already exists for this name.
744 */
745 /*
746 * Can't truncate an existing file if in exclusive mode.
747 */
3558fd73 748 if (excl) {
2e528b49 749 error = SET_ERROR(EEXIST);
34dc7c2f
BB
750 goto out;
751 }
752 /*
753 * Can't open a directory for writing.
754 */
3558fd73 755 if (S_ISDIR(ZTOI(zp)->i_mode)) {
2e528b49 756 error = SET_ERROR(EISDIR);
34dc7c2f
BB
757 goto out;
758 }
759 /*
760 * Verify requested access to file.
761 */
2a068a13
YY
762 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr,
763 mnt_ns))) {
34dc7c2f
BB
764 goto out;
765 }
766
767 mutex_enter(&dzp->z_lock);
768 dzp->z_seq++;
769 mutex_exit(&dzp->z_lock);
770
771 /*
772 * Truncate regular files if requested.
773 */
3558fd73
BB
774 if (S_ISREG(ZTOI(zp)->i_mode) &&
775 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
b128c09f 776 /* we can't hold any locks when calling zfs_freesp() */
609603a5
B
777 if (dl) {
778 zfs_dirent_unlock(dl);
779 dl = NULL;
780 }
34dc7c2f 781 error = zfs_freesp(zp, 0, 0, mode, TRUE);
34dc7c2f
BB
782 }
783 }
784out:
785
786 if (dl)
787 zfs_dirent_unlock(dl);
788
789 if (error) {
790 if (zp)
657ce253 791 zrele(zp);
34dc7c2f 792 } else {
fc273894
KHN
793 zfs_znode_update_vfs(dzp);
794 zfs_znode_update_vfs(zp);
657ce253 795 *zpp = zp;
34dc7c2f 796 }
34dc7c2f 797
0037b49e 798 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 799 zil_commit(zilog, 0);
428870ff 800
768eaced 801 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
802 return (error);
803}
804
ace1eae8
CC
805int
806zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl,
2a068a13 807 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp,
d4dc53da 808 zidmap_t *mnt_ns)
ace1eae8 809{
ef70eff1 810 (void) excl, (void) mode, (void) flag;
ace1eae8 811 znode_t *zp = NULL, *dzp = ITOZ(dip);
0037b49e 812 zfsvfs_t *zfsvfs = ITOZSB(dip);
ace1eae8
CC
813 objset_t *os;
814 dmu_tx_t *tx;
815 int error;
816 uid_t uid;
817 gid_t gid;
818 zfs_acl_ids_t acl_ids;
9c5167d1 819 uint64_t projid = ZFS_DEFAULT_PROJID;
ace1eae8
CC
820 boolean_t fuid_dirtied;
821 boolean_t have_acl = B_FALSE;
822 boolean_t waited = B_FALSE;
823
824 /*
825 * If we have an ephemeral id, ACL, or XVATTR then
826 * make sure file system is at proper version
827 */
828
829 gid = crgetgid(cr);
830 uid = crgetuid(cr);
831
0037b49e 832 if (zfsvfs->z_use_fuids == B_FALSE &&
ace1eae8
CC
833 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
834 return (SET_ERROR(EINVAL));
835
768eaced
CC
836 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
837 return (error);
0037b49e 838 os = zfsvfs->z_os;
ace1eae8
CC
839
840 if (vap->va_mask & ATTR_XVATTR) {
841 if ((error = secpolicy_xvattr((xvattr_t *)vap,
842 crgetuid(cr), cr, vap->va_mode)) != 0) {
768eaced 843 zfs_exit(zfsvfs, FTAG);
ace1eae8
CC
844 return (error);
845 }
846 }
847
848top:
849 *ipp = NULL;
850
851 /*
852 * Create a new file object and update the directory
853 * to reference it.
854 */
2a068a13 855 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
ace1eae8
CC
856 if (have_acl)
857 zfs_acl_ids_free(&acl_ids);
858 goto out;
859 }
860
861 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
2a068a13 862 cr, vsecp, &acl_ids, mnt_ns)) != 0)
ace1eae8
CC
863 goto out;
864 have_acl = B_TRUE;
865
9c5167d1
NF
866 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
867 projid = zfs_inherit_projid(dzp);
868 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
ace1eae8
CC
869 zfs_acl_ids_free(&acl_ids);
870 error = SET_ERROR(EDQUOT);
871 goto out;
872 }
873
874 tx = dmu_tx_create(os);
875
876 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
877 ZFS_SA_BASE_ATTR_SIZE);
0037b49e 878 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
ace1eae8 879
0037b49e 880 fuid_dirtied = zfsvfs->z_fuid_dirty;
ace1eae8 881 if (fuid_dirtied)
0037b49e
BB
882 zfs_fuid_txhold(zfsvfs, tx);
883 if (!zfsvfs->z_use_sa &&
ace1eae8
CC
884 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
885 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
886 0, acl_ids.z_aclp->z_acl_bytes);
887 }
0735ecb3 888 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
ace1eae8
CC
889 if (error) {
890 if (error == ERESTART) {
891 waited = B_TRUE;
892 dmu_tx_wait(tx);
893 dmu_tx_abort(tx);
894 goto top;
895 }
896 zfs_acl_ids_free(&acl_ids);
897 dmu_tx_abort(tx);
768eaced 898 zfs_exit(zfsvfs, FTAG);
ace1eae8
CC
899 return (error);
900 }
901 zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids);
902
903 if (fuid_dirtied)
0037b49e 904 zfs_fuid_sync(zfsvfs, tx);
ace1eae8
CC
905
906 /* Add to unlinked set */
a43570c5 907 zp->z_unlinked = B_TRUE;
ace1eae8
CC
908 zfs_unlinked_add(zp, tx);
909 zfs_acl_ids_free(&acl_ids);
910 dmu_tx_commit(tx);
911out:
912
913 if (error) {
914 if (zp)
657ce253 915 zrele(zp);
ace1eae8 916 } else {
fc273894
KHN
917 zfs_znode_update_vfs(dzp);
918 zfs_znode_update_vfs(zp);
ace1eae8
CC
919 *ipp = ZTOI(zp);
920 }
921
768eaced 922 zfs_exit(zfsvfs, FTAG);
ace1eae8
CC
923 return (error);
924}
925
34dc7c2f
BB
926/*
927 * Remove an entry from a directory.
928 *
657ce253 929 * IN: dzp - znode of directory to remove entry from.
34dc7c2f
BB
930 * name - name of entry to remove.
931 * cr - credentials of caller.
841a7a98 932 * flags - case flags.
34dc7c2f
BB
933 *
934 * RETURN: 0 if success
935 * error code if failure
936 *
937 * Timestamps:
657ce253 938 * dzp - ctime|mtime
3558fd73 939 * ip - ctime (if nlink > 0)
34dc7c2f 940 */
428870ff 941
51c747de 942static uint64_t null_xattr = 0;
428870ff 943
e5c39b95 944int
657ce253 945zfs_remove(znode_t *dzp, char *name, cred_t *cr, int flags)
34dc7c2f 946{
657ce253 947 znode_t *zp;
572e2857 948 znode_t *xzp;
657ce253 949 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f 950 zilog_t *zilog;
a966c564 951 uint64_t acl_obj, xattr_obj;
3558fd73 952 uint64_t xattr_obj_unlinked = 0;
572e2857 953 uint64_t obj = 0;
dfbc8630 954 uint64_t links;
34dc7c2f
BB
955 zfs_dirlock_t *dl;
956 dmu_tx_t *tx;
a966c564
K
957 boolean_t may_delete_now, delete_now = FALSE;
958 boolean_t unlinked, toobig = FALSE;
34dc7c2f
BB
959 uint64_t txtype;
960 pathname_t *realnmp = NULL;
961 pathname_t realnm;
962 int error;
963 int zflg = ZEXISTS;
e8b96c60 964 boolean_t waited = B_FALSE;
34dc7c2f 965
32dec7bd 966 if (name == NULL)
967 return (SET_ERROR(EINVAL));
968
768eaced
CC
969 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
970 return (error);
0037b49e 971 zilog = zfsvfs->z_log;
34dc7c2f
BB
972
973 if (flags & FIGNORECASE) {
974 zflg |= ZCILOOK;
975 pn_alloc(&realnm);
976 realnmp = &realnm;
977 }
978
979top:
572e2857
BB
980 xattr_obj = 0;
981 xzp = NULL;
34dc7c2f
BB
982 /*
983 * Attempt to lock directory; fail if entry doesn't exist.
984 */
149e873a
BB
985 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
986 NULL, realnmp))) {
34dc7c2f
BB
987 if (realnmp)
988 pn_free(realnmp);
768eaced 989 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
990 return (error);
991 }
992
d4dc53da 993 if ((error = zfs_zaccess_delete(dzp, zp, cr, zfs_init_idmap))) {
34dc7c2f
BB
994 goto out;
995 }
996
997 /*
998 * Need to use rmdir for removing directories.
999 */
657ce253 1000 if (S_ISDIR(ZTOI(zp)->i_mode)) {
2e528b49 1001 error = SET_ERROR(EPERM);
34dc7c2f
BB
1002 goto out;
1003 }
1004
19d55079 1005 mutex_enter(&zp->z_lock);
657ce253 1006 may_delete_now = atomic_read(&ZTOI(zp)->i_count) == 1 &&
3fc92adc 1007 !zn_has_cached_data(zp, 0, LLONG_MAX);
19d55079
MA
1008 mutex_exit(&zp->z_lock);
1009
34dc7c2f 1010 /*
a966c564
K
1011 * We may delete the znode now, or we may put it in the unlinked set;
1012 * it depends on whether we're the last link, and on whether there are
1013 * other holds on the inode. So we dmu_tx_hold() the right things to
1014 * allow for either case.
34dc7c2f 1015 */
572e2857 1016 obj = zp->z_id;
0037b49e 1017 tx = dmu_tx_create(zfsvfs->z_os);
34dc7c2f 1018 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
428870ff
BB
1019 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1020 zfs_sa_upgrade_txholds(tx, zp);
1021 zfs_sa_upgrade_txholds(tx, dzp);
a966c564
K
1022 if (may_delete_now) {
1023 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1024 /* if the file is too big, only hold_free a token amount */
1025 dmu_tx_hold_free(tx, zp->z_id, 0,
1026 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1027 }
34dc7c2f
BB
1028
1029 /* are there any extended attributes? */
0037b49e 1030 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
428870ff 1031 &xattr_obj, sizeof (xattr_obj));
572e2857 1032 if (error == 0 && xattr_obj) {
0037b49e 1033 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
c99c9001 1034 ASSERT0(error);
428870ff
BB
1035 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1036 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
1037 }
1038
a966c564
K
1039 mutex_enter(&zp->z_lock);
1040 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1041 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1042 mutex_exit(&zp->z_lock);
1043
34dc7c2f 1044 /* charge as an update -- would be nice not to charge at all */
0037b49e 1045 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
34dc7c2f 1046
19d55079 1047 /*
1a04bab3 1048 * Mark this transaction as typically resulting in a net free of space
19d55079 1049 */
1a04bab3 1050 dmu_tx_mark_netfree(tx);
19d55079 1051
0735ecb3 1052 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
1053 if (error) {
1054 zfs_dirent_unlock(dl);
fb5f0bc8 1055 if (error == ERESTART) {
e8b96c60 1056 waited = B_TRUE;
34dc7c2f
BB
1057 dmu_tx_wait(tx);
1058 dmu_tx_abort(tx);
657ce253 1059 zrele(zp);
ea7e86d8 1060 if (xzp)
657ce253 1061 zrele(xzp);
34dc7c2f
BB
1062 goto top;
1063 }
1064 if (realnmp)
1065 pn_free(realnmp);
1066 dmu_tx_abort(tx);
657ce253 1067 zrele(zp);
ea7e86d8 1068 if (xzp)
657ce253 1069 zrele(xzp);
768eaced 1070 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1071 return (error);
1072 }
1073
1074 /*
1075 * Remove the directory entry.
1076 */
1077 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1078
1079 if (error) {
1080 dmu_tx_commit(tx);
1081 goto out;
1082 }
1083
1084 if (unlinked) {
572e2857
BB
1085 /*
1086 * Hold z_lock so that we can make sure that the ACL obj
1087 * hasn't changed. Could have been deleted due to
1088 * zfs_sa_upgrade().
1089 */
1090 mutex_enter(&zp->z_lock);
0037b49e 1091 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
428870ff 1092 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
a966c564 1093 delete_now = may_delete_now && !toobig &&
657ce253 1094 atomic_read(&ZTOI(zp)->i_count) == 1 &&
3fc92adc
BB
1095 !zn_has_cached_data(zp, 0, LLONG_MAX) &&
1096 xattr_obj == xattr_obj_unlinked &&
657ce253 1097 zfs_external_acl(zp) == acl_obj;
66a38fd1 1098 VERIFY_IMPLY(xattr_obj_unlinked, xzp);
a966c564
K
1099 }
1100
1101 if (delete_now) {
1102 if (xattr_obj_unlinked) {
dfbc8630 1103 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
a966c564 1104 mutex_enter(&xzp->z_lock);
a43570c5 1105 xzp->z_unlinked = B_TRUE;
dfbc8630
CD
1106 clear_nlink(ZTOI(xzp));
1107 links = 0;
0037b49e 1108 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
dfbc8630 1109 &links, sizeof (links), tx);
a966c564
K
1110 ASSERT3U(error, ==, 0);
1111 mutex_exit(&xzp->z_lock);
1112 zfs_unlinked_add(xzp, tx);
1113
1114 if (zp->z_is_sa)
1115 error = sa_remove(zp->z_sa_hdl,
0037b49e 1116 SA_ZPL_XATTR(zfsvfs), tx);
a966c564
K
1117 else
1118 error = sa_update(zp->z_sa_hdl,
0037b49e 1119 SA_ZPL_XATTR(zfsvfs), &null_xattr,
a966c564
K
1120 sizeof (uint64_t), tx);
1121 ASSERT0(error);
1122 }
1123 /*
1124 * Add to the unlinked set because a new reference could be
1125 * taken concurrently resulting in a deferred destruction.
1126 */
1127 zfs_unlinked_add(zp, tx);
1128 mutex_exit(&zp->z_lock);
a966c564 1129 } else if (unlinked) {
572e2857 1130 mutex_exit(&zp->z_lock);
34dc7c2f
BB
1131 zfs_unlinked_add(zp, tx);
1132 }
1133
1134 txtype = TX_REMOVE;
1135 if (flags & FIGNORECASE)
1136 txtype |= TX_CI;
8e556c5e 1137 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
34dc7c2f
BB
1138
1139 dmu_tx_commit(tx);
1140out:
1141 if (realnmp)
1142 pn_free(realnmp);
1143
1144 zfs_dirent_unlock(dl);
fc273894
KHN
1145 zfs_znode_update_vfs(dzp);
1146 zfs_znode_update_vfs(zp);
34dc7c2f 1147
ea7e86d8 1148 if (delete_now)
657ce253 1149 zrele(zp);
ea7e86d8 1150 else
657ce253 1151 zfs_zrele_async(zp);
a966c564
K
1152
1153 if (xzp) {
fc273894 1154 zfs_znode_update_vfs(xzp);
657ce253 1155 zfs_zrele_async(xzp);
a966c564 1156 }
428870ff 1157
0037b49e 1158 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1159 zil_commit(zilog, 0);
34dc7c2f 1160
768eaced 1161 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1162 return (error);
1163}
1164
1165/*
657ce253 1166 * Create a new directory and insert it into dzp using the name
34dc7c2f
BB
1167 * provided. Return a pointer to the inserted directory.
1168 *
657ce253 1169 * IN: dzp - znode of directory to add subdir to.
34dc7c2f
BB
1170 * dirname - name of new directory.
1171 * vap - attributes of new directory.
1172 * cr - credentials of caller.
841a7a98 1173 * flags - case flags.
34dc7c2f 1174 * vsecp - ACL to be set
2a068a13 1175 * mnt_ns - user namespace of the mount
34dc7c2f 1176 *
657ce253 1177 * OUT: zpp - znode of created directory.
34dc7c2f
BB
1178 *
1179 * RETURN: 0 if success
1180 * error code if failure
1181 *
1182 * Timestamps:
657ce253
MM
1183 * dzp - ctime|mtime updated
1184 * zpp - ctime|mtime|atime updated
34dc7c2f 1185 */
e5c39b95 1186int
657ce253 1187zfs_mkdir(znode_t *dzp, char *dirname, vattr_t *vap, znode_t **zpp,
d4dc53da 1188 cred_t *cr, int flags, vsecattr_t *vsecp, zidmap_t *mnt_ns)
34dc7c2f 1189{
657ce253
MM
1190 znode_t *zp;
1191 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f
BB
1192 zilog_t *zilog;
1193 zfs_dirlock_t *dl;
1194 uint64_t txtype;
1195 dmu_tx_t *tx;
1196 int error;
34dc7c2f 1197 int zf = ZNEW;
b128c09f
BB
1198 uid_t uid;
1199 gid_t gid = crgetgid(cr);
428870ff 1200 zfs_acl_ids_t acl_ids;
9babb374 1201 boolean_t fuid_dirtied;
e8b96c60 1202 boolean_t waited = B_FALSE;
34dc7c2f 1203
3558fd73 1204 ASSERT(S_ISDIR(vap->va_mode));
34dc7c2f
BB
1205
1206 /*
1207 * If we have an ephemeral id, ACL, or XVATTR then
1208 * make sure file system is at proper version
1209 */
1210
3558fd73 1211 uid = crgetuid(cr);
0037b49e 1212 if (zfsvfs->z_use_fuids == B_FALSE &&
3558fd73 1213 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2e528b49 1214 return (SET_ERROR(EINVAL));
34dc7c2f 1215
32dec7bd 1216 if (dirname == NULL)
1217 return (SET_ERROR(EINVAL));
1218
768eaced
CC
1219 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1220 return (error);
0037b49e 1221 zilog = zfsvfs->z_log;
34dc7c2f 1222
428870ff 1223 if (dzp->z_pflags & ZFS_XATTR) {
768eaced 1224 zfs_exit(zfsvfs, FTAG);
2e528b49 1225 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1226 }
1227
0037b49e 1228 if (zfsvfs->z_utf8 && u8_validate(dirname,
34dc7c2f 1229 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 1230 zfs_exit(zfsvfs, FTAG);
2e528b49 1231 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
1232 }
1233 if (flags & FIGNORECASE)
1234 zf |= ZCILOOK;
1235
5484965a 1236 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f 1237 if ((error = secpolicy_xvattr((xvattr_t *)vap,
3558fd73 1238 crgetuid(cr), cr, vap->va_mode)) != 0) {
768eaced 1239 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1240 return (error);
1241 }
428870ff 1242 }
34dc7c2f 1243
428870ff 1244 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2a068a13 1245 vsecp, &acl_ids, mnt_ns)) != 0) {
768eaced 1246 zfs_exit(zfsvfs, FTAG);
428870ff
BB
1247 return (error);
1248 }
34dc7c2f
BB
1249 /*
1250 * First make sure the new directory doesn't exist.
428870ff
BB
1251 *
1252 * Existence is checked first to make sure we don't return
1253 * EACCES instead of EEXIST which can cause some applications
1254 * to fail.
34dc7c2f
BB
1255 */
1256top:
657ce253 1257 *zpp = NULL;
34dc7c2f 1258
149e873a
BB
1259 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1260 NULL, NULL))) {
428870ff 1261 zfs_acl_ids_free(&acl_ids);
768eaced 1262 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1263 return (error);
1264 }
1265
2a068a13
YY
1266 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr,
1267 mnt_ns))) {
428870ff 1268 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1269 zfs_dirent_unlock(dl);
768eaced 1270 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1271 return (error);
1272 }
1273
9c5167d1 1274 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
45d1cae3 1275 zfs_acl_ids_free(&acl_ids);
9babb374 1276 zfs_dirent_unlock(dl);
768eaced 1277 zfs_exit(zfsvfs, FTAG);
2e528b49 1278 return (SET_ERROR(EDQUOT));
9babb374
BB
1279 }
1280
34dc7c2f
BB
1281 /*
1282 * Add a new entry to the directory.
1283 */
0037b49e 1284 tx = dmu_tx_create(zfsvfs->z_os);
34dc7c2f
BB
1285 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1286 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
0037b49e 1287 fuid_dirtied = zfsvfs->z_fuid_dirty;
9babb374 1288 if (fuid_dirtied)
0037b49e
BB
1289 zfs_fuid_txhold(zfsvfs, tx);
1290 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
428870ff
BB
1291 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1292 acl_ids.z_aclp->z_acl_bytes);
1293 }
1294
1295 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1296 ZFS_SA_BASE_ATTR_SIZE);
1297
0735ecb3 1298 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
1299 if (error) {
1300 zfs_dirent_unlock(dl);
fb5f0bc8 1301 if (error == ERESTART) {
e8b96c60 1302 waited = B_TRUE;
34dc7c2f
BB
1303 dmu_tx_wait(tx);
1304 dmu_tx_abort(tx);
1305 goto top;
1306 }
428870ff 1307 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1308 dmu_tx_abort(tx);
768eaced 1309 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1310 return (error);
1311 }
1312
1313 /*
1314 * Create new node.
1315 */
428870ff 1316 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
34dc7c2f
BB
1317
1318 /*
1319 * Now put new name in parent dir.
1320 */
599b8648
CC
1321 error = zfs_link_create(dl, zp, tx, ZNEW);
1322 if (error != 0) {
1323 zfs_znode_delete(zp, tx);
1324 remove_inode_hash(ZTOI(zp));
1325 goto out;
1326 }
1327
1328 if (fuid_dirtied)
1329 zfs_fuid_sync(zfsvfs, tx);
34dc7c2f 1330
657ce253 1331 *zpp = zp;
34dc7c2f
BB
1332
1333 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1334 if (flags & FIGNORECASE)
1335 txtype |= TX_CI;
9babb374
BB
1336 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1337 acl_ids.z_fuidp, vap);
34dc7c2f 1338
599b8648 1339out:
9babb374 1340 zfs_acl_ids_free(&acl_ids);
428870ff 1341
34dc7c2f
BB
1342 dmu_tx_commit(tx);
1343
1344 zfs_dirent_unlock(dl);
1345
0037b49e 1346 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1347 zil_commit(zilog, 0);
428870ff 1348
599b8648 1349 if (error != 0) {
657ce253 1350 zrele(zp);
599b8648 1351 } else {
fc273894
KHN
1352 zfs_znode_update_vfs(dzp);
1353 zfs_znode_update_vfs(zp);
599b8648 1354 }
768eaced 1355 zfs_exit(zfsvfs, FTAG);
599b8648 1356 return (error);
34dc7c2f
BB
1357}
1358
1359/*
1360 * Remove a directory subdir entry. If the current working
1361 * directory is the same as the subdir to be removed, the
1362 * remove will fail.
1363 *
657ce253 1364 * IN: dzp - znode of directory to remove from.
34dc7c2f 1365 * name - name of directory to be removed.
3558fd73 1366 * cwd - inode of current working directory.
34dc7c2f 1367 * cr - credentials of caller.
34dc7c2f
BB
1368 * flags - case flags
1369 *
d3cc8b15 1370 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
1371 *
1372 * Timestamps:
657ce253 1373 * dzp - ctime|mtime updated
34dc7c2f 1374 */
e5c39b95 1375int
657ce253 1376zfs_rmdir(znode_t *dzp, char *name, znode_t *cwd, cred_t *cr,
3558fd73 1377 int flags)
34dc7c2f 1378{
34dc7c2f 1379 znode_t *zp;
657ce253 1380 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f
BB
1381 zilog_t *zilog;
1382 zfs_dirlock_t *dl;
1383 dmu_tx_t *tx;
1384 int error;
1385 int zflg = ZEXISTS;
e8b96c60 1386 boolean_t waited = B_FALSE;
34dc7c2f 1387
32dec7bd 1388 if (name == NULL)
1389 return (SET_ERROR(EINVAL));
1390
768eaced
CC
1391 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1392 return (error);
0037b49e 1393 zilog = zfsvfs->z_log;
34dc7c2f
BB
1394
1395 if (flags & FIGNORECASE)
1396 zflg |= ZCILOOK;
1397top:
1398 zp = NULL;
1399
1400 /*
1401 * Attempt to lock directory; fail if entry doesn't exist.
1402 */
149e873a
BB
1403 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1404 NULL, NULL))) {
768eaced 1405 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1406 return (error);
1407 }
1408
d4dc53da 1409 if ((error = zfs_zaccess_delete(dzp, zp, cr, zfs_init_idmap))) {
34dc7c2f
BB
1410 goto out;
1411 }
1412
657ce253 1413 if (!S_ISDIR(ZTOI(zp)->i_mode)) {
2e528b49 1414 error = SET_ERROR(ENOTDIR);
34dc7c2f
BB
1415 goto out;
1416 }
1417
657ce253 1418 if (zp == cwd) {
2e528b49 1419 error = SET_ERROR(EINVAL);
34dc7c2f
BB
1420 goto out;
1421 }
1422
34dc7c2f 1423 /*
4e33ba4c 1424 * Grab a lock on the directory to make sure that no one is
34dc7c2f
BB
1425 * trying to add (or lookup) entries while we are removing it.
1426 */
1427 rw_enter(&zp->z_name_lock, RW_WRITER);
1428
1429 /*
1430 * Grab a lock on the parent pointer to make sure we play well
1431 * with the treewalk and directory rename code.
1432 */
1433 rw_enter(&zp->z_parent_lock, RW_WRITER);
1434
0037b49e 1435 tx = dmu_tx_create(zfsvfs->z_os);
34dc7c2f 1436 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
428870ff 1437 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
0037b49e 1438 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
428870ff
BB
1439 zfs_sa_upgrade_txholds(tx, zp);
1440 zfs_sa_upgrade_txholds(tx, dzp);
db707ad0 1441 dmu_tx_mark_netfree(tx);
0735ecb3 1442 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
1443 if (error) {
1444 rw_exit(&zp->z_parent_lock);
1445 rw_exit(&zp->z_name_lock);
1446 zfs_dirent_unlock(dl);
fb5f0bc8 1447 if (error == ERESTART) {
e8b96c60 1448 waited = B_TRUE;
34dc7c2f
BB
1449 dmu_tx_wait(tx);
1450 dmu_tx_abort(tx);
657ce253 1451 zrele(zp);
34dc7c2f
BB
1452 goto top;
1453 }
1454 dmu_tx_abort(tx);
657ce253 1455 zrele(zp);
768eaced 1456 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1457 return (error);
1458 }
1459
1460 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1461
1462 if (error == 0) {
1463 uint64_t txtype = TX_RMDIR;
1464 if (flags & FIGNORECASE)
1465 txtype |= TX_CI;
8e556c5e
CC
1466 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT,
1467 B_FALSE);
34dc7c2f
BB
1468 }
1469
1470 dmu_tx_commit(tx);
1471
1472 rw_exit(&zp->z_parent_lock);
1473 rw_exit(&zp->z_name_lock);
1474out:
1475 zfs_dirent_unlock(dl);
1476
fc273894
KHN
1477 zfs_znode_update_vfs(dzp);
1478 zfs_znode_update_vfs(zp);
657ce253 1479 zrele(zp);
34dc7c2f 1480
0037b49e 1481 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1482 zil_commit(zilog, 0);
428870ff 1483
768eaced 1484 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
1485 return (error);
1486}
1487
1488/*
841a7a98
TK
1489 * Read directory entries from the given directory cursor position and emit
1490 * name and position for each entry.
34dc7c2f 1491 *
3558fd73 1492 * IN: ip - inode of directory to read.
841a7a98
TK
1493 * ctx - directory entry context.
1494 * cr - credentials of caller.
34dc7c2f
BB
1495 *
1496 * RETURN: 0 if success
1497 * error code if failure
1498 *
1499 * Timestamps:
3558fd73 1500 * ip - atime updated
34dc7c2f
BB
1501 *
1502 * Note that the low 4 bits of the cookie returned by zap is always zero.
1503 * This allows us to use the low range for "special" directory entries:
1504 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1505 * we use the offset 2 for the '.zfs' directory.
1506 */
3558fd73 1507int
9464b959 1508zfs_readdir(struct inode *ip, zpl_dir_context_t *ctx, cred_t *cr)
34dc7c2f 1509{
ef70eff1 1510 (void) cr;
3558fd73 1511 znode_t *zp = ITOZ(ip);
0037b49e 1512 zfsvfs_t *zfsvfs = ITOZSB(ip);
34dc7c2f 1513 objset_t *os;
34dc7c2f
BB
1514 zap_cursor_t zc;
1515 zap_attribute_t zap;
34dc7c2f
BB
1516 int error;
1517 uint8_t prefetch;
c12e3a59 1518 uint8_t type;
3558fd73
BB
1519 int done = 0;
1520 uint64_t parent;
c12e3a59 1521 uint64_t offset; /* must be unsigned; checks for < 1 */
34dc7c2f 1522
768eaced
CC
1523 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1524 return (error);
34dc7c2f 1525
0037b49e 1526 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3558fd73
BB
1527 &parent, sizeof (parent))) != 0)
1528 goto out;
34dc7c2f
BB
1529
1530 /*
1531 * Quit if directory has been removed (posix)
1532 */
3558fd73
BB
1533 if (zp->z_unlinked)
1534 goto out;
1535
c12e3a59 1536 error = 0;
0037b49e 1537 os = zfsvfs->z_os;
c12e3a59 1538 offset = ctx->pos;
34dc7c2f
BB
1539 prefetch = zp->z_zn_prefetch;
1540
1541 /*
1542 * Initialize the iterator cursor.
1543 */
c12e3a59 1544 if (offset <= 3) {
34dc7c2f
BB
1545 /*
1546 * Start iteration from the beginning of the directory.
1547 */
1548 zap_cursor_init(&zc, os, zp->z_id);
1549 } else {
1550 /*
1551 * The offset is a serialized cursor.
1552 */
c12e3a59 1553 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
34dc7c2f
BB
1554 }
1555
34dc7c2f
BB
1556 /*
1557 * Transform to file-system independent format
1558 */
3558fd73
BB
1559 while (!done) {
1560 uint64_t objnum;
34dc7c2f
BB
1561 /*
1562 * Special case `.', `..', and `.zfs'.
1563 */
c12e3a59 1564 if (offset == 0) {
34dc7c2f
BB
1565 (void) strcpy(zap.za_name, ".");
1566 zap.za_normalization_conflict = 0;
1567 objnum = zp->z_id;
c12e3a59
RY
1568 type = DT_DIR;
1569 } else if (offset == 1) {
34dc7c2f
BB
1570 (void) strcpy(zap.za_name, "..");
1571 zap.za_normalization_conflict = 0;
428870ff 1572 objnum = parent;
c12e3a59
RY
1573 type = DT_DIR;
1574 } else if (offset == 2 && zfs_show_ctldir(zp)) {
34dc7c2f
BB
1575 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1576 zap.za_normalization_conflict = 0;
1577 objnum = ZFSCTL_INO_ROOT;
c12e3a59 1578 type = DT_DIR;
34dc7c2f
BB
1579 } else {
1580 /*
1581 * Grab next entry.
1582 */
3558fd73
BB
1583 if ((error = zap_cursor_retrieve(&zc, &zap))) {
1584 if (error == ENOENT)
34dc7c2f
BB
1585 break;
1586 else
1587 goto update;
1588 }
1589
0c5dde49
BB
1590 /*
1591 * Allow multiple entries provided the first entry is
1592 * the object id. Non-zpl consumers may safely make
1593 * use of the additional space.
1594 *
1595 * XXX: This should be a feature flag for compatibility
1596 */
34dc7c2f 1597 if (zap.za_integer_length != 8 ||
0c5dde49 1598 zap.za_num_integers == 0) {
34dc7c2f 1599 cmn_err(CE_WARN, "zap_readdir: bad directory "
0c5dde49
BB
1600 "entry, obj = %lld, offset = %lld, "
1601 "length = %d, num = %lld\n",
34dc7c2f 1602 (u_longlong_t)zp->z_id,
c12e3a59 1603 (u_longlong_t)offset,
0c5dde49
BB
1604 zap.za_integer_length,
1605 (u_longlong_t)zap.za_num_integers);
2e528b49 1606 error = SET_ERROR(ENXIO);
34dc7c2f
BB
1607 goto update;
1608 }
1609
1610 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
c12e3a59 1611 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
34dc7c2f 1612 }
0f37d0c8 1613
9464b959 1614 done = !zpl_dir_emit(ctx, zap.za_name, strlen(zap.za_name),
c12e3a59 1615 objnum, type);
0f37d0c8 1616 if (done)
34dc7c2f 1617 break;
34dc7c2f 1618
6c94e649
AM
1619 if (prefetch)
1620 dmu_prefetch_dnode(os, objnum, ZIO_PRIORITY_SYNC_READ);
34dc7c2f 1621
c12e3a59
RY
1622 /*
1623 * Move to the next entry, fill in the previous offset.
1624 */
1625 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
34dc7c2f 1626 zap_cursor_advance(&zc);
c12e3a59 1627 offset = zap_cursor_serialize(&zc);
34dc7c2f 1628 } else {
c12e3a59 1629 offset += 1;
34dc7c2f 1630 }
c12e3a59 1631 ctx->pos = offset;
34dc7c2f
BB
1632 }
1633 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1634
34dc7c2f
BB
1635update:
1636 zap_cursor_fini(&zc);
34dc7c2f
BB
1637 if (error == ENOENT)
1638 error = 0;
3558fd73 1639out:
768eaced 1640 zfs_exit(zfsvfs, FTAG);
34dc7c2f 1641
34dc7c2f
BB
1642 return (error);
1643}
1644
057e8eee
BB
1645/*
1646 * Get the basic file attributes and place them in the provided kstat
1647 * structure. The inode is assumed to be the authoritative source
1648 * for most of the attributes. However, the znode currently has the
1649 * authoritative atime, blksize, and block count.
1650 *
1651 * IN: ip - inode of file.
1652 *
1653 * OUT: sp - kstat values.
1654 *
1655 * RETURN: 0 (always succeeds)
1656 */
057e8eee 1657int
01d00dfa
CK
1658#ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
1659zfs_getattr_fast(zidmap_t *user_ns, u32 request_mask, struct inode *ip,
1660 struct kstat *sp)
1661#else
d4dc53da 1662zfs_getattr_fast(zidmap_t *user_ns, struct inode *ip, struct kstat *sp)
01d00dfa 1663#endif
057e8eee
BB
1664{
1665 znode_t *zp = ITOZ(ip);
0037b49e 1666 zfsvfs_t *zfsvfs = ITOZSB(ip);
b585bc4a
BB
1667 uint32_t blksize;
1668 u_longlong_t nblocks;
768eaced 1669 int error;
057e8eee 1670
768eaced
CC
1671 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1672 return (error);
a7b125e9 1673
057e8eee
BB
1674 mutex_enter(&zp->z_lock);
1675
01d00dfa
CK
1676#ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
1677 zpl_generic_fillattr(user_ns, request_mask, ip, sp);
1678#else
e2a82961 1679 zpl_generic_fillattr(user_ns, ip, sp);
01d00dfa 1680#endif
97aa3ba4
TK
1681 /*
1682 * +1 link count for root inode with visible '.zfs' directory.
1683 */
1684 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp))
1685 if (sp->nlink < ZFS_LINK_MAX)
1686 sp->nlink++;
057e8eee 1687
b585bc4a
BB
1688 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1689 sp->blksize = blksize;
1690 sp->blocks = nblocks;
1691
057e8eee
BB
1692 if (unlikely(zp->z_blksz == 0)) {
1693 /*
1694 * Block size hasn't been set; suggest maximal I/O transfers.
1695 */
0037b49e 1696 sp->blksize = zfsvfs->z_max_blksz;
057e8eee
BB
1697 }
1698
1699 mutex_exit(&zp->z_lock);
1700
aa9b2708
AV
1701 /*
1702 * Required to prevent NFS client from detecting different inode
1703 * numbers of snapshot root dentry before and after snapshot mount.
1704 */
0037b49e 1705 if (zfsvfs->z_issnap) {
aa9b2708
AV
1706 if (ip->i_sb->s_root->d_inode == ip)
1707 sp->ino = ZFSCTL_INO_SNAPDIRS -
0037b49e 1708 dmu_objset_id(zfsvfs->z_os);
aa9b2708
AV
1709 }
1710
768eaced 1711 zfs_exit(zfsvfs, FTAG);
a7b125e9 1712
057e8eee
BB
1713 return (0);
1714}
057e8eee 1715
9c5167d1
NF
1716/*
1717 * For the operation of changing file's user/group/project, we need to
1718 * handle not only the main object that is assigned to the file directly,
1719 * but also the ones that are used by the file via hidden xattr directory.
1720 *
1721 * Because the xattr directory may contains many EA entries, as to it may
1722 * be impossible to change all of them via the transaction of changing the
1723 * main object's user/group/project attributes. Then we have to change them
1724 * via other multiple independent transactions one by one. It may be not good
1725 * solution, but we have no better idea yet.
1726 */
1727static int
1728zfs_setattr_dir(znode_t *dzp)
1729{
1730 struct inode *dxip = ZTOI(dzp);
1731 struct inode *xip = NULL;
657ce253 1732 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
9c5167d1
NF
1733 objset_t *os = zfsvfs->z_os;
1734 zap_cursor_t zc;
1735 zap_attribute_t zap;
1736 zfs_dirlock_t *dl;
b7ab7ae2 1737 znode_t *zp = NULL;
9c5167d1
NF
1738 dmu_tx_t *tx = NULL;
1739 uint64_t uid, gid;
1740 sa_bulk_attr_t bulk[4];
8cb34421 1741 int count;
9c5167d1
NF
1742 int err;
1743
1744 zap_cursor_init(&zc, os, dzp->z_id);
1745 while ((err = zap_cursor_retrieve(&zc, &zap)) == 0) {
8cb34421 1746 count = 0;
9c5167d1
NF
1747 if (zap.za_integer_length != 8 || zap.za_num_integers != 1) {
1748 err = ENXIO;
1749 break;
1750 }
1751
1752 err = zfs_dirent_lock(&dl, dzp, (char *)zap.za_name, &zp,
1753 ZEXISTS, NULL, NULL);
1754 if (err == ENOENT)
1755 goto next;
1756 if (err)
1757 break;
1758
1759 xip = ZTOI(zp);
1760 if (KUID_TO_SUID(xip->i_uid) == KUID_TO_SUID(dxip->i_uid) &&
1761 KGID_TO_SGID(xip->i_gid) == KGID_TO_SGID(dxip->i_gid) &&
1762 zp->z_projid == dzp->z_projid)
1763 goto next;
1764
1765 tx = dmu_tx_create(os);
1766 if (!(zp->z_pflags & ZFS_PROJID))
1767 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1768 else
1769 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1770
1771 err = dmu_tx_assign(tx, TXG_WAIT);
1772 if (err)
1773 break;
1774
1775 mutex_enter(&dzp->z_lock);
1776
1777 if (KUID_TO_SUID(xip->i_uid) != KUID_TO_SUID(dxip->i_uid)) {
1778 xip->i_uid = dxip->i_uid;
1779 uid = zfs_uid_read(dxip);
1780 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1781 &uid, sizeof (uid));
1782 }
1783
1784 if (KGID_TO_SGID(xip->i_gid) != KGID_TO_SGID(dxip->i_gid)) {
1785 xip->i_gid = dxip->i_gid;
1786 gid = zfs_gid_read(dxip);
1787 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1788 &gid, sizeof (gid));
1789 }
1790
1791 if (zp->z_projid != dzp->z_projid) {
1792 if (!(zp->z_pflags & ZFS_PROJID)) {
1793 zp->z_pflags |= ZFS_PROJID;
1794 SA_ADD_BULK_ATTR(bulk, count,
1795 SA_ZPL_FLAGS(zfsvfs), NULL, &zp->z_pflags,
1796 sizeof (zp->z_pflags));
1797 }
1798
1799 zp->z_projid = dzp->z_projid;
1800 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PROJID(zfsvfs),
1801 NULL, &zp->z_projid, sizeof (zp->z_projid));
1802 }
1803
1804 mutex_exit(&dzp->z_lock);
1805
1806 if (likely(count > 0)) {
1807 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1808 dmu_tx_commit(tx);
1809 } else {
1810 dmu_tx_abort(tx);
1811 }
1812 tx = NULL;
1813 if (err != 0 && err != ENOENT)
1814 break;
1815
1816next:
657ce253
MM
1817 if (zp) {
1818 zrele(zp);
1819 zp = NULL;
9c5167d1
NF
1820 zfs_dirent_unlock(dl);
1821 }
1822 zap_cursor_advance(&zc);
1823 }
1824
1825 if (tx)
1826 dmu_tx_abort(tx);
657ce253
MM
1827 if (zp) {
1828 zrele(zp);
9c5167d1
NF
1829 zfs_dirent_unlock(dl);
1830 }
1831 zap_cursor_fini(&zc);
1832
1833 return (err == ENOENT ? 0 : err);
1834}
1835
34dc7c2f
BB
1836/*
1837 * Set the file attributes to the values contained in the
1838 * vattr structure.
1839 *
657ce253 1840 * IN: zp - znode of file to be modified.
34dc7c2f 1841 * vap - new attribute values.
5484965a 1842 * If ATTR_XVATTR set, then optional attrs are being set
34dc7c2f
BB
1843 * flags - ATTR_UTIME set if non-default time values provided.
1844 * - ATTR_NOACLCHECK (CIFS context only).
1845 * cr - credentials of caller.
2a068a13 1846 * mnt_ns - user namespace of the mount
34dc7c2f
BB
1847 *
1848 * RETURN: 0 if success
1849 * error code if failure
1850 *
1851 * Timestamps:
3558fd73 1852 * ip - ctime updated, mtime updated if size changed.
34dc7c2f 1853 */
e5c39b95 1854int
d4dc53da 1855zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zidmap_t *mnt_ns)
34dc7c2f 1856{
657ce253
MM
1857 struct inode *ip;
1858 zfsvfs_t *zfsvfs = ZTOZSB(zp);
d8b26866 1859 objset_t *os;
34dc7c2f
BB
1860 zilog_t *zilog;
1861 dmu_tx_t *tx;
1862 vattr_t oldva;
f4ea75d4 1863 xvattr_t *tmpxvattr;
5484965a 1864 uint_t mask = vap->va_mask;
a117a6d6 1865 uint_t saved_mask = 0;
34dc7c2f
BB
1866 int trim_mask = 0;
1867 uint64_t new_mode;
64aefee1 1868 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
572e2857 1869 uint64_t xattr_obj;
0df9673f 1870 uint64_t mtime[2], ctime[2], atime[2];
9c5167d1 1871 uint64_t projid = ZFS_INVALID_PROJID;
34dc7c2f
BB
1872 znode_t *attrzp;
1873 int need_policy = FALSE;
9c5167d1 1874 int err, err2 = 0;
34dc7c2f 1875 zfs_fuid_info_t *fuidp = NULL;
5484965a
BB
1876 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
1877 xoptattr_t *xoap;
1878 zfs_acl_t *aclp;
34dc7c2f 1879 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
428870ff 1880 boolean_t fuid_dirtied = B_FALSE;
9c5167d1 1881 boolean_t handle_eadir = B_FALSE;
17c37660 1882 sa_bulk_attr_t *bulk, *xattr_bulk;
9c5167d1 1883 int count = 0, xattr_count = 0, bulks = 8;
34dc7c2f
BB
1884
1885 if (mask == 0)
1886 return (0);
1887
768eaced
CC
1888 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1889 return (err);
657ce253 1890 ip = ZTOI(zp);
d8b26866 1891 os = zfsvfs->z_os;
34dc7c2f 1892
9c5167d1
NF
1893 /*
1894 * If this is a xvattr_t, then get a pointer to the structure of
1895 * optional attributes. If this is NULL, then we have a vattr_t.
1896 */
1897 xoap = xva_getxoptattr(xvap);
1898 if (xoap != NULL && (mask & ATTR_XVATTR)) {
1899 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
1900 if (!dmu_objset_projectquota_enabled(os) ||
1901 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode))) {
768eaced 1902 zfs_exit(zfsvfs, FTAG);
9c5167d1
NF
1903 return (SET_ERROR(ENOTSUP));
1904 }
1905
1906 projid = xoap->xoa_projid;
1907 if (unlikely(projid == ZFS_INVALID_PROJID)) {
768eaced 1908 zfs_exit(zfsvfs, FTAG);
9c5167d1
NF
1909 return (SET_ERROR(EINVAL));
1910 }
1911
1912 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
1913 projid = ZFS_INVALID_PROJID;
1914 else
1915 need_policy = TRUE;
1916 }
1917
1918 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2705ebf0
NF
1919 (xoap->xoa_projinherit !=
1920 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
9c5167d1
NF
1921 (!dmu_objset_projectquota_enabled(os) ||
1922 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode)))) {
768eaced 1923 zfs_exit(zfsvfs, FTAG);
2705ebf0 1924 return (SET_ERROR(ENOTSUP));
9c5167d1
NF
1925 }
1926 }
1927
0037b49e 1928 zilog = zfsvfs->z_log;
34dc7c2f
BB
1929
1930 /*
1931 * Make sure that if we have ephemeral uid/gid or xvattr specified
1932 * that file system is at proper version level
1933 */
5484965a 1934
0037b49e 1935 if (zfsvfs->z_use_fuids == B_FALSE &&
5484965a
BB
1936 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
1937 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
1938 (mask & ATTR_XVATTR))) {
768eaced 1939 zfs_exit(zfsvfs, FTAG);
2e528b49 1940 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1941 }
1942
3558fd73 1943 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
768eaced 1944 zfs_exit(zfsvfs, FTAG);
2e528b49 1945 return (SET_ERROR(EISDIR));
34dc7c2f
BB
1946 }
1947
3558fd73 1948 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
768eaced 1949 zfs_exit(zfsvfs, FTAG);
2e528b49 1950 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1951 }
1952
d1d7e268 1953 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
f4ea75d4 1954 xva_init(tmpxvattr);
5484965a 1955
9c5167d1
NF
1956 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP);
1957 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP);
17c37660 1958
5484965a
BB
1959 /*
1960 * Immutable files can only alter immutable bit and atime
1961 */
1962 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
1963 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
1964 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
ecb2b7dc 1965 err = SET_ERROR(EPERM);
f4ea75d4 1966 goto out3;
5484965a
BB
1967 }
1968
3558fd73 1969 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
ecb2b7dc 1970 err = SET_ERROR(EPERM);
f4ea75d4 1971 goto out3;
34dc7c2f
BB
1972 }
1973
5484965a
BB
1974 /*
1975 * Verify timestamps doesn't overflow 32 bits.
1976 * ZFS can handle large timestamps, but 32bit syscalls can't
1977 * handle times greater than 2039. This check should be removed
1978 * once large timestamps are fully supported.
1979 */
1980 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
d1d7e268
MK
1981 if (((mask & ATTR_ATIME) &&
1982 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
1983 ((mask & ATTR_MTIME) &&
1984 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
ecb2b7dc 1985 err = SET_ERROR(EOVERFLOW);
f4ea75d4 1986 goto out3;
5484965a
BB
1987 }
1988 }
1989
34dc7c2f
BB
1990top:
1991 attrzp = NULL;
572e2857 1992 aclp = NULL;
34dc7c2f 1993
45d1cae3 1994 /* Can this be moved to before the top label? */
0037b49e 1995 if (zfs_is_readonly(zfsvfs)) {
ecb2b7dc 1996 err = SET_ERROR(EROFS);
f4ea75d4 1997 goto out3;
34dc7c2f
BB
1998 }
1999
2000 /*
2001 * First validate permissions
2002 */
2003
3558fd73 2004 if (mask & ATTR_SIZE) {
2a068a13
YY
2005 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr,
2006 mnt_ns);
f4ea75d4
BB
2007 if (err)
2008 goto out3;
2009
34dc7c2f
BB
2010 /*
2011 * XXX - Note, we are not providing any open
2012 * mode flags here (like FNDELAY), so we may
2013 * block if there are locks present... this
2014 * should be addressed in openat().
2015 */
b128c09f 2016 /* XXX - would it be OK to generate a log record here? */
5484965a 2017 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
f4ea75d4
BB
2018 if (err)
2019 goto out3;
428870ff 2020 }
34dc7c2f 2021
5484965a
BB
2022 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2023 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2024 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2025 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2026 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2027 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2028 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2029 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2030 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2a068a13 2031 skipaclchk, cr, mnt_ns);
5484965a
BB
2032 }
2033
3558fd73
BB
2034 if (mask & (ATTR_UID|ATTR_GID)) {
2035 int idmask = (mask & (ATTR_UID|ATTR_GID));
34dc7c2f
BB
2036 int take_owner;
2037 int take_group;
2a068a13
YY
2038 uid_t uid;
2039 gid_t gid;
34dc7c2f
BB
2040
2041 /*
2042 * NOTE: even if a new mode is being set,
2043 * we may clear S_ISUID/S_ISGID bits.
2044 */
2045
3558fd73 2046 if (!(mask & ATTR_MODE))
5484965a 2047 vap->va_mode = zp->z_mode;
34dc7c2f
BB
2048
2049 /*
2050 * Take ownership or chgrp to group we are a member of
2051 */
2052
d4dc53da
YY
2053 uid = zfs_uid_to_vfsuid(mnt_ns, zfs_i_user_ns(ip),
2054 vap->va_uid);
2055 gid = zfs_gid_to_vfsgid(mnt_ns, zfs_i_user_ns(ip),
2056 vap->va_gid);
2a068a13 2057 take_owner = (mask & ATTR_UID) && (uid == crgetuid(cr));
3558fd73 2058 take_group = (mask & ATTR_GID) &&
2a068a13 2059 zfs_groupmember(zfsvfs, gid, cr);
34dc7c2f
BB
2060
2061 /*
5484965a 2062 * If both ATTR_UID and ATTR_GID are set then take_owner and
34dc7c2f
BB
2063 * take_group must both be set in order to allow taking
2064 * ownership.
2065 *
2066 * Otherwise, send the check through secpolicy_vnode_setattr()
2067 *
2068 */
2069
3558fd73
BB
2070 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2071 take_owner && take_group) ||
2072 ((idmask == ATTR_UID) && take_owner) ||
2073 ((idmask == ATTR_GID) && take_group)) {
34dc7c2f 2074 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2a068a13 2075 skipaclchk, cr, mnt_ns) == 0) {
34dc7c2f
BB
2076 /*
2077 * Remove setuid/setgid for non-privileged users
2078 */
5484965a 2079 (void) secpolicy_setid_clear(vap, cr);
3558fd73 2080 trim_mask = (mask & (ATTR_UID|ATTR_GID));
34dc7c2f
BB
2081 } else {
2082 need_policy = TRUE;
2083 }
2084 } else {
2085 need_policy = TRUE;
2086 }
2087 }
2088
2089 mutex_enter(&zp->z_lock);
428870ff 2090 oldva.va_mode = zp->z_mode;
572e2857 2091 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
5484965a
BB
2092 if (mask & ATTR_XVATTR) {
2093 /*
2094 * Update xvattr mask to include only those attributes
2095 * that are actually changing.
2096 *
2097 * the bits will be restored prior to actually setting
2098 * the attributes so the caller thinks they were set.
2099 */
2100 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2101 if (xoap->xoa_appendonly !=
2102 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2103 need_policy = TRUE;
2104 } else {
2105 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
f4ea75d4 2106 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
5484965a
BB
2107 }
2108 }
2109
9c5167d1
NF
2110 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2111 if (xoap->xoa_projinherit !=
2112 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2113 need_policy = TRUE;
2114 } else {
2115 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2116 XVA_SET_REQ(tmpxvattr, XAT_PROJINHERIT);
2117 }
2118 }
2119
5484965a
BB
2120 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2121 if (xoap->xoa_nounlink !=
2122 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2123 need_policy = TRUE;
2124 } else {
2125 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
f4ea75d4 2126 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
5484965a
BB
2127 }
2128 }
2129
2130 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2131 if (xoap->xoa_immutable !=
2132 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2133 need_policy = TRUE;
2134 } else {
2135 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
f4ea75d4 2136 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
5484965a
BB
2137 }
2138 }
2139
2140 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2141 if (xoap->xoa_nodump !=
2142 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2143 need_policy = TRUE;
2144 } else {
2145 XVA_CLR_REQ(xvap, XAT_NODUMP);
f4ea75d4 2146 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
5484965a
BB
2147 }
2148 }
2149
2150 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2151 if (xoap->xoa_av_modified !=
2152 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2153 need_policy = TRUE;
2154 } else {
2155 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
f4ea75d4 2156 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
5484965a
BB
2157 }
2158 }
2159
2160 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2161 if ((!S_ISREG(ip->i_mode) &&
2162 xoap->xoa_av_quarantined) ||
2163 xoap->xoa_av_quarantined !=
2164 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2165 need_policy = TRUE;
2166 } else {
2167 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
f4ea75d4 2168 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
5484965a
BB
2169 }
2170 }
2171
2172 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2173 mutex_exit(&zp->z_lock);
ecb2b7dc 2174 err = SET_ERROR(EPERM);
f4ea75d4 2175 goto out3;
5484965a
BB
2176 }
2177
2178 if (need_policy == FALSE &&
2179 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2180 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2181 need_policy = TRUE;
2182 }
2183 }
34dc7c2f
BB
2184
2185 mutex_exit(&zp->z_lock);
2186
3558fd73 2187 if (mask & ATTR_MODE) {
2a068a13
YY
2188 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr,
2189 mnt_ns) == 0) {
5484965a 2190 err = secpolicy_setid_setsticky_clear(ip, vap,
f224eddf 2191 &oldva, cr, mnt_ns, zfs_i_user_ns(ip));
f4ea75d4
BB
2192 if (err)
2193 goto out3;
3558fd73 2194 trim_mask |= ATTR_MODE;
34dc7c2f
BB
2195 } else {
2196 need_policy = TRUE;
2197 }
2198 }
2199
2200 if (need_policy) {
2201 /*
2202 * If trim_mask is set then take ownership
2203 * has been granted or write_acl is present and user
2204 * has the ability to modify mode. In that case remove
2205 * UID|GID and or MODE from mask so that
2206 * secpolicy_vnode_setattr() doesn't revoke it.
2207 */
2208
2209 if (trim_mask) {
5484965a
BB
2210 saved_mask = vap->va_mask;
2211 vap->va_mask &= ~trim_mask;
34dc7c2f 2212 }
5484965a 2213 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
b5459dd3 2214 zfs_zaccess_unix, zp);
f4ea75d4
BB
2215 if (err)
2216 goto out3;
34dc7c2f
BB
2217
2218 if (trim_mask)
5484965a 2219 vap->va_mask |= saved_mask;
34dc7c2f
BB
2220 }
2221
2222 /*
2223 * secpolicy_vnode_setattr, or take ownership may have
2224 * changed va_mask
2225 */
5484965a 2226 mask = vap->va_mask;
34dc7c2f 2227
9c5167d1
NF
2228 if ((mask & (ATTR_UID | ATTR_GID)) || projid != ZFS_INVALID_PROJID) {
2229 handle_eadir = B_TRUE;
0037b49e 2230 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
572e2857 2231 &xattr_obj, sizeof (xattr_obj));
428870ff 2232
572e2857 2233 if (err == 0 && xattr_obj) {
3558fd73 2234 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
428870ff
BB
2235 if (err)
2236 goto out2;
2237 }
3558fd73 2238 if (mask & ATTR_UID) {
0037b49e 2239 new_kuid = zfs_fuid_create(zfsvfs,
5484965a 2240 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
64aefee1 2241 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
9c5167d1
NF
2242 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2243 new_kuid)) {
572e2857 2244 if (attrzp)
657ce253 2245 zrele(attrzp);
ecb2b7dc 2246 err = SET_ERROR(EDQUOT);
428870ff
BB
2247 goto out2;
2248 }
2249 }
2250
3558fd73 2251 if (mask & ATTR_GID) {
0037b49e
BB
2252 new_kgid = zfs_fuid_create(zfsvfs,
2253 (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp);
64aefee1 2254 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
9c5167d1
NF
2255 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2256 new_kgid)) {
572e2857 2257 if (attrzp)
657ce253 2258 zrele(attrzp);
ecb2b7dc 2259 err = SET_ERROR(EDQUOT);
428870ff
BB
2260 goto out2;
2261 }
2262 }
9c5167d1
NF
2263
2264 if (projid != ZFS_INVALID_PROJID &&
2265 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2266 if (attrzp)
657ce253 2267 zrele(attrzp);
9c5167d1
NF
2268 err = EDQUOT;
2269 goto out2;
2270 }
428870ff 2271 }
9c5167d1 2272 tx = dmu_tx_create(os);
34dc7c2f 2273
3558fd73 2274 if (mask & ATTR_MODE) {
428870ff 2275 uint64_t pmode = zp->z_mode;
572e2857 2276 uint64_t acl_obj;
5484965a 2277 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
34dc7c2f 2278
7bf3e1fa
PH
2279 if (ZTOZSB(zp)->z_acl_mode == ZFS_ACL_RESTRICTED &&
2280 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2281 err = EPERM;
2282 goto out;
2283 }
2284
156f74fc
MM
2285 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2286 goto out;
428870ff 2287
572e2857
BB
2288 mutex_enter(&zp->z_lock);
2289 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
428870ff
BB
2290 /*
2291 * Are we upgrading ACL from old V0 format
2292 * to V1 format?
2293 */
0037b49e 2294 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
572e2857 2295 zfs_znode_acl_version(zp) ==
34dc7c2f 2296 ZFS_ACL_VERSION_INITIAL) {
572e2857 2297 dmu_tx_hold_free(tx, acl_obj, 0,
34dc7c2f
BB
2298 DMU_OBJECT_END);
2299 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2300 0, aclp->z_acl_bytes);
2301 } else {
572e2857 2302 dmu_tx_hold_write(tx, acl_obj, 0,
34dc7c2f
BB
2303 aclp->z_acl_bytes);
2304 }
428870ff 2305 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
34dc7c2f
BB
2306 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2307 0, aclp->z_acl_bytes);
2308 }
572e2857 2309 mutex_exit(&zp->z_lock);
428870ff
BB
2310 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2311 } else {
9c5167d1
NF
2312 if (((mask & ATTR_XVATTR) &&
2313 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2314 (projid != ZFS_INVALID_PROJID &&
2315 !(zp->z_pflags & ZFS_PROJID)))
5484965a
BB
2316 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2317 else
2318 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
2319 }
2320
428870ff
BB
2321 if (attrzp) {
2322 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
2323 }
2324
0037b49e 2325 fuid_dirtied = zfsvfs->z_fuid_dirty;
428870ff 2326 if (fuid_dirtied)
0037b49e 2327 zfs_fuid_txhold(zfsvfs, tx);
428870ff
BB
2328
2329 zfs_sa_upgrade_txholds(tx, zp);
2330
384f8a09
MA
2331 err = dmu_tx_assign(tx, TXG_WAIT);
2332 if (err)
9babb374 2333 goto out;
34dc7c2f 2334
428870ff 2335 count = 0;
34dc7c2f
BB
2336 /*
2337 * Set each attribute requested.
2338 * We group settings according to the locks they need to acquire.
2339 *
2340 * Note: you cannot set ctime directly, although it will be
2341 * updated as a side-effect of calling this function.
2342 */
2343
9c5167d1
NF
2344 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2345 /*
2346 * For the existed object that is upgraded from old system,
2347 * its on-disk layout has no slot for the project ID attribute.
2348 * But quota accounting logic needs to access related slots by
2349 * offset directly. So we need to adjust old objects' layout
2350 * to make the project ID to some unified and fixed offset.
2351 */
2352 if (attrzp)
2353 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2354 if (err == 0)
2355 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2356
2357 if (unlikely(err == EEXIST))
2358 err = 0;
2359 else if (err != 0)
2360 goto out;
2361 else
2362 projid = ZFS_INVALID_PROJID;
2363 }
572e2857 2364
3558fd73 2365 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 2366 mutex_enter(&zp->z_acl_lock);
34dc7c2f
BB
2367 mutex_enter(&zp->z_lock);
2368
0037b49e 2369 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff
BB
2370 &zp->z_pflags, sizeof (zp->z_pflags));
2371
2372 if (attrzp) {
3558fd73 2373 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 2374 mutex_enter(&attrzp->z_acl_lock);
428870ff
BB
2375 mutex_enter(&attrzp->z_lock);
2376 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
0037b49e 2377 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
428870ff 2378 sizeof (attrzp->z_pflags));
9c5167d1
NF
2379 if (projid != ZFS_INVALID_PROJID) {
2380 attrzp->z_projid = projid;
2381 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2382 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2383 sizeof (attrzp->z_projid));
2384 }
428870ff
BB
2385 }
2386
3558fd73 2387 if (mask & (ATTR_UID|ATTR_GID)) {
428870ff 2388
3558fd73 2389 if (mask & ATTR_UID) {
64aefee1
NB
2390 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
2391 new_uid = zfs_uid_read(ZTOI(zp));
0037b49e 2392 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
428870ff 2393 &new_uid, sizeof (new_uid));
428870ff
BB
2394 if (attrzp) {
2395 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
0037b49e 2396 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
428870ff 2397 sizeof (new_uid));
2c6abf15 2398 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
428870ff
BB
2399 }
2400 }
2401
3558fd73 2402 if (mask & ATTR_GID) {
64aefee1
NB
2403 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
2404 new_gid = zfs_gid_read(ZTOI(zp));
0037b49e 2405 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
428870ff 2406 NULL, &new_gid, sizeof (new_gid));
428870ff
BB
2407 if (attrzp) {
2408 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
0037b49e 2409 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
428870ff 2410 sizeof (new_gid));
64aefee1 2411 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
428870ff
BB
2412 }
2413 }
3558fd73 2414 if (!(mask & ATTR_MODE)) {
0037b49e 2415 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
428870ff
BB
2416 NULL, &new_mode, sizeof (new_mode));
2417 new_mode = zp->z_mode;
2418 }
2419 err = zfs_acl_chown_setattr(zp);
2420 ASSERT(err == 0);
2421 if (attrzp) {
2422 err = zfs_acl_chown_setattr(attrzp);
2423 ASSERT(err == 0);
2424 }
2425 }
2426
3558fd73 2427 if (mask & ATTR_MODE) {
0037b49e 2428 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
428870ff 2429 &new_mode, sizeof (new_mode));
12fa7f34 2430 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
99c564bc 2431 ASSERT3P(aclp, !=, NULL);
9babb374 2432 err = zfs_aclset_common(zp, aclp, cr, tx);
c99c9001 2433 ASSERT0(err);
572e2857
BB
2434 if (zp->z_acl_cached)
2435 zfs_acl_free(zp->z_acl_cached);
45d1cae3
BB
2436 zp->z_acl_cached = aclp;
2437 aclp = NULL;
34dc7c2f
BB
2438 }
2439
704cd075 2440 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
a43570c5 2441 zp->z_atime_dirty = B_FALSE;
f0bf7a24 2442 inode_timespec_t tmp_atime = zpl_inode_get_atime(ip);
db4fc559 2443 ZFS_TIME_ENCODE(&tmp_atime, atime);
0037b49e 2444 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
0df9673f 2445 &atime, sizeof (atime));
34dc7c2f
BB
2446 }
2447
99834d19 2448 if (mask & (ATTR_MTIME | ATTR_SIZE)) {
5484965a 2449 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
db4fc559
RN
2450 zpl_inode_set_mtime_to_ts(ZTOI(zp),
2451 zpl_inode_timestamp_truncate(vap->va_mtime, ZTOI(zp)));
87f9371a 2452
0037b49e 2453 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
428870ff 2454 mtime, sizeof (mtime));
34dc7c2f
BB
2455 }
2456
99834d19 2457 if (mask & (ATTR_CTIME | ATTR_SIZE)) {
87f9371a 2458 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
b37f2934
CK
2459 zpl_inode_set_ctime_to_ts(ZTOI(zp),
2460 zpl_inode_timestamp_truncate(vap->va_ctime, ZTOI(zp)));
0037b49e 2461 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
87f9371a 2462 ctime, sizeof (ctime));
428870ff 2463 }
87f9371a 2464
9c5167d1
NF
2465 if (projid != ZFS_INVALID_PROJID) {
2466 zp->z_projid = projid;
2467 SA_ADD_BULK_ATTR(bulk, count,
2468 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2469 sizeof (zp->z_projid));
2470 }
2471
87f9371a
NB
2472 if (attrzp && mask) {
2473 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
0037b49e 2474 SA_ZPL_CTIME(zfsvfs), NULL, &ctime,
87f9371a
NB
2475 sizeof (ctime));
2476 }
2477
34dc7c2f
BB
2478 /*
2479 * Do this after setting timestamps to prevent timestamp
2480 * update from toggling bit
2481 */
2482
5484965a
BB
2483 if (xoap && (mask & ATTR_XVATTR)) {
2484
2485 /*
2486 * restore trimmed off masks
2487 * so that return masks can be set for caller.
2488 */
2489
f4ea75d4 2490 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
5484965a
BB
2491 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2492 }
f4ea75d4 2493 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
5484965a
BB
2494 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2495 }
f4ea75d4 2496 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
5484965a
BB
2497 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2498 }
f4ea75d4 2499 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
5484965a
BB
2500 XVA_SET_REQ(xvap, XAT_NODUMP);
2501 }
f4ea75d4 2502 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
5484965a
BB
2503 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2504 }
f4ea75d4 2505 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
5484965a
BB
2506 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2507 }
9c5167d1
NF
2508 if (XVA_ISSET_REQ(tmpxvattr, XAT_PROJINHERIT)) {
2509 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2510 }
5484965a
BB
2511
2512 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2513 ASSERT(S_ISREG(ip->i_mode));
2514
2515 zfs_xvattr_set(zp, xvap, tx);
2516 }
2517
9babb374 2518 if (fuid_dirtied)
0037b49e 2519 zfs_fuid_sync(zfsvfs, tx);
9babb374 2520
34dc7c2f 2521 if (mask != 0)
5484965a 2522 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
34dc7c2f 2523
34dc7c2f 2524 mutex_exit(&zp->z_lock);
3558fd73 2525 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 2526 mutex_exit(&zp->z_acl_lock);
34dc7c2f 2527
572e2857 2528 if (attrzp) {
3558fd73 2529 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857
BB
2530 mutex_exit(&attrzp->z_acl_lock);
2531 mutex_exit(&attrzp->z_lock);
2532 }
9babb374 2533out:
9c5167d1 2534 if (err == 0 && xattr_count > 0) {
428870ff
BB
2535 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2536 xattr_count, tx);
2537 ASSERT(err2 == 0);
2538 }
2539
45d1cae3 2540 if (aclp)
9babb374 2541 zfs_acl_free(aclp);
9babb374
BB
2542
2543 if (fuidp) {
2544 zfs_fuid_info_free(fuidp);
2545 fuidp = NULL;
2546 }
2547
428870ff 2548 if (err) {
9babb374 2549 dmu_tx_abort(tx);
ea7e86d8 2550 if (attrzp)
657ce253 2551 zrele(attrzp);
428870ff
BB
2552 if (err == ERESTART)
2553 goto top;
2554 } else {
9c5167d1
NF
2555 if (count > 0)
2556 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
9babb374 2557 dmu_tx_commit(tx);
9c5167d1
NF
2558 if (attrzp) {
2559 if (err2 == 0 && handle_eadir)
2e7f664f 2560 err = zfs_setattr_dir(attrzp);
657ce253 2561 zrele(attrzp);
9c5167d1 2562 }
fc273894 2563 zfs_znode_update_vfs(zp);
428870ff
BB
2564 }
2565
428870ff 2566out2:
9c5167d1 2567 if (os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 2568 zil_commit(zilog, 0);
34dc7c2f 2569
f4ea75d4 2570out3:
9c5167d1
NF
2571 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * bulks);
2572 kmem_free(bulk, sizeof (sa_bulk_attr_t) * bulks);
d1d7e268 2573 kmem_free(tmpxvattr, sizeof (xvattr_t));
768eaced 2574 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
2575 return (err);
2576}
2577
2578typedef struct zfs_zlock {
2579 krwlock_t *zl_rwlock; /* lock we acquired */
2580 znode_t *zl_znode; /* znode we held */
2581 struct zfs_zlock *zl_next; /* next in list */
2582} zfs_zlock_t;
2583
2584/*
2585 * Drop locks and release vnodes that were held by zfs_rename_lock().
2586 */
2587static void
2588zfs_rename_unlock(zfs_zlock_t **zlpp)
2589{
2590 zfs_zlock_t *zl;
2591
2592 while ((zl = *zlpp) != NULL) {
2593 if (zl->zl_znode != NULL)
657ce253 2594 zfs_zrele_async(zl->zl_znode);
34dc7c2f
BB
2595 rw_exit(zl->zl_rwlock);
2596 *zlpp = zl->zl_next;
2597 kmem_free(zl, sizeof (*zl));
2598 }
2599}
2600
2601/*
2602 * Search back through the directory tree, using the ".." entries.
2603 * Lock each directory in the chain to prevent concurrent renames.
2604 * Fail any attempt to move a directory into one of its own descendants.
2605 * XXX - z_parent_lock can overlap with map or grow locks
2606 */
2607static int
2608zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2609{
2610 zfs_zlock_t *zl;
2611 znode_t *zp = tdzp;
3558fd73 2612 uint64_t rootid = ZTOZSB(zp)->z_root;
428870ff 2613 uint64_t oidp = zp->z_id;
34dc7c2f
BB
2614 krwlock_t *rwlp = &szp->z_parent_lock;
2615 krw_t rw = RW_WRITER;
2616
2617 /*
2618 * First pass write-locks szp and compares to zp->z_id.
2619 * Later passes read-lock zp and compare to zp->z_parent.
2620 */
2621 do {
2622 if (!rw_tryenter(rwlp, rw)) {
2623 /*
2624 * Another thread is renaming in this path.
2625 * Note that if we are a WRITER, we don't have any
2626 * parent_locks held yet.
2627 */
2628 if (rw == RW_READER && zp->z_id > szp->z_id) {
2629 /*
2630 * Drop our locks and restart
2631 */
2632 zfs_rename_unlock(&zl);
2633 *zlpp = NULL;
2634 zp = tdzp;
428870ff 2635 oidp = zp->z_id;
34dc7c2f
BB
2636 rwlp = &szp->z_parent_lock;
2637 rw = RW_WRITER;
2638 continue;
2639 } else {
2640 /*
2641 * Wait for other thread to drop its locks
2642 */
2643 rw_enter(rwlp, rw);
2644 }
2645 }
2646
2647 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2648 zl->zl_rwlock = rwlp;
2649 zl->zl_znode = NULL;
2650 zl->zl_next = *zlpp;
2651 *zlpp = zl;
2652
428870ff 2653 if (oidp == szp->z_id) /* We're a descendant of szp */
2e528b49 2654 return (SET_ERROR(EINVAL));
34dc7c2f 2655
428870ff 2656 if (oidp == rootid) /* We've hit the top */
34dc7c2f
BB
2657 return (0);
2658
2659 if (rw == RW_READER) { /* i.e. not the first pass */
3558fd73 2660 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
34dc7c2f
BB
2661 if (error)
2662 return (error);
2663 zl->zl_znode = zp;
2664 }
3558fd73 2665 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
428870ff 2666 &oidp, sizeof (oidp));
34dc7c2f
BB
2667 rwlp = &zp->z_parent_lock;
2668 rw = RW_READER;
2669
2670 } while (zp->z_id != sdzp->z_id);
2671
2672 return (0);
2673}
2674
2675/*
2676 * Move an entry from the provided source directory to the target
2677 * directory. Change the entry name as indicated.
2678 *
657ce253 2679 * IN: sdzp - Source directory containing the "old entry".
34dc7c2f 2680 * snm - Old entry name.
657ce253 2681 * tdzp - Target directory to contain the "new entry".
34dc7c2f
BB
2682 * tnm - New entry name.
2683 * cr - credentials of caller.
34dc7c2f 2684 * flags - case flags
dbf6108b
AS
2685 * rflags - RENAME_* flags
2686 * wa_vap - attributes for RENAME_WHITEOUT (must be a char 0:0).
2a068a13 2687 * mnt_ns - user namespace of the mount
34dc7c2f 2688 *
d3cc8b15 2689 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
2690 *
2691 * Timestamps:
657ce253 2692 * sdzp,tdzp - ctime|mtime updated
34dc7c2f 2693 */
e5c39b95 2694int
657ce253 2695zfs_rename(znode_t *sdzp, char *snm, znode_t *tdzp, char *tnm,
d4dc53da 2696 cred_t *cr, int flags, uint64_t rflags, vattr_t *wo_vap, zidmap_t *mnt_ns)
34dc7c2f 2697{
657ce253
MM
2698 znode_t *szp, *tzp;
2699 zfsvfs_t *zfsvfs = ZTOZSB(sdzp);
34dc7c2f 2700 zilog_t *zilog;
34dc7c2f
BB
2701 zfs_dirlock_t *sdl, *tdl;
2702 dmu_tx_t *tx;
2703 zfs_zlock_t *zl;
2704 int cmp, serr, terr;
2705 int error = 0;
2706 int zflg = 0;
e8b96c60 2707 boolean_t waited = B_FALSE;
dbf6108b
AS
2708 /* Needed for whiteout inode creation. */
2709 boolean_t fuid_dirtied;
2710 zfs_acl_ids_t acl_ids;
2711 boolean_t have_acl = B_FALSE;
2712 znode_t *wzp = NULL;
2713
34dc7c2f 2714
32dec7bd 2715 if (snm == NULL || tnm == NULL)
2716 return (SET_ERROR(EINVAL));
2717
dbf6108b
AS
2718 if (rflags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
2719 return (SET_ERROR(EINVAL));
2720
2721 /* Already checked by Linux VFS, but just to make sure. */
2722 if (rflags & RENAME_EXCHANGE &&
2723 (rflags & (RENAME_NOREPLACE | RENAME_WHITEOUT)))
2724 return (SET_ERROR(EINVAL));
2725
2726 /*
2727 * Make sure we only get wo_vap iff. RENAME_WHITEOUT and that it's the
2728 * right kind of vattr_t for the whiteout file. These are set
2729 * internally by ZFS so should never be incorrect.
2730 */
2731 VERIFY_EQUIV(rflags & RENAME_WHITEOUT, wo_vap != NULL);
2732 VERIFY_IMPLY(wo_vap, wo_vap->va_mode == S_IFCHR);
2733 VERIFY_IMPLY(wo_vap, wo_vap->va_rdev == makedevice(0, 0));
2734
768eaced
CC
2735 if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0)
2736 return (error);
0037b49e 2737 zilog = zfsvfs->z_log;
34dc7c2f 2738
768eaced
CC
2739 if ((error = zfs_verify_zp(tdzp)) != 0) {
2740 zfs_exit(zfsvfs, FTAG);
2741 return (error);
2742 }
812e91a7
MT
2743
2744 /*
2745 * We check i_sb because snapshots and the ctldir must have different
2746 * super blocks.
2747 */
657ce253
MM
2748 if (ZTOI(tdzp)->i_sb != ZTOI(sdzp)->i_sb ||
2749 zfsctl_is_node(ZTOI(tdzp))) {
768eaced 2750 zfs_exit(zfsvfs, FTAG);
2e528b49 2751 return (SET_ERROR(EXDEV));
34dc7c2f
BB
2752 }
2753
0037b49e 2754 if (zfsvfs->z_utf8 && u8_validate(tnm,
34dc7c2f 2755 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 2756 zfs_exit(zfsvfs, FTAG);
2e528b49 2757 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
2758 }
2759
2760 if (flags & FIGNORECASE)
2761 zflg |= ZCILOOK;
2762
2763top:
2764 szp = NULL;
2765 tzp = NULL;
2766 zl = NULL;
2767
2768 /*
2769 * This is to prevent the creation of links into attribute space
2770 * by renaming a linked file into/outof an attribute directory.
2771 * See the comment in zfs_link() for why this is considered bad.
2772 */
428870ff 2773 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
768eaced 2774 zfs_exit(zfsvfs, FTAG);
2e528b49 2775 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2776 }
2777
2778 /*
2779 * Lock source and target directory entries. To prevent deadlock,
2780 * a lock ordering must be defined. We lock the directory with
2781 * the smallest object id first, or if it's a tie, the one with
2782 * the lexically first name.
2783 */
2784 if (sdzp->z_id < tdzp->z_id) {
2785 cmp = -1;
2786 } else if (sdzp->z_id > tdzp->z_id) {
2787 cmp = 1;
2788 } else {
2789 /*
2790 * First compare the two name arguments without
2791 * considering any case folding.
2792 */
0037b49e 2793 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
34dc7c2f
BB
2794
2795 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
0037b49e 2796 ASSERT(error == 0 || !zfsvfs->z_utf8);
34dc7c2f
BB
2797 if (cmp == 0) {
2798 /*
2799 * POSIX: "If the old argument and the new argument
2800 * both refer to links to the same existing file,
2801 * the rename() function shall return successfully
2802 * and perform no other action."
2803 */
768eaced 2804 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
2805 return (0);
2806 }
2807 /*
2808 * If the file system is case-folding, then we may
2809 * have some more checking to do. A case-folding file
2810 * system is either supporting mixed case sensitivity
2811 * access or is completely case-insensitive. Note
2812 * that the file system is always case preserving.
2813 *
2814 * In mixed sensitivity mode case sensitive behavior
2815 * is the default. FIGNORECASE must be used to
2816 * explicitly request case insensitive behavior.
2817 *
2818 * If the source and target names provided differ only
2819 * by case (e.g., a request to rename 'tim' to 'Tim'),
2820 * we will treat this as a special case in the
2821 * case-insensitive mode: as long as the source name
2822 * is an exact match, we will allow this to proceed as
2823 * a name-change request.
2824 */
0037b49e
BB
2825 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
2826 (zfsvfs->z_case == ZFS_CASE_MIXED &&
34dc7c2f 2827 flags & FIGNORECASE)) &&
0037b49e 2828 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
34dc7c2f
BB
2829 &error) == 0) {
2830 /*
2831 * case preserving rename request, require exact
2832 * name matches
2833 */
2834 zflg |= ZCIEXACT;
2835 zflg &= ~ZCILOOK;
2836 }
2837 }
2838
428870ff
BB
2839 /*
2840 * If the source and destination directories are the same, we should
2841 * grab the z_name_lock of that directory only once.
2842 */
2843 if (sdzp == tdzp) {
2844 zflg |= ZHAVELOCK;
2845 rw_enter(&sdzp->z_name_lock, RW_READER);
2846 }
2847
34dc7c2f
BB
2848 if (cmp < 0) {
2849 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
2850 ZEXISTS | zflg, NULL, NULL);
2851 terr = zfs_dirent_lock(&tdl,
2852 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
2853 } else {
2854 terr = zfs_dirent_lock(&tdl,
2855 tdzp, tnm, &tzp, zflg, NULL, NULL);
2856 serr = zfs_dirent_lock(&sdl,
2857 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
2858 NULL, NULL);
2859 }
2860
2861 if (serr) {
2862 /*
2863 * Source entry invalid or not there.
2864 */
2865 if (!terr) {
2866 zfs_dirent_unlock(tdl);
2867 if (tzp)
657ce253 2868 zrele(tzp);
34dc7c2f 2869 }
428870ff
BB
2870
2871 if (sdzp == tdzp)
2872 rw_exit(&sdzp->z_name_lock);
2873
34dc7c2f
BB
2874 if (strcmp(snm, "..") == 0)
2875 serr = EINVAL;
768eaced 2876 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
2877 return (serr);
2878 }
2879 if (terr) {
2880 zfs_dirent_unlock(sdl);
657ce253 2881 zrele(szp);
428870ff
BB
2882
2883 if (sdzp == tdzp)
2884 rw_exit(&sdzp->z_name_lock);
2885
34dc7c2f
BB
2886 if (strcmp(tnm, "..") == 0)
2887 terr = EINVAL;
768eaced 2888 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
2889 return (terr);
2890 }
2891
9c5167d1
NF
2892 /*
2893 * If we are using project inheritance, means if the directory has
2894 * ZFS_PROJINHERIT set, then its descendant directories will inherit
2895 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
2896 * such case, we only allow renames into our tree when the project
2897 * IDs are the same.
2898 */
2899 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
2900 tdzp->z_projid != szp->z_projid) {
2901 error = SET_ERROR(EXDEV);
2902 goto out;
2903 }
2904
34dc7c2f
BB
2905 /*
2906 * Must have write access at the source to remove the old entry
2907 * and write access at the target to create the new entry.
2908 * Note that if target and source are the same, this can be
2909 * done in a single check.
2910 */
2a068a13 2911 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, mnt_ns)))
34dc7c2f
BB
2912 goto out;
2913
3558fd73 2914 if (S_ISDIR(ZTOI(szp)->i_mode)) {
34dc7c2f
BB
2915 /*
2916 * Check to make sure rename is valid.
2917 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2918 */
149e873a 2919 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
34dc7c2f
BB
2920 goto out;
2921 }
2922
2923 /*
2924 * Does target exist?
2925 */
2926 if (tzp) {
dbf6108b
AS
2927 if (rflags & RENAME_NOREPLACE) {
2928 error = SET_ERROR(EEXIST);
2929 goto out;
2930 }
34dc7c2f 2931 /*
dbf6108b 2932 * Source and target must be the same type (unless exchanging).
34dc7c2f 2933 */
dbf6108b
AS
2934 if (!(rflags & RENAME_EXCHANGE)) {
2935 boolean_t s_is_dir = S_ISDIR(ZTOI(szp)->i_mode) != 0;
2936 boolean_t t_is_dir = S_ISDIR(ZTOI(tzp)->i_mode) != 0;
e015d6cc 2937
dbf6108b
AS
2938 if (s_is_dir != t_is_dir) {
2939 error = SET_ERROR(s_is_dir ? ENOTDIR : EISDIR);
2940 goto out;
2941 }
34dc7c2f
BB
2942 }
2943 /*
2944 * POSIX dictates that when the source and target
2945 * entries refer to the same file object, rename
2946 * must do nothing and exit without error.
2947 */
2948 if (szp->z_id == tzp->z_id) {
2949 error = 0;
2950 goto out;
2951 }
dbf6108b
AS
2952 } else if (rflags & RENAME_EXCHANGE) {
2953 /* Target must exist for RENAME_EXCHANGE. */
2954 error = SET_ERROR(ENOENT);
2955 goto out;
2956 }
2957
2958 /* Set up inode creation for RENAME_WHITEOUT. */
2959 if (rflags & RENAME_WHITEOUT) {
2960 /*
2961 * Whiteout files are not regular files or directories, so to
2962 * match zfs_create() we do not inherit the project id.
2963 */
2964 uint64_t wo_projid = ZFS_DEFAULT_PROJID;
2965
2966 error = zfs_zaccess(sdzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns);
2967 if (error)
2968 goto out;
2969
2970 if (!have_acl) {
2971 error = zfs_acl_ids_create(sdzp, 0, wo_vap, cr, NULL,
2972 &acl_ids, mnt_ns);
2973 if (error)
2974 goto out;
2975 have_acl = B_TRUE;
2976 }
2977
2978 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, wo_projid)) {
2979 error = SET_ERROR(EDQUOT);
2980 goto out;
2981 }
34dc7c2f
BB
2982 }
2983
0037b49e 2984 tx = dmu_tx_create(zfsvfs->z_os);
428870ff
BB
2985 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
2986 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
dbf6108b
AS
2987 dmu_tx_hold_zap(tx, sdzp->z_id,
2988 (rflags & RENAME_EXCHANGE) ? TRUE : FALSE, snm);
34dc7c2f 2989 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
428870ff
BB
2990 if (sdzp != tdzp) {
2991 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
2992 zfs_sa_upgrade_txholds(tx, tdzp);
2993 }
2994 if (tzp) {
2995 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
2996 zfs_sa_upgrade_txholds(tx, tzp);
2997 }
dbf6108b
AS
2998 if (rflags & RENAME_WHITEOUT) {
2999 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3000 ZFS_SA_BASE_ATTR_SIZE);
428870ff 3001
dbf6108b
AS
3002 dmu_tx_hold_zap(tx, sdzp->z_id, TRUE, snm);
3003 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3004 if (!zfsvfs->z_use_sa &&
3005 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3006 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3007 0, acl_ids.z_aclp->z_acl_bytes);
3008 }
3009 }
3010 fuid_dirtied = zfsvfs->z_fuid_dirty;
3011 if (fuid_dirtied)
3012 zfs_fuid_txhold(zfsvfs, tx);
428870ff 3013 zfs_sa_upgrade_txholds(tx, szp);
0037b49e 3014 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
0735ecb3 3015 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
3016 if (error) {
3017 if (zl != NULL)
3018 zfs_rename_unlock(&zl);
3019 zfs_dirent_unlock(sdl);
3020 zfs_dirent_unlock(tdl);
428870ff
BB
3021
3022 if (sdzp == tdzp)
3023 rw_exit(&sdzp->z_name_lock);
3024
fb5f0bc8 3025 if (error == ERESTART) {
e8b96c60 3026 waited = B_TRUE;
34dc7c2f
BB
3027 dmu_tx_wait(tx);
3028 dmu_tx_abort(tx);
657ce253 3029 zrele(szp);
ea7e86d8 3030 if (tzp)
657ce253 3031 zrele(tzp);
34dc7c2f
BB
3032 goto top;
3033 }
3034 dmu_tx_abort(tx);
657ce253 3035 zrele(szp);
ea7e86d8 3036 if (tzp)
657ce253 3037 zrele(tzp);
768eaced 3038 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3039 return (error);
3040 }
3041
e015d6cc
AS
3042 /*
3043 * Unlink the source.
3044 */
3045 szp->z_pflags |= ZFS_AV_MODIFIED;
3046 if (tdzp->z_pflags & ZFS_PROJINHERIT)
3047 szp->z_pflags |= ZFS_PROJINHERIT;
34dc7c2f 3048
e015d6cc
AS
3049 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3050 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
dbf6108b 3051 VERIFY0(error);
34dc7c2f 3052
e015d6cc
AS
3053 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3054 if (error)
3055 goto commit;
3056
3057 /*
3058 * Unlink the target.
3059 */
3060 if (tzp) {
dbf6108b
AS
3061 int tzflg = zflg;
3062
3063 if (rflags & RENAME_EXCHANGE) {
3064 /* This inode will be re-linked soon. */
3065 tzflg |= ZRENAMING;
3066
3067 tzp->z_pflags |= ZFS_AV_MODIFIED;
3068 if (sdzp->z_pflags & ZFS_PROJINHERIT)
3069 tzp->z_pflags |= ZFS_PROJINHERIT;
3070
3071 error = sa_update(tzp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3072 (void *)&tzp->z_pflags, sizeof (uint64_t), tx);
3073 ASSERT0(error);
3074 }
3075 error = zfs_link_destroy(tdl, tzp, tx, tzflg, NULL);
e015d6cc
AS
3076 if (error)
3077 goto commit_link_szp;
3078 }
3079
3080 /*
dbf6108b
AS
3081 * Create the new target links:
3082 * * We always link the target.
3083 * * RENAME_EXCHANGE: Link the old target to the source.
3084 * * RENAME_WHITEOUT: Create a whiteout inode in-place of the source.
e015d6cc
AS
3085 */
3086 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3087 if (error) {
3088 /*
3089 * If we have removed the existing target, a subsequent call to
3090 * zfs_link_create() to add back the same entry, but with a new
3091 * dnode (szp), should not fail.
3092 */
3093 ASSERT3P(tzp, ==, NULL);
3094 goto commit_link_tzp;
34dc7c2f
BB
3095 }
3096
dbf6108b
AS
3097 switch (rflags & (RENAME_EXCHANGE | RENAME_WHITEOUT)) {
3098 case RENAME_EXCHANGE:
3099 error = zfs_link_create(sdl, tzp, tx, ZRENAMING);
3100 /*
3101 * The same argument as zfs_link_create() failing for
3102 * szp applies here, since the source directory must
3103 * have had an entry we are replacing.
3104 */
3105 ASSERT0(error);
3106 if (error)
3107 goto commit_unlink_td_szp;
3108 break;
3109 case RENAME_WHITEOUT:
3110 zfs_mknode(sdzp, wo_vap, tx, cr, 0, &wzp, &acl_ids);
3111 error = zfs_link_create(sdl, wzp, tx, ZNEW);
3112 if (error) {
3113 zfs_znode_delete(wzp, tx);
3114 remove_inode_hash(ZTOI(wzp));
3115 goto commit_unlink_td_szp;
3116 }
3117 break;
3118 }
3119
3120 if (fuid_dirtied)
3121 zfs_fuid_sync(zfsvfs, tx);
3122
3123 switch (rflags & (RENAME_EXCHANGE | RENAME_WHITEOUT)) {
3124 case RENAME_EXCHANGE:
3125 zfs_log_rename_exchange(zilog, tx,
3126 (flags & FIGNORECASE ? TX_CI : 0), sdzp, sdl->dl_name,
3127 tdzp, tdl->dl_name, szp);
3128 break;
3129 case RENAME_WHITEOUT:
3130 zfs_log_rename_whiteout(zilog, tx,
3131 (flags & FIGNORECASE ? TX_CI : 0), sdzp, sdl->dl_name,
3132 tdzp, tdl->dl_name, szp, wzp);
3133 break;
3134 default:
3135 ASSERT0(rflags & ~RENAME_NOREPLACE);
3136 zfs_log_rename(zilog, tx, (flags & FIGNORECASE ? TX_CI : 0),
3137 sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3138 break;
3139 }
e015d6cc
AS
3140
3141commit:
34dc7c2f
BB
3142 dmu_tx_commit(tx);
3143out:
dbf6108b
AS
3144 if (have_acl)
3145 zfs_acl_ids_free(&acl_ids);
34dc7c2f 3146
fc273894 3147 zfs_znode_update_vfs(sdzp);
428870ff
BB
3148 if (sdzp == tdzp)
3149 rw_exit(&sdzp->z_name_lock);
3150
960e08fe 3151 if (sdzp != tdzp)
fc273894 3152 zfs_znode_update_vfs(tdzp);
428870ff 3153
fc273894 3154 zfs_znode_update_vfs(szp);
657ce253 3155 zrele(szp);
dbf6108b
AS
3156 if (wzp) {
3157 zfs_znode_update_vfs(wzp);
3158 zrele(wzp);
3159 }
960e08fe 3160 if (tzp) {
fc273894 3161 zfs_znode_update_vfs(tzp);
657ce253 3162 zrele(tzp);
960e08fe 3163 }
34dc7c2f 3164
dbf6108b
AS
3165 if (zl != NULL)
3166 zfs_rename_unlock(&zl);
3167
3168 zfs_dirent_unlock(sdl);
3169 zfs_dirent_unlock(tdl);
3170
0037b49e 3171 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3172 zil_commit(zilog, 0);
428870ff 3173
768eaced 3174 zfs_exit(zfsvfs, FTAG);
34dc7c2f 3175 return (error);
e015d6cc
AS
3176
3177 /*
3178 * Clean-up path for broken link state.
3179 *
3180 * At this point we are in a (very) bad state, so we need to do our
dbf6108b
AS
3181 * best to correct the state. In particular, all of the nlinks are
3182 * wrong because we were destroying and creating links with ZRENAMING.
3183 *
3184 * In some form, all of these operations have to resolve the state:
3185 *
3186 * * link_destroy() *must* succeed. Fortunately, this is very likely
3187 * since we only just created it.
e015d6cc 3188 *
dbf6108b
AS
3189 * * link_create()s are allowed to fail (though they shouldn't because
3190 * we only just unlinked them and are putting the entries back
3191 * during clean-up). But if they fail, we can just forcefully drop
3192 * the nlink value to (at the very least) avoid broken nlink values
3193 * -- though in the case of non-empty directories we will have to
3194 * panic (otherwise we'd have a leaked directory with a broken ..).
e015d6cc 3195 */
dbf6108b
AS
3196commit_unlink_td_szp:
3197 VERIFY0(zfs_link_destroy(tdl, szp, tx, ZRENAMING, NULL));
e015d6cc
AS
3198commit_link_tzp:
3199 if (tzp) {
3200 if (zfs_link_create(tdl, tzp, tx, ZRENAMING))
dbf6108b 3201 VERIFY0(zfs_drop_nlink(tzp, tx, NULL));
e015d6cc
AS
3202 }
3203commit_link_szp:
3204 if (zfs_link_create(sdl, szp, tx, ZRENAMING))
dbf6108b 3205 VERIFY0(zfs_drop_nlink(szp, tx, NULL));
e015d6cc 3206 goto commit;
34dc7c2f
BB
3207}
3208
3209/*
3210 * Insert the indicated symbolic reference entry into the directory.
3211 *
657ce253 3212 * IN: dzp - Directory to contain new symbolic link.
841a7a98 3213 * name - Name of directory entry in dip.
34dc7c2f 3214 * vap - Attributes of new entry.
841a7a98 3215 * link - Name for new symlink entry.
34dc7c2f 3216 * cr - credentials of caller.
34dc7c2f 3217 * flags - case flags
2a068a13 3218 * mnt_ns - user namespace of the mount
34dc7c2f 3219 *
657ce253 3220 * OUT: zpp - Znode for new symbolic link.
841a7a98 3221 *
d3cc8b15 3222 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
3223 *
3224 * Timestamps:
3558fd73 3225 * dip - ctime|mtime updated
34dc7c2f 3226 */
e5c39b95 3227int
657ce253 3228zfs_symlink(znode_t *dzp, char *name, vattr_t *vap, char *link,
d4dc53da 3229 znode_t **zpp, cred_t *cr, int flags, zidmap_t *mnt_ns)
34dc7c2f 3230{
657ce253 3231 znode_t *zp;
34dc7c2f
BB
3232 zfs_dirlock_t *dl;
3233 dmu_tx_t *tx;
657ce253 3234 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f 3235 zilog_t *zilog;
428870ff 3236 uint64_t len = strlen(link);
34dc7c2f
BB
3237 int error;
3238 int zflg = ZNEW;
9babb374
BB
3239 zfs_acl_ids_t acl_ids;
3240 boolean_t fuid_dirtied;
428870ff 3241 uint64_t txtype = TX_SYMLINK;
e8b96c60 3242 boolean_t waited = B_FALSE;
34dc7c2f 3243
3558fd73 3244 ASSERT(S_ISLNK(vap->va_mode));
34dc7c2f 3245
32dec7bd 3246 if (name == NULL)
3247 return (SET_ERROR(EINVAL));
3248
768eaced
CC
3249 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3250 return (error);
0037b49e 3251 zilog = zfsvfs->z_log;
34dc7c2f 3252
0037b49e 3253 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
34dc7c2f 3254 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 3255 zfs_exit(zfsvfs, FTAG);
2e528b49 3256 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
3257 }
3258 if (flags & FIGNORECASE)
3259 zflg |= ZCILOOK;
34dc7c2f
BB
3260
3261 if (len > MAXPATHLEN) {
768eaced 3262 zfs_exit(zfsvfs, FTAG);
2e528b49 3263 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
3264 }
3265
428870ff 3266 if ((error = zfs_acl_ids_create(dzp, 0,
2a068a13 3267 vap, cr, NULL, &acl_ids, mnt_ns)) != 0) {
768eaced 3268 zfs_exit(zfsvfs, FTAG);
428870ff
BB
3269 return (error);
3270 }
3271top:
657ce253 3272 *zpp = NULL;
3558fd73 3273
34dc7c2f
BB
3274 /*
3275 * Attempt to lock directory; fail if entry already exists.
3276 */
3277 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3278 if (error) {
428870ff 3279 zfs_acl_ids_free(&acl_ids);
768eaced 3280 zfs_exit(zfsvfs, FTAG);
428870ff
BB
3281 return (error);
3282 }
3283
2a068a13 3284 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
428870ff
BB
3285 zfs_acl_ids_free(&acl_ids);
3286 zfs_dirent_unlock(dl);
768eaced 3287 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3288 return (error);
3289 }
3290
9c5167d1 3291 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, ZFS_DEFAULT_PROJID)) {
9babb374
BB
3292 zfs_acl_ids_free(&acl_ids);
3293 zfs_dirent_unlock(dl);
768eaced 3294 zfs_exit(zfsvfs, FTAG);
2e528b49 3295 return (SET_ERROR(EDQUOT));
9babb374 3296 }
0037b49e
BB
3297 tx = dmu_tx_create(zfsvfs->z_os);
3298 fuid_dirtied = zfsvfs->z_fuid_dirty;
34dc7c2f 3299 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
34dc7c2f 3300 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
428870ff
BB
3301 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3302 ZFS_SA_BASE_ATTR_SIZE + len);
3303 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
0037b49e 3304 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
428870ff
BB
3305 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3306 acl_ids.z_aclp->z_acl_bytes);
3307 }
9babb374 3308 if (fuid_dirtied)
0037b49e 3309 zfs_fuid_txhold(zfsvfs, tx);
0735ecb3 3310 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
3311 if (error) {
3312 zfs_dirent_unlock(dl);
fb5f0bc8 3313 if (error == ERESTART) {
e8b96c60 3314 waited = B_TRUE;
34dc7c2f
BB
3315 dmu_tx_wait(tx);
3316 dmu_tx_abort(tx);
3317 goto top;
3318 }
428870ff 3319 zfs_acl_ids_free(&acl_ids);
34dc7c2f 3320 dmu_tx_abort(tx);
768eaced 3321 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3322 return (error);
3323 }
3324
34dc7c2f
BB
3325 /*
3326 * Create a new object for the symlink.
bf169e9f 3327 * for version 4 ZPL datasets the symlink will be an SA attribute
34dc7c2f 3328 */
428870ff 3329 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
9babb374 3330
428870ff 3331 if (fuid_dirtied)
0037b49e 3332 zfs_fuid_sync(zfsvfs, tx);
34dc7c2f 3333
572e2857 3334 mutex_enter(&zp->z_lock);
428870ff 3335 if (zp->z_is_sa)
0037b49e 3336 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
428870ff
BB
3337 link, len, tx);
3338 else
3339 zfs_sa_symlink(zp, link, len, tx);
572e2857 3340 mutex_exit(&zp->z_lock);
34dc7c2f 3341
428870ff 3342 zp->z_size = len;
0037b49e 3343 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
428870ff 3344 &zp->z_size, sizeof (zp->z_size), tx);
34dc7c2f
BB
3345 /*
3346 * Insert the new object into the directory.
3347 */
599b8648
CC
3348 error = zfs_link_create(dl, zp, tx, ZNEW);
3349 if (error != 0) {
3350 zfs_znode_delete(zp, tx);
3351 remove_inode_hash(ZTOI(zp));
3352 } else {
3353 if (flags & FIGNORECASE)
3354 txtype |= TX_CI;
3355 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4f301661 3356
fc273894
KHN
3357 zfs_znode_update_vfs(dzp);
3358 zfs_znode_update_vfs(zp);
599b8648 3359 }
960e08fe 3360
9babb374 3361 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
3362
3363 dmu_tx_commit(tx);
3364
3365 zfs_dirent_unlock(dl);
3366
599b8648 3367 if (error == 0) {
657ce253 3368 *zpp = zp;
34dc7c2f 3369
599b8648
CC
3370 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3371 zil_commit(zilog, 0);
3372 } else {
657ce253 3373 zrele(zp);
599b8648 3374 }
428870ff 3375
768eaced 3376 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3377 return (error);
3378}
3379
3380/*
3381 * Return, in the buffer contained in the provided uio structure,
3558fd73 3382 * the symbolic path referred to by ip.
34dc7c2f 3383 *
8b4f9a2d
BB
3384 * IN: ip - inode of symbolic link
3385 * uio - structure to contain the link path.
3386 * cr - credentials of caller.
34dc7c2f
BB
3387 *
3388 * RETURN: 0 if success
3389 * error code if failure
3390 *
3391 * Timestamps:
3558fd73 3392 * ip - atime updated
34dc7c2f 3393 */
e5c39b95 3394int
d0cd9a5c 3395zfs_readlink(struct inode *ip, zfs_uio_t *uio, cred_t *cr)
34dc7c2f 3396{
ef70eff1 3397 (void) cr;
3558fd73 3398 znode_t *zp = ITOZ(ip);
0037b49e 3399 zfsvfs_t *zfsvfs = ITOZSB(ip);
34dc7c2f
BB
3400 int error;
3401
768eaced
CC
3402 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3403 return (error);
34dc7c2f 3404
572e2857 3405 mutex_enter(&zp->z_lock);
428870ff 3406 if (zp->z_is_sa)
8b4f9a2d 3407 error = sa_lookup_uio(zp->z_sa_hdl,
0037b49e 3408 SA_ZPL_SYMLINK(zfsvfs), uio);
428870ff 3409 else
8b4f9a2d 3410 error = zfs_sa_readlink(zp, uio);
572e2857 3411 mutex_exit(&zp->z_lock);
34dc7c2f 3412
768eaced 3413 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3414 return (error);
3415}
3416
3417/*
657ce253 3418 * Insert a new entry into directory tdzp referencing szp.
34dc7c2f 3419 *
657ce253
MM
3420 * IN: tdzp - Directory to contain new entry.
3421 * szp - znode of new entry.
34dc7c2f
BB
3422 * name - name of new entry.
3423 * cr - credentials of caller.
841a7a98 3424 * flags - case flags.
34dc7c2f
BB
3425 *
3426 * RETURN: 0 if success
3427 * error code if failure
3428 *
3429 * Timestamps:
657ce253
MM
3430 * tdzp - ctime|mtime updated
3431 * szp - ctime updated
34dc7c2f 3432 */
e5c39b95 3433int
657ce253 3434zfs_link(znode_t *tdzp, znode_t *szp, char *name, cred_t *cr,
da5e151f 3435 int flags)
34dc7c2f 3436{
657ce253
MM
3437 struct inode *sip = ZTOI(szp);
3438 znode_t *tzp;
3439 zfsvfs_t *zfsvfs = ZTOZSB(tdzp);
34dc7c2f
BB
3440 zilog_t *zilog;
3441 zfs_dirlock_t *dl;
3442 dmu_tx_t *tx;
34dc7c2f
BB
3443 int error;
3444 int zf = ZNEW;
428870ff 3445 uint64_t parent;
572e2857 3446 uid_t owner;
e8b96c60 3447 boolean_t waited = B_FALSE;
ace1eae8
CC
3448 boolean_t is_tmpfile = 0;
3449 uint64_t txg;
3450#ifdef HAVE_TMPFILE
3451 is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE));
3452#endif
657ce253 3453 ASSERT(S_ISDIR(ZTOI(tdzp)->i_mode));
34dc7c2f 3454
32dec7bd 3455 if (name == NULL)
3456 return (SET_ERROR(EINVAL));
3457
768eaced
CC
3458 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3459 return (error);
0037b49e 3460 zilog = zfsvfs->z_log;
34dc7c2f 3461
428870ff
BB
3462 /*
3463 * POSIX dictates that we return EPERM here.
3464 * Better choices include ENOTSUP or EISDIR.
3465 */
3558fd73 3466 if (S_ISDIR(sip->i_mode)) {
768eaced 3467 zfs_exit(zfsvfs, FTAG);
2e528b49 3468 return (SET_ERROR(EPERM));
428870ff
BB
3469 }
3470
768eaced
CC
3471 if ((error = zfs_verify_zp(szp)) != 0) {
3472 zfs_exit(zfsvfs, FTAG);
3473 return (error);
3474 }
812e91a7 3475
9c5167d1
NF
3476 /*
3477 * If we are using project inheritance, means if the directory has
3478 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3479 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3480 * such case, we only allow hard link creation in our tree when the
3481 * project IDs are the same.
3482 */
657ce253
MM
3483 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3484 tdzp->z_projid != szp->z_projid) {
768eaced 3485 zfs_exit(zfsvfs, FTAG);
9c5167d1
NF
3486 return (SET_ERROR(EXDEV));
3487 }
3488
812e91a7
MT
3489 /*
3490 * We check i_sb because snapshots and the ctldir must have different
3491 * super blocks.
3492 */
657ce253 3493 if (sip->i_sb != ZTOI(tdzp)->i_sb || zfsctl_is_node(sip)) {
768eaced 3494 zfs_exit(zfsvfs, FTAG);
2e528b49 3495 return (SET_ERROR(EXDEV));
34dc7c2f 3496 }
428870ff 3497
428870ff
BB
3498 /* Prevent links to .zfs/shares files */
3499
0037b49e 3500 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
428870ff 3501 &parent, sizeof (uint64_t))) != 0) {
768eaced 3502 zfs_exit(zfsvfs, FTAG);
428870ff
BB
3503 return (error);
3504 }
0037b49e 3505 if (parent == zfsvfs->z_shares_dir) {
768eaced 3506 zfs_exit(zfsvfs, FTAG);
2e528b49 3507 return (SET_ERROR(EPERM));
428870ff
BB
3508 }
3509
0037b49e 3510 if (zfsvfs->z_utf8 && u8_validate(name,
34dc7c2f 3511 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
768eaced 3512 zfs_exit(zfsvfs, FTAG);
2e528b49 3513 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
3514 }
3515 if (flags & FIGNORECASE)
3516 zf |= ZCILOOK;
3517
34dc7c2f
BB
3518 /*
3519 * We do not support links between attributes and non-attributes
3520 * because of the potential security risk of creating links
3521 * into "normal" file space in order to circumvent restrictions
3522 * imposed in attribute space.
3523 */
657ce253 3524 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
768eaced 3525 zfs_exit(zfsvfs, FTAG);
2e528b49 3526 return (SET_ERROR(EINVAL));
34dc7c2f
BB
3527 }
3528
0037b49e
BB
3529 owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid),
3530 cr, ZFS_OWNER);
572e2857 3531 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
768eaced 3532 zfs_exit(zfsvfs, FTAG);
2e528b49 3533 return (SET_ERROR(EPERM));
34dc7c2f
BB
3534 }
3535
f224eddf 3536 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr,
d4dc53da 3537 zfs_init_idmap))) {
768eaced 3538 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3539 return (error);
3540 }
3541
428870ff 3542top:
34dc7c2f
BB
3543 /*
3544 * Attempt to lock directory; fail if entry already exists.
3545 */
657ce253 3546 error = zfs_dirent_lock(&dl, tdzp, name, &tzp, zf, NULL, NULL);
34dc7c2f 3547 if (error) {
768eaced 3548 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3549 return (error);
3550 }
3551
0037b49e 3552 tx = dmu_tx_create(zfsvfs->z_os);
428870ff 3553 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
657ce253 3554 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
ace1eae8 3555 if (is_tmpfile)
0037b49e 3556 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
ace1eae8 3557
428870ff 3558 zfs_sa_upgrade_txholds(tx, szp);
657ce253 3559 zfs_sa_upgrade_txholds(tx, tdzp);
0735ecb3 3560 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
34dc7c2f
BB
3561 if (error) {
3562 zfs_dirent_unlock(dl);
fb5f0bc8 3563 if (error == ERESTART) {
e8b96c60 3564 waited = B_TRUE;
34dc7c2f
BB
3565 dmu_tx_wait(tx);
3566 dmu_tx_abort(tx);
3567 goto top;
3568 }
3569 dmu_tx_abort(tx);
768eaced 3570 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3571 return (error);
3572 }
ace1eae8
CC
3573 /* unmark z_unlinked so zfs_link_create will not reject */
3574 if (is_tmpfile)
a43570c5 3575 szp->z_unlinked = B_FALSE;
34dc7c2f
BB
3576 error = zfs_link_create(dl, szp, tx, 0);
3577
3578 if (error == 0) {
3579 uint64_t txtype = TX_LINK;
ace1eae8
CC
3580 /*
3581 * tmpfile is created to be in z_unlinkedobj, so remove it.
e1cfd73f 3582 * Also, we don't log in ZIL, because all previous file
ace1eae8
CC
3583 * operation on the tmpfile are ignored by ZIL. Instead we
3584 * always wait for txg to sync to make sure all previous
3585 * operation are sync safe.
3586 */
3587 if (is_tmpfile) {
0037b49e
BB
3588 VERIFY(zap_remove_int(zfsvfs->z_os,
3589 zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0);
ace1eae8
CC
3590 } else {
3591 if (flags & FIGNORECASE)
3592 txtype |= TX_CI;
657ce253 3593 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
ace1eae8
CC
3594 }
3595 } else if (is_tmpfile) {
3596 /* restore z_unlinked since when linking failed */
a43570c5 3597 szp->z_unlinked = B_TRUE;
34dc7c2f 3598 }
ace1eae8 3599 txg = dmu_tx_get_txg(tx);
34dc7c2f
BB
3600 dmu_tx_commit(tx);
3601
3602 zfs_dirent_unlock(dl);
3603
0037b49e 3604 if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3605 zil_commit(zilog, 0);
428870ff 3606
d09dc598 3607 if (is_tmpfile && zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED)
0037b49e 3608 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg);
ace1eae8 3609
fc273894
KHN
3610 zfs_znode_update_vfs(tdzp);
3611 zfs_znode_update_vfs(szp);
768eaced 3612 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
3613 return (error);
3614}
3615
3c0e5c0f 3616static void
411f4a01 3617zfs_putpage_sync_commit_cb(void *arg)
3c0e5c0f
BB
3618{
3619 struct page *pp = arg;
3620
119a394a 3621 ClearPageError(pp);
3c0e5c0f
BB
3622 end_page_writeback(pp);
3623}
3624
411f4a01
SN
3625static void
3626zfs_putpage_async_commit_cb(void *arg)
3627{
3628 struct page *pp = arg;
3629 znode_t *zp = ITOZ(pp->mapping->host);
3630
3631 ClearPageError(pp);
3632 end_page_writeback(pp);
3633 atomic_dec_32(&zp->z_async_writes_cnt);
3634}
3635
34dc7c2f 3636/*
3c0e5c0f
BB
3637 * Push a page out to disk, once the page is on stable storage the
3638 * registered commit callback will be run as notification of completion.
34dc7c2f 3639 *
411f4a01
SN
3640 * IN: ip - page mapped for inode.
3641 * pp - page to push (page is locked)
3642 * wbc - writeback control data
3643 * for_sync - does the caller intend to wait synchronously for the
3644 * page writeback to complete?
34dc7c2f
BB
3645 *
3646 * RETURN: 0 if success
3647 * error code if failure
3648 *
3c0e5c0f
BB
3649 * Timestamps:
3650 * ip - ctime|mtime updated
34dc7c2f 3651 */
3c0e5c0f 3652int
411f4a01
SN
3653zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc,
3654 boolean_t for_sync)
34dc7c2f 3655{
3c0e5c0f 3656 znode_t *zp = ITOZ(ip);
0037b49e 3657 zfsvfs_t *zfsvfs = ITOZSB(ip);
3c0e5c0f
BB
3658 loff_t offset;
3659 loff_t pgoff;
4c837f0d 3660 unsigned int pglen;
3c0e5c0f
BB
3661 dmu_tx_t *tx;
3662 caddr_t va;
3663 int err = 0;
3664 uint64_t mtime[2], ctime[2];
db4fc559 3665 inode_timespec_t tmp_ts;
3c0e5c0f
BB
3666 sa_bulk_attr_t bulk[3];
3667 int cnt = 0;
21a96fb6 3668 struct address_space *mapping;
3c0e5c0f 3669
768eaced
CC
3670 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3671 return (err);
d164b209 3672
3c0e5c0f
BB
3673 ASSERT(PageLocked(pp));
3674
d1d7e268
MK
3675 pgoff = page_offset(pp); /* Page byte-offset in file */
3676 offset = i_size_read(ip); /* File length in bytes */
8b1899d3
BB
3677 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
3678 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
3c0e5c0f
BB
3679
3680 /* Page is beyond end of file */
3681 if (pgoff >= offset) {
3682 unlock_page(pp);
768eaced 3683 zfs_exit(zfsvfs, FTAG);
3c0e5c0f
BB
3684 return (0);
3685 }
3686
3687 /* Truncate page length to end of file */
3688 if (pgoff + pglen > offset)
3689 pglen = offset - pgoff;
3690
3691#if 0
34dc7c2f 3692 /*
3c0e5c0f
BB
3693 * FIXME: Allow mmap writes past its quota. The correct fix
3694 * is to register a page_mkwrite() handler to count the page
3695 * against its quota when it is about to be dirtied.
34dc7c2f 3696 */
9c5167d1
NF
3697 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
3698 KUID_TO_SUID(ip->i_uid)) ||
3699 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
3700 KGID_TO_SGID(ip->i_gid)) ||
3701 (zp->z_projid != ZFS_DEFAULT_PROJID &&
3702 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
3703 zp->z_projid))) {
9babb374 3704 err = EDQUOT;
9babb374 3705 }
3c0e5c0f
BB
3706#endif
3707
d958324f
BB
3708 /*
3709 * The ordering here is critical and must adhere to the following
3710 * rules in order to avoid deadlocking in either zfs_read() or
3711 * zfs_free_range() due to a lock inversion.
3712 *
3713 * 1) The page must be unlocked prior to acquiring the range lock.
3714 * This is critical because zfs_read() calls find_lock_page()
3715 * which may block on the page lock while holding the range lock.
3716 *
3717 * 2) Before setting or clearing write back on a page the range lock
3718 * must be held in order to prevent a lock inversion with the
3719 * zfs_free_range() function.
21a96fb6
CC
3720 *
3721 * This presents a problem because upon entering this function the
3722 * page lock is already held. To safely acquire the range lock the
3723 * page lock must be dropped. This creates a window where another
3724 * process could truncate, invalidate, dirty, or write out the page.
3725 *
3726 * Therefore, after successfully reacquiring the range and page locks
3727 * the current page state is checked. In the common case everything
3728 * will be as is expected and it can be written out. However, if
3729 * the page state has changed it must be handled accordingly.
d958324f 3730 */
21a96fb6
CC
3731 mapping = pp->mapping;
3732 redirty_page_for_writepage(wbc, pp);
d958324f 3733 unlock_page(pp);
21a96fb6 3734
bd4dde8e 3735 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock,
5d43cc9a 3736 pgoff, pglen, RL_WRITER);
21a96fb6
CC
3737 lock_page(pp);
3738
3739 /* Page mapping changed or it was no longer dirty, we're done */
3740 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
3741 unlock_page(pp);
2cc479d0 3742 zfs_rangelock_exit(lr);
768eaced 3743 zfs_exit(zfsvfs, FTAG);
21a96fb6
CC
3744 return (0);
3745 }
3746
3747 /* Another process started write block if required */
3748 if (PageWriteback(pp)) {
3749 unlock_page(pp);
2cc479d0 3750 zfs_rangelock_exit(lr);
21a96fb6 3751
bfd5a709 3752 if (wbc->sync_mode != WB_SYNC_NONE) {
411f4a01
SN
3753 /*
3754 * Speed up any non-sync page writebacks since
3755 * they may take several seconds to complete.
3756 * Refer to the comment in zpl_fsync() (when
3757 * HAVE_FSYNC_RANGE is defined) for details.
3758 */
3759 if (atomic_load_32(&zp->z_async_writes_cnt) > 0) {
3760 zil_commit(zfsvfs->z_log, zp->z_id);
3761 }
3762
bfd5a709 3763 if (PageWriteback(pp))
c0fb44c5
CK
3764#ifdef HAVE_PAGEMAP_FOLIO_WAIT_BIT
3765 folio_wait_bit(page_folio(pp), PG_writeback);
3766#else
bfd5a709 3767 wait_on_page_bit(pp, PG_writeback);
c0fb44c5 3768#endif
bfd5a709 3769 }
21a96fb6 3770
768eaced 3771 zfs_exit(zfsvfs, FTAG);
21a96fb6
CC
3772 return (0);
3773 }
3774
3775 /* Clear the dirty flag the required locks are held */
3776 if (!clear_page_dirty_for_io(pp)) {
3777 unlock_page(pp);
2cc479d0 3778 zfs_rangelock_exit(lr);
768eaced 3779 zfs_exit(zfsvfs, FTAG);
21a96fb6
CC
3780 return (0);
3781 }
3782
3783 /*
3784 * Counterpart for redirty_page_for_writepage() above. This page
3785 * was in fact not skipped and should not be counted as if it were.
3786 */
3787 wbc->pages_skipped--;
411f4a01
SN
3788 if (!for_sync)
3789 atomic_inc_32(&zp->z_async_writes_cnt);
3c0e5c0f 3790 set_page_writeback(pp);
21a96fb6 3791 unlock_page(pp);
3c0e5c0f 3792
0037b49e 3793 tx = dmu_tx_create(zfsvfs->z_os);
3c0e5c0f 3794 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
428870ff
BB
3795 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3796 zfs_sa_upgrade_txholds(tx, zp);
d958324f 3797
102b468b 3798 err = dmu_tx_assign(tx, TXG_WAIT);
34dc7c2f 3799 if (err != 0) {
34dc7c2f 3800 dmu_tx_abort(tx);
7dde17e8
SP
3801#ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
3802 filemap_dirty_folio(page_mapping(pp), page_folio(pp));
3803#else
119a394a 3804 __set_page_dirty_nobuffers(pp);
7dde17e8 3805#endif
119a394a
ED
3806 ClearPageError(pp);
3807 end_page_writeback(pp);
411f4a01
SN
3808 if (!for_sync)
3809 atomic_dec_32(&zp->z_async_writes_cnt);
2cc479d0 3810 zfs_rangelock_exit(lr);
768eaced 3811 zfs_exit(zfsvfs, FTAG);
3c0e5c0f 3812 return (err);
34dc7c2f
BB
3813 }
3814
dde471ef 3815 va = kmap(pp);
8b1899d3 3816 ASSERT3U(pglen, <=, PAGE_SIZE);
0037b49e 3817 dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx);
dde471ef 3818 kunmap(pp);
34dc7c2f 3819
0037b49e
BB
3820 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
3821 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
3822 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL,
3823 &zp->z_pflags, 8);
428870ff 3824
d3aa3ea9 3825 /* Preserve the mtime and ctime provided by the inode */
db4fc559
RN
3826 tmp_ts = zpl_inode_get_mtime(ip);
3827 ZFS_TIME_ENCODE(&tmp_ts, mtime);
3828 tmp_ts = zpl_inode_get_ctime(ip);
3829 ZFS_TIME_ENCODE(&tmp_ts, ctime);
a43570c5 3830 zp->z_atime_dirty = B_FALSE;
d3aa3ea9
BB
3831 zp->z_seq++;
3832
3833 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3834
c3773de1 3835 boolean_t commit = B_FALSE;
119a394a
ED
3836 if (wbc->sync_mode != WB_SYNC_NONE) {
3837 /*
3838 * Note that this is rarely called under writepages(), because
3839 * writepages() normally handles the entire commit for
3840 * performance reasons.
3841 */
c3773de1 3842 commit = B_TRUE;
411f4a01
SN
3843 } else if (!for_sync && atomic_load_32(&zp->z_sync_writes_cnt) > 0) {
3844 /*
3845 * If the caller does not intend to wait synchronously
3846 * for this page writeback to complete and there are active
3847 * synchronous calls on this file, do a commit so that
3848 * the latter don't accidentally end up waiting for
3849 * our writeback to complete. Refer to the comment in
3850 * zpl_fsync() (when HAVE_FSYNC_RANGE is defined) for details.
3851 */
c3773de1 3852 commit = B_TRUE;
2b286136 3853 }
3c0e5c0f 3854
c3773de1
AM
3855 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, commit,
3856 for_sync ? zfs_putpage_sync_commit_cb :
3857 zfs_putpage_async_commit_cb, pp);
3858
3859 dmu_tx_commit(tx);
3860
3861 zfs_rangelock_exit(lr);
3862
3863 if (commit)
3864 zil_commit(zfsvfs->z_log, zp->z_id);
3865
3819aaaf
MB
3866 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, pglen);
3867
768eaced 3868 zfs_exit(zfsvfs, FTAG);
3c0e5c0f 3869 return (err);
34dc7c2f
BB
3870}
3871
8780c539
BB
3872/*
3873 * Update the system attributes when the inode has been dirtied. For the
023699cd 3874 * moment we only update the mode, atime, mtime, and ctime.
8780c539
BB
3875 */
3876int
3877zfs_dirty_inode(struct inode *ip, int flags)
3878{
3879 znode_t *zp = ITOZ(ip);
0037b49e 3880 zfsvfs_t *zfsvfs = ITOZSB(ip);
8780c539 3881 dmu_tx_t *tx;
023699cd 3882 uint64_t mode, atime[2], mtime[2], ctime[2];
db4fc559 3883 inode_timespec_t tmp_ts;
023699cd 3884 sa_bulk_attr_t bulk[4];
704cd075 3885 int error = 0;
8780c539
BB
3886 int cnt = 0;
3887
0037b49e 3888 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
c944be5d
BB
3889 return (0);
3890
768eaced
CC
3891 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3892 return (error);
8780c539 3893
704cd075
CC
3894#ifdef I_DIRTY_TIME
3895 /*
e1cfd73f 3896 * This is the lazytime semantic introduced in Linux 4.0
704cd075
CC
3897 * This flag will only be called from update_time when lazytime is set.
3898 * (Note, I_DIRTY_SYNC will also set if not lazytime)
3899 * Fortunately mtime and ctime are managed within ZFS itself, so we
3900 * only need to dirty atime.
3901 */
3902 if (flags == I_DIRTY_TIME) {
a43570c5 3903 zp->z_atime_dirty = B_TRUE;
704cd075
CC
3904 goto out;
3905 }
3906#endif
3907
0037b49e 3908 tx = dmu_tx_create(zfsvfs->z_os);
8780c539
BB
3909
3910 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3911 zfs_sa_upgrade_txholds(tx, zp);
3912
3913 error = dmu_tx_assign(tx, TXG_WAIT);
3914 if (error) {
3915 dmu_tx_abort(tx);
3916 goto out;
3917 }
3918
3919 mutex_enter(&zp->z_lock);
a43570c5 3920 zp->z_atime_dirty = B_FALSE;
704cd075 3921
0037b49e
BB
3922 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
3923 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
3924 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
3925 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
8780c539 3926
023699cd 3927 /* Preserve the mode, mtime and ctime provided by the inode */
db4fc559
RN
3928 tmp_ts = zpl_inode_get_atime(ip);
3929 ZFS_TIME_ENCODE(&tmp_ts, atime);
3930 tmp_ts = zpl_inode_get_mtime(ip);
3931 ZFS_TIME_ENCODE(&tmp_ts, mtime);
3932 tmp_ts = zpl_inode_get_ctime(ip);
3933 ZFS_TIME_ENCODE(&tmp_ts, ctime);
023699cd
MM
3934 mode = ip->i_mode;
3935
3936 zp->z_mode = mode;
8780c539
BB
3937
3938 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3939 mutex_exit(&zp->z_lock);
3940
3941 dmu_tx_commit(tx);
3942out:
768eaced 3943 zfs_exit(zfsvfs, FTAG);
8780c539
BB
3944 return (error);
3945}
8780c539 3946
34dc7c2f 3947void
c0d35759 3948zfs_inactive(struct inode *ip)
34dc7c2f 3949{
c0d35759 3950 znode_t *zp = ITOZ(ip);
0037b49e 3951 zfsvfs_t *zfsvfs = ITOZSB(ip);
0df9673f 3952 uint64_t atime[2];
34dc7c2f 3953 int error;
cafbd2ac 3954 int need_unlock = 0;
34dc7c2f 3955
cafbd2ac 3956 /* Only read lock if we haven't already write locked, e.g. rollback */
0037b49e 3957 if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) {
cafbd2ac 3958 need_unlock = 1;
0037b49e 3959 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
cafbd2ac 3960 }
c0d35759 3961 if (zp->z_sa_hdl == NULL) {
cafbd2ac 3962 if (need_unlock)
0037b49e 3963 rw_exit(&zfsvfs->z_teardown_inactive_lock);
c0d35759 3964 return;
34dc7c2f
BB
3965 }
3966
a43570c5 3967 if (zp->z_atime_dirty && zp->z_unlinked == B_FALSE) {
0037b49e 3968 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
34dc7c2f 3969
428870ff
BB
3970 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3971 zfs_sa_upgrade_txholds(tx, zp);
34dc7c2f
BB
3972 error = dmu_tx_assign(tx, TXG_WAIT);
3973 if (error) {
3974 dmu_tx_abort(tx);
3975 } else {
db4fc559
RN
3976 inode_timespec_t tmp_atime;
3977 tmp_atime = zpl_inode_get_atime(ip);
3978 ZFS_TIME_ENCODE(&tmp_atime, atime);
34dc7c2f 3979 mutex_enter(&zp->z_lock);
0037b49e 3980 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
0df9673f 3981 (void *)&atime, sizeof (atime), tx);
a43570c5 3982 zp->z_atime_dirty = B_FALSE;
34dc7c2f
BB
3983 mutex_exit(&zp->z_lock);
3984 dmu_tx_commit(tx);
3985 }
3986 }
3987
3988 zfs_zinactive(zp);
cafbd2ac 3989 if (need_unlock)
0037b49e 3990 rw_exit(&zfsvfs->z_teardown_inactive_lock);
34dc7c2f
BB
3991}
3992
34dc7c2f 3993/*
dde471ef 3994 * Fill pages with data from the disk.
34dc7c2f
BB
3995 */
3996static int
89cd2197 3997zfs_fillpage(struct inode *ip, struct page *pp)
34dc7c2f 3998{
0037b49e 3999 zfsvfs_t *zfsvfs = ITOZSB(ip);
89cd2197
BB
4000 loff_t i_size = i_size_read(ip);
4001 u_offset_t io_off = page_offset(pp);
4002 size_t io_len = PAGE_SIZE;
dde471ef 4003
dd108f5d
RY
4004 ASSERT3U(io_off, <, i_size);
4005
dde471ef
PJ
4006 if (io_off + io_len > i_size)
4007 io_len = i_size - io_off;
34dc7c2f 4008
89cd2197
BB
4009 void *va = kmap(pp);
4010 int error = dmu_read(zfsvfs->z_os, ITOZ(ip)->z_id, io_off,
dd108f5d
RY
4011 io_len, va, DMU_READ_PREFETCH);
4012 if (io_len != PAGE_SIZE)
4013 memset((char *)va + io_len, 0, PAGE_SIZE - io_len);
89cd2197 4014 kunmap(pp);
d164b209 4015
89cd2197
BB
4016 if (error) {
4017 /* convert checksum errors into IO errors */
4018 if (error == ECKSUM)
4019 error = SET_ERROR(EIO);
4020
4021 SetPageError(pp);
4022 ClearPageUptodate(pp);
4023 } else {
4024 ClearPageError(pp);
4025 SetPageUptodate(pp);
34dc7c2f 4026 }
d164b209 4027
89cd2197 4028 return (error);
34dc7c2f
BB
4029}
4030
4031/*
89cd2197 4032 * Uses zfs_fillpage to read data from the file and fill the page.
34dc7c2f 4033 *
dde471ef 4034 * IN: ip - inode of file to get data from.
89cd2197 4035 * pp - page to read
34dc7c2f 4036 *
d3cc8b15 4037 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
4038 *
4039 * Timestamps:
4040 * vp - atime updated
4041 */
dde471ef 4042int
89cd2197 4043zfs_getpage(struct inode *ip, struct page *pp)
34dc7c2f 4044{
0037b49e 4045 zfsvfs_t *zfsvfs = ITOZSB(ip);
89cd2197
BB
4046 znode_t *zp = ITOZ(ip);
4047 int error;
34dc7c2f 4048
89cd2197
BB
4049 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4050 return (error);
34dc7c2f 4051
89cd2197
BB
4052 error = zfs_fillpage(ip, pp);
4053 if (error == 0)
4054 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, PAGE_SIZE);
3819aaaf 4055
768eaced 4056 zfs_exit(zfsvfs, FTAG);
89cd2197
BB
4057
4058 return (error);
34dc7c2f
BB
4059}
4060
4061/*
e2e7aa2d 4062 * Check ZFS specific permissions to memory map a section of a file.
34dc7c2f 4063 *
e2e7aa2d
BB
4064 * IN: ip - inode of the file to mmap
4065 * off - file offset
4066 * addrp - start address in memory region
4067 * len - length of memory region
4068 * vm_flags- address flags
34dc7c2f 4069 *
e2e7aa2d
BB
4070 * RETURN: 0 if success
4071 * error code if failure
34dc7c2f 4072 */
e2e7aa2d
BB
4073int
4074zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4075 unsigned long vm_flags)
34dc7c2f 4076{
ef70eff1 4077 (void) addrp;
e2e7aa2d 4078 znode_t *zp = ITOZ(ip);
0037b49e 4079 zfsvfs_t *zfsvfs = ITOZSB(ip);
768eaced 4080 int error;
34dc7c2f 4081
768eaced
CC
4082 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4083 return (error);
34dc7c2f 4084
a160c153
W
4085 if ((vm_flags & VM_WRITE) && (vm_flags & VM_SHARED) &&
4086 (zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
768eaced 4087 zfs_exit(zfsvfs, FTAG);
2e528b49 4088 return (SET_ERROR(EPERM));
34dc7c2f
BB
4089 }
4090
e2e7aa2d 4091 if ((vm_flags & (VM_READ | VM_EXEC)) &&
428870ff 4092 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
768eaced 4093 zfs_exit(zfsvfs, FTAG);
2e528b49 4094 return (SET_ERROR(EACCES));
34dc7c2f
BB
4095 }
4096
34dc7c2f 4097 if (off < 0 || len > MAXOFFSET_T - off) {
768eaced 4098 zfs_exit(zfsvfs, FTAG);
2e528b49 4099 return (SET_ERROR(ENXIO));
34dc7c2f
BB
4100 }
4101
768eaced 4102 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
4103 return (0);
4104}
4105
4106/*
4107 * Free or allocate space in a file. Currently, this function only
4108 * supports the `F_FREESP' command. However, this command is somewhat
4109 * misnamed, as its functionality includes the ability to allocate as
4110 * well as free space.
4111 *
657ce253 4112 * IN: zp - znode of file to free data in.
34dc7c2f
BB
4113 * cmd - action to take (only F_FREESP supported).
4114 * bfp - section of file to free/alloc.
4115 * flag - current file open mode flags.
4116 * offset - current file offset.
a35c1207 4117 * cr - credentials of caller.
34dc7c2f 4118 *
d3cc8b15 4119 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
4120 *
4121 * Timestamps:
657ce253 4122 * zp - ctime|mtime updated
34dc7c2f 4123 */
e5c39b95 4124int
657ce253 4125zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3558fd73 4126 offset_t offset, cred_t *cr)
34dc7c2f 4127{
ef70eff1 4128 (void) offset;
657ce253 4129 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f
BB
4130 uint64_t off, len;
4131 int error;
4132
768eaced
CC
4133 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4134 return (error);
34dc7c2f 4135
34dc7c2f 4136 if (cmd != F_FREESP) {
768eaced 4137 zfs_exit(zfsvfs, FTAG);
2e528b49 4138 return (SET_ERROR(EINVAL));
34dc7c2f
BB
4139 }
4140
f3c9dca0
MT
4141 /*
4142 * Callers might not be able to detect properly that we are read-only,
4143 * so check it explicitly here.
4144 */
0037b49e 4145 if (zfs_is_readonly(zfsvfs)) {
768eaced 4146 zfs_exit(zfsvfs, FTAG);
f3c9dca0
MT
4147 return (SET_ERROR(EROFS));
4148 }
4149
34dc7c2f 4150 if (bfp->l_len < 0) {
768eaced 4151 zfs_exit(zfsvfs, FTAG);
2e528b49 4152 return (SET_ERROR(EINVAL));
34dc7c2f
BB
4153 }
4154
aec69371
ED
4155 /*
4156 * Permissions aren't checked on Solaris because on this OS
4157 * zfs_space() can only be called with an opened file handle.
4158 * On Linux we can get here through truncate_range() which
4159 * operates directly on inodes, so we need to check access rights.
4160 */
f224eddf 4161 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr,
d4dc53da 4162 zfs_init_idmap))) {
768eaced 4163 zfs_exit(zfsvfs, FTAG);
aec69371
ED
4164 return (error);
4165 }
4166
34dc7c2f
BB
4167 off = bfp->l_start;
4168 len = bfp->l_len; /* 0 means from off to end of file */
4169
b128c09f 4170 error = zfs_freesp(zp, off, len, flag, TRUE);
34dc7c2f 4171
768eaced 4172 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
4173 return (error);
4174}
4175
e5c39b95 4176int
3558fd73 4177zfs_fid(struct inode *ip, fid_t *fidp)
34dc7c2f 4178{
3558fd73 4179 znode_t *zp = ITOZ(ip);
0037b49e 4180 zfsvfs_t *zfsvfs = ITOZSB(ip);
34dc7c2f 4181 uint32_t gen;
428870ff 4182 uint64_t gen64;
34dc7c2f
BB
4183 uint64_t object = zp->z_id;
4184 zfid_short_t *zfid;
428870ff 4185 int size, i, error;
34dc7c2f 4186
768eaced
CC
4187 if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
4188 return (error);
c074a7de
AR
4189
4190 if (fidp->fid_len < SHORT_FID_LEN) {
4191 fidp->fid_len = SHORT_FID_LEN;
768eaced 4192 zfs_exit(zfsvfs, FTAG);
c074a7de
AR
4193 return (SET_ERROR(ENOSPC));
4194 }
4195
768eaced
CC
4196 if ((error = zfs_verify_zp(zp)) != 0) {
4197 zfs_exit(zfsvfs, FTAG);
4198 return (error);
4199 }
428870ff 4200
0037b49e 4201 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
428870ff 4202 &gen64, sizeof (uint64_t))) != 0) {
768eaced 4203 zfs_exit(zfsvfs, FTAG);
428870ff
BB
4204 return (error);
4205 }
4206
4207 gen = (uint32_t)gen64;
34dc7c2f 4208
9b77d1c9 4209 size = SHORT_FID_LEN;
34dc7c2f
BB
4210
4211 zfid = (zfid_short_t *)fidp;
4212
4213 zfid->zf_len = size;
4214
4215 for (i = 0; i < sizeof (zfid->zf_object); i++)
4216 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4217
4218 /* Must have a non-zero generation number to distinguish from .zfs */
4219 if (gen == 0)
4220 gen = 1;
4221 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4222 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4223
768eaced 4224 zfs_exit(zfsvfs, FTAG);
34dc7c2f
BB
4225 return (0);
4226}
4227
93ce2b4c 4228#if defined(_KERNEL)
f298b24d
BB
4229EXPORT_SYMBOL(zfs_open);
4230EXPORT_SYMBOL(zfs_close);
f298b24d
BB
4231EXPORT_SYMBOL(zfs_lookup);
4232EXPORT_SYMBOL(zfs_create);
4233EXPORT_SYMBOL(zfs_tmpfile);
4234EXPORT_SYMBOL(zfs_remove);
4235EXPORT_SYMBOL(zfs_mkdir);
4236EXPORT_SYMBOL(zfs_rmdir);
4237EXPORT_SYMBOL(zfs_readdir);
f298b24d
BB
4238EXPORT_SYMBOL(zfs_getattr_fast);
4239EXPORT_SYMBOL(zfs_setattr);
4240EXPORT_SYMBOL(zfs_rename);
4241EXPORT_SYMBOL(zfs_symlink);
4242EXPORT_SYMBOL(zfs_readlink);
4243EXPORT_SYMBOL(zfs_link);
4244EXPORT_SYMBOL(zfs_inactive);
4245EXPORT_SYMBOL(zfs_space);
4246EXPORT_SYMBOL(zfs_fid);
f298b24d
BB
4247EXPORT_SYMBOL(zfs_getpage);
4248EXPORT_SYMBOL(zfs_putpage);
4249EXPORT_SYMBOL(zfs_dirty_inode);
4250EXPORT_SYMBOL(zfs_map);
4251
7ada752a 4252/* CSTYLED */
a966c564
K
4253module_param(zfs_delete_blocks, ulong, 0644);
4254MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
c409e464 4255#endif