]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_znode.c
Fix 'zfs rollback' on mounted file systems
[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.
34dc7c2f
BB
23 */
24
25/* Portions Copyright 2007 Jeremy Teo */
26
34dc7c2f
BB
27#ifdef _KERNEL
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/time.h>
31#include <sys/systm.h>
32#include <sys/sysmacros.h>
33#include <sys/resource.h>
34#include <sys/mntent.h>
35#include <sys/mkdev.h>
36#include <sys/u8_textprep.h>
37#include <sys/dsl_dataset.h>
38#include <sys/vfs.h>
39#include <sys/vfs_opreg.h>
40#include <sys/vnode.h>
41#include <sys/file.h>
42#include <sys/kmem.h>
43#include <sys/errno.h>
44#include <sys/unistd.h>
45#include <sys/mode.h>
46#include <sys/atomic.h>
47#include <vm/pvn.h>
48#include "fs/fs_subr.h"
49#include <sys/zfs_dir.h>
50#include <sys/zfs_acl.h>
51#include <sys/zfs_ioctl.h>
52#include <sys/zfs_rlock.h>
53#include <sys/zfs_fuid.h>
3558fd73 54#include <sys/zfs_vnops.h>
ebe7e575 55#include <sys/zfs_ctldir.h>
428870ff 56#include <sys/dnode.h>
34dc7c2f
BB
57#include <sys/fs/zfs.h>
58#include <sys/kidmap.h>
3558fd73 59#include <sys/zpl.h>
34dc7c2f
BB
60#endif /* _KERNEL */
61
62#include <sys/dmu.h>
63#include <sys/refcount.h>
64#include <sys/stat.h>
65#include <sys/zap.h>
66#include <sys/zfs_znode.h>
428870ff
BB
67#include <sys/sa.h>
68#include <sys/zfs_sa.h>
572e2857 69#include <sys/zfs_stat.h>
34dc7c2f
BB
70
71#include "zfs_prop.h"
428870ff 72#include "zfs_comutil.h"
34dc7c2f 73
b128c09f
BB
74/*
75 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
76 * turned on when DEBUG is also defined.
77 */
78#ifdef DEBUG
79#define ZNODE_STATS
80#endif /* DEBUG */
81
82#ifdef ZNODE_STATS
83#define ZNODE_STAT_ADD(stat) ((stat)++)
84#else
85#define ZNODE_STAT_ADD(stat) /* nothing */
86#endif /* ZNODE_STATS */
87
34dc7c2f
BB
88/*
89 * Functions needed for userland (ie: libzpool) are not put under
90 * #ifdef_KERNEL; the rest of the functions have dependencies
91 * (such as VFS logic) that will not compile easily in userland.
92 */
93#ifdef _KERNEL
9babb374 94
b128c09f 95static kmem_cache_t *znode_cache = NULL;
34dc7c2f 96
34dc7c2f
BB
97/*ARGSUSED*/
98static int
b128c09f 99zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
34dc7c2f
BB
100{
101 znode_t *zp = buf;
102
3558fd73 103 inode_init_once(ZTOI(zp));
b128c09f
BB
104 list_link_init(&zp->z_link_node);
105
34dc7c2f 106 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
107 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
108 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
109 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
82a37189 110 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
34dc7c2f
BB
111
112 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
113 avl_create(&zp->z_range_avl, zfs_range_compare,
114 sizeof (rl_t), offsetof(rl_t, r_node));
115
b128c09f 116 zp->z_dirlocks = NULL;
45d1cae3 117 zp->z_acl_cached = NULL;
82a37189 118 zp->z_xattr_cached = NULL;
e89260a1 119 zp->z_xattr_parent = NULL;
572e2857 120 zp->z_moved = 0;
34dc7c2f
BB
121 return (0);
122}
123
124/*ARGSUSED*/
125static void
b128c09f 126zfs_znode_cache_destructor(void *buf, void *arg)
34dc7c2f
BB
127{
128 znode_t *zp = buf;
129
b128c09f 130 ASSERT(!list_link_active(&zp->z_link_node));
34dc7c2f 131 mutex_destroy(&zp->z_lock);
34dc7c2f
BB
132 rw_destroy(&zp->z_parent_lock);
133 rw_destroy(&zp->z_name_lock);
134 mutex_destroy(&zp->z_acl_lock);
82a37189 135 rw_destroy(&zp->z_xattr_lock);
34dc7c2f
BB
136 avl_destroy(&zp->z_range_avl);
137 mutex_destroy(&zp->z_range_lock);
138
b128c09f 139 ASSERT(zp->z_dirlocks == NULL);
45d1cae3 140 ASSERT(zp->z_acl_cached == NULL);
82a37189 141 ASSERT(zp->z_xattr_cached == NULL);
e89260a1 142 ASSERT(zp->z_xattr_parent == NULL);
b128c09f
BB
143}
144
34dc7c2f
BB
145void
146zfs_znode_init(void)
147{
148 /*
149 * Initialize zcache
150 */
151 ASSERT(znode_cache == NULL);
152 znode_cache = kmem_cache_create("zfs_znode_cache",
153 sizeof (znode_t), 0, zfs_znode_cache_constructor,
3558fd73 154 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_KMEM);
34dc7c2f
BB
155}
156
157void
158zfs_znode_fini(void)
159{
34dc7c2f
BB
160 /*
161 * Cleanup zcache
162 */
163 if (znode_cache)
164 kmem_cache_destroy(znode_cache);
165 znode_cache = NULL;
166}
167
34dc7c2f 168int
3558fd73 169zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx)
34dc7c2f 170{
3c9609b3 171#ifdef HAVE_SMB_SHARE
9babb374
BB
172 zfs_acl_ids_t acl_ids;
173 vattr_t vattr;
174 znode_t *sharezp;
175 vnode_t *vp;
176 znode_t *zp;
177 int error;
34dc7c2f 178
9babb374 179 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
3558fd73 180 vattr.va_mode = S_IFDIR | 0555;
9babb374
BB
181 vattr.va_uid = crgetuid(kcred);
182 vattr.va_gid = crgetgid(kcred);
34dc7c2f 183
691f6ac4 184 sharezp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
572e2857 185 sharezp->z_moved = 0;
9babb374
BB
186 sharezp->z_unlinked = 0;
187 sharezp->z_atime_dirty = 0;
188 sharezp->z_zfsvfs = zfsvfs;
428870ff 189 sharezp->z_is_sa = zfsvfs->z_use_sa;
34dc7c2f 190
9babb374
BB
191 vp = ZTOV(sharezp);
192 vn_reinit(vp);
193 vp->v_type = VDIR;
34dc7c2f 194
9babb374
BB
195 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
196 kcred, NULL, &acl_ids));
428870ff 197 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
9babb374
BB
198 ASSERT3P(zp, ==, sharezp);
199 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
200 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
201 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
202 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
203 zfsvfs->z_shares_dir = sharezp->z_id;
204
205 zfs_acl_ids_free(&acl_ids);
3558fd73 206 // ZTOV(sharezp)->v_count = 0;
428870ff 207 sa_handle_destroy(sharezp->z_sa_hdl);
9babb374 208 kmem_cache_free(znode_cache, sharezp);
34dc7c2f 209
9babb374 210 return (error);
9ee7fac5
BB
211#else
212 return (0);
3c9609b3 213#endif /* HAVE_SMB_SHARE */
34dc7c2f
BB
214}
215
34dc7c2f 216static void
3558fd73 217zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp,
428870ff 218 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
34dc7c2f 219{
3558fd73 220 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id)));
34dc7c2f
BB
221
222 mutex_enter(&zp->z_lock);
223
428870ff
BB
224 ASSERT(zp->z_sa_hdl == NULL);
225 ASSERT(zp->z_acl_cached == NULL);
226 if (sa_hdl == NULL) {
3558fd73 227 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp,
428870ff
BB
228 SA_HDL_SHARED, &zp->z_sa_hdl));
229 } else {
230 zp->z_sa_hdl = sa_hdl;
231 sa_set_userp(sa_hdl, zp);
232 }
34dc7c2f 233
428870ff 234 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
34dc7c2f 235
34dc7c2f 236 mutex_exit(&zp->z_lock);
34dc7c2f
BB
237}
238
239void
240zfs_znode_dmu_fini(znode_t *zp)
241{
3558fd73 242 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) ||
b128c09f 243 zp->z_unlinked ||
3558fd73 244 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
428870ff
BB
245
246 sa_handle_destroy(zp->z_sa_hdl);
247 zp->z_sa_hdl = NULL;
34dc7c2f
BB
248}
249
250/*
3558fd73
BB
251 * Called by new_inode() to allocate a new inode.
252 */
253int
254zfs_inode_alloc(struct super_block *sb, struct inode **ip)
255{
256 znode_t *zp;
257
691f6ac4 258 zp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
3558fd73
BB
259 *ip = ZTOI(zp);
260
261 return (0);
262}
263
264/*
265 * Called in multiple places when an inode should be destroyed.
266 */
267void
268zfs_inode_destroy(struct inode *ip)
269{
270 znode_t *zp = ITOZ(ip);
271 zfs_sb_t *zsb = ZTOZSB(zp);
272
ebe7e575
BB
273 if (zfsctl_is_node(ip))
274 zfsctl_inode_destroy(ip);
275
3558fd73 276 mutex_enter(&zsb->z_znodes_lock);
7b3e34ba
BB
277 if (list_link_active(&zp->z_link_node)) {
278 list_remove(&zsb->z_all_znodes, zp);
279 zsb->z_nr_znodes--;
280 }
3558fd73
BB
281 mutex_exit(&zsb->z_znodes_lock);
282
283 if (zp->z_acl_cached) {
284 zfs_acl_free(zp->z_acl_cached);
285 zp->z_acl_cached = NULL;
286 }
287
82a37189
BB
288 if (zp->z_xattr_cached) {
289 nvlist_free(zp->z_xattr_cached);
290 zp->z_xattr_cached = NULL;
291 }
292
e89260a1
BB
293 if (zp->z_xattr_parent) {
294 iput(ZTOI(zp->z_xattr_parent));
295 zp->z_xattr_parent = NULL;
296 }
297
3558fd73
BB
298 kmem_cache_free(znode_cache, zp);
299}
300
301static void
302zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip)
303{
aa6d8c10 304 uint64_t rdev = 0;
3558fd73
BB
305
306 switch (ip->i_mode & S_IFMT) {
307 case S_IFREG:
308 ip->i_op = &zpl_inode_operations;
309 ip->i_fop = &zpl_file_operations;
310 ip->i_mapping->a_ops = &zpl_address_space_operations;
311 break;
312
313 case S_IFDIR:
314 ip->i_op = &zpl_dir_inode_operations;
315 ip->i_fop = &zpl_dir_file_operations;
316 ITOZ(ip)->z_zn_prefetch = B_TRUE;
317 break;
318
319 case S_IFLNK:
320 ip->i_op = &zpl_symlink_inode_operations;
321 break;
322
aa6d8c10
NB
323 /*
324 * rdev is only stored in a SA only for device files.
325 */
3558fd73
BB
326 case S_IFCHR:
327 case S_IFBLK:
3558fd73
BB
328 VERIFY(sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb),
329 &rdev, sizeof (rdev)) == 0);
aa6d8c10
NB
330 /*FALLTHROUGH*/
331 case S_IFIFO:
332 case S_IFSOCK:
3558fd73
BB
333 init_special_inode(ip, ip->i_mode, rdev);
334 ip->i_op = &zpl_special_inode_operations;
335 break;
336
337 default:
338 printk("ZFS: Invalid mode: 0x%x\n", ip->i_mode);
339 VERIFY(0);
340 }
341}
342
343/*
344 * Construct a znode+inode and initialize.
34dc7c2f
BB
345 *
346 * This does not do a call to dmu_set_user() that is
347 * up to the caller to do, in case you don't want to
348 * return the znode
349 */
350static znode_t *
3558fd73 351zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
c85b224f 352 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl,
7b3e34ba 353 struct inode *dip)
34dc7c2f
BB
354{
355 znode_t *zp;
3558fd73 356 struct inode *ip;
428870ff 357 uint64_t parent;
428870ff
BB
358 sa_bulk_attr_t bulk[9];
359 int count = 0;
34dc7c2f 360
3558fd73 361 ASSERT(zsb != NULL);
34dc7c2f 362
3558fd73
BB
363 ip = new_inode(zsb->z_sb);
364 if (ip == NULL)
365 return (NULL);
7304b6e5 366
3558fd73 367 zp = ITOZ(ip);
34dc7c2f 368 ASSERT(zp->z_dirlocks == NULL);
ebe7e575
BB
369 ASSERT3P(zp->z_acl_cached, ==, NULL);
370 ASSERT3P(zp->z_xattr_cached, ==, NULL);
e89260a1 371 ASSERT3P(zp->z_xattr_parent, ==, NULL);
572e2857 372 zp->z_moved = 0;
428870ff 373 zp->z_sa_hdl = NULL;
34dc7c2f
BB
374 zp->z_unlinked = 0;
375 zp->z_atime_dirty = 0;
376 zp->z_mapcnt = 0;
34dc7c2f
BB
377 zp->z_id = db->db_object;
378 zp->z_blksz = blksz;
379 zp->z_seq = 0x7A4653;
380 zp->z_sync_cnt = 0;
ebe7e575
BB
381 zp->z_is_zvol = B_FALSE;
382 zp->z_is_mapped = B_FALSE;
383 zp->z_is_ctldir = B_FALSE;
7b3e34ba 384 zp->z_is_stale = B_FALSE;
34dc7c2f 385
3558fd73
BB
386 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
387
388 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &zp->z_mode, 8);
389 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
390 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
392 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
428870ff 393 &zp->z_pflags, 8);
3558fd73 394 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
7304b6e5 395 &parent, 8);
3558fd73 396 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
428870ff 397 &zp->z_atime, 16);
3558fd73
BB
398 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
399 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
428870ff
BB
400
401 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
402 if (hdl == NULL)
403 sa_handle_destroy(zp->z_sa_hdl);
34dc7c2f 404
3558fd73 405 goto error;
34dc7c2f 406 }
7304b6e5 407
e89260a1
BB
408 /*
409 * xattr znodes hold a reference on their unique parent
410 */
411 if (dip && zp->z_pflags & ZFS_XATTR) {
412 igrab(dip);
413 zp->z_xattr_parent = ITOZ(dip);
414 }
415
3558fd73 416 ip->i_ino = obj;
5f35b190 417 zfs_inode_update(zp);
3558fd73
BB
418 zfs_inode_set_ops(zsb, ip);
419
7b3e34ba
BB
420 /*
421 * The only way insert_inode_locked() can fail is if the ip->i_ino
422 * number is already hashed for this super block. This can never
423 * happen because the inode numbers map 1:1 with the object numbers.
424 *
425 * The one exception is rolling back a mounted file system, but in
426 * this case all the active inode are unhashed during the rollback.
427 */
428 VERIFY3S(insert_inode_locked(ip), ==, 0);
c85b224f 429
3558fd73
BB
430 mutex_enter(&zsb->z_znodes_lock);
431 list_insert_tail(&zsb->z_all_znodes, zp);
ab26409d 432 zsb->z_nr_znodes++;
b128c09f 433 membar_producer();
3558fd73 434 mutex_exit(&zsb->z_znodes_lock);
b128c09f 435
3558fd73 436 unlock_new_inode(ip);
34dc7c2f 437 return (zp);
3558fd73
BB
438
439error:
440 unlock_new_inode(ip);
441 iput(ip);
442 return NULL;
34dc7c2f
BB
443}
444
960e08fe
BB
445/*
446 * Update the embedded inode given the znode. We should work toward
447 * eliminating this function as soon as possible by removing values
448 * which are duplicated between the znode and inode. If the generic
449 * inode has the correct field it should be used, and the ZFS code
450 * updated to access the inode. This can be done incrementally.
451 */
452void
453zfs_inode_update(znode_t *zp)
454{
3558fd73
BB
455 zfs_sb_t *zsb;
456 struct inode *ip;
960e08fe
BB
457 uint32_t blksize;
458 uint64_t atime[2], mtime[2], ctime[2];
459
460 ASSERT(zp != NULL);
3558fd73
BB
461 zsb = ZTOZSB(zp);
462 ip = ZTOI(zp);
463
ebe7e575
BB
464 /* Skip .zfs control nodes which do not exist on disk. */
465 if (zfsctl_is_node(ip))
466 return;
467
3558fd73
BB
468 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
469 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
470 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
471
472 spin_lock(&ip->i_lock);
473 ip->i_generation = zp->z_gen;
474 ip->i_uid = zp->z_uid;
475 ip->i_gid = zp->z_gid;
28eb9213 476 set_nlink(ip, zp->z_links);
3558fd73
BB
477 ip->i_mode = zp->z_mode;
478 ip->i_blkbits = SPA_MINBLOCKSHIFT;
960e08fe 479 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
3558fd73 480 (u_longlong_t *)&ip->i_blocks);
960e08fe 481
3558fd73
BB
482 ZFS_TIME_DECODE(&ip->i_atime, atime);
483 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
484 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
960e08fe 485
3558fd73
BB
486 i_size_write(ip, zp->z_size);
487 spin_unlock(&ip->i_lock);
960e08fe
BB
488}
489
428870ff
BB
490static uint64_t empty_xattr;
491static uint64_t pad[4];
492static zfs_acl_phys_t acl_phys;
34dc7c2f
BB
493/*
494 * Create a new DMU object to hold a zfs znode.
495 *
496 * IN: dzp - parent directory for new znode
497 * vap - file attributes for new znode
498 * tx - dmu transaction id for zap operations
499 * cr - credentials of caller
500 * flag - flags:
501 * IS_ROOT_NODE - new object will be root
502 * IS_XATTR - new object is an attribute
34dc7c2f
BB
503 * bonuslen - length of bonus buffer
504 * setaclp - File/Dir initial ACL
505 * fuidp - Tracks fuid allocation.
506 *
507 * OUT: zpp - allocated znode
508 *
509 */
510void
511zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
428870ff 512 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
34dc7c2f 513{
428870ff
BB
514 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
515 uint64_t mode, size, links, parent, pflags;
572e2857 516 uint64_t dzp_pflags = 0;
428870ff 517 uint64_t rdev = 0;
3558fd73 518 zfs_sb_t *zsb = ZTOZSB(dzp);
428870ff 519 dmu_buf_t *db;
34dc7c2f
BB
520 timestruc_t now;
521 uint64_t gen, obj;
522 int err;
428870ff
BB
523 int bonuslen;
524 sa_handle_t *sa_hdl;
525 dmu_object_type_t obj_type;
f30484af 526 sa_bulk_attr_t *sa_attrs;
428870ff
BB
527 int cnt = 0;
528 zfs_acl_locator_cb_t locate = { 0 };
34dc7c2f 529
3558fd73 530 if (zsb->z_replay) {
34dc7c2f 531 obj = vap->va_nodeid;
34dc7c2f
BB
532 now = vap->va_ctime; /* see zfs_replay_create() */
533 gen = vap->va_nblocks; /* ditto */
534 } else {
535 obj = 0;
536 gethrestime(&now);
537 gen = dmu_tx_get_txg(tx);
538 }
539
3558fd73 540 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
428870ff
BB
541 bonuslen = (obj_type == DMU_OT_SA) ?
542 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
543
34dc7c2f
BB
544 /*
545 * Create a new DMU object.
546 */
547 /*
548 * There's currently no mechanism for pre-reading the blocks that will
572e2857 549 * be needed to allocate a new object, so we accept the small chance
34dc7c2f
BB
550 * that there will be an i/o error and we will fail one of the
551 * assertions below.
552 */
3558fd73
BB
553 if (S_ISDIR(vap->va_mode)) {
554 if (zsb->z_replay) {
555 err = zap_create_claim_norm(zsb->z_os, obj,
556 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
428870ff 557 obj_type, bonuslen, tx);
34dc7c2f
BB
558 ASSERT3U(err, ==, 0);
559 } else {
3558fd73
BB
560 obj = zap_create_norm(zsb->z_os,
561 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
428870ff 562 obj_type, bonuslen, tx);
34dc7c2f
BB
563 }
564 } else {
3558fd73
BB
565 if (zsb->z_replay) {
566 err = dmu_object_claim(zsb->z_os, obj,
34dc7c2f 567 DMU_OT_PLAIN_FILE_CONTENTS, 0,
428870ff 568 obj_type, bonuslen, tx);
34dc7c2f
BB
569 ASSERT3U(err, ==, 0);
570 } else {
3558fd73 571 obj = dmu_object_alloc(zsb->z_os,
34dc7c2f 572 DMU_OT_PLAIN_FILE_CONTENTS, 0,
428870ff 573 obj_type, bonuslen, tx);
34dc7c2f
BB
574 }
575 }
34dc7c2f 576
3558fd73
BB
577 ZFS_OBJ_HOLD_ENTER(zsb, obj);
578 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
34dc7c2f
BB
579
580 /*
581 * If this is the root, fix up the half-initialized parent pointer
582 * to reference the just-allocated physical data area.
583 */
584 if (flag & IS_ROOT_NODE) {
34dc7c2f 585 dzp->z_id = obj;
428870ff
BB
586 } else {
587 dzp_pflags = dzp->z_pflags;
34dc7c2f
BB
588 }
589
590 /*
591 * If parent is an xattr, so am I.
592 */
428870ff 593 if (dzp_pflags & ZFS_XATTR) {
34dc7c2f 594 flag |= IS_XATTR;
34dc7c2f
BB
595 }
596
3558fd73 597 if (zsb->z_use_fuids)
428870ff
BB
598 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
599 else
600 pflags = 0;
34dc7c2f 601
3558fd73 602 if (S_ISDIR(vap->va_mode)) {
428870ff
BB
603 size = 2; /* contents ("." and "..") */
604 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
605 } else {
606 size = links = 0;
34dc7c2f
BB
607 }
608
aa6d8c10 609 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
dc1d7665 610 rdev = vap->va_rdev;
428870ff
BB
611
612 parent = dzp->z_id;
613 mode = acl_ids->z_mode;
34dc7c2f 614 if (flag & IS_XATTR)
428870ff 615 pflags |= ZFS_XATTR;
34dc7c2f 616
428870ff
BB
617 /*
618 * No execs denied will be deterimed when zfs_mode_compute() is called.
619 */
620 pflags |= acl_ids->z_aclp->z_hints &
621 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
622 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
34dc7c2f 623
428870ff
BB
624 ZFS_TIME_ENCODE(&now, crtime);
625 ZFS_TIME_ENCODE(&now, ctime);
34dc7c2f 626
3558fd73 627 if (vap->va_mask & ATTR_ATIME) {
428870ff 628 ZFS_TIME_ENCODE(&vap->va_atime, atime);
34dc7c2f 629 } else {
428870ff 630 ZFS_TIME_ENCODE(&now, atime);
34dc7c2f
BB
631 }
632
3558fd73 633 if (vap->va_mask & ATTR_MTIME) {
428870ff
BB
634 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
635 } else {
636 ZFS_TIME_ENCODE(&now, mtime);
637 }
638
639 /* Now add in all of the "SA" attributes */
3558fd73 640 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
428870ff
BB
641 &sa_hdl));
642
643 /*
644 * Setup the array of attributes to be replaced/set on the new file
645 *
646 * order for DMU_OT_ZNODE is critical since it needs to be constructed
647 * in the old znode_phys_t format. Don't change this ordering
648 */
b8d06fca 649 sa_attrs = kmem_alloc(sizeof(sa_bulk_attr_t) * ZPL_END, KM_PUSHPAGE);
428870ff
BB
650
651 if (obj_type == DMU_OT_ZNODE) {
3558fd73 652 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
428870ff 653 NULL, &atime, 16);
3558fd73 654 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
428870ff 655 NULL, &mtime, 16);
3558fd73 656 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
428870ff 657 NULL, &ctime, 16);
3558fd73 658 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
428870ff 659 NULL, &crtime, 16);
3558fd73 660 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
428870ff 661 NULL, &gen, 8);
3558fd73 662 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
428870ff 663 NULL, &mode, 8);
3558fd73 664 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
428870ff 665 NULL, &size, 8);
3558fd73 666 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
428870ff 667 NULL, &parent, 8);
34dc7c2f 668 } else {
3558fd73 669 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
428870ff 670 NULL, &mode, 8);
3558fd73 671 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
428870ff 672 NULL, &size, 8);
3558fd73 673 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
428870ff 674 NULL, &gen, 8);
3558fd73
BB
675 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
676 NULL, &acl_ids->z_fuid, 8);
677 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
678 NULL, &acl_ids->z_fgid, 8);
679 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
428870ff 680 NULL, &parent, 8);
3558fd73 681 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
428870ff 682 NULL, &pflags, 8);
3558fd73 683 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
428870ff 684 NULL, &atime, 16);
3558fd73 685 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
428870ff 686 NULL, &mtime, 16);
3558fd73 687 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
428870ff 688 NULL, &ctime, 16);
3558fd73 689 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
428870ff
BB
690 NULL, &crtime, 16);
691 }
692
3558fd73 693 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
428870ff
BB
694
695 if (obj_type == DMU_OT_ZNODE) {
3558fd73 696 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
428870ff 697 &empty_xattr, 8);
34dc7c2f 698 }
428870ff 699 if (obj_type == DMU_OT_ZNODE ||
aa6d8c10 700 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
3558fd73 701 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
428870ff 702 NULL, &rdev, 8);
428870ff
BB
703 }
704 if (obj_type == DMU_OT_ZNODE) {
3558fd73 705 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
428870ff 706 NULL, &pflags, 8);
3558fd73 707 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
428870ff 708 &acl_ids->z_fuid, 8);
3558fd73 709 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
428870ff 710 &acl_ids->z_fgid, 8);
3558fd73 711 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
428870ff 712 sizeof (uint64_t) * 4);
3558fd73 713 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
428870ff
BB
714 &acl_phys, sizeof (zfs_acl_phys_t));
715 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
3558fd73 716 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
428870ff
BB
717 &acl_ids->z_aclp->z_acl_count, 8);
718 locate.cb_aclp = acl_ids->z_aclp;
3558fd73 719 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
428870ff
BB
720 zfs_acl_data_locator, &locate,
721 acl_ids->z_aclp->z_acl_bytes);
722 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
723 acl_ids->z_fuid, acl_ids->z_fgid);
724 }
725
726 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
34dc7c2f 727
34dc7c2f 728 if (!(flag & IS_ROOT_NODE)) {
c85b224f 729 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
7b3e34ba
BB
730 ZTOI(dzp));
731 VERIFY(*zpp != NULL);
732 VERIFY(dzp != NULL);
34dc7c2f
BB
733 } else {
734 /*
735 * If we are creating the root node, the "parent" we
736 * passed in is the znode for the root.
737 */
738 *zpp = dzp;
428870ff
BB
739
740 (*zpp)->z_sa_hdl = sa_hdl;
34dc7c2f 741 }
428870ff
BB
742
743 (*zpp)->z_pflags = pflags;
744 (*zpp)->z_mode = mode;
745
428870ff
BB
746 if (obj_type == DMU_OT_ZNODE ||
747 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
748 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
149e873a 749 ASSERT3S(err, ==, 0);
428870ff 750 }
f30484af 751 kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * ZPL_END);
3558fd73 752 ZFS_OBJ_HOLD_EXIT(zsb, obj);
34dc7c2f
BB
753}
754
5484965a
BB
755/*
756 * zfs_xvattr_set only updates the in-core attributes
757 * it is assumed the caller will be doing an sa_bulk_update
758 * to push the changes out
759 */
760void
761zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
762{
763 xoptattr_t *xoap;
764
765 xoap = xva_getxoptattr(xvap);
766 ASSERT(xoap);
767
768 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
769 uint64_t times[2];
770 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
771 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
772 &times, sizeof (times), tx);
773 XVA_SET_RTN(xvap, XAT_CREATETIME);
774 }
775 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
776 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
777 zp->z_pflags, tx);
778 XVA_SET_RTN(xvap, XAT_READONLY);
779 }
780 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
781 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
782 zp->z_pflags, tx);
783 XVA_SET_RTN(xvap, XAT_HIDDEN);
784 }
785 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
786 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
787 zp->z_pflags, tx);
788 XVA_SET_RTN(xvap, XAT_SYSTEM);
789 }
790 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
791 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
792 zp->z_pflags, tx);
793 XVA_SET_RTN(xvap, XAT_ARCHIVE);
794 }
795 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
796 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
797 zp->z_pflags, tx);
798 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
799 }
800 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
801 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
802 zp->z_pflags, tx);
803 XVA_SET_RTN(xvap, XAT_NOUNLINK);
804 }
805 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
806 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
807 zp->z_pflags, tx);
808 XVA_SET_RTN(xvap, XAT_APPENDONLY);
809 }
810 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
811 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
812 zp->z_pflags, tx);
813 XVA_SET_RTN(xvap, XAT_NODUMP);
814 }
815 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
816 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
817 zp->z_pflags, tx);
818 XVA_SET_RTN(xvap, XAT_OPAQUE);
819 }
820 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
821 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
822 xoap->xoa_av_quarantined, zp->z_pflags, tx);
823 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
824 }
825 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
826 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
827 zp->z_pflags, tx);
828 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
829 }
830 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
831 zfs_sa_set_scanstamp(zp, xvap, tx);
832 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
833 }
834 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
835 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
836 zp->z_pflags, tx);
837 XVA_SET_RTN(xvap, XAT_REPARSE);
838 }
839 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
840 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
841 zp->z_pflags, tx);
842 XVA_SET_RTN(xvap, XAT_OFFLINE);
843 }
844 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
845 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
846 zp->z_pflags, tx);
847 XVA_SET_RTN(xvap, XAT_SPARSE);
848 }
849}
850
34dc7c2f 851int
3558fd73 852zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
34dc7c2f
BB
853{
854 dmu_object_info_t doi;
855 dmu_buf_t *db;
856 znode_t *zp;
857 int err;
428870ff 858 sa_handle_t *hdl;
36df2843 859 struct inode *ip;
34dc7c2f
BB
860
861 *zpp = NULL;
862
36df2843
GB
863again:
864 ip = ilookup(zsb->z_sb, obj_num);
865
3558fd73 866 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
34dc7c2f 867
3558fd73 868 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
34dc7c2f 869 if (err) {
3558fd73 870 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
36df2843 871 iput(ip);
34dc7c2f
BB
872 return (err);
873 }
874
875 dmu_object_info_from_db(db, &doi);
428870ff
BB
876 if (doi.doi_bonus_type != DMU_OT_SA &&
877 (doi.doi_bonus_type != DMU_OT_ZNODE ||
878 (doi.doi_bonus_type == DMU_OT_ZNODE &&
879 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
880 sa_buf_rele(db, NULL);
3558fd73 881 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
36df2843 882 iput(ip);
34dc7c2f
BB
883 return (EINVAL);
884 }
885
428870ff
BB
886 hdl = dmu_buf_get_user(db);
887 if (hdl != NULL) {
36df2843
GB
888 if (ip == NULL) {
889 /*
890 * ilookup returned NULL, which means
891 * the znode is dying - but the SA handle isn't
892 * quite dead yet, we need to drop any locks
893 * we're holding, re-schedule the task and try again.
894 */
895 sa_buf_rele(db, NULL);
896 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
897
898 schedule();
899 goto again;
900 }
428870ff 901
36df2843 902 zp = sa_get_userdata(hdl);
34dc7c2f
BB
903
904 /*
428870ff
BB
905 * Since "SA" does immediate eviction we
906 * should never find a sa handle that doesn't
907 * know about the znode.
34dc7c2f 908 */
428870ff
BB
909
910 ASSERT3P(zp, !=, NULL);
911
912 mutex_enter(&zp->z_lock);
34dc7c2f
BB
913 ASSERT3U(zp->z_id, ==, obj_num);
914 if (zp->z_unlinked) {
915 err = ENOENT;
916 } else {
3558fd73 917 igrab(ZTOI(zp));
34dc7c2f
BB
918 *zpp = zp;
919 err = 0;
920 }
428870ff 921 sa_buf_rele(db, NULL);
34dc7c2f 922 mutex_exit(&zp->z_lock);
3558fd73 923 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
36df2843 924 iput(ip);
34dc7c2f
BB
925 return (err);
926 }
927
36df2843
GB
928 ASSERT3P(ip, ==, NULL);
929
34dc7c2f 930 /*
3558fd73 931 * Not found create new znode/vnode but only if file exists.
428870ff
BB
932 *
933 * There is a small window where zfs_vget() could
934 * find this object while a file create is still in
935 * progress. This is checked for in zfs_znode_alloc()
936 *
937 * if zfs_znode_alloc() fails it will drop the hold on the
938 * bonus buffer.
34dc7c2f 939 */
3558fd73 940 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
7b3e34ba 941 doi.doi_bonus_type, obj_num, NULL, NULL);
428870ff
BB
942 if (zp == NULL) {
943 err = ENOENT;
944 } else {
945 *zpp = zp;
946 }
3558fd73 947 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
428870ff 948 return (err);
34dc7c2f
BB
949}
950
951int
952zfs_rezget(znode_t *zp)
953{
3558fd73 954 zfs_sb_t *zsb = ZTOZSB(zp);
34dc7c2f
BB
955 dmu_object_info_t doi;
956 dmu_buf_t *db;
957 uint64_t obj_num = zp->z_id;
428870ff 958 uint64_t mode;
428870ff 959 sa_bulk_attr_t bulk[8];
34dc7c2f 960 int err;
428870ff
BB
961 int count = 0;
962 uint64_t gen;
34dc7c2f 963
3558fd73 964 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
34dc7c2f 965
428870ff
BB
966 mutex_enter(&zp->z_acl_lock);
967 if (zp->z_acl_cached) {
968 zfs_acl_free(zp->z_acl_cached);
969 zp->z_acl_cached = NULL;
970 }
428870ff 971 mutex_exit(&zp->z_acl_lock);
7b3e34ba
BB
972
973 rw_enter(&zp->z_xattr_lock, RW_WRITER);
974 if (zp->z_xattr_cached) {
975 nvlist_free(zp->z_xattr_cached);
976 zp->z_xattr_cached = NULL;
977 }
978
979 if (zp->z_xattr_parent) {
980 iput(ZTOI(zp->z_xattr_parent));
981 zp->z_xattr_parent = NULL;
982 }
983 rw_exit(&zp->z_xattr_lock);
984
428870ff 985 ASSERT(zp->z_sa_hdl == NULL);
3558fd73 986 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
34dc7c2f 987 if (err) {
3558fd73 988 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
34dc7c2f
BB
989 return (err);
990 }
991
992 dmu_object_info_from_db(db, &doi);
428870ff
BB
993 if (doi.doi_bonus_type != DMU_OT_SA &&
994 (doi.doi_bonus_type != DMU_OT_ZNODE ||
995 (doi.doi_bonus_type == DMU_OT_ZNODE &&
996 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
997 sa_buf_rele(db, NULL);
3558fd73 998 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
34dc7c2f
BB
999 return (EINVAL);
1000 }
1001
3558fd73 1002 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
428870ff
BB
1003
1004 /* reload cached values */
3558fd73 1005 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
428870ff 1006 &gen, sizeof (gen));
3558fd73 1007 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
428870ff 1008 &zp->z_size, sizeof (zp->z_size));
3558fd73 1009 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
428870ff 1010 &zp->z_links, sizeof (zp->z_links));
3558fd73 1011 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
428870ff 1012 &zp->z_pflags, sizeof (zp->z_pflags));
3558fd73 1013 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
428870ff 1014 &zp->z_atime, sizeof (zp->z_atime));
3558fd73 1015 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
572e2857 1016 &zp->z_uid, sizeof (zp->z_uid));
3558fd73 1017 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
572e2857 1018 &zp->z_gid, sizeof (zp->z_gid));
3558fd73 1019 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
428870ff
BB
1020 &mode, sizeof (mode));
1021
428870ff
BB
1022 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1023 zfs_znode_dmu_fini(zp);
3558fd73 1024 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
428870ff
BB
1025 return (EIO);
1026 }
1027
572e2857
BB
1028 zp->z_mode = mode;
1029
428870ff
BB
1030 if (gen != zp->z_gen) {
1031 zfs_znode_dmu_fini(zp);
3558fd73 1032 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
34dc7c2f
BB
1033 return (EIO);
1034 }
1035
428870ff 1036 zp->z_unlinked = (zp->z_links == 0);
34dc7c2f 1037 zp->z_blksz = doi.doi_data_block_size;
7b3e34ba 1038 zfs_inode_update(zp);
34dc7c2f 1039
3558fd73 1040 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
34dc7c2f
BB
1041
1042 return (0);
1043}
1044
1045void
1046zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1047{
3558fd73
BB
1048 zfs_sb_t *zsb = ZTOZSB(zp);
1049 objset_t *os = zsb->z_os;
34dc7c2f 1050 uint64_t obj = zp->z_id;
572e2857 1051 uint64_t acl_obj = zfs_external_acl(zp);
34dc7c2f 1052
3558fd73 1053 ZFS_OBJ_HOLD_ENTER(zsb, obj);
572e2857
BB
1054 if (acl_obj) {
1055 VERIFY(!zp->z_is_sa);
b128c09f 1056 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
572e2857 1057 }
b128c09f 1058 VERIFY(0 == dmu_object_free(os, obj, tx));
34dc7c2f 1059 zfs_znode_dmu_fini(zp);
3558fd73 1060 ZFS_OBJ_HOLD_EXIT(zsb, obj);
34dc7c2f
BB
1061}
1062
1063void
1064zfs_zinactive(znode_t *zp)
1065{
3558fd73 1066 zfs_sb_t *zsb = ZTOZSB(zp);
34dc7c2f 1067 uint64_t z_id = zp->z_id;
d6bd8eaa 1068 boolean_t drop_mutex = 0;
34dc7c2f 1069
428870ff 1070 ASSERT(zp->z_sa_hdl);
34dc7c2f
BB
1071
1072 /*
d6bd8eaa
BB
1073 * Don't allow a zfs_zget() while were trying to release this znode.
1074 *
1075 * Linux allows direct memory reclaim which means that any KM_SLEEP
1076 * allocation may trigger inode eviction. This can lead to a deadlock
1077 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1078 * zfs_zinactive() call path. To avoid this deadlock the process
1079 * must not reacquire the mutex when it is already holding it.
34dc7c2f 1080 */
d6bd8eaa
BB
1081 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1082 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1083 drop_mutex = 1;
1084 }
1085
34dc7c2f 1086 mutex_enter(&zp->z_lock);
34dc7c2f
BB
1087
1088 /*
1089 * If this was the last reference to a file with no links,
1090 * remove the file from the file system.
1091 */
1092 if (zp->z_unlinked) {
1093 mutex_exit(&zp->z_lock);
d6bd8eaa
BB
1094
1095 if (drop_mutex)
1096 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1097
34dc7c2f
BB
1098 zfs_rmnode(zp);
1099 return;
1100 }
428870ff 1101
34dc7c2f
BB
1102 mutex_exit(&zp->z_lock);
1103 zfs_znode_dmu_fini(zp);
d6bd8eaa
BB
1104
1105 if (drop_mutex)
1106 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
34dc7c2f
BB
1107}
1108
1109void
428870ff
BB
1110zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1111 uint64_t ctime[2], boolean_t have_tx)
34dc7c2f
BB
1112{
1113 timestruc_t now;
1114
34dc7c2f
BB
1115 gethrestime(&now);
1116
428870ff 1117 if (have_tx) { /* will sa_bulk_update happen really soon? */
34dc7c2f
BB
1118 zp->z_atime_dirty = 0;
1119 zp->z_seq++;
1120 } else {
1121 zp->z_atime_dirty = 1;
1122 }
1123
3558fd73 1124 if (flag & ATTR_ATIME) {
428870ff
BB
1125 ZFS_TIME_ENCODE(&now, zp->z_atime);
1126 }
34dc7c2f 1127
3558fd73 1128 if (flag & ATTR_MTIME) {
428870ff 1129 ZFS_TIME_ENCODE(&now, mtime);
3558fd73 1130 if (ZTOZSB(zp)->z_use_fuids) {
428870ff
BB
1131 zp->z_pflags |= (ZFS_ARCHIVE |
1132 ZFS_AV_MODIFIED);
1133 }
34dc7c2f
BB
1134 }
1135
3558fd73 1136 if (flag & ATTR_CTIME) {
428870ff 1137 ZFS_TIME_ENCODE(&now, ctime);
3558fd73 1138 if (ZTOZSB(zp)->z_use_fuids)
428870ff 1139 zp->z_pflags |= ZFS_ARCHIVE;
34dc7c2f
BB
1140 }
1141}
1142
34dc7c2f
BB
1143/*
1144 * Grow the block size for a file.
1145 *
1146 * IN: zp - znode of file to free data in.
1147 * size - requested block size
1148 * tx - open transaction.
1149 *
1150 * NOTE: this function assumes that the znode is write locked.
1151 */
1152void
1153zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1154{
1155 int error;
1156 u_longlong_t dummy;
1157
1158 if (size <= zp->z_blksz)
1159 return;
1160 /*
1161 * If the file size is already greater than the current blocksize,
1162 * we will not grow. If there is more than one block in a file,
1163 * the blocksize cannot change.
1164 */
428870ff 1165 if (zp->z_blksz && zp->z_size > zp->z_blksz)
34dc7c2f
BB
1166 return;
1167
3558fd73 1168 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
34dc7c2f 1169 size, 0, tx);
428870ff 1170
34dc7c2f
BB
1171 if (error == ENOTSUP)
1172 return;
1173 ASSERT3U(error, ==, 0);
1174
1175 /* What blocksize did we actually get? */
428870ff 1176 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
34dc7c2f
BB
1177}
1178
34dc7c2f 1179/*
b128c09f 1180 * Increase the file length
34dc7c2f
BB
1181 *
1182 * IN: zp - znode of file to free data in.
b128c09f 1183 * end - new end-of-file
34dc7c2f
BB
1184 *
1185 * RETURN: 0 if success
1186 * error code if failure
1187 */
b128c09f
BB
1188static int
1189zfs_extend(znode_t *zp, uint64_t end)
34dc7c2f 1190{
3558fd73 1191 zfs_sb_t *zsb = ZTOZSB(zp);
b128c09f 1192 dmu_tx_t *tx;
34dc7c2f 1193 rl_t *rl;
b128c09f 1194 uint64_t newblksz;
34dc7c2f
BB
1195 int error;
1196
34dc7c2f 1197 /*
b128c09f 1198 * We will change zp_size, lock the whole file.
34dc7c2f 1199 */
b128c09f 1200 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
34dc7c2f
BB
1201
1202 /*
1203 * Nothing to do if file already at desired length.
1204 */
428870ff 1205 if (end <= zp->z_size) {
34dc7c2f
BB
1206 zfs_range_unlock(rl);
1207 return (0);
1208 }
b128c09f 1209top:
3558fd73 1210 tx = dmu_tx_create(zsb->z_os);
428870ff
BB
1211 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1212 zfs_sa_upgrade_txholds(tx, zp);
b128c09f 1213 if (end > zp->z_blksz &&
3558fd73 1214 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
34dc7c2f
BB
1215 /*
1216 * We are growing the file past the current block size.
1217 */
3558fd73 1218 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
34dc7c2f 1219 ASSERT(!ISP2(zp->z_blksz));
b128c09f 1220 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
34dc7c2f 1221 } else {
3558fd73 1222 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
34dc7c2f 1223 }
b128c09f
BB
1224 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1225 } else {
1226 newblksz = 0;
34dc7c2f
BB
1227 }
1228
fb5f0bc8 1229 error = dmu_tx_assign(tx, TXG_NOWAIT);
34dc7c2f 1230 if (error) {
fb5f0bc8 1231 if (error == ERESTART) {
34dc7c2f 1232 dmu_tx_wait(tx);
b128c09f
BB
1233 dmu_tx_abort(tx);
1234 goto top;
1235 }
34dc7c2f
BB
1236 dmu_tx_abort(tx);
1237 zfs_range_unlock(rl);
1238 return (error);
1239 }
1240
b128c09f
BB
1241 if (newblksz)
1242 zfs_grow_blocksize(zp, newblksz, tx);
34dc7c2f 1243
428870ff
BB
1244 zp->z_size = end;
1245
3558fd73 1246 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
428870ff 1247 &zp->z_size, sizeof (zp->z_size), tx));
34dc7c2f 1248
b128c09f 1249 zfs_range_unlock(rl);
34dc7c2f 1250
b128c09f 1251 dmu_tx_commit(tx);
34dc7c2f 1252
b128c09f
BB
1253 return (0);
1254}
1255
1256/*
1257 * Free space in a file.
1258 *
1259 * IN: zp - znode of file to free data in.
1260 * off - start of section to free.
1261 * len - length of section to free.
1262 *
3558fd73 1263 * RETURN: 0 if success
b128c09f
BB
1264 * error code if failure
1265 */
1266static int
1267zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1268{
3558fd73 1269 zfs_sb_t *zsb = ZTOZSB(zp);
b128c09f
BB
1270 rl_t *rl;
1271 int error;
1272
1273 /*
1274 * Lock the range being freed.
1275 */
1276 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1277
1278 /*
1279 * Nothing to do if file already at desired length.
1280 */
428870ff 1281 if (off >= zp->z_size) {
b128c09f
BB
1282 zfs_range_unlock(rl);
1283 return (0);
34dc7c2f
BB
1284 }
1285
428870ff
BB
1286 if (off + len > zp->z_size)
1287 len = zp->z_size - off;
b128c09f 1288
3558fd73 1289 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
b128c09f 1290
34dc7c2f
BB
1291 zfs_range_unlock(rl);
1292
b128c09f
BB
1293 return (error);
1294}
1295
1296/*
1297 * Truncate a file
1298 *
1299 * IN: zp - znode of file to free data in.
1300 * end - new end-of-file.
1301 *
3558fd73 1302 * RETURN: 0 if success
b128c09f
BB
1303 * error code if failure
1304 */
1305static int
1306zfs_trunc(znode_t *zp, uint64_t end)
1307{
3558fd73 1308 zfs_sb_t *zsb = ZTOZSB(zp);
b128c09f
BB
1309 dmu_tx_t *tx;
1310 rl_t *rl;
1311 int error;
572e2857
BB
1312 sa_bulk_attr_t bulk[2];
1313 int count = 0;
b128c09f
BB
1314
1315 /*
1316 * We will change zp_size, lock the whole file.
1317 */
1318 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1319
1320 /*
1321 * Nothing to do if file already at desired length.
1322 */
428870ff 1323 if (end >= zp->z_size) {
b128c09f
BB
1324 zfs_range_unlock(rl);
1325 return (0);
1326 }
1327
3558fd73 1328 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
b128c09f
BB
1329 if (error) {
1330 zfs_range_unlock(rl);
1331 return (error);
1332 }
1333top:
3558fd73 1334 tx = dmu_tx_create(zsb->z_os);
428870ff
BB
1335 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1336 zfs_sa_upgrade_txholds(tx, zp);
fb5f0bc8 1337 error = dmu_tx_assign(tx, TXG_NOWAIT);
b128c09f 1338 if (error) {
fb5f0bc8 1339 if (error == ERESTART) {
b128c09f
BB
1340 dmu_tx_wait(tx);
1341 dmu_tx_abort(tx);
1342 goto top;
1343 }
1344 dmu_tx_abort(tx);
1345 zfs_range_unlock(rl);
1346 return (error);
1347 }
b128c09f 1348
428870ff 1349 zp->z_size = end;
3558fd73 1350 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
572e2857 1351 NULL, &zp->z_size, sizeof (zp->z_size));
428870ff 1352
572e2857
BB
1353 if (end == 0) {
1354 zp->z_pflags &= ~ZFS_SPARSE;
3558fd73 1355 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
572e2857
BB
1356 NULL, &zp->z_pflags, 8);
1357 }
1358 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
b128c09f 1359
34dc7c2f
BB
1360 dmu_tx_commit(tx);
1361
d164b209 1362 zfs_range_unlock(rl);
34dc7c2f
BB
1363
1364 return (0);
1365}
1366
b128c09f
BB
1367/*
1368 * Free space in a file
1369 *
1370 * IN: zp - znode of file to free data in.
1371 * off - start of range
1372 * len - end of range (0 => EOF)
1373 * flag - current file open mode flags.
1374 * log - TRUE if this action should be logged
1375 *
3558fd73 1376 * RETURN: 0 if success
b128c09f
BB
1377 * error code if failure
1378 */
1379int
1380zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1381{
3558fd73 1382 struct inode *ip = ZTOI(zp);
b128c09f 1383 dmu_tx_t *tx;
3558fd73
BB
1384 zfs_sb_t *zsb = ZTOZSB(zp);
1385 zilog_t *zilog = zsb->z_log;
428870ff
BB
1386 uint64_t mode;
1387 uint64_t mtime[2], ctime[2];
1388 sa_bulk_attr_t bulk[3];
1389 int count = 0;
b128c09f
BB
1390 int error;
1391
3558fd73 1392 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
428870ff
BB
1393 sizeof (mode))) != 0)
1394 return (error);
1395
1396 if (off > zp->z_size) {
b128c09f
BB
1397 error = zfs_extend(zp, off+len);
1398 if (error == 0 && log)
1399 goto log;
1400 else
1401 return (error);
1402 }
1403
1404 /*
1405 * Check for any locks in the region to be freed.
1406 */
a89c3e0b 1407 if (ip->i_flock && mandatory_lock(ip)) {
428870ff 1408 uint64_t length = (len ? len : zp->z_size - off);
a89c3e0b
RP
1409 if (!lock_may_write(ip, off, length))
1410 return (EAGAIN);
b128c09f
BB
1411 }
1412
1413 if (len == 0) {
1414 error = zfs_trunc(zp, off);
1415 } else {
1416 if ((error = zfs_free_range(zp, off, len)) == 0 &&
428870ff 1417 off + len > zp->z_size)
b128c09f
BB
1418 error = zfs_extend(zp, off+len);
1419 }
1420 if (error || !log)
1421 return (error);
1422log:
3558fd73 1423 tx = dmu_tx_create(zsb->z_os);
428870ff
BB
1424 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1425 zfs_sa_upgrade_txholds(tx, zp);
fb5f0bc8 1426 error = dmu_tx_assign(tx, TXG_NOWAIT);
b128c09f 1427 if (error) {
fb5f0bc8 1428 if (error == ERESTART) {
b128c09f
BB
1429 dmu_tx_wait(tx);
1430 dmu_tx_abort(tx);
1431 goto log;
1432 }
1433 dmu_tx_abort(tx);
1434 return (error);
1435 }
1436
3558fd73
BB
1437 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1438 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1439 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
428870ff
BB
1440 NULL, &zp->z_pflags, 8);
1441 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1442 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1443 ASSERT(error == 0);
1444
b128c09f
BB
1445 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1446
1447 dmu_tx_commit(tx);
960e08fe 1448 zfs_inode_update(zp);
b128c09f
BB
1449 return (0);
1450}
1451
34dc7c2f
BB
1452void
1453zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1454{
22872ff5
BB
1455 struct super_block *sb;
1456 zfs_sb_t *zsb;
428870ff 1457 uint64_t moid, obj, sa_obj, version;
22872ff5 1458 uint64_t sense = ZFS_CASE_SENSITIVE;
34dc7c2f
BB
1459 uint64_t norm = 0;
1460 nvpair_t *elem;
1461 int error;
22872ff5
BB
1462 int i;
1463 znode_t *rootzp = NULL;
1464 vattr_t vattr;
1465 znode_t *zp;
1466 zfs_acl_ids_t acl_ids;
34dc7c2f
BB
1467
1468 /*
1469 * First attempt to create master node.
1470 */
1471 /*
1472 * In an empty objset, there are no blocks to read and thus
1473 * there can be no i/o errors (which we assert below).
1474 */
1475 moid = MASTER_NODE_OBJ;
1476 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1477 DMU_OT_NONE, 0, tx);
1478 ASSERT(error == 0);
1479
1480 /*
1481 * Set starting attributes.
1482 */
428870ff 1483 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
34dc7c2f
BB
1484 elem = NULL;
1485 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1486 /* For the moment we expect all zpl props to be uint64_ts */
1487 uint64_t val;
1488 char *name;
1489
1490 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1491 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1492 name = nvpair_name(elem);
1493 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
9babb374
BB
1494 if (val < version)
1495 version = val;
34dc7c2f
BB
1496 } else {
1497 error = zap_update(os, moid, name, 8, 1, &val, tx);
1498 }
1499 ASSERT(error == 0);
1500 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1501 norm = val;
22872ff5
BB
1502 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1503 sense = val;
34dc7c2f
BB
1504 }
1505 ASSERT(version != 0);
9babb374 1506 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
34dc7c2f 1507
428870ff
BB
1508 /*
1509 * Create zap object used for SA attribute registration
1510 */
1511
1512 if (version >= ZPL_VERSION_SA) {
1513 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1514 DMU_OT_NONE, 0, tx);
1515 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1516 ASSERT(error == 0);
1517 } else {
1518 sa_obj = 0;
1519 }
34dc7c2f
BB
1520 /*
1521 * Create a delete queue.
1522 */
9babb374 1523 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
34dc7c2f 1524
9babb374 1525 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
34dc7c2f
BB
1526 ASSERT(error == 0);
1527
9babb374 1528 /*
22872ff5
BB
1529 * Create root znode. Create minimal znode/inode/zsb/sb
1530 * to allow zfs_mknode to work.
9babb374 1531 */
22872ff5
BB
1532 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1533 vattr.va_mode = S_IFDIR|0755;
1534 vattr.va_uid = crgetuid(cr);
1535 vattr.va_gid = crgetgid(cr);
1536
b8d06fca 1537 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
22872ff5
BB
1538 rootzp->z_moved = 0;
1539 rootzp->z_unlinked = 0;
1540 rootzp->z_atime_dirty = 0;
1541 rootzp->z_is_sa = USE_SA(version, os);
1542
004324ec 1543 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
22872ff5
BB
1544 zsb->z_os = os;
1545 zsb->z_parent = zsb;
1546 zsb->z_version = version;
1547 zsb->z_use_fuids = USE_FUIDS(version, os);
1548 zsb->z_use_sa = USE_SA(version, os);
1549 zsb->z_norm = norm;
1550
b8d06fca 1551 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
22872ff5
BB
1552 sb->s_fs_info = zsb;
1553
1554 ZTOI(rootzp)->i_sb = sb;
1555
1556 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1557 &zsb->z_attr_table);
9babb374 1558
22872ff5 1559 ASSERT(error == 0);
9babb374 1560
60101509 1561 /*
22872ff5
BB
1562 * Fold case on file systems that are always or sometimes case
1563 * insensitive.
60101509 1564 */
22872ff5
BB
1565 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1566 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
60101509 1567
22872ff5
BB
1568 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1569 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1570 offsetof(znode_t, z_link_node));
60101509 1571
22872ff5
BB
1572 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1573 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
60101509 1574
22872ff5
BB
1575 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1576 cr, NULL, &acl_ids));
1577 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1578 ASSERT3P(zp, ==, rootzp);
1579 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1580 ASSERT(error == 0);
1581 zfs_acl_ids_free(&acl_ids);
60101509 1582
22872ff5
BB
1583 atomic_set(&ZTOI(rootzp)->i_count, 0);
1584 sa_handle_destroy(rootzp->z_sa_hdl);
22872ff5
BB
1585 kmem_cache_free(znode_cache, rootzp);
1586
1587 /*
1588 * Create shares directory
1589 */
22872ff5 1590 error = zfs_create_share_dir(zsb, tx);
9babb374 1591 ASSERT(error == 0);
428870ff 1592
22872ff5
BB
1593 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1594 mutex_destroy(&zsb->z_hold_mtx[i]);
2708f716
BB
1595
1596 kmem_free(sb, sizeof (struct super_block));
1597 kmem_free(zsb, sizeof (zfs_sb_t));
34dc7c2f 1598}
34dc7c2f 1599#endif /* _KERNEL */
428870ff 1600
34dc7c2f 1601static int
572e2857
BB
1602zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1603{
1604 uint64_t sa_obj = 0;
1605 int error;
1606
1607 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1608 if (error != 0 && error != ENOENT)
1609 return (error);
1610
1611 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1612 return (error);
1613}
1614
1615static int
1616zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
7b8518cb 1617 dmu_buf_t **db, void *tag)
34dc7c2f 1618{
34dc7c2f 1619 dmu_object_info_t doi;
34dc7c2f 1620 int error;
428870ff 1621
7b8518cb 1622 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
34dc7c2f
BB
1623 return (error);
1624
572e2857 1625 dmu_object_info_from_db(*db, &doi);
428870ff
BB
1626 if ((doi.doi_bonus_type != DMU_OT_SA &&
1627 doi.doi_bonus_type != DMU_OT_ZNODE) ||
d6320ddb
BB
1628 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1629 doi.doi_bonus_size < sizeof (znode_phys_t))) {
7b8518cb 1630 sa_buf_rele(*db, tag);
572e2857 1631 return (ENOTSUP);
34dc7c2f
BB
1632 }
1633
572e2857
BB
1634 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1635 if (error != 0) {
7b8518cb 1636 sa_buf_rele(*db, tag);
428870ff
BB
1637 return (error);
1638 }
1639
572e2857
BB
1640 return (0);
1641}
1642
1643void
7b8518cb 1644zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
572e2857
BB
1645{
1646 sa_handle_destroy(hdl);
7b8518cb 1647 sa_buf_rele(db, tag);
572e2857
BB
1648}
1649
1650/*
1651 * Given an object number, return its parent object number and whether
1652 * or not the object is an extended attribute directory.
1653 */
1654static int
1655zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1656 int *is_xattrdir)
1657{
1658 uint64_t parent;
1659 uint64_t pflags;
1660 uint64_t mode;
1661 sa_bulk_attr_t bulk[3];
1662 int count = 0;
1663 int error;
1664
1665 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1666 &parent, sizeof (parent));
428870ff 1667 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
572e2857 1668 &pflags, sizeof (pflags));
428870ff 1669 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
572e2857 1670 &mode, sizeof (mode));
428870ff 1671
572e2857 1672 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
428870ff 1673 return (error);
572e2857 1674
428870ff
BB
1675 *pobjp = parent;
1676 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
34dc7c2f
BB
1677
1678 return (0);
1679}
1680
572e2857
BB
1681/*
1682 * Given an object number, return some zpl level statistics
1683 */
1684static int
1685zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1686 zfs_stat_t *sb)
34dc7c2f 1687{
572e2857
BB
1688 sa_bulk_attr_t bulk[4];
1689 int count = 0;
1690
1691 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1692 &sb->zs_mode, sizeof (sb->zs_mode));
1693 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1694 &sb->zs_gen, sizeof (sb->zs_gen));
1695 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1696 &sb->zs_links, sizeof (sb->zs_links));
1697 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1698 &sb->zs_ctime, sizeof (sb->zs_ctime));
1699
1700 return (sa_bulk_lookup(hdl, bulk, count));
1701}
1702
1703static int
1704zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1705 sa_attr_type_t *sa_table, char *buf, int len)
1706{
1707 sa_handle_t *sa_hdl;
1708 sa_handle_t *prevhdl = NULL;
1709 dmu_buf_t *prevdb = NULL;
1710 dmu_buf_t *sa_db = NULL;
34dc7c2f
BB
1711 char *path = buf + len - 1;
1712 int error;
1713
1714 *path = '\0';
572e2857 1715 sa_hdl = hdl;
428870ff 1716
34dc7c2f
BB
1717 for (;;) {
1718 uint64_t pobj;
1719 char component[MAXNAMELEN + 2];
1720 size_t complen;
1721 int is_xattrdir;
1722
572e2857 1723 if (prevdb)
7b8518cb 1724 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
572e2857
BB
1725
1726 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1727 &is_xattrdir)) != 0)
34dc7c2f
BB
1728 break;
1729
1730 if (pobj == obj) {
1731 if (path[0] != '/')
1732 *--path = '/';
1733 break;
1734 }
1735
1736 component[0] = '/';
1737 if (is_xattrdir) {
1738 (void) sprintf(component + 1, "<xattrdir>");
1739 } else {
1740 error = zap_value_search(osp, pobj, obj,
1741 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1742 if (error != 0)
1743 break;
1744 }
1745
1746 complen = strlen(component);
1747 path -= complen;
1748 ASSERT(path >= buf);
1749 bcopy(component, path, complen);
1750 obj = pobj;
572e2857
BB
1751
1752 if (sa_hdl != hdl) {
1753 prevhdl = sa_hdl;
1754 prevdb = sa_db;
1755 }
7b8518cb 1756 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
572e2857
BB
1757 if (error != 0) {
1758 sa_hdl = prevhdl;
1759 sa_db = prevdb;
1760 break;
1761 }
1762 }
1763
1764 if (sa_hdl != NULL && sa_hdl != hdl) {
1765 ASSERT(sa_db != NULL);
7b8518cb 1766 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
34dc7c2f
BB
1767 }
1768
1769 if (error == 0)
1770 (void) memmove(buf, path, buf + len - path);
428870ff 1771
34dc7c2f
BB
1772 return (error);
1773}
572e2857
BB
1774
1775int
1776zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1777{
1778 sa_attr_type_t *sa_table;
1779 sa_handle_t *hdl;
1780 dmu_buf_t *db;
1781 int error;
1782
1783 error = zfs_sa_setup(osp, &sa_table);
1784 if (error != 0)
1785 return (error);
1786
7b8518cb 1787 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
572e2857
BB
1788 if (error != 0)
1789 return (error);
1790
1791 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1792
7b8518cb 1793 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
1794 return (error);
1795}
1796
1797int
1798zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1799 char *buf, int len)
1800{
1801 char *path = buf + len - 1;
1802 sa_attr_type_t *sa_table;
1803 sa_handle_t *hdl;
1804 dmu_buf_t *db;
1805 int error;
1806
1807 *path = '\0';
1808
1809 error = zfs_sa_setup(osp, &sa_table);
1810 if (error != 0)
1811 return (error);
1812
7b8518cb 1813 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
572e2857
BB
1814 if (error != 0)
1815 return (error);
1816
1817 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1818 if (error != 0) {
7b8518cb 1819 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
1820 return (error);
1821 }
1822
1823 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1824
7b8518cb 1825 zfs_release_sa_handle(hdl, db, FTAG);
572e2857
BB
1826 return (error);
1827}
c28b2279
BB
1828
1829#if defined(_KERNEL) && defined(HAVE_SPL)
1830EXPORT_SYMBOL(zfs_create_fs);
1831EXPORT_SYMBOL(zfs_obj_to_path);
1832#endif