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