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