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