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