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