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