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