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