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