]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-vnode.c
Add 3 missing typedefs.
[mirror_spl-debian.git] / module / spl / spl-vnode.c
CommitLineData
715f6251 1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
0b3cf046 27#include <sys/sysmacros.h>
4b171585 28#include <sys/vnode.h>
0b3cf046 29
937879f1 30
31#ifdef DEBUG_SUBSYSTEM
32#undef DEBUG_SUBSYSTEM
33#endif
34
35#define DEBUG_SUBSYSTEM S_VNODE
36
51a727e9 37vnode_t *rootdir = (vnode_t *)0xabcd1234;
4b171585 38EXPORT_SYMBOL(rootdir);
39
7afde631 40static spl_kmem_cache_t *vn_cache;
41static spl_kmem_cache_t *vn_file_cache;
e4f1d29f 42
43static spinlock_t vn_file_lock = SPIN_LOCK_UNLOCKED;
44static LIST_HEAD(vn_file_list);
af828292 45
4b171585 46static vtype_t
47vn_get_sol_type(umode_t mode)
48{
49 if (S_ISREG(mode))
50 return VREG;
51
52 if (S_ISDIR(mode))
53 return VDIR;
54
55 if (S_ISCHR(mode))
56 return VCHR;
57
58 if (S_ISBLK(mode))
59 return VBLK;
60
61 if (S_ISFIFO(mode))
62 return VFIFO;
63
64 if (S_ISLNK(mode))
65 return VLNK;
66
67 if (S_ISSOCK(mode))
68 return VSOCK;
69
70 if (S_ISCHR(mode))
71 return VCHR;
72
73 return VNON;
74} /* vn_get_sol_type() */
75
af828292 76vnode_t *
77vn_alloc(int flag)
78{
79 vnode_t *vp;
937879f1 80 ENTRY;
af828292 81
82 vp = kmem_cache_alloc(vn_cache, flag);
af828292 83 if (vp != NULL) {
e4f1d29f 84 vp->v_file = NULL;
af828292 85 vp->v_type = 0;
86 }
87
937879f1 88 RETURN(vp);
af828292 89} /* vn_alloc() */
90EXPORT_SYMBOL(vn_alloc);
91
92void
93vn_free(vnode_t *vp)
94{
937879f1 95 ENTRY;
af828292 96 kmem_cache_free(vn_cache, vp);
937879f1 97 EXIT;
af828292 98} /* vn_free() */
99EXPORT_SYMBOL(vn_free);
100
0b3cf046 101int
af828292 102vn_open(const char *path, uio_seg_t seg, int flags, int mode,
4b171585 103 vnode_t **vpp, int x1, void *x2)
0b3cf046 104{
f7e8739c
RC
105 struct file *fp;
106 struct kstat stat;
107 int rc, saved_umask = 0;
0b3cf046 108 vnode_t *vp;
937879f1 109 ENTRY;
0b3cf046 110
937879f1 111 ASSERT(flags & (FWRITE | FREAD));
112 ASSERT(seg == UIO_SYSSPACE);
113 ASSERT(vpp);
4b171585 114 *vpp = NULL;
115
116 if (!(flags & FCREAT) && (flags & FWRITE))
117 flags |= FEXCL;
118
728b9dd8 119 /* Note for filp_open() the two low bits must be remapped to mean:
120 * 01 - read-only -> 00 read-only
121 * 10 - write-only -> 01 write-only
122 * 11 - read-write -> 10 read-write
123 */
124 flags--;
0b3cf046 125
126 if (flags & FCREAT)
4b171585 127 saved_umask = xchg(&current->fs->umask, 0);
0b3cf046 128
f7e8739c 129 fp = filp_open(path, flags, mode);
0b3cf046 130
131 if (flags & FCREAT)
4b171585 132 (void)xchg(&current->fs->umask, saved_umask);
0b3cf046 133
f7e8739c 134 if (IS_ERR(fp))
937879f1 135 RETURN(-PTR_ERR(fp));
0b3cf046 136
f7e8739c 137 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
4b171585 138 if (rc) {
139 filp_close(fp, 0);
937879f1 140 RETURN(-rc);
0b3cf046 141 }
142
af828292 143 vp = vn_alloc(KM_SLEEP);
4b171585 144 if (!vp) {
145 filp_close(fp, 0);
937879f1 146 RETURN(ENOMEM);
4b171585 147 }
0b3cf046 148
e4f1d29f 149 mutex_enter(&vp->v_lock);
4b171585 150 vp->v_type = vn_get_sol_type(stat.mode);
e4f1d29f 151 vp->v_file = fp;
4b171585 152 *vpp = vp;
e4f1d29f 153 mutex_exit(&vp->v_lock);
0b3cf046 154
937879f1 155 RETURN(0);
4b171585 156} /* vn_open() */
157EXPORT_SYMBOL(vn_open);
0b3cf046 158
0b3cf046 159int
af828292 160vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
4b171585 161 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
0b3cf046 162{
4b171585 163 char *realpath;
12018327 164 int len, rc;
937879f1 165 ENTRY;
0b3cf046 166
937879f1 167 ASSERT(vp == rootdir);
0b3cf046 168
12018327 169 len = strlen(path) + 2;
170 realpath = kmalloc(len, GFP_KERNEL);
4b171585 171 if (!realpath)
937879f1 172 RETURN(ENOMEM);
0b3cf046 173
12018327 174 (void)snprintf(realpath, len, "/%s", path);
4b171585 175 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
4b171585 176 kfree(realpath);
177
937879f1 178 RETURN(rc);
4b171585 179} /* vn_openat() */
180EXPORT_SYMBOL(vn_openat);
0b3cf046 181
0b3cf046 182int
4b171585 183vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
663e02a1 184 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
0b3cf046 185{
4b171585 186 loff_t offset;
187 mm_segment_t saved_fs;
188 struct file *fp;
189 int rc;
937879f1 190 ENTRY;
4b171585 191
937879f1 192 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
193 ASSERT(vp);
194 ASSERT(vp->v_file);
195 ASSERT(seg == UIO_SYSSPACE);
663e02a1 196 ASSERT((ioflag & ~FAPPEND) == 0);
937879f1 197 ASSERT(x2 == RLIM64_INFINITY);
4b171585 198
e4f1d29f 199 fp = vp->v_file;
4b171585 200
663e02a1
RC
201 offset = off;
202 if (ioflag & FAPPEND)
203 offset = fp->f_pos;
204
4b171585 205 /* Writable user data segment must be briefly increased for this
206 * process so we can use the user space read call paths to write
207 * in to memory allocated by the kernel. */
208 saved_fs = get_fs();
209 set_fs(get_ds());
210
211 if (uio & UIO_WRITE)
212 rc = vfs_write(fp, addr, len, &offset);
213 else
214 rc = vfs_read(fp, addr, len, &offset);
215
216 set_fs(saved_fs);
217
218 if (rc < 0)
937879f1 219 RETURN(-rc);
0b3cf046 220
4b171585 221 if (residp) {
222 *residp = len - rc;
0b3cf046 223 } else {
4b171585 224 if (rc != len)
937879f1 225 RETURN(EIO);
0b3cf046 226 }
227
937879f1 228 RETURN(0);
4b171585 229} /* vn_rdwr() */
230EXPORT_SYMBOL(vn_rdwr);
231
232int
2f5d55aa 233vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
4b171585 234{
235 int rc;
937879f1 236 ENTRY;
4b171585 237
937879f1 238 ASSERT(vp);
239 ASSERT(vp->v_file);
4b171585 240
97735c39
BB
241 rc = filp_close(vp->v_file, 0);
242 vn_free(vp);
4b171585 243
937879f1 244 RETURN(-rc);
4b171585 245} /* vn_close() */
246EXPORT_SYMBOL(vn_close);
247
97735c39
BB
248/* vn_seek() does not actually seek it only performs bounds checking on the
249 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
250 * anything more serious. */
251int
252vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
253{
254 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
255}
256EXPORT_SYMBOL(vn_seek);
257
258static struct dentry *
259vn_lookup_hash(struct nameidata *nd)
4b171585 260{
57d86234 261 return lookup_one_len(nd->last.name, 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;
3d061e9d 279 ENTRY;
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)
937879f1 286 GOTO(exit, rc);
4b171585 287
288 rc = -EISDIR;
289 if (nd.last_type != LAST_NORM)
937879f1 290 GOTO(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])
937879f1 302 GOTO(slashes, rc);
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:
937879f1 325 RETURN(-rc);
4b171585 326
327slashes:
328 rc = !dentry->d_inode ? -ENOENT :
329 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
937879f1 330 GOTO(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;
937879f1 343 ENTRY;
4b171585 344
345 rc = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
346 if (rc)
937879f1 347 GOTO(exit, rc);
4b171585 348
349 rc = path_lookup(newname, LOOKUP_PARENT, &newnd);
350 if (rc)
937879f1 351 GOTO(exit1, rc);
4b171585 352
353 rc = -EXDEV;
57d86234 354 if (oldnd.nd_mnt != newnd.nd_mnt)
937879f1 355 GOTO(exit2, rc);
4b171585 356
57d86234 357 old_dir = oldnd.nd_dentry;
4b171585 358 rc = -EBUSY;
359 if (oldnd.last_type != LAST_NORM)
937879f1 360 GOTO(exit2, rc);
4b171585 361
57d86234 362 new_dir = newnd.nd_dentry;
4b171585 363 if (newnd.last_type != LAST_NORM)
937879f1 364 GOTO(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))
937879f1 372 GOTO(exit3, rc);
4b171585 373
374 /* source must exist */
375 rc = -ENOENT;
376 if (!old_dentry->d_inode)
937879f1 377 GOTO(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])
937879f1 383 GOTO(exit4, rc);
4b171585 384 if (newnd.last.name[newnd.last.len])
937879f1 385 GOTO(exit4, rc);
4b171585 386 }
387
388 /* source should not be ancestor of target */
389 rc = -EINVAL;
390 if (old_dentry == trap)
937879f1 391 GOTO(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))
937879f1 396 GOTO(exit4, rc);
4b171585 397
398 /* target should not be an ancestor of source */
399 rc = -ENOTEMPTY;
400 if (new_dentry == trap)
937879f1 401 GOTO(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:
937879f1 421 RETURN(-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;
937879f1 431 ENTRY;
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)
937879f1 441 RETURN(-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
937879f1 461 RETURN(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;
937879f1 468 ENTRY;
36e6f861 469
937879f1 470 ASSERT(vp);
471 ASSERT(vp->v_file);
4b171585 472
36e6f861 473 if (flags & FDSYNC)
474 datasync = 1;
475
937879f1 476 RETURN(-file_fsync(vp->v_file, vp->v_file->f_dentry, 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;
506 ENTRY;
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);
937879f1 515 RETURN(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)
937879f1 523 GOTO(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)
937879f1 533 GOTO(out_mutex, rc);
e4f1d29f 534
535 vp = vn_alloc(KM_SLEEP);
536 if (vp == NULL)
937879f1 537 GOTO(out_fget, rc);
e4f1d29f 538
539 if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
937879f1 540 GOTO(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);
937879f1 556 RETURN(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:
937879f1 566 RETURN(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;
937879f1 586 ENTRY;
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);
937879f1 594 EXIT;
e4f1d29f 595 return;
596 }
597
598 list_del(&fp->f_list);
599 releasef_locked(fp);
600 }
601 spin_unlock(&vn_file_lock);
602
937879f1 603 EXIT;
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{
651#ifdef HAVE_2ARGS_SET_FS_PWD
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
BB
657 int rc;
658 ENTRY;
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
51a727e9
BB
669 rc = user_path_dir(filename, &path);
670 if (rc)
671 GOTO(out, rc);
672
673 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
674 if (rc)
675 GOTO(dput_and_out, rc);
676
677 set_fs_pwd(current->fs, &path);
678
679dput_and_out:
680 path_put(&path);
681#else
51a727e9
BB
682 rc = __user_walk(filename,
683 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
684 if (rc)
685 GOTO(out, rc);
686
687 rc = vfs_permission(&nd, MAY_EXEC);
688 if (rc)
689 GOTO(dput_and_out, rc);
690
691 set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
692
693dput_and_out:
694 vn_path_release(&nd);
695#endif /* HAVE_2ARGS_SET_FS_PWD */
696out:
82a358d9
BB
697 set_fs(saved_fs);
698
51a727e9
BB
699 RETURN(-rc);
700} /* vn_set_pwd() */
701EXPORT_SYMBOL(vn_set_pwd);
702
af828292 703static int
704vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
705{
706 struct vnode *vp = buf;
707
708 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
709
710 return (0);
711} /* vn_cache_constructor() */
712
713static void
714vn_cache_destructor(void *buf, void *cdrarg)
715{
716 struct vnode *vp = buf;
717
718 mutex_destroy(&vp->v_lock);
719} /* vn_cache_destructor() */
720
e4f1d29f 721static int
722vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
723{
724 file_t *fp = buf;
725
726 atomic_set(&fp->f_ref, 0);
727 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
4e62fd41 728 INIT_LIST_HEAD(&fp->f_list);
e4f1d29f 729
730 return (0);
731} /* file_cache_constructor() */
732
733static void
734vn_file_cache_destructor(void *buf, void *cdrarg)
735{
736 file_t *fp = buf;
737
738 mutex_destroy(&fp->f_lock);
739} /* vn_file_cache_destructor() */
740
af828292 741int
742vn_init(void)
743{
937879f1 744 ENTRY;
57d86234 745 vn_cache = kmem_cache_create("spl_vn_cache",
746 sizeof(struct vnode), 64,
5d86345d 747 vn_cache_constructor,
748 vn_cache_destructor,
af828292 749 NULL, NULL, NULL, 0);
e4f1d29f 750
751 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
752 sizeof(file_t), 64,
753 vn_file_cache_constructor,
754 vn_file_cache_destructor,
755 NULL, NULL, NULL, 0);
937879f1 756 RETURN(0);
af828292 757} /* vn_init() */
758
759void
760vn_fini(void)
761{
e4f1d29f 762 file_t *fp, *next_fp;
2fb9b26a 763 int leaked = 0;
937879f1 764 ENTRY;
e4f1d29f 765
766 spin_lock(&vn_file_lock);
767
768 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
769 list_del(&fp->f_list);
770 releasef_locked(fp);
771 leaked++;
772 }
773
2fb9b26a 774 kmem_cache_destroy(vn_file_cache);
e4f1d29f 775 vn_file_cache = NULL;
776 spin_unlock(&vn_file_lock);
777
778 if (leaked > 0)
937879f1 779 CWARN("Warning %d files leaked\n", leaked);
e4f1d29f 780
2fb9b26a 781 kmem_cache_destroy(vn_cache);
e4f1d29f 782
937879f1 783 EXIT;
e4f1d29f 784 return;
af828292 785} /* vn_fini() */