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