]> granicus.if.org Git - zfs/blob - module/spl/spl-vnode.c
Linux 2.6.39 compat, kern_path_parent()
[zfs] / module / spl / spl-vnode.c
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>.
6  *  UCRL-CODE-235197
7  *
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.
15  *
16  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
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
22  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23  *****************************************************************************
24  *  Solaris Porting Layer (SPL) Vnode Implementation.
25 \*****************************************************************************/
26
27 #include <sys/vnode.h>
28 #include <spl-debug.h>
29
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
32 #endif
33
34 #define SS_DEBUG_SUBSYS SS_VNODE
35
36 vnode_t *rootdir = (vnode_t *)0xabcd1234;
37 EXPORT_SYMBOL(rootdir);
38
39 static spl_kmem_cache_t *vn_cache;
40 static spl_kmem_cache_t *vn_file_cache;
41
42 static DEFINE_SPINLOCK(vn_file_lock);
43 static LIST_HEAD(vn_file_list);
44
45 vtype_t
46 vn_mode_to_vtype(mode_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_mode_to_vtype() */
74 EXPORT_SYMBOL(vn_mode_to_vtype);
75
76 mode_t
77 vn_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() */
102 EXPORT_SYMBOL(vn_vtype_to_mode);
103
104 vnode_t *
105 vn_alloc(int flag)
106 {
107         vnode_t *vp;
108         SENTRY;
109
110         vp = kmem_cache_alloc(vn_cache, flag);
111         if (vp != NULL) {
112                 vp->v_file = NULL;
113                 vp->v_type = 0;
114         }
115
116         SRETURN(vp);
117 } /* vn_alloc() */
118 EXPORT_SYMBOL(vn_alloc);
119
120 void
121 vn_free(vnode_t *vp)
122 {
123         SENTRY;
124         kmem_cache_free(vn_cache, vp);
125         SEXIT;
126 } /* vn_free() */
127 EXPORT_SYMBOL(vn_free);
128
129 int
130 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
131         vnode_t **vpp, int x1, void *x2)
132 {
133         struct file *fp;
134         struct kstat stat;
135         int rc, saved_umask = 0;
136         gfp_t saved_gfp;
137         vnode_t *vp;
138         SENTRY;
139
140         ASSERT(flags & (FWRITE | FREAD));
141         ASSERT(seg == UIO_SYSSPACE);
142         ASSERT(vpp);
143         *vpp = NULL;
144
145         if (!(flags & FCREAT) && (flags & FWRITE))
146                 flags |= FEXCL;
147
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--;
154
155         if (flags & FCREAT)
156                 saved_umask = xchg(&current->fs->umask, 0);
157
158         fp = filp_open(path, flags, mode);
159
160         if (flags & FCREAT)
161                 (void)xchg(&current->fs->umask, saved_umask);
162
163         if (IS_ERR(fp))
164                 SRETURN(-PTR_ERR(fp));
165
166         rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
167         if (rc) {
168                 filp_close(fp, 0);
169                 SRETURN(-rc);
170         }
171
172         vp = vn_alloc(KM_SLEEP);
173         if (!vp) {
174                 filp_close(fp, 0);
175                 SRETURN(ENOMEM);
176         }
177
178         saved_gfp = mapping_gfp_mask(fp->f_mapping);
179         mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
180
181         mutex_enter(&vp->v_lock);
182         vp->v_type = vn_mode_to_vtype(stat.mode);
183         vp->v_file = fp;
184         vp->v_gfp_mask = saved_gfp;
185         *vpp = vp;
186         mutex_exit(&vp->v_lock);
187
188         SRETURN(0);
189 } /* vn_open() */
190 EXPORT_SYMBOL(vn_open);
191
192 int
193 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
194           vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
195 {
196         char *realpath;
197         int len, rc;
198         SENTRY;
199
200         ASSERT(vp == rootdir);
201
202         len = strlen(path) + 2;
203         realpath = kmalloc(len, GFP_KERNEL);
204         if (!realpath)
205                 SRETURN(ENOMEM);
206
207         (void)snprintf(realpath, len, "/%s", path);
208         rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
209         kfree(realpath);
210
211         SRETURN(rc);
212 } /* vn_openat() */
213 EXPORT_SYMBOL(vn_openat);
214
215 int
216 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
217         uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
218 {
219         loff_t offset;
220         mm_segment_t saved_fs;
221         struct file *fp;
222         int rc;
223         SENTRY;
224
225         ASSERT(uio == UIO_WRITE || uio == UIO_READ);
226         ASSERT(vp);
227         ASSERT(vp->v_file);
228         ASSERT(seg == UIO_SYSSPACE);
229         ASSERT((ioflag & ~FAPPEND) == 0);
230         ASSERT(x2 == RLIM64_INFINITY);
231
232         fp = vp->v_file;
233
234         offset = off;
235         if (ioflag & FAPPEND)
236                 offset = fp->f_pos;
237
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)
252                 SRETURN(-rc);
253
254         if (residp) {
255                 *residp = len - rc;
256         } else {
257                 if (rc != len)
258                         SRETURN(EIO);
259         }
260
261         SRETURN(0);
262 } /* vn_rdwr() */
263 EXPORT_SYMBOL(vn_rdwr);
264
265 int
266 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
267 {
268         int rc;
269         SENTRY;
270
271         ASSERT(vp);
272         ASSERT(vp->v_file);
273
274         mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
275         rc = filp_close(vp->v_file, 0);
276         vn_free(vp);
277
278         SRETURN(-rc);
279 } /* vn_close() */
280 EXPORT_SYMBOL(vn_close);
281
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. */
285 int
286 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
287 {
288         return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
289 }
290 EXPORT_SYMBOL(vn_seek);
291
292 static struct dentry *
293 vn_lookup_hash(struct nameidata *nd)
294 {
295         return lookup_one_len((const char *)nd->last.name,
296                               nd->nd_dentry, nd->last.len);
297 } /* lookup_hash() */
298
299 static void
300 vn_path_release(struct nameidata *nd)
301 {
302         dput(nd->nd_dentry);
303         mntput(nd->nd_mnt);
304 }
305
306 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
307 int
308 vn_remove(const char *path, uio_seg_t seg, int flags)
309 {
310         struct dentry *dentry;
311         struct nameidata nd;
312         struct inode *inode = NULL;
313         int rc = 0;
314         SENTRY;
315
316         ASSERT(seg == UIO_SYSSPACE);
317         ASSERT(flags == RMFILE);
318
319         rc = spl_kern_path_parent(path, &nd);
320         if (rc)
321                 SGOTO(exit, rc);
322
323         rc = -EISDIR;
324         if (nd.last_type != LAST_NORM)
325                 SGOTO(exit1, rc);
326
327         spl_inode_lock_nested(nd.nd_dentry->d_inode, I_MUTEX_PARENT);
328         dentry = vn_lookup_hash(&nd);
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])
333                         SGOTO(slashes, rc);
334
335                 inode = dentry->d_inode;
336                 if (inode)
337                         atomic_inc(&inode->i_count);
338 #ifdef HAVE_2ARGS_VFS_UNLINK
339                 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
340 #else
341                 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
342 #endif /* HAVE_2ARGS_VFS_UNLINK */
343 exit2:
344                 dput(dentry);
345         }
346
347         spl_inode_unlock(nd.nd_dentry->d_inode);
348         if (inode)
349                 iput(inode);    /* truncate the inode here */
350 exit1:
351         vn_path_release(&nd);
352 exit:
353         SRETURN(-rc);
354
355 slashes:
356         rc = !dentry->d_inode ? -ENOENT :
357                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
358         SGOTO(exit2, rc);
359 } /* vn_remove() */
360 EXPORT_SYMBOL(vn_remove);
361
362 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
363 int
364 vn_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 nameidata oldnd, newnd;
370         int rc = 0;
371         SENTRY;
372
373         rc = spl_kern_path_parent(oldname, &oldnd);
374         if (rc)
375                 SGOTO(exit, rc);
376
377         rc = spl_kern_path_parent(newname, &newnd);
378         if (rc)
379                 SGOTO(exit1, rc);
380
381         rc = -EXDEV;
382         if (oldnd.nd_mnt != newnd.nd_mnt)
383                 SGOTO(exit2, rc);
384
385         old_dir = oldnd.nd_dentry;
386         rc = -EBUSY;
387         if (oldnd.last_type != LAST_NORM)
388                 SGOTO(exit2, rc);
389
390         new_dir = newnd.nd_dentry;
391         if (newnd.last_type != LAST_NORM)
392                 SGOTO(exit2, rc);
393
394         trap = lock_rename(new_dir, old_dir);
395
396         old_dentry = vn_lookup_hash(&oldnd);
397
398         rc = PTR_ERR(old_dentry);
399         if (IS_ERR(old_dentry))
400                 SGOTO(exit3, rc);
401
402         /* source must exist */
403         rc = -ENOENT;
404         if (!old_dentry->d_inode)
405                 SGOTO(exit4, rc);
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])
411                         SGOTO(exit4, rc);
412                 if (newnd.last.name[newnd.last.len])
413                         SGOTO(exit4, rc);
414         }
415
416         /* source should not be ancestor of target */
417         rc = -EINVAL;
418         if (old_dentry == trap)
419                 SGOTO(exit4, rc);
420
421         new_dentry = vn_lookup_hash(&newnd);
422         rc = PTR_ERR(new_dentry);
423         if (IS_ERR(new_dentry))
424                 SGOTO(exit4, rc);
425
426         /* target should not be an ancestor of source */
427         rc = -ENOTEMPTY;
428         if (new_dentry == trap)
429                 SGOTO(exit5, rc);
430
431 #ifdef HAVE_4ARGS_VFS_RENAME
432         rc = vfs_rename(old_dir->d_inode, old_dentry,
433                         new_dir->d_inode, new_dentry);
434 #else
435         rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
436                         new_dir->d_inode, new_dentry, newnd.nd_mnt);
437 #endif /* HAVE_4ARGS_VFS_RENAME */
438 exit5:
439         dput(new_dentry);
440 exit4:
441         dput(old_dentry);
442 exit3:
443         unlock_rename(new_dir, old_dir);
444 exit2:
445         vn_path_release(&newnd);
446 exit1:
447         vn_path_release(&oldnd);
448 exit:
449         SRETURN(-rc);
450 }
451 EXPORT_SYMBOL(vn_rename);
452
453 int
454 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
455 {
456         struct file *fp;
457         struct kstat stat;
458         int rc;
459         SENTRY;
460
461         ASSERT(vp);
462         ASSERT(vp->v_file);
463         ASSERT(vap);
464
465         fp = vp->v_file;
466
467         rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
468         if (rc)
469                 SRETURN(-rc);
470
471         vap->va_type          = vn_mode_to_vtype(stat.mode);
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;
479         vap->va_blksize       = stat.blksize;
480         vap->va_atime         = stat.atime;
481         vap->va_mtime         = stat.mtime;
482         vap->va_ctime         = stat.ctime;
483         vap->va_rdev          = stat.rdev;
484         vap->va_nblocks       = stat.blocks;
485
486         SRETURN(0);
487 }
488 EXPORT_SYMBOL(vn_getattr);
489
490 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
491 {
492         int datasync = 0;
493         SENTRY;
494
495         ASSERT(vp);
496         ASSERT(vp->v_file);
497
498         if (flags & FDSYNC)
499                 datasync = 1;
500
501         SRETURN(-spl_filp_fsync(vp->v_file, datasync));
502 } /* vn_fsync() */
503 EXPORT_SYMBOL(vn_fsync);
504
505 /* Function must be called while holding the vn_file_lock */
506 static file_t *
507 file_find(int fd)
508 {
509         file_t *fp;
510
511         ASSERT(spin_is_locked(&vn_file_lock));
512
513         list_for_each_entry(fp, &vn_file_list,  f_list) {
514                 if (fd == fp->f_fd) {
515                         ASSERT(atomic_read(&fp->f_ref) != 0);
516                         return fp;
517                 }
518         }
519
520         return NULL;
521 } /* file_find() */
522
523 file_t *
524 vn_getf(int fd)
525 {
526         struct kstat stat;
527         struct file *lfp;
528         file_t *fp;
529         vnode_t *vp;
530         int rc = 0;
531         SENTRY;
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);
540                 SRETURN(fp);
541         }
542
543         spin_unlock(&vn_file_lock);
544
545         /* File was not yet opened create the object and setup */
546         fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
547         if (fp == NULL)
548                 SGOTO(out, rc);
549
550         mutex_enter(&fp->f_lock);
551
552         fp->f_fd = fd;
553         fp->f_offset = 0;
554         atomic_inc(&fp->f_ref);
555
556         lfp = fget(fd);
557         if (lfp == NULL)
558                 SGOTO(out_mutex, rc);
559
560         vp = vn_alloc(KM_SLEEP);
561         if (vp == NULL)
562                 SGOTO(out_fget, rc);
563
564         if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
565                 SGOTO(out_vnode, rc);
566
567         mutex_enter(&vp->v_lock);
568         vp->v_type = vn_mode_to_vtype(stat.mode);
569         vp->v_file = lfp;
570         mutex_exit(&vp->v_lock);
571
572         fp->f_vnode = vp;
573         fp->f_file = lfp;
574
575         /* Put it on the tracking list */
576         spin_lock(&vn_file_lock);
577         list_add(&fp->f_list, &vn_file_list);
578         spin_unlock(&vn_file_lock);
579
580         mutex_exit(&fp->f_lock);
581         SRETURN(fp);
582
583 out_vnode:
584         vn_free(vp);
585 out_fget:
586         fput(lfp);
587 out_mutex:
588         mutex_exit(&fp->f_lock);
589         kmem_cache_free(vn_file_cache, fp);
590 out:
591         SRETURN(NULL);
592 } /* getf() */
593 EXPORT_SYMBOL(getf);
594
595 static void releasef_locked(file_t *fp)
596 {
597         ASSERT(fp->f_file);
598         ASSERT(fp->f_vnode);
599
600         /* Unlinked from list, no refs, safe to free outside mutex */
601         fput(fp->f_file);
602         vn_free(fp->f_vnode);
603
604         kmem_cache_free(vn_file_cache, fp);
605 }
606
607 void
608 vn_releasef(int fd)
609 {
610         file_t *fp;
611         SENTRY;
612
613         spin_lock(&vn_file_lock);
614         fp = file_find(fd);
615         if (fp) {
616                 atomic_dec(&fp->f_ref);
617                 if (atomic_read(&fp->f_ref) > 0) {
618                         spin_unlock(&vn_file_lock);
619                         SEXIT;
620                         return;
621                 }
622
623                 list_del(&fp->f_list);
624                 releasef_locked(fp);
625         }
626         spin_unlock(&vn_file_lock);
627
628         SEXIT;
629         return;
630 } /* releasef() */
631 EXPORT_SYMBOL(releasef);
632
633 #ifndef HAVE_SET_FS_PWD
634 # ifdef HAVE_2ARGS_SET_FS_PWD
635 /* Used from 2.6.25 - 2.6.31+ */
636 void
637 set_fs_pwd(struct fs_struct *fs, struct path *path)
638 {
639         struct path old_pwd;
640
641 #  ifdef HAVE_FS_STRUCT_SPINLOCK
642         spin_lock(&fs->lock);
643         old_pwd = fs->pwd;
644         fs->pwd = *path;
645         path_get(path);
646         spin_unlock(&fs->lock);
647 #  else
648         write_lock(&fs->lock);
649         old_pwd = fs->pwd;
650         fs->pwd = *path;
651         path_get(path);
652         write_unlock(&fs->lock);
653 #  endif /* HAVE_FS_STRUCT_SPINLOCK */
654
655         if (old_pwd.dentry)
656                 path_put(&old_pwd);
657 }
658 # else
659 /* Used from 2.6.11 - 2.6.24 */
660 void
661 set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt, struct dentry *dentry)
662 {
663         struct dentry *old_pwd;
664         struct vfsmount *old_pwdmnt;
665
666         write_lock(&fs->lock);
667         old_pwd = fs->pwd;
668         old_pwdmnt = fs->pwdmnt;
669         fs->pwdmnt = mntget(mnt);
670         fs->pwd = dget(dentry);
671         write_unlock(&fs->lock);
672
673         if (old_pwd) {
674                 dput(old_pwd);
675                 mntput(old_pwdmnt);
676         }
677 }
678 # endif /* HAVE_2ARGS_SET_FS_PWD */
679 #endif /* HAVE_SET_FS_PWD */
680
681 int
682 vn_set_pwd(const char *filename)
683 {
684 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
685         struct path path;
686 #else
687         struct nameidata nd;
688 #endif /* HAVE_2ARGS_SET_FS_PWD */
689         mm_segment_t saved_fs;
690         int rc;
691         SENTRY;
692
693         /*
694          * user_path_dir() and __user_walk() both expect 'filename' to be
695          * a user space address so we must briefly increase the data segment
696          * size to ensure strncpy_from_user() does not fail with -EFAULT.
697          */
698         saved_fs = get_fs();
699         set_fs(get_ds());
700
701 #ifdef HAVE_2ARGS_SET_FS_PWD
702 # ifdef HAVE_USER_PATH_DIR
703         rc = user_path_dir(filename, &path);
704         if (rc)
705                 SGOTO(out, rc);
706
707         rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
708         if (rc)
709                 SGOTO(dput_and_out, rc);
710
711         set_fs_pwd(current->fs, &path);
712
713 dput_and_out:
714         path_put(&path);
715 # else
716         rc = __user_walk(filename,
717                          LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
718         if (rc)
719                 SGOTO(out, rc);
720
721         rc = vfs_permission(&nd, MAY_EXEC);
722         if (rc)
723                 SGOTO(dput_and_out, rc);
724
725         set_fs_pwd(current->fs, &nd.path);
726
727 dput_and_out:
728         path_put(&nd.path);
729 # endif /* HAVE_USER_PATH_DIR */
730 #else
731         rc = __user_walk(filename,
732                          LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
733         if (rc)
734                 SGOTO(out, rc);
735
736         rc = vfs_permission(&nd, MAY_EXEC);
737         if (rc)
738                 SGOTO(dput_and_out, rc);
739
740         set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
741
742 dput_and_out:
743         vn_path_release(&nd);
744 #endif /* HAVE_2ARGS_SET_FS_PWD */
745 out:
746         set_fs(saved_fs);
747
748         SRETURN(-rc);
749 } /* vn_set_pwd() */
750 EXPORT_SYMBOL(vn_set_pwd);
751
752 static int
753 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
754 {
755         struct vnode *vp = buf;
756
757         mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
758
759         return (0);
760 } /* vn_cache_constructor() */
761
762 static void
763 vn_cache_destructor(void *buf, void *cdrarg)
764 {
765         struct vnode *vp = buf;
766
767         mutex_destroy(&vp->v_lock);
768 } /* vn_cache_destructor() */
769
770 static int
771 vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
772 {
773         file_t *fp = buf;
774
775         atomic_set(&fp->f_ref, 0);
776         mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
777         INIT_LIST_HEAD(&fp->f_list);
778
779         return (0);
780 } /* file_cache_constructor() */
781
782 static void
783 vn_file_cache_destructor(void *buf, void *cdrarg)
784 {
785         file_t *fp = buf;
786
787         mutex_destroy(&fp->f_lock);
788 } /* vn_file_cache_destructor() */
789
790 int
791 vn_init(void)
792 {
793         SENTRY;
794         vn_cache = kmem_cache_create("spl_vn_cache",
795                                      sizeof(struct vnode), 64,
796                                      vn_cache_constructor,
797                                      vn_cache_destructor,
798                                      NULL, NULL, NULL, KMC_KMEM);
799
800         vn_file_cache = kmem_cache_create("spl_vn_file_cache",
801                                           sizeof(file_t), 64,
802                                           vn_file_cache_constructor,
803                                           vn_file_cache_destructor,
804                                           NULL, NULL, NULL, KMC_KMEM);
805         SRETURN(0);
806 } /* vn_init() */
807
808 void
809 vn_fini(void)
810 {
811         file_t *fp, *next_fp;
812         int leaked = 0;
813         SENTRY;
814
815         spin_lock(&vn_file_lock);
816
817         list_for_each_entry_safe(fp, next_fp, &vn_file_list,  f_list) {
818                 list_del(&fp->f_list);
819                 releasef_locked(fp);
820                 leaked++;
821         }
822
823         kmem_cache_destroy(vn_file_cache);
824         vn_file_cache = NULL;
825         spin_unlock(&vn_file_lock);
826
827         if (leaked > 0)
828                 SWARN("Warning %d files leaked\n", leaked);
829
830         kmem_cache_destroy(vn_cache);
831
832         SEXIT;
833         return;
834 } /* vn_fini() */