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