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