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