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