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