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