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