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