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