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