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