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