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