]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_inode.c
Add L2ARC tunables
[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
54static int
55zpl_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
80static int
81zpl_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
113static int
114zpl_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
127static int
128zpl_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
151static int
152zpl_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
165static int
166zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
167{
81e97e21 168 cred_t *cr = CRED();
5484965a
BB
169 vattr_t *vap;
170 struct inode *ip;
ee154f01
BB
171 int error;
172
5484965a 173 ip = dentry->d_inode;
81e97e21 174 crhold(cr);
5484965a
BB
175 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
176
177 error = -zfs_getattr(ip, vap, 0, cr);
178 if (error)
179 goto out;
180
181 stat->ino = ip->i_ino;
53cf50e0 182 stat->dev = ip->i_sb->s_dev;
5484965a
BB
183 stat->mode = vap->va_mode;
184 stat->nlink = vap->va_nlink;
185 stat->uid = vap->va_uid;
186 stat->gid = vap->va_gid;
187 stat->rdev = vap->va_rdev;
188 stat->size = vap->va_size;
189 stat->atime = vap->va_atime;
190 stat->mtime = vap->va_mtime;
191 stat->ctime = vap->va_ctime;
192 stat->blksize = vap->va_blksize;
193 stat->blocks = vap->va_nblocks;
194out:
195 kmem_free(vap, sizeof(vattr_t));
81e97e21 196 crfree(cr);
ee154f01
BB
197 ASSERT3S(error, <=, 0);
198
199 return (error);
200}
201
202static int
5484965a 203zpl_setattr(struct dentry *dentry, struct iattr *ia)
ee154f01 204{
81e97e21 205 cred_t *cr = CRED();
5484965a 206 vattr_t *vap;
ee154f01
BB
207 int error;
208
5484965a 209 error = inode_change_ok(dentry->d_inode, ia);
ee154f01
BB
210 if (error)
211 return (error);
212
81e97e21 213 crhold(cr);
5484965a
BB
214 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
215 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
216 vap->va_mode = ia->ia_mode;
217 vap->va_uid = ia->ia_uid;
218 vap->va_gid = ia->ia_gid;
219 vap->va_size = ia->ia_size;
220 vap->va_atime = ia->ia_atime;
221 vap->va_mtime = ia->ia_mtime;
222 vap->va_ctime = ia->ia_ctime;
223
224 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
225
226 kmem_free(vap, sizeof(vattr_t));
81e97e21 227 crfree(cr);
ee154f01
BB
228 ASSERT3S(error, <=, 0);
229
5484965a 230 return (error);
ee154f01
BB
231}
232
233static int
234zpl_rename(struct inode *sdip, struct dentry *sdentry,
235 struct inode *tdip, struct dentry *tdentry)
236{
81e97e21 237 cred_t *cr = CRED();
ee154f01
BB
238 int error;
239
81e97e21 240 crhold(cr);
ee154f01 241 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
81e97e21 242 crfree(cr);
ee154f01
BB
243 ASSERT3S(error, <=, 0);
244
245 return (error);
246}
247
248static int
249zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
250{
81e97e21 251 cred_t *cr = CRED();
ee154f01
BB
252 vattr_t *vap;
253 struct inode *ip;
254 int error;
255
81e97e21 256 crhold(cr);
ee154f01
BB
257 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
258 vap->va_mode = S_IFLNK | S_IRWXUGO;
259 vap->va_mask = ATTR_MODE;
81e97e21
BB
260 vap->va_uid = crgetfsuid(cr);
261 vap->va_gid = crgetfsgid(cr);
c85b224f 262 vap->va_dentry = dentry;
ee154f01
BB
263
264 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
ee154f01 265 kmem_free(vap, sizeof(vattr_t));
81e97e21 266 crfree(cr);
ee154f01
BB
267 ASSERT3S(error, <=, 0);
268
269 return (error);
270}
271
272static void *
273zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
274{
81e97e21 275 cred_t *cr = CRED();
8b4f9a2d
BB
276 struct inode *ip = dentry->d_inode;
277 struct iovec iov;
278 uio_t uio;
279 char *link;
8b4f9a2d
BB
280 int error;
281
81e97e21 282 crhold(cr);
8b4f9a2d
BB
283
284 iov.iov_len = MAXPATHLEN;
285 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
286
287 uio.uio_iov = &iov;
288 uio.uio_iovcnt = 1;
289 uio.uio_resid = (MAXPATHLEN - 1);
290 uio.uio_segflg = UIO_SYSSPACE;
291
50950001 292 error = -zfs_readlink(ip, &uio, cr);
8b4f9a2d
BB
293 if (error) {
294 kmem_free(link, MAXPATHLEN);
295 nd_set_link(nd, ERR_PTR(error));
296 } else {
297 nd_set_link(nd, link);
298 }
299
81e97e21 300 crfree(cr);
8b4f9a2d 301 return (NULL);
ee154f01
BB
302}
303
304static void
305zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
306{
307 char *link;
308
309 link = nd_get_link(nd);
310 if (!IS_ERR(link))
8b4f9a2d 311 kmem_free(link, MAXPATHLEN);
ee154f01
BB
312}
313
314static int
315zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
316{
81e97e21 317 cred_t *cr = CRED();
ee154f01 318 struct inode *ip = old_dentry->d_inode;
ee154f01
BB
319 int error;
320
321 if (ip->i_nlink >= ZFS_LINK_MAX)
322 return -EMLINK;
323
81e97e21 324 crhold(cr);
ee154f01
BB
325 ip->i_ctime = CURRENT_TIME_SEC;
326 igrab(ip); /* Use ihold() if available */
327
328 error = -zfs_link(dir, ip, dname(dentry), cr);
329 if (error) {
330 iput(ip);
331 goto out;
332 }
333
334 d_instantiate(dentry, ip);
335out:
81e97e21 336 crfree(cr);
ee154f01
BB
337 ASSERT3S(error, <=, 0);
338
339 return (error);
340}
341
342const struct inode_operations zpl_inode_operations = {
ee154f01
BB
343 .create = zpl_create,
344 .link = zpl_link,
345 .unlink = zpl_unlink,
346 .symlink = zpl_symlink,
347 .mkdir = zpl_mkdir,
348 .rmdir = zpl_rmdir,
349 .mknod = zpl_mknod,
350 .rename = zpl_rename,
351 .setattr = zpl_setattr,
352 .getattr = zpl_getattr,
353 .setxattr = generic_setxattr,
354 .getxattr = generic_getxattr,
355 .removexattr = generic_removexattr,
356 .listxattr = zpl_xattr_list,
357};
358
359const struct inode_operations zpl_dir_inode_operations = {
ee154f01
BB
360 .create = zpl_create,
361 .lookup = zpl_lookup,
362 .link = zpl_link,
363 .unlink = zpl_unlink,
364 .symlink = zpl_symlink,
365 .mkdir = zpl_mkdir,
366 .rmdir = zpl_rmdir,
367 .mknod = zpl_mknod,
368 .rename = zpl_rename,
369 .setattr = zpl_setattr,
a6695d83
BB
370 .getattr = zpl_getattr,
371 .setxattr = generic_setxattr,
372 .getxattr = generic_getxattr,
373 .removexattr = generic_removexattr,
374 .listxattr = zpl_xattr_list,
ee154f01
BB
375};
376
377const struct inode_operations zpl_symlink_inode_operations = {
ee154f01
BB
378 .readlink = generic_readlink,
379 .follow_link = zpl_follow_link,
380 .put_link = zpl_put_link,
381};
382
383const struct inode_operations zpl_special_inode_operations = {
a6695d83
BB
384 .setattr = zpl_setattr,
385 .getattr = zpl_getattr,
386 .setxattr = generic_setxattr,
387 .getxattr = generic_getxattr,
388 .removexattr = generic_removexattr,
389 .listxattr = zpl_xattr_list,
ee154f01 390};