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