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