]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-vnode.c
Linux 3.9 compat: Include linux/sched/rt.h
[mirror_spl.git] / module / spl / spl-vnode.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Vnode Implementation.
25\*****************************************************************************/
715f6251 26
4b171585 27#include <sys/vnode.h>
bbdc6ae4 28#include <linux/falloc.h>
55abb092 29#include <spl-debug.h>
937879f1 30
b17edc10
BB
31#ifdef SS_DEBUG_SUBSYS
32#undef SS_DEBUG_SUBSYS
937879f1 33#endif
34
b17edc10 35#define SS_DEBUG_SUBSYS SS_VNODE
937879f1 36
51a727e9 37vnode_t *rootdir = (vnode_t *)0xabcd1234;
4b171585 38EXPORT_SYMBOL(rootdir);
39
7afde631 40static spl_kmem_cache_t *vn_cache;
41static spl_kmem_cache_t *vn_file_cache;
e4f1d29f 42
83c623aa 43static DEFINE_SPINLOCK(vn_file_lock);
e4f1d29f 44static LIST_HEAD(vn_file_list);
af828292 45
12ff95ff
BB
46#ifdef HAVE_KERN_PATH_PARENT_HEADER
47#ifndef HAVE_KERN_PATH_PARENT_SYMBOL
48kern_path_parent_t kern_path_parent_fn = SYMBOL_POISON;
49EXPORT_SYMBOL(kern_path_parent_fn);
50#endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
51#endif /* HAVE_KERN_PATH_PARENT_HEADER */
52
bcb15891
YS
53#ifdef HAVE_KERN_PATH_LOCKED
54kern_path_locked_t kern_path_locked_fn = SYMBOL_POISON;
55#endif /* HAVE_KERN_PATH_LOCKED */
56
4295b530
BB
57vtype_t
58vn_mode_to_vtype(mode_t mode)
4b171585 59{
60 if (S_ISREG(mode))
61 return VREG;
62
63 if (S_ISDIR(mode))
64 return VDIR;
65
66 if (S_ISCHR(mode))
67 return VCHR;
68
69 if (S_ISBLK(mode))
70 return VBLK;
71
72 if (S_ISFIFO(mode))
73 return VFIFO;
74
75 if (S_ISLNK(mode))
76 return VLNK;
77
78 if (S_ISSOCK(mode))
79 return VSOCK;
80
81 if (S_ISCHR(mode))
82 return VCHR;
83
84 return VNON;
4295b530
BB
85} /* vn_mode_to_vtype() */
86EXPORT_SYMBOL(vn_mode_to_vtype);
87
88mode_t
89vn_vtype_to_mode(vtype_t vtype)
90{
91 if (vtype == VREG)
92 return S_IFREG;
93
94 if (vtype == VDIR)
95 return S_IFDIR;
96
97 if (vtype == VCHR)
98 return S_IFCHR;
99
100 if (vtype == VBLK)
101 return S_IFBLK;
102
103 if (vtype == VFIFO)
104 return S_IFIFO;
105
106 if (vtype == VLNK)
107 return S_IFLNK;
108
109 if (vtype == VSOCK)
110 return S_IFSOCK;
111
112 return VNON;
113} /* vn_vtype_to_mode() */
114EXPORT_SYMBOL(vn_vtype_to_mode);
4b171585 115
af828292 116vnode_t *
117vn_alloc(int flag)
118{
119 vnode_t *vp;
b17edc10 120 SENTRY;
af828292 121
122 vp = kmem_cache_alloc(vn_cache, flag);
af828292 123 if (vp != NULL) {
e4f1d29f 124 vp->v_file = NULL;
af828292 125 vp->v_type = 0;
126 }
127
b17edc10 128 SRETURN(vp);
af828292 129} /* vn_alloc() */
130EXPORT_SYMBOL(vn_alloc);
131
132void
133vn_free(vnode_t *vp)
134{
b17edc10 135 SENTRY;
af828292 136 kmem_cache_free(vn_cache, vp);
b17edc10 137 SEXIT;
af828292 138} /* vn_free() */
139EXPORT_SYMBOL(vn_free);
140
0b3cf046 141int
af828292 142vn_open(const char *path, uio_seg_t seg, int flags, int mode,
4b171585 143 vnode_t **vpp, int x1, void *x2)
0b3cf046 144{
f7e8739c
RC
145 struct file *fp;
146 struct kstat stat;
147 int rc, saved_umask = 0;
4be55565 148 gfp_t saved_gfp;
0b3cf046 149 vnode_t *vp;
b17edc10 150 SENTRY;
0b3cf046 151
937879f1 152 ASSERT(flags & (FWRITE | FREAD));
153 ASSERT(seg == UIO_SYSSPACE);
154 ASSERT(vpp);
4b171585 155 *vpp = NULL;
156
157 if (!(flags & FCREAT) && (flags & FWRITE))
158 flags |= FEXCL;
159
728b9dd8 160 /* Note for filp_open() the two low bits must be remapped to mean:
161 * 01 - read-only -> 00 read-only
162 * 10 - write-only -> 01 write-only
163 * 11 - read-write -> 10 read-write
164 */
165 flags--;
0b3cf046 166
167 if (flags & FCREAT)
4b171585 168 saved_umask = xchg(&current->fs->umask, 0);
0b3cf046 169
f7e8739c 170 fp = filp_open(path, flags, mode);
0b3cf046 171
172 if (flags & FCREAT)
4b171585 173 (void)xchg(&current->fs->umask, saved_umask);
0b3cf046 174
f7e8739c 175 if (IS_ERR(fp))
b17edc10 176 SRETURN(-PTR_ERR(fp));
0b3cf046 177
f7e8739c 178 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
4b171585 179 if (rc) {
180 filp_close(fp, 0);
b17edc10 181 SRETURN(-rc);
0b3cf046 182 }
183
af828292 184 vp = vn_alloc(KM_SLEEP);
4b171585 185 if (!vp) {
186 filp_close(fp, 0);
b17edc10 187 SRETURN(ENOMEM);
4b171585 188 }
0b3cf046 189
4be55565
LW
190 saved_gfp = mapping_gfp_mask(fp->f_mapping);
191 mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
192
e4f1d29f 193 mutex_enter(&vp->v_lock);
4295b530 194 vp->v_type = vn_mode_to_vtype(stat.mode);
e4f1d29f 195 vp->v_file = fp;
4be55565 196 vp->v_gfp_mask = saved_gfp;
4b171585 197 *vpp = vp;
e4f1d29f 198 mutex_exit(&vp->v_lock);
0b3cf046 199
b17edc10 200 SRETURN(0);
4b171585 201} /* vn_open() */
202EXPORT_SYMBOL(vn_open);
0b3cf046 203
0b3cf046 204int
af828292 205vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
4b171585 206 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
0b3cf046 207{
4b171585 208 char *realpath;
12018327 209 int len, rc;
b17edc10 210 SENTRY;
0b3cf046 211
937879f1 212 ASSERT(vp == rootdir);
0b3cf046 213
12018327 214 len = strlen(path) + 2;
215 realpath = kmalloc(len, GFP_KERNEL);
4b171585 216 if (!realpath)
b17edc10 217 SRETURN(ENOMEM);
0b3cf046 218
12018327 219 (void)snprintf(realpath, len, "/%s", path);
4b171585 220 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
4b171585 221 kfree(realpath);
222
b17edc10 223 SRETURN(rc);
4b171585 224} /* vn_openat() */
225EXPORT_SYMBOL(vn_openat);
0b3cf046 226
0b3cf046 227int
4b171585 228vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
663e02a1 229 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
0b3cf046 230{
4b171585 231 loff_t offset;
232 mm_segment_t saved_fs;
233 struct file *fp;
234 int rc;
b17edc10 235 SENTRY;
4b171585 236
937879f1 237 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
238 ASSERT(vp);
239 ASSERT(vp->v_file);
240 ASSERT(seg == UIO_SYSSPACE);
663e02a1 241 ASSERT((ioflag & ~FAPPEND) == 0);
937879f1 242 ASSERT(x2 == RLIM64_INFINITY);
4b171585 243
e4f1d29f 244 fp = vp->v_file;
4b171585 245
663e02a1
RC
246 offset = off;
247 if (ioflag & FAPPEND)
248 offset = fp->f_pos;
249
4b171585 250 /* Writable user data segment must be briefly increased for this
251 * process so we can use the user space read call paths to write
252 * in to memory allocated by the kernel. */
253 saved_fs = get_fs();
254 set_fs(get_ds());
255
256 if (uio & UIO_WRITE)
257 rc = vfs_write(fp, addr, len, &offset);
258 else
259 rc = vfs_read(fp, addr, len, &offset);
260
261 set_fs(saved_fs);
f3989ed3 262 fp->f_pos = offset;
4b171585 263
264 if (rc < 0)
b17edc10 265 SRETURN(-rc);
0b3cf046 266
4b171585 267 if (residp) {
268 *residp = len - rc;
0b3cf046 269 } else {
4b171585 270 if (rc != len)
b17edc10 271 SRETURN(EIO);
0b3cf046 272 }
273
b17edc10 274 SRETURN(0);
4b171585 275} /* vn_rdwr() */
276EXPORT_SYMBOL(vn_rdwr);
277
278int
2f5d55aa 279vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
4b171585 280{
281 int rc;
b17edc10 282 SENTRY;
4b171585 283
937879f1 284 ASSERT(vp);
285 ASSERT(vp->v_file);
4b171585 286
4be55565 287 mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
97735c39
BB
288 rc = filp_close(vp->v_file, 0);
289 vn_free(vp);
4b171585 290
b17edc10 291 SRETURN(-rc);
4b171585 292} /* vn_close() */
293EXPORT_SYMBOL(vn_close);
294
97735c39
BB
295/* vn_seek() does not actually seek it only performs bounds checking on the
296 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
297 * anything more serious. */
298int
47995fa6 299vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
97735c39
BB
300{
301 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
302}
303EXPORT_SYMBOL(vn_seek);
304
bcb15891
YS
305#ifdef HAVE_KERN_PATH_LOCKED
306/* Based on do_unlinkat() from linux/fs/namei.c */
307int
308vn_remove(const char *path, uio_seg_t seg, int flags)
309{
310 struct dentry *dentry;
311 struct path parent;
312 struct inode *inode = NULL;
313 int rc = 0;
314 SENTRY;
315
316 ASSERT(seg == UIO_SYSSPACE);
317 ASSERT(flags == RMFILE);
318
319 dentry = spl_kern_path_locked(path, &parent);
320 rc = PTR_ERR(dentry);
321 if (!IS_ERR(dentry)) {
322 if (parent.dentry->d_name.name[parent.dentry->d_name.len])
323 SGOTO(slashes, rc = 0);
324
325 inode = dentry->d_inode;
326 if (!inode)
327 SGOTO(slashes, rc = 0);
328
329 if (inode)
330 ihold(inode);
331
332 rc = vfs_unlink(parent.dentry->d_inode, dentry);
333exit1:
334 dput(dentry);
053678f3
BB
335 } else {
336 return (-rc);
bcb15891
YS
337 }
338
339 spl_inode_unlock(parent.dentry->d_inode);
340 if (inode)
341 iput(inode); /* truncate the inode here */
342
343 path_put(&parent);
344 SRETURN(-rc);
345
346slashes:
347 rc = !dentry->d_inode ? -ENOENT :
348 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
349 SGOTO(exit1, rc);
350} /* vn_remove() */
351EXPORT_SYMBOL(vn_remove);
352
353/* Based on do_rename() from linux/fs/namei.c */
354int
355vn_rename(const char *oldname, const char *newname, int x1)
356{
357 struct dentry *old_dir, *new_dir;
358 struct dentry *old_dentry, *new_dentry;
359 struct dentry *trap;
360 struct path old_parent, new_parent;
361 int rc = 0;
362 SENTRY;
363
364 old_dentry = spl_kern_path_locked(oldname, &old_parent);
365 if (IS_ERR(old_dentry))
366 SGOTO(exit, rc = PTR_ERR(old_dentry));
367
368 spl_inode_unlock(old_parent.dentry->d_inode);
369
370 new_dentry = spl_kern_path_locked(newname, &new_parent);
371 if (IS_ERR(new_dentry))
372 SGOTO(exit2, rc = PTR_ERR(new_dentry));
373
374 spl_inode_unlock(new_parent.dentry->d_inode);
375
376 rc = -EXDEV;
377 if (old_parent.mnt != new_parent.mnt)
378 SGOTO(exit3, rc);
379
380 old_dir = old_parent.dentry;
381 new_dir = new_parent.dentry;
382 trap = lock_rename(new_dir, old_dir);
383
384 /* source should not be ancestor of target */
385 rc = -EINVAL;
386 if (old_dentry == trap)
387 SGOTO(exit4, rc);
388
389 /* target should not be an ancestor of source */
390 rc = -ENOTEMPTY;
391 if (new_dentry == trap)
392 SGOTO(exit4, rc);
393
394 /* source must exist */
395 rc = -ENOENT;
396 if (!old_dentry->d_inode)
397 SGOTO(exit4, rc);
398
399 /* unless the source is a directory trailing slashes give -ENOTDIR */
400 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
401 rc = -ENOTDIR;
402 if (old_dentry->d_name.name[old_dentry->d_name.len])
403 SGOTO(exit4, rc);
404 if (new_dentry->d_name.name[new_dentry->d_name.len])
405 SGOTO(exit4, rc);
406 }
407
408#ifdef HAVE_4ARGS_VFS_RENAME
409 rc = vfs_rename(old_dir->d_inode, old_dentry,
410 new_dir->d_inode, new_dentry);
411#else
412 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
413 new_dir->d_inode, new_dentry, newnd.nd_mnt);
414#endif /* HAVE_4ARGS_VFS_RENAME */
415exit4:
416 unlock_rename(new_dir, old_dir);
417exit3:
418 dput(new_dentry);
419 path_put(&new_parent);
420exit2:
421 dput(old_dentry);
422 path_put(&old_parent);
423exit:
424 SRETURN(-rc);
425}
426EXPORT_SYMBOL(vn_rename);
427
428#else
97735c39
BB
429static struct dentry *
430vn_lookup_hash(struct nameidata *nd)
4b171585 431{
849c50e7
BB
432 return lookup_one_len((const char *)nd->last.name,
433 nd->nd_dentry, nd->last.len);
4b171585 434} /* lookup_hash() */
435
97735c39
BB
436static void
437vn_path_release(struct nameidata *nd)
57d86234 438{
439 dput(nd->nd_dentry);
440 mntput(nd->nd_mnt);
441}
442
4b171585 443/* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
444int
af828292 445vn_remove(const char *path, uio_seg_t seg, int flags)
4b171585 446{
447 struct dentry *dentry;
448 struct nameidata nd;
449 struct inode *inode = NULL;
450 int rc = 0;
b17edc10 451 SENTRY;
4b171585 452
3d061e9d 453 ASSERT(seg == UIO_SYSSPACE);
454 ASSERT(flags == RMFILE);
2f5d55aa 455
b1cbc461 456 rc = spl_kern_path_parent(path, &nd);
4b171585 457 if (rc)
b17edc10 458 SGOTO(exit, rc);
4b171585 459
460 rc = -EISDIR;
461 if (nd.last_type != LAST_NORM)
b17edc10 462 SGOTO(exit1, rc);
4b171585 463
6bf4d76f 464 spl_inode_lock_nested(nd.nd_dentry->d_inode, I_MUTEX_PARENT);
57d86234 465 dentry = vn_lookup_hash(&nd);
4b171585 466 rc = PTR_ERR(dentry);
467 if (!IS_ERR(dentry)) {
468 /* Why not before? Because we want correct rc value */
469 if (nd.last.name[nd.last.len])
b17edc10 470 SGOTO(slashes, rc);
937879f1 471
4b171585 472 inode = dentry->d_inode;
473 if (inode)
474 atomic_inc(&inode->i_count);
a093c6a4 475#ifdef HAVE_2ARGS_VFS_UNLINK
57d86234 476 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
a093c6a4 477#else
21411161 478 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
a093c6a4 479#endif /* HAVE_2ARGS_VFS_UNLINK */
4b171585 480exit2:
481 dput(dentry);
482 }
6bf4d76f
BB
483
484 spl_inode_unlock(nd.nd_dentry->d_inode);
4b171585 485 if (inode)
486 iput(inode); /* truncate the inode here */
487exit1:
57d86234 488 vn_path_release(&nd);
4b171585 489exit:
b17edc10 490 SRETURN(-rc);
4b171585 491
492slashes:
493 rc = !dentry->d_inode ? -ENOENT :
494 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
b17edc10 495 SGOTO(exit2, rc);
4b171585 496} /* vn_remove() */
497EXPORT_SYMBOL(vn_remove);
498
499/* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
500int
501vn_rename(const char *oldname, const char *newname, int x1)
502{
a093c6a4
BB
503 struct dentry *old_dir, *new_dir;
504 struct dentry *old_dentry, *new_dentry;
505 struct dentry *trap;
4b171585 506 struct nameidata oldnd, newnd;
507 int rc = 0;
b17edc10 508 SENTRY;
4b171585 509
b1cbc461 510 rc = spl_kern_path_parent(oldname, &oldnd);
4b171585 511 if (rc)
b17edc10 512 SGOTO(exit, rc);
4b171585 513
b1cbc461 514 rc = spl_kern_path_parent(newname, &newnd);
4b171585 515 if (rc)
b17edc10 516 SGOTO(exit1, rc);
4b171585 517
518 rc = -EXDEV;
57d86234 519 if (oldnd.nd_mnt != newnd.nd_mnt)
b17edc10 520 SGOTO(exit2, rc);
4b171585 521
57d86234 522 old_dir = oldnd.nd_dentry;
4b171585 523 rc = -EBUSY;
524 if (oldnd.last_type != LAST_NORM)
b17edc10 525 SGOTO(exit2, rc);
4b171585 526
57d86234 527 new_dir = newnd.nd_dentry;
4b171585 528 if (newnd.last_type != LAST_NORM)
b17edc10 529 SGOTO(exit2, rc);
4b171585 530
531 trap = lock_rename(new_dir, old_dir);
532
57d86234 533 old_dentry = vn_lookup_hash(&oldnd);
4b171585 534
535 rc = PTR_ERR(old_dentry);
536 if (IS_ERR(old_dentry))
b17edc10 537 SGOTO(exit3, rc);
4b171585 538
539 /* source must exist */
540 rc = -ENOENT;
541 if (!old_dentry->d_inode)
b17edc10 542 SGOTO(exit4, rc);
4b171585 543
544 /* unless the source is a directory trailing slashes give -ENOTDIR */
545 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
546 rc = -ENOTDIR;
547 if (oldnd.last.name[oldnd.last.len])
b17edc10 548 SGOTO(exit4, rc);
4b171585 549 if (newnd.last.name[newnd.last.len])
b17edc10 550 SGOTO(exit4, rc);
4b171585 551 }
552
553 /* source should not be ancestor of target */
554 rc = -EINVAL;
555 if (old_dentry == trap)
b17edc10 556 SGOTO(exit4, rc);
4b171585 557
57d86234 558 new_dentry = vn_lookup_hash(&newnd);
4b171585 559 rc = PTR_ERR(new_dentry);
560 if (IS_ERR(new_dentry))
b17edc10 561 SGOTO(exit4, rc);
4b171585 562
563 /* target should not be an ancestor of source */
564 rc = -ENOTEMPTY;
565 if (new_dentry == trap)
b17edc10 566 SGOTO(exit5, rc);
4b171585 567
a093c6a4 568#ifdef HAVE_4ARGS_VFS_RENAME
4b171585 569 rc = vfs_rename(old_dir->d_inode, old_dentry,
570 new_dir->d_inode, new_dentry);
a093c6a4 571#else
21411161
BB
572 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
573 new_dir->d_inode, new_dentry, newnd.nd_mnt);
a093c6a4 574#endif /* HAVE_4ARGS_VFS_RENAME */
4b171585 575exit5:
576 dput(new_dentry);
577exit4:
578 dput(old_dentry);
579exit3:
580 unlock_rename(new_dir, old_dir);
581exit2:
57d86234 582 vn_path_release(&newnd);
4b171585 583exit1:
57d86234 584 vn_path_release(&oldnd);
4b171585 585exit:
b17edc10 586 SRETURN(-rc);
0b3cf046 587}
4b171585 588EXPORT_SYMBOL(vn_rename);
bcb15891 589#endif /* HAVE_KERN_PATH_LOCKED */
0b3cf046 590
4b171585 591int
36e6f861 592vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
0b3cf046 593{
4b171585 594 struct file *fp;
dcd9cb5a 595 struct kstat stat;
4b171585 596 int rc;
b17edc10 597 SENTRY;
4b171585 598
937879f1 599 ASSERT(vp);
600 ASSERT(vp->v_file);
601 ASSERT(vap);
4b171585 602
e4f1d29f 603 fp = vp->v_file;
4b171585 604
605 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
606 if (rc)
b17edc10 607 SRETURN(-rc);
4b171585 608
4295b530 609 vap->va_type = vn_mode_to_vtype(stat.mode);
4b171585 610 vap->va_mode = stat.mode;
611 vap->va_uid = stat.uid;
612 vap->va_gid = stat.gid;
613 vap->va_fsid = 0;
614 vap->va_nodeid = stat.ino;
615 vap->va_nlink = stat.nlink;
616 vap->va_size = stat.size;
47995fa6 617 vap->va_blksize = stat.blksize;
dcd9cb5a
BB
618 vap->va_atime = stat.atime;
619 vap->va_mtime = stat.mtime;
620 vap->va_ctime = stat.ctime;
4b171585 621 vap->va_rdev = stat.rdev;
dcd9cb5a 622 vap->va_nblocks = stat.blocks;
4b171585 623
dcd9cb5a 624 SRETURN(0);
0b3cf046 625}
4b171585 626EXPORT_SYMBOL(vn_getattr);
627
2f5d55aa 628int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
4b171585 629{
36e6f861 630 int datasync = 0;
b17edc10 631 SENTRY;
36e6f861 632
937879f1 633 ASSERT(vp);
634 ASSERT(vp->v_file);
4b171585 635
36e6f861 636 if (flags & FDSYNC)
637 datasync = 1;
638
b17edc10 639 SRETURN(-spl_filp_fsync(vp->v_file, datasync));
4b171585 640} /* vn_fsync() */
641EXPORT_SYMBOL(vn_fsync);
af828292 642
bbdc6ae4
ED
643int vn_space(vnode_t *vp, int cmd, struct flock *bfp, int flag,
644 offset_t offset, void *x6, void *x7)
645{
646 int error = EOPNOTSUPP;
647 SENTRY;
648
649 if (cmd != F_FREESP || bfp->l_whence != 0)
650 SRETURN(EOPNOTSUPP);
651
652 ASSERT(vp);
653 ASSERT(vp->v_file);
654 ASSERT(bfp->l_start >= 0 && bfp->l_len > 0);
655
656#ifdef FALLOC_FL_PUNCH_HOLE
1c7b3eaf
BB
657 /*
658 * When supported by the underlying file system preferentially
659 * use the fallocate() callback to preallocate the space.
660 */
661 error = -spl_filp_fallocate(vp->v_file,
662 FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
663 bfp->l_start, bfp->l_len);
664 if (error == 0)
665 SRETURN(0);
bbdc6ae4
ED
666#endif
667
668#ifdef HAVE_INODE_TRUNCATE_RANGE
669 if (vp->v_file->f_dentry && vp->v_file->f_dentry->d_inode &&
670 vp->v_file->f_dentry->d_inode->i_op &&
671 vp->v_file->f_dentry->d_inode->i_op->truncate_range) {
672 off_t end = bfp->l_start + bfp->l_len;
673 /*
674 * Judging from the code in shmem_truncate_range(),
675 * it seems the kernel expects the end offset to be
676 * inclusive and aligned to the end of a page.
677 */
678 if (end % PAGE_SIZE != 0) {
679 end &= ~(off_t)(PAGE_SIZE - 1);
680 if (end <= bfp->l_start)
681 SRETURN(0);
682 }
683 --end;
684
685 vp->v_file->f_dentry->d_inode->i_op->truncate_range(
686 vp->v_file->f_dentry->d_inode,
687 bfp->l_start, end
688 );
689 SRETURN(0);
690 }
691#endif
692
693 SRETURN(error);
694}
695EXPORT_SYMBOL(vn_space);
696
e4f1d29f 697/* Function must be called while holding the vn_file_lock */
698static file_t *
699file_find(int fd)
700{
701 file_t *fp;
702
937879f1 703 ASSERT(spin_is_locked(&vn_file_lock));
e4f1d29f 704
705 list_for_each_entry(fp, &vn_file_list, f_list) {
763b2f3b 706 if (fd == fp->f_fd && fp->f_task == current) {
937879f1 707 ASSERT(atomic_read(&fp->f_ref) != 0);
e4f1d29f 708 return fp;
709 }
710 }
711
712 return NULL;
713} /* file_find() */
714
715file_t *
716vn_getf(int fd)
717{
718 struct kstat stat;
719 struct file *lfp;
720 file_t *fp;
721 vnode_t *vp;
937879f1 722 int rc = 0;
b17edc10 723 SENTRY;
e4f1d29f 724
725 /* Already open just take an extra reference */
726 spin_lock(&vn_file_lock);
727
728 fp = file_find(fd);
729 if (fp) {
730 atomic_inc(&fp->f_ref);
731 spin_unlock(&vn_file_lock);
b17edc10 732 SRETURN(fp);
e4f1d29f 733 }
734
735 spin_unlock(&vn_file_lock);
736
737 /* File was not yet opened create the object and setup */
4afaaefa 738 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
e4f1d29f 739 if (fp == NULL)
b17edc10 740 SGOTO(out, rc);
e4f1d29f 741
742 mutex_enter(&fp->f_lock);
743
744 fp->f_fd = fd;
763b2f3b 745 fp->f_task = current;
e4f1d29f 746 fp->f_offset = 0;
747 atomic_inc(&fp->f_ref);
748
749 lfp = fget(fd);
750 if (lfp == NULL)
b17edc10 751 SGOTO(out_mutex, rc);
e4f1d29f 752
753 vp = vn_alloc(KM_SLEEP);
754 if (vp == NULL)
b17edc10 755 SGOTO(out_fget, rc);
e4f1d29f 756
757 if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
b17edc10 758 SGOTO(out_vnode, rc);
e4f1d29f 759
760 mutex_enter(&vp->v_lock);
4295b530 761 vp->v_type = vn_mode_to_vtype(stat.mode);
e4f1d29f 762 vp->v_file = lfp;
763 mutex_exit(&vp->v_lock);
764
765 fp->f_vnode = vp;
766 fp->f_file = lfp;
767
768 /* Put it on the tracking list */
769 spin_lock(&vn_file_lock);
770 list_add(&fp->f_list, &vn_file_list);
771 spin_unlock(&vn_file_lock);
772
773 mutex_exit(&fp->f_lock);
b17edc10 774 SRETURN(fp);
e4f1d29f 775
776out_vnode:
e4f1d29f 777 vn_free(vp);
778out_fget:
e4f1d29f 779 fput(lfp);
780out_mutex:
e4f1d29f 781 mutex_exit(&fp->f_lock);
782 kmem_cache_free(vn_file_cache, fp);
783out:
b17edc10 784 SRETURN(NULL);
e4f1d29f 785} /* getf() */
786EXPORT_SYMBOL(getf);
787
788static void releasef_locked(file_t *fp)
789{
937879f1 790 ASSERT(fp->f_file);
791 ASSERT(fp->f_vnode);
e4f1d29f 792
793 /* Unlinked from list, no refs, safe to free outside mutex */
794 fput(fp->f_file);
795 vn_free(fp->f_vnode);
796
797 kmem_cache_free(vn_file_cache, fp);
798}
799
800void
801vn_releasef(int fd)
802{
803 file_t *fp;
b17edc10 804 SENTRY;
e4f1d29f 805
806 spin_lock(&vn_file_lock);
807 fp = file_find(fd);
808 if (fp) {
809 atomic_dec(&fp->f_ref);
810 if (atomic_read(&fp->f_ref) > 0) {
811 spin_unlock(&vn_file_lock);
b17edc10 812 SEXIT;
e4f1d29f 813 return;
814 }
815
816 list_del(&fp->f_list);
817 releasef_locked(fp);
818 }
819 spin_unlock(&vn_file_lock);
820
b17edc10 821 SEXIT;
e4f1d29f 822 return;
823} /* releasef() */
824EXPORT_SYMBOL(releasef);
825
51a727e9
BB
826#ifndef HAVE_SET_FS_PWD
827# ifdef HAVE_2ARGS_SET_FS_PWD
828/* Used from 2.6.25 - 2.6.31+ */
829void
830set_fs_pwd(struct fs_struct *fs, struct path *path)
831{
9b2048c2
BB
832 struct path old_pwd;
833
834# ifdef HAVE_FS_STRUCT_SPINLOCK
835 spin_lock(&fs->lock);
836 old_pwd = fs->pwd;
837 fs->pwd = *path;
838 path_get(path);
839 spin_unlock(&fs->lock);
840# else
841 write_lock(&fs->lock);
842 old_pwd = fs->pwd;
843 fs->pwd = *path;
844 path_get(path);
845 write_unlock(&fs->lock);
846# endif /* HAVE_FS_STRUCT_SPINLOCK */
847
848 if (old_pwd.dentry)
849 path_put(&old_pwd);
51a727e9
BB
850}
851# else
852/* Used from 2.6.11 - 2.6.24 */
853void
854set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt, struct dentry *dentry)
855{
856 struct dentry *old_pwd;
857 struct vfsmount *old_pwdmnt;
858
859 write_lock(&fs->lock);
860 old_pwd = fs->pwd;
861 old_pwdmnt = fs->pwdmnt;
862 fs->pwdmnt = mntget(mnt);
863 fs->pwd = dget(dentry);
864 write_unlock(&fs->lock);
865
866 if (old_pwd) {
867 dput(old_pwd);
868 mntput(old_pwdmnt);
869 }
870}
871# endif /* HAVE_2ARGS_SET_FS_PWD */
872#endif /* HAVE_SET_FS_PWD */
873
874int
875vn_set_pwd(const char *filename)
876{
7119bf70 877#if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
51a727e9 878 struct path path;
82a358d9
BB
879#else
880 struct nameidata nd;
881#endif /* HAVE_2ARGS_SET_FS_PWD */
882 mm_segment_t saved_fs;
51a727e9 883 int rc;
b17edc10 884 SENTRY;
51a727e9 885
82a358d9
BB
886 /*
887 * user_path_dir() and __user_walk() both expect 'filename' to be
888 * a user space address so we must briefly increase the data segment
889 * size to ensure strncpy_from_user() does not fail with -EFAULT.
890 */
891 saved_fs = get_fs();
892 set_fs(get_ds());
893
894#ifdef HAVE_2ARGS_SET_FS_PWD
7119bf70 895# ifdef HAVE_USER_PATH_DIR
51a727e9
BB
896 rc = user_path_dir(filename, &path);
897 if (rc)
b17edc10 898 SGOTO(out, rc);
51a727e9
BB
899
900 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
901 if (rc)
b17edc10 902 SGOTO(dput_and_out, rc);
51a727e9
BB
903
904 set_fs_pwd(current->fs, &path);
905
906dput_and_out:
907 path_put(&path);
7119bf70
BB
908# else
909 rc = __user_walk(filename,
910 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
911 if (rc)
b17edc10 912 SGOTO(out, rc);
7119bf70
BB
913
914 rc = vfs_permission(&nd, MAY_EXEC);
915 if (rc)
b17edc10 916 SGOTO(dput_and_out, rc);
7119bf70
BB
917
918 set_fs_pwd(current->fs, &nd.path);
919
920dput_and_out:
921 path_put(&nd.path);
922# endif /* HAVE_USER_PATH_DIR */
51a727e9 923#else
51a727e9
BB
924 rc = __user_walk(filename,
925 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
926 if (rc)
b17edc10 927 SGOTO(out, rc);
51a727e9
BB
928
929 rc = vfs_permission(&nd, MAY_EXEC);
930 if (rc)
b17edc10 931 SGOTO(dput_and_out, rc);
51a727e9
BB
932
933 set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
934
935dput_and_out:
936 vn_path_release(&nd);
937#endif /* HAVE_2ARGS_SET_FS_PWD */
938out:
82a358d9
BB
939 set_fs(saved_fs);
940
b17edc10 941 SRETURN(-rc);
51a727e9
BB
942} /* vn_set_pwd() */
943EXPORT_SYMBOL(vn_set_pwd);
944
af828292 945static int
946vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
947{
948 struct vnode *vp = buf;
949
950 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
951
952 return (0);
953} /* vn_cache_constructor() */
954
955static void
956vn_cache_destructor(void *buf, void *cdrarg)
957{
958 struct vnode *vp = buf;
959
960 mutex_destroy(&vp->v_lock);
961} /* vn_cache_destructor() */
962
e4f1d29f 963static int
964vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
965{
966 file_t *fp = buf;
967
968 atomic_set(&fp->f_ref, 0);
969 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
4e62fd41 970 INIT_LIST_HEAD(&fp->f_list);
e4f1d29f 971
972 return (0);
973} /* file_cache_constructor() */
974
975static void
976vn_file_cache_destructor(void *buf, void *cdrarg)
977{
978 file_t *fp = buf;
979
980 mutex_destroy(&fp->f_lock);
981} /* vn_file_cache_destructor() */
982
12ff95ff
BB
983int spl_vn_init_kallsyms_lookup(void)
984{
985#ifdef HAVE_KERN_PATH_PARENT_HEADER
986#ifndef HAVE_KERN_PATH_PARENT_SYMBOL
987 kern_path_parent_fn = (kern_path_parent_t)
988 spl_kallsyms_lookup_name("kern_path_parent");
989 if (!kern_path_parent_fn) {
990 printk(KERN_ERR "Error: Unknown symbol kern_path_parent\n");
991 return -EFAULT;
992 }
993#endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
994#endif /* HAVE_KERN_PATH_PARENT_HEADER */
995
bcb15891
YS
996#ifdef HAVE_KERN_PATH_LOCKED
997 kern_path_locked_fn = (kern_path_locked_t)
998 spl_kallsyms_lookup_name("kern_path_locked");
999 if (!kern_path_locked_fn) {
1000 printk(KERN_ERR "Error: Unknown symbol kern_path_locked\n");
1001 return -EFAULT;
1002 }
1003#endif
1004
12ff95ff
BB
1005 return (0);
1006}
1007
af828292 1008int
12ff95ff 1009spl_vn_init(void)
af828292 1010{
b17edc10 1011 SENTRY;
57d86234 1012 vn_cache = kmem_cache_create("spl_vn_cache",
1013 sizeof(struct vnode), 64,
5d86345d 1014 vn_cache_constructor,
1015 vn_cache_destructor,
a5b40eed 1016 NULL, NULL, NULL, KMC_KMEM);
e4f1d29f 1017
1018 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
1019 sizeof(file_t), 64,
1020 vn_file_cache_constructor,
1021 vn_file_cache_destructor,
a5b40eed 1022 NULL, NULL, NULL, KMC_KMEM);
b17edc10 1023 SRETURN(0);
af828292 1024} /* vn_init() */
1025
1026void
12ff95ff 1027spl_vn_fini(void)
af828292 1028{
e4f1d29f 1029 file_t *fp, *next_fp;
2fb9b26a 1030 int leaked = 0;
b17edc10 1031 SENTRY;
e4f1d29f 1032
1033 spin_lock(&vn_file_lock);
1034
1035 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
1036 list_del(&fp->f_list);
1037 releasef_locked(fp);
1038 leaked++;
1039 }
1040
e4f1d29f 1041 spin_unlock(&vn_file_lock);
1042
1043 if (leaked > 0)
b17edc10 1044 SWARN("Warning %d files leaked\n", leaked);
e4f1d29f 1045
2371321e 1046 kmem_cache_destroy(vn_file_cache);
2fb9b26a 1047 kmem_cache_destroy(vn_cache);
e4f1d29f 1048
b17edc10 1049 SEXIT;
e4f1d29f 1050 return;
af828292 1051} /* vn_fini() */