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