]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-vnode.c
Linux 3.1 compat, kern_path_parent()
[mirror_spl.git] / 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 #ifdef HAVE_KERN_PATH_PARENT_HEADER
46 #ifndef HAVE_KERN_PATH_PARENT_SYMBOL
47 kern_path_parent_t kern_path_parent_fn = SYMBOL_POISON;
48 EXPORT_SYMBOL(kern_path_parent_fn);
49 #endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
50 #endif /* HAVE_KERN_PATH_PARENT_HEADER */
51
52 vtype_t
53 vn_mode_to_vtype(mode_t mode)
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;
80 } /* vn_mode_to_vtype() */
81 EXPORT_SYMBOL(vn_mode_to_vtype);
82
83 mode_t
84 vn_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() */
109 EXPORT_SYMBOL(vn_vtype_to_mode);
110
111 vnode_t *
112 vn_alloc(int flag)
113 {
114 vnode_t *vp;
115 SENTRY;
116
117 vp = kmem_cache_alloc(vn_cache, flag);
118 if (vp != NULL) {
119 vp->v_file = NULL;
120 vp->v_type = 0;
121 }
122
123 SRETURN(vp);
124 } /* vn_alloc() */
125 EXPORT_SYMBOL(vn_alloc);
126
127 void
128 vn_free(vnode_t *vp)
129 {
130 SENTRY;
131 kmem_cache_free(vn_cache, vp);
132 SEXIT;
133 } /* vn_free() */
134 EXPORT_SYMBOL(vn_free);
135
136 int
137 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
138 vnode_t **vpp, int x1, void *x2)
139 {
140 struct file *fp;
141 struct kstat stat;
142 int rc, saved_umask = 0;
143 gfp_t saved_gfp;
144 vnode_t *vp;
145 SENTRY;
146
147 ASSERT(flags & (FWRITE | FREAD));
148 ASSERT(seg == UIO_SYSSPACE);
149 ASSERT(vpp);
150 *vpp = NULL;
151
152 if (!(flags & FCREAT) && (flags & FWRITE))
153 flags |= FEXCL;
154
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--;
161
162 if (flags & FCREAT)
163 saved_umask = xchg(&current->fs->umask, 0);
164
165 fp = filp_open(path, flags, mode);
166
167 if (flags & FCREAT)
168 (void)xchg(&current->fs->umask, saved_umask);
169
170 if (IS_ERR(fp))
171 SRETURN(-PTR_ERR(fp));
172
173 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
174 if (rc) {
175 filp_close(fp, 0);
176 SRETURN(-rc);
177 }
178
179 vp = vn_alloc(KM_SLEEP);
180 if (!vp) {
181 filp_close(fp, 0);
182 SRETURN(ENOMEM);
183 }
184
185 saved_gfp = mapping_gfp_mask(fp->f_mapping);
186 mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
187
188 mutex_enter(&vp->v_lock);
189 vp->v_type = vn_mode_to_vtype(stat.mode);
190 vp->v_file = fp;
191 vp->v_gfp_mask = saved_gfp;
192 *vpp = vp;
193 mutex_exit(&vp->v_lock);
194
195 SRETURN(0);
196 } /* vn_open() */
197 EXPORT_SYMBOL(vn_open);
198
199 int
200 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
201 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
202 {
203 char *realpath;
204 int len, rc;
205 SENTRY;
206
207 ASSERT(vp == rootdir);
208
209 len = strlen(path) + 2;
210 realpath = kmalloc(len, GFP_KERNEL);
211 if (!realpath)
212 SRETURN(ENOMEM);
213
214 (void)snprintf(realpath, len, "/%s", path);
215 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
216 kfree(realpath);
217
218 SRETURN(rc);
219 } /* vn_openat() */
220 EXPORT_SYMBOL(vn_openat);
221
222 int
223 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
224 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
225 {
226 loff_t offset;
227 mm_segment_t saved_fs;
228 struct file *fp;
229 int rc;
230 SENTRY;
231
232 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
233 ASSERT(vp);
234 ASSERT(vp->v_file);
235 ASSERT(seg == UIO_SYSSPACE);
236 ASSERT((ioflag & ~FAPPEND) == 0);
237 ASSERT(x2 == RLIM64_INFINITY);
238
239 fp = vp->v_file;
240
241 offset = off;
242 if (ioflag & FAPPEND)
243 offset = fp->f_pos;
244
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);
257 fp->f_pos = offset;
258
259 if (rc < 0)
260 SRETURN(-rc);
261
262 if (residp) {
263 *residp = len - rc;
264 } else {
265 if (rc != len)
266 SRETURN(EIO);
267 }
268
269 SRETURN(0);
270 } /* vn_rdwr() */
271 EXPORT_SYMBOL(vn_rdwr);
272
273 int
274 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
275 {
276 int rc;
277 SENTRY;
278
279 ASSERT(vp);
280 ASSERT(vp->v_file);
281
282 mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
283 rc = filp_close(vp->v_file, 0);
284 vn_free(vp);
285
286 SRETURN(-rc);
287 } /* vn_close() */
288 EXPORT_SYMBOL(vn_close);
289
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. */
293 int
294 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
295 {
296 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
297 }
298 EXPORT_SYMBOL(vn_seek);
299
300 static struct dentry *
301 vn_lookup_hash(struct nameidata *nd)
302 {
303 return lookup_one_len((const char *)nd->last.name,
304 nd->nd_dentry, nd->last.len);
305 } /* lookup_hash() */
306
307 static void
308 vn_path_release(struct nameidata *nd)
309 {
310 dput(nd->nd_dentry);
311 mntput(nd->nd_mnt);
312 }
313
314 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
315 int
316 vn_remove(const char *path, uio_seg_t seg, int flags)
317 {
318 struct dentry *dentry;
319 struct nameidata nd;
320 struct inode *inode = NULL;
321 int rc = 0;
322 SENTRY;
323
324 ASSERT(seg == UIO_SYSSPACE);
325 ASSERT(flags == RMFILE);
326
327 rc = spl_kern_path_parent(path, &nd);
328 if (rc)
329 SGOTO(exit, rc);
330
331 rc = -EISDIR;
332 if (nd.last_type != LAST_NORM)
333 SGOTO(exit1, rc);
334
335 spl_inode_lock_nested(nd.nd_dentry->d_inode, I_MUTEX_PARENT);
336 dentry = vn_lookup_hash(&nd);
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])
341 SGOTO(slashes, rc);
342
343 inode = dentry->d_inode;
344 if (inode)
345 atomic_inc(&inode->i_count);
346 #ifdef HAVE_2ARGS_VFS_UNLINK
347 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
348 #else
349 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
350 #endif /* HAVE_2ARGS_VFS_UNLINK */
351 exit2:
352 dput(dentry);
353 }
354
355 spl_inode_unlock(nd.nd_dentry->d_inode);
356 if (inode)
357 iput(inode); /* truncate the inode here */
358 exit1:
359 vn_path_release(&nd);
360 exit:
361 SRETURN(-rc);
362
363 slashes:
364 rc = !dentry->d_inode ? -ENOENT :
365 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
366 SGOTO(exit2, rc);
367 } /* vn_remove() */
368 EXPORT_SYMBOL(vn_remove);
369
370 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
371 int
372 vn_rename(const char *oldname, const char *newname, int x1)
373 {
374 struct dentry *old_dir, *new_dir;
375 struct dentry *old_dentry, *new_dentry;
376 struct dentry *trap;
377 struct nameidata oldnd, newnd;
378 int rc = 0;
379 SENTRY;
380
381 rc = spl_kern_path_parent(oldname, &oldnd);
382 if (rc)
383 SGOTO(exit, rc);
384
385 rc = spl_kern_path_parent(newname, &newnd);
386 if (rc)
387 SGOTO(exit1, rc);
388
389 rc = -EXDEV;
390 if (oldnd.nd_mnt != newnd.nd_mnt)
391 SGOTO(exit2, rc);
392
393 old_dir = oldnd.nd_dentry;
394 rc = -EBUSY;
395 if (oldnd.last_type != LAST_NORM)
396 SGOTO(exit2, rc);
397
398 new_dir = newnd.nd_dentry;
399 if (newnd.last_type != LAST_NORM)
400 SGOTO(exit2, rc);
401
402 trap = lock_rename(new_dir, old_dir);
403
404 old_dentry = vn_lookup_hash(&oldnd);
405
406 rc = PTR_ERR(old_dentry);
407 if (IS_ERR(old_dentry))
408 SGOTO(exit3, rc);
409
410 /* source must exist */
411 rc = -ENOENT;
412 if (!old_dentry->d_inode)
413 SGOTO(exit4, rc);
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])
419 SGOTO(exit4, rc);
420 if (newnd.last.name[newnd.last.len])
421 SGOTO(exit4, rc);
422 }
423
424 /* source should not be ancestor of target */
425 rc = -EINVAL;
426 if (old_dentry == trap)
427 SGOTO(exit4, rc);
428
429 new_dentry = vn_lookup_hash(&newnd);
430 rc = PTR_ERR(new_dentry);
431 if (IS_ERR(new_dentry))
432 SGOTO(exit4, rc);
433
434 /* target should not be an ancestor of source */
435 rc = -ENOTEMPTY;
436 if (new_dentry == trap)
437 SGOTO(exit5, rc);
438
439 #ifdef HAVE_4ARGS_VFS_RENAME
440 rc = vfs_rename(old_dir->d_inode, old_dentry,
441 new_dir->d_inode, new_dentry);
442 #else
443 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
444 new_dir->d_inode, new_dentry, newnd.nd_mnt);
445 #endif /* HAVE_4ARGS_VFS_RENAME */
446 exit5:
447 dput(new_dentry);
448 exit4:
449 dput(old_dentry);
450 exit3:
451 unlock_rename(new_dir, old_dir);
452 exit2:
453 vn_path_release(&newnd);
454 exit1:
455 vn_path_release(&oldnd);
456 exit:
457 SRETURN(-rc);
458 }
459 EXPORT_SYMBOL(vn_rename);
460
461 int
462 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
463 {
464 struct file *fp;
465 struct kstat stat;
466 int rc;
467 SENTRY;
468
469 ASSERT(vp);
470 ASSERT(vp->v_file);
471 ASSERT(vap);
472
473 fp = vp->v_file;
474
475 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
476 if (rc)
477 SRETURN(-rc);
478
479 vap->va_type = vn_mode_to_vtype(stat.mode);
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;
487 vap->va_blksize = stat.blksize;
488 vap->va_atime = stat.atime;
489 vap->va_mtime = stat.mtime;
490 vap->va_ctime = stat.ctime;
491 vap->va_rdev = stat.rdev;
492 vap->va_nblocks = stat.blocks;
493
494 SRETURN(0);
495 }
496 EXPORT_SYMBOL(vn_getattr);
497
498 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
499 {
500 int datasync = 0;
501 SENTRY;
502
503 ASSERT(vp);
504 ASSERT(vp->v_file);
505
506 if (flags & FDSYNC)
507 datasync = 1;
508
509 SRETURN(-spl_filp_fsync(vp->v_file, datasync));
510 } /* vn_fsync() */
511 EXPORT_SYMBOL(vn_fsync);
512
513 /* Function must be called while holding the vn_file_lock */
514 static file_t *
515 file_find(int fd)
516 {
517 file_t *fp;
518
519 ASSERT(spin_is_locked(&vn_file_lock));
520
521 list_for_each_entry(fp, &vn_file_list, f_list) {
522 if (fd == fp->f_fd && fp->f_task == current) {
523 ASSERT(atomic_read(&fp->f_ref) != 0);
524 return fp;
525 }
526 }
527
528 return NULL;
529 } /* file_find() */
530
531 file_t *
532 vn_getf(int fd)
533 {
534 struct kstat stat;
535 struct file *lfp;
536 file_t *fp;
537 vnode_t *vp;
538 int rc = 0;
539 SENTRY;
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);
548 SRETURN(fp);
549 }
550
551 spin_unlock(&vn_file_lock);
552
553 /* File was not yet opened create the object and setup */
554 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
555 if (fp == NULL)
556 SGOTO(out, rc);
557
558 mutex_enter(&fp->f_lock);
559
560 fp->f_fd = fd;
561 fp->f_task = current;
562 fp->f_offset = 0;
563 atomic_inc(&fp->f_ref);
564
565 lfp = fget(fd);
566 if (lfp == NULL)
567 SGOTO(out_mutex, rc);
568
569 vp = vn_alloc(KM_SLEEP);
570 if (vp == NULL)
571 SGOTO(out_fget, rc);
572
573 if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
574 SGOTO(out_vnode, rc);
575
576 mutex_enter(&vp->v_lock);
577 vp->v_type = vn_mode_to_vtype(stat.mode);
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);
590 SRETURN(fp);
591
592 out_vnode:
593 vn_free(vp);
594 out_fget:
595 fput(lfp);
596 out_mutex:
597 mutex_exit(&fp->f_lock);
598 kmem_cache_free(vn_file_cache, fp);
599 out:
600 SRETURN(NULL);
601 } /* getf() */
602 EXPORT_SYMBOL(getf);
603
604 static void releasef_locked(file_t *fp)
605 {
606 ASSERT(fp->f_file);
607 ASSERT(fp->f_vnode);
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
616 void
617 vn_releasef(int fd)
618 {
619 file_t *fp;
620 SENTRY;
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);
628 SEXIT;
629 return;
630 }
631
632 list_del(&fp->f_list);
633 releasef_locked(fp);
634 }
635 spin_unlock(&vn_file_lock);
636
637 SEXIT;
638 return;
639 } /* releasef() */
640 EXPORT_SYMBOL(releasef);
641
642 #ifndef HAVE_SET_FS_PWD
643 # ifdef HAVE_2ARGS_SET_FS_PWD
644 /* Used from 2.6.25 - 2.6.31+ */
645 void
646 set_fs_pwd(struct fs_struct *fs, struct path *path)
647 {
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);
666 }
667 # else
668 /* Used from 2.6.11 - 2.6.24 */
669 void
670 set_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
690 int
691 vn_set_pwd(const char *filename)
692 {
693 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
694 struct path path;
695 #else
696 struct nameidata nd;
697 #endif /* HAVE_2ARGS_SET_FS_PWD */
698 mm_segment_t saved_fs;
699 int rc;
700 SENTRY;
701
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
711 # ifdef HAVE_USER_PATH_DIR
712 rc = user_path_dir(filename, &path);
713 if (rc)
714 SGOTO(out, rc);
715
716 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
717 if (rc)
718 SGOTO(dput_and_out, rc);
719
720 set_fs_pwd(current->fs, &path);
721
722 dput_and_out:
723 path_put(&path);
724 # else
725 rc = __user_walk(filename,
726 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
727 if (rc)
728 SGOTO(out, rc);
729
730 rc = vfs_permission(&nd, MAY_EXEC);
731 if (rc)
732 SGOTO(dput_and_out, rc);
733
734 set_fs_pwd(current->fs, &nd.path);
735
736 dput_and_out:
737 path_put(&nd.path);
738 # endif /* HAVE_USER_PATH_DIR */
739 #else
740 rc = __user_walk(filename,
741 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
742 if (rc)
743 SGOTO(out, rc);
744
745 rc = vfs_permission(&nd, MAY_EXEC);
746 if (rc)
747 SGOTO(dput_and_out, rc);
748
749 set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
750
751 dput_and_out:
752 vn_path_release(&nd);
753 #endif /* HAVE_2ARGS_SET_FS_PWD */
754 out:
755 set_fs(saved_fs);
756
757 SRETURN(-rc);
758 } /* vn_set_pwd() */
759 EXPORT_SYMBOL(vn_set_pwd);
760
761 static int
762 vn_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
771 static void
772 vn_cache_destructor(void *buf, void *cdrarg)
773 {
774 struct vnode *vp = buf;
775
776 mutex_destroy(&vp->v_lock);
777 } /* vn_cache_destructor() */
778
779 static int
780 vn_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);
786 INIT_LIST_HEAD(&fp->f_list);
787
788 return (0);
789 } /* file_cache_constructor() */
790
791 static void
792 vn_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
799 int 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
815 int
816 spl_vn_init(void)
817 {
818 SENTRY;
819 vn_cache = kmem_cache_create("spl_vn_cache",
820 sizeof(struct vnode), 64,
821 vn_cache_constructor,
822 vn_cache_destructor,
823 NULL, NULL, NULL, KMC_KMEM);
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,
829 NULL, NULL, NULL, KMC_KMEM);
830 SRETURN(0);
831 } /* vn_init() */
832
833 void
834 spl_vn_fini(void)
835 {
836 file_t *fp, *next_fp;
837 int leaked = 0;
838 SENTRY;
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
848 kmem_cache_destroy(vn_file_cache);
849 vn_file_cache = NULL;
850 spin_unlock(&vn_file_lock);
851
852 if (leaked > 0)
853 SWARN("Warning %d files leaked\n", leaked);
854
855 kmem_cache_destroy(vn_cache);
856
857 SEXIT;
858 return;
859 } /* vn_fini() */