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