1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 */
25
26 /* Portions Copyright 2007 Jeremy Teo */
27
28 #ifdef _KERNEL
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/sysmacros.h>
33 #include <sys/mntent.h>
34 #include <sys/u8_textprep.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/vfs.h>
37 #include <sys/vnode.h>
38 #include <sys/file.h>
39 #include <sys/kmem.h>
40 #include <sys/errno.h>
41 #include <sys/atomic.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zfs_acl.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/zfs_rlock.h>
46 #include <sys/zfs_fuid.h>
47 #include <sys/zfs_vnops.h>
48 #include <sys/zfs_ctldir.h>
49 #include <sys/dnode.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/zpl.h>
52 #endif /* _KERNEL */
53
54 #include <sys/dmu.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/dmu_tx.h>
57 #include <sys/zfs_refcount.h>
58 #include <sys/stat.h>
59 #include <sys/zap.h>
60 #include <sys/zfs_znode.h>
61 #include <sys/sa.h>
62 #include <sys/zfs_sa.h>
63 #include <sys/zfs_stat.h>
64
65 #include "zfs_prop.h"
66 #include "zfs_comutil.h"
67
68 /*
69 * Functions needed for userland (ie: libzpool) are not put under
70 * #ifdef_KERNEL; the rest of the functions have dependencies
71 * (such as VFS logic) that will not compile easily in userland.
72 */
73 #ifdef _KERNEL
74
75 static kmem_cache_t *znode_cache = NULL;
76 static kmem_cache_t *znode_hold_cache = NULL;
77 unsigned int zfs_object_mutex_size = ZFS_OBJ_MTX_SZ;
78
79 /*
80 * This is used by the test suite so that it can delay znodes from being
81 * freed in order to inspect the unlinked set.
82 */
83 int zfs_unlink_suspend_progress = 0;
84
85 /*
86 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
87 * z_rangelock. It will modify the offset and length of the lock to reflect
88 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is
89 * called with the rangelock_t's rl_lock held, which avoids races.
90 */
91 static void
zfs_rangelock_cb(zfs_locked_range_t * new,void * arg)92 zfs_rangelock_cb(zfs_locked_range_t *new, void *arg)
93 {
94 znode_t *zp = arg;
95
96 /*
97 * If in append mode, convert to writer and lock starting at the
98 * current end of file.
99 */
100 if (new->lr_type == RL_APPEND) {
101 new->lr_offset = zp->z_size;
102 new->lr_type = RL_WRITER;
103 }
104
105 /*
106 * If we need to grow the block size then lock the whole file range.
107 */
108 uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
109 if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
110 zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) {
111 new->lr_offset = 0;
112 new->lr_length = UINT64_MAX;
113 }
114 }
115
116 /*ARGSUSED*/
117 static int
zfs_znode_cache_constructor(void * buf,void * arg,int kmflags)118 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
119 {
120 znode_t *zp = buf;
121
122 inode_init_once(ZTOI(zp));
123 list_link_init(&zp->z_link_node);
124
125 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
126 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127 rw_init(&zp->z_name_lock, NULL, RW_NOLOCKDEP, NULL);
128 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
130
131 zfs_rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
132
133 zp->z_dirlocks = NULL;
134 zp->z_acl_cached = NULL;
135 zp->z_xattr_cached = NULL;
136 zp->z_xattr_parent = 0;
137 return (0);
138 }
139
140 /*ARGSUSED*/
141 static void
zfs_znode_cache_destructor(void * buf,void * arg)142 zfs_znode_cache_destructor(void *buf, void *arg)
143 {
144 znode_t *zp = buf;
145
146 ASSERT(!list_link_active(&zp->z_link_node));
147 mutex_destroy(&zp->z_lock);
148 rw_destroy(&zp->z_parent_lock);
149 rw_destroy(&zp->z_name_lock);
150 mutex_destroy(&zp->z_acl_lock);
151 rw_destroy(&zp->z_xattr_lock);
152 zfs_rangelock_fini(&zp->z_rangelock);
153
154 ASSERT(zp->z_dirlocks == NULL);
155 ASSERT(zp->z_acl_cached == NULL);
156 ASSERT(zp->z_xattr_cached == NULL);
157 }
158
159 static int
zfs_znode_hold_cache_constructor(void * buf,void * arg,int kmflags)160 zfs_znode_hold_cache_constructor(void *buf, void *arg, int kmflags)
161 {
162 znode_hold_t *zh = buf;
163
164 mutex_init(&zh->zh_lock, NULL, MUTEX_DEFAULT, NULL);
165 zfs_refcount_create(&zh->zh_refcount);
166 zh->zh_obj = ZFS_NO_OBJECT;
167
168 return (0);
169 }
170
171 static void
zfs_znode_hold_cache_destructor(void * buf,void * arg)172 zfs_znode_hold_cache_destructor(void *buf, void *arg)
173 {
174 znode_hold_t *zh = buf;
175
176 mutex_destroy(&zh->zh_lock);
177 zfs_refcount_destroy(&zh->zh_refcount);
178 }
179
180 void
zfs_znode_init(void)181 zfs_znode_init(void)
182 {
183 /*
184 * Initialize zcache. The KMC_SLAB hint is used in order that it be
185 * backed by kmalloc() when on the Linux slab in order that any
186 * wait_on_bit() operations on the related inode operate properly.
187 */
188 ASSERT(znode_cache == NULL);
189 znode_cache = kmem_cache_create("zfs_znode_cache",
190 sizeof (znode_t), 0, zfs_znode_cache_constructor,
191 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_SLAB);
192
193 ASSERT(znode_hold_cache == NULL);
194 znode_hold_cache = kmem_cache_create("zfs_znode_hold_cache",
195 sizeof (znode_hold_t), 0, zfs_znode_hold_cache_constructor,
196 zfs_znode_hold_cache_destructor, NULL, NULL, NULL, 0);
197 }
198
199 void
zfs_znode_fini(void)200 zfs_znode_fini(void)
201 {
202 /*
203 * Cleanup zcache
204 */
205 if (znode_cache)
206 kmem_cache_destroy(znode_cache);
207 znode_cache = NULL;
208
209 if (znode_hold_cache)
210 kmem_cache_destroy(znode_hold_cache);
211 znode_hold_cache = NULL;
212 }
213
214 /*
215 * The zfs_znode_hold_enter() / zfs_znode_hold_exit() functions are used to
216 * serialize access to a znode and its SA buffer while the object is being
217 * created or destroyed. This kind of locking would normally reside in the
218 * znode itself but in this case that's impossible because the znode and SA
219 * buffer may not yet exist. Therefore the locking is handled externally
220 * with an array of mutexs and AVLs trees which contain per-object locks.
221 *
222 * In zfs_znode_hold_enter() a per-object lock is created as needed, inserted
223 * in to the correct AVL tree and finally the per-object lock is held. In
224 * zfs_znode_hold_exit() the process is reversed. The per-object lock is
225 * released, removed from the AVL tree and destroyed if there are no waiters.
226 *
227 * This scheme has two important properties:
228 *
229 * 1) No memory allocations are performed while holding one of the z_hold_locks.
230 * This ensures evict(), which can be called from direct memory reclaim, will
231 * never block waiting on a z_hold_locks which just happens to have hashed
232 * to the same index.
233 *
234 * 2) All locks used to serialize access to an object are per-object and never
235 * shared. This minimizes lock contention without creating a large number
236 * of dedicated locks.
237 *
238 * On the downside it does require znode_lock_t structures to be frequently
239 * allocated and freed. However, because these are backed by a kmem cache
240 * and very short lived this cost is minimal.
241 */
242 int
zfs_znode_hold_compare(const void * a,const void * b)243 zfs_znode_hold_compare(const void *a, const void *b)
244 {
245 const znode_hold_t *zh_a = (const znode_hold_t *)a;
246 const znode_hold_t *zh_b = (const znode_hold_t *)b;
247
248 return (TREE_CMP(zh_a->zh_obj, zh_b->zh_obj));
249 }
250
251 static boolean_t __maybe_unused
zfs_znode_held(zfsvfs_t * zfsvfs,uint64_t obj)252 zfs_znode_held(zfsvfs_t *zfsvfs, uint64_t obj)
253 {
254 znode_hold_t *zh, search;
255 int i = ZFS_OBJ_HASH(zfsvfs, obj);
256 boolean_t held;
257
258 search.zh_obj = obj;
259
260 mutex_enter(&zfsvfs->z_hold_locks[i]);
261 zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
262 held = (zh && MUTEX_HELD(&zh->zh_lock)) ? B_TRUE : B_FALSE;
263 mutex_exit(&zfsvfs->z_hold_locks[i]);
264
265 return (held);
266 }
267
268 static znode_hold_t *
zfs_znode_hold_enter(zfsvfs_t * zfsvfs,uint64_t obj)269 zfs_znode_hold_enter(zfsvfs_t *zfsvfs, uint64_t obj)
270 {
271 znode_hold_t *zh, *zh_new, search;
272 int i = ZFS_OBJ_HASH(zfsvfs, obj);
273 boolean_t found = B_FALSE;
274
275 zh_new = kmem_cache_alloc(znode_hold_cache, KM_SLEEP);
276 zh_new->zh_obj = obj;
277 search.zh_obj = obj;
278
279 mutex_enter(&zfsvfs->z_hold_locks[i]);
280 zh = avl_find(&zfsvfs->z_hold_trees[i], &search, NULL);
281 if (likely(zh == NULL)) {
282 zh = zh_new;
283 avl_add(&zfsvfs->z_hold_trees[i], zh);
284 } else {
285 ASSERT3U(zh->zh_obj, ==, obj);
286 found = B_TRUE;
287 }
288 zfs_refcount_add(&zh->zh_refcount, NULL);
289 mutex_exit(&zfsvfs->z_hold_locks[i]);
290
291 if (found == B_TRUE)
292 kmem_cache_free(znode_hold_cache, zh_new);
293
294 ASSERT(MUTEX_NOT_HELD(&zh->zh_lock));
295 ASSERT3S(zfs_refcount_count(&zh->zh_refcount), >, 0);
296 mutex_enter(&zh->zh_lock);
297
298 return (zh);
299 }
300
301 static void
zfs_znode_hold_exit(zfsvfs_t * zfsvfs,znode_hold_t * zh)302 zfs_znode_hold_exit(zfsvfs_t *zfsvfs, znode_hold_t *zh)
303 {
304 int i = ZFS_OBJ_HASH(zfsvfs, zh->zh_obj);
305 boolean_t remove = B_FALSE;
306
307 ASSERT(zfs_znode_held(zfsvfs, zh->zh_obj));
308 ASSERT3S(zfs_refcount_count(&zh->zh_refcount), >, 0);
309 mutex_exit(&zh->zh_lock);
310
311 mutex_enter(&zfsvfs->z_hold_locks[i]);
312 if (zfs_refcount_remove(&zh->zh_refcount, NULL) == 0) {
313 avl_remove(&zfsvfs->z_hold_trees[i], zh);
314 remove = B_TRUE;
315 }
316 mutex_exit(&zfsvfs->z_hold_locks[i]);
317
318 if (remove == B_TRUE)
319 kmem_cache_free(znode_hold_cache, zh);
320 }
321
322 dev_t
zfs_cmpldev(uint64_t dev)323 zfs_cmpldev(uint64_t dev)
324 {
325 return (dev);
326 }
327
328 static void
zfs_znode_sa_init(zfsvfs_t * zfsvfs,znode_t * zp,dmu_buf_t * db,dmu_object_type_t obj_type,sa_handle_t * sa_hdl)329 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
330 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
331 {
332 ASSERT(zfs_znode_held(zfsvfs, zp->z_id));
333
334 mutex_enter(&zp->z_lock);
335
336 ASSERT(zp->z_sa_hdl == NULL);
337 ASSERT(zp->z_acl_cached == NULL);
338 if (sa_hdl == NULL) {
339 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
340 SA_HDL_SHARED, &zp->z_sa_hdl));
341 } else {
342 zp->z_sa_hdl = sa_hdl;
343 sa_set_userp(sa_hdl, zp);
344 }
345
346 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
347
348 mutex_exit(&zp->z_lock);
349 }
350
351 void
zfs_znode_dmu_fini(znode_t * zp)352 zfs_znode_dmu_fini(znode_t *zp)
353 {
354 ASSERT(zfs_znode_held(ZTOZSB(zp), zp->z_id) || zp->z_unlinked ||
355 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
356
357 sa_handle_destroy(zp->z_sa_hdl);
358 zp->z_sa_hdl = NULL;
359 }
360
361 /*
362 * Called by new_inode() to allocate a new inode.
363 */
364 int
zfs_inode_alloc(struct super_block * sb,struct inode ** ip)365 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
366 {
367 znode_t *zp;
368
369 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
370 *ip = ZTOI(zp);
371
372 return (0);
373 }
374
375 /*
376 * Called in multiple places when an inode should be destroyed.
377 */
378 void
zfs_inode_destroy(struct inode * ip)379 zfs_inode_destroy(struct inode *ip)
380 {
381 znode_t *zp = ITOZ(ip);
382 zfsvfs_t *zfsvfs = ZTOZSB(zp);
383
384 mutex_enter(&zfsvfs->z_znodes_lock);
385 if (list_link_active(&zp->z_link_node)) {
386 list_remove(&zfsvfs->z_all_znodes, zp);
387 zfsvfs->z_nr_znodes--;
388 }
389 mutex_exit(&zfsvfs->z_znodes_lock);
390
391 if (zp->z_acl_cached) {
392 zfs_acl_free(zp->z_acl_cached);
393 zp->z_acl_cached = NULL;
394 }
395
396 if (zp->z_xattr_cached) {
397 nvlist_free(zp->z_xattr_cached);
398 zp->z_xattr_cached = NULL;
399 }
400
401 kmem_cache_free(znode_cache, zp);
402 }
403
404 static void
zfs_inode_set_ops(zfsvfs_t * zfsvfs,struct inode * ip)405 zfs_inode_set_ops(zfsvfs_t *zfsvfs, struct inode *ip)
406 {
407 uint64_t rdev = 0;
408
409 switch (ip->i_mode & S_IFMT) {
410 case S_IFREG:
411 ip->i_op = &zpl_inode_operations;
412 ip->i_fop = &zpl_file_operations;
413 ip->i_mapping->a_ops = &zpl_address_space_operations;
414 break;
415
416 case S_IFDIR:
417 ip->i_op = &zpl_dir_inode_operations;
418 ip->i_fop = &zpl_dir_file_operations;
419 ITOZ(ip)->z_zn_prefetch = B_TRUE;
420 break;
421
422 case S_IFLNK:
423 ip->i_op = &zpl_symlink_inode_operations;
424 break;
425
426 /*
427 * rdev is only stored in a SA only for device files.
428 */
429 case S_IFCHR:
430 case S_IFBLK:
431 (void) sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), &rdev,
432 sizeof (rdev));
433 /*FALLTHROUGH*/
434 case S_IFIFO:
435 case S_IFSOCK:
436 init_special_inode(ip, ip->i_mode, rdev);
437 ip->i_op = &zpl_special_inode_operations;
438 break;
439
440 default:
441 zfs_panic_recover("inode %llu has invalid mode: 0x%x\n",
442 (u_longlong_t)ip->i_ino, ip->i_mode);
443
444 /* Assume the inode is a file and attempt to continue */
445 ip->i_mode = S_IFREG | 0644;
446 ip->i_op = &zpl_inode_operations;
447 ip->i_fop = &zpl_file_operations;
448 ip->i_mapping->a_ops = &zpl_address_space_operations;
449 break;
450 }
451 }
452
453 static void
zfs_set_inode_flags(znode_t * zp,struct inode * ip)454 zfs_set_inode_flags(znode_t *zp, struct inode *ip)
455 {
456 /*
457 * Linux and Solaris have different sets of file attributes, so we
458 * restrict this conversion to the intersection of the two.
459 */
460 #ifdef HAVE_INODE_SET_FLAGS
461 unsigned int flags = 0;
462 if (zp->z_pflags & ZFS_IMMUTABLE)
463 flags |= S_IMMUTABLE;
464 if (zp->z_pflags & ZFS_APPENDONLY)
465 flags |= S_APPEND;
466
467 inode_set_flags(ip, flags, S_IMMUTABLE|S_APPEND);
468 #else
469 if (zp->z_pflags & ZFS_IMMUTABLE)
470 ip->i_flags |= S_IMMUTABLE;
471 else
472 ip->i_flags &= ~S_IMMUTABLE;
473
474 if (zp->z_pflags & ZFS_APPENDONLY)
475 ip->i_flags |= S_APPEND;
476 else
477 ip->i_flags &= ~S_APPEND;
478 #endif
479 }
480
481 /*
482 * Update the embedded inode given the znode. We should work toward
483 * eliminating this function as soon as possible by removing values
484 * which are duplicated between the znode and inode. If the generic
485 * inode has the correct field it should be used, and the ZFS code
486 * updated to access the inode. This can be done incrementally.
487 */
488 void
zfs_inode_update(znode_t * zp)489 zfs_inode_update(znode_t *zp)
490 {
491 zfsvfs_t *zfsvfs;
492 struct inode *ip;
493 uint32_t blksize;
494 u_longlong_t i_blocks;
495
496 ASSERT(zp != NULL);
497 zfsvfs = ZTOZSB(zp);
498 ip = ZTOI(zp);
499
500 /* Skip .zfs control nodes which do not exist on disk. */
501 if (zfsctl_is_node(ip))
502 return;
503
504 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize, &i_blocks);
505
506 spin_lock(&ip->i_lock);
507 ip->i_mode = zp->z_mode;
508 ip->i_blocks = i_blocks;
509 i_size_write(ip, zp->z_size);
510 spin_unlock(&ip->i_lock);
511 }
512
513
514 /*
515 * Construct a znode+inode and initialize.
516 *
517 * This does not do a call to dmu_set_user() that is
518 * up to the caller to do, in case you don't want to
519 * return the znode
520 */
521 static znode_t *
zfs_znode_alloc(zfsvfs_t * zfsvfs,dmu_buf_t * db,int blksz,dmu_object_type_t obj_type,sa_handle_t * hdl)522 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
523 dmu_object_type_t obj_type, sa_handle_t *hdl)
524 {
525 znode_t *zp;
526 struct inode *ip;
527 uint64_t mode;
528 uint64_t parent;
529 uint64_t tmp_gen;
530 uint64_t links;
531 uint64_t z_uid, z_gid;
532 uint64_t atime[2], mtime[2], ctime[2];
533 uint64_t projid = ZFS_DEFAULT_PROJID;
534 sa_bulk_attr_t bulk[11];
535 int count = 0;
536
537 ASSERT(zfsvfs != NULL);
538
539 ip = new_inode(zfsvfs->z_sb);
540 if (ip == NULL)
541 return (NULL);
542
543 zp = ITOZ(ip);
544 ASSERT(zp->z_dirlocks == NULL);
545 ASSERT3P(zp->z_acl_cached, ==, NULL);
546 ASSERT3P(zp->z_xattr_cached, ==, NULL);
547 zp->z_unlinked = B_FALSE;
548 zp->z_atime_dirty = B_FALSE;
549 zp->z_is_mapped = B_FALSE;
550 zp->z_is_ctldir = B_FALSE;
551 zp->z_is_stale = B_FALSE;
552 zp->z_suspended = B_FALSE;
553 zp->z_sa_hdl = NULL;
554 zp->z_mapcnt = 0;
555 zp->z_id = db->db_object;
556 zp->z_blksz = blksz;
557 zp->z_seq = 0x7A4653;
558 zp->z_sync_cnt = 0;
559
560 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
561
562 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
563 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
564 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
565 &zp->z_size, 8);
566 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
567 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
568 &zp->z_pflags, 8);
569 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
570 &parent, 8);
571 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &z_uid, 8);
572 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &z_gid, 8);
573 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
574 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
575 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
576
577 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || tmp_gen == 0 ||
578 (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
579 (zp->z_pflags & ZFS_PROJID) &&
580 sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) {
581 if (hdl == NULL)
582 sa_handle_destroy(zp->z_sa_hdl);
583 zp->z_sa_hdl = NULL;
584 goto error;
585 }
586
587 zp->z_projid = projid;
588 zp->z_mode = ip->i_mode = mode;
589 ip->i_generation = (uint32_t)tmp_gen;
590 ip->i_blkbits = SPA_MINBLOCKSHIFT;
591 set_nlink(ip, (uint32_t)links);
592 zfs_uid_write(ip, z_uid);
593 zfs_gid_write(ip, z_gid);
594 zfs_set_inode_flags(zp, ip);
595
596 /* Cache the xattr parent id */
597 if (zp->z_pflags & ZFS_XATTR)
598 zp->z_xattr_parent = parent;
599
600 ZFS_TIME_DECODE(&ip->i_atime, atime);
601 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
602 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
603
604 ip->i_ino = zp->z_id;
605 zfs_inode_update(zp);
606 zfs_inode_set_ops(zfsvfs, ip);
607
608 /*
609 * The only way insert_inode_locked() can fail is if the ip->i_ino
610 * number is already hashed for this super block. This can never
611 * happen because the inode numbers map 1:1 with the object numbers.
612 *
613 * The one exception is rolling back a mounted file system, but in
614 * this case all the active inode are unhashed during the rollback.
615 */
616 VERIFY3S(insert_inode_locked(ip), ==, 0);
617
618 mutex_enter(&zfsvfs->z_znodes_lock);
619 list_insert_tail(&zfsvfs->z_all_znodes, zp);
620 zfsvfs->z_nr_znodes++;
621 mutex_exit(&zfsvfs->z_znodes_lock);
622
623 unlock_new_inode(ip);
624 return (zp);
625
626 error:
627 iput(ip);
628 return (NULL);
629 }
630
631 /*
632 * Safely mark an inode dirty. Inodes which are part of a read-only
633 * file system or snapshot may not be dirtied.
634 */
635 void
zfs_mark_inode_dirty(struct inode * ip)636 zfs_mark_inode_dirty(struct inode *ip)
637 {
638 zfsvfs_t *zfsvfs = ITOZSB(ip);
639
640 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
641 return;
642
643 mark_inode_dirty(ip);
644 }
645
646 static uint64_t empty_xattr;
647 static uint64_t pad[4];
648 static zfs_acl_phys_t acl_phys;
649 /*
650 * Create a new DMU object to hold a zfs znode.
651 *
652 * IN: dzp - parent directory for new znode
653 * vap - file attributes for new znode
654 * tx - dmu transaction id for zap operations
655 * cr - credentials of caller
656 * flag - flags:
657 * IS_ROOT_NODE - new object will be root
658 * IS_TMPFILE - new object is of O_TMPFILE
659 * IS_XATTR - new object is an attribute
660 * acl_ids - ACL related attributes
661 *
662 * OUT: zpp - allocated znode (set to dzp if IS_ROOT_NODE)
663 *
664 */
665 void
zfs_mknode(znode_t * dzp,vattr_t * vap,dmu_tx_t * tx,cred_t * cr,uint_t flag,znode_t ** zpp,zfs_acl_ids_t * acl_ids)666 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
667 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
668 {
669 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
670 uint64_t mode, size, links, parent, pflags;
671 uint64_t projid = ZFS_DEFAULT_PROJID;
672 uint64_t rdev = 0;
673 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
674 dmu_buf_t *db;
675 inode_timespec_t now;
676 uint64_t gen, obj;
677 int bonuslen;
678 int dnodesize;
679 sa_handle_t *sa_hdl;
680 dmu_object_type_t obj_type;
681 sa_bulk_attr_t *sa_attrs;
682 int cnt = 0;
683 zfs_acl_locator_cb_t locate = { 0 };
684 znode_hold_t *zh;
685
686 if (zfsvfs->z_replay) {
687 obj = vap->va_nodeid;
688 now = vap->va_ctime; /* see zfs_replay_create() */
689 gen = vap->va_nblocks; /* ditto */
690 dnodesize = vap->va_fsid; /* ditto */
691 } else {
692 obj = 0;
693 gethrestime(&now);
694 gen = dmu_tx_get_txg(tx);
695 dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
696 }
697
698 if (dnodesize == 0)
699 dnodesize = DNODE_MIN_SIZE;
700
701 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
702
703 bonuslen = (obj_type == DMU_OT_SA) ?
704 DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
705
706 /*
707 * Create a new DMU object.
708 */
709 /*
710 * There's currently no mechanism for pre-reading the blocks that will
711 * be needed to allocate a new object, so we accept the small chance
712 * that there will be an i/o error and we will fail one of the
713 * assertions below.
714 */
715 if (S_ISDIR(vap->va_mode)) {
716 if (zfsvfs->z_replay) {
717 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
718 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
719 obj_type, bonuslen, dnodesize, tx));
720 } else {
721 obj = zap_create_norm_dnsize(zfsvfs->z_os,
722 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
723 obj_type, bonuslen, dnodesize, tx);
724 }
725 } else {
726 if (zfsvfs->z_replay) {
727 VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
728 DMU_OT_PLAIN_FILE_CONTENTS, 0,
729 obj_type, bonuslen, dnodesize, tx));
730 } else {
731 obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
732 DMU_OT_PLAIN_FILE_CONTENTS, 0,
733 obj_type, bonuslen, dnodesize, tx);
734 }
735 }
736
737 zh = zfs_znode_hold_enter(zfsvfs, obj);
738 VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
739
740 /*
741 * If this is the root, fix up the half-initialized parent pointer
742 * to reference the just-allocated physical data area.
743 */
744 if (flag & IS_ROOT_NODE) {
745 dzp->z_id = obj;
746 }
747
748 /*
749 * If parent is an xattr, so am I.
750 */
751 if (dzp->z_pflags & ZFS_XATTR) {
752 flag |= IS_XATTR;
753 }
754
755 if (zfsvfs->z_use_fuids)
756 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
757 else
758 pflags = 0;
759
760 if (S_ISDIR(vap->va_mode)) {
761 size = 2; /* contents ("." and "..") */
762 links = 2;
763 } else {
764 size = 0;
765 links = (flag & IS_TMPFILE) ? 0 : 1;
766 }
767
768 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
769 rdev = vap->va_rdev;
770
771 parent = dzp->z_id;
772 mode = acl_ids->z_mode;
773 if (flag & IS_XATTR)
774 pflags |= ZFS_XATTR;
775
776 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) {
777 /*
778 * With ZFS_PROJID flag, we can easily know whether there is
779 * project ID stored on disk or not. See zfs_space_delta_cb().
780 */
781 if (obj_type != DMU_OT_ZNODE &&
782 dmu_objset_projectquota_enabled(zfsvfs->z_os))
783 pflags |= ZFS_PROJID;
784
785 /*
786 * Inherit project ID from parent if required.
787 */
788 projid = zfs_inherit_projid(dzp);
789 if (dzp->z_pflags & ZFS_PROJINHERIT)
790 pflags |= ZFS_PROJINHERIT;
791 }
792
793 /*
794 * No execs denied will be determined when zfs_mode_compute() is called.
795 */
796 pflags |= acl_ids->z_aclp->z_hints &
797 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
798 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
799
800 ZFS_TIME_ENCODE(&now, crtime);
801 ZFS_TIME_ENCODE(&now, ctime);
802
803 if (vap->va_mask & ATTR_ATIME) {
804 ZFS_TIME_ENCODE(&vap->va_atime, atime);
805 } else {
806 ZFS_TIME_ENCODE(&now, atime);
807 }
808
809 if (vap->va_mask & ATTR_MTIME) {
810 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
811 } else {
812 ZFS_TIME_ENCODE(&now, mtime);
813 }
814
815 /* Now add in all of the "SA" attributes */
816 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
817 &sa_hdl));
818
819 /*
820 * Setup the array of attributes to be replaced/set on the new file
821 *
822 * order for DMU_OT_ZNODE is critical since it needs to be constructed
823 * in the old znode_phys_t format. Don't change this ordering
824 */
825 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
826
827 if (obj_type == DMU_OT_ZNODE) {
828 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
829 NULL, &atime, 16);
830 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
831 NULL, &mtime, 16);
832 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
833 NULL, &ctime, 16);
834 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
835 NULL, &crtime, 16);
836 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
837 NULL, &gen, 8);
838 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
839 NULL, &mode, 8);
840 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
841 NULL, &size, 8);
842 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
843 NULL, &parent, 8);
844 } else {
845 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
846 NULL, &mode, 8);
847 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
848 NULL, &size, 8);
849 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
850 NULL, &gen, 8);
851 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
852 NULL, &acl_ids->z_fuid, 8);
853 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
854 NULL, &acl_ids->z_fgid, 8);
855 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
856 NULL, &parent, 8);
857 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
858 NULL, &pflags, 8);
859 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
860 NULL, &atime, 16);
861 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
862 NULL, &mtime, 16);
863 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
864 NULL, &ctime, 16);
865 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
866 NULL, &crtime, 16);
867 }
868
869 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
870
871 if (obj_type == DMU_OT_ZNODE) {
872 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
873 &empty_xattr, 8);
874 } else if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
875 pflags & ZFS_PROJID) {
876 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PROJID(zfsvfs),
877 NULL, &projid, 8);
878 }
879 if (obj_type == DMU_OT_ZNODE ||
880 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
881 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
882 NULL, &rdev, 8);
883 }
884 if (obj_type == DMU_OT_ZNODE) {
885 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
886 NULL, &pflags, 8);
887 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
888 &acl_ids->z_fuid, 8);
889 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
890 &acl_ids->z_fgid, 8);
891 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
892 sizeof (uint64_t) * 4);
893 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
894 &acl_phys, sizeof (zfs_acl_phys_t));
895 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
896 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
897 &acl_ids->z_aclp->z_acl_count, 8);
898 locate.cb_aclp = acl_ids->z_aclp;
899 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
900 zfs_acl_data_locator, &locate,
901 acl_ids->z_aclp->z_acl_bytes);
902 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
903 acl_ids->z_fuid, acl_ids->z_fgid);
904 }
905
906 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
907
908 if (!(flag & IS_ROOT_NODE)) {
909 /*
910 * The call to zfs_znode_alloc() may fail if memory is low
911 * via the call path: alloc_inode() -> inode_init_always() ->
912 * security_inode_alloc() -> inode_alloc_security(). Since
913 * the existing code is written such that zfs_mknode() can
914 * not fail retry until sufficient memory has been reclaimed.
915 */
916 do {
917 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
918 } while (*zpp == NULL);
919
920 VERIFY(*zpp != NULL);
921 VERIFY(dzp != NULL);
922 } else {
923 /*
924 * If we are creating the root node, the "parent" we
925 * passed in is the znode for the root.
926 */
927 *zpp = dzp;
928
929 (*zpp)->z_sa_hdl = sa_hdl;
930 }
931
932 (*zpp)->z_pflags = pflags;
933 (*zpp)->z_mode = ZTOI(*zpp)->i_mode = mode;
934 (*zpp)->z_dnodesize = dnodesize;
935 (*zpp)->z_projid = projid;
936
937 if (obj_type == DMU_OT_ZNODE ||
938 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
939 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
940 }
941 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
942 zfs_znode_hold_exit(zfsvfs, zh);
943 }
944
945 /*
946 * Update in-core attributes. It is assumed the caller will be doing an
947 * sa_bulk_update to push the changes out.
948 */
949 void
zfs_xvattr_set(znode_t * zp,xvattr_t * xvap,dmu_tx_t * tx)950 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
951 {
952 xoptattr_t *xoap;
953 boolean_t update_inode = B_FALSE;
954
955 xoap = xva_getxoptattr(xvap);
956 ASSERT(xoap);
957
958 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
959 uint64_t times[2];
960 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
961 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
962 ×, sizeof (times), tx);
963 XVA_SET_RTN(xvap, XAT_CREATETIME);
964 }
965 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
966 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
967 zp->z_pflags, tx);
968 XVA_SET_RTN(xvap, XAT_READONLY);
969 }
970 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
971 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
972 zp->z_pflags, tx);
973 XVA_SET_RTN(xvap, XAT_HIDDEN);
974 }
975 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
976 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
977 zp->z_pflags, tx);
978 XVA_SET_RTN(xvap, XAT_SYSTEM);
979 }
980 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
981 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
982 zp->z_pflags, tx);
983 XVA_SET_RTN(xvap, XAT_ARCHIVE);
984 }
985 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
986 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
987 zp->z_pflags, tx);
988 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
989
990 update_inode = B_TRUE;
991 }
992 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
993 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
994 zp->z_pflags, tx);
995 XVA_SET_RTN(xvap, XAT_NOUNLINK);
996 }
997 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
998 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
999 zp->z_pflags, tx);
1000 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1001
1002 update_inode = B_TRUE;
1003 }
1004 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1005 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1006 zp->z_pflags, tx);
1007 XVA_SET_RTN(xvap, XAT_NODUMP);
1008 }
1009 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1010 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1011 zp->z_pflags, tx);
1012 XVA_SET_RTN(xvap, XAT_OPAQUE);
1013 }
1014 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1015 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1016 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1017 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1018 }
1019 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1020 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1021 zp->z_pflags, tx);
1022 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1023 }
1024 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1025 zfs_sa_set_scanstamp(zp, xvap, tx);
1026 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1027 }
1028 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1029 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1030 zp->z_pflags, tx);
1031 XVA_SET_RTN(xvap, XAT_REPARSE);
1032 }
1033 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1034 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1035 zp->z_pflags, tx);
1036 XVA_SET_RTN(xvap, XAT_OFFLINE);
1037 }
1038 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1039 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1040 zp->z_pflags, tx);
1041 XVA_SET_RTN(xvap, XAT_SPARSE);
1042 }
1043 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
1044 ZFS_ATTR_SET(zp, ZFS_PROJINHERIT, xoap->xoa_projinherit,
1045 zp->z_pflags, tx);
1046 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
1047 }
1048
1049 if (update_inode)
1050 zfs_set_inode_flags(zp, ZTOI(zp));
1051 }
1052
1053 int
zfs_zget(zfsvfs_t * zfsvfs,uint64_t obj_num,znode_t ** zpp)1054 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1055 {
1056 dmu_object_info_t doi;
1057 dmu_buf_t *db;
1058 znode_t *zp;
1059 znode_hold_t *zh;
1060 int err;
1061 sa_handle_t *hdl;
1062
1063 *zpp = NULL;
1064
1065 again:
1066 zh = zfs_znode_hold_enter(zfsvfs, obj_num);
1067
1068 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1069 if (err) {
1070 zfs_znode_hold_exit(zfsvfs, zh);
1071 return (err);
1072 }
1073
1074 dmu_object_info_from_db(db, &doi);
1075 if (doi.doi_bonus_type != DMU_OT_SA &&
1076 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1077 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1078 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1079 sa_buf_rele(db, NULL);
1080 zfs_znode_hold_exit(zfsvfs, zh);
1081 return (SET_ERROR(EINVAL));
1082 }
1083
1084 hdl = dmu_buf_get_user(db);
1085 if (hdl != NULL) {
1086 zp = sa_get_userdata(hdl);
1087
1088
1089 /*
1090 * Since "SA" does immediate eviction we
1091 * should never find a sa handle that doesn't
1092 * know about the znode.
1093 */
1094
1095 ASSERT3P(zp, !=, NULL);
1096
1097 mutex_enter(&zp->z_lock);
1098 ASSERT3U(zp->z_id, ==, obj_num);
1099 /*
1100 * If zp->z_unlinked is set, the znode is already marked
1101 * for deletion and should not be discovered. Check this
1102 * after checking igrab() due to fsetxattr() & O_TMPFILE.
1103 *
1104 * If igrab() returns NULL the VFS has independently
1105 * determined the inode should be evicted and has
1106 * called iput_final() to start the eviction process.
1107 * The SA handle is still valid but because the VFS
1108 * requires that the eviction succeed we must drop
1109 * our locks and references to allow the eviction to
1110 * complete. The zfs_zget() may then be retried.
1111 *
1112 * This unlikely case could be optimized by registering
1113 * a sops->drop_inode() callback. The callback would
1114 * need to detect the active SA hold thereby informing
1115 * the VFS that this inode should not be evicted.
1116 */
1117 if (igrab(ZTOI(zp)) == NULL) {
1118 if (zp->z_unlinked)
1119 err = SET_ERROR(ENOENT);
1120 else
1121 err = SET_ERROR(EAGAIN);
1122 } else {
1123 *zpp = zp;
1124 err = 0;
1125 }
1126
1127 mutex_exit(&zp->z_lock);
1128 sa_buf_rele(db, NULL);
1129 zfs_znode_hold_exit(zfsvfs, zh);
1130
1131 if (err == EAGAIN) {
1132 /* inode might need this to finish evict */
1133 cond_resched();
1134 goto again;
1135 }
1136 return (err);
1137 }
1138
1139 /*
1140 * Not found create new znode/vnode but only if file exists.
1141 *
1142 * There is a small window where zfs_vget() could
1143 * find this object while a file create is still in
1144 * progress. This is checked for in zfs_znode_alloc()
1145 *
1146 * if zfs_znode_alloc() fails it will drop the hold on the
1147 * bonus buffer.
1148 */
1149 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1150 doi.doi_bonus_type, NULL);
1151 if (zp == NULL) {
1152 err = SET_ERROR(ENOENT);
1153 } else {
1154 *zpp = zp;
1155 }
1156 zfs_znode_hold_exit(zfsvfs, zh);
1157 return (err);
1158 }
1159
1160 int
zfs_rezget(znode_t * zp)1161 zfs_rezget(znode_t *zp)
1162 {
1163 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1164 dmu_object_info_t doi;
1165 dmu_buf_t *db;
1166 uint64_t obj_num = zp->z_id;
1167 uint64_t mode;
1168 uint64_t links;
1169 sa_bulk_attr_t bulk[10];
1170 int err;
1171 int count = 0;
1172 uint64_t gen;
1173 uint64_t z_uid, z_gid;
1174 uint64_t atime[2], mtime[2], ctime[2];
1175 uint64_t projid = ZFS_DEFAULT_PROJID;
1176 znode_hold_t *zh;
1177
1178 /*
1179 * skip ctldir, otherwise they will always get invalidated. This will
1180 * cause funny behaviour for the mounted snapdirs. Especially for
1181 * Linux >= 3.18, d_invalidate will detach the mountpoint and prevent
1182 * anyone automount it again as long as someone is still using the
1183 * detached mount.
1184 */
1185 if (zp->z_is_ctldir)
1186 return (0);
1187
1188 zh = zfs_znode_hold_enter(zfsvfs, obj_num);
1189
1190 mutex_enter(&zp->z_acl_lock);
1191 if (zp->z_acl_cached) {
1192 zfs_acl_free(zp->z_acl_cached);
1193 zp->z_acl_cached = NULL;
1194 }
1195 mutex_exit(&zp->z_acl_lock);
1196
1197 rw_enter(&zp->z_xattr_lock, RW_WRITER);
1198 if (zp->z_xattr_cached) {
1199 nvlist_free(zp->z_xattr_cached);
1200 zp->z_xattr_cached = NULL;
1201 }
1202 rw_exit(&zp->z_xattr_lock);
1203
1204 ASSERT(zp->z_sa_hdl == NULL);
1205 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1206 if (err) {
1207 zfs_znode_hold_exit(zfsvfs, zh);
1208 return (err);
1209 }
1210
1211 dmu_object_info_from_db(db, &doi);
1212 if (doi.doi_bonus_type != DMU_OT_SA &&
1213 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1214 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1215 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1216 sa_buf_rele(db, NULL);
1217 zfs_znode_hold_exit(zfsvfs, zh);
1218 return (SET_ERROR(EINVAL));
1219 }
1220
1221 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1222
1223 /* reload cached values */
1224 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1225 &gen, sizeof (gen));
1226 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1227 &zp->z_size, sizeof (zp->z_size));
1228 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1229 &links, sizeof (links));
1230 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1231 &zp->z_pflags, sizeof (zp->z_pflags));
1232 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1233 &z_uid, sizeof (z_uid));
1234 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1235 &z_gid, sizeof (z_gid));
1236 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1237 &mode, sizeof (mode));
1238 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1239 &atime, 16);
1240 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1241 &mtime, 16);
1242 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1243 &ctime, 16);
1244
1245 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1246 zfs_znode_dmu_fini(zp);
1247 zfs_znode_hold_exit(zfsvfs, zh);
1248 return (SET_ERROR(EIO));
1249 }
1250
1251 if (dmu_objset_projectquota_enabled(zfsvfs->z_os)) {
1252 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs),
1253 &projid, 8);
1254 if (err != 0 && err != ENOENT) {
1255 zfs_znode_dmu_fini(zp);
1256 zfs_znode_hold_exit(zfsvfs, zh);
1257 return (SET_ERROR(err));
1258 }
1259 }
1260
1261 zp->z_projid = projid;
1262 zp->z_mode = ZTOI(zp)->i_mode = mode;
1263 zfs_uid_write(ZTOI(zp), z_uid);
1264 zfs_gid_write(ZTOI(zp), z_gid);
1265
1266 ZFS_TIME_DECODE(&ZTOI(zp)->i_atime, atime);
1267 ZFS_TIME_DECODE(&ZTOI(zp)->i_mtime, mtime);
1268 ZFS_TIME_DECODE(&ZTOI(zp)->i_ctime, ctime);
1269
1270 if ((uint32_t)gen != ZTOI(zp)->i_generation) {
1271 zfs_znode_dmu_fini(zp);
1272 zfs_znode_hold_exit(zfsvfs, zh);
1273 return (SET_ERROR(EIO));
1274 }
1275
1276 set_nlink(ZTOI(zp), (uint32_t)links);
1277 zfs_set_inode_flags(zp, ZTOI(zp));
1278
1279 zp->z_blksz = doi.doi_data_block_size;
1280 zp->z_atime_dirty = B_FALSE;
1281 zfs_inode_update(zp);
1282
1283 /*
1284 * If the file has zero links, then it has been unlinked on the send
1285 * side and it must be in the received unlinked set.
1286 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1287 * stale data and to prevent automatic removal of the file in
1288 * zfs_zinactive(). The file will be removed either when it is removed
1289 * on the send side and the next incremental stream is received or
1290 * when the unlinked set gets processed.
1291 */
1292 zp->z_unlinked = (ZTOI(zp)->i_nlink == 0);
1293 if (zp->z_unlinked)
1294 zfs_znode_dmu_fini(zp);
1295
1296 zfs_znode_hold_exit(zfsvfs, zh);
1297
1298 return (0);
1299 }
1300
1301 void
zfs_znode_delete(znode_t * zp,dmu_tx_t * tx)1302 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1303 {
1304 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1305 objset_t *os = zfsvfs->z_os;
1306 uint64_t obj = zp->z_id;
1307 uint64_t acl_obj = zfs_external_acl(zp);
1308 znode_hold_t *zh;
1309
1310 zh = zfs_znode_hold_enter(zfsvfs, obj);
1311 if (acl_obj) {
1312 VERIFY(!zp->z_is_sa);
1313 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1314 }
1315 VERIFY(0 == dmu_object_free(os, obj, tx));
1316 zfs_znode_dmu_fini(zp);
1317 zfs_znode_hold_exit(zfsvfs, zh);
1318 }
1319
1320 void
zfs_zinactive(znode_t * zp)1321 zfs_zinactive(znode_t *zp)
1322 {
1323 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1324 uint64_t z_id = zp->z_id;
1325 znode_hold_t *zh;
1326
1327 ASSERT(zp->z_sa_hdl);
1328
1329 /*
1330 * Don't allow a zfs_zget() while were trying to release this znode.
1331 */
1332 zh = zfs_znode_hold_enter(zfsvfs, z_id);
1333
1334 mutex_enter(&zp->z_lock);
1335
1336 /*
1337 * If this was the last reference to a file with no links, remove
1338 * the file from the file system unless the file system is mounted
1339 * read-only. That can happen, for example, if the file system was
1340 * originally read-write, the file was opened, then unlinked and
1341 * the file system was made read-only before the file was finally
1342 * closed. The file will remain in the unlinked set.
1343 */
1344 if (zp->z_unlinked) {
1345 ASSERT(!zfsvfs->z_issnap);
1346 if (!zfs_is_readonly(zfsvfs) && !zfs_unlink_suspend_progress) {
1347 mutex_exit(&zp->z_lock);
1348 zfs_znode_hold_exit(zfsvfs, zh);
1349 zfs_rmnode(zp);
1350 return;
1351 }
1352 }
1353
1354 mutex_exit(&zp->z_lock);
1355 zfs_znode_dmu_fini(zp);
1356
1357 zfs_znode_hold_exit(zfsvfs, zh);
1358 }
1359
1360 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
1361 #define zfs_compare_timespec timespec64_compare
1362 #else
1363 #define zfs_compare_timespec timespec_compare
1364 #endif
1365
1366 /*
1367 * Determine whether the znode's atime must be updated. The logic mostly
1368 * duplicates the Linux kernel's relatime_need_update() functionality.
1369 * This function is only called if the underlying filesystem actually has
1370 * atime updates enabled.
1371 */
1372 boolean_t
zfs_relatime_need_update(const struct inode * ip)1373 zfs_relatime_need_update(const struct inode *ip)
1374 {
1375 inode_timespec_t now;
1376
1377 gethrestime(&now);
1378 /*
1379 * In relatime mode, only update the atime if the previous atime
1380 * is earlier than either the ctime or mtime or if at least a day
1381 * has passed since the last update of atime.
1382 */
1383 if (zfs_compare_timespec(&ip->i_mtime, &ip->i_atime) >= 0)
1384 return (B_TRUE);
1385
1386 if (zfs_compare_timespec(&ip->i_ctime, &ip->i_atime) >= 0)
1387 return (B_TRUE);
1388
1389 if ((hrtime_t)now.tv_sec - (hrtime_t)ip->i_atime.tv_sec >= 24*60*60)
1390 return (B_TRUE);
1391
1392 return (B_FALSE);
1393 }
1394
1395 /*
1396 * Prepare to update znode time stamps.
1397 *
1398 * IN: zp - znode requiring timestamp update
1399 * flag - ATTR_MTIME, ATTR_CTIME flags
1400 *
1401 * OUT: zp - z_seq
1402 * mtime - new mtime
1403 * ctime - new ctime
1404 *
1405 * Note: We don't update atime here, because we rely on Linux VFS to do
1406 * atime updating.
1407 */
1408 void
zfs_tstamp_update_setup(znode_t * zp,uint_t flag,uint64_t mtime[2],uint64_t ctime[2])1409 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1410 uint64_t ctime[2])
1411 {
1412 inode_timespec_t now;
1413
1414 gethrestime(&now);
1415
1416 zp->z_seq++;
1417
1418 if (flag & ATTR_MTIME) {
1419 ZFS_TIME_ENCODE(&now, mtime);
1420 ZFS_TIME_DECODE(&(ZTOI(zp)->i_mtime), mtime);
1421 if (ZTOZSB(zp)->z_use_fuids) {
1422 zp->z_pflags |= (ZFS_ARCHIVE |
1423 ZFS_AV_MODIFIED);
1424 }
1425 }
1426
1427 if (flag & ATTR_CTIME) {
1428 ZFS_TIME_ENCODE(&now, ctime);
1429 ZFS_TIME_DECODE(&(ZTOI(zp)->i_ctime), ctime);
1430 if (ZTOZSB(zp)->z_use_fuids)
1431 zp->z_pflags |= ZFS_ARCHIVE;
1432 }
1433 }
1434
1435 /*
1436 * Grow the block size for a file.
1437 *
1438 * IN: zp - znode of file to free data in.
1439 * size - requested block size
1440 * tx - open transaction.
1441 *
1442 * NOTE: this function assumes that the znode is write locked.
1443 */
1444 void
zfs_grow_blocksize(znode_t * zp,uint64_t size,dmu_tx_t * tx)1445 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1446 {
1447 int error;
1448 u_longlong_t dummy;
1449
1450 if (size <= zp->z_blksz)
1451 return;
1452 /*
1453 * If the file size is already greater than the current blocksize,
1454 * we will not grow. If there is more than one block in a file,
1455 * the blocksize cannot change.
1456 */
1457 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1458 return;
1459
1460 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1461 size, 0, tx);
1462
1463 if (error == ENOTSUP)
1464 return;
1465 ASSERT0(error);
1466
1467 /* What blocksize did we actually get? */
1468 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1469 }
1470
1471 /*
1472 * Increase the file length
1473 *
1474 * IN: zp - znode of file to free data in.
1475 * end - new end-of-file
1476 *
1477 * RETURN: 0 on success, error code on failure
1478 */
1479 static int
zfs_extend(znode_t * zp,uint64_t end)1480 zfs_extend(znode_t *zp, uint64_t end)
1481 {
1482 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1483 dmu_tx_t *tx;
1484 zfs_locked_range_t *lr;
1485 uint64_t newblksz;
1486 int error;
1487
1488 /*
1489 * We will change zp_size, lock the whole file.
1490 */
1491 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1492
1493 /*
1494 * Nothing to do if file already at desired length.
1495 */
1496 if (end <= zp->z_size) {
1497 zfs_rangelock_exit(lr);
1498 return (0);
1499 }
1500 tx = dmu_tx_create(zfsvfs->z_os);
1501 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1502 zfs_sa_upgrade_txholds(tx, zp);
1503 if (end > zp->z_blksz &&
1504 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1505 /*
1506 * We are growing the file past the current block size.
1507 */
1508 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1509 /*
1510 * File's blocksize is already larger than the
1511 * "recordsize" property. Only let it grow to
1512 * the next power of 2.
1513 */
1514 ASSERT(!ISP2(zp->z_blksz));
1515 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1516 } else {
1517 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1518 }
1519 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1520 } else {
1521 newblksz = 0;
1522 }
1523
1524 error = dmu_tx_assign(tx, TXG_WAIT);
1525 if (error) {
1526 dmu_tx_abort(tx);
1527 zfs_rangelock_exit(lr);
1528 return (error);
1529 }
1530
1531 if (newblksz)
1532 zfs_grow_blocksize(zp, newblksz, tx);
1533
1534 zp->z_size = end;
1535
1536 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1537 &zp->z_size, sizeof (zp->z_size), tx));
1538
1539 zfs_rangelock_exit(lr);
1540
1541 dmu_tx_commit(tx);
1542
1543 return (0);
1544 }
1545
1546 /*
1547 * zfs_zero_partial_page - Modeled after update_pages() but
1548 * with different arguments and semantics for use by zfs_freesp().
1549 *
1550 * Zeroes a piece of a single page cache entry for zp at offset
1551 * start and length len.
1552 *
1553 * Caller must acquire a range lock on the file for the region
1554 * being zeroed in order that the ARC and page cache stay in sync.
1555 */
1556 static void
zfs_zero_partial_page(znode_t * zp,uint64_t start,uint64_t len)1557 zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len)
1558 {
1559 struct address_space *mp = ZTOI(zp)->i_mapping;
1560 struct page *pp;
1561 int64_t off;
1562 void *pb;
1563
1564 ASSERT((start & PAGE_MASK) == ((start + len - 1) & PAGE_MASK));
1565
1566 off = start & (PAGE_SIZE - 1);
1567 start &= PAGE_MASK;
1568
1569 pp = find_lock_page(mp, start >> PAGE_SHIFT);
1570 if (pp) {
1571 if (mapping_writably_mapped(mp))
1572 flush_dcache_page(pp);
1573
1574 pb = kmap(pp);
1575 bzero(pb + off, len);
1576 kunmap(pp);
1577
1578 if (mapping_writably_mapped(mp))
1579 flush_dcache_page(pp);
1580
1581 mark_page_accessed(pp);
1582 SetPageUptodate(pp);
1583 ClearPageError(pp);
1584 unlock_page(pp);
1585 put_page(pp);
1586 }
1587 }
1588
1589 /*
1590 * Free space in a file.
1591 *
1592 * IN: zp - znode of file to free data in.
1593 * off - start of section to free.
1594 * len - length of section to free.
1595 *
1596 * RETURN: 0 on success, error code on failure
1597 */
1598 static int
zfs_free_range(znode_t * zp,uint64_t off,uint64_t len)1599 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1600 {
1601 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1602 zfs_locked_range_t *lr;
1603 int error;
1604
1605 /*
1606 * Lock the range being freed.
1607 */
1608 lr = zfs_rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
1609
1610 /*
1611 * Nothing to do if file already at desired length.
1612 */
1613 if (off >= zp->z_size) {
1614 zfs_rangelock_exit(lr);
1615 return (0);
1616 }
1617
1618 if (off + len > zp->z_size)
1619 len = zp->z_size - off;
1620
1621 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1622
1623 /*
1624 * Zero partial page cache entries. This must be done under a
1625 * range lock in order to keep the ARC and page cache in sync.
1626 */
1627 if (zp->z_is_mapped) {
1628 loff_t first_page, last_page, page_len;
1629 loff_t first_page_offset, last_page_offset;
1630
1631 /* first possible full page in hole */
1632 first_page = (off + PAGE_SIZE - 1) >> PAGE_SHIFT;
1633 /* last page of hole */
1634 last_page = (off + len) >> PAGE_SHIFT;
1635
1636 /* offset of first_page */
1637 first_page_offset = first_page << PAGE_SHIFT;
1638 /* offset of last_page */
1639 last_page_offset = last_page << PAGE_SHIFT;
1640
1641 /* truncate whole pages */
1642 if (last_page_offset > first_page_offset) {
1643 truncate_inode_pages_range(ZTOI(zp)->i_mapping,
1644 first_page_offset, last_page_offset - 1);
1645 }
1646
1647 /* truncate sub-page ranges */
1648 if (first_page > last_page) {
1649 /* entire punched area within a single page */
1650 zfs_zero_partial_page(zp, off, len);
1651 } else {
1652 /* beginning of punched area at the end of a page */
1653 page_len = first_page_offset - off;
1654 if (page_len > 0)
1655 zfs_zero_partial_page(zp, off, page_len);
1656
1657 /* end of punched area at the beginning of a page */
1658 page_len = off + len - last_page_offset;
1659 if (page_len > 0)
1660 zfs_zero_partial_page(zp, last_page_offset,
1661 page_len);
1662 }
1663 }
1664 zfs_rangelock_exit(lr);
1665
1666 return (error);
1667 }
1668
1669 /*
1670 * Truncate a file
1671 *
1672 * IN: zp - znode of file to free data in.
1673 * end - new end-of-file.
1674 *
1675 * RETURN: 0 on success, error code on failure
1676 */
1677 static int
zfs_trunc(znode_t * zp,uint64_t end)1678 zfs_trunc(znode_t *zp, uint64_t end)
1679 {
1680 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1681 dmu_tx_t *tx;
1682 zfs_locked_range_t *lr;
1683 int error;
1684 sa_bulk_attr_t bulk[2];
1685 int count = 0;
1686
1687 /*
1688 * We will change zp_size, lock the whole file.
1689 */
1690 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1691
1692 /*
1693 * Nothing to do if file already at desired length.
1694 */
1695 if (end >= zp->z_size) {
1696 zfs_rangelock_exit(lr);
1697 return (0);
1698 }
1699
1700 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1701 DMU_OBJECT_END);
1702 if (error) {
1703 zfs_rangelock_exit(lr);
1704 return (error);
1705 }
1706 tx = dmu_tx_create(zfsvfs->z_os);
1707 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1708 zfs_sa_upgrade_txholds(tx, zp);
1709 dmu_tx_mark_netfree(tx);
1710 error = dmu_tx_assign(tx, TXG_WAIT);
1711 if (error) {
1712 dmu_tx_abort(tx);
1713 zfs_rangelock_exit(lr);
1714 return (error);
1715 }
1716
1717 zp->z_size = end;
1718 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1719 NULL, &zp->z_size, sizeof (zp->z_size));
1720
1721 if (end == 0) {
1722 zp->z_pflags &= ~ZFS_SPARSE;
1723 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1724 NULL, &zp->z_pflags, 8);
1725 }
1726 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1727
1728 dmu_tx_commit(tx);
1729 zfs_rangelock_exit(lr);
1730
1731 return (0);
1732 }
1733
1734 /*
1735 * Free space in a file
1736 *
1737 * IN: zp - znode of file to free data in.
1738 * off - start of range
1739 * len - end of range (0 => EOF)
1740 * flag - current file open mode flags.
1741 * log - TRUE if this action should be logged
1742 *
1743 * RETURN: 0 on success, error code on failure
1744 */
1745 int
zfs_freesp(znode_t * zp,uint64_t off,uint64_t len,int flag,boolean_t log)1746 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1747 {
1748 dmu_tx_t *tx;
1749 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1750 zilog_t *zilog = zfsvfs->z_log;
1751 uint64_t mode;
1752 uint64_t mtime[2], ctime[2];
1753 sa_bulk_attr_t bulk[3];
1754 int count = 0;
1755 int error;
1756
1757 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1758 sizeof (mode))) != 0)
1759 return (error);
1760
1761 if (off > zp->z_size) {
1762 error = zfs_extend(zp, off+len);
1763 if (error == 0 && log)
1764 goto log;
1765 goto out;
1766 }
1767
1768 if (len == 0) {
1769 error = zfs_trunc(zp, off);
1770 } else {
1771 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1772 off + len > zp->z_size)
1773 error = zfs_extend(zp, off+len);
1774 }
1775 if (error || !log)
1776 goto out;
1777 log:
1778 tx = dmu_tx_create(zfsvfs->z_os);
1779 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1780 zfs_sa_upgrade_txholds(tx, zp);
1781 error = dmu_tx_assign(tx, TXG_WAIT);
1782 if (error) {
1783 dmu_tx_abort(tx);
1784 goto out;
1785 }
1786
1787 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1788 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1789 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1790 NULL, &zp->z_pflags, 8);
1791 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
1792 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1793 ASSERT(error == 0);
1794
1795 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1796
1797 dmu_tx_commit(tx);
1798
1799 zfs_inode_update(zp);
1800 error = 0;
1801
1802 out:
1803 /*
1804 * Truncate the page cache - for file truncate operations, use
1805 * the purpose-built API for truncations. For punching operations,
1806 * the truncation is handled under a range lock in zfs_free_range.
1807 */
1808 if (len == 0)
1809 truncate_setsize(ZTOI(zp), off);
1810 return (error);
1811 }
1812
1813 void
zfs_create_fs(objset_t * os,cred_t * cr,nvlist_t * zplprops,dmu_tx_t * tx)1814 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1815 {
1816 struct super_block *sb;
1817 zfsvfs_t *zfsvfs;
1818 uint64_t moid, obj, sa_obj, version;
1819 uint64_t sense = ZFS_CASE_SENSITIVE;
1820 uint64_t norm = 0;
1821 nvpair_t *elem;
1822 int size;
1823 int error;
1824 int i;
1825 znode_t *rootzp = NULL;
1826 vattr_t vattr;
1827 znode_t *zp;
1828 zfs_acl_ids_t acl_ids;
1829
1830 /*
1831 * First attempt to create master node.
1832 */
1833 /*
1834 * In an empty objset, there are no blocks to read and thus
1835 * there can be no i/o errors (which we assert below).
1836 */
1837 moid = MASTER_NODE_OBJ;
1838 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1839 DMU_OT_NONE, 0, tx);
1840 ASSERT(error == 0);
1841
1842 /*
1843 * Set starting attributes.
1844 */
1845 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1846 elem = NULL;
1847 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1848 /* For the moment we expect all zpl props to be uint64_ts */
1849 uint64_t val;
1850 char *name;
1851
1852 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1853 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1854 name = nvpair_name(elem);
1855 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1856 if (val < version)
1857 version = val;
1858 } else {
1859 error = zap_update(os, moid, name, 8, 1, &val, tx);
1860 }
1861 ASSERT(error == 0);
1862 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1863 norm = val;
1864 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1865 sense = val;
1866 }
1867 ASSERT(version != 0);
1868 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1869
1870 /*
1871 * Create zap object used for SA attribute registration
1872 */
1873
1874 if (version >= ZPL_VERSION_SA) {
1875 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1876 DMU_OT_NONE, 0, tx);
1877 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1878 ASSERT(error == 0);
1879 } else {
1880 sa_obj = 0;
1881 }
1882 /*
1883 * Create a delete queue.
1884 */
1885 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1886
1887 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1888 ASSERT(error == 0);
1889
1890 /*
1891 * Create root znode. Create minimal znode/inode/zfsvfs/sb
1892 * to allow zfs_mknode to work.
1893 */
1894 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1895 vattr.va_mode = S_IFDIR|0755;
1896 vattr.va_uid = crgetuid(cr);
1897 vattr.va_gid = crgetgid(cr);
1898
1899 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1900 rootzp->z_unlinked = B_FALSE;
1901 rootzp->z_atime_dirty = B_FALSE;
1902 rootzp->z_is_sa = USE_SA(version, os);
1903 rootzp->z_pflags = 0;
1904
1905 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1906 zfsvfs->z_os = os;
1907 zfsvfs->z_parent = zfsvfs;
1908 zfsvfs->z_version = version;
1909 zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1910 zfsvfs->z_use_sa = USE_SA(version, os);
1911 zfsvfs->z_norm = norm;
1912
1913 sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP);
1914 sb->s_fs_info = zfsvfs;
1915
1916 ZTOI(rootzp)->i_sb = sb;
1917
1918 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1919 &zfsvfs->z_attr_table);
1920
1921 ASSERT(error == 0);
1922
1923 /*
1924 * Fold case on file systems that are always or sometimes case
1925 * insensitive.
1926 */
1927 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1928 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1929
1930 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1931 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1932 offsetof(znode_t, z_link_node));
1933
1934 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
1935 zfsvfs->z_hold_size = size;
1936 zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
1937 KM_SLEEP);
1938 zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
1939 for (i = 0; i != size; i++) {
1940 avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
1941 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
1942 mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
1943 }
1944
1945 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1946 cr, NULL, &acl_ids));
1947 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1948 ASSERT3P(zp, ==, rootzp);
1949 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1950 ASSERT(error == 0);
1951 zfs_acl_ids_free(&acl_ids);
1952
1953 atomic_set(&ZTOI(rootzp)->i_count, 0);
1954 sa_handle_destroy(rootzp->z_sa_hdl);
1955 kmem_cache_free(znode_cache, rootzp);
1956
1957 for (i = 0; i != size; i++) {
1958 avl_destroy(&zfsvfs->z_hold_trees[i]);
1959 mutex_destroy(&zfsvfs->z_hold_locks[i]);
1960 }
1961
1962 mutex_destroy(&zfsvfs->z_znodes_lock);
1963
1964 vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
1965 vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
1966 kmem_free(sb, sizeof (struct super_block));
1967 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1968 }
1969 #endif /* _KERNEL */
1970
1971 static int
zfs_sa_setup(objset_t * osp,sa_attr_type_t ** sa_table)1972 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1973 {
1974 uint64_t sa_obj = 0;
1975 int error;
1976
1977 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1978 if (error != 0 && error != ENOENT)
1979 return (error);
1980
1981 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1982 return (error);
1983 }
1984
1985 static int
zfs_grab_sa_handle(objset_t * osp,uint64_t obj,sa_handle_t ** hdlp,dmu_buf_t ** db,void * tag)1986 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1987 dmu_buf_t **db, void *tag)
1988 {
1989 dmu_object_info_t doi;
1990 int error;
1991
1992 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1993 return (error);
1994
1995 dmu_object_info_from_db(*db, &doi);
1996 if ((doi.doi_bonus_type != DMU_OT_SA &&
1997 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1998 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1999 doi.doi_bonus_size < sizeof (znode_phys_t))) {
2000 sa_buf_rele(*db, tag);
2001 return (SET_ERROR(ENOTSUP));
2002 }
2003
2004 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2005 if (error != 0) {
2006 sa_buf_rele(*db, tag);
2007 return (error);
2008 }
2009
2010 return (0);
2011 }
2012
2013 static void
zfs_release_sa_handle(sa_handle_t * hdl,dmu_buf_t * db,void * tag)2014 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2015 {
2016 sa_handle_destroy(hdl);
2017 sa_buf_rele(db, tag);
2018 }
2019
2020 /*
2021 * Given an object number, return its parent object number and whether
2022 * or not the object is an extended attribute directory.
2023 */
2024 static int
zfs_obj_to_pobj(objset_t * osp,sa_handle_t * hdl,sa_attr_type_t * sa_table,uint64_t * pobjp,int * is_xattrdir)2025 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2026 uint64_t *pobjp, int *is_xattrdir)
2027 {
2028 uint64_t parent;
2029 uint64_t pflags;
2030 uint64_t mode;
2031 uint64_t parent_mode;
2032 sa_bulk_attr_t bulk[3];
2033 sa_handle_t *sa_hdl;
2034 dmu_buf_t *sa_db;
2035 int count = 0;
2036 int error;
2037
2038 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2039 &parent, sizeof (parent));
2040 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2041 &pflags, sizeof (pflags));
2042 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2043 &mode, sizeof (mode));
2044
2045 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2046 return (error);
2047
2048 /*
2049 * When a link is removed its parent pointer is not changed and will
2050 * be invalid. There are two cases where a link is removed but the
2051 * file stays around, when it goes to the delete queue and when there
2052 * are additional links.
2053 */
2054 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2055 if (error != 0)
2056 return (error);
2057
2058 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2059 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2060 if (error != 0)
2061 return (error);
2062
2063 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2064
2065 /*
2066 * Extended attributes can be applied to files, directories, etc.
2067 * Otherwise the parent must be a directory.
2068 */
2069 if (!*is_xattrdir && !S_ISDIR(parent_mode))
2070 return (SET_ERROR(EINVAL));
2071
2072 *pobjp = parent;
2073
2074 return (0);
2075 }
2076
2077 /*
2078 * Given an object number, return some zpl level statistics
2079 */
2080 static int
zfs_obj_to_stats_impl(sa_handle_t * hdl,sa_attr_type_t * sa_table,zfs_stat_t * sb)2081 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2082 zfs_stat_t *sb)
2083 {
2084 sa_bulk_attr_t bulk[4];
2085 int count = 0;
2086
2087 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2088 &sb->zs_mode, sizeof (sb->zs_mode));
2089 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2090 &sb->zs_gen, sizeof (sb->zs_gen));
2091 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2092 &sb->zs_links, sizeof (sb->zs_links));
2093 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2094 &sb->zs_ctime, sizeof (sb->zs_ctime));
2095
2096 return (sa_bulk_lookup(hdl, bulk, count));
2097 }
2098
2099 static int
zfs_obj_to_path_impl(objset_t * osp,uint64_t obj,sa_handle_t * hdl,sa_attr_type_t * sa_table,char * buf,int len)2100 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2101 sa_attr_type_t *sa_table, char *buf, int len)
2102 {
2103 sa_handle_t *sa_hdl;
2104 sa_handle_t *prevhdl = NULL;
2105 dmu_buf_t *prevdb = NULL;
2106 dmu_buf_t *sa_db = NULL;
2107 char *path = buf + len - 1;
2108 int error;
2109
2110 *path = '\0';
2111 sa_hdl = hdl;
2112
2113 uint64_t deleteq_obj;
2114 VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
2115 ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
2116 error = zap_lookup_int(osp, deleteq_obj, obj);
2117 if (error == 0) {
2118 return (ESTALE);
2119 } else if (error != ENOENT) {
2120 return (error);
2121 }
2122 error = 0;
2123
2124 for (;;) {
2125 uint64_t pobj = 0;
2126 char component[MAXNAMELEN + 2];
2127 size_t complen;
2128 int is_xattrdir = 0;
2129
2130 if (prevdb)
2131 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2132
2133 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2134 &is_xattrdir)) != 0)
2135 break;
2136
2137 if (pobj == obj) {
2138 if (path[0] != '/')
2139 *--path = '/';
2140 break;
2141 }
2142
2143 component[0] = '/';
2144 if (is_xattrdir) {
2145 (void) sprintf(component + 1, "<xattrdir>");
2146 } else {
2147 error = zap_value_search(osp, pobj, obj,
2148 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2149 if (error != 0)
2150 break;
2151 }
2152
2153 complen = strlen(component);
2154 path -= complen;
2155 ASSERT(path >= buf);
2156 bcopy(component, path, complen);
2157 obj = pobj;
2158
2159 if (sa_hdl != hdl) {
2160 prevhdl = sa_hdl;
2161 prevdb = sa_db;
2162 }
2163 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2164 if (error != 0) {
2165 sa_hdl = prevhdl;
2166 sa_db = prevdb;
2167 break;
2168 }
2169 }
2170
2171 if (sa_hdl != NULL && sa_hdl != hdl) {
2172 ASSERT(sa_db != NULL);
2173 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2174 }
2175
2176 if (error == 0)
2177 (void) memmove(buf, path, buf + len - path);
2178
2179 return (error);
2180 }
2181
2182 int
zfs_obj_to_path(objset_t * osp,uint64_t obj,char * buf,int len)2183 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2184 {
2185 sa_attr_type_t *sa_table;
2186 sa_handle_t *hdl;
2187 dmu_buf_t *db;
2188 int error;
2189
2190 error = zfs_sa_setup(osp, &sa_table);
2191 if (error != 0)
2192 return (error);
2193
2194 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2195 if (error != 0)
2196 return (error);
2197
2198 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2199
2200 zfs_release_sa_handle(hdl, db, FTAG);
2201 return (error);
2202 }
2203
2204 int
zfs_obj_to_stats(objset_t * osp,uint64_t obj,zfs_stat_t * sb,char * buf,int len)2205 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2206 char *buf, int len)
2207 {
2208 char *path = buf + len - 1;
2209 sa_attr_type_t *sa_table;
2210 sa_handle_t *hdl;
2211 dmu_buf_t *db;
2212 int error;
2213
2214 *path = '\0';
2215
2216 error = zfs_sa_setup(osp, &sa_table);
2217 if (error != 0)
2218 return (error);
2219
2220 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2221 if (error != 0)
2222 return (error);
2223
2224 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2225 if (error != 0) {
2226 zfs_release_sa_handle(hdl, db, FTAG);
2227 return (error);
2228 }
2229
2230 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2231
2232 zfs_release_sa_handle(hdl, db, FTAG);
2233 return (error);
2234 }
2235
2236 #if defined(_KERNEL)
2237 EXPORT_SYMBOL(zfs_create_fs);
2238 EXPORT_SYMBOL(zfs_obj_to_path);
2239
2240 /* CSTYLED */
2241 module_param(zfs_object_mutex_size, uint, 0644);
2242 MODULE_PARM_DESC(zfs_object_mutex_size, "Size of znode hold array");
2243 module_param(zfs_unlink_suspend_progress, int, 0644);
2244 MODULE_PARM_DESC(zfs_unlink_suspend_progress, "Set to prevent async unlinks "
2245 "(debug - leaks space into the unlinked set)");
2246 #endif
2247