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