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