xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_inode.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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) 2011, Lawrence Livermore National Security, LLC.
24  * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
25  * Copyright (c) 2025, Rob Norris <robn@despairlabs.com>
26  */
27 
28 
29 #include <sys/sysmacros.h>
30 #include <sys/zfs_ctldir.h>
31 #include <sys/zfs_vfsops.h>
32 #include <sys/zfs_vnops.h>
33 #include <sys/zfs_znode.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/spa_impl.h>
36 #include <sys/vfs.h>
37 #include <sys/zpl.h>
38 #include <sys/file.h>
39 
40 static struct dentry *
41 zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
42 {
43 	cred_t *cr = CRED();
44 	struct inode *ip;
45 	znode_t *zp;
46 	int error;
47 	fstrans_cookie_t cookie;
48 	pathname_t *ppn = NULL;
49 	pathname_t pn;
50 	int zfs_flags = 0;
51 	zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
52 	dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
53 	size_t dlen = dlen(dentry);
54 
55 	/*
56 	 * If z_longname is disabled, disallow create or rename of names
57 	 * longer than ZAP_MAXNAMELEN.
58 	 *
59 	 * This is needed in cases where longname was enabled first and some
60 	 * files/dirs with names > ZAP_MAXNAMELEN were created. And later
61 	 * longname was disabled. In such a case allow access to existing
62 	 * longnames. But disallow creation newer longnamed entities.
63 	 */
64 	if (!zfsvfs->z_longname && (dlen >= ZAP_MAXNAMELEN)) {
65 		/*
66 		 * If this is for create or rename fail it.
67 		 */
68 		if (!dsl_dataset_feature_is_active(ds, SPA_FEATURE_LONGNAME) ||
69 		    (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)))
70 			return (ERR_PTR(-ENAMETOOLONG));
71 	}
72 	if (dlen >= ZAP_MAXNAMELEN_NEW) {
73 		return (ERR_PTR(-ENAMETOOLONG));
74 	}
75 
76 	crhold(cr);
77 	cookie = spl_fstrans_mark();
78 
79 	/* If we are a case insensitive fs, we need the real name */
80 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
81 		zfs_flags = FIGNORECASE;
82 		pn_alloc(&pn);
83 		ppn = &pn;
84 	}
85 
86 	error = -zfs_lookup(ITOZ(dir), dname(dentry), &zp,
87 	    zfs_flags, cr, NULL, ppn);
88 	spl_fstrans_unmark(cookie);
89 	ASSERT3S(error, <=, 0);
90 	crfree(cr);
91 
92 	spin_lock(&dentry->d_lock);
93 	dentry->d_time = jiffies;
94 	spin_unlock(&dentry->d_lock);
95 
96 	if (error) {
97 		/*
98 		 * If we have a case sensitive fs, we do not want to
99 		 * insert negative entries, so return NULL for ENOENT.
100 		 * Fall through if the error is not ENOENT. Also free memory.
101 		 */
102 		if (ppn) {
103 			pn_free(ppn);
104 			if (error == -ENOENT)
105 				return (NULL);
106 		}
107 
108 		if (error == -ENOENT)
109 			return (d_splice_alias(NULL, dentry));
110 		else
111 			return (ERR_PTR(error));
112 	}
113 	ip = ZTOI(zp);
114 
115 	/*
116 	 * If we are case insensitive, call the correct function
117 	 * to install the name.
118 	 */
119 	if (ppn) {
120 		struct dentry *new_dentry;
121 		struct qstr ci_name;
122 
123 		if (strcmp(dname(dentry), pn.pn_buf) == 0) {
124 			new_dentry = d_splice_alias(ip,  dentry);
125 		} else {
126 			ci_name.name = pn.pn_buf;
127 			ci_name.len = strlen(pn.pn_buf);
128 			new_dentry = d_add_ci(dentry, ip, &ci_name);
129 		}
130 		pn_free(ppn);
131 		return (new_dentry);
132 	} else {
133 		return (d_splice_alias(ip, dentry));
134 	}
135 }
136 
137 void
138 zpl_vap_init(vattr_t *vap, struct inode *dir, umode_t mode, cred_t *cr,
139     zidmap_t *mnt_ns)
140 {
141 	vap->va_mask = ATTR_MODE;
142 	vap->va_mode = mode;
143 
144 	vap->va_uid = zfs_vfsuid_to_uid(mnt_ns,
145 	    zfs_i_user_ns(dir), crgetuid(cr));
146 
147 	if (dir->i_mode & S_ISGID) {
148 		vap->va_gid = KGID_TO_SGID(dir->i_gid);
149 		if (S_ISDIR(mode))
150 			vap->va_mode |= S_ISGID;
151 	} else {
152 		vap->va_gid = zfs_vfsgid_to_gid(mnt_ns,
153 		    zfs_i_user_ns(dir), crgetgid(cr));
154 	}
155 }
156 
157 static inline bool
158 is_nametoolong(struct dentry *dentry)
159 {
160 	zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
161 	size_t dlen = dlen(dentry);
162 
163 	return ((!zfsvfs->z_longname && dlen >= ZAP_MAXNAMELEN) ||
164 	    dlen >= ZAP_MAXNAMELEN_NEW);
165 }
166 
167 static int
168 #ifdef HAVE_IOPS_CREATE_USERNS
169 zpl_create(struct user_namespace *user_ns, struct inode *dir,
170     struct dentry *dentry, umode_t mode, bool flag)
171 #elif defined(HAVE_IOPS_CREATE_IDMAP)
172 zpl_create(struct mnt_idmap *user_ns, struct inode *dir,
173     struct dentry *dentry, umode_t mode, bool flag)
174 #else
175 zpl_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool flag)
176 #endif
177 {
178 	cred_t *cr = CRED();
179 	znode_t *zp;
180 	vattr_t *vap;
181 	int error;
182 	fstrans_cookie_t cookie;
183 #if !(defined(HAVE_IOPS_CREATE_USERNS) || defined(HAVE_IOPS_CREATE_IDMAP))
184 	zidmap_t *user_ns = kcred->user_ns;
185 #endif
186 
187 	if (is_nametoolong(dentry)) {
188 		return (-ENAMETOOLONG);
189 	}
190 
191 	crhold(cr);
192 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
193 	zpl_vap_init(vap, dir, mode, cr, user_ns);
194 
195 	cookie = spl_fstrans_mark();
196 	error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0,
197 	    mode, &zp, cr, 0, NULL, user_ns);
198 	if (error == 0) {
199 		error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
200 		if (error == 0)
201 			error = zpl_init_acl(ZTOI(zp), dir);
202 
203 		if (error) {
204 			(void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
205 			remove_inode_hash(ZTOI(zp));
206 			iput(ZTOI(zp));
207 		} else {
208 			d_instantiate(dentry, ZTOI(zp));
209 		}
210 	}
211 
212 	spl_fstrans_unmark(cookie);
213 	kmem_free(vap, sizeof (vattr_t));
214 	crfree(cr);
215 	ASSERT3S(error, <=, 0);
216 
217 	return (error);
218 }
219 
220 static int
221 #ifdef HAVE_IOPS_MKNOD_USERNS
222 zpl_mknod(struct user_namespace *user_ns, struct inode *dir,
223     struct dentry *dentry, umode_t mode,
224 #elif defined(HAVE_IOPS_MKNOD_IDMAP)
225 zpl_mknod(struct mnt_idmap *user_ns, struct inode *dir,
226     struct dentry *dentry, umode_t mode,
227 #else
228 zpl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
229 #endif
230     dev_t rdev)
231 {
232 	cred_t *cr = CRED();
233 	znode_t *zp;
234 	vattr_t *vap;
235 	int error;
236 	fstrans_cookie_t cookie;
237 #if !(defined(HAVE_IOPS_MKNOD_USERNS) || defined(HAVE_IOPS_MKNOD_IDMAP))
238 	zidmap_t *user_ns = kcred->user_ns;
239 #endif
240 
241 	if (is_nametoolong(dentry)) {
242 		return (-ENAMETOOLONG);
243 	}
244 
245 	/*
246 	 * We currently expect Linux to supply rdev=0 for all sockets
247 	 * and fifos, but we want to know if this behavior ever changes.
248 	 */
249 	if (S_ISSOCK(mode) || S_ISFIFO(mode))
250 		ASSERT(rdev == 0);
251 
252 	crhold(cr);
253 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
254 	zpl_vap_init(vap, dir, mode, cr, user_ns);
255 	vap->va_rdev = rdev;
256 
257 	cookie = spl_fstrans_mark();
258 	error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0,
259 	    mode, &zp, cr, 0, NULL, user_ns);
260 	if (error == 0) {
261 		error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
262 		if (error == 0)
263 			error = zpl_init_acl(ZTOI(zp), dir);
264 
265 		if (error) {
266 			(void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
267 			remove_inode_hash(ZTOI(zp));
268 			iput(ZTOI(zp));
269 		} else {
270 			d_instantiate(dentry, ZTOI(zp));
271 		}
272 	}
273 
274 	spl_fstrans_unmark(cookie);
275 	kmem_free(vap, sizeof (vattr_t));
276 	crfree(cr);
277 	ASSERT3S(error, <=, 0);
278 
279 	return (error);
280 }
281 
282 static int
283 #ifdef HAVE_TMPFILE_IDMAP
284 zpl_tmpfile(struct mnt_idmap *userns, struct inode *dir,
285     struct file *file, umode_t mode)
286 #elif !defined(HAVE_TMPFILE_DENTRY)
287 zpl_tmpfile(struct user_namespace *userns, struct inode *dir,
288     struct file *file, umode_t mode)
289 #else
290 #ifdef HAVE_TMPFILE_USERNS
291 zpl_tmpfile(struct user_namespace *userns, struct inode *dir,
292     struct dentry *dentry, umode_t mode)
293 #else
294 zpl_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
295 #endif
296 #endif
297 {
298 	cred_t *cr = CRED();
299 	struct inode *ip;
300 	vattr_t *vap;
301 	int error;
302 	fstrans_cookie_t cookie;
303 #if !(defined(HAVE_TMPFILE_USERNS) || defined(HAVE_TMPFILE_IDMAP))
304 	zidmap_t *userns = kcred->user_ns;
305 #endif
306 
307 	crhold(cr);
308 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
309 	/*
310 	 * The VFS does not apply the umask, therefore it is applied here
311 	 * when POSIX ACLs are not enabled.
312 	 */
313 	if (!IS_POSIXACL(dir))
314 		mode &= ~current_umask();
315 	zpl_vap_init(vap, dir, mode, cr, userns);
316 
317 	cookie = spl_fstrans_mark();
318 	error = -zfs_tmpfile(dir, vap, 0, mode, &ip, cr, 0, NULL, userns);
319 	if (error == 0) {
320 		/* d_tmpfile will do drop_nlink, so we should set it first */
321 		set_nlink(ip, 1);
322 #ifndef HAVE_TMPFILE_DENTRY
323 		d_tmpfile(file, ip);
324 
325 		error = zpl_xattr_security_init(ip, dir,
326 		    &file->f_path.dentry->d_name);
327 #else
328 		d_tmpfile(dentry, ip);
329 
330 		error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
331 #endif
332 		if (error == 0)
333 			error = zpl_init_acl(ip, dir);
334 #ifndef HAVE_TMPFILE_DENTRY
335 		error = finish_open_simple(file, error);
336 #endif
337 		/*
338 		 * don't need to handle error here, file is already in
339 		 * unlinked set.
340 		 */
341 	}
342 
343 	spl_fstrans_unmark(cookie);
344 	kmem_free(vap, sizeof (vattr_t));
345 	crfree(cr);
346 	ASSERT3S(error, <=, 0);
347 
348 	return (error);
349 }
350 
351 static int
352 zpl_unlink(struct inode *dir, struct dentry *dentry)
353 {
354 	cred_t *cr = CRED();
355 	int error;
356 	fstrans_cookie_t cookie;
357 	zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
358 
359 	crhold(cr);
360 	cookie = spl_fstrans_mark();
361 	error = -zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
362 
363 	/*
364 	 * For a CI FS we must invalidate the dentry to prevent the
365 	 * creation of negative entries.
366 	 */
367 	if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE)
368 		d_invalidate(dentry);
369 
370 	spl_fstrans_unmark(cookie);
371 	crfree(cr);
372 	ASSERT3S(error, <=, 0);
373 
374 	return (error);
375 }
376 
377 static int
378 #ifdef HAVE_IOPS_MKDIR_USERNS
379 zpl_mkdir(struct user_namespace *user_ns, struct inode *dir,
380     struct dentry *dentry, umode_t mode)
381 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
382 zpl_mkdir(struct mnt_idmap *user_ns, struct inode *dir,
383     struct dentry *dentry, umode_t mode)
384 #else
385 zpl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
386 #endif
387 {
388 	cred_t *cr = CRED();
389 	vattr_t *vap;
390 	znode_t *zp;
391 	int error;
392 	fstrans_cookie_t cookie;
393 #if !(defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
394 	zidmap_t *user_ns = kcred->user_ns;
395 #endif
396 
397 	if (is_nametoolong(dentry)) {
398 		return (-ENAMETOOLONG);
399 	}
400 
401 	crhold(cr);
402 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
403 	zpl_vap_init(vap, dir, mode | S_IFDIR, cr, user_ns);
404 
405 	cookie = spl_fstrans_mark();
406 	error = -zfs_mkdir(ITOZ(dir), dname(dentry), vap, &zp, cr, 0, NULL,
407 	    user_ns);
408 	if (error == 0) {
409 		error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
410 		if (error == 0)
411 			error = zpl_init_acl(ZTOI(zp), dir);
412 
413 		if (error) {
414 			(void) zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0);
415 			remove_inode_hash(ZTOI(zp));
416 			iput(ZTOI(zp));
417 		} else {
418 			d_instantiate(dentry, ZTOI(zp));
419 		}
420 	}
421 
422 	spl_fstrans_unmark(cookie);
423 	kmem_free(vap, sizeof (vattr_t));
424 	crfree(cr);
425 	ASSERT3S(error, <=, 0);
426 
427 	return (error);
428 }
429 
430 static int
431 zpl_rmdir(struct inode *dir, struct dentry *dentry)
432 {
433 	cred_t *cr = CRED();
434 	int error;
435 	fstrans_cookie_t cookie;
436 	zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
437 
438 	crhold(cr);
439 	cookie = spl_fstrans_mark();
440 	error = -zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0);
441 
442 	/*
443 	 * For a CI FS we must invalidate the dentry to prevent the
444 	 * creation of negative entries.
445 	 */
446 	if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE)
447 		d_invalidate(dentry);
448 
449 	spl_fstrans_unmark(cookie);
450 	crfree(cr);
451 	ASSERT3S(error, <=, 0);
452 
453 	return (error);
454 }
455 
456 static int
457 #ifdef HAVE_USERNS_IOPS_GETATTR
458 zpl_getattr_impl(struct user_namespace *user_ns,
459     const struct path *path, struct kstat *stat, u32 request_mask,
460     unsigned int query_flags)
461 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
462 zpl_getattr_impl(struct mnt_idmap *user_ns,
463     const struct path *path, struct kstat *stat, u32 request_mask,
464     unsigned int query_flags)
465 #else
466 zpl_getattr_impl(const struct path *path, struct kstat *stat, u32 request_mask,
467     unsigned int query_flags)
468 #endif
469 {
470 	int error;
471 	fstrans_cookie_t cookie;
472 	struct inode *ip = path->dentry->d_inode;
473 	znode_t *zp __maybe_unused = ITOZ(ip);
474 
475 	cookie = spl_fstrans_mark();
476 
477 	/*
478 	 * XXX query_flags currently ignored.
479 	 */
480 
481 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
482 	error = -zfs_getattr_fast(user_ns, request_mask, ip, stat);
483 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
484 	error = -zfs_getattr_fast(user_ns, ip, stat);
485 #else
486 	error = -zfs_getattr_fast(kcred->user_ns, ip, stat);
487 #endif
488 
489 #ifdef STATX_BTIME
490 	if (request_mask & STATX_BTIME) {
491 		stat->btime = zp->z_btime;
492 		stat->result_mask |= STATX_BTIME;
493 	}
494 #endif
495 
496 #ifdef STATX_DIOALIGN
497 	if (request_mask & STATX_DIOALIGN) {
498 		uint64_t align;
499 		if (zfs_get_direct_alignment(zp, &align) == 0) {
500 			stat->dio_mem_align = PAGE_SIZE;
501 			stat->dio_offset_align = align;
502 			stat->result_mask |= STATX_DIOALIGN;
503 		}
504 	}
505 #endif
506 
507 #ifdef STATX_ATTR_IMMUTABLE
508 	if (zp->z_pflags & ZFS_IMMUTABLE)
509 		stat->attributes |= STATX_ATTR_IMMUTABLE;
510 	stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
511 #endif
512 
513 #ifdef STATX_ATTR_APPEND
514 	if (zp->z_pflags & ZFS_APPENDONLY)
515 		stat->attributes |= STATX_ATTR_APPEND;
516 	stat->attributes_mask |= STATX_ATTR_APPEND;
517 #endif
518 
519 #ifdef STATX_ATTR_NODUMP
520 	if (zp->z_pflags & ZFS_NODUMP)
521 		stat->attributes |= STATX_ATTR_NODUMP;
522 	stat->attributes_mask |= STATX_ATTR_NODUMP;
523 #endif
524 
525 	spl_fstrans_unmark(cookie);
526 	ASSERT3S(error, <=, 0);
527 
528 	return (error);
529 }
530 ZPL_GETATTR_WRAPPER(zpl_getattr);
531 
532 static int
533 #ifdef HAVE_USERNS_IOPS_SETATTR
534 zpl_setattr(struct user_namespace *user_ns, struct dentry *dentry,
535     struct iattr *ia)
536 #elif defined(HAVE_IDMAP_IOPS_SETATTR)
537 zpl_setattr(struct mnt_idmap *user_ns, struct dentry *dentry,
538     struct iattr *ia)
539 #else
540 zpl_setattr(struct dentry *dentry, struct iattr *ia)
541 #endif
542 {
543 	struct inode *ip = dentry->d_inode;
544 	cred_t *cr = CRED();
545 	vattr_t *vap;
546 	int error;
547 	fstrans_cookie_t cookie;
548 
549 #ifdef HAVE_SETATTR_PREPARE_USERNS
550 	error = zpl_setattr_prepare(user_ns, dentry, ia);
551 #elif defined(HAVE_SETATTR_PREPARE_IDMAP)
552 	error = zpl_setattr_prepare(user_ns, dentry, ia);
553 #else
554 	error = zpl_setattr_prepare(zfs_init_idmap, dentry, ia);
555 #endif
556 	if (error)
557 		return (error);
558 
559 	crhold(cr);
560 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
561 	vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
562 	vap->va_mode = ia->ia_mode;
563 	if (ia->ia_valid & ATTR_UID)
564 #ifdef HAVE_IATTR_VFSID
565 		vap->va_uid = zfs_vfsuid_to_uid(user_ns, zfs_i_user_ns(ip),
566 		    __vfsuid_val(ia->ia_vfsuid));
567 #else
568 		vap->va_uid = KUID_TO_SUID(ia->ia_uid);
569 #endif
570 	if (ia->ia_valid & ATTR_GID)
571 #ifdef HAVE_IATTR_VFSID
572 		vap->va_gid = zfs_vfsgid_to_gid(user_ns, zfs_i_user_ns(ip),
573 		    __vfsgid_val(ia->ia_vfsgid));
574 #else
575 		vap->va_gid = KGID_TO_SGID(ia->ia_gid);
576 #endif
577 	vap->va_size = ia->ia_size;
578 	vap->va_atime = ia->ia_atime;
579 	vap->va_mtime = ia->ia_mtime;
580 	vap->va_ctime = ia->ia_ctime;
581 
582 	if (vap->va_mask & ATTR_ATIME)
583 		zpl_inode_set_atime_to_ts(ip,
584 		    zpl_inode_timestamp_truncate(ia->ia_atime, ip));
585 
586 	cookie = spl_fstrans_mark();
587 #ifdef HAVE_USERNS_IOPS_SETATTR
588 	error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns);
589 #elif defined(HAVE_IDMAP_IOPS_SETATTR)
590 	error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns);
591 #else
592 	error = -zfs_setattr(ITOZ(ip), vap, 0, cr, zfs_init_idmap);
593 #endif
594 	if (!error && (ia->ia_valid & ATTR_MODE))
595 		error = zpl_chmod_acl(ip);
596 
597 	spl_fstrans_unmark(cookie);
598 	kmem_free(vap, sizeof (vattr_t));
599 	crfree(cr);
600 	ASSERT3S(error, <=, 0);
601 
602 	return (error);
603 }
604 
605 static int
606 #ifdef HAVE_IOPS_RENAME_USERNS
607 zpl_rename2(struct user_namespace *user_ns, struct inode *sdip,
608     struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
609     unsigned int rflags)
610 #elif defined(HAVE_IOPS_RENAME_IDMAP)
611 zpl_rename2(struct mnt_idmap *user_ns, struct inode *sdip,
612     struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
613     unsigned int rflags)
614 #else
615 zpl_rename2(struct inode *sdip, struct dentry *sdentry,
616     struct inode *tdip, struct dentry *tdentry, unsigned int rflags)
617 #endif
618 {
619 	cred_t *cr = CRED();
620 	vattr_t *wo_vap = NULL;
621 	int error;
622 	fstrans_cookie_t cookie;
623 #if !(defined(HAVE_IOPS_RENAME_USERNS) || defined(HAVE_IOPS_RENAME_IDMAP))
624 	zidmap_t *user_ns = kcred->user_ns;
625 #endif
626 
627 	if (is_nametoolong(tdentry)) {
628 		return (-ENAMETOOLONG);
629 	}
630 
631 	crhold(cr);
632 	if (rflags & RENAME_WHITEOUT) {
633 		wo_vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
634 		zpl_vap_init(wo_vap, sdip, S_IFCHR, cr, user_ns);
635 		wo_vap->va_rdev = makedevice(0, 0);
636 	}
637 
638 	cookie = spl_fstrans_mark();
639 	error = -zfs_rename(ITOZ(sdip), dname(sdentry), ITOZ(tdip),
640 	    dname(tdentry), cr, 0, rflags, wo_vap, user_ns);
641 	spl_fstrans_unmark(cookie);
642 	if (wo_vap)
643 		kmem_free(wo_vap, sizeof (vattr_t));
644 	crfree(cr);
645 	ASSERT3S(error, <=, 0);
646 
647 	return (error);
648 }
649 
650 #if !defined(HAVE_IOPS_RENAME_USERNS) && \
651 	!defined(HAVE_RENAME_WANTS_FLAGS) && \
652 	!defined(HAVE_IOPS_RENAME_IDMAP)
653 static int
654 zpl_rename(struct inode *sdip, struct dentry *sdentry,
655     struct inode *tdip, struct dentry *tdentry)
656 {
657 	return (zpl_rename2(sdip, sdentry, tdip, tdentry, 0));
658 }
659 #endif
660 
661 static int
662 #ifdef HAVE_IOPS_SYMLINK_USERNS
663 zpl_symlink(struct user_namespace *user_ns, struct inode *dir,
664     struct dentry *dentry, const char *name)
665 #elif defined(HAVE_IOPS_SYMLINK_IDMAP)
666 zpl_symlink(struct mnt_idmap *user_ns, struct inode *dir,
667     struct dentry *dentry, const char *name)
668 #else
669 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
670 #endif
671 {
672 	cred_t *cr = CRED();
673 	vattr_t *vap;
674 	znode_t *zp;
675 	int error;
676 	fstrans_cookie_t cookie;
677 #if !(defined(HAVE_IOPS_SYMLINK_USERNS) || defined(HAVE_IOPS_SYMLINK_IDMAP))
678 	zidmap_t *user_ns = kcred->user_ns;
679 #endif
680 
681 	if (is_nametoolong(dentry)) {
682 		return (-ENAMETOOLONG);
683 	}
684 
685 	crhold(cr);
686 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
687 	zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr, user_ns);
688 
689 	cookie = spl_fstrans_mark();
690 	error = -zfs_symlink(ITOZ(dir), dname(dentry), vap,
691 	    (char *)name, &zp, cr, 0, user_ns);
692 	if (error == 0) {
693 		error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
694 		if (error) {
695 			(void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
696 			remove_inode_hash(ZTOI(zp));
697 			iput(ZTOI(zp));
698 		} else {
699 			d_instantiate(dentry, ZTOI(zp));
700 		}
701 	}
702 
703 	spl_fstrans_unmark(cookie);
704 	kmem_free(vap, sizeof (vattr_t));
705 	crfree(cr);
706 	ASSERT3S(error, <=, 0);
707 
708 	return (error);
709 }
710 
711 static void
712 zpl_put_link(void *ptr)
713 {
714 	kmem_free(ptr, MAXPATHLEN);
715 }
716 
717 static int
718 zpl_get_link_common(struct dentry *dentry, struct inode *ip, char **link)
719 {
720 	fstrans_cookie_t cookie;
721 	cred_t *cr = CRED();
722 	int error;
723 
724 	crhold(cr);
725 	*link = NULL;
726 
727 	struct iovec iov;
728 	iov.iov_len = MAXPATHLEN;
729 	iov.iov_base = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
730 
731 	zfs_uio_t uio;
732 	zfs_uio_iovec_init(&uio, &iov, 1, 0, UIO_SYSSPACE, MAXPATHLEN - 1, 0);
733 
734 	cookie = spl_fstrans_mark();
735 	error = -zfs_readlink(ip, &uio, cr);
736 	spl_fstrans_unmark(cookie);
737 	crfree(cr);
738 
739 	if (error)
740 		kmem_free(iov.iov_base, MAXPATHLEN);
741 	else
742 		*link = iov.iov_base;
743 
744 	return (error);
745 }
746 
747 static const char *
748 zpl_get_link(struct dentry *dentry, struct inode *inode,
749     struct delayed_call *done)
750 {
751 	char *link = NULL;
752 	int error;
753 
754 	if (!dentry)
755 		return (ERR_PTR(-ECHILD));
756 
757 	error = zpl_get_link_common(dentry, inode, &link);
758 	if (error)
759 		return (ERR_PTR(error));
760 
761 	set_delayed_call(done, zpl_put_link, link);
762 
763 	return (link);
764 }
765 
766 static int
767 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
768 {
769 	cred_t *cr = CRED();
770 	struct inode *ip = old_dentry->d_inode;
771 	int error;
772 	fstrans_cookie_t cookie;
773 
774 	if (is_nametoolong(dentry)) {
775 		return (-ENAMETOOLONG);
776 	}
777 
778 	if (ip->i_nlink >= ZFS_LINK_MAX)
779 		return (-EMLINK);
780 
781 	crhold(cr);
782 	zpl_inode_set_ctime_to_ts(ip, current_time(ip));
783 	/* Must have an existing ref, so igrab() cannot return NULL */
784 	VERIFY3P(igrab(ip), !=, NULL);
785 
786 	cookie = spl_fstrans_mark();
787 	error = -zfs_link(ITOZ(dir), ITOZ(ip), dname(dentry), cr, 0);
788 	if (error) {
789 		iput(ip);
790 		goto out;
791 	}
792 
793 	d_instantiate(dentry, ip);
794 out:
795 	spl_fstrans_unmark(cookie);
796 	crfree(cr);
797 	ASSERT3S(error, <=, 0);
798 
799 	return (error);
800 }
801 
802 const struct inode_operations zpl_inode_operations = {
803 	.setattr	= zpl_setattr,
804 	.getattr	= zpl_getattr,
805 	.listxattr	= zpl_xattr_list,
806 #if defined(CONFIG_FS_POSIX_ACL)
807 	.set_acl	= zpl_set_acl,
808 #if defined(HAVE_GET_INODE_ACL)
809 	.get_inode_acl	= zpl_get_acl,
810 #else
811 	.get_acl	= zpl_get_acl,
812 #endif /* HAVE_GET_INODE_ACL */
813 #endif /* CONFIG_FS_POSIX_ACL */
814 };
815 
816 const struct inode_operations zpl_dir_inode_operations = {
817 	.create		= zpl_create,
818 	.lookup		= zpl_lookup,
819 	.link		= zpl_link,
820 	.unlink		= zpl_unlink,
821 	.symlink	= zpl_symlink,
822 	.mkdir		= zpl_mkdir,
823 	.rmdir		= zpl_rmdir,
824 	.mknod		= zpl_mknod,
825 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
826 	.rename		= zpl_rename2,
827 #elif defined(HAVE_IOPS_RENAME_IDMAP)
828 	.rename		= zpl_rename2,
829 #else
830 	.rename		= zpl_rename,
831 #endif
832 	.tmpfile	= zpl_tmpfile,
833 	.setattr	= zpl_setattr,
834 	.getattr	= zpl_getattr,
835 	.listxattr	= zpl_xattr_list,
836 #if defined(CONFIG_FS_POSIX_ACL)
837 	.set_acl	= zpl_set_acl,
838 #if defined(HAVE_GET_INODE_ACL)
839 	.get_inode_acl	= zpl_get_acl,
840 #else
841 	.get_acl	= zpl_get_acl,
842 #endif /* HAVE_GET_INODE_ACL */
843 #endif /* CONFIG_FS_POSIX_ACL */
844 };
845 
846 const struct inode_operations zpl_symlink_inode_operations = {
847 	.get_link	= zpl_get_link,
848 	.setattr	= zpl_setattr,
849 	.getattr	= zpl_getattr,
850 	.listxattr	= zpl_xattr_list,
851 };
852 
853 const struct inode_operations zpl_special_inode_operations = {
854 	.setattr	= zpl_setattr,
855 	.getattr	= zpl_getattr,
856 	.listxattr	= zpl_xattr_list,
857 #if defined(CONFIG_FS_POSIX_ACL)
858 	.set_acl	= zpl_set_acl,
859 #if defined(HAVE_GET_INODE_ACL)
860 	.get_inode_acl	= zpl_get_acl,
861 #else
862 	.get_acl	= zpl_get_acl,
863 #endif /* HAVE_GET_INODE_ACL */
864 #endif /* CONFIG_FS_POSIX_ACL */
865 };
866