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