]>
Commit | Line | Data |
---|---|---|
715f6251 | 1 | /* |
2 | * This file is part of the SPL: Solaris Porting Layer. | |
3 | * | |
4 | * Copyright (c) 2008 Lawrence Livermore National Security, LLC. | |
5 | * Produced at Lawrence Livermore National Laboratory | |
6 | * Written by: | |
7 | * Brian Behlendorf <behlendorf1@llnl.gov>, | |
8 | * Herb Wartens <wartens2@llnl.gov>, | |
9 | * Jim Garlick <garlick@llnl.gov> | |
10 | * UCRL-CODE-235197 | |
11 | * | |
12 | * This is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
25 | */ | |
26 | ||
0b3cf046 | 27 | #include <sys/sysmacros.h> |
4b171585 | 28 | #include <sys/vnode.h> |
0b3cf046 | 29 | |
937879f1 | 30 | |
31 | #ifdef DEBUG_SUBSYSTEM | |
32 | #undef DEBUG_SUBSYSTEM | |
33 | #endif | |
34 | ||
35 | #define DEBUG_SUBSYSTEM S_VNODE | |
36 | ||
4b171585 | 37 | void *rootdir = NULL; |
38 | EXPORT_SYMBOL(rootdir); | |
39 | ||
7afde631 | 40 | static spl_kmem_cache_t *vn_cache; |
41 | static spl_kmem_cache_t *vn_file_cache; | |
e4f1d29f | 42 | |
43 | static spinlock_t vn_file_lock = SPIN_LOCK_UNLOCKED; | |
44 | static LIST_HEAD(vn_file_list); | |
af828292 | 45 | |
4b171585 | 46 | static vtype_t |
47 | vn_get_sol_type(umode_t mode) | |
48 | { | |
49 | if (S_ISREG(mode)) | |
50 | return VREG; | |
51 | ||
52 | if (S_ISDIR(mode)) | |
53 | return VDIR; | |
54 | ||
55 | if (S_ISCHR(mode)) | |
56 | return VCHR; | |
57 | ||
58 | if (S_ISBLK(mode)) | |
59 | return VBLK; | |
60 | ||
61 | if (S_ISFIFO(mode)) | |
62 | return VFIFO; | |
63 | ||
64 | if (S_ISLNK(mode)) | |
65 | return VLNK; | |
66 | ||
67 | if (S_ISSOCK(mode)) | |
68 | return VSOCK; | |
69 | ||
70 | if (S_ISCHR(mode)) | |
71 | return VCHR; | |
72 | ||
73 | return VNON; | |
74 | } /* vn_get_sol_type() */ | |
75 | ||
af828292 | 76 | vnode_t * |
77 | vn_alloc(int flag) | |
78 | { | |
79 | vnode_t *vp; | |
937879f1 | 80 | ENTRY; |
af828292 | 81 | |
82 | vp = kmem_cache_alloc(vn_cache, flag); | |
af828292 | 83 | if (vp != NULL) { |
e4f1d29f | 84 | vp->v_file = NULL; |
af828292 | 85 | vp->v_type = 0; |
86 | } | |
87 | ||
937879f1 | 88 | RETURN(vp); |
af828292 | 89 | } /* vn_alloc() */ |
90 | EXPORT_SYMBOL(vn_alloc); | |
91 | ||
92 | void | |
93 | vn_free(vnode_t *vp) | |
94 | { | |
937879f1 | 95 | ENTRY; |
af828292 | 96 | kmem_cache_free(vn_cache, vp); |
937879f1 | 97 | EXIT; |
af828292 | 98 | } /* vn_free() */ |
99 | EXPORT_SYMBOL(vn_free); | |
100 | ||
0b3cf046 | 101 | int |
af828292 | 102 | vn_open(const char *path, uio_seg_t seg, int flags, int mode, |
4b171585 | 103 | vnode_t **vpp, int x1, void *x2) |
0b3cf046 | 104 | { |
4b171585 | 105 | struct file *fp; |
106 | struct kstat stat; | |
57d86234 | 107 | int rc, saved_umask = 0; |
0b3cf046 | 108 | vnode_t *vp; |
937879f1 | 109 | ENTRY; |
0b3cf046 | 110 | |
937879f1 | 111 | ASSERT(flags & (FWRITE | FREAD)); |
112 | ASSERT(seg == UIO_SYSSPACE); | |
113 | ASSERT(vpp); | |
4b171585 | 114 | *vpp = NULL; |
115 | ||
116 | if (!(flags & FCREAT) && (flags & FWRITE)) | |
117 | flags |= FEXCL; | |
118 | ||
728b9dd8 | 119 | /* Note for filp_open() the two low bits must be remapped to mean: |
120 | * 01 - read-only -> 00 read-only | |
121 | * 10 - write-only -> 01 write-only | |
122 | * 11 - read-write -> 10 read-write | |
123 | */ | |
124 | flags--; | |
0b3cf046 | 125 | |
126 | if (flags & FCREAT) | |
4b171585 | 127 | saved_umask = xchg(¤t->fs->umask, 0); |
0b3cf046 | 128 | |
4b171585 | 129 | fp = filp_open(path, flags, mode); |
0b3cf046 | 130 | |
131 | if (flags & FCREAT) | |
4b171585 | 132 | (void)xchg(¤t->fs->umask, saved_umask); |
0b3cf046 | 133 | |
4b171585 | 134 | if (IS_ERR(fp)) |
937879f1 | 135 | RETURN(-PTR_ERR(fp)); |
0b3cf046 | 136 | |
4b171585 | 137 | rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat); |
138 | if (rc) { | |
139 | filp_close(fp, 0); | |
937879f1 | 140 | RETURN(-rc); |
0b3cf046 | 141 | } |
142 | ||
af828292 | 143 | vp = vn_alloc(KM_SLEEP); |
4b171585 | 144 | if (!vp) { |
145 | filp_close(fp, 0); | |
937879f1 | 146 | RETURN(ENOMEM); |
4b171585 | 147 | } |
0b3cf046 | 148 | |
e4f1d29f | 149 | mutex_enter(&vp->v_lock); |
4b171585 | 150 | vp->v_type = vn_get_sol_type(stat.mode); |
e4f1d29f | 151 | vp->v_file = fp; |
4b171585 | 152 | *vpp = vp; |
e4f1d29f | 153 | mutex_exit(&vp->v_lock); |
0b3cf046 | 154 | |
937879f1 | 155 | RETURN(0); |
4b171585 | 156 | } /* vn_open() */ |
157 | EXPORT_SYMBOL(vn_open); | |
0b3cf046 | 158 | |
0b3cf046 | 159 | int |
af828292 | 160 | vn_openat(const char *path, uio_seg_t seg, int flags, int mode, |
4b171585 | 161 | vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd) |
0b3cf046 | 162 | { |
4b171585 | 163 | char *realpath; |
12018327 | 164 | int len, rc; |
937879f1 | 165 | ENTRY; |
0b3cf046 | 166 | |
937879f1 | 167 | ASSERT(vp == rootdir); |
0b3cf046 | 168 | |
12018327 | 169 | len = strlen(path) + 2; |
170 | realpath = kmalloc(len, GFP_KERNEL); | |
4b171585 | 171 | if (!realpath) |
937879f1 | 172 | RETURN(ENOMEM); |
0b3cf046 | 173 | |
12018327 | 174 | (void)snprintf(realpath, len, "/%s", path); |
4b171585 | 175 | rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2); |
4b171585 | 176 | kfree(realpath); |
177 | ||
937879f1 | 178 | RETURN(rc); |
4b171585 | 179 | } /* vn_openat() */ |
180 | EXPORT_SYMBOL(vn_openat); | |
0b3cf046 | 181 | |
0b3cf046 | 182 | int |
4b171585 | 183 | vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off, |
af828292 | 184 | uio_seg_t seg, int x1, rlim64_t x2, void *x3, ssize_t *residp) |
0b3cf046 | 185 | { |
4b171585 | 186 | loff_t offset; |
187 | mm_segment_t saved_fs; | |
188 | struct file *fp; | |
189 | int rc; | |
937879f1 | 190 | ENTRY; |
4b171585 | 191 | |
937879f1 | 192 | ASSERT(uio == UIO_WRITE || uio == UIO_READ); |
193 | ASSERT(vp); | |
194 | ASSERT(vp->v_file); | |
195 | ASSERT(seg == UIO_SYSSPACE); | |
196 | ASSERT(x1 == 0); | |
197 | ASSERT(x2 == RLIM64_INFINITY); | |
4b171585 | 198 | |
199 | offset = off; | |
e4f1d29f | 200 | fp = vp->v_file; |
4b171585 | 201 | |
202 | /* Writable user data segment must be briefly increased for this | |
203 | * process so we can use the user space read call paths to write | |
204 | * in to memory allocated by the kernel. */ | |
205 | saved_fs = get_fs(); | |
206 | set_fs(get_ds()); | |
207 | ||
208 | if (uio & UIO_WRITE) | |
209 | rc = vfs_write(fp, addr, len, &offset); | |
210 | else | |
211 | rc = vfs_read(fp, addr, len, &offset); | |
212 | ||
213 | set_fs(saved_fs); | |
214 | ||
215 | if (rc < 0) | |
937879f1 | 216 | RETURN(-rc); |
0b3cf046 | 217 | |
4b171585 | 218 | if (residp) { |
219 | *residp = len - rc; | |
0b3cf046 | 220 | } else { |
4b171585 | 221 | if (rc != len) |
937879f1 | 222 | RETURN(EIO); |
0b3cf046 | 223 | } |
224 | ||
937879f1 | 225 | RETURN(0); |
4b171585 | 226 | } /* vn_rdwr() */ |
227 | EXPORT_SYMBOL(vn_rdwr); | |
228 | ||
229 | int | |
2f5d55aa | 230 | vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4) |
4b171585 | 231 | { |
232 | int rc; | |
937879f1 | 233 | ENTRY; |
4b171585 | 234 | |
937879f1 | 235 | ASSERT(vp); |
236 | ASSERT(vp->v_file); | |
4b171585 | 237 | |
97735c39 BB |
238 | rc = filp_close(vp->v_file, 0); |
239 | vn_free(vp); | |
4b171585 | 240 | |
937879f1 | 241 | RETURN(-rc); |
4b171585 | 242 | } /* vn_close() */ |
243 | EXPORT_SYMBOL(vn_close); | |
244 | ||
97735c39 BB |
245 | /* vn_seek() does not actually seek it only performs bounds checking on the |
246 | * proposed seek. We perform minimal checking and allow vn_rdwr() to catch | |
247 | * anything more serious. */ | |
248 | int | |
249 | vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) | |
250 | { | |
251 | return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); | |
252 | } | |
253 | EXPORT_SYMBOL(vn_seek); | |
254 | ||
255 | static struct dentry * | |
256 | vn_lookup_hash(struct nameidata *nd) | |
4b171585 | 257 | { |
57d86234 | 258 | return lookup_one_len(nd->last.name, nd->nd_dentry, nd->last.len); |
4b171585 | 259 | } /* lookup_hash() */ |
260 | ||
97735c39 BB |
261 | static void |
262 | vn_path_release(struct nameidata *nd) | |
57d86234 | 263 | { |
264 | dput(nd->nd_dentry); | |
265 | mntput(nd->nd_mnt); | |
266 | } | |
267 | ||
4b171585 | 268 | /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */ |
269 | int | |
af828292 | 270 | vn_remove(const char *path, uio_seg_t seg, int flags) |
4b171585 | 271 | { |
272 | struct dentry *dentry; | |
273 | struct nameidata nd; | |
274 | struct inode *inode = NULL; | |
275 | int rc = 0; | |
3d061e9d | 276 | ENTRY; |
4b171585 | 277 | |
3d061e9d | 278 | ASSERT(seg == UIO_SYSSPACE); |
279 | ASSERT(flags == RMFILE); | |
2f5d55aa | 280 | |
4b171585 | 281 | rc = path_lookup(path, LOOKUP_PARENT, &nd); |
282 | if (rc) | |
937879f1 | 283 | GOTO(exit, rc); |
4b171585 | 284 | |
285 | rc = -EISDIR; | |
286 | if (nd.last_type != LAST_NORM) | |
937879f1 | 287 | GOTO(exit1, rc); |
4b171585 | 288 | |
3d061e9d | 289 | #ifdef HAVE_INODE_I_MUTEX |
57d86234 | 290 | mutex_lock_nested(&nd.nd_dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
3d061e9d | 291 | #else |
292 | down(&nd.nd_dentry->d_inode->i_sem); | |
a093c6a4 | 293 | #endif /* HAVE_INODE_I_MUTEX */ |
57d86234 | 294 | dentry = vn_lookup_hash(&nd); |
4b171585 | 295 | rc = PTR_ERR(dentry); |
296 | if (!IS_ERR(dentry)) { | |
297 | /* Why not before? Because we want correct rc value */ | |
298 | if (nd.last.name[nd.last.len]) | |
937879f1 | 299 | GOTO(slashes, rc); |
300 | ||
4b171585 | 301 | inode = dentry->d_inode; |
302 | if (inode) | |
303 | atomic_inc(&inode->i_count); | |
a093c6a4 | 304 | #ifdef HAVE_2ARGS_VFS_UNLINK |
57d86234 | 305 | rc = vfs_unlink(nd.nd_dentry->d_inode, dentry); |
a093c6a4 | 306 | #else |
21411161 | 307 | rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt); |
a093c6a4 | 308 | #endif /* HAVE_2ARGS_VFS_UNLINK */ |
4b171585 | 309 | exit2: |
310 | dput(dentry); | |
311 | } | |
3d061e9d | 312 | #ifdef HAVE_INODE_I_MUTEX |
57d86234 | 313 | mutex_unlock(&nd.nd_dentry->d_inode->i_mutex); |
3d061e9d | 314 | #else |
315 | up(&nd.nd_dentry->d_inode->i_sem); | |
a093c6a4 | 316 | #endif /* HAVE_INODE_I_MUTEX */ |
4b171585 | 317 | if (inode) |
318 | iput(inode); /* truncate the inode here */ | |
319 | exit1: | |
57d86234 | 320 | vn_path_release(&nd); |
4b171585 | 321 | exit: |
937879f1 | 322 | RETURN(-rc); |
4b171585 | 323 | |
324 | slashes: | |
325 | rc = !dentry->d_inode ? -ENOENT : | |
326 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; | |
937879f1 | 327 | GOTO(exit2, rc); |
4b171585 | 328 | } /* vn_remove() */ |
329 | EXPORT_SYMBOL(vn_remove); | |
330 | ||
331 | /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */ | |
332 | int | |
333 | vn_rename(const char *oldname, const char *newname, int x1) | |
334 | { | |
a093c6a4 BB |
335 | struct dentry *old_dir, *new_dir; |
336 | struct dentry *old_dentry, *new_dentry; | |
337 | struct dentry *trap; | |
4b171585 | 338 | struct nameidata oldnd, newnd; |
339 | int rc = 0; | |
937879f1 | 340 | ENTRY; |
4b171585 | 341 | |
342 | rc = path_lookup(oldname, LOOKUP_PARENT, &oldnd); | |
343 | if (rc) | |
937879f1 | 344 | GOTO(exit, rc); |
4b171585 | 345 | |
346 | rc = path_lookup(newname, LOOKUP_PARENT, &newnd); | |
347 | if (rc) | |
937879f1 | 348 | GOTO(exit1, rc); |
4b171585 | 349 | |
350 | rc = -EXDEV; | |
57d86234 | 351 | if (oldnd.nd_mnt != newnd.nd_mnt) |
937879f1 | 352 | GOTO(exit2, rc); |
4b171585 | 353 | |
57d86234 | 354 | old_dir = oldnd.nd_dentry; |
4b171585 | 355 | rc = -EBUSY; |
356 | if (oldnd.last_type != LAST_NORM) | |
937879f1 | 357 | GOTO(exit2, rc); |
4b171585 | 358 | |
57d86234 | 359 | new_dir = newnd.nd_dentry; |
4b171585 | 360 | if (newnd.last_type != LAST_NORM) |
937879f1 | 361 | GOTO(exit2, rc); |
4b171585 | 362 | |
363 | trap = lock_rename(new_dir, old_dir); | |
364 | ||
57d86234 | 365 | old_dentry = vn_lookup_hash(&oldnd); |
4b171585 | 366 | |
367 | rc = PTR_ERR(old_dentry); | |
368 | if (IS_ERR(old_dentry)) | |
937879f1 | 369 | GOTO(exit3, rc); |
4b171585 | 370 | |
371 | /* source must exist */ | |
372 | rc = -ENOENT; | |
373 | if (!old_dentry->d_inode) | |
937879f1 | 374 | GOTO(exit4, rc); |
4b171585 | 375 | |
376 | /* unless the source is a directory trailing slashes give -ENOTDIR */ | |
377 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { | |
378 | rc = -ENOTDIR; | |
379 | if (oldnd.last.name[oldnd.last.len]) | |
937879f1 | 380 | GOTO(exit4, rc); |
4b171585 | 381 | if (newnd.last.name[newnd.last.len]) |
937879f1 | 382 | GOTO(exit4, rc); |
4b171585 | 383 | } |
384 | ||
385 | /* source should not be ancestor of target */ | |
386 | rc = -EINVAL; | |
387 | if (old_dentry == trap) | |
937879f1 | 388 | GOTO(exit4, rc); |
4b171585 | 389 | |
57d86234 | 390 | new_dentry = vn_lookup_hash(&newnd); |
4b171585 | 391 | rc = PTR_ERR(new_dentry); |
392 | if (IS_ERR(new_dentry)) | |
937879f1 | 393 | GOTO(exit4, rc); |
4b171585 | 394 | |
395 | /* target should not be an ancestor of source */ | |
396 | rc = -ENOTEMPTY; | |
397 | if (new_dentry == trap) | |
937879f1 | 398 | GOTO(exit5, rc); |
4b171585 | 399 | |
a093c6a4 | 400 | #ifdef HAVE_4ARGS_VFS_RENAME |
4b171585 | 401 | rc = vfs_rename(old_dir->d_inode, old_dentry, |
402 | new_dir->d_inode, new_dentry); | |
a093c6a4 | 403 | #else |
21411161 BB |
404 | rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt, |
405 | new_dir->d_inode, new_dentry, newnd.nd_mnt); | |
a093c6a4 | 406 | #endif /* HAVE_4ARGS_VFS_RENAME */ |
4b171585 | 407 | exit5: |
408 | dput(new_dentry); | |
409 | exit4: | |
410 | dput(old_dentry); | |
411 | exit3: | |
412 | unlock_rename(new_dir, old_dir); | |
413 | exit2: | |
57d86234 | 414 | vn_path_release(&newnd); |
4b171585 | 415 | exit1: |
57d86234 | 416 | vn_path_release(&oldnd); |
4b171585 | 417 | exit: |
937879f1 | 418 | RETURN(-rc); |
0b3cf046 | 419 | } |
4b171585 | 420 | EXPORT_SYMBOL(vn_rename); |
0b3cf046 | 421 | |
4b171585 | 422 | int |
36e6f861 | 423 | vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4) |
0b3cf046 | 424 | { |
4b171585 | 425 | struct file *fp; |
426 | struct kstat stat; | |
427 | int rc; | |
937879f1 | 428 | ENTRY; |
4b171585 | 429 | |
937879f1 | 430 | ASSERT(vp); |
431 | ASSERT(vp->v_file); | |
432 | ASSERT(vap); | |
4b171585 | 433 | |
e4f1d29f | 434 | fp = vp->v_file; |
4b171585 | 435 | |
436 | rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat); | |
437 | if (rc) | |
937879f1 | 438 | RETURN(-rc); |
4b171585 | 439 | |
440 | vap->va_type = vn_get_sol_type(stat.mode); | |
441 | vap->va_mode = stat.mode; | |
442 | vap->va_uid = stat.uid; | |
443 | vap->va_gid = stat.gid; | |
444 | vap->va_fsid = 0; | |
445 | vap->va_nodeid = stat.ino; | |
446 | vap->va_nlink = stat.nlink; | |
447 | vap->va_size = stat.size; | |
448 | vap->va_blocksize = stat.blksize; | |
449 | vap->va_atime.tv_sec = stat.atime.tv_sec; | |
450 | vap->va_atime.tv_usec = stat.atime.tv_nsec / NSEC_PER_USEC; | |
451 | vap->va_mtime.tv_sec = stat.mtime.tv_sec; | |
452 | vap->va_mtime.tv_usec = stat.mtime.tv_nsec / NSEC_PER_USEC; | |
453 | vap->va_ctime.tv_sec = stat.ctime.tv_sec; | |
454 | vap->va_ctime.tv_usec = stat.ctime.tv_nsec / NSEC_PER_USEC; | |
455 | vap->va_rdev = stat.rdev; | |
456 | vap->va_blocks = stat.blocks; | |
457 | ||
937879f1 | 458 | RETURN(0); |
0b3cf046 | 459 | } |
4b171585 | 460 | EXPORT_SYMBOL(vn_getattr); |
461 | ||
2f5d55aa | 462 | int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4) |
4b171585 | 463 | { |
36e6f861 | 464 | int datasync = 0; |
937879f1 | 465 | ENTRY; |
36e6f861 | 466 | |
937879f1 | 467 | ASSERT(vp); |
468 | ASSERT(vp->v_file); | |
4b171585 | 469 | |
36e6f861 | 470 | if (flags & FDSYNC) |
471 | datasync = 1; | |
472 | ||
937879f1 | 473 | RETURN(-file_fsync(vp->v_file, vp->v_file->f_dentry, datasync)); |
4b171585 | 474 | } /* vn_fsync() */ |
475 | EXPORT_SYMBOL(vn_fsync); | |
af828292 | 476 | |
e4f1d29f | 477 | /* Function must be called while holding the vn_file_lock */ |
478 | static file_t * | |
479 | file_find(int fd) | |
480 | { | |
481 | file_t *fp; | |
482 | ||
937879f1 | 483 | ASSERT(spin_is_locked(&vn_file_lock)); |
e4f1d29f | 484 | |
485 | list_for_each_entry(fp, &vn_file_list, f_list) { | |
486 | if (fd == fp->f_fd) { | |
937879f1 | 487 | ASSERT(atomic_read(&fp->f_ref) != 0); |
e4f1d29f | 488 | return fp; |
489 | } | |
490 | } | |
491 | ||
492 | return NULL; | |
493 | } /* file_find() */ | |
494 | ||
495 | file_t * | |
496 | vn_getf(int fd) | |
497 | { | |
498 | struct kstat stat; | |
499 | struct file *lfp; | |
500 | file_t *fp; | |
501 | vnode_t *vp; | |
937879f1 | 502 | int rc = 0; |
503 | ENTRY; | |
e4f1d29f | 504 | |
505 | /* Already open just take an extra reference */ | |
506 | spin_lock(&vn_file_lock); | |
507 | ||
508 | fp = file_find(fd); | |
509 | if (fp) { | |
510 | atomic_inc(&fp->f_ref); | |
511 | spin_unlock(&vn_file_lock); | |
937879f1 | 512 | RETURN(fp); |
e4f1d29f | 513 | } |
514 | ||
515 | spin_unlock(&vn_file_lock); | |
516 | ||
517 | /* File was not yet opened create the object and setup */ | |
4afaaefa | 518 | fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP); |
e4f1d29f | 519 | if (fp == NULL) |
937879f1 | 520 | GOTO(out, rc); |
e4f1d29f | 521 | |
522 | mutex_enter(&fp->f_lock); | |
523 | ||
524 | fp->f_fd = fd; | |
525 | fp->f_offset = 0; | |
526 | atomic_inc(&fp->f_ref); | |
527 | ||
528 | lfp = fget(fd); | |
529 | if (lfp == NULL) | |
937879f1 | 530 | GOTO(out_mutex, rc); |
e4f1d29f | 531 | |
532 | vp = vn_alloc(KM_SLEEP); | |
533 | if (vp == NULL) | |
937879f1 | 534 | GOTO(out_fget, rc); |
e4f1d29f | 535 | |
536 | if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat)) | |
937879f1 | 537 | GOTO(out_vnode, rc); |
e4f1d29f | 538 | |
539 | mutex_enter(&vp->v_lock); | |
540 | vp->v_type = vn_get_sol_type(stat.mode); | |
541 | vp->v_file = lfp; | |
542 | mutex_exit(&vp->v_lock); | |
543 | ||
544 | fp->f_vnode = vp; | |
545 | fp->f_file = lfp; | |
546 | ||
547 | /* Put it on the tracking list */ | |
548 | spin_lock(&vn_file_lock); | |
549 | list_add(&fp->f_list, &vn_file_list); | |
550 | spin_unlock(&vn_file_lock); | |
551 | ||
552 | mutex_exit(&fp->f_lock); | |
937879f1 | 553 | RETURN(fp); |
e4f1d29f | 554 | |
555 | out_vnode: | |
e4f1d29f | 556 | vn_free(vp); |
557 | out_fget: | |
e4f1d29f | 558 | fput(lfp); |
559 | out_mutex: | |
e4f1d29f | 560 | mutex_exit(&fp->f_lock); |
561 | kmem_cache_free(vn_file_cache, fp); | |
562 | out: | |
937879f1 | 563 | RETURN(NULL); |
e4f1d29f | 564 | } /* getf() */ |
565 | EXPORT_SYMBOL(getf); | |
566 | ||
567 | static void releasef_locked(file_t *fp) | |
568 | { | |
937879f1 | 569 | ASSERT(fp->f_file); |
570 | ASSERT(fp->f_vnode); | |
e4f1d29f | 571 | |
572 | /* Unlinked from list, no refs, safe to free outside mutex */ | |
573 | fput(fp->f_file); | |
574 | vn_free(fp->f_vnode); | |
575 | ||
576 | kmem_cache_free(vn_file_cache, fp); | |
577 | } | |
578 | ||
579 | void | |
580 | vn_releasef(int fd) | |
581 | { | |
582 | file_t *fp; | |
937879f1 | 583 | ENTRY; |
e4f1d29f | 584 | |
585 | spin_lock(&vn_file_lock); | |
586 | fp = file_find(fd); | |
587 | if (fp) { | |
588 | atomic_dec(&fp->f_ref); | |
589 | if (atomic_read(&fp->f_ref) > 0) { | |
590 | spin_unlock(&vn_file_lock); | |
937879f1 | 591 | EXIT; |
e4f1d29f | 592 | return; |
593 | } | |
594 | ||
595 | list_del(&fp->f_list); | |
596 | releasef_locked(fp); | |
597 | } | |
598 | spin_unlock(&vn_file_lock); | |
599 | ||
937879f1 | 600 | EXIT; |
e4f1d29f | 601 | return; |
602 | } /* releasef() */ | |
603 | EXPORT_SYMBOL(releasef); | |
604 | ||
af828292 | 605 | static int |
606 | vn_cache_constructor(void *buf, void *cdrarg, int kmflags) | |
607 | { | |
608 | struct vnode *vp = buf; | |
609 | ||
610 | mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL); | |
611 | ||
612 | return (0); | |
613 | } /* vn_cache_constructor() */ | |
614 | ||
615 | static void | |
616 | vn_cache_destructor(void *buf, void *cdrarg) | |
617 | { | |
618 | struct vnode *vp = buf; | |
619 | ||
620 | mutex_destroy(&vp->v_lock); | |
621 | } /* vn_cache_destructor() */ | |
622 | ||
e4f1d29f | 623 | static int |
624 | vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags) | |
625 | { | |
626 | file_t *fp = buf; | |
627 | ||
628 | atomic_set(&fp->f_ref, 0); | |
629 | mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL); | |
4e62fd41 | 630 | INIT_LIST_HEAD(&fp->f_list); |
e4f1d29f | 631 | |
632 | return (0); | |
633 | } /* file_cache_constructor() */ | |
634 | ||
635 | static void | |
636 | vn_file_cache_destructor(void *buf, void *cdrarg) | |
637 | { | |
638 | file_t *fp = buf; | |
639 | ||
640 | mutex_destroy(&fp->f_lock); | |
641 | } /* vn_file_cache_destructor() */ | |
642 | ||
af828292 | 643 | int |
644 | vn_init(void) | |
645 | { | |
937879f1 | 646 | ENTRY; |
57d86234 | 647 | vn_cache = kmem_cache_create("spl_vn_cache", |
648 | sizeof(struct vnode), 64, | |
5d86345d | 649 | vn_cache_constructor, |
650 | vn_cache_destructor, | |
af828292 | 651 | NULL, NULL, NULL, 0); |
e4f1d29f | 652 | |
653 | vn_file_cache = kmem_cache_create("spl_vn_file_cache", | |
654 | sizeof(file_t), 64, | |
655 | vn_file_cache_constructor, | |
656 | vn_file_cache_destructor, | |
657 | NULL, NULL, NULL, 0); | |
937879f1 | 658 | RETURN(0); |
af828292 | 659 | } /* vn_init() */ |
660 | ||
661 | void | |
662 | vn_fini(void) | |
663 | { | |
e4f1d29f | 664 | file_t *fp, *next_fp; |
2fb9b26a | 665 | int leaked = 0; |
937879f1 | 666 | ENTRY; |
e4f1d29f | 667 | |
668 | spin_lock(&vn_file_lock); | |
669 | ||
670 | list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) { | |
671 | list_del(&fp->f_list); | |
672 | releasef_locked(fp); | |
673 | leaked++; | |
674 | } | |
675 | ||
2fb9b26a | 676 | kmem_cache_destroy(vn_file_cache); |
e4f1d29f | 677 | vn_file_cache = NULL; |
678 | spin_unlock(&vn_file_lock); | |
679 | ||
680 | if (leaked > 0) | |
937879f1 | 681 | CWARN("Warning %d files leaked\n", leaked); |
e4f1d29f | 682 | |
2fb9b26a | 683 | kmem_cache_destroy(vn_cache); |
e4f1d29f | 684 | |
937879f1 | 685 | EXIT; |
e4f1d29f | 686 | return; |
af828292 | 687 | } /* vn_fini() */ |