]>
Commit | Line | Data |
---|---|---|
ee154f01 BB |
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 | */ | |
24 | ||
25 | ||
26 | #include <sys/zfs_vfsops.h> | |
27 | #include <sys/zfs_vnops.h> | |
28 | #include <sys/vfs.h> | |
29 | #include <sys/zpl.h> | |
30 | ||
31 | ||
32 | static struct dentry * | |
33 | zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | |
34 | { | |
81e97e21 | 35 | cred_t *cr = CRED(); |
ee154f01 | 36 | struct inode *ip; |
ee154f01 BB |
37 | int error; |
38 | ||
81e97e21 | 39 | crhold(cr); |
ee154f01 BB |
40 | error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL); |
41 | ASSERT3S(error, <=, 0); | |
81e97e21 | 42 | crfree(cr); |
ee154f01 BB |
43 | |
44 | if (error) { | |
45 | if (error == -ENOENT) | |
46 | return d_splice_alias(NULL, dentry); | |
47 | else | |
48 | return ERR_PTR(error); | |
49 | } | |
50 | ||
51 | return d_splice_alias(ip, dentry); | |
52 | } | |
53 | ||
54 | static int | |
55 | zpl_create(struct inode *dir, struct dentry *dentry, int mode, | |
56 | struct nameidata *nd) | |
57 | { | |
81e97e21 | 58 | cred_t *cr = CRED(); |
ee154f01 BB |
59 | struct inode *ip; |
60 | vattr_t *vap; | |
61 | int error; | |
62 | ||
81e97e21 | 63 | crhold(cr); |
ee154f01 BB |
64 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
65 | vap->va_mode = mode; | |
66 | vap->va_mask = ATTR_MODE; | |
81e97e21 BB |
67 | vap->va_uid = crgetfsuid(cr); |
68 | vap->va_gid = crgetfsgid(cr); | |
c85b224f | 69 | vap->va_dentry = dentry; |
ee154f01 BB |
70 | |
71 | error = -zfs_create(dir, (char *)dentry->d_name.name, | |
81e97e21 | 72 | vap, 0, mode, &ip, cr, 0, NULL); |
ee154f01 | 73 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 74 | crfree(cr); |
ee154f01 BB |
75 | ASSERT3S(error, <=, 0); |
76 | ||
77 | return (error); | |
78 | } | |
79 | ||
80 | static int | |
81 | zpl_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) | |
82 | { | |
81e97e21 | 83 | cred_t *cr = CRED(); |
ee154f01 BB |
84 | struct inode *ip; |
85 | vattr_t *vap; | |
86 | int error; | |
87 | ||
aa6d8c10 NB |
88 | /* |
89 | * We currently expect Linux to supply rdev=0 for all sockets | |
90 | * and fifos, but we want to know if this behavior ever changes. | |
91 | */ | |
92 | if (S_ISSOCK(mode) || S_ISFIFO(mode)) | |
93 | ASSERT(rdev == 0); | |
94 | ||
81e97e21 | 95 | crhold(cr); |
ee154f01 BB |
96 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
97 | vap->va_mode = mode; | |
98 | vap->va_mask = ATTR_MODE; | |
99 | vap->va_rdev = rdev; | |
81e97e21 BB |
100 | vap->va_uid = crgetfsuid(cr); |
101 | vap->va_gid = crgetfsgid(cr); | |
c85b224f | 102 | vap->va_dentry = dentry; |
ee154f01 BB |
103 | |
104 | error = -zfs_create(dir, (char *)dentry->d_name.name, | |
81e97e21 | 105 | vap, 0, mode, &ip, cr, 0, NULL); |
ee154f01 | 106 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 107 | crfree(cr); |
ee154f01 BB |
108 | ASSERT3S(error, <=, 0); |
109 | ||
110 | return (-error); | |
111 | } | |
112 | ||
113 | static int | |
114 | zpl_unlink(struct inode *dir, struct dentry *dentry) | |
115 | { | |
81e97e21 | 116 | cred_t *cr = CRED(); |
ee154f01 BB |
117 | int error; |
118 | ||
81e97e21 | 119 | crhold(cr); |
ee154f01 | 120 | error = -zfs_remove(dir, dname(dentry), cr); |
81e97e21 | 121 | crfree(cr); |
ee154f01 BB |
122 | ASSERT3S(error, <=, 0); |
123 | ||
124 | return (error); | |
125 | } | |
126 | ||
127 | static int | |
128 | zpl_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
129 | { | |
81e97e21 | 130 | cred_t *cr = CRED(); |
ee154f01 BB |
131 | vattr_t *vap; |
132 | struct inode *ip; | |
133 | int error; | |
134 | ||
81e97e21 | 135 | crhold(cr); |
ee154f01 BB |
136 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
137 | vap->va_mode = S_IFDIR | mode; | |
138 | vap->va_mask = ATTR_MODE; | |
81e97e21 BB |
139 | vap->va_uid = crgetfsuid(cr); |
140 | vap->va_gid = crgetfsgid(cr); | |
c85b224f | 141 | vap->va_dentry = dentry; |
ee154f01 BB |
142 | |
143 | error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL); | |
ee154f01 | 144 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 145 | crfree(cr); |
ee154f01 BB |
146 | ASSERT3S(error, <=, 0); |
147 | ||
148 | return (error); | |
149 | } | |
150 | ||
151 | static int | |
152 | zpl_rmdir(struct inode * dir, struct dentry *dentry) | |
153 | { | |
81e97e21 | 154 | cred_t *cr = CRED(); |
ee154f01 BB |
155 | int error; |
156 | ||
81e97e21 | 157 | crhold(cr); |
ee154f01 | 158 | error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0); |
81e97e21 | 159 | crfree(cr); |
ee154f01 BB |
160 | ASSERT3S(error, <=, 0); |
161 | ||
162 | return (error); | |
163 | } | |
164 | ||
165 | static int | |
166 | zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) | |
167 | { | |
ee154f01 BB |
168 | int error; |
169 | ||
057e8eee | 170 | error = -zfs_getattr_fast(dentry->d_inode, stat); |
ee154f01 BB |
171 | ASSERT3S(error, <=, 0); |
172 | ||
173 | return (error); | |
174 | } | |
175 | ||
176 | static int | |
5484965a | 177 | zpl_setattr(struct dentry *dentry, struct iattr *ia) |
ee154f01 | 178 | { |
81e97e21 | 179 | cred_t *cr = CRED(); |
5484965a | 180 | vattr_t *vap; |
ee154f01 BB |
181 | int error; |
182 | ||
5484965a | 183 | error = inode_change_ok(dentry->d_inode, ia); |
ee154f01 BB |
184 | if (error) |
185 | return (error); | |
186 | ||
81e97e21 | 187 | crhold(cr); |
5484965a BB |
188 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
189 | vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK; | |
190 | vap->va_mode = ia->ia_mode; | |
191 | vap->va_uid = ia->ia_uid; | |
192 | vap->va_gid = ia->ia_gid; | |
193 | vap->va_size = ia->ia_size; | |
194 | vap->va_atime = ia->ia_atime; | |
195 | vap->va_mtime = ia->ia_mtime; | |
196 | vap->va_ctime = ia->ia_ctime; | |
197 | ||
198 | error = -zfs_setattr(dentry->d_inode, vap, 0, cr); | |
199 | ||
200 | kmem_free(vap, sizeof(vattr_t)); | |
81e97e21 | 201 | crfree(cr); |
ee154f01 BB |
202 | ASSERT3S(error, <=, 0); |
203 | ||
5484965a | 204 | return (error); |
ee154f01 BB |
205 | } |
206 | ||
207 | static int | |
208 | zpl_rename(struct inode *sdip, struct dentry *sdentry, | |
209 | struct inode *tdip, struct dentry *tdentry) | |
210 | { | |
81e97e21 | 211 | cred_t *cr = CRED(); |
ee154f01 BB |
212 | int error; |
213 | ||
81e97e21 | 214 | crhold(cr); |
ee154f01 | 215 | error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0); |
81e97e21 | 216 | crfree(cr); |
ee154f01 BB |
217 | ASSERT3S(error, <=, 0); |
218 | ||
219 | return (error); | |
220 | } | |
221 | ||
222 | static int | |
223 | zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name) | |
224 | { | |
81e97e21 | 225 | cred_t *cr = CRED(); |
ee154f01 BB |
226 | vattr_t *vap; |
227 | struct inode *ip; | |
228 | int error; | |
229 | ||
81e97e21 | 230 | crhold(cr); |
ee154f01 BB |
231 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
232 | vap->va_mode = S_IFLNK | S_IRWXUGO; | |
233 | vap->va_mask = ATTR_MODE; | |
81e97e21 BB |
234 | vap->va_uid = crgetfsuid(cr); |
235 | vap->va_gid = crgetfsgid(cr); | |
c85b224f | 236 | vap->va_dentry = dentry; |
ee154f01 BB |
237 | |
238 | error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0); | |
ee154f01 | 239 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 240 | crfree(cr); |
ee154f01 BB |
241 | ASSERT3S(error, <=, 0); |
242 | ||
243 | return (error); | |
244 | } | |
245 | ||
246 | static void * | |
247 | zpl_follow_link(struct dentry *dentry, struct nameidata *nd) | |
248 | { | |
81e97e21 | 249 | cred_t *cr = CRED(); |
8b4f9a2d BB |
250 | struct inode *ip = dentry->d_inode; |
251 | struct iovec iov; | |
252 | uio_t uio; | |
253 | char *link; | |
8b4f9a2d BB |
254 | int error; |
255 | ||
81e97e21 | 256 | crhold(cr); |
8b4f9a2d BB |
257 | |
258 | iov.iov_len = MAXPATHLEN; | |
259 | iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP); | |
260 | ||
261 | uio.uio_iov = &iov; | |
262 | uio.uio_iovcnt = 1; | |
263 | uio.uio_resid = (MAXPATHLEN - 1); | |
264 | uio.uio_segflg = UIO_SYSSPACE; | |
265 | ||
50950001 | 266 | error = -zfs_readlink(ip, &uio, cr); |
8b4f9a2d BB |
267 | if (error) { |
268 | kmem_free(link, MAXPATHLEN); | |
269 | nd_set_link(nd, ERR_PTR(error)); | |
270 | } else { | |
271 | nd_set_link(nd, link); | |
272 | } | |
273 | ||
81e97e21 | 274 | crfree(cr); |
8b4f9a2d | 275 | return (NULL); |
ee154f01 BB |
276 | } |
277 | ||
278 | static void | |
279 | zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) | |
280 | { | |
281 | char *link; | |
282 | ||
283 | link = nd_get_link(nd); | |
284 | if (!IS_ERR(link)) | |
8b4f9a2d | 285 | kmem_free(link, MAXPATHLEN); |
ee154f01 BB |
286 | } |
287 | ||
288 | static int | |
289 | zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
290 | { | |
81e97e21 | 291 | cred_t *cr = CRED(); |
ee154f01 | 292 | struct inode *ip = old_dentry->d_inode; |
ee154f01 BB |
293 | int error; |
294 | ||
295 | if (ip->i_nlink >= ZFS_LINK_MAX) | |
296 | return -EMLINK; | |
297 | ||
81e97e21 | 298 | crhold(cr); |
ee154f01 BB |
299 | ip->i_ctime = CURRENT_TIME_SEC; |
300 | igrab(ip); /* Use ihold() if available */ | |
301 | ||
302 | error = -zfs_link(dir, ip, dname(dentry), cr); | |
303 | if (error) { | |
304 | iput(ip); | |
305 | goto out; | |
306 | } | |
307 | ||
308 | d_instantiate(dentry, ip); | |
309 | out: | |
81e97e21 | 310 | crfree(cr); |
ee154f01 BB |
311 | ASSERT3S(error, <=, 0); |
312 | ||
313 | return (error); | |
314 | } | |
315 | ||
316 | const struct inode_operations zpl_inode_operations = { | |
ee154f01 BB |
317 | .create = zpl_create, |
318 | .link = zpl_link, | |
319 | .unlink = zpl_unlink, | |
320 | .symlink = zpl_symlink, | |
321 | .mkdir = zpl_mkdir, | |
322 | .rmdir = zpl_rmdir, | |
323 | .mknod = zpl_mknod, | |
324 | .rename = zpl_rename, | |
325 | .setattr = zpl_setattr, | |
326 | .getattr = zpl_getattr, | |
327 | .setxattr = generic_setxattr, | |
328 | .getxattr = generic_getxattr, | |
329 | .removexattr = generic_removexattr, | |
330 | .listxattr = zpl_xattr_list, | |
331 | }; | |
332 | ||
333 | const struct inode_operations zpl_dir_inode_operations = { | |
ee154f01 BB |
334 | .create = zpl_create, |
335 | .lookup = zpl_lookup, | |
336 | .link = zpl_link, | |
337 | .unlink = zpl_unlink, | |
338 | .symlink = zpl_symlink, | |
339 | .mkdir = zpl_mkdir, | |
340 | .rmdir = zpl_rmdir, | |
341 | .mknod = zpl_mknod, | |
342 | .rename = zpl_rename, | |
343 | .setattr = zpl_setattr, | |
a6695d83 BB |
344 | .getattr = zpl_getattr, |
345 | .setxattr = generic_setxattr, | |
346 | .getxattr = generic_getxattr, | |
347 | .removexattr = generic_removexattr, | |
348 | .listxattr = zpl_xattr_list, | |
ee154f01 BB |
349 | }; |
350 | ||
351 | const struct inode_operations zpl_symlink_inode_operations = { | |
ee154f01 BB |
352 | .readlink = generic_readlink, |
353 | .follow_link = zpl_follow_link, | |
354 | .put_link = zpl_put_link, | |
355 | }; | |
356 | ||
357 | const struct inode_operations zpl_special_inode_operations = { | |
a6695d83 BB |
358 | .setattr = zpl_setattr, |
359 | .getattr = zpl_getattr, | |
360 | .setxattr = generic_setxattr, | |
361 | .getxattr = generic_getxattr, | |
362 | .removexattr = generic_removexattr, | |
363 | .listxattr = zpl_xattr_list, | |
ee154f01 | 364 | }; |