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