]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_znode.c
port async unlinked drain from illumos-nexenta
[mirror_zfs.git] / module / zfs / zfs_znode.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
5d43cc9a 23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
26/* Portions Copyright 2007 Jeremy Teo */
27
34dc7c2f
BB
28#ifdef _KERNEL
29#include <sys/types.h>
30#include <sys/param.h>
31#include <sys/time.h>
34dc7c2f 32#include <sys/sysmacros.h>
34dc7c2f 33#include <sys/mntent.h>
34dc7c2f
BB
34#include <sys/u8_textprep.h>
35#include <sys/dsl_dataset.h>
36#include <sys/vfs.h>
34dc7c2f
BB
37#include <sys/vnode.h>
38#include <sys/file.h>
39#include <sys/kmem.h>
40#include <sys/errno.h>
34dc7c2f
BB
41#include <sys/mode.h>
42#include <sys/atomic.h>
34dc7c2f
BB
43#include <sys/zfs_dir.h>
44#include <sys/zfs_acl.h>
45#include <sys/zfs_ioctl.h>
46#include <sys/zfs_rlock.h>
47#include <sys/zfs_fuid.h>
3558fd73 48#include <sys/zfs_vnops.h>
ebe7e575 49#include <sys/zfs_ctldir.h>
428870ff 50#include <sys/dnode.h>
34dc7c2f 51#include <sys/fs/zfs.h>
3558fd73 52#include <sys/zpl.h>
34dc7c2f
BB
53#endif /* _KERNEL */
54
55#include <sys/dmu.h>
f1512ee6 56#include <sys/dmu_objset.h>
50c957f7 57#include <sys/dmu_tx.h>
34dc7c2f
BB
58#include <sys/refcount.h>
59#include <sys/stat.h>
60#include <sys/zap.h>
61#include <sys/zfs_znode.h>
428870ff
BB
62#include <sys/sa.h>
63#include <sys/zfs_sa.h>
572e2857 64#include <sys/zfs_stat.h>
34dc7c2f
BB
65
66#include "zfs_prop.h"
428870ff 67#include "zfs_comutil.h"
34dc7c2f 68
b128c09f
BB
69/*
70 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
71 * turned on when DEBUG is also defined.
72 */
73#ifdef DEBUG
74#define ZNODE_STATS
75#endif /* DEBUG */
76
77#ifdef ZNODE_STATS
78#define ZNODE_STAT_ADD(stat) ((stat)++)
79#else
80#define ZNODE_STAT_ADD(stat) /* nothing */
81#endif /* ZNODE_STATS */
82
34dc7c2f
BB
83/*
84 * Functions needed for userland (ie: libzpool) are not put under
85 * #ifdef_KERNEL; the rest of the functions have dependencies
86 * (such as VFS logic) that will not compile easily in userland.
87 */
88#ifdef _KERNEL
9babb374 89
b128c09f 90static kmem_cache_t *znode_cache = NULL;
c96c36fa 91static kmem_cache_t *znode_hold_cache = NULL;
0720116d 92unsigned int zfs_object_mutex_size = ZFS_OBJ_MTX_SZ;
34dc7c2f 93
dcec0a12
AP
94/*
95 * This is used by the test suite so that it can delay znodes from being
96 * freed in order to inspect the unlinked set.
97 */
98int zfs_unlink_suspend_progress = 0;
99
5d43cc9a
MA
100/*
101 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
102 * z_rangelock. It will modify the offset and length of the lock to reflect
103 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is
104 * called with the rangelock_t's rl_lock held, which avoids races.
105 */
106static void
107zfs_rangelock_cb(locked_range_t *new, void *arg)
108{
109 znode_t *zp = arg;
110
111 /*
112 * If in append mode, convert to writer and lock starting at the
113 * current end of file.
114 */
115 if (new->lr_type == RL_APPEND) {
116 new->lr_offset = zp->z_size;
117 new->lr_type = RL_WRITER;
118 }
119
120 /*
121 * If we need to grow the block size then lock the whole file range.
122 */
123 uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
124 if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
125 zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) {
126 new->lr_offset = 0;
127 new->lr_length = UINT64_MAX;
128 }
129}
130
34dc7c2f
BB
131/*ARGSUSED*/
132static int
b128c09f 133zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
34dc7c2f
BB
134{
135 znode_t *zp = buf;
136
3558fd73 137 inode_init_once(ZTOI(zp));
b128c09f
BB
138 list_link_init(&zp->z_link_node);
139
34dc7c2f 140 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 141 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
448d7aaa 142 rw_init(&zp->z_name_lock, NULL, RW_NOLOCKDEP, NULL);
34dc7c2f 143 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
82a37189 144 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
34dc7c2f 145
5d43cc9a 146 rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
34dc7c2f 147
b128c09f 148 zp->z_dirlocks = NULL;
45d1cae3 149 zp->z_acl_cached = NULL;
82a37189 150 zp->z_xattr_cached = NULL;
98701490 151 zp->z_xattr_parent = 0;
572e2857 152 zp->z_moved = 0;
34dc7c2f
BB
153 return (0);
154}
155
156/*ARGSUSED*/
157static void
b128c09f 158zfs_znode_cache_destructor(void *buf, void *arg)
34dc7c2f
BB
159{
160 znode_t *zp = buf;
161
b128c09f 162 ASSERT(!list_link_active(&zp->z_link_node));
34dc7c2f 163 mutex_destroy(&zp->z_lock);
34dc7c2f
BB
164 rw_destroy(&zp->z_parent_lock);
165 rw_destroy(&zp->z_name_lock);
166 mutex_destroy(&zp->z_acl_lock);
82a37189 167 rw_destroy(&zp->z_xattr_lock);
5d43cc9a 168 rangelock_fini(&zp->z_rangelock);
34dc7c2f 169
b128c09f 170 ASSERT(zp->z_dirlocks == NULL);
45d1cae3 171 ASSERT(zp->z_acl_cached == NULL);
82a37189 172 ASSERT(zp->z_xattr_cached == NULL);
b128c09f
BB
173}
174
c96c36fa
BB
175static int
176zfs_znode_hold_cache_constructor(void *buf, void *arg, int kmflags)
177{
178 znode_hold_t *zh = buf;
179
180 mutex_init(&zh->zh_lock, NULL, MUTEX_DEFAULT, NULL);
424fd7c3 181 zfs_refcount_create(&zh->zh_refcount);
c96c36fa
BB
182 zh->zh_obj = ZFS_NO_OBJECT;
183
184 return (0);
185}
186
187static void
188zfs_znode_hold_cache_destructor(void *buf, void *arg)
189{
190 znode_hold_t *zh = buf;
191
192 mutex_destroy(&zh->zh_lock);
424fd7c3 193 zfs_refcount_destroy(&zh->zh_refcount);
c96c36fa
BB
194}
195
34dc7c2f
BB
196void
197zfs_znode_init(void)
198{
199 /*
5074bfe8
TC
200 * Initialize zcache. The KMC_SLAB hint is used in order that it be
201 * backed by kmalloc() when on the Linux slab in order that any
202 * wait_on_bit() operations on the related inode operate properly.
34dc7c2f
BB
203 */
204 ASSERT(znode_cache == NULL);
205 znode_cache = kmem_cache_create("zfs_znode_cache",
206 sizeof (znode_t), 0, zfs_znode_cache_constructor,
5074bfe8 207 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_SLAB);
c96c36fa
BB
208
209 ASSERT(znode_hold_cache == NULL);
210 znode_hold_cache = kmem_cache_create("zfs_znode_hold_cache",
211 sizeof (znode_hold_t), 0, zfs_znode_hold_cache_constructor,
212 zfs_znode_hold_cache_destructor, NULL, NULL, NULL, 0);
34dc7c2f
BB
213}
214
215void
216zfs_znode_fini(void)
217{
34dc7c2f
BB
218 /*
219 * Cleanup zcache
220 */
221 if (znode_cache)
222 kmem_cache_destroy(znode_cache);
223 znode_cache = NULL;
c96c36fa
BB
224
225 if (znode_hold_cache)
226 kmem_cache_destroy(znode_hold_cache);
227 znode_hold_cache = NULL;
228}
229
230/*
231 * The zfs_znode_hold_enter() / zfs_znode_hold_exit() functions are used to
232 * serialize access to a znode and its SA buffer while the object is being
233 * created or destroyed. This kind of locking would normally reside in the
234 * znode itself but in this case that's impossible because the znode and SA
235 * buffer may not yet exist. Therefore the locking is handled externally
236 * with an array of mutexs and AVLs trees which contain per-object locks.
237 *
238 * In zfs_znode_hold_enter() a per-object lock is created as needed, inserted
239 * in to the correct AVL tree and finally the per-object lock is held. In
240 * zfs_znode_hold_exit() the process is reversed. The per-object lock is
241 * released, removed from the AVL tree and destroyed if there are no waiters.
242 *
243 * This scheme has two important properties:
244 *
245 * 1) No memory allocations are performed while holding one of the z_hold_locks.
246 * This ensures evict(), which can be called from direct memory reclaim, will
247 * never block waiting on a z_hold_locks which just happens to have hashed
248 * to the same index.
249 *
250 * 2) All locks used to serialize access to an object are per-object and never
251 * shared. This minimizes lock contention without creating a large number
252 * of dedicated locks.
253 *
254 * On the downside it does require znode_lock_t structures to be frequently
255 * allocated and freed. However, because these are backed by a kmem cache
256 * and very short lived this cost is minimal.
257 */
258int
259zfs_znode_hold_compare(const void *a, const void *b)
260{
ee36c709
GN
261 const znode_hold_t *zh_a = (const znode_hold_t *)a;
262 const znode_hold_t *zh_b = (const znode_hold_t *)b;
263
264 return (AVL_CMP(zh_a->zh_obj, zh_b->zh_obj));
c96c36fa
BB
265}
266
267boolean_t
0037b49e 268zfs_znode_held(zfsvfs_t *zfsvfs, uint64_t obj)
c96c36fa
BB
269{
270 znode_hold_t *zh, search;
0037b49e 271 int i = ZFS_OBJ_HASH(zfsvfs, obj);
37c56346 272 boolean_t held;
c96c36fa
BB
273
274 search.zh_obj = obj;
275
0037b49e
BB
276 mutex_enter(&zfsvfs->z_hold_locks[i]);
277 zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
37c56346 278 held = (zh && MUTEX_HELD(&zh->zh_lock)) ? B_TRUE : B_FALSE;
0037b49e 279 mutex_exit(&zfsvfs->z_hold_locks[i]);
c96c36fa 280
37c56346 281 return (held);
c96c36fa
BB
282}
283
284static znode_hold_t *
0037b49e 285zfs_znode_hold_enter(zfsvfs_t *zfsvfs, uint64_t obj)
c96c36fa
BB
286{
287 znode_hold_t *zh, *zh_new, search;
0037b49e 288 int i = ZFS_OBJ_HASH(zfsvfs, obj);
c96c36fa
BB
289 boolean_t found = B_FALSE;
290
291 zh_new = kmem_cache_alloc(znode_hold_cache, KM_SLEEP);
292 zh_new->zh_obj = obj;
293 search.zh_obj = obj;
294
0037b49e
BB
295 mutex_enter(&zfsvfs->z_hold_locks[i]);
296 zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
c96c36fa
BB
297 if (likely(zh == NULL)) {
298 zh = zh_new;
0037b49e 299 avl_add(&zfsvfs->z_hold_trees[i], zh);
c96c36fa
BB
300 } else {
301 ASSERT3U(zh->zh_obj, ==, obj);
302 found = B_TRUE;
303 }
c13060e4 304 zfs_refcount_add(&zh->zh_refcount, NULL);
0037b49e 305 mutex_exit(&zfsvfs->z_hold_locks[i]);
c96c36fa
BB
306
307 if (found == B_TRUE)
308 kmem_cache_free(znode_hold_cache, zh_new);
309
310 ASSERT(MUTEX_NOT_HELD(&zh->zh_lock));
424fd7c3 311 ASSERT3S(zfs_refcount_count(&zh->zh_refcount), >, 0);
c96c36fa
BB
312 mutex_enter(&zh->zh_lock);
313
314 return (zh);
315}
316
317static void
0037b49e 318zfs_znode_hold_exit(zfsvfs_t *zfsvfs, znode_hold_t *zh)
c96c36fa 319{
0037b49e 320 int i = ZFS_OBJ_HASH(zfsvfs, zh->zh_obj);
c96c36fa
BB
321 boolean_t remove = B_FALSE;
322
0037b49e 323 ASSERT(zfs_znode_held(zfsvfs, zh->zh_obj));
424fd7c3 324 ASSERT3S(zfs_refcount_count(&zh->zh_refcount), >, 0);
c96c36fa
BB
325 mutex_exit(&zh->zh_lock);
326
0037b49e 327 mutex_enter(&zfsvfs->z_hold_locks[i]);
424fd7c3 328 if (zfs_refcount_remove(&zh->zh_refcount, NULL) == 0) {
0037b49e 329 avl_remove(&zfsvfs->z_hold_trees[i], zh);
c96c36fa
BB
330 remove = B_TRUE;
331 }
0037b49e 332 mutex_exit(&zfsvfs->z_hold_locks[i]);
c96c36fa
BB
333
334 if (remove == B_TRUE)
335 kmem_cache_free(znode_hold_cache, zh);
34dc7c2f
BB
336}
337
34dc7c2f 338static void
0037b49e 339zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
428870ff 340 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
34dc7c2f 341{
0037b49e 342 ASSERT(zfs_znode_held(zfsvfs, zp->z_id));
34dc7c2f
BB
343
344 mutex_enter(&zp->z_lock);
345
428870ff
BB
346 ASSERT(zp->z_sa_hdl == NULL);
347 ASSERT(zp->z_acl_cached == NULL);
348 if (sa_hdl == NULL) {
0037b49e 349 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
428870ff
BB
350 SA_HDL_SHARED, &zp->z_sa_hdl));
351 } else {
352 zp->z_sa_hdl = sa_hdl;
353 sa_set_userp(sa_hdl, zp);
354 }
34dc7c2f 355
428870ff 356 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
34dc7c2f 357
34dc7c2f 358 mutex_exit(&zp->z_lock);
34dc7c2f
BB
359}
360
361void
362zfs_znode_dmu_fini(znode_t *zp)
363{
c96c36fa 364 ASSERT(zfs_znode_held(ZTOZSB(zp), zp->z_id) || zp->z_unlinked ||
3558fd73 365 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
428870ff
BB
366
367 sa_handle_destroy(zp->z_sa_hdl);
368 zp->z_sa_hdl = NULL;
34dc7c2f
BB
369}
370
371/*
3558fd73
BB
372 * Called by new_inode() to allocate a new inode.
373 */
374int
375zfs_inode_alloc(struct super_block *sb, struct inode **ip)
376{
377 znode_t *zp;
378
79c76d5b 379 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
3558fd73
BB
380 *ip = ZTOI(zp);
381
382 return (0);
383}
384
385/*
386 * Called in multiple places when an inode should be destroyed.
387 */
388void
389zfs_inode_destroy(struct inode *ip)
390{
391 znode_t *zp = ITOZ(ip);
0037b49e 392 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3558fd73 393
0037b49e 394 mutex_enter(&zfsvfs->z_znodes_lock);
7b3e34ba 395 if (list_link_active(&zp->z_link_node)) {
0037b49e
BB
396 list_remove(&zfsvfs->z_all_znodes, zp);
397 zfsvfs->z_nr_znodes--;
7b3e34ba 398 }
0037b49e 399 mutex_exit(&zfsvfs->z_znodes_lock);
3558fd73
BB
400
401 if (zp->z_acl_cached) {
402 zfs_acl_free(zp->z_acl_cached);
403 zp->z_acl_cached = NULL;
404 }
405
82a37189
BB
406 if (zp->z_xattr_cached) {
407 nvlist_free(zp->z_xattr_cached);
408 zp->z_xattr_cached = NULL;
409 }
410
3558fd73
BB
411 kmem_cache_free(znode_cache, zp);
412}
413
414static void
0037b49e 415zfs_inode_set_ops(zfsvfs_t *zfsvfs, struct inode *ip)
3558fd73 416{
aa6d8c10 417 uint64_t rdev = 0;
3558fd73
BB
418
419 switch (ip->i_mode & S_IFMT) {
420 case S_IFREG:
421 ip->i_op = &zpl_inode_operations;
422 ip->i_fop = &zpl_file_operations;
423 ip->i_mapping->a_ops = &zpl_address_space_operations;
424 break;
425
426 case S_IFDIR:
427 ip->i_op = &zpl_dir_inode_operations;
428 ip->i_fop = &zpl_dir_file_operations;
429 ITOZ(ip)->z_zn_prefetch = B_TRUE;
430 break;
431
432 case S_IFLNK:
433 ip->i_op = &zpl_symlink_inode_operations;
434 break;
435
aa6d8c10
NB
436 /*
437 * rdev is only stored in a SA only for device files.
438 */
3558fd73
BB
439 case S_IFCHR:
440 case S_IFBLK:
0037b49e 441 (void) sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), &rdev,
53b1d979 442 sizeof (rdev));
aa6d8c10
NB
443 /*FALLTHROUGH*/
444 case S_IFIFO:
445 case S_IFSOCK:
3558fd73
BB
446 init_special_inode(ip, ip->i_mode, rdev);
447 ip->i_op = &zpl_special_inode_operations;
448 break;
449
450 default:
53b1d979
BB
451 zfs_panic_recover("inode %llu has invalid mode: 0x%x\n",
452 (u_longlong_t)ip->i_ino, ip->i_mode);
453
454 /* Assume the inode is a file and attempt to continue */
455 ip->i_mode = S_IFREG | 0644;
456 ip->i_op = &zpl_inode_operations;
457 ip->i_fop = &zpl_file_operations;
458 ip->i_mapping->a_ops = &zpl_address_space_operations;
459 break;
3558fd73
BB
460 }
461}
462
7bb1325f
CC
463void
464zfs_set_inode_flags(znode_t *zp, struct inode *ip)
465{
466 /*
467 * Linux and Solaris have different sets of file attributes, so we
468 * restrict this conversion to the intersection of the two.
469 */
a5248129
CC
470#ifdef HAVE_INODE_SET_FLAGS
471 unsigned int flags = 0;
472 if (zp->z_pflags & ZFS_IMMUTABLE)
473 flags |= S_IMMUTABLE;
474 if (zp->z_pflags & ZFS_APPENDONLY)
475 flags |= S_APPEND;
7bb1325f 476
a5248129
CC
477 inode_set_flags(ip, flags, S_IMMUTABLE|S_APPEND);
478#else
7bb1325f
CC
479 if (zp->z_pflags & ZFS_IMMUTABLE)
480 ip->i_flags |= S_IMMUTABLE;
481 else
482 ip->i_flags &= ~S_IMMUTABLE;
483
484 if (zp->z_pflags & ZFS_APPENDONLY)
485 ip->i_flags |= S_APPEND;
486 else
487 ip->i_flags &= ~S_APPEND;
a5248129 488#endif
7bb1325f
CC
489}
490
704cd075
CC
491/*
492 * Update the embedded inode given the znode. We should work toward
493 * eliminating this function as soon as possible by removing values
494 * which are duplicated between the znode and inode. If the generic
495 * inode has the correct field it should be used, and the ZFS code
496 * updated to access the inode. This can be done incrementally.
497 */
9f5f0019
NB
498void
499zfs_inode_update(znode_t *zp)
704cd075 500{
0037b49e 501 zfsvfs_t *zfsvfs;
704cd075
CC
502 struct inode *ip;
503 uint32_t blksize;
504 u_longlong_t i_blocks;
704cd075
CC
505
506 ASSERT(zp != NULL);
0037b49e 507 zfsvfs = ZTOZSB(zp);
704cd075
CC
508 ip = ZTOI(zp);
509
510 /* Skip .zfs control nodes which do not exist on disk. */
511 if (zfsctl_is_node(ip))
512 return;
513
704cd075
CC
514 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize, &i_blocks);
515
516 spin_lock(&ip->i_lock);
704cd075 517 ip->i_blocks = i_blocks;
704cd075
CC
518 i_size_write(ip, zp->z_size);
519 spin_unlock(&ip->i_lock);
520}
521
704cd075 522
3558fd73
BB
523/*
524 * Construct a znode+inode and initialize.
34dc7c2f
BB
525 *
526 * This does not do a call to dmu_set_user() that is
527 * up to the caller to do, in case you don't want to
528 * return the znode
529 */
530static znode_t *
0037b49e 531zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
31b6111f 532 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl)
34dc7c2f
BB
533{
534 znode_t *zp;
3558fd73 535 struct inode *ip;
7f89ae6b 536 uint64_t mode;
428870ff 537 uint64_t parent;
278f2236 538 uint64_t tmp_gen;
dfbc8630 539 uint64_t links;
2c6abf15 540 uint64_t z_uid, z_gid;
9f5f0019 541 uint64_t atime[2], mtime[2], ctime[2];
9c5167d1 542 uint64_t projid = ZFS_DEFAULT_PROJID;
9f5f0019 543 sa_bulk_attr_t bulk[11];
428870ff 544 int count = 0;
34dc7c2f 545
0037b49e 546 ASSERT(zfsvfs != NULL);
34dc7c2f 547
0037b49e 548 ip = new_inode(zfsvfs->z_sb);
3558fd73
BB
549 if (ip == NULL)
550 return (NULL);
7304b6e5 551
3558fd73 552 zp = ITOZ(ip);
34dc7c2f 553 ASSERT(zp->z_dirlocks == NULL);
ebe7e575
BB
554 ASSERT3P(zp->z_acl_cached, ==, NULL);
555 ASSERT3P(zp->z_xattr_cached, ==, NULL);
572e2857 556 zp->z_moved = 0;
428870ff 557 zp->z_sa_hdl = NULL;
34dc7c2f
BB
558 zp->z_unlinked = 0;
559 zp->z_atime_dirty = 0;
560 zp->z_mapcnt = 0;
34dc7c2f
BB
561 zp->z_id = db->db_object;
562 zp->z_blksz = blksz;
563 zp->z_seq = 0x7A4653;
564 zp->z_sync_cnt = 0;
ebe7e575
BB
565 zp->z_is_mapped = B_FALSE;
566 zp->z_is_ctldir = B_FALSE;
7b3e34ba 567 zp->z_is_stale = B_FALSE;
34dc7c2f 568
0037b49e 569 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
3558fd73 570
0037b49e
BB
571 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
572 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
573 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
574 &zp->z_size, 8);
575 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
576 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff 577 &zp->z_pflags, 8);
0037b49e 578 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
7304b6e5 579 &parent, 8);
0037b49e
BB
580 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &z_uid, 8);
581 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &z_gid, 8);
582 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
583 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
584 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
428870ff 585
9c5167d1
NF
586 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || tmp_gen == 0 ||
587 (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
588 (zp->z_pflags & ZFS_PROJID) &&
589 sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) {
428870ff
BB
590 if (hdl == NULL)
591 sa_handle_destroy(zp->z_sa_hdl);
07d63f0c 592 zp->z_sa_hdl = NULL;
3558fd73 593 goto error;
34dc7c2f 594 }
7304b6e5 595
9c5167d1 596 zp->z_projid = projid;
12fa7f34 597 zp->z_mode = ip->i_mode = mode;
278f2236 598 ip->i_generation = (uint32_t)tmp_gen;
ba2fe6af 599 ip->i_blkbits = SPA_MINBLOCKSHIFT;
dfbc8630 600 set_nlink(ip, (uint32_t)links);
2c6abf15
NB
601 zfs_uid_write(ip, z_uid);
602 zfs_gid_write(ip, z_gid);
7bb1325f 603 zfs_set_inode_flags(zp, ip);
7f89ae6b 604
98701490
CC
605 /* Cache the xattr parent id */
606 if (zp->z_pflags & ZFS_XATTR)
607 zp->z_xattr_parent = parent;
608
9f5f0019
NB
609 ZFS_TIME_DECODE(&ip->i_atime, atime);
610 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
611 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
612
3558fd73 613 ip->i_ino = obj;
9f5f0019 614 zfs_inode_update(zp);
0037b49e 615 zfs_inode_set_ops(zfsvfs, ip);
3558fd73 616
7b3e34ba
BB
617 /*
618 * The only way insert_inode_locked() can fail is if the ip->i_ino
619 * number is already hashed for this super block. This can never
620 * happen because the inode numbers map 1:1 with the object numbers.
621 *
622 * The one exception is rolling back a mounted file system, but in
623 * this case all the active inode are unhashed during the rollback.
624 */
625 VERIFY3S(insert_inode_locked(ip), ==, 0);
c85b224f 626
0037b49e
BB
627 mutex_enter(&zfsvfs->z_znodes_lock);
628 list_insert_tail(&zfsvfs->z_all_znodes, zp);
629 zfsvfs->z_nr_znodes++;
b128c09f 630 membar_producer();
0037b49e 631 mutex_exit(&zfsvfs->z_znodes_lock);
b128c09f 632
3558fd73 633 unlock_new_inode(ip);
34dc7c2f 634 return (zp);
3558fd73
BB
635
636error:
3558fd73 637 iput(ip);
d1d7e268 638 return (NULL);
34dc7c2f
BB
639}
640
1e8db771
BB
641/*
642 * Safely mark an inode dirty. Inodes which are part of a read-only
643 * file system or snapshot may not be dirtied.
644 */
645void
646zfs_mark_inode_dirty(struct inode *ip)
647{
0037b49e 648 zfsvfs_t *zfsvfs = ITOZSB(ip);
1e8db771 649
0037b49e 650 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
1e8db771
BB
651 return;
652
653 mark_inode_dirty(ip);
654}
655
428870ff
BB
656static uint64_t empty_xattr;
657static uint64_t pad[4];
658static zfs_acl_phys_t acl_phys;
34dc7c2f
BB
659/*
660 * Create a new DMU object to hold a zfs znode.
661 *
662 * IN: dzp - parent directory for new znode
663 * vap - file attributes for new znode
664 * tx - dmu transaction id for zap operations
665 * cr - credentials of caller
666 * flag - flags:
667 * IS_ROOT_NODE - new object will be root
668 * IS_XATTR - new object is an attribute
34dc7c2f
BB
669 * bonuslen - length of bonus buffer
670 * setaclp - File/Dir initial ACL
671 * fuidp - Tracks fuid allocation.
672 *
673 * OUT: zpp - allocated znode
674 *
675 */
676void
677zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
428870ff 678 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
34dc7c2f 679{
428870ff
BB
680 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
681 uint64_t mode, size, links, parent, pflags;
9c5167d1 682 uint64_t projid = ZFS_DEFAULT_PROJID;
428870ff 683 uint64_t rdev = 0;
0037b49e 684 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
428870ff 685 dmu_buf_t *db;
6413c95f 686 inode_timespec_t now;
34dc7c2f 687 uint64_t gen, obj;
428870ff 688 int bonuslen;
50c957f7 689 int dnodesize;
428870ff
BB
690 sa_handle_t *sa_hdl;
691 dmu_object_type_t obj_type;
f30484af 692 sa_bulk_attr_t *sa_attrs;
428870ff
BB
693 int cnt = 0;
694 zfs_acl_locator_cb_t locate = { 0 };
c96c36fa 695 znode_hold_t *zh;
34dc7c2f 696
0037b49e 697 if (zfsvfs->z_replay) {
34dc7c2f 698 obj = vap->va_nodeid;
34dc7c2f
BB
699 now = vap->va_ctime; /* see zfs_replay_create() */
700 gen = vap->va_nblocks; /* ditto */
50c957f7 701 dnodesize = vap->va_fsid; /* ditto */
34dc7c2f
BB
702 } else {
703 obj = 0;
704 gethrestime(&now);
705 gen = dmu_tx_get_txg(tx);
0037b49e 706 dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
34dc7c2f
BB
707 }
708
50c957f7
NB
709 if (dnodesize == 0)
710 dnodesize = DNODE_MIN_SIZE;
711
0037b49e 712 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
50c957f7 713
428870ff 714 bonuslen = (obj_type == DMU_OT_SA) ?
50c957f7 715 DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
428870ff 716
34dc7c2f
BB
717 /*
718 * Create a new DMU object.
719 */
720 /*
721 * There's currently no mechanism for pre-reading the blocks that will
572e2857 722 * be needed to allocate a new object, so we accept the small chance
34dc7c2f
BB
723 * that there will be an i/o error and we will fail one of the
724 * assertions below.
725 */
3558fd73 726 if (S_ISDIR(vap->va_mode)) {
0037b49e
BB
727 if (zfsvfs->z_replay) {
728 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
729 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
50c957f7 730 obj_type, bonuslen, dnodesize, tx));
34dc7c2f 731 } else {
0037b49e
BB
732 obj = zap_create_norm_dnsize(zfsvfs->z_os,
733 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
50c957f7 734 obj_type, bonuslen, dnodesize, tx);
34dc7c2f
BB
735 }
736 } else {
0037b49e
BB
737 if (zfsvfs->z_replay) {
738 VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
34dc7c2f 739 DMU_OT_PLAIN_FILE_CONTENTS, 0,
50c957f7 740 obj_type, bonuslen, dnodesize, tx));
34dc7c2f 741 } else {
0037b49e 742 obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
34dc7c2f 743 DMU_OT_PLAIN_FILE_CONTENTS, 0,
50c957f7 744 obj_type, bonuslen, dnodesize, tx);
34dc7c2f
BB
745 }
746 }
34dc7c2f 747
0037b49e 748 zh = zfs_znode_hold_enter(zfsvfs, obj);
9631681b 749 VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
34dc7c2f
BB
750
751 /*
752 * If this is the root, fix up the half-initialized parent pointer
753 * to reference the just-allocated physical data area.
754 */
755 if (flag & IS_ROOT_NODE) {
34dc7c2f
BB
756 dzp->z_id = obj;
757 }
758
759 /*
760 * If parent is an xattr, so am I.
761 */
9c5167d1 762 if (dzp->z_pflags & ZFS_XATTR) {
34dc7c2f 763 flag |= IS_XATTR;
34dc7c2f
BB
764 }
765
0037b49e 766 if (zfsvfs->z_use_fuids)
428870ff
BB
767 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
768 else
769 pflags = 0;
34dc7c2f 770
3558fd73 771 if (S_ISDIR(vap->va_mode)) {
428870ff 772 size = 2; /* contents ("." and "..") */
dfbc8630 773 links = 2;
428870ff 774 } else {
dfbc8630 775 size = 0;
ace1eae8 776 links = (flag & IS_TMPFILE) ? 0 : 1;
34dc7c2f
BB
777 }
778
aa6d8c10 779 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
dc1d7665 780 rdev = vap->va_rdev;
428870ff
BB
781
782 parent = dzp->z_id;
783 mode = acl_ids->z_mode;
34dc7c2f 784 if (flag & IS_XATTR)
428870ff 785 pflags |= ZFS_XATTR;
34dc7c2f 786
9c5167d1
NF
787 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) {
788 /*
789 * With ZFS_PROJID flag, we can easily know whether there is
790 * project ID stored on disk or not. See zfs_space_delta_cb().
791 */
792 if (obj_type != DMU_OT_ZNODE &&
793 dmu_objset_projectquota_enabled(zfsvfs->z_os))
794 pflags |= ZFS_PROJID;
795
796 /*
797 * Inherit project ID from parent if required.
798 */
799 projid = zfs_inherit_projid(dzp);
800 if (dzp->z_pflags & ZFS_PROJINHERIT)
801 pflags |= ZFS_PROJINHERIT;
802 }
803
428870ff
BB
804 /*
805 * No execs denied will be deterimed when zfs_mode_compute() is called.
806 */
807 pflags |= acl_ids->z_aclp->z_hints &
808 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
809 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
34dc7c2f 810
428870ff
BB
811 ZFS_TIME_ENCODE(&now, crtime);
812 ZFS_TIME_ENCODE(&now, ctime);
34dc7c2f 813
3558fd73 814 if (vap->va_mask & ATTR_ATIME) {
428870ff 815 ZFS_TIME_ENCODE(&vap->va_atime, atime);
34dc7c2f 816 } else {
428870ff 817 ZFS_TIME_ENCODE(&now, atime);
34dc7c2f
BB
818 }
819
3558fd73 820 if (vap->va_mask & ATTR_MTIME) {
428870ff
BB
821 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
822 } else {
823 ZFS_TIME_ENCODE(&now, mtime);
824 }
825
826 /* Now add in all of the "SA" attributes */
0037b49e 827 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
428870ff
BB
828 &sa_hdl));
829
830 /*
831 * Setup the array of attributes to be replaced/set on the new file
832 *
833 * order for DMU_OT_ZNODE is critical since it needs to be constructed
834 * in the old znode_phys_t format. Don't change this ordering
835 */
79c76d5b 836 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
428870ff
BB
837
838 if (obj_type == DMU_OT_ZNODE) {
0037b49e 839 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
428870ff 840 NULL, &atime, 16);
0037b49e 841 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
428870ff 842 NULL, &mtime, 16);
0037b49e 843 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
428870ff 844 NULL, &ctime, 16);
0037b49e 845 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
428870ff 846 NULL, &crtime, 16);
0037b49e 847 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
428870ff 848 NULL, &gen, 8);
0037b49e 849 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
428870ff 850 NULL, &mode, 8);
0037b49e 851 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
428870ff 852 NULL, &size, 8);
0037b49e 853 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
428870ff 854 NULL, &parent, 8);
34dc7c2f 855 } else {
0037b49e 856 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
428870ff 857 NULL, &mode, 8);
0037b49e 858 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
428870ff 859 NULL, &size, 8);
0037b49e 860 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
428870ff 861 NULL, &gen, 8);
0037b49e 862 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
3558fd73 863 NULL, &acl_ids->z_fuid, 8);
0037b49e 864 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
3558fd73 865 NULL, &acl_ids->z_fgid, 8);
0037b49e 866 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
428870ff 867 NULL, &parent, 8);
0037b49e 868 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
428870ff 869 NULL, &pflags, 8);
0037b49e 870 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
428870ff 871 NULL, &atime, 16);
0037b49e 872 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
428870ff 873 NULL, &mtime, 16);
0037b49e 874 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
428870ff 875 NULL, &ctime, 16);
0037b49e 876 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
428870ff
BB
877 NULL, &crtime, 16);
878 }
879
0037b49e 880 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
428870ff
BB
881
882 if (obj_type == DMU_OT_ZNODE) {
0037b49e 883 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
428870ff 884 &empty_xattr, 8);
9c5167d1
NF
885 } else if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
886 pflags & ZFS_PROJID) {
887 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PROJID(zfsvfs),
888 NULL, &projid, 8);
34dc7c2f 889 }
428870ff 890 if (obj_type == DMU_OT_ZNODE ||
aa6d8c10 891 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
0037b49e 892 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
428870ff 893 NULL, &rdev, 8);
428870ff
BB
894 }
895 if (obj_type == DMU_OT_ZNODE) {
0037b49e 896 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
428870ff 897 NULL, &pflags, 8);
0037b49e 898 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
428870ff 899 &acl_ids->z_fuid, 8);
0037b49e 900 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
428870ff 901 &acl_ids->z_fgid, 8);
0037b49e 902 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
428870ff 903 sizeof (uint64_t) * 4);
0037b49e 904 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
428870ff
BB
905 &acl_phys, sizeof (zfs_acl_phys_t));
906 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
0037b49e 907 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
428870ff
BB
908 &acl_ids->z_aclp->z_acl_count, 8);
909 locate.cb_aclp = acl_ids->z_aclp;
0037b49e 910 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
428870ff
BB
911 zfs_acl_data_locator, &locate,
912 acl_ids->z_aclp->z_acl_bytes);
913 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
914 acl_ids->z_fuid, acl_ids->z_fgid);
915 }
916
917 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
34dc7c2f 918
34dc7c2f 919 if (!(flag & IS_ROOT_NODE)) {
8d703987
BB
920 /*
921 * The call to zfs_znode_alloc() may fail if memory is low
922 * via the call path: alloc_inode() -> inode_init_always() ->
923 * security_inode_alloc() -> inode_alloc_security(). Since
924 * the existing code is written such that zfs_mknode() can
925 * not fail retry until sufficient memory has been reclaimed.
926 */
927 do {
928 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, obj,
929 sa_hdl);
930 } while (*zpp == NULL);
931
7b3e34ba
BB
932 VERIFY(*zpp != NULL);
933 VERIFY(dzp != NULL);
34dc7c2f
BB
934 } else {
935 /*
936 * If we are creating the root node, the "parent" we
937 * passed in is the znode for the root.
938 */
939 *zpp = dzp;
428870ff
BB
940
941 (*zpp)->z_sa_hdl = sa_hdl;
34dc7c2f 942 }
428870ff
BB
943
944 (*zpp)->z_pflags = pflags;
12fa7f34 945 (*zpp)->z_mode = ZTOI(*zpp)->i_mode = mode;
50c957f7 946 (*zpp)->z_dnodesize = dnodesize;
9c5167d1 947 (*zpp)->z_projid = projid;
428870ff 948
428870ff
BB
949 if (obj_type == DMU_OT_ZNODE ||
950 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
b0bc7a84 951 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
428870ff 952 }
d1d7e268 953 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
0037b49e 954 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
955}
956
5484965a 957/*
d3cc8b15
WA
958 * Update in-core attributes. It is assumed the caller will be doing an
959 * sa_bulk_update to push the changes out.
5484965a
BB
960 */
961void
962zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
963{
964 xoptattr_t *xoap;
7bb1325f 965 boolean_t update_inode = B_FALSE;
5484965a
BB
966
967 xoap = xva_getxoptattr(xvap);
968 ASSERT(xoap);
969
970 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
971 uint64_t times[2];
972 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
973 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
974 &times, sizeof (times), tx);
975 XVA_SET_RTN(xvap, XAT_CREATETIME);
976 }
977 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
978 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
979 zp->z_pflags, tx);
980 XVA_SET_RTN(xvap, XAT_READONLY);
981 }
982 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
983 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
984 zp->z_pflags, tx);
985 XVA_SET_RTN(xvap, XAT_HIDDEN);
986 }
987 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
988 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
989 zp->z_pflags, tx);
990 XVA_SET_RTN(xvap, XAT_SYSTEM);
991 }
992 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
993 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
994 zp->z_pflags, tx);
995 XVA_SET_RTN(xvap, XAT_ARCHIVE);
996 }
997 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
998 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
999 zp->z_pflags, tx);
1000 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
64c688d7 1001
7bb1325f 1002 update_inode = B_TRUE;
5484965a
BB
1003 }
1004 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1005 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1006 zp->z_pflags, tx);
1007 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1008 }
1009 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1010 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1011 zp->z_pflags, tx);
1012 XVA_SET_RTN(xvap, XAT_APPENDONLY);
64c688d7 1013
7bb1325f 1014 update_inode = B_TRUE;
5484965a
BB
1015 }
1016 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1017 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1018 zp->z_pflags, tx);
1019 XVA_SET_RTN(xvap, XAT_NODUMP);
1020 }
1021 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1022 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1023 zp->z_pflags, tx);
1024 XVA_SET_RTN(xvap, XAT_OPAQUE);
1025 }
1026 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1027 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1028 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1029 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1030 }
1031 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1032 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1033 zp->z_pflags, tx);
1034 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1035 }
1036 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1037 zfs_sa_set_scanstamp(zp, xvap, tx);
1038 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1039 }
1040 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1041 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1042 zp->z_pflags, tx);
1043 XVA_SET_RTN(xvap, XAT_REPARSE);
1044 }
1045 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1046 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1047 zp->z_pflags, tx);
1048 XVA_SET_RTN(xvap, XAT_OFFLINE);
1049 }
1050 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1051 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1052 zp->z_pflags, tx);
1053 XVA_SET_RTN(xvap, XAT_SPARSE);
1054 }
9c5167d1
NF
1055 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
1056 ZFS_ATTR_SET(zp, ZFS_PROJINHERIT, xoap->xoa_projinherit,
1057 zp->z_pflags, tx);
1058 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
1059 }
7bb1325f
CC
1060
1061 if (update_inode)
1062 zfs_set_inode_flags(zp, ZTOI(zp));
5484965a
BB
1063}
1064
34dc7c2f 1065int
0037b49e 1066zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
34dc7c2f
BB
1067{
1068 dmu_object_info_t doi;
1069 dmu_buf_t *db;
1070 znode_t *zp;
c96c36fa 1071 znode_hold_t *zh;
34dc7c2f 1072 int err;
428870ff 1073 sa_handle_t *hdl;
34dc7c2f
BB
1074
1075 *zpp = NULL;
1076
6f9548c4 1077again:
0037b49e 1078 zh = zfs_znode_hold_enter(zfsvfs, obj_num);
34dc7c2f 1079
0037b49e 1080 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
34dc7c2f 1081 if (err) {
0037b49e 1082 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1083 return (err);
1084 }
1085
1086 dmu_object_info_from_db(db, &doi);
428870ff
BB
1087 if (doi.doi_bonus_type != DMU_OT_SA &&
1088 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1089 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1090 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1091 sa_buf_rele(db, NULL);
0037b49e 1092 zfs_znode_hold_exit(zfsvfs, zh);
2e528b49 1093 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1094 }
1095
428870ff
BB
1096 hdl = dmu_buf_get_user(db);
1097 if (hdl != NULL) {
36df2843 1098 zp = sa_get_userdata(hdl);
34dc7c2f 1099
8ac67298 1100
34dc7c2f 1101 /*
428870ff
BB
1102 * Since "SA" does immediate eviction we
1103 * should never find a sa handle that doesn't
1104 * know about the znode.
34dc7c2f 1105 */
428870ff
BB
1106
1107 ASSERT3P(zp, !=, NULL);
1108
1109 mutex_enter(&zp->z_lock);
34dc7c2f 1110 ASSERT3U(zp->z_id, ==, obj_num);
98701490
CC
1111 /*
1112 * If igrab() returns NULL the VFS has independently
1113 * determined the inode should be evicted and has
1114 * called iput_final() to start the eviction process.
1115 * The SA handle is still valid but because the VFS
1116 * requires that the eviction succeed we must drop
1117 * our locks and references to allow the eviction to
1118 * complete. The zfs_zget() may then be retried.
1119 *
1120 * This unlikely case could be optimized by registering
1121 * a sops->drop_inode() callback. The callback would
1122 * need to detect the active SA hold thereby informing
1123 * the VFS that this inode should not be evicted.
1124 */
1125 if (igrab(ZTOI(zp)) == NULL) {
1126 mutex_exit(&zp->z_lock);
1127 sa_buf_rele(db, NULL);
0037b49e 1128 zfs_znode_hold_exit(zfsvfs, zh);
98701490
CC
1129 /* inode might need this to finish evict */
1130 cond_resched();
1131 goto again;
34dc7c2f 1132 }
98701490
CC
1133 *zpp = zp;
1134 err = 0;
34dc7c2f 1135 mutex_exit(&zp->z_lock);
f3ad9cd6 1136 sa_buf_rele(db, NULL);
0037b49e 1137 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1138 return (err);
1139 }
1140
1141 /*
3558fd73 1142 * Not found create new znode/vnode but only if file exists.
428870ff
BB
1143 *
1144 * There is a small window where zfs_vget() could
1145 * find this object while a file create is still in
1146 * progress. This is checked for in zfs_znode_alloc()
1147 *
1148 * if zfs_znode_alloc() fails it will drop the hold on the
1149 * bonus buffer.
34dc7c2f 1150 */
0037b49e 1151 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
31b6111f 1152 doi.doi_bonus_type, obj_num, NULL);
428870ff 1153 if (zp == NULL) {
2e528b49 1154 err = SET_ERROR(ENOENT);
428870ff
BB
1155 } else {
1156 *zpp = zp;
1157 }
0037b49e 1158 zfs_znode_hold_exit(zfsvfs, zh);
428870ff 1159 return (err);
34dc7c2f
BB
1160}
1161
1162int
1163zfs_rezget(znode_t *zp)
1164{
0037b49e 1165 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f
BB
1166 dmu_object_info_t doi;
1167 dmu_buf_t *db;
1168 uint64_t obj_num = zp->z_id;
428870ff 1169 uint64_t mode;
dfbc8630 1170 uint64_t links;
9f5f0019 1171 sa_bulk_attr_t bulk[10];
34dc7c2f 1172 int err;
428870ff
BB
1173 int count = 0;
1174 uint64_t gen;
2c6abf15 1175 uint64_t z_uid, z_gid;
9f5f0019 1176 uint64_t atime[2], mtime[2], ctime[2];
9c5167d1 1177 uint64_t projid = ZFS_DEFAULT_PROJID;
c96c36fa 1178 znode_hold_t *zh;
34dc7c2f 1179
cbecb4fb
CC
1180 /*
1181 * skip ctldir, otherwise they will always get invalidated. This will
1182 * cause funny behaviour for the mounted snapdirs. Especially for
1183 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1184 * anyone automount it again as long as someone is still using the
1185 * detached mount.
1186 */
1187 if (zp->z_is_ctldir)
1188 return (0);
1189
0037b49e 1190 zh = zfs_znode_hold_enter(zfsvfs, obj_num);
34dc7c2f 1191
428870ff
BB
1192 mutex_enter(&zp->z_acl_lock);
1193 if (zp->z_acl_cached) {
1194 zfs_acl_free(zp->z_acl_cached);
1195 zp->z_acl_cached = NULL;
1196 }
428870ff 1197 mutex_exit(&zp->z_acl_lock);
7b3e34ba 1198
228b461b 1199 rw_enter(&zp->z_xattr_lock, RW_WRITER);
7b3e34ba
BB
1200 if (zp->z_xattr_cached) {
1201 nvlist_free(zp->z_xattr_cached);
1202 zp->z_xattr_cached = NULL;
1203 }
7b3e34ba
BB
1204 rw_exit(&zp->z_xattr_lock);
1205
428870ff 1206 ASSERT(zp->z_sa_hdl == NULL);
0037b49e 1207 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
34dc7c2f 1208 if (err) {
0037b49e 1209 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1210 return (err);
1211 }
1212
1213 dmu_object_info_from_db(db, &doi);
428870ff
BB
1214 if (doi.doi_bonus_type != DMU_OT_SA &&
1215 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1216 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1217 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1218 sa_buf_rele(db, NULL);
0037b49e 1219 zfs_znode_hold_exit(zfsvfs, zh);
2e528b49 1220 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1221 }
1222
0037b49e 1223 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
428870ff
BB
1224
1225 /* reload cached values */
0037b49e 1226 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
428870ff 1227 &gen, sizeof (gen));
0037b49e 1228 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
428870ff 1229 &zp->z_size, sizeof (zp->z_size));
0037b49e 1230 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
dfbc8630 1231 &links, sizeof (links));
0037b49e 1232 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff 1233 &zp->z_pflags, sizeof (zp->z_pflags));
0037b49e 1234 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2c6abf15 1235 &z_uid, sizeof (z_uid));
0037b49e 1236 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
2c6abf15 1237 &z_gid, sizeof (z_gid));
0037b49e 1238 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
428870ff 1239 &mode, sizeof (mode));
0037b49e 1240 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
9f5f0019 1241 &atime, 16);
0037b49e 1242 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
9f5f0019 1243 &mtime, 16);
0037b49e 1244 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
9f5f0019 1245 &ctime, 16);
428870ff 1246
428870ff
BB
1247 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1248 zfs_znode_dmu_fini(zp);
0037b49e 1249 zfs_znode_hold_exit(zfsvfs, zh);
2e528b49 1250 return (SET_ERROR(EIO));
428870ff
BB
1251 }
1252
9c5167d1
NF
1253 if (dmu_objset_projectquota_enabled(zfsvfs->z_os)) {
1254 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs),
1255 &projid, 8);
1256 if (err != 0 && err != ENOENT) {
1257 zfs_znode_dmu_fini(zp);
1258 zfs_znode_hold_exit(zfsvfs, zh);
1259 return (SET_ERROR(err));
1260 }
1261 }
1262
1263 zp->z_projid = projid;
12fa7f34 1264 zp->z_mode = ZTOI(zp)->i_mode = mode;
2c6abf15
NB
1265 zfs_uid_write(ZTOI(zp), z_uid);
1266 zfs_gid_write(ZTOI(zp), z_gid);
572e2857 1267
9f5f0019
NB
1268 ZFS_TIME_DECODE(&ZTOI(zp)->i_atime, atime);
1269 ZFS_TIME_DECODE(&ZTOI(zp)->i_mtime, mtime);
1270 ZFS_TIME_DECODE(&ZTOI(zp)->i_ctime, ctime);
1271
278f2236 1272 if (gen != ZTOI(zp)->i_generation) {
428870ff 1273 zfs_znode_dmu_fini(zp);
0037b49e 1274 zfs_znode_hold_exit(zfsvfs, zh);
2e528b49 1275 return (SET_ERROR(EIO));
34dc7c2f
BB
1276 }
1277
dfbc8630 1278 set_nlink(ZTOI(zp), (uint32_t)links);
7bb1325f 1279 zfs_set_inode_flags(zp, ZTOI(zp));
dfbc8630 1280
34dc7c2f 1281 zp->z_blksz = doi.doi_data_block_size;
704cd075 1282 zp->z_atime_dirty = 0;
9f5f0019 1283 zfs_inode_update(zp);
34dc7c2f 1284
6a218566
AG
1285 /*
1286 * If the file has zero links, then it has been unlinked on the send
1287 * side and it must be in the received unlinked set.
1288 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1289 * stale data and to prevent automatical removal of the file in
1290 * zfs_zinactive(). The file will be removed either when it is removed
1291 * on the send side and the next incremental stream is received or
1292 * when the unlinked set gets processed.
1293 */
1294 zp->z_unlinked = (ZTOI(zp)->i_nlink == 0);
1295 if (zp->z_unlinked)
1296 zfs_znode_dmu_fini(zp);
1297
0037b49e 1298 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1299
1300 return (0);
1301}
1302
1303void
1304zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1305{
0037b49e
BB
1306 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1307 objset_t *os = zfsvfs->z_os;
34dc7c2f 1308 uint64_t obj = zp->z_id;
572e2857 1309 uint64_t acl_obj = zfs_external_acl(zp);
c96c36fa 1310 znode_hold_t *zh;
34dc7c2f 1311
0037b49e 1312 zh = zfs_znode_hold_enter(zfsvfs, obj);
572e2857
BB
1313 if (acl_obj) {
1314 VERIFY(!zp->z_is_sa);
b128c09f 1315 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
572e2857 1316 }
b128c09f 1317 VERIFY(0 == dmu_object_free(os, obj, tx));
34dc7c2f 1318 zfs_znode_dmu_fini(zp);
0037b49e 1319 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1320}
1321
1322void
1323zfs_zinactive(znode_t *zp)
1324{
0037b49e 1325 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f 1326 uint64_t z_id = zp->z_id;
c96c36fa 1327 znode_hold_t *zh;
34dc7c2f 1328
428870ff 1329 ASSERT(zp->z_sa_hdl);
34dc7c2f
BB
1330
1331 /*
d6bd8eaa 1332 * Don't allow a zfs_zget() while were trying to release this znode.
34dc7c2f 1333 */
0037b49e 1334 zh = zfs_znode_hold_enter(zfsvfs, z_id);
d6bd8eaa 1335
34dc7c2f 1336 mutex_enter(&zp->z_lock);
34dc7c2f
BB
1337
1338 /*
6a218566
AG
1339 * If this was the last reference to a file with no links, remove
1340 * the file from the file system unless the file system is mounted
1341 * read-only. That can happen, for example, if the file system was
1342 * originally read-write, the file was opened, then unlinked and
1343 * the file system was made read-only before the file was finally
1344 * closed. The file will remain in the unlinked set.
34dc7c2f
BB
1345 */
1346 if (zp->z_unlinked) {
6a218566 1347 ASSERT(!zfsvfs->z_issnap);
dcec0a12 1348 if (!zfs_is_readonly(zfsvfs) && !zfs_unlink_suspend_progress) {
6a218566
AG
1349 mutex_exit(&zp->z_lock);
1350 zfs_znode_hold_exit(zfsvfs, zh);
1351 zfs_rmnode(zp);
1352 return;
1353 }
34dc7c2f 1354 }
428870ff 1355
34dc7c2f
BB
1356 mutex_exit(&zp->z_lock);
1357 zfs_znode_dmu_fini(zp);
d6bd8eaa 1358
0037b49e 1359 zfs_znode_hold_exit(zfsvfs, zh);
34dc7c2f
BB
1360}
1361
6d111134
TC
1362static inline int
1363zfs_compare_timespec(struct timespec *t1, struct timespec *t2)
1364{
1365 if (t1->tv_sec < t2->tv_sec)
1366 return (-1);
1367
1368 if (t1->tv_sec > t2->tv_sec)
1369 return (1);
1370
1371 return (t1->tv_nsec - t2->tv_nsec);
1372}
1373
6d111134
TC
1374/*
1375 * Prepare to update znode time stamps.
1376 *
1377 * IN: zp - znode requiring timestamp update
0df9673f 1378 * flag - ATTR_MTIME, ATTR_CTIME flags
6d111134 1379 *
0df9673f 1380 * OUT: zp - z_seq
6d111134
TC
1381 * mtime - new mtime
1382 * ctime - new ctime
1383 *
0df9673f
CC
1384 * Note: We don't update atime here, because we rely on Linux VFS to do
1385 * atime updating.
6d111134 1386 */
34dc7c2f 1387void
428870ff 1388zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
0df9673f 1389 uint64_t ctime[2])
34dc7c2f 1390{
6413c95f 1391 inode_timespec_t now;
34dc7c2f 1392
34dc7c2f
BB
1393 gethrestime(&now);
1394
0df9673f 1395 zp->z_seq++;
34dc7c2f 1396
3558fd73 1397 if (flag & ATTR_MTIME) {
428870ff 1398 ZFS_TIME_ENCODE(&now, mtime);
9f5f0019 1399 ZFS_TIME_DECODE(&(ZTOI(zp)->i_mtime), mtime);
3558fd73 1400 if (ZTOZSB(zp)->z_use_fuids) {
428870ff
BB
1401 zp->z_pflags |= (ZFS_ARCHIVE |
1402 ZFS_AV_MODIFIED);
1403 }
34dc7c2f
BB
1404 }
1405
3558fd73 1406 if (flag & ATTR_CTIME) {
428870ff 1407 ZFS_TIME_ENCODE(&now, ctime);
9f5f0019 1408 ZFS_TIME_DECODE(&(ZTOI(zp)->i_ctime), ctime);
3558fd73 1409 if (ZTOZSB(zp)->z_use_fuids)
428870ff 1410 zp->z_pflags |= ZFS_ARCHIVE;
34dc7c2f
BB
1411 }
1412}
1413
34dc7c2f
BB
1414/*
1415 * Grow the block size for a file.
1416 *
1417 * IN: zp - znode of file to free data in.
1418 * size - requested block size
1419 * tx - open transaction.
1420 *
1421 * NOTE: this function assumes that the znode is write locked.
1422 */
1423void
1424zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1425{
1426 int error;
1427 u_longlong_t dummy;
1428
1429 if (size <= zp->z_blksz)
1430 return;
1431 /*
1432 * If the file size is already greater than the current blocksize,
1433 * we will not grow. If there is more than one block in a file,
1434 * the blocksize cannot change.
1435 */
428870ff 1436 if (zp->z_blksz && zp->z_size > zp->z_blksz)
34dc7c2f
BB
1437 return;
1438
3558fd73 1439 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
34dc7c2f 1440 size, 0, tx);
428870ff 1441
34dc7c2f
BB
1442 if (error == ENOTSUP)
1443 return;
c99c9001 1444 ASSERT0(error);
34dc7c2f
BB
1445
1446 /* What blocksize did we actually get? */
428870ff 1447 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
34dc7c2f
BB
1448}
1449
34dc7c2f 1450/*
b128c09f 1451 * Increase the file length
34dc7c2f
BB
1452 *
1453 * IN: zp - znode of file to free data in.
b128c09f 1454 * end - new end-of-file
34dc7c2f 1455 *
19d55079 1456 * RETURN: 0 on success, error code on failure
34dc7c2f 1457 */
b128c09f
BB
1458static int
1459zfs_extend(znode_t *zp, uint64_t end)
34dc7c2f 1460{
0037b49e 1461 zfsvfs_t *zfsvfs = ZTOZSB(zp);
b128c09f 1462 dmu_tx_t *tx;
5d43cc9a 1463 locked_range_t *lr;
b128c09f 1464 uint64_t newblksz;
34dc7c2f
BB
1465 int error;
1466
34dc7c2f 1467 /*
b128c09f 1468 * We will change zp_size, lock the whole file.
34dc7c2f 1469 */
5d43cc9a 1470 lr = rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
34dc7c2f
BB
1471
1472 /*
1473 * Nothing to do if file already at desired length.
1474 */
428870ff 1475 if (end <= zp->z_size) {
5d43cc9a 1476 rangelock_exit(lr);
34dc7c2f
BB
1477 return (0);
1478 }
0037b49e 1479 tx = dmu_tx_create(zfsvfs->z_os);
428870ff
BB
1480 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1481 zfs_sa_upgrade_txholds(tx, zp);
b128c09f 1482 if (end > zp->z_blksz &&
0037b49e 1483 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
34dc7c2f
BB
1484 /*
1485 * We are growing the file past the current block size.
1486 */
3558fd73 1487 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
f1512ee6
MA
1488 /*
1489 * File's blocksize is already larger than the
1490 * "recordsize" property. Only let it grow to
1491 * the next power of 2.
1492 */
34dc7c2f 1493 ASSERT(!ISP2(zp->z_blksz));
f1512ee6 1494 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
34dc7c2f 1495 } else {
3558fd73 1496 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
34dc7c2f 1497 }
b128c09f
BB
1498 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1499 } else {
1500 newblksz = 0;
34dc7c2f
BB
1501 }
1502
384f8a09 1503 error = dmu_tx_assign(tx, TXG_WAIT);
34dc7c2f 1504 if (error) {
34dc7c2f 1505 dmu_tx_abort(tx);
5d43cc9a 1506 rangelock_exit(lr);
34dc7c2f
BB
1507 return (error);
1508 }
1509
b128c09f
BB
1510 if (newblksz)
1511 zfs_grow_blocksize(zp, newblksz, tx);
34dc7c2f 1512
428870ff
BB
1513 zp->z_size = end;
1514
3558fd73 1515 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
428870ff 1516 &zp->z_size, sizeof (zp->z_size), tx));
34dc7c2f 1517
5d43cc9a 1518 rangelock_exit(lr);
34dc7c2f 1519
b128c09f 1520 dmu_tx_commit(tx);
34dc7c2f 1521
b128c09f
BB
1522 return (0);
1523}
1524
223df016
TC
1525/*
1526 * zfs_zero_partial_page - Modeled after update_pages() but
1527 * with different arguments and semantics for use by zfs_freesp().
1528 *
1529 * Zeroes a piece of a single page cache entry for zp at offset
1530 * start and length len.
1531 *
1532 * Caller must acquire a range lock on the file for the region
1533 * being zeroed in order that the ARC and page cache stay in sync.
1534 */
1535static void
1536zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len)
1537{
1538 struct address_space *mp = ZTOI(zp)->i_mapping;
1539 struct page *pp;
1540 int64_t off;
1541 void *pb;
1542
8b1899d3 1543 ASSERT((start & PAGE_MASK) == ((start + len - 1) & PAGE_MASK));
223df016 1544
8b1899d3
BB
1545 off = start & (PAGE_SIZE - 1);
1546 start &= PAGE_MASK;
223df016 1547
8b1899d3 1548 pp = find_lock_page(mp, start >> PAGE_SHIFT);
223df016
TC
1549 if (pp) {
1550 if (mapping_writably_mapped(mp))
1551 flush_dcache_page(pp);
1552
1553 pb = kmap(pp);
1554 bzero(pb + off, len);
1555 kunmap(pp);
1556
1557 if (mapping_writably_mapped(mp))
1558 flush_dcache_page(pp);
1559
1560 mark_page_accessed(pp);
1561 SetPageUptodate(pp);
1562 ClearPageError(pp);
1563 unlock_page(pp);
8b1899d3 1564 put_page(pp);
223df016
TC
1565 }
1566}
1567
b128c09f
BB
1568/*
1569 * Free space in a file.
1570 *
1571 * IN: zp - znode of file to free data in.
1572 * off - start of section to free.
1573 * len - length of section to free.
1574 *
19d55079 1575 * RETURN: 0 on success, error code on failure
b128c09f
BB
1576 */
1577static int
1578zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1579{
0037b49e 1580 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5d43cc9a 1581 locked_range_t *lr;
b128c09f
BB
1582 int error;
1583
1584 /*
1585 * Lock the range being freed.
1586 */
5d43cc9a 1587 lr = rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
b128c09f
BB
1588
1589 /*
1590 * Nothing to do if file already at desired length.
1591 */
428870ff 1592 if (off >= zp->z_size) {
5d43cc9a 1593 rangelock_exit(lr);
b128c09f 1594 return (0);
34dc7c2f
BB
1595 }
1596
428870ff
BB
1597 if (off + len > zp->z_size)
1598 len = zp->z_size - off;
b128c09f 1599
0037b49e 1600 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
b128c09f 1601
223df016
TC
1602 /*
1603 * Zero partial page cache entries. This must be done under a
1604 * range lock in order to keep the ARC and page cache in sync.
1605 */
1606 if (zp->z_is_mapped) {
1607 loff_t first_page, last_page, page_len;
1608 loff_t first_page_offset, last_page_offset;
1609
1610 /* first possible full page in hole */
8b1899d3 1611 first_page = (off + PAGE_SIZE - 1) >> PAGE_SHIFT;
223df016 1612 /* last page of hole */
8b1899d3 1613 last_page = (off + len) >> PAGE_SHIFT;
223df016
TC
1614
1615 /* offset of first_page */
8b1899d3 1616 first_page_offset = first_page << PAGE_SHIFT;
223df016 1617 /* offset of last_page */
8b1899d3 1618 last_page_offset = last_page << PAGE_SHIFT;
223df016 1619
cb08f063
TC
1620 /* truncate whole pages */
1621 if (last_page_offset > first_page_offset) {
1622 truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1623 first_page_offset, last_page_offset - 1);
1624 }
1625
1626 /* truncate sub-page ranges */
223df016
TC
1627 if (first_page > last_page) {
1628 /* entire punched area within a single page */
1629 zfs_zero_partial_page(zp, off, len);
1630 } else {
1631 /* beginning of punched area at the end of a page */
1632 page_len = first_page_offset - off;
1633 if (page_len > 0)
1634 zfs_zero_partial_page(zp, off, page_len);
1635
1636 /* end of punched area at the beginning of a page */
1637 page_len = off + len - last_page_offset;
1638 if (page_len > 0)
1639 zfs_zero_partial_page(zp, last_page_offset,
1640 page_len);
1641 }
1642 }
5d43cc9a 1643 rangelock_exit(lr);
34dc7c2f 1644
b128c09f
BB
1645 return (error);
1646}
1647
1648/*
1649 * Truncate a file
1650 *
1651 * IN: zp - znode of file to free data in.
1652 * end - new end-of-file.
1653 *
19d55079 1654 * RETURN: 0 on success, error code on failure
b128c09f
BB
1655 */
1656static int
1657zfs_trunc(znode_t *zp, uint64_t end)
1658{
0037b49e 1659 zfsvfs_t *zfsvfs = ZTOZSB(zp);
b128c09f 1660 dmu_tx_t *tx;
5d43cc9a 1661 locked_range_t *lr;
b128c09f 1662 int error;
572e2857
BB
1663 sa_bulk_attr_t bulk[2];
1664 int count = 0;
b128c09f
BB
1665
1666 /*
1667 * We will change zp_size, lock the whole file.
1668 */
5d43cc9a 1669 lr = rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
b128c09f
BB
1670
1671 /*
1672 * Nothing to do if file already at desired length.
1673 */
428870ff 1674 if (end >= zp->z_size) {
5d43cc9a 1675 rangelock_exit(lr);
b128c09f
BB
1676 return (0);
1677 }
1678
18a2485f
FS
1679 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1680 DMU_OBJECT_END);
b128c09f 1681 if (error) {
5d43cc9a 1682 rangelock_exit(lr);
b128c09f
BB
1683 return (error);
1684 }
0037b49e 1685 tx = dmu_tx_create(zfsvfs->z_os);
428870ff
BB
1686 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1687 zfs_sa_upgrade_txholds(tx, zp);
19d55079 1688 dmu_tx_mark_netfree(tx);
7a8f0e80 1689 error = dmu_tx_assign(tx, TXG_WAIT);
b128c09f 1690 if (error) {
b128c09f 1691 dmu_tx_abort(tx);
5d43cc9a 1692 rangelock_exit(lr);
b128c09f
BB
1693 return (error);
1694 }
b128c09f 1695
428870ff 1696 zp->z_size = end;
0037b49e 1697 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
572e2857 1698 NULL, &zp->z_size, sizeof (zp->z_size));
428870ff 1699
572e2857
BB
1700 if (end == 0) {
1701 zp->z_pflags &= ~ZFS_SPARSE;
0037b49e 1702 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
572e2857
BB
1703 NULL, &zp->z_pflags, 8);
1704 }
1705 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
b128c09f 1706
34dc7c2f 1707 dmu_tx_commit(tx);
5d43cc9a 1708 rangelock_exit(lr);
34dc7c2f
BB
1709
1710 return (0);
1711}
1712
b128c09f
BB
1713/*
1714 * Free space in a file
1715 *
1716 * IN: zp - znode of file to free data in.
1717 * off - start of range
1718 * len - end of range (0 => EOF)
1719 * flag - current file open mode flags.
1720 * log - TRUE if this action should be logged
1721 *
19d55079 1722 * RETURN: 0 on success, error code on failure
b128c09f
BB
1723 */
1724int
1725zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1726{
b128c09f 1727 dmu_tx_t *tx;
0037b49e
BB
1728 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1729 zilog_t *zilog = zfsvfs->z_log;
428870ff
BB
1730 uint64_t mode;
1731 uint64_t mtime[2], ctime[2];
1732 sa_bulk_attr_t bulk[3];
1733 int count = 0;
b128c09f
BB
1734 int error;
1735
0037b49e 1736 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
428870ff
BB
1737 sizeof (mode))) != 0)
1738 return (error);
1739
1740 if (off > zp->z_size) {
b128c09f
BB
1741 error = zfs_extend(zp, off+len);
1742 if (error == 0 && log)
1743 goto log;
223df016 1744 goto out;
b128c09f
BB
1745 }
1746
b128c09f
BB
1747 if (len == 0) {
1748 error = zfs_trunc(zp, off);
1749 } else {
1750 if ((error = zfs_free_range(zp, off, len)) == 0 &&
428870ff 1751 off + len > zp->z_size)
b128c09f
BB
1752 error = zfs_extend(zp, off+len);
1753 }
1754 if (error || !log)
223df016 1755 goto out;
b128c09f 1756log:
0037b49e 1757 tx = dmu_tx_create(zfsvfs->z_os);
428870ff
BB
1758 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1759 zfs_sa_upgrade_txholds(tx, zp);
384f8a09 1760 error = dmu_tx_assign(tx, TXG_WAIT);
b128c09f 1761 if (error) {
b128c09f 1762 dmu_tx_abort(tx);
223df016 1763 goto out;
b128c09f
BB
1764 }
1765
0037b49e
BB
1766 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1768 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
428870ff 1769 NULL, &zp->z_pflags, 8);
0df9673f 1770 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
428870ff
BB
1771 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1772 ASSERT(error == 0);
1773
b128c09f
BB
1774 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1775
1776 dmu_tx_commit(tx);
223df016 1777
960e08fe 1778 zfs_inode_update(zp);
223df016
TC
1779 error = 0;
1780
1781out:
1782 /*
1783 * Truncate the page cache - for file truncate operations, use
1784 * the purpose-built API for truncations. For punching operations,
cb08f063 1785 * the truncation is handled under a range lock in zfs_free_range.
223df016
TC
1786 */
1787 if (len == 0)
1788 truncate_setsize(ZTOI(zp), off);
223df016 1789 return (error);
b128c09f
BB
1790}
1791
34dc7c2f
BB
1792void
1793zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1794{
22872ff5 1795 struct super_block *sb;
0037b49e 1796 zfsvfs_t *zfsvfs;
428870ff 1797 uint64_t moid, obj, sa_obj, version;
22872ff5 1798 uint64_t sense = ZFS_CASE_SENSITIVE;
34dc7c2f
BB
1799 uint64_t norm = 0;
1800 nvpair_t *elem;
c96c36fa 1801 int size;
34dc7c2f 1802 int error;
22872ff5
BB
1803 int i;
1804 znode_t *rootzp = NULL;
1805 vattr_t vattr;
1806 znode_t *zp;
1807 zfs_acl_ids_t acl_ids;
34dc7c2f
BB
1808
1809 /*
1810 * First attempt to create master node.
1811 */
1812 /*
1813 * In an empty objset, there are no blocks to read and thus
1814 * there can be no i/o errors (which we assert below).
1815 */
1816 moid = MASTER_NODE_OBJ;
1817 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1818 DMU_OT_NONE, 0, tx);
1819 ASSERT(error == 0);
1820
1821 /*
1822 * Set starting attributes.
1823 */
428870ff 1824 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
34dc7c2f
BB
1825 elem = NULL;
1826 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1827 /* For the moment we expect all zpl props to be uint64_ts */
1828 uint64_t val;
1829 char *name;
1830
1831 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1832 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1833 name = nvpair_name(elem);
1834 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
9babb374
BB
1835 if (val < version)
1836 version = val;
34dc7c2f
BB
1837 } else {
1838 error = zap_update(os, moid, name, 8, 1, &val, tx);
1839 }
1840 ASSERT(error == 0);
1841 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1842 norm = val;
22872ff5
BB
1843 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1844 sense = val;
34dc7c2f
BB
1845 }
1846 ASSERT(version != 0);
9babb374 1847 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
34dc7c2f 1848
428870ff
BB
1849 /*
1850 * Create zap object used for SA attribute registration
1851 */
1852
1853 if (version >= ZPL_VERSION_SA) {
1854 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1855 DMU_OT_NONE, 0, tx);
1856 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1857 ASSERT(error == 0);
1858 } else {
1859 sa_obj = 0;
1860 }
34dc7c2f
BB
1861 /*
1862 * Create a delete queue.
1863 */
9babb374 1864 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
34dc7c2f 1865
9babb374 1866 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
34dc7c2f
BB
1867 ASSERT(error == 0);
1868
9babb374 1869 /*
0037b49e 1870 * Create root znode. Create minimal znode/inode/zfsvfs/sb
22872ff5 1871 * to allow zfs_mknode to work.
9babb374 1872 */
22872ff5
BB
1873 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1874 vattr.va_mode = S_IFDIR|0755;
1875 vattr.va_uid = crgetuid(cr);
1876 vattr.va_gid = crgetgid(cr);
1877
79c76d5b 1878 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
22872ff5
BB
1879 rootzp->z_moved = 0;
1880 rootzp->z_unlinked = 0;
1881 rootzp->z_atime_dirty = 0;
1882 rootzp->z_is_sa = USE_SA(version, os);
9c5167d1 1883 rootzp->z_pflags = 0;
22872ff5 1884
0037b49e
BB
1885 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1886 zfsvfs->z_os = os;
1887 zfsvfs->z_parent = zfsvfs;
1888 zfsvfs->z_version = version;
1889 zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1890 zfsvfs->z_use_sa = USE_SA(version, os);
1891 zfsvfs->z_norm = norm;
22872ff5 1892
79c76d5b 1893 sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP);
0037b49e 1894 sb->s_fs_info = zfsvfs;
22872ff5
BB
1895
1896 ZTOI(rootzp)->i_sb = sb;
1897
1898 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
0037b49e 1899 &zfsvfs->z_attr_table);
9babb374 1900
22872ff5 1901 ASSERT(error == 0);
9babb374 1902
60101509 1903 /*
22872ff5
BB
1904 * Fold case on file systems that are always or sometimes case
1905 * insensitive.
60101509 1906 */
22872ff5 1907 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
0037b49e 1908 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
60101509 1909
0037b49e
BB
1910 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1911 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
22872ff5 1912 offsetof(znode_t, z_link_node));
60101509 1913
c96c36fa 1914 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
0037b49e
BB
1915 zfsvfs->z_hold_size = size;
1916 zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
1917 KM_SLEEP);
1918 zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
c96c36fa 1919 for (i = 0; i != size; i++) {
0037b49e 1920 avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
c96c36fa 1921 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
0037b49e 1922 mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
c96c36fa 1923 }
60101509 1924
22872ff5
BB
1925 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1926 cr, NULL, &acl_ids));
1927 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1928 ASSERT3P(zp, ==, rootzp);
1929 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1930 ASSERT(error == 0);
1931 zfs_acl_ids_free(&acl_ids);
60101509 1932
22872ff5
BB
1933 atomic_set(&ZTOI(rootzp)->i_count, 0);
1934 sa_handle_destroy(rootzp->z_sa_hdl);
22872ff5
BB
1935 kmem_cache_free(znode_cache, rootzp);
1936
c96c36fa 1937 for (i = 0; i != size; i++) {
0037b49e
BB
1938 avl_destroy(&zfsvfs->z_hold_trees[i]);
1939 mutex_destroy(&zfsvfs->z_hold_locks[i]);
c96c36fa 1940 }
2708f716 1941
c17486b2
GN
1942 mutex_destroy(&zfsvfs->z_znodes_lock);
1943
0037b49e
BB
1944 vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
1945 vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
2708f716 1946 kmem_free(sb, sizeof (struct super_block));
0037b49e 1947 kmem_free(zfsvfs, sizeof (zfsvfs_t));
34dc7c2f 1948}
34dc7c2f 1949#endif /* _KERNEL */
428870ff 1950
34dc7c2f 1951static int
572e2857
BB
1952zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1953{
1954 uint64_t sa_obj = 0;
1955 int error;
1956
1957 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1958 if (error != 0 && error != ENOENT)
1959 return (error);
1960
1961 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1962 return (error);
1963}
1964
1965static int
1966zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
7b8518cb 1967 dmu_buf_t **db, void *tag)
34dc7c2f 1968{
34dc7c2f 1969 dmu_object_info_t doi;
34dc7c2f 1970 int error;
428870ff 1971
7b8518cb 1972 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
34dc7c2f
BB
1973 return (error);
1974
572e2857 1975 dmu_object_info_from_db(*db, &doi);
428870ff
BB
1976 if ((doi.doi_bonus_type != DMU_OT_SA &&
1977 doi.doi_bonus_type != DMU_OT_ZNODE) ||
d6320ddb
BB
1978 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1979 doi.doi_bonus_size < sizeof (znode_phys_t))) {
7b8518cb 1980 sa_buf_rele(*db, tag);
2e528b49 1981 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1982 }
1983
572e2857
BB
1984 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1985 if (error != 0) {
7b8518cb 1986 sa_buf_rele(*db, tag);
428870ff
BB
1987 return (error);
1988 }
1989
572e2857
BB
1990 return (0);
1991}
1992
1993void
7b8518cb 1994zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
572e2857
BB
1995{
1996 sa_handle_destroy(hdl);
7b8518cb 1997 sa_buf_rele(db, tag);
572e2857
BB
1998}
1999
2000/*
2001 * Given an object number, return its parent object number and whether
2002 * or not the object is an extended attribute directory.
2003 */
2004static int
b23ad7f3
JJ
2005zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2006 uint64_t *pobjp, int *is_xattrdir)
572e2857
BB
2007{
2008 uint64_t parent;
2009 uint64_t pflags;
2010 uint64_t mode;
b23ad7f3 2011 uint64_t parent_mode;
572e2857 2012 sa_bulk_attr_t bulk[3];
b23ad7f3
JJ
2013 sa_handle_t *sa_hdl;
2014 dmu_buf_t *sa_db;
572e2857
BB
2015 int count = 0;
2016 int error;
2017
2018 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2019 &parent, sizeof (parent));
428870ff 2020 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
572e2857 2021 &pflags, sizeof (pflags));
428870ff 2022 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
572e2857 2023 &mode, sizeof (mode));
428870ff 2024
572e2857 2025 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
428870ff 2026 return (error);
572e2857 2027
b23ad7f3
JJ
2028 /*
2029 * When a link is removed its parent pointer is not changed and will
2030 * be invalid. There are two cases where a link is removed but the
2031 * file stays around, when it goes to the delete queue and when there
2032 * are additional links.
2033 */
2034 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2035 if (error != 0)
2036 return (error);
2037
2038 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2039 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2040 if (error != 0)
2041 return (error);
2042
428870ff 2043 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
34dc7c2f 2044
b23ad7f3
JJ
2045 /*
2046 * Extended attributes can be applied to files, directories, etc.
2047 * Otherwise the parent must be a directory.
2048 */
2049 if (!*is_xattrdir && !S_ISDIR(parent_mode))
ecb2b7dc 2050 return (SET_ERROR(EINVAL));
b23ad7f3
JJ
2051
2052 *pobjp = parent;
2053
34dc7c2f
BB
2054 return (0);
2055}
2056
572e2857
BB
2057/*
2058 * Given an object number, return some zpl level statistics
2059 */
2060static int
2061zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2062 zfs_stat_t *sb)
34dc7c2f 2063{
572e2857
BB
2064 sa_bulk_attr_t bulk[4];
2065 int count = 0;
2066
2067 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2068 &sb->zs_mode, sizeof (sb->zs_mode));
2069 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2070 &sb->zs_gen, sizeof (sb->zs_gen));
2071 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2072 &sb->zs_links, sizeof (sb->zs_links));
2073 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2074 &sb->zs_ctime, sizeof (sb->zs_ctime));
2075
2076 return (sa_bulk_lookup(hdl, bulk, count));
2077}
2078
2079static int
2080zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2081 sa_attr_type_t *sa_table, char *buf, int len)
2082{
2083 sa_handle_t *sa_hdl;
2084 sa_handle_t *prevhdl = NULL;
2085 dmu_buf_t *prevdb = NULL;
2086 dmu_buf_t *sa_db = NULL;
34dc7c2f
BB
2087 char *path = buf + len - 1;
2088 int error;
2089
2090 *path = '\0';
572e2857 2091 sa_hdl = hdl;
428870ff 2092
64c1dcef
PD
2093 uint64_t deleteq_obj;
2094 VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
2095 ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
2096 error = zap_lookup_int(osp, deleteq_obj, obj);
2097 if (error == 0) {
2098 return (ESTALE);
2099 } else if (error != ENOENT) {
2100 return (error);
2101 }
2102 error = 0;
2103
34dc7c2f 2104 for (;;) {
17897ce2 2105 uint64_t pobj = 0;
34dc7c2f
BB
2106 char component[MAXNAMELEN + 2];
2107 size_t complen;
17897ce2 2108 int is_xattrdir = 0;
34dc7c2f 2109
572e2857 2110 if (prevdb)
7b8518cb 2111 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
572e2857 2112
b23ad7f3 2113 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
572e2857 2114 &is_xattrdir)) != 0)
34dc7c2f
BB
2115 break;
2116
2117 if (pobj == obj) {
2118 if (path[0] != '/')
2119 *--path = '/';
2120 break;
2121 }
2122
2123 component[0] = '/';
2124 if (is_xattrdir) {
2125 (void) sprintf(component + 1, "<xattrdir>");
2126 } else {
2127 error = zap_value_search(osp, pobj, obj,
2128 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2129 if (error != 0)
2130 break;
2131 }
2132
2133 complen = strlen(component);
2134 path -= complen;
2135 ASSERT(path >= buf);
2136 bcopy(component, path, complen);
2137 obj = pobj;
572e2857
BB
2138
2139 if (sa_hdl != hdl) {
2140 prevhdl = sa_hdl;
2141 prevdb = sa_db;
2142 }
7b8518cb 2143 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
572e2857
BB
2144 if (error != 0) {
2145 sa_hdl = prevhdl;
2146 sa_db = prevdb;
2147 break;
2148 }
2149 }
2150
2151 if (sa_hdl != NULL && sa_hdl != hdl) {
2152 ASSERT(sa_db != NULL);
7b8518cb 2153 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
34dc7c2f
BB
2154 }
2155
2156 if (error == 0)
2157 (void) memmove(buf, path, buf + len - path);
428870ff 2158
34dc7c2f
BB
2159 return (error);
2160}
572e2857
BB
2161
2162int
2163zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2164{
2165 sa_attr_type_t *sa_table;
2166 sa_handle_t *hdl;
2167 dmu_buf_t *db;
2168 int error;
2169
2170 error = zfs_sa_setup(osp, &sa_table);
2171 if (error != 0)
2172 return (error);
2173
7b8518cb 2174 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
572e2857
BB
2175 if (error != 0)
2176 return (error);
2177
2178 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2179
7b8518cb 2180 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
2181 return (error);
2182}
2183
2184int
2185zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2186 char *buf, int len)
2187{
2188 char *path = buf + len - 1;
2189 sa_attr_type_t *sa_table;
2190 sa_handle_t *hdl;
2191 dmu_buf_t *db;
2192 int error;
2193
2194 *path = '\0';
2195
2196 error = zfs_sa_setup(osp, &sa_table);
2197 if (error != 0)
2198 return (error);
2199
7b8518cb 2200 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
572e2857
BB
2201 if (error != 0)
2202 return (error);
2203
2204 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2205 if (error != 0) {
7b8518cb 2206 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
2207 return (error);
2208 }
2209
2210 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2211
7b8518cb 2212 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
2213 return (error);
2214}
c28b2279 2215
93ce2b4c 2216#if defined(_KERNEL)
c28b2279
BB
2217EXPORT_SYMBOL(zfs_create_fs);
2218EXPORT_SYMBOL(zfs_obj_to_path);
0720116d 2219
02730c33 2220/* CSTYLED */
0720116d
BB
2221module_param(zfs_object_mutex_size, uint, 0644);
2222MODULE_PARM_DESC(zfs_object_mutex_size, "Size of znode hold array");
dcec0a12
AP
2223module_param(zfs_unlink_suspend_progress, int, 0644);
2224MODULE_PARM_DESC(zfs_unlink_suspend_progress, "Set to prevent async unlinks "
2225"(debug - leaks space into the unlinked set)");
c28b2279 2226#endif