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