]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_znode.c
Linux 4.6 compat: PAGE_CACHE_SIZE removal
[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_MASK) == ((start + len - 1) & PAGE_MASK));
1516
1517 off = start & (PAGE_SIZE - 1);
1518 start &= PAGE_MASK;
1519
1520 pp = find_lock_page(mp, start >> PAGE_SHIFT);
1521 if (pp) {
1522 if (mapping_writably_mapped(mp))
1523 flush_dcache_page(pp);
1524
1525 pb = kmap(pp);
1526 bzero(pb + off, len);
1527 kunmap(pp);
1528
1529 if (mapping_writably_mapped(mp))
1530 flush_dcache_page(pp);
1531
1532 mark_page_accessed(pp);
1533 SetPageUptodate(pp);
1534 ClearPageError(pp);
1535 unlock_page(pp);
1536 put_page(pp);
1537 }
1538 }
1539
1540 /*
1541 * Free space in a file.
1542 *
1543 * IN: zp - znode of file to free data in.
1544 * off - start of section to free.
1545 * len - length of section to free.
1546 *
1547 * RETURN: 0 on success, error code on failure
1548 */
1549 static int
1550 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1551 {
1552 zfs_sb_t *zsb = ZTOZSB(zp);
1553 rl_t *rl;
1554 int error;
1555
1556 /*
1557 * Lock the range being freed.
1558 */
1559 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1560
1561 /*
1562 * Nothing to do if file already at desired length.
1563 */
1564 if (off >= zp->z_size) {
1565 zfs_range_unlock(rl);
1566 return (0);
1567 }
1568
1569 if (off + len > zp->z_size)
1570 len = zp->z_size - off;
1571
1572 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1573
1574 /*
1575 * Zero partial page cache entries. This must be done under a
1576 * range lock in order to keep the ARC and page cache in sync.
1577 */
1578 if (zp->z_is_mapped) {
1579 loff_t first_page, last_page, page_len;
1580 loff_t first_page_offset, last_page_offset;
1581
1582 /* first possible full page in hole */
1583 first_page = (off + PAGE_SIZE - 1) >> PAGE_SHIFT;
1584 /* last page of hole */
1585 last_page = (off + len) >> PAGE_SHIFT;
1586
1587 /* offset of first_page */
1588 first_page_offset = first_page << PAGE_SHIFT;
1589 /* offset of last_page */
1590 last_page_offset = last_page << PAGE_SHIFT;
1591
1592 /* truncate whole pages */
1593 if (last_page_offset > first_page_offset) {
1594 truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1595 first_page_offset, last_page_offset - 1);
1596 }
1597
1598 /* truncate sub-page ranges */
1599 if (first_page > last_page) {
1600 /* entire punched area within a single page */
1601 zfs_zero_partial_page(zp, off, len);
1602 } else {
1603 /* beginning of punched area at the end of a page */
1604 page_len = first_page_offset - off;
1605 if (page_len > 0)
1606 zfs_zero_partial_page(zp, off, page_len);
1607
1608 /* end of punched area at the beginning of a page */
1609 page_len = off + len - last_page_offset;
1610 if (page_len > 0)
1611 zfs_zero_partial_page(zp, last_page_offset,
1612 page_len);
1613 }
1614 }
1615 zfs_range_unlock(rl);
1616
1617 return (error);
1618 }
1619
1620 /*
1621 * Truncate a file
1622 *
1623 * IN: zp - znode of file to free data in.
1624 * end - new end-of-file.
1625 *
1626 * RETURN: 0 on success, error code on failure
1627 */
1628 static int
1629 zfs_trunc(znode_t *zp, uint64_t end)
1630 {
1631 zfs_sb_t *zsb = ZTOZSB(zp);
1632 dmu_tx_t *tx;
1633 rl_t *rl;
1634 int error;
1635 sa_bulk_attr_t bulk[2];
1636 int count = 0;
1637
1638 /*
1639 * We will change zp_size, lock the whole file.
1640 */
1641 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1642
1643 /*
1644 * Nothing to do if file already at desired length.
1645 */
1646 if (end >= zp->z_size) {
1647 zfs_range_unlock(rl);
1648 return (0);
1649 }
1650
1651 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1652 if (error) {
1653 zfs_range_unlock(rl);
1654 return (error);
1655 }
1656 tx = dmu_tx_create(zsb->z_os);
1657 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1658 zfs_sa_upgrade_txholds(tx, zp);
1659 dmu_tx_mark_netfree(tx);
1660 error = dmu_tx_assign(tx, TXG_WAIT);
1661 if (error) {
1662 dmu_tx_abort(tx);
1663 zfs_range_unlock(rl);
1664 return (error);
1665 }
1666
1667 zp->z_size = end;
1668 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1669 NULL, &zp->z_size, sizeof (zp->z_size));
1670
1671 if (end == 0) {
1672 zp->z_pflags &= ~ZFS_SPARSE;
1673 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1674 NULL, &zp->z_pflags, 8);
1675 }
1676 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1677
1678 dmu_tx_commit(tx);
1679
1680 zfs_range_unlock(rl);
1681
1682 return (0);
1683 }
1684
1685 /*
1686 * Free space in a file
1687 *
1688 * IN: zp - znode of file to free data in.
1689 * off - start of range
1690 * len - end of range (0 => EOF)
1691 * flag - current file open mode flags.
1692 * log - TRUE if this action should be logged
1693 *
1694 * RETURN: 0 on success, error code on failure
1695 */
1696 int
1697 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1698 {
1699 dmu_tx_t *tx;
1700 zfs_sb_t *zsb = ZTOZSB(zp);
1701 zilog_t *zilog = zsb->z_log;
1702 uint64_t mode;
1703 uint64_t mtime[2], ctime[2];
1704 sa_bulk_attr_t bulk[3];
1705 int count = 0;
1706 int error;
1707
1708 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1709 sizeof (mode))) != 0)
1710 return (error);
1711
1712 if (off > zp->z_size) {
1713 error = zfs_extend(zp, off+len);
1714 if (error == 0 && log)
1715 goto log;
1716 goto out;
1717 }
1718
1719 if (len == 0) {
1720 error = zfs_trunc(zp, off);
1721 } else {
1722 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1723 off + len > zp->z_size)
1724 error = zfs_extend(zp, off+len);
1725 }
1726 if (error || !log)
1727 goto out;
1728 log:
1729 tx = dmu_tx_create(zsb->z_os);
1730 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1731 zfs_sa_upgrade_txholds(tx, zp);
1732 error = dmu_tx_assign(tx, TXG_WAIT);
1733 if (error) {
1734 dmu_tx_abort(tx);
1735 goto out;
1736 }
1737
1738 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1739 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1740 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1741 NULL, &zp->z_pflags, 8);
1742 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1743 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1744 ASSERT(error == 0);
1745
1746 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1747
1748 dmu_tx_commit(tx);
1749
1750 zfs_inode_update(zp);
1751 error = 0;
1752
1753 out:
1754 /*
1755 * Truncate the page cache - for file truncate operations, use
1756 * the purpose-built API for truncations. For punching operations,
1757 * the truncation is handled under a range lock in zfs_free_range.
1758 */
1759 if (len == 0)
1760 truncate_setsize(ZTOI(zp), off);
1761 return (error);
1762 }
1763
1764 void
1765 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1766 {
1767 struct super_block *sb;
1768 zfs_sb_t *zsb;
1769 uint64_t moid, obj, sa_obj, version;
1770 uint64_t sense = ZFS_CASE_SENSITIVE;
1771 uint64_t norm = 0;
1772 nvpair_t *elem;
1773 int size;
1774 int error;
1775 int i;
1776 znode_t *rootzp = NULL;
1777 vattr_t vattr;
1778 znode_t *zp;
1779 zfs_acl_ids_t acl_ids;
1780
1781 /*
1782 * First attempt to create master node.
1783 */
1784 /*
1785 * In an empty objset, there are no blocks to read and thus
1786 * there can be no i/o errors (which we assert below).
1787 */
1788 moid = MASTER_NODE_OBJ;
1789 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1790 DMU_OT_NONE, 0, tx);
1791 ASSERT(error == 0);
1792
1793 /*
1794 * Set starting attributes.
1795 */
1796 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1797 elem = NULL;
1798 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1799 /* For the moment we expect all zpl props to be uint64_ts */
1800 uint64_t val;
1801 char *name;
1802
1803 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1804 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1805 name = nvpair_name(elem);
1806 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1807 if (val < version)
1808 version = val;
1809 } else {
1810 error = zap_update(os, moid, name, 8, 1, &val, tx);
1811 }
1812 ASSERT(error == 0);
1813 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1814 norm = val;
1815 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1816 sense = val;
1817 }
1818 ASSERT(version != 0);
1819 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1820
1821 /*
1822 * Create zap object used for SA attribute registration
1823 */
1824
1825 if (version >= ZPL_VERSION_SA) {
1826 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1827 DMU_OT_NONE, 0, tx);
1828 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1829 ASSERT(error == 0);
1830 } else {
1831 sa_obj = 0;
1832 }
1833 /*
1834 * Create a delete queue.
1835 */
1836 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1837
1838 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1839 ASSERT(error == 0);
1840
1841 /*
1842 * Create root znode. Create minimal znode/inode/zsb/sb
1843 * to allow zfs_mknode to work.
1844 */
1845 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1846 vattr.va_mode = S_IFDIR|0755;
1847 vattr.va_uid = crgetuid(cr);
1848 vattr.va_gid = crgetgid(cr);
1849
1850 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1851 rootzp->z_moved = 0;
1852 rootzp->z_unlinked = 0;
1853 rootzp->z_atime_dirty = 0;
1854 rootzp->z_is_sa = USE_SA(version, os);
1855
1856 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP);
1857 zsb->z_os = os;
1858 zsb->z_parent = zsb;
1859 zsb->z_version = version;
1860 zsb->z_use_fuids = USE_FUIDS(version, os);
1861 zsb->z_use_sa = USE_SA(version, os);
1862 zsb->z_norm = norm;
1863
1864 sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP);
1865 sb->s_fs_info = zsb;
1866
1867 ZTOI(rootzp)->i_sb = sb;
1868
1869 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1870 &zsb->z_attr_table);
1871
1872 ASSERT(error == 0);
1873
1874 /*
1875 * Fold case on file systems that are always or sometimes case
1876 * insensitive.
1877 */
1878 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1879 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1880
1881 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1882 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1883 offsetof(znode_t, z_link_node));
1884
1885 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
1886 zsb->z_hold_size = size;
1887 zsb->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size, KM_SLEEP);
1888 zsb->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
1889 for (i = 0; i != size; i++) {
1890 avl_create(&zsb->z_hold_trees[i], zfs_znode_hold_compare,
1891 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
1892 mutex_init(&zsb->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
1893 }
1894
1895 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1896 cr, NULL, &acl_ids));
1897 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1898 ASSERT3P(zp, ==, rootzp);
1899 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1900 ASSERT(error == 0);
1901 zfs_acl_ids_free(&acl_ids);
1902
1903 atomic_set(&ZTOI(rootzp)->i_count, 0);
1904 sa_handle_destroy(rootzp->z_sa_hdl);
1905 kmem_cache_free(znode_cache, rootzp);
1906
1907 /*
1908 * Create shares directory
1909 */
1910 error = zfs_create_share_dir(zsb, tx);
1911 ASSERT(error == 0);
1912
1913 for (i = 0; i != size; i++) {
1914 avl_destroy(&zsb->z_hold_trees[i]);
1915 mutex_destroy(&zsb->z_hold_locks[i]);
1916 }
1917
1918 vmem_free(zsb->z_hold_trees, sizeof (avl_tree_t) * size);
1919 vmem_free(zsb->z_hold_locks, sizeof (kmutex_t) * size);
1920 kmem_free(sb, sizeof (struct super_block));
1921 kmem_free(zsb, sizeof (zfs_sb_t));
1922 }
1923 #endif /* _KERNEL */
1924
1925 static int
1926 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1927 {
1928 uint64_t sa_obj = 0;
1929 int error;
1930
1931 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1932 if (error != 0 && error != ENOENT)
1933 return (error);
1934
1935 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1936 return (error);
1937 }
1938
1939 static int
1940 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1941 dmu_buf_t **db, void *tag)
1942 {
1943 dmu_object_info_t doi;
1944 int error;
1945
1946 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1947 return (error);
1948
1949 dmu_object_info_from_db(*db, &doi);
1950 if ((doi.doi_bonus_type != DMU_OT_SA &&
1951 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1952 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1953 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1954 sa_buf_rele(*db, tag);
1955 return (SET_ERROR(ENOTSUP));
1956 }
1957
1958 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1959 if (error != 0) {
1960 sa_buf_rele(*db, tag);
1961 return (error);
1962 }
1963
1964 return (0);
1965 }
1966
1967 void
1968 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1969 {
1970 sa_handle_destroy(hdl);
1971 sa_buf_rele(db, tag);
1972 }
1973
1974 /*
1975 * Given an object number, return its parent object number and whether
1976 * or not the object is an extended attribute directory.
1977 */
1978 static int
1979 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1980 uint64_t *pobjp, int *is_xattrdir)
1981 {
1982 uint64_t parent;
1983 uint64_t pflags;
1984 uint64_t mode;
1985 uint64_t parent_mode;
1986 sa_bulk_attr_t bulk[3];
1987 sa_handle_t *sa_hdl;
1988 dmu_buf_t *sa_db;
1989 int count = 0;
1990 int error;
1991
1992 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1993 &parent, sizeof (parent));
1994 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1995 &pflags, sizeof (pflags));
1996 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1997 &mode, sizeof (mode));
1998
1999 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2000 return (error);
2001
2002 /*
2003 * When a link is removed its parent pointer is not changed and will
2004 * be invalid. There are two cases where a link is removed but the
2005 * file stays around, when it goes to the delete queue and when there
2006 * are additional links.
2007 */
2008 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2009 if (error != 0)
2010 return (error);
2011
2012 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2013 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2014 if (error != 0)
2015 return (error);
2016
2017 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2018
2019 /*
2020 * Extended attributes can be applied to files, directories, etc.
2021 * Otherwise the parent must be a directory.
2022 */
2023 if (!*is_xattrdir && !S_ISDIR(parent_mode))
2024 return (EINVAL);
2025
2026 *pobjp = parent;
2027
2028 return (0);
2029 }
2030
2031 /*
2032 * Given an object number, return some zpl level statistics
2033 */
2034 static int
2035 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2036 zfs_stat_t *sb)
2037 {
2038 sa_bulk_attr_t bulk[4];
2039 int count = 0;
2040
2041 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2042 &sb->zs_mode, sizeof (sb->zs_mode));
2043 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2044 &sb->zs_gen, sizeof (sb->zs_gen));
2045 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2046 &sb->zs_links, sizeof (sb->zs_links));
2047 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2048 &sb->zs_ctime, sizeof (sb->zs_ctime));
2049
2050 return (sa_bulk_lookup(hdl, bulk, count));
2051 }
2052
2053 static int
2054 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2055 sa_attr_type_t *sa_table, char *buf, int len)
2056 {
2057 sa_handle_t *sa_hdl;
2058 sa_handle_t *prevhdl = NULL;
2059 dmu_buf_t *prevdb = NULL;
2060 dmu_buf_t *sa_db = NULL;
2061 char *path = buf + len - 1;
2062 int error;
2063
2064 *path = '\0';
2065 sa_hdl = hdl;
2066
2067 for (;;) {
2068 uint64_t pobj = 0;
2069 char component[MAXNAMELEN + 2];
2070 size_t complen;
2071 int is_xattrdir = 0;
2072
2073 if (prevdb)
2074 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2075
2076 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2077 &is_xattrdir)) != 0)
2078 break;
2079
2080 if (pobj == obj) {
2081 if (path[0] != '/')
2082 *--path = '/';
2083 break;
2084 }
2085
2086 component[0] = '/';
2087 if (is_xattrdir) {
2088 (void) sprintf(component + 1, "<xattrdir>");
2089 } else {
2090 error = zap_value_search(osp, pobj, obj,
2091 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2092 if (error != 0)
2093 break;
2094 }
2095
2096 complen = strlen(component);
2097 path -= complen;
2098 ASSERT(path >= buf);
2099 bcopy(component, path, complen);
2100 obj = pobj;
2101
2102 if (sa_hdl != hdl) {
2103 prevhdl = sa_hdl;
2104 prevdb = sa_db;
2105 }
2106 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2107 if (error != 0) {
2108 sa_hdl = prevhdl;
2109 sa_db = prevdb;
2110 break;
2111 }
2112 }
2113
2114 if (sa_hdl != NULL && sa_hdl != hdl) {
2115 ASSERT(sa_db != NULL);
2116 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2117 }
2118
2119 if (error == 0)
2120 (void) memmove(buf, path, buf + len - path);
2121
2122 return (error);
2123 }
2124
2125 int
2126 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2127 {
2128 sa_attr_type_t *sa_table;
2129 sa_handle_t *hdl;
2130 dmu_buf_t *db;
2131 int error;
2132
2133 error = zfs_sa_setup(osp, &sa_table);
2134 if (error != 0)
2135 return (error);
2136
2137 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2138 if (error != 0)
2139 return (error);
2140
2141 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2142
2143 zfs_release_sa_handle(hdl, db, FTAG);
2144 return (error);
2145 }
2146
2147 int
2148 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2149 char *buf, int len)
2150 {
2151 char *path = buf + len - 1;
2152 sa_attr_type_t *sa_table;
2153 sa_handle_t *hdl;
2154 dmu_buf_t *db;
2155 int error;
2156
2157 *path = '\0';
2158
2159 error = zfs_sa_setup(osp, &sa_table);
2160 if (error != 0)
2161 return (error);
2162
2163 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2164 if (error != 0)
2165 return (error);
2166
2167 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2168 if (error != 0) {
2169 zfs_release_sa_handle(hdl, db, FTAG);
2170 return (error);
2171 }
2172
2173 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2174
2175 zfs_release_sa_handle(hdl, db, FTAG);
2176 return (error);
2177 }
2178
2179 #if defined(_KERNEL) && defined(HAVE_SPL)
2180 EXPORT_SYMBOL(zfs_create_fs);
2181 EXPORT_SYMBOL(zfs_obj_to_path);
2182
2183 module_param(zfs_object_mutex_size, uint, 0644);
2184 MODULE_PARM_DESC(zfs_object_mutex_size, "Size of znode hold array");
2185 #endif