]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_inode.c
Fixed a NULL pointer dereference bug in zfs_preumount
[mirror_zfs.git] / module / zfs / zpl_inode.c
CommitLineData
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>
ebe7e575 28#include <sys/zfs_znode.h>
ee154f01
BB
29#include <sys/vfs.h>
30#include <sys/zpl.h>
31
32
33static struct dentry *
34zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
35{
81e97e21 36 cred_t *cr = CRED();
ee154f01 37 struct inode *ip;
ee154f01
BB
38 int error;
39
81e97e21 40 crhold(cr);
ee154f01
BB
41 error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
42 ASSERT3S(error, <=, 0);
81e97e21 43 crfree(cr);
ee154f01
BB
44
45 if (error) {
46 if (error == -ENOENT)
47 return d_splice_alias(NULL, dentry);
48 else
49 return ERR_PTR(error);
50 }
51
52 return d_splice_alias(ip, dentry);
53}
54
ebe7e575 55void
9fd91dae
BB
56zpl_vap_init(vattr_t *vap, struct inode *dir, struct dentry *dentry,
57 mode_t mode, cred_t *cr)
58{
59 vap->va_mask = ATTR_MODE;
60 vap->va_mode = mode;
61 vap->va_dentry = dentry;
62 vap->va_uid = crgetfsuid(cr);
63
64 if (dir && dir->i_mode & S_ISGID) {
65 vap->va_gid = dir->i_gid;
66 if (S_ISDIR(mode))
67 vap->va_mode |= S_ISGID;
68 } else {
69 vap->va_gid = crgetfsgid(cr);
70 }
71}
72
ee154f01
BB
73static int
74zpl_create(struct inode *dir, struct dentry *dentry, int mode,
75 struct nameidata *nd)
76{
81e97e21 77 cred_t *cr = CRED();
ee154f01
BB
78 struct inode *ip;
79 vattr_t *vap;
80 int error;
81
81e97e21 82 crhold(cr);
ee154f01 83 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 84 zpl_vap_init(vap, dir, dentry, mode, cr);
ee154f01
BB
85
86 error = -zfs_create(dir, (char *)dentry->d_name.name,
81e97e21 87 vap, 0, mode, &ip, cr, 0, NULL);
ee154f01 88 kmem_free(vap, sizeof(vattr_t));
81e97e21 89 crfree(cr);
ee154f01
BB
90 ASSERT3S(error, <=, 0);
91
92 return (error);
93}
94
95static int
96zpl_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
97{
81e97e21 98 cred_t *cr = CRED();
ee154f01
BB
99 struct inode *ip;
100 vattr_t *vap;
101 int error;
102
aa6d8c10
NB
103 /*
104 * We currently expect Linux to supply rdev=0 for all sockets
105 * and fifos, but we want to know if this behavior ever changes.
106 */
107 if (S_ISSOCK(mode) || S_ISFIFO(mode))
108 ASSERT(rdev == 0);
109
81e97e21 110 crhold(cr);
ee154f01 111 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 112 zpl_vap_init(vap, dir, dentry, mode, cr);
ee154f01 113 vap->va_rdev = rdev;
ee154f01
BB
114
115 error = -zfs_create(dir, (char *)dentry->d_name.name,
81e97e21 116 vap, 0, mode, &ip, cr, 0, NULL);
ee154f01 117 kmem_free(vap, sizeof(vattr_t));
81e97e21 118 crfree(cr);
ee154f01
BB
119 ASSERT3S(error, <=, 0);
120
121 return (-error);
122}
123
124static int
125zpl_unlink(struct inode *dir, struct dentry *dentry)
126{
81e97e21 127 cred_t *cr = CRED();
ee154f01
BB
128 int error;
129
81e97e21 130 crhold(cr);
ee154f01 131 error = -zfs_remove(dir, dname(dentry), cr);
81e97e21 132 crfree(cr);
ee154f01
BB
133 ASSERT3S(error, <=, 0);
134
135 return (error);
136}
137
138static int
139zpl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
140{
81e97e21 141 cred_t *cr = CRED();
ee154f01
BB
142 vattr_t *vap;
143 struct inode *ip;
144 int error;
145
81e97e21 146 crhold(cr);
ee154f01 147 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 148 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
ee154f01
BB
149
150 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
ee154f01 151 kmem_free(vap, sizeof(vattr_t));
81e97e21 152 crfree(cr);
ee154f01
BB
153 ASSERT3S(error, <=, 0);
154
155 return (error);
156}
157
158static int
159zpl_rmdir(struct inode * dir, struct dentry *dentry)
160{
81e97e21 161 cred_t *cr = CRED();
ee154f01
BB
162 int error;
163
81e97e21 164 crhold(cr);
ee154f01 165 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
81e97e21 166 crfree(cr);
ee154f01
BB
167 ASSERT3S(error, <=, 0);
168
169 return (error);
170}
171
172static int
173zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
174{
ebe7e575 175 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
ee154f01
BB
176 int error;
177
ebe7e575
BB
178 /*
179 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
180 * unmounted automatically with the parent file system. This
181 * is done on the first getattr because it's not easy to get the
182 * vfsmount structure at mount time. This call path is explicitly
183 * marked unlikely to avoid any performance impact. FWIW, ext4
184 * resorts to a similar trick for sysadmin convenience.
185 */
186 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
187 mnt->mnt_flags |= MNT_SHRINKABLE;
188
057e8eee 189 error = -zfs_getattr_fast(dentry->d_inode, stat);
ee154f01
BB
190 ASSERT3S(error, <=, 0);
191
192 return (error);
193}
194
195static int
5484965a 196zpl_setattr(struct dentry *dentry, struct iattr *ia)
ee154f01 197{
81e97e21 198 cred_t *cr = CRED();
5484965a 199 vattr_t *vap;
ee154f01
BB
200 int error;
201
5484965a 202 error = inode_change_ok(dentry->d_inode, ia);
ee154f01
BB
203 if (error)
204 return (error);
205
81e97e21 206 crhold(cr);
5484965a
BB
207 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
208 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
209 vap->va_mode = ia->ia_mode;
210 vap->va_uid = ia->ia_uid;
211 vap->va_gid = ia->ia_gid;
212 vap->va_size = ia->ia_size;
213 vap->va_atime = ia->ia_atime;
214 vap->va_mtime = ia->ia_mtime;
215 vap->va_ctime = ia->ia_ctime;
216
217 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
218
219 kmem_free(vap, sizeof(vattr_t));
81e97e21 220 crfree(cr);
ee154f01
BB
221 ASSERT3S(error, <=, 0);
222
5484965a 223 return (error);
ee154f01
BB
224}
225
226static int
227zpl_rename(struct inode *sdip, struct dentry *sdentry,
228 struct inode *tdip, struct dentry *tdentry)
229{
81e97e21 230 cred_t *cr = CRED();
ee154f01
BB
231 int error;
232
81e97e21 233 crhold(cr);
ee154f01 234 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
81e97e21 235 crfree(cr);
ee154f01
BB
236 ASSERT3S(error, <=, 0);
237
238 return (error);
239}
240
241static int
242zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
243{
81e97e21 244 cred_t *cr = CRED();
ee154f01
BB
245 vattr_t *vap;
246 struct inode *ip;
247 int error;
248
81e97e21 249 crhold(cr);
ee154f01 250 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 251 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
ee154f01
BB
252
253 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
ee154f01 254 kmem_free(vap, sizeof(vattr_t));
81e97e21 255 crfree(cr);
ee154f01
BB
256 ASSERT3S(error, <=, 0);
257
258 return (error);
259}
260
261static void *
262zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
263{
81e97e21 264 cred_t *cr = CRED();
8b4f9a2d
BB
265 struct inode *ip = dentry->d_inode;
266 struct iovec iov;
267 uio_t uio;
268 char *link;
8b4f9a2d
BB
269 int error;
270
81e97e21 271 crhold(cr);
8b4f9a2d
BB
272
273 iov.iov_len = MAXPATHLEN;
274 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
275
276 uio.uio_iov = &iov;
277 uio.uio_iovcnt = 1;
278 uio.uio_resid = (MAXPATHLEN - 1);
279 uio.uio_segflg = UIO_SYSSPACE;
280
50950001 281 error = -zfs_readlink(ip, &uio, cr);
8b4f9a2d
BB
282 if (error) {
283 kmem_free(link, MAXPATHLEN);
284 nd_set_link(nd, ERR_PTR(error));
285 } else {
286 nd_set_link(nd, link);
287 }
288
81e97e21 289 crfree(cr);
8b4f9a2d 290 return (NULL);
ee154f01
BB
291}
292
293static void
294zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
295{
296 char *link;
297
298 link = nd_get_link(nd);
299 if (!IS_ERR(link))
8b4f9a2d 300 kmem_free(link, MAXPATHLEN);
ee154f01
BB
301}
302
303static int
304zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
305{
81e97e21 306 cred_t *cr = CRED();
ee154f01 307 struct inode *ip = old_dentry->d_inode;
ee154f01
BB
308 int error;
309
310 if (ip->i_nlink >= ZFS_LINK_MAX)
311 return -EMLINK;
312
81e97e21 313 crhold(cr);
ee154f01
BB
314 ip->i_ctime = CURRENT_TIME_SEC;
315 igrab(ip); /* Use ihold() if available */
316
317 error = -zfs_link(dir, ip, dname(dentry), cr);
318 if (error) {
319 iput(ip);
320 goto out;
321 }
322
323 d_instantiate(dentry, ip);
324out:
81e97e21 325 crfree(cr);
ee154f01
BB
326 ASSERT3S(error, <=, 0);
327
328 return (error);
329}
330
5cb63a57
ED
331static void
332zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
333{
334 cred_t *cr = CRED();
335 flock64_t bf;
336
337 ASSERT3S(start, <=, end);
338
339 /*
340 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
341 * the end of the file". We don't want that.
342 */
343 if (start == end)
344 return;
345
346 crhold(cr);
347
348 bf.l_type = F_WRLCK;
349 bf.l_whence = 0;
350 bf.l_start = start;
351 bf.l_len = end - start;
352 bf.l_pid = 0;
353 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
354
355 crfree(cr);
356}
357
cb2d1901
ED
358#ifdef HAVE_INODE_FALLOCATE
359static long
360zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
361{
362 return zpl_fallocate_common(ip, mode, offset, len);
363}
364#endif /* HAVE_INODE_FALLOCATE */
365
366
ee154f01 367const struct inode_operations zpl_inode_operations = {
ee154f01
BB
368 .create = zpl_create,
369 .link = zpl_link,
370 .unlink = zpl_unlink,
371 .symlink = zpl_symlink,
372 .mkdir = zpl_mkdir,
373 .rmdir = zpl_rmdir,
374 .mknod = zpl_mknod,
375 .rename = zpl_rename,
376 .setattr = zpl_setattr,
377 .getattr = zpl_getattr,
378 .setxattr = generic_setxattr,
379 .getxattr = generic_getxattr,
380 .removexattr = generic_removexattr,
381 .listxattr = zpl_xattr_list,
5cb63a57 382 .truncate_range = zpl_truncate_range,
cb2d1901
ED
383#ifdef HAVE_INODE_FALLOCATE
384 .fallocate = zpl_fallocate,
385#endif /* HAVE_INODE_FALLOCATE */
ee154f01
BB
386};
387
388const struct inode_operations zpl_dir_inode_operations = {
ee154f01
BB
389 .create = zpl_create,
390 .lookup = zpl_lookup,
391 .link = zpl_link,
392 .unlink = zpl_unlink,
393 .symlink = zpl_symlink,
394 .mkdir = zpl_mkdir,
395 .rmdir = zpl_rmdir,
396 .mknod = zpl_mknod,
397 .rename = zpl_rename,
398 .setattr = zpl_setattr,
a6695d83
BB
399 .getattr = zpl_getattr,
400 .setxattr = generic_setxattr,
401 .getxattr = generic_getxattr,
402 .removexattr = generic_removexattr,
403 .listxattr = zpl_xattr_list,
ee154f01
BB
404};
405
406const struct inode_operations zpl_symlink_inode_operations = {
ee154f01
BB
407 .readlink = generic_readlink,
408 .follow_link = zpl_follow_link,
409 .put_link = zpl_put_link,
6f2255ba
BB
410 .setattr = zpl_setattr,
411 .getattr = zpl_getattr,
f31b3ebe
BB
412 .setxattr = generic_setxattr,
413 .getxattr = generic_getxattr,
414 .removexattr = generic_removexattr,
415 .listxattr = zpl_xattr_list,
ee154f01
BB
416};
417
418const struct inode_operations zpl_special_inode_operations = {
a6695d83
BB
419 .setattr = zpl_setattr,
420 .getattr = zpl_getattr,
421 .setxattr = generic_setxattr,
422 .getxattr = generic_getxattr,
423 .removexattr = generic_removexattr,
424 .listxattr = zpl_xattr_list,
ee154f01 425};