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 Gunnar Beutner
23 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
24 */
25
26
27 #include <sys/file.h>
28 #include <sys/zfs_znode.h>
29 #include <sys/zfs_vnops.h>
30 #include <sys/zfs_ctldir.h>
31 #include <sys/zpl.h>
32
33
34 static int
zpl_encode_fh(struct inode * ip,__u32 * fh,int * max_len,struct inode * parent)35 zpl_encode_fh(struct inode *ip, __u32 *fh, int *max_len, struct inode *parent)
36 {
37 fstrans_cookie_t cookie;
38 ushort_t empty_fid = 0;
39 fid_t *fid;
40 int len_bytes, rc;
41
42 len_bytes = *max_len * sizeof (__u32);
43
44 if (len_bytes < offsetof(fid_t, fid_data)) {
45 fid = (fid_t *)&empty_fid;
46 } else {
47 fid = (fid_t *)fh;
48 fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
49 }
50
51 cookie = spl_fstrans_mark();
52
53 if (zfsctl_is_node(ip))
54 rc = zfsctl_fid(ip, fid);
55 else
56 rc = zfs_fid(ip, fid);
57
58 spl_fstrans_unmark(cookie);
59 len_bytes = offsetof(fid_t, fid_data) + fid->fid_len;
60 *max_len = roundup(len_bytes, sizeof (__u32)) / sizeof (__u32);
61
62 return (rc == 0 ? FILEID_INO32_GEN : 255);
63 }
64
65 static struct dentry *
zpl_fh_to_dentry(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)66 zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
67 int fh_len, int fh_type)
68 {
69 fid_t *fid = (fid_t *)fh;
70 fstrans_cookie_t cookie;
71 struct inode *ip;
72 int len_bytes, rc;
73
74 len_bytes = fh_len * sizeof (__u32);
75
76 if (fh_type != FILEID_INO32_GEN ||
77 len_bytes < offsetof(fid_t, fid_data) ||
78 len_bytes < offsetof(fid_t, fid_data) + fid->fid_len)
79 return (ERR_PTR(-EINVAL));
80
81 cookie = spl_fstrans_mark();
82 rc = zfs_vget(sb, &ip, fid);
83 spl_fstrans_unmark(cookie);
84
85 if (rc) {
86 /*
87 * If we see ENOENT it might mean that an NFSv4 * client
88 * is using a cached inode value in a file handle and
89 * that the sought after file has had its inode changed
90 * by a third party. So change the error to ESTALE
91 * which will trigger a full lookup by the client and
92 * will find the new filename/inode pair if it still
93 * exists.
94 */
95 if (rc == ENOENT)
96 rc = ESTALE;
97
98 return (ERR_PTR(-rc));
99 }
100
101 ASSERT((ip != NULL) && !IS_ERR(ip));
102
103 return (d_obtain_alias(ip));
104 }
105
106 /*
107 * In case the filesystem contains name longer than 255, we need to override
108 * the default get_name so we don't get buffer overflow. Unfortunately, since
109 * the buffer size is hardcoded in Linux, we will get ESTALE error in this
110 * case.
111 */
112 static int
zpl_get_name(struct dentry * parent,char * name,struct dentry * child)113 zpl_get_name(struct dentry *parent, char *name, struct dentry *child)
114 {
115 cred_t *cr = CRED();
116 fstrans_cookie_t cookie;
117 struct inode *dir = parent->d_inode;
118 struct inode *ip = child->d_inode;
119 int error;
120
121 if (!dir || !S_ISDIR(dir->i_mode))
122 return (-ENOTDIR);
123
124 crhold(cr);
125 cookie = spl_fstrans_mark();
126 spl_inode_lock_shared(dir);
127 error = -zfs_get_name(ITOZ(dir), name, ITOZ(ip));
128 spl_inode_unlock_shared(dir);
129 spl_fstrans_unmark(cookie);
130 crfree(cr);
131
132 return (error);
133 }
134
135 static struct dentry *
zpl_get_parent(struct dentry * child)136 zpl_get_parent(struct dentry *child)
137 {
138 cred_t *cr = CRED();
139 fstrans_cookie_t cookie;
140 znode_t *zp;
141 int error;
142
143 crhold(cr);
144 cookie = spl_fstrans_mark();
145 error = -zfs_lookup(ITOZ(child->d_inode), "..", &zp, 0, cr, NULL, NULL);
146 spl_fstrans_unmark(cookie);
147 crfree(cr);
148 ASSERT3S(error, <=, 0);
149
150 if (error)
151 return (ERR_PTR(error));
152
153 return (d_obtain_alias(ZTOI(zp)));
154 }
155
156 static int
zpl_commit_metadata(struct inode * inode)157 zpl_commit_metadata(struct inode *inode)
158 {
159 cred_t *cr = CRED();
160 fstrans_cookie_t cookie;
161 int error;
162
163 if (zfsctl_is_node(inode))
164 return (0);
165
166 crhold(cr);
167 cookie = spl_fstrans_mark();
168 error = -zfs_fsync(ITOZ(inode), 0, cr);
169 spl_fstrans_unmark(cookie);
170 crfree(cr);
171 ASSERT3S(error, <=, 0);
172
173 return (error);
174 }
175
176 const struct export_operations zpl_export_operations = {
177 .encode_fh = zpl_encode_fh,
178 .fh_to_dentry = zpl_fh_to_dentry,
179 .get_name = zpl_get_name,
180 .get_parent = zpl_get_parent,
181 .commit_metadata = zpl_commit_metadata,
182 };
183