]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_znode.c
Implement fallocate FALLOC_FL_PUNCH_HOLE
[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_PUSHPAGE);
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_PUSHPAGE);
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_PUSHPAGE);
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 iput(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 if (first_page > last_page) {
1444 /* entire punched area within a single page */
1445 zfs_zero_partial_page(zp, off, len);
1446 } else {
1447 /* beginning of punched area at the end of a page */
1448 page_len = first_page_offset - off;
1449 if (page_len > 0)
1450 zfs_zero_partial_page(zp, off, page_len);
1451
1452 /* end of punched area at the beginning of a page */
1453 page_len = off + len - last_page_offset;
1454 if (page_len > 0)
1455 zfs_zero_partial_page(zp, last_page_offset,
1456 page_len);
1457 }
1458 }
1459 zfs_range_unlock(rl);
1460
1461 return (error);
1462 }
1463
1464 /*
1465 * Truncate a file
1466 *
1467 * IN: zp - znode of file to free data in.
1468 * end - new end-of-file.
1469 *
1470 * RETURN: 0 on success, error code on failure
1471 */
1472 static int
1473 zfs_trunc(znode_t *zp, uint64_t end)
1474 {
1475 zfs_sb_t *zsb = ZTOZSB(zp);
1476 dmu_tx_t *tx;
1477 rl_t *rl;
1478 int error;
1479 sa_bulk_attr_t bulk[2];
1480 int count = 0;
1481
1482 /*
1483 * We will change zp_size, lock the whole file.
1484 */
1485 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1486
1487 /*
1488 * Nothing to do if file already at desired length.
1489 */
1490 if (end >= zp->z_size) {
1491 zfs_range_unlock(rl);
1492 return (0);
1493 }
1494
1495 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1496 if (error) {
1497 zfs_range_unlock(rl);
1498 return (error);
1499 }
1500 tx = dmu_tx_create(zsb->z_os);
1501 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1502 zfs_sa_upgrade_txholds(tx, zp);
1503 error = dmu_tx_assign(tx, TXG_WAIT);
1504 if (error) {
1505 dmu_tx_abort(tx);
1506 zfs_range_unlock(rl);
1507 return (error);
1508 }
1509
1510 zp->z_size = end;
1511 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1512 NULL, &zp->z_size, sizeof (zp->z_size));
1513
1514 if (end == 0) {
1515 zp->z_pflags &= ~ZFS_SPARSE;
1516 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1517 NULL, &zp->z_pflags, 8);
1518 }
1519 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1520
1521 dmu_tx_commit(tx);
1522
1523 zfs_range_unlock(rl);
1524
1525 return (0);
1526 }
1527
1528 /*
1529 * Free space in a file
1530 *
1531 * IN: zp - znode of file to free data in.
1532 * off - start of range
1533 * len - end of range (0 => EOF)
1534 * flag - current file open mode flags.
1535 * log - TRUE if this action should be logged
1536 *
1537 * RETURN: 0 on success, error code on failure
1538 */
1539 int
1540 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1541 {
1542 struct inode *ip = ZTOI(zp);
1543 dmu_tx_t *tx;
1544 zfs_sb_t *zsb = ZTOZSB(zp);
1545 zilog_t *zilog = zsb->z_log;
1546 uint64_t mode;
1547 uint64_t mtime[2], ctime[2];
1548 sa_bulk_attr_t bulk[3];
1549 int count = 0;
1550 int error;
1551
1552 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1553 sizeof (mode))) != 0)
1554 return (error);
1555
1556 if (off > zp->z_size) {
1557 error = zfs_extend(zp, off+len);
1558 if (error == 0 && log)
1559 goto log;
1560 goto out;
1561 }
1562
1563 /*
1564 * Check for any locks in the region to be freed.
1565 */
1566 if (ip->i_flock && mandatory_lock(ip)) {
1567 uint64_t length = (len ? len : zp->z_size - off);
1568 if (!lock_may_write(ip, off, length))
1569 return (SET_ERROR(EAGAIN));
1570 }
1571
1572 if (len == 0) {
1573 error = zfs_trunc(zp, off);
1574 } else {
1575 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1576 off + len > zp->z_size)
1577 error = zfs_extend(zp, off+len);
1578 }
1579 if (error || !log)
1580 goto out;
1581 log:
1582 tx = dmu_tx_create(zsb->z_os);
1583 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1584 zfs_sa_upgrade_txholds(tx, zp);
1585 error = dmu_tx_assign(tx, TXG_WAIT);
1586 if (error) {
1587 dmu_tx_abort(tx);
1588 goto out;
1589 }
1590
1591 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1592 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1593 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1594 NULL, &zp->z_pflags, 8);
1595 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1596 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1597 ASSERT(error == 0);
1598
1599 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1600
1601 dmu_tx_commit(tx);
1602
1603 zfs_inode_update(zp);
1604 error = 0;
1605
1606 out:
1607 /*
1608 * Truncate the page cache - for file truncate operations, use
1609 * the purpose-built API for truncations. For punching operations,
1610 * truncate only whole pages within the region; partial pages are
1611 * zeroed under a range lock in zfs_free_range().
1612 */
1613 if (len == 0)
1614 truncate_setsize(ZTOI(zp), off);
1615 else if (zp->z_is_mapped) {
1616 loff_t first_page, last_page;
1617 loff_t first_page_offset, last_page_offset;
1618
1619 /* first possible full page in hole */
1620 first_page = (off + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1621 /* last page of hole */
1622 last_page = (off + len) >> PAGE_CACHE_SHIFT;
1623
1624 /* offset of first_page */
1625 first_page_offset = first_page << PAGE_CACHE_SHIFT;
1626 /* offset of last_page */
1627 last_page_offset = last_page << PAGE_CACHE_SHIFT;
1628
1629 /* truncate whole pages */
1630 if (last_page_offset > first_page_offset) {
1631 truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1632 first_page_offset, last_page_offset - 1);
1633 }
1634 }
1635 return (error);
1636 }
1637
1638 void
1639 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1640 {
1641 struct super_block *sb;
1642 zfs_sb_t *zsb;
1643 uint64_t moid, obj, sa_obj, version;
1644 uint64_t sense = ZFS_CASE_SENSITIVE;
1645 uint64_t norm = 0;
1646 nvpair_t *elem;
1647 int error;
1648 int i;
1649 znode_t *rootzp = NULL;
1650 vattr_t vattr;
1651 znode_t *zp;
1652 zfs_acl_ids_t acl_ids;
1653
1654 /*
1655 * First attempt to create master node.
1656 */
1657 /*
1658 * In an empty objset, there are no blocks to read and thus
1659 * there can be no i/o errors (which we assert below).
1660 */
1661 moid = MASTER_NODE_OBJ;
1662 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1663 DMU_OT_NONE, 0, tx);
1664 ASSERT(error == 0);
1665
1666 /*
1667 * Set starting attributes.
1668 */
1669 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1670 elem = NULL;
1671 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1672 /* For the moment we expect all zpl props to be uint64_ts */
1673 uint64_t val;
1674 char *name;
1675
1676 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1677 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1678 name = nvpair_name(elem);
1679 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1680 if (val < version)
1681 version = val;
1682 } else {
1683 error = zap_update(os, moid, name, 8, 1, &val, tx);
1684 }
1685 ASSERT(error == 0);
1686 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1687 norm = val;
1688 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1689 sense = val;
1690 }
1691 ASSERT(version != 0);
1692 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1693
1694 /*
1695 * Create zap object used for SA attribute registration
1696 */
1697
1698 if (version >= ZPL_VERSION_SA) {
1699 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1700 DMU_OT_NONE, 0, tx);
1701 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1702 ASSERT(error == 0);
1703 } else {
1704 sa_obj = 0;
1705 }
1706 /*
1707 * Create a delete queue.
1708 */
1709 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1710
1711 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1712 ASSERT(error == 0);
1713
1714 /*
1715 * Create root znode. Create minimal znode/inode/zsb/sb
1716 * to allow zfs_mknode to work.
1717 */
1718 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1719 vattr.va_mode = S_IFDIR|0755;
1720 vattr.va_uid = crgetuid(cr);
1721 vattr.va_gid = crgetgid(cr);
1722
1723 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
1724 rootzp->z_moved = 0;
1725 rootzp->z_unlinked = 0;
1726 rootzp->z_atime_dirty = 0;
1727 rootzp->z_is_sa = USE_SA(version, os);
1728
1729 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
1730 zsb->z_os = os;
1731 zsb->z_parent = zsb;
1732 zsb->z_version = version;
1733 zsb->z_use_fuids = USE_FUIDS(version, os);
1734 zsb->z_use_sa = USE_SA(version, os);
1735 zsb->z_norm = norm;
1736
1737 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
1738 sb->s_fs_info = zsb;
1739
1740 ZTOI(rootzp)->i_sb = sb;
1741
1742 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1743 &zsb->z_attr_table);
1744
1745 ASSERT(error == 0);
1746
1747 /*
1748 * Fold case on file systems that are always or sometimes case
1749 * insensitive.
1750 */
1751 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1752 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1753
1754 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1755 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1756 offsetof(znode_t, z_link_node));
1757
1758 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1759 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1760
1761 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1762 cr, NULL, &acl_ids));
1763 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1764 ASSERT3P(zp, ==, rootzp);
1765 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1766 ASSERT(error == 0);
1767 zfs_acl_ids_free(&acl_ids);
1768
1769 atomic_set(&ZTOI(rootzp)->i_count, 0);
1770 sa_handle_destroy(rootzp->z_sa_hdl);
1771 kmem_cache_free(znode_cache, rootzp);
1772
1773 /*
1774 * Create shares directory
1775 */
1776 error = zfs_create_share_dir(zsb, tx);
1777 ASSERT(error == 0);
1778
1779 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1780 mutex_destroy(&zsb->z_hold_mtx[i]);
1781
1782 kmem_free(sb, sizeof (struct super_block));
1783 kmem_free(zsb, sizeof (zfs_sb_t));
1784 }
1785 #endif /* _KERNEL */
1786
1787 static int
1788 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1789 {
1790 uint64_t sa_obj = 0;
1791 int error;
1792
1793 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1794 if (error != 0 && error != ENOENT)
1795 return (error);
1796
1797 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1798 return (error);
1799 }
1800
1801 static int
1802 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1803 dmu_buf_t **db, void *tag)
1804 {
1805 dmu_object_info_t doi;
1806 int error;
1807
1808 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1809 return (error);
1810
1811 dmu_object_info_from_db(*db, &doi);
1812 if ((doi.doi_bonus_type != DMU_OT_SA &&
1813 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1814 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1815 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1816 sa_buf_rele(*db, tag);
1817 return (SET_ERROR(ENOTSUP));
1818 }
1819
1820 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1821 if (error != 0) {
1822 sa_buf_rele(*db, tag);
1823 return (error);
1824 }
1825
1826 return (0);
1827 }
1828
1829 void
1830 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1831 {
1832 sa_handle_destroy(hdl);
1833 sa_buf_rele(db, tag);
1834 }
1835
1836 /*
1837 * Given an object number, return its parent object number and whether
1838 * or not the object is an extended attribute directory.
1839 */
1840 static int
1841 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1842 int *is_xattrdir)
1843 {
1844 uint64_t parent;
1845 uint64_t pflags;
1846 uint64_t mode;
1847 sa_bulk_attr_t bulk[3];
1848 int count = 0;
1849 int error;
1850
1851 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1852 &parent, sizeof (parent));
1853 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1854 &pflags, sizeof (pflags));
1855 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1856 &mode, sizeof (mode));
1857
1858 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1859 return (error);
1860
1861 *pobjp = parent;
1862 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1863
1864 return (0);
1865 }
1866
1867 /*
1868 * Given an object number, return some zpl level statistics
1869 */
1870 static int
1871 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1872 zfs_stat_t *sb)
1873 {
1874 sa_bulk_attr_t bulk[4];
1875 int count = 0;
1876
1877 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1878 &sb->zs_mode, sizeof (sb->zs_mode));
1879 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1880 &sb->zs_gen, sizeof (sb->zs_gen));
1881 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1882 &sb->zs_links, sizeof (sb->zs_links));
1883 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1884 &sb->zs_ctime, sizeof (sb->zs_ctime));
1885
1886 return (sa_bulk_lookup(hdl, bulk, count));
1887 }
1888
1889 static int
1890 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1891 sa_attr_type_t *sa_table, char *buf, int len)
1892 {
1893 sa_handle_t *sa_hdl;
1894 sa_handle_t *prevhdl = NULL;
1895 dmu_buf_t *prevdb = NULL;
1896 dmu_buf_t *sa_db = NULL;
1897 char *path = buf + len - 1;
1898 int error;
1899
1900 *path = '\0';
1901 sa_hdl = hdl;
1902
1903 for (;;) {
1904 uint64_t pobj = 0;
1905 char component[MAXNAMELEN + 2];
1906 size_t complen;
1907 int is_xattrdir = 0;
1908
1909 if (prevdb)
1910 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1911
1912 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1913 &is_xattrdir)) != 0)
1914 break;
1915
1916 if (pobj == obj) {
1917 if (path[0] != '/')
1918 *--path = '/';
1919 break;
1920 }
1921
1922 component[0] = '/';
1923 if (is_xattrdir) {
1924 (void) sprintf(component + 1, "<xattrdir>");
1925 } else {
1926 error = zap_value_search(osp, pobj, obj,
1927 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1928 if (error != 0)
1929 break;
1930 }
1931
1932 complen = strlen(component);
1933 path -= complen;
1934 ASSERT(path >= buf);
1935 bcopy(component, path, complen);
1936 obj = pobj;
1937
1938 if (sa_hdl != hdl) {
1939 prevhdl = sa_hdl;
1940 prevdb = sa_db;
1941 }
1942 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1943 if (error != 0) {
1944 sa_hdl = prevhdl;
1945 sa_db = prevdb;
1946 break;
1947 }
1948 }
1949
1950 if (sa_hdl != NULL && sa_hdl != hdl) {
1951 ASSERT(sa_db != NULL);
1952 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1953 }
1954
1955 if (error == 0)
1956 (void) memmove(buf, path, buf + len - path);
1957
1958 return (error);
1959 }
1960
1961 int
1962 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1963 {
1964 sa_attr_type_t *sa_table;
1965 sa_handle_t *hdl;
1966 dmu_buf_t *db;
1967 int error;
1968
1969 error = zfs_sa_setup(osp, &sa_table);
1970 if (error != 0)
1971 return (error);
1972
1973 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1974 if (error != 0)
1975 return (error);
1976
1977 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1978
1979 zfs_release_sa_handle(hdl, db, FTAG);
1980 return (error);
1981 }
1982
1983 int
1984 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1985 char *buf, int len)
1986 {
1987 char *path = buf + len - 1;
1988 sa_attr_type_t *sa_table;
1989 sa_handle_t *hdl;
1990 dmu_buf_t *db;
1991 int error;
1992
1993 *path = '\0';
1994
1995 error = zfs_sa_setup(osp, &sa_table);
1996 if (error != 0)
1997 return (error);
1998
1999 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2000 if (error != 0)
2001 return (error);
2002
2003 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2004 if (error != 0) {
2005 zfs_release_sa_handle(hdl, db, FTAG);
2006 return (error);
2007 }
2008
2009 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2010
2011 zfs_release_sa_handle(hdl, db, FTAG);
2012 return (error);
2013 }
2014
2015 #if defined(_KERNEL) && defined(HAVE_SPL)
2016 EXPORT_SYMBOL(zfs_create_fs);
2017 EXPORT_SYMBOL(zfs_obj_to_path);
2018 #endif