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