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