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