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