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