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