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