]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/module/zfs/zfs_znode.c
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.8-0ubuntu1, zfs to 0.6.5.8-0ubuntu1
[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 /*
486 * Construct a znode+inode and initialize.
487 *
488 * This does not do a call to dmu_set_user() that is
489 * up to the caller to do, in case you don't want to
490 * return the znode
491 */
492 static znode_t *
493 zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
494 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl)
495 {
496 znode_t *zp;
497 struct inode *ip;
498 uint64_t mode;
499 uint64_t parent;
500 sa_bulk_attr_t bulk[9];
501 int count = 0;
502
503 ASSERT(zsb != NULL);
504
505 ip = new_inode(zsb->z_sb);
506 if (ip == NULL)
507 return (NULL);
508
509 zp = ITOZ(ip);
510 ASSERT(zp->z_dirlocks == NULL);
511 ASSERT3P(zp->z_acl_cached, ==, NULL);
512 ASSERT3P(zp->z_xattr_cached, ==, NULL);
513 zp->z_moved = 0;
514 zp->z_sa_hdl = NULL;
515 zp->z_unlinked = 0;
516 zp->z_atime_dirty = 0;
517 zp->z_mapcnt = 0;
518 zp->z_id = db->db_object;
519 zp->z_blksz = blksz;
520 zp->z_seq = 0x7A4653;
521 zp->z_sync_cnt = 0;
522 zp->z_is_mapped = B_FALSE;
523 zp->z_is_ctldir = B_FALSE;
524 zp->z_is_stale = B_FALSE;
525 zp->z_range_lock.zr_size = &zp->z_size;
526 zp->z_range_lock.zr_blksz = &zp->z_blksz;
527 zp->z_range_lock.zr_max_blksz = &ZTOZSB(zp)->z_max_blksz;
528
529 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
530
531 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
532 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
533 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
534 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
535 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
536 &zp->z_pflags, 8);
537 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
538 &parent, 8);
539 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
540 &zp->z_atime, 16);
541 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
542 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
543
544 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
545 if (hdl == NULL)
546 sa_handle_destroy(zp->z_sa_hdl);
547 zp->z_sa_hdl = NULL;
548 goto error;
549 }
550
551 zp->z_mode = mode;
552
553 ip->i_ino = obj;
554 zfs_inode_update(zp);
555 zfs_inode_set_ops(zsb, ip);
556
557 /*
558 * The only way insert_inode_locked() can fail is if the ip->i_ino
559 * number is already hashed for this super block. This can never
560 * happen because the inode numbers map 1:1 with the object numbers.
561 *
562 * The one exception is rolling back a mounted file system, but in
563 * this case all the active inode are unhashed during the rollback.
564 */
565 VERIFY3S(insert_inode_locked(ip), ==, 0);
566
567 mutex_enter(&zsb->z_znodes_lock);
568 list_insert_tail(&zsb->z_all_znodes, zp);
569 zsb->z_nr_znodes++;
570 membar_producer();
571 mutex_exit(&zsb->z_znodes_lock);
572
573 unlock_new_inode(ip);
574 return (zp);
575
576 error:
577 iput(ip);
578 return (NULL);
579 }
580
581 void
582 zfs_set_inode_flags(znode_t *zp, struct inode *ip)
583 {
584 /*
585 * Linux and Solaris have different sets of file attributes, so we
586 * restrict this conversion to the intersection of the two.
587 */
588
589 if (zp->z_pflags & ZFS_IMMUTABLE)
590 ip->i_flags |= S_IMMUTABLE;
591 else
592 ip->i_flags &= ~S_IMMUTABLE;
593
594 if (zp->z_pflags & ZFS_APPENDONLY)
595 ip->i_flags |= S_APPEND;
596 else
597 ip->i_flags &= ~S_APPEND;
598 }
599
600 /*
601 * Update the embedded inode given the znode. We should work toward
602 * eliminating this function as soon as possible by removing values
603 * which are duplicated between the znode and inode. If the generic
604 * inode has the correct field it should be used, and the ZFS code
605 * updated to access the inode. This can be done incrementally.
606 */
607 void
608 zfs_inode_update(znode_t *zp)
609 {
610 zfs_sb_t *zsb;
611 struct inode *ip;
612 uint32_t blksize;
613 uint64_t atime[2], mtime[2], ctime[2];
614
615 ASSERT(zp != NULL);
616 zsb = ZTOZSB(zp);
617 ip = ZTOI(zp);
618
619 /* Skip .zfs control nodes which do not exist on disk. */
620 if (zfsctl_is_node(ip))
621 return;
622
623 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
624 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
625 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
626
627 spin_lock(&ip->i_lock);
628 ip->i_generation = zp->z_gen;
629 ip->i_uid = SUID_TO_KUID(zp->z_uid);
630 ip->i_gid = SGID_TO_KGID(zp->z_gid);
631 set_nlink(ip, zp->z_links);
632 ip->i_mode = zp->z_mode;
633 zfs_set_inode_flags(zp, ip);
634 ip->i_blkbits = SPA_MINBLOCKSHIFT;
635 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
636 (u_longlong_t *)&ip->i_blocks);
637
638 ZFS_TIME_DECODE(&ip->i_atime, atime);
639 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
640 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
641
642 i_size_write(ip, zp->z_size);
643 spin_unlock(&ip->i_lock);
644 }
645
646 /*
647 * Safely mark an inode dirty. Inodes which are part of a read-only
648 * file system or snapshot may not be dirtied.
649 */
650 void
651 zfs_mark_inode_dirty(struct inode *ip)
652 {
653 zfs_sb_t *zsb = ITOZSB(ip);
654
655 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
656 return;
657
658 mark_inode_dirty(ip);
659 }
660
661 static uint64_t empty_xattr;
662 static uint64_t pad[4];
663 static zfs_acl_phys_t acl_phys;
664 /*
665 * Create a new DMU object to hold a zfs znode.
666 *
667 * IN: dzp - parent directory for new znode
668 * vap - file attributes for new znode
669 * tx - dmu transaction id for zap operations
670 * cr - credentials of caller
671 * flag - flags:
672 * IS_ROOT_NODE - new object will be root
673 * IS_XATTR - new object is an attribute
674 * bonuslen - length of bonus buffer
675 * setaclp - File/Dir initial ACL
676 * fuidp - Tracks fuid allocation.
677 *
678 * OUT: zpp - allocated znode
679 *
680 */
681 void
682 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
683 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
684 {
685 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
686 uint64_t mode, size, links, parent, pflags;
687 uint64_t dzp_pflags = 0;
688 uint64_t rdev = 0;
689 zfs_sb_t *zsb = ZTOZSB(dzp);
690 dmu_buf_t *db;
691 timestruc_t now;
692 uint64_t gen, obj;
693 int bonuslen;
694 sa_handle_t *sa_hdl;
695 dmu_object_type_t obj_type;
696 sa_bulk_attr_t *sa_attrs;
697 int cnt = 0;
698 zfs_acl_locator_cb_t locate = { 0 };
699 znode_hold_t *zh;
700
701 if (zsb->z_replay) {
702 obj = vap->va_nodeid;
703 now = vap->va_ctime; /* see zfs_replay_create() */
704 gen = vap->va_nblocks; /* ditto */
705 } else {
706 obj = 0;
707 gethrestime(&now);
708 gen = dmu_tx_get_txg(tx);
709 }
710
711 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
712 bonuslen = (obj_type == DMU_OT_SA) ?
713 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
714
715 /*
716 * Create a new DMU object.
717 */
718 /*
719 * There's currently no mechanism for pre-reading the blocks that will
720 * be needed to allocate a new object, so we accept the small chance
721 * that there will be an i/o error and we will fail one of the
722 * assertions below.
723 */
724 if (S_ISDIR(vap->va_mode)) {
725 if (zsb->z_replay) {
726 VERIFY0(zap_create_claim_norm(zsb->z_os, obj,
727 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
728 obj_type, bonuslen, tx));
729 } else {
730 obj = zap_create_norm(zsb->z_os,
731 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
732 obj_type, bonuslen, tx);
733 }
734 } else {
735 if (zsb->z_replay) {
736 VERIFY0(dmu_object_claim(zsb->z_os, obj,
737 DMU_OT_PLAIN_FILE_CONTENTS, 0,
738 obj_type, bonuslen, tx));
739 } else {
740 obj = dmu_object_alloc(zsb->z_os,
741 DMU_OT_PLAIN_FILE_CONTENTS, 0,
742 obj_type, bonuslen, tx);
743 }
744 }
745
746 zh = zfs_znode_hold_enter(zsb, obj);
747 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
748
749 /*
750 * If this is the root, fix up the half-initialized parent pointer
751 * to reference the just-allocated physical data area.
752 */
753 if (flag & IS_ROOT_NODE) {
754 dzp->z_id = obj;
755 } else {
756 dzp_pflags = dzp->z_pflags;
757 }
758
759 /*
760 * If parent is an xattr, so am I.
761 */
762 if (dzp_pflags & ZFS_XATTR) {
763 flag |= IS_XATTR;
764 }
765
766 if (zsb->z_use_fuids)
767 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
768 else
769 pflags = 0;
770
771 if (S_ISDIR(vap->va_mode)) {
772 size = 2; /* contents ("." and "..") */
773 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
774 } else {
775 size = links = 0;
776 }
777
778 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
779 rdev = vap->va_rdev;
780
781 parent = dzp->z_id;
782 mode = acl_ids->z_mode;
783 if (flag & IS_XATTR)
784 pflags |= ZFS_XATTR;
785
786 /*
787 * No execs denied will be deterimed when zfs_mode_compute() is called.
788 */
789 pflags |= acl_ids->z_aclp->z_hints &
790 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
791 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
792
793 ZFS_TIME_ENCODE(&now, crtime);
794 ZFS_TIME_ENCODE(&now, ctime);
795
796 if (vap->va_mask & ATTR_ATIME) {
797 ZFS_TIME_ENCODE(&vap->va_atime, atime);
798 } else {
799 ZFS_TIME_ENCODE(&now, atime);
800 }
801
802 if (vap->va_mask & ATTR_MTIME) {
803 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
804 } else {
805 ZFS_TIME_ENCODE(&now, mtime);
806 }
807
808 /* Now add in all of the "SA" attributes */
809 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
810 &sa_hdl));
811
812 /*
813 * Setup the array of attributes to be replaced/set on the new file
814 *
815 * order for DMU_OT_ZNODE is critical since it needs to be constructed
816 * in the old znode_phys_t format. Don't change this ordering
817 */
818 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
819
820 if (obj_type == DMU_OT_ZNODE) {
821 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
822 NULL, &atime, 16);
823 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
824 NULL, &mtime, 16);
825 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
826 NULL, &ctime, 16);
827 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
828 NULL, &crtime, 16);
829 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
830 NULL, &gen, 8);
831 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
832 NULL, &mode, 8);
833 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
834 NULL, &size, 8);
835 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
836 NULL, &parent, 8);
837 } else {
838 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
839 NULL, &mode, 8);
840 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
841 NULL, &size, 8);
842 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
843 NULL, &gen, 8);
844 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
845 NULL, &acl_ids->z_fuid, 8);
846 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
847 NULL, &acl_ids->z_fgid, 8);
848 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
849 NULL, &parent, 8);
850 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
851 NULL, &pflags, 8);
852 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
853 NULL, &atime, 16);
854 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
855 NULL, &mtime, 16);
856 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
857 NULL, &ctime, 16);
858 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
859 NULL, &crtime, 16);
860 }
861
862 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
863
864 if (obj_type == DMU_OT_ZNODE) {
865 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
866 &empty_xattr, 8);
867 }
868 if (obj_type == DMU_OT_ZNODE ||
869 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
870 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
871 NULL, &rdev, 8);
872 }
873 if (obj_type == DMU_OT_ZNODE) {
874 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
875 NULL, &pflags, 8);
876 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
877 &acl_ids->z_fuid, 8);
878 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
879 &acl_ids->z_fgid, 8);
880 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
881 sizeof (uint64_t) * 4);
882 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
883 &acl_phys, sizeof (zfs_acl_phys_t));
884 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
885 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
886 &acl_ids->z_aclp->z_acl_count, 8);
887 locate.cb_aclp = acl_ids->z_aclp;
888 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
889 zfs_acl_data_locator, &locate,
890 acl_ids->z_aclp->z_acl_bytes);
891 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
892 acl_ids->z_fuid, acl_ids->z_fgid);
893 }
894
895 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
896
897 if (!(flag & IS_ROOT_NODE)) {
898 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl);
899 VERIFY(*zpp != NULL);
900 VERIFY(dzp != NULL);
901 } else {
902 /*
903 * If we are creating the root node, the "parent" we
904 * passed in is the znode for the root.
905 */
906 *zpp = dzp;
907
908 (*zpp)->z_sa_hdl = sa_hdl;
909 }
910
911 (*zpp)->z_pflags = pflags;
912 (*zpp)->z_mode = mode;
913
914 if (obj_type == DMU_OT_ZNODE ||
915 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
916 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
917 }
918 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
919 zfs_znode_hold_exit(zsb, zh);
920 }
921
922 /*
923 * Update in-core attributes. It is assumed the caller will be doing an
924 * sa_bulk_update to push the changes out.
925 */
926 void
927 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
928 {
929 xoptattr_t *xoap;
930
931 xoap = xva_getxoptattr(xvap);
932 ASSERT(xoap);
933
934 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
935 uint64_t times[2];
936 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
937 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
938 &times, sizeof (times), tx);
939 XVA_SET_RTN(xvap, XAT_CREATETIME);
940 }
941 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
942 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
943 zp->z_pflags, tx);
944 XVA_SET_RTN(xvap, XAT_READONLY);
945 }
946 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
947 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
948 zp->z_pflags, tx);
949 XVA_SET_RTN(xvap, XAT_HIDDEN);
950 }
951 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
952 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
953 zp->z_pflags, tx);
954 XVA_SET_RTN(xvap, XAT_SYSTEM);
955 }
956 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
957 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
958 zp->z_pflags, tx);
959 XVA_SET_RTN(xvap, XAT_ARCHIVE);
960 }
961 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
962 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
963 zp->z_pflags, tx);
964 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
965 }
966 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
967 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
968 zp->z_pflags, tx);
969 XVA_SET_RTN(xvap, XAT_NOUNLINK);
970 }
971 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
972 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
973 zp->z_pflags, tx);
974 XVA_SET_RTN(xvap, XAT_APPENDONLY);
975 }
976 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
977 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
978 zp->z_pflags, tx);
979 XVA_SET_RTN(xvap, XAT_NODUMP);
980 }
981 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
982 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
983 zp->z_pflags, tx);
984 XVA_SET_RTN(xvap, XAT_OPAQUE);
985 }
986 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
987 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
988 xoap->xoa_av_quarantined, zp->z_pflags, tx);
989 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
990 }
991 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
992 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
993 zp->z_pflags, tx);
994 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
995 }
996 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
997 zfs_sa_set_scanstamp(zp, xvap, tx);
998 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
999 }
1000 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1001 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1002 zp->z_pflags, tx);
1003 XVA_SET_RTN(xvap, XAT_REPARSE);
1004 }
1005 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1006 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1007 zp->z_pflags, tx);
1008 XVA_SET_RTN(xvap, XAT_OFFLINE);
1009 }
1010 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1011 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1012 zp->z_pflags, tx);
1013 XVA_SET_RTN(xvap, XAT_SPARSE);
1014 }
1015 }
1016
1017 int
1018 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
1019 {
1020 dmu_object_info_t doi;
1021 dmu_buf_t *db;
1022 znode_t *zp;
1023 znode_hold_t *zh;
1024 int err;
1025 sa_handle_t *hdl;
1026
1027 *zpp = NULL;
1028
1029 again:
1030 zh = zfs_znode_hold_enter(zsb, obj_num);
1031
1032 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
1033 if (err) {
1034 zfs_znode_hold_exit(zsb, zh);
1035 return (err);
1036 }
1037
1038 dmu_object_info_from_db(db, &doi);
1039 if (doi.doi_bonus_type != DMU_OT_SA &&
1040 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1041 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1042 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1043 sa_buf_rele(db, NULL);
1044 zfs_znode_hold_exit(zsb, zh);
1045 return (SET_ERROR(EINVAL));
1046 }
1047
1048 hdl = dmu_buf_get_user(db);
1049 if (hdl != NULL) {
1050 zp = sa_get_userdata(hdl);
1051
1052
1053 /*
1054 * Since "SA" does immediate eviction we
1055 * should never find a sa handle that doesn't
1056 * know about the znode.
1057 */
1058
1059 ASSERT3P(zp, !=, NULL);
1060
1061 mutex_enter(&zp->z_lock);
1062 ASSERT3U(zp->z_id, ==, obj_num);
1063 if (zp->z_unlinked) {
1064 err = SET_ERROR(ENOENT);
1065 } else {
1066 /*
1067 * If igrab() returns NULL the VFS has independently
1068 * determined the inode should be evicted and has
1069 * called iput_final() to start the eviction process.
1070 * The SA handle is still valid but because the VFS
1071 * requires that the eviction succeed we must drop
1072 * our locks and references to allow the eviction to
1073 * complete. The zfs_zget() may then be retried.
1074 *
1075 * This unlikely case could be optimized by registering
1076 * a sops->drop_inode() callback. The callback would
1077 * need to detect the active SA hold thereby informing
1078 * the VFS that this inode should not be evicted.
1079 */
1080 if (igrab(ZTOI(zp)) == NULL) {
1081 mutex_exit(&zp->z_lock);
1082 sa_buf_rele(db, NULL);
1083 zfs_znode_hold_exit(zsb, zh);
1084 /* inode might need this to finish evict */
1085 cond_resched();
1086 goto again;
1087 }
1088 *zpp = zp;
1089 err = 0;
1090 }
1091 mutex_exit(&zp->z_lock);
1092 sa_buf_rele(db, NULL);
1093 zfs_znode_hold_exit(zsb, zh);
1094 return (err);
1095 }
1096
1097 /*
1098 * Not found create new znode/vnode but only if file exists.
1099 *
1100 * There is a small window where zfs_vget() could
1101 * find this object while a file create is still in
1102 * progress. This is checked for in zfs_znode_alloc()
1103 *
1104 * if zfs_znode_alloc() fails it will drop the hold on the
1105 * bonus buffer.
1106 */
1107 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
1108 doi.doi_bonus_type, obj_num, NULL);
1109 if (zp == NULL) {
1110 err = SET_ERROR(ENOENT);
1111 } else {
1112 *zpp = zp;
1113 }
1114 zfs_znode_hold_exit(zsb, zh);
1115 return (err);
1116 }
1117
1118 int
1119 zfs_rezget(znode_t *zp)
1120 {
1121 zfs_sb_t *zsb = ZTOZSB(zp);
1122 dmu_object_info_t doi;
1123 dmu_buf_t *db;
1124 uint64_t obj_num = zp->z_id;
1125 uint64_t mode;
1126 sa_bulk_attr_t bulk[8];
1127 int err;
1128 int count = 0;
1129 uint64_t gen;
1130 znode_hold_t *zh;
1131
1132 /*
1133 * skip ctldir, otherwise they will always get invalidated. This will
1134 * cause funny behaviour for the mounted snapdirs. Especially for
1135 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1136 * anyone automount it again as long as someone is still using the
1137 * detached mount.
1138 */
1139 if (zp->z_is_ctldir)
1140 return (0);
1141
1142 zh = zfs_znode_hold_enter(zsb, obj_num);
1143
1144 mutex_enter(&zp->z_acl_lock);
1145 if (zp->z_acl_cached) {
1146 zfs_acl_free(zp->z_acl_cached);
1147 zp->z_acl_cached = NULL;
1148 }
1149 mutex_exit(&zp->z_acl_lock);
1150
1151 rw_enter(&zp->z_xattr_lock, RW_WRITER);
1152 if (zp->z_xattr_cached) {
1153 nvlist_free(zp->z_xattr_cached);
1154 zp->z_xattr_cached = NULL;
1155 }
1156 rw_exit(&zp->z_xattr_lock);
1157
1158 ASSERT(zp->z_sa_hdl == NULL);
1159 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
1160 if (err) {
1161 zfs_znode_hold_exit(zsb, zh);
1162 return (err);
1163 }
1164
1165 dmu_object_info_from_db(db, &doi);
1166 if (doi.doi_bonus_type != DMU_OT_SA &&
1167 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1168 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1169 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1170 sa_buf_rele(db, NULL);
1171 zfs_znode_hold_exit(zsb, zh);
1172 return (SET_ERROR(EINVAL));
1173 }
1174
1175 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
1176
1177 /* reload cached values */
1178 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
1179 &gen, sizeof (gen));
1180 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
1181 &zp->z_size, sizeof (zp->z_size));
1182 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
1183 &zp->z_links, sizeof (zp->z_links));
1184 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
1185 &zp->z_pflags, sizeof (zp->z_pflags));
1186 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
1187 &zp->z_atime, sizeof (zp->z_atime));
1188 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
1189 &zp->z_uid, sizeof (zp->z_uid));
1190 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
1191 &zp->z_gid, sizeof (zp->z_gid));
1192 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
1193 &mode, sizeof (mode));
1194
1195 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1196 zfs_znode_dmu_fini(zp);
1197 zfs_znode_hold_exit(zsb, zh);
1198 return (SET_ERROR(EIO));
1199 }
1200
1201 zp->z_mode = mode;
1202
1203 if (gen != zp->z_gen) {
1204 zfs_znode_dmu_fini(zp);
1205 zfs_znode_hold_exit(zsb, zh);
1206 return (SET_ERROR(EIO));
1207 }
1208
1209 zp->z_unlinked = (zp->z_links == 0);
1210 zp->z_blksz = doi.doi_data_block_size;
1211 zfs_inode_update(zp);
1212
1213 zfs_znode_hold_exit(zsb, zh);
1214
1215 return (0);
1216 }
1217
1218 void
1219 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1220 {
1221 zfs_sb_t *zsb = ZTOZSB(zp);
1222 objset_t *os = zsb->z_os;
1223 uint64_t obj = zp->z_id;
1224 uint64_t acl_obj = zfs_external_acl(zp);
1225 znode_hold_t *zh;
1226
1227 zh = zfs_znode_hold_enter(zsb, obj);
1228 if (acl_obj) {
1229 VERIFY(!zp->z_is_sa);
1230 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1231 }
1232 VERIFY(0 == dmu_object_free(os, obj, tx));
1233 zfs_znode_dmu_fini(zp);
1234 zfs_znode_hold_exit(zsb, zh);
1235 }
1236
1237 void
1238 zfs_zinactive(znode_t *zp)
1239 {
1240 zfs_sb_t *zsb = ZTOZSB(zp);
1241 uint64_t z_id = zp->z_id;
1242 znode_hold_t *zh;
1243
1244 ASSERT(zp->z_sa_hdl);
1245
1246 /*
1247 * Don't allow a zfs_zget() while were trying to release this znode.
1248 */
1249 zh = zfs_znode_hold_enter(zsb, z_id);
1250
1251 mutex_enter(&zp->z_lock);
1252
1253 /*
1254 * If this was the last reference to a file with no links,
1255 * remove the file from the file system.
1256 */
1257 if (zp->z_unlinked) {
1258 mutex_exit(&zp->z_lock);
1259 zfs_znode_hold_exit(zsb, zh);
1260 zfs_rmnode(zp);
1261 return;
1262 }
1263
1264 mutex_exit(&zp->z_lock);
1265 zfs_znode_dmu_fini(zp);
1266
1267 zfs_znode_hold_exit(zsb, zh);
1268 }
1269
1270 static inline int
1271 zfs_compare_timespec(struct timespec *t1, struct timespec *t2)
1272 {
1273 if (t1->tv_sec < t2->tv_sec)
1274 return (-1);
1275
1276 if (t1->tv_sec > t2->tv_sec)
1277 return (1);
1278
1279 return (t1->tv_nsec - t2->tv_nsec);
1280 }
1281
1282 /*
1283 * Determine whether the znode's atime must be updated. The logic mostly
1284 * duplicates the Linux kernel's relatime_need_update() functionality.
1285 * This function is only called if the underlying filesystem actually has
1286 * atime updates enabled.
1287 */
1288 static inline boolean_t
1289 zfs_atime_need_update(znode_t *zp, timestruc_t *now)
1290 {
1291 if (!ZTOZSB(zp)->z_relatime)
1292 return (B_TRUE);
1293
1294 /*
1295 * In relatime mode, only update the atime if the previous atime
1296 * is earlier than either the ctime or mtime or if at least a day
1297 * has passed since the last update of atime.
1298 */
1299 if (zfs_compare_timespec(&ZTOI(zp)->i_mtime, &ZTOI(zp)->i_atime) >= 0)
1300 return (B_TRUE);
1301
1302 if (zfs_compare_timespec(&ZTOI(zp)->i_ctime, &ZTOI(zp)->i_atime) >= 0)
1303 return (B_TRUE);
1304
1305 if ((long)now->tv_sec - ZTOI(zp)->i_atime.tv_sec >= 24*60*60)
1306 return (B_TRUE);
1307
1308 return (B_FALSE);
1309 }
1310
1311 /*
1312 * Prepare to update znode time stamps.
1313 *
1314 * IN: zp - znode requiring timestamp update
1315 * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags
1316 * have_tx - true of caller is creating a new txg
1317 *
1318 * OUT: zp - new atime (via underlying inode's i_atime)
1319 * mtime - new mtime
1320 * ctime - new ctime
1321 *
1322 * NOTE: The arguments are somewhat redundant. The following condition
1323 * is always true:
1324 *
1325 * have_tx == !(flag & ATTR_ATIME)
1326 */
1327 void
1328 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1329 uint64_t ctime[2], boolean_t have_tx)
1330 {
1331 timestruc_t now;
1332
1333 ASSERT(have_tx == !(flag & ATTR_ATIME));
1334 gethrestime(&now);
1335
1336 /*
1337 * NOTE: The following test intentionally does not update z_atime_dirty
1338 * in the case where an ATIME update has been requested but for which
1339 * the update is omitted due to relatime logic. The rationale being
1340 * that if the flag was set somewhere else, we should leave it alone
1341 * here.
1342 */
1343 if (flag & ATTR_ATIME) {
1344 if (zfs_atime_need_update(zp, &now)) {
1345 ZFS_TIME_ENCODE(&now, zp->z_atime);
1346 ZTOI(zp)->i_atime.tv_sec = zp->z_atime[0];
1347 ZTOI(zp)->i_atime.tv_nsec = zp->z_atime[1];
1348 zp->z_atime_dirty = 1;
1349 }
1350 } else {
1351 zp->z_atime_dirty = 0;
1352 zp->z_seq++;
1353 }
1354
1355 if (flag & ATTR_MTIME) {
1356 ZFS_TIME_ENCODE(&now, mtime);
1357 if (ZTOZSB(zp)->z_use_fuids) {
1358 zp->z_pflags |= (ZFS_ARCHIVE |
1359 ZFS_AV_MODIFIED);
1360 }
1361 }
1362
1363 if (flag & ATTR_CTIME) {
1364 ZFS_TIME_ENCODE(&now, ctime);
1365 if (ZTOZSB(zp)->z_use_fuids)
1366 zp->z_pflags |= ZFS_ARCHIVE;
1367 }
1368 }
1369
1370 /*
1371 * Grow the block size for a file.
1372 *
1373 * IN: zp - znode of file to free data in.
1374 * size - requested block size
1375 * tx - open transaction.
1376 *
1377 * NOTE: this function assumes that the znode is write locked.
1378 */
1379 void
1380 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1381 {
1382 int error;
1383 u_longlong_t dummy;
1384
1385 if (size <= zp->z_blksz)
1386 return;
1387 /*
1388 * If the file size is already greater than the current blocksize,
1389 * we will not grow. If there is more than one block in a file,
1390 * the blocksize cannot change.
1391 */
1392 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1393 return;
1394
1395 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1396 size, 0, tx);
1397
1398 if (error == ENOTSUP)
1399 return;
1400 ASSERT0(error);
1401
1402 /* What blocksize did we actually get? */
1403 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1404 }
1405
1406 /*
1407 * Increase the file length
1408 *
1409 * IN: zp - znode of file to free data in.
1410 * end - new end-of-file
1411 *
1412 * RETURN: 0 on success, error code on failure
1413 */
1414 static int
1415 zfs_extend(znode_t *zp, uint64_t end)
1416 {
1417 zfs_sb_t *zsb = ZTOZSB(zp);
1418 dmu_tx_t *tx;
1419 rl_t *rl;
1420 uint64_t newblksz;
1421 int error;
1422
1423 /*
1424 * We will change zp_size, lock the whole file.
1425 */
1426 rl = zfs_range_lock(&zp->z_range_lock, 0, UINT64_MAX, RL_WRITER);
1427
1428 /*
1429 * Nothing to do if file already at desired length.
1430 */
1431 if (end <= zp->z_size) {
1432 zfs_range_unlock(rl);
1433 return (0);
1434 }
1435 tx = dmu_tx_create(zsb->z_os);
1436 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1437 zfs_sa_upgrade_txholds(tx, zp);
1438 if (end > zp->z_blksz &&
1439 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1440 /*
1441 * We are growing the file past the current block size.
1442 */
1443 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1444 /*
1445 * File's blocksize is already larger than the
1446 * "recordsize" property. Only let it grow to
1447 * the next power of 2.
1448 */
1449 ASSERT(!ISP2(zp->z_blksz));
1450 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1451 } else {
1452 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1453 }
1454 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1455 } else {
1456 newblksz = 0;
1457 }
1458
1459 error = dmu_tx_assign(tx, TXG_WAIT);
1460 if (error) {
1461 dmu_tx_abort(tx);
1462 zfs_range_unlock(rl);
1463 return (error);
1464 }
1465
1466 if (newblksz)
1467 zfs_grow_blocksize(zp, newblksz, tx);
1468
1469 zp->z_size = end;
1470
1471 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1472 &zp->z_size, sizeof (zp->z_size), tx));
1473
1474 zfs_range_unlock(rl);
1475
1476 dmu_tx_commit(tx);
1477
1478 return (0);
1479 }
1480
1481 /*
1482 * zfs_zero_partial_page - Modeled after update_pages() but
1483 * with different arguments and semantics for use by zfs_freesp().
1484 *
1485 * Zeroes a piece of a single page cache entry for zp at offset
1486 * start and length len.
1487 *
1488 * Caller must acquire a range lock on the file for the region
1489 * being zeroed in order that the ARC and page cache stay in sync.
1490 */
1491 static void
1492 zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len)
1493 {
1494 struct address_space *mp = ZTOI(zp)->i_mapping;
1495 struct page *pp;
1496 int64_t off;
1497 void *pb;
1498
1499 ASSERT((start & PAGE_MASK) == ((start + len - 1) & PAGE_MASK));
1500
1501 off = start & (PAGE_SIZE - 1);
1502 start &= PAGE_MASK;
1503
1504 pp = find_lock_page(mp, start >> PAGE_SHIFT);
1505 if (pp) {
1506 if (mapping_writably_mapped(mp))
1507 flush_dcache_page(pp);
1508
1509 pb = kmap(pp);
1510 bzero(pb + off, len);
1511 kunmap(pp);
1512
1513 if (mapping_writably_mapped(mp))
1514 flush_dcache_page(pp);
1515
1516 mark_page_accessed(pp);
1517 SetPageUptodate(pp);
1518 ClearPageError(pp);
1519 unlock_page(pp);
1520 put_page(pp);
1521 }
1522 }
1523
1524 /*
1525 * Free space in a file.
1526 *
1527 * IN: zp - znode of file to free data in.
1528 * off - start of section to free.
1529 * len - length of section to free.
1530 *
1531 * RETURN: 0 on success, error code on failure
1532 */
1533 static int
1534 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1535 {
1536 zfs_sb_t *zsb = ZTOZSB(zp);
1537 rl_t *rl;
1538 int error;
1539
1540 /*
1541 * Lock the range being freed.
1542 */
1543 rl = zfs_range_lock(&zp->z_range_lock, off, len, RL_WRITER);
1544
1545 /*
1546 * Nothing to do if file already at desired length.
1547 */
1548 if (off >= zp->z_size) {
1549 zfs_range_unlock(rl);
1550 return (0);
1551 }
1552
1553 if (off + len > zp->z_size)
1554 len = zp->z_size - off;
1555
1556 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1557
1558 /*
1559 * Zero partial page cache entries. This must be done under a
1560 * range lock in order to keep the ARC and page cache in sync.
1561 */
1562 if (zp->z_is_mapped) {
1563 loff_t first_page, last_page, page_len;
1564 loff_t first_page_offset, last_page_offset;
1565
1566 /* first possible full page in hole */
1567 first_page = (off + PAGE_SIZE - 1) >> PAGE_SHIFT;
1568 /* last page of hole */
1569 last_page = (off + len) >> PAGE_SHIFT;
1570
1571 /* offset of first_page */
1572 first_page_offset = first_page << PAGE_SHIFT;
1573 /* offset of last_page */
1574 last_page_offset = last_page << PAGE_SHIFT;
1575
1576 /* truncate whole pages */
1577 if (last_page_offset > first_page_offset) {
1578 truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1579 first_page_offset, last_page_offset - 1);
1580 }
1581
1582 /* truncate sub-page ranges */
1583 if (first_page > last_page) {
1584 /* entire punched area within a single page */
1585 zfs_zero_partial_page(zp, off, len);
1586 } else {
1587 /* beginning of punched area at the end of a page */
1588 page_len = first_page_offset - off;
1589 if (page_len > 0)
1590 zfs_zero_partial_page(zp, off, page_len);
1591
1592 /* end of punched area at the beginning of a page */
1593 page_len = off + len - last_page_offset;
1594 if (page_len > 0)
1595 zfs_zero_partial_page(zp, last_page_offset,
1596 page_len);
1597 }
1598 }
1599 zfs_range_unlock(rl);
1600
1601 return (error);
1602 }
1603
1604 /*
1605 * Truncate a file
1606 *
1607 * IN: zp - znode of file to free data in.
1608 * end - new end-of-file.
1609 *
1610 * RETURN: 0 on success, error code on failure
1611 */
1612 static int
1613 zfs_trunc(znode_t *zp, uint64_t end)
1614 {
1615 zfs_sb_t *zsb = ZTOZSB(zp);
1616 dmu_tx_t *tx;
1617 rl_t *rl;
1618 int error;
1619 sa_bulk_attr_t bulk[2];
1620 int count = 0;
1621
1622 /*
1623 * We will change zp_size, lock the whole file.
1624 */
1625 rl = zfs_range_lock(&zp->z_range_lock, 0, UINT64_MAX, RL_WRITER);
1626
1627 /*
1628 * Nothing to do if file already at desired length.
1629 */
1630 if (end >= zp->z_size) {
1631 zfs_range_unlock(rl);
1632 return (0);
1633 }
1634
1635 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1636 if (error) {
1637 zfs_range_unlock(rl);
1638 return (error);
1639 }
1640 tx = dmu_tx_create(zsb->z_os);
1641 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1642 zfs_sa_upgrade_txholds(tx, zp);
1643 error = dmu_tx_assign(tx, TXG_WAIT);
1644 if (error) {
1645 dmu_tx_abort(tx);
1646 zfs_range_unlock(rl);
1647 return (error);
1648 }
1649
1650 zp->z_size = end;
1651 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1652 NULL, &zp->z_size, sizeof (zp->z_size));
1653
1654 if (end == 0) {
1655 zp->z_pflags &= ~ZFS_SPARSE;
1656 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1657 NULL, &zp->z_pflags, 8);
1658 }
1659 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1660
1661 dmu_tx_commit(tx);
1662
1663 zfs_range_unlock(rl);
1664
1665 return (0);
1666 }
1667
1668 /*
1669 * Free space in a file
1670 *
1671 * IN: zp - znode of file to free data in.
1672 * off - start of range
1673 * len - end of range (0 => EOF)
1674 * flag - current file open mode flags.
1675 * log - TRUE if this action should be logged
1676 *
1677 * RETURN: 0 on success, error code on failure
1678 */
1679 int
1680 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1681 {
1682 dmu_tx_t *tx;
1683 zfs_sb_t *zsb = ZTOZSB(zp);
1684 zilog_t *zilog = zsb->z_log;
1685 uint64_t mode;
1686 uint64_t mtime[2], ctime[2];
1687 sa_bulk_attr_t bulk[3];
1688 int count = 0;
1689 int error;
1690
1691 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1692 sizeof (mode))) != 0)
1693 return (error);
1694
1695 if (off > zp->z_size) {
1696 error = zfs_extend(zp, off+len);
1697 if (error == 0 && log)
1698 goto log;
1699 goto out;
1700 }
1701
1702 if (len == 0) {
1703 error = zfs_trunc(zp, off);
1704 } else {
1705 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1706 off + len > zp->z_size)
1707 error = zfs_extend(zp, off+len);
1708 }
1709 if (error || !log)
1710 goto out;
1711 log:
1712 tx = dmu_tx_create(zsb->z_os);
1713 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1714 zfs_sa_upgrade_txholds(tx, zp);
1715 error = dmu_tx_assign(tx, TXG_WAIT);
1716 if (error) {
1717 dmu_tx_abort(tx);
1718 goto out;
1719 }
1720
1721 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1722 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1723 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1724 NULL, &zp->z_pflags, 8);
1725 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1726 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1727 ASSERT(error == 0);
1728
1729 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1730
1731 dmu_tx_commit(tx);
1732
1733 zfs_inode_update(zp);
1734 error = 0;
1735
1736 out:
1737 /*
1738 * Truncate the page cache - for file truncate operations, use
1739 * the purpose-built API for truncations. For punching operations,
1740 * the truncation is handled under a range lock in zfs_free_range.
1741 */
1742 if (len == 0)
1743 truncate_setsize(ZTOI(zp), off);
1744 return (error);
1745 }
1746
1747 void
1748 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1749 {
1750 struct super_block *sb;
1751 zfs_sb_t *zsb;
1752 uint64_t moid, obj, sa_obj, version;
1753 uint64_t sense = ZFS_CASE_SENSITIVE;
1754 uint64_t norm = 0;
1755 nvpair_t *elem;
1756 int size;
1757 int error;
1758 int i;
1759 znode_t *rootzp = NULL;
1760 vattr_t vattr;
1761 znode_t *zp;
1762 zfs_acl_ids_t acl_ids;
1763
1764 /*
1765 * First attempt to create master node.
1766 */
1767 /*
1768 * In an empty objset, there are no blocks to read and thus
1769 * there can be no i/o errors (which we assert below).
1770 */
1771 moid = MASTER_NODE_OBJ;
1772 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1773 DMU_OT_NONE, 0, tx);
1774 ASSERT(error == 0);
1775
1776 /*
1777 * Set starting attributes.
1778 */
1779 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1780 elem = NULL;
1781 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1782 /* For the moment we expect all zpl props to be uint64_ts */
1783 uint64_t val;
1784 char *name;
1785
1786 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1787 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1788 name = nvpair_name(elem);
1789 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1790 if (val < version)
1791 version = val;
1792 } else {
1793 error = zap_update(os, moid, name, 8, 1, &val, tx);
1794 }
1795 ASSERT(error == 0);
1796 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1797 norm = val;
1798 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1799 sense = val;
1800 }
1801 ASSERT(version != 0);
1802 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1803
1804 /*
1805 * Create zap object used for SA attribute registration
1806 */
1807
1808 if (version >= ZPL_VERSION_SA) {
1809 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1810 DMU_OT_NONE, 0, tx);
1811 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1812 ASSERT(error == 0);
1813 } else {
1814 sa_obj = 0;
1815 }
1816 /*
1817 * Create a delete queue.
1818 */
1819 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1820
1821 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1822 ASSERT(error == 0);
1823
1824 /*
1825 * Create root znode. Create minimal znode/inode/zsb/sb
1826 * to allow zfs_mknode to work.
1827 */
1828 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1829 vattr.va_mode = S_IFDIR|0755;
1830 vattr.va_uid = crgetuid(cr);
1831 vattr.va_gid = crgetgid(cr);
1832
1833 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1834 rootzp->z_moved = 0;
1835 rootzp->z_unlinked = 0;
1836 rootzp->z_atime_dirty = 0;
1837 rootzp->z_is_sa = USE_SA(version, os);
1838
1839 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP);
1840 zsb->z_os = os;
1841 zsb->z_parent = zsb;
1842 zsb->z_version = version;
1843 zsb->z_use_fuids = USE_FUIDS(version, os);
1844 zsb->z_use_sa = USE_SA(version, os);
1845 zsb->z_norm = norm;
1846
1847 sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP);
1848 sb->s_fs_info = zsb;
1849
1850 ZTOI(rootzp)->i_sb = sb;
1851
1852 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1853 &zsb->z_attr_table);
1854
1855 ASSERT(error == 0);
1856
1857 /*
1858 * Fold case on file systems that are always or sometimes case
1859 * insensitive.
1860 */
1861 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1862 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1863
1864 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1865 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1866 offsetof(znode_t, z_link_node));
1867
1868 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
1869 zsb->z_hold_size = size;
1870 zsb->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size, KM_SLEEP);
1871 zsb->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
1872 for (i = 0; i != size; i++) {
1873 avl_create(&zsb->z_hold_trees[i], zfs_znode_hold_compare,
1874 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
1875 mutex_init(&zsb->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
1876 }
1877
1878 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1879 cr, NULL, &acl_ids));
1880 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1881 ASSERT3P(zp, ==, rootzp);
1882 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1883 ASSERT(error == 0);
1884 zfs_acl_ids_free(&acl_ids);
1885
1886 atomic_set(&ZTOI(rootzp)->i_count, 0);
1887 sa_handle_destroy(rootzp->z_sa_hdl);
1888 kmem_cache_free(znode_cache, rootzp);
1889
1890 /*
1891 * Create shares directory
1892 */
1893 error = zfs_create_share_dir(zsb, tx);
1894 ASSERT(error == 0);
1895
1896 for (i = 0; i != size; i++) {
1897 avl_destroy(&zsb->z_hold_trees[i]);
1898 mutex_destroy(&zsb->z_hold_locks[i]);
1899 }
1900
1901 vmem_free(zsb->z_hold_trees, sizeof (avl_tree_t) * size);
1902 vmem_free(zsb->z_hold_locks, sizeof (kmutex_t) * size);
1903 kmem_free(sb, sizeof (struct super_block));
1904 kmem_free(zsb, sizeof (zfs_sb_t));
1905 }
1906 #endif /* _KERNEL */
1907
1908 static int
1909 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1910 {
1911 uint64_t sa_obj = 0;
1912 int error;
1913
1914 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1915 if (error != 0 && error != ENOENT)
1916 return (error);
1917
1918 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1919 return (error);
1920 }
1921
1922 static int
1923 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1924 dmu_buf_t **db, void *tag)
1925 {
1926 dmu_object_info_t doi;
1927 int error;
1928
1929 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1930 return (error);
1931
1932 dmu_object_info_from_db(*db, &doi);
1933 if ((doi.doi_bonus_type != DMU_OT_SA &&
1934 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1935 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1936 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1937 sa_buf_rele(*db, tag);
1938 return (SET_ERROR(ENOTSUP));
1939 }
1940
1941 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1942 if (error != 0) {
1943 sa_buf_rele(*db, tag);
1944 return (error);
1945 }
1946
1947 return (0);
1948 }
1949
1950 void
1951 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1952 {
1953 sa_handle_destroy(hdl);
1954 sa_buf_rele(db, tag);
1955 }
1956
1957 /*
1958 * Given an object number, return its parent object number and whether
1959 * or not the object is an extended attribute directory.
1960 */
1961 static int
1962 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1963 int *is_xattrdir)
1964 {
1965 uint64_t parent;
1966 uint64_t pflags;
1967 uint64_t mode;
1968 sa_bulk_attr_t bulk[3];
1969 int count = 0;
1970 int error;
1971
1972 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1973 &parent, sizeof (parent));
1974 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1975 &pflags, sizeof (pflags));
1976 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1977 &mode, sizeof (mode));
1978
1979 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1980 return (error);
1981
1982 *pobjp = parent;
1983 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1984
1985 return (0);
1986 }
1987
1988 /*
1989 * Given an object number, return some zpl level statistics
1990 */
1991 static int
1992 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1993 zfs_stat_t *sb)
1994 {
1995 sa_bulk_attr_t bulk[4];
1996 int count = 0;
1997
1998 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1999 &sb->zs_mode, sizeof (sb->zs_mode));
2000 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2001 &sb->zs_gen, sizeof (sb->zs_gen));
2002 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2003 &sb->zs_links, sizeof (sb->zs_links));
2004 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2005 &sb->zs_ctime, sizeof (sb->zs_ctime));
2006
2007 return (sa_bulk_lookup(hdl, bulk, count));
2008 }
2009
2010 static int
2011 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2012 sa_attr_type_t *sa_table, char *buf, int len)
2013 {
2014 sa_handle_t *sa_hdl;
2015 sa_handle_t *prevhdl = NULL;
2016 dmu_buf_t *prevdb = NULL;
2017 dmu_buf_t *sa_db = NULL;
2018 char *path = buf + len - 1;
2019 int error;
2020
2021 *path = '\0';
2022 sa_hdl = hdl;
2023
2024 for (;;) {
2025 uint64_t pobj = 0;
2026 char component[MAXNAMELEN + 2];
2027 size_t complen;
2028 int is_xattrdir = 0;
2029
2030 if (prevdb)
2031 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2032
2033 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
2034 &is_xattrdir)) != 0)
2035 break;
2036
2037 if (pobj == obj) {
2038 if (path[0] != '/')
2039 *--path = '/';
2040 break;
2041 }
2042
2043 component[0] = '/';
2044 if (is_xattrdir) {
2045 (void) sprintf(component + 1, "<xattrdir>");
2046 } else {
2047 error = zap_value_search(osp, pobj, obj,
2048 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2049 if (error != 0)
2050 break;
2051 }
2052
2053 complen = strlen(component);
2054 path -= complen;
2055 ASSERT(path >= buf);
2056 bcopy(component, path, complen);
2057 obj = pobj;
2058
2059 if (sa_hdl != hdl) {
2060 prevhdl = sa_hdl;
2061 prevdb = sa_db;
2062 }
2063 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2064 if (error != 0) {
2065 sa_hdl = prevhdl;
2066 sa_db = prevdb;
2067 break;
2068 }
2069 }
2070
2071 if (sa_hdl != NULL && sa_hdl != hdl) {
2072 ASSERT(sa_db != NULL);
2073 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2074 }
2075
2076 if (error == 0)
2077 (void) memmove(buf, path, buf + len - path);
2078
2079 return (error);
2080 }
2081
2082 int
2083 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2084 {
2085 sa_attr_type_t *sa_table;
2086 sa_handle_t *hdl;
2087 dmu_buf_t *db;
2088 int error;
2089
2090 error = zfs_sa_setup(osp, &sa_table);
2091 if (error != 0)
2092 return (error);
2093
2094 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2095 if (error != 0)
2096 return (error);
2097
2098 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2099
2100 zfs_release_sa_handle(hdl, db, FTAG);
2101 return (error);
2102 }
2103
2104 int
2105 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2106 char *buf, int len)
2107 {
2108 char *path = buf + len - 1;
2109 sa_attr_type_t *sa_table;
2110 sa_handle_t *hdl;
2111 dmu_buf_t *db;
2112 int error;
2113
2114 *path = '\0';
2115
2116 error = zfs_sa_setup(osp, &sa_table);
2117 if (error != 0)
2118 return (error);
2119
2120 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2121 if (error != 0)
2122 return (error);
2123
2124 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2125 if (error != 0) {
2126 zfs_release_sa_handle(hdl, db, FTAG);
2127 return (error);
2128 }
2129
2130 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2131
2132 zfs_release_sa_handle(hdl, db, FTAG);
2133 return (error);
2134 }
2135
2136 #if defined(_KERNEL) && defined(HAVE_SPL)
2137 EXPORT_SYMBOL(zfs_create_fs);
2138 EXPORT_SYMBOL(zfs_obj_to_path);
2139
2140 module_param(zfs_object_mutex_size, uint, 0644);
2141 MODULE_PARM_DESC(zfs_object_mutex_size, "Size of znode hold array");
2142 #endif