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