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