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