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