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