1 | /** @file
|
---|
2 | *
|
---|
3 | * vboxvfs -- VirtualBox Guest Additions for Linux:
|
---|
4 | * Regular file inode and file operations
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Limitations: only COW memory mapping is supported
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "vfsmod.h"
|
---|
28 |
|
---|
29 | #define CHUNK_SIZE 4096
|
---|
30 |
|
---|
31 | /* fops */
|
---|
32 | static int
|
---|
33 | sf_reg_read_aux (const char *caller, struct sf_glob_info *sf_g,
|
---|
34 | struct sf_reg_info *sf_r, void *buf, uint32_t *nread,
|
---|
35 | uint64_t pos)
|
---|
36 | {
|
---|
37 | int rc = vboxCallRead (&client_handle, &sf_g->map, sf_r->handle,
|
---|
38 | pos, nread, buf, false /* already locked? */);
|
---|
39 | if (VBOX_FAILURE (rc)) {
|
---|
40 | LogFunc(("vboxCallRead failed. caller=%s, rc=%Vrc\n",
|
---|
41 | caller, rc));
|
---|
42 | return -EPROTO;
|
---|
43 | }
|
---|
44 | return 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static ssize_t
|
---|
48 | sf_reg_read (struct file *file, char *buf, size_t size, loff_t *off)
|
---|
49 | {
|
---|
50 | int err;
|
---|
51 | void *tmp;
|
---|
52 | size_t left = size;
|
---|
53 | ssize_t total_bytes_read = 0;
|
---|
54 | struct inode *inode = file->f_dentry->d_inode;
|
---|
55 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
56 | struct sf_reg_info *sf_r = file->private_data;
|
---|
57 | loff_t pos = *off;
|
---|
58 |
|
---|
59 | TRACE ();
|
---|
60 | if (!S_ISREG (inode->i_mode)) {
|
---|
61 | LogFunc(("read from non regular file %d\n", inode->i_mode));
|
---|
62 | return -EINVAL;
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (!size) {
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | tmp = kmalloc (CHUNK_SIZE, GFP_KERNEL);
|
---|
70 | if (!tmp) {
|
---|
71 | LogRelFunc(("could not allocate bounce buffer memory %d bytes\n", CHUNK_SIZE));
|
---|
72 | return -ENOMEM;
|
---|
73 | }
|
---|
74 |
|
---|
75 | while (left) {
|
---|
76 | uint32_t to_read, nread;
|
---|
77 |
|
---|
78 | to_read = CHUNK_SIZE;
|
---|
79 | if (to_read > left) {
|
---|
80 | to_read = (uint32_t) left;
|
---|
81 | }
|
---|
82 | nread = to_read;
|
---|
83 |
|
---|
84 | err = sf_reg_read_aux (__func__, sf_g, sf_r, tmp, &nread, pos);
|
---|
85 | if (err) {
|
---|
86 | goto fail;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (copy_to_user (buf, tmp, nread)) {
|
---|
90 | err = -EFAULT;
|
---|
91 | goto fail;
|
---|
92 | }
|
---|
93 |
|
---|
94 | pos += nread;
|
---|
95 | left -= nread;
|
---|
96 | buf += nread;
|
---|
97 | total_bytes_read += nread;
|
---|
98 | if (nread != to_read) {
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | *off += total_bytes_read;
|
---|
104 | kfree (tmp);
|
---|
105 | return total_bytes_read;
|
---|
106 |
|
---|
107 | fail:
|
---|
108 | kfree (tmp);
|
---|
109 | return err;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static ssize_t
|
---|
113 | sf_reg_write (struct file *file, const char *buf, size_t size, loff_t *off)
|
---|
114 | {
|
---|
115 | int err;
|
---|
116 | void *tmp;
|
---|
117 | size_t left = size;
|
---|
118 | ssize_t total_bytes_written = 0;
|
---|
119 | struct inode *inode = file->f_dentry->d_inode;
|
---|
120 | struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
|
---|
121 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
122 | struct sf_reg_info *sf_r = file->private_data;
|
---|
123 | loff_t pos = *off;
|
---|
124 |
|
---|
125 | TRACE ();
|
---|
126 | BUG_ON (!sf_i);
|
---|
127 | BUG_ON (!sf_g);
|
---|
128 | BUG_ON (!sf_r);
|
---|
129 |
|
---|
130 | if (!S_ISREG (inode->i_mode)) {
|
---|
131 | LogFunc(("write to non regular file %d\n", inode->i_mode));
|
---|
132 | return -EINVAL;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!size) {
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 | tmp = kmalloc (CHUNK_SIZE, GFP_KERNEL);
|
---|
140 | if (!tmp) {
|
---|
141 | LogRelFunc(("could not allocate bounce buffer memory %d\n", CHUNK_SIZE));
|
---|
142 | return -ENOMEM;
|
---|
143 | }
|
---|
144 |
|
---|
145 | while (left) {
|
---|
146 | int rc;
|
---|
147 | uint32_t to_write, nwritten;
|
---|
148 |
|
---|
149 | to_write = CHUNK_SIZE;
|
---|
150 | if (to_write > left) {
|
---|
151 | to_write = (uint32_t) left;
|
---|
152 | }
|
---|
153 | nwritten = to_write;
|
---|
154 |
|
---|
155 | if (copy_from_user (tmp, buf, to_write)) {
|
---|
156 | err = -EFAULT;
|
---|
157 | goto fail;
|
---|
158 | }
|
---|
159 |
|
---|
160 | rc = vboxCallWrite (&client_handle, &sf_g->map, sf_r->handle,
|
---|
161 | pos, &nwritten, tmp, false /* already locked? */);
|
---|
162 | if (VBOX_FAILURE (rc)) {
|
---|
163 | err = -EPROTO;
|
---|
164 | LogFunc(("vboxCallWrite(%s) failed rc=%Vrc\n",
|
---|
165 | sf_i->path->String.utf8, rc));
|
---|
166 | goto fail;
|
---|
167 | }
|
---|
168 |
|
---|
169 | pos += nwritten;
|
---|
170 | left -= nwritten;
|
---|
171 | buf += nwritten;
|
---|
172 | total_bytes_written += nwritten;
|
---|
173 | if (nwritten != to_write) {
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | #if 1 /* XXX: which way is correct? */
|
---|
179 | *off += total_bytes_written;
|
---|
180 | #else
|
---|
181 | file->f_pos += total_bytes_written;
|
---|
182 | #endif
|
---|
183 | sf_i->force_restat = 1;
|
---|
184 | kfree (tmp);
|
---|
185 | return total_bytes_written;
|
---|
186 |
|
---|
187 | fail:
|
---|
188 | kfree (tmp);
|
---|
189 | return err;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int
|
---|
193 | sf_reg_open (struct inode *inode, struct file *file)
|
---|
194 | {
|
---|
195 | int rc, rc_linux = 0;
|
---|
196 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
197 | struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
|
---|
198 | struct sf_reg_info *sf_r;
|
---|
199 | SHFLCREATEPARMS params;
|
---|
200 |
|
---|
201 | TRACE ();
|
---|
202 | BUG_ON (!sf_g);
|
---|
203 | BUG_ON (!sf_i);
|
---|
204 |
|
---|
205 | sf_r = kmalloc (sizeof (*sf_r), GFP_KERNEL);
|
---|
206 | if (!sf_r) {
|
---|
207 | LogRelFunc(("could not allocate reg info\n"));
|
---|
208 | return -ENOMEM;
|
---|
209 | }
|
---|
210 |
|
---|
211 | LogFunc(("open %s\n", sf_i->path->String.utf8));
|
---|
212 |
|
---|
213 | params.CreateFlags = 0;
|
---|
214 | params.Info.cbObject = 0;
|
---|
215 | /* We check this afterwards to find out if the call succeeded
|
---|
216 | or failed, as the API does not seem to cleanly distinguish
|
---|
217 | error and informational messages. */
|
---|
218 | params.Handle = 0;
|
---|
219 |
|
---|
220 | if (file->f_flags & O_CREAT) {
|
---|
221 | LogFunc(("O_CREAT set\n"));
|
---|
222 | params.CreateFlags |= SHFL_CF_ACT_CREATE_IF_NEW;
|
---|
223 | /* We ignore O_EXCL, as the Linux kernel seems to call create
|
---|
224 | beforehand itself, so O_EXCL should always fail. */
|
---|
225 | if (file->f_flags & O_TRUNC) {
|
---|
226 | LogFunc(("O_TRUNC set\n"));
|
---|
227 | params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
|
---|
228 | | SHFL_CF_ACCESS_WRITE);
|
---|
229 | }
|
---|
230 | else {
|
---|
231 | params.CreateFlags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | else {
|
---|
235 | params.CreateFlags |= SHFL_CF_ACT_FAIL_IF_NEW;
|
---|
236 | if (file->f_flags & O_TRUNC) {
|
---|
237 | LogFunc(("O_TRUNC set\n"));
|
---|
238 | params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
|
---|
239 | | SHFL_CF_ACCESS_WRITE);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (!(params.CreateFlags & SHFL_CF_ACCESS_READWRITE)) {
|
---|
244 | switch (file->f_flags & O_ACCMODE) {
|
---|
245 | case O_RDONLY:
|
---|
246 | params.CreateFlags |= SHFL_CF_ACCESS_READ;
|
---|
247 | break;
|
---|
248 |
|
---|
249 | case O_WRONLY:
|
---|
250 | params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
|
---|
251 | break;
|
---|
252 |
|
---|
253 | case O_RDWR:
|
---|
254 | params.CreateFlags |= SHFL_CF_ACCESS_READWRITE;
|
---|
255 | break;
|
---|
256 |
|
---|
257 | default:
|
---|
258 | BUG ();
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | LogFunc(("sf_reg_open: calling vboxCallCreate, file %s, flags=%d, %#x\n",
|
---|
263 | sf_i->path->String.utf8 , file->f_flags, params.CreateFlags));
|
---|
264 | rc = vboxCallCreate (&client_handle, &sf_g->map, sf_i->path, ¶ms);
|
---|
265 |
|
---|
266 | if (VBOX_FAILURE (rc)) {
|
---|
267 | LogFunc(("vboxCallCreate failed flags=%d,%#x rc=%Vrc\n",
|
---|
268 | file->f_flags, params.CreateFlags, rc));
|
---|
269 | kfree (sf_r);
|
---|
270 | return -RTErrConvertToErrno(rc);
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (SHFL_HANDLE_NIL == params.Handle) {
|
---|
274 | switch (params.Result) {
|
---|
275 | case SHFL_PATH_NOT_FOUND:
|
---|
276 | case SHFL_FILE_NOT_FOUND:
|
---|
277 | rc_linux = -ENOENT;
|
---|
278 | break;
|
---|
279 | case SHFL_FILE_EXISTS:
|
---|
280 | rc_linux = -EEXIST;
|
---|
281 | break;
|
---|
282 | default:
|
---|
283 | break;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | sf_i->force_restat = 1;
|
---|
288 | sf_r->handle = params.Handle;
|
---|
289 | file->private_data = sf_r;
|
---|
290 | return rc_linux;
|
---|
291 | }
|
---|
292 |
|
---|
293 | static int
|
---|
294 | sf_reg_release (struct inode *inode, struct file *file)
|
---|
295 | {
|
---|
296 | int rc;
|
---|
297 | struct sf_reg_info *sf_r;
|
---|
298 | struct sf_glob_info *sf_g;
|
---|
299 |
|
---|
300 | TRACE ();
|
---|
301 | sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
302 | sf_r = file->private_data;
|
---|
303 |
|
---|
304 | BUG_ON (!sf_g);
|
---|
305 | BUG_ON (!sf_r);
|
---|
306 |
|
---|
307 | rc = vboxCallClose (&client_handle, &sf_g->map, sf_r->handle);
|
---|
308 | if (VBOX_FAILURE (rc)) {
|
---|
309 | LogFunc(("vboxCallClose failed rc=%Vrc\n", rc));
|
---|
310 | }
|
---|
311 |
|
---|
312 | kfree (sf_r);
|
---|
313 | file->private_data = NULL;
|
---|
314 | return 0;
|
---|
315 | }
|
---|
316 |
|
---|
317 | static struct page *
|
---|
318 | #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
|
---|
319 | sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int unused)
|
---|
320 | #define SET_TYPE(t)
|
---|
321 | #else
|
---|
322 | sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int *type)
|
---|
323 | #define SET_TYPE(t) *type = (t)
|
---|
324 | #endif
|
---|
325 | {
|
---|
326 | struct page *page;
|
---|
327 | char *buf;
|
---|
328 | loff_t off;
|
---|
329 | uint32_t nread = PAGE_SIZE;
|
---|
330 | int err;
|
---|
331 | struct file *file = vma->vm_file;
|
---|
332 | struct inode *inode = file->f_dentry->d_inode;
|
---|
333 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
334 | struct sf_reg_info *sf_r = file->private_data;
|
---|
335 |
|
---|
336 | TRACE ();
|
---|
337 | if (vaddr > vma->vm_end) {
|
---|
338 | SET_TYPE (VM_FAULT_SIGBUS);
|
---|
339 | return NOPAGE_SIGBUS;
|
---|
340 | }
|
---|
341 |
|
---|
342 | page = alloc_page (GFP_HIGHUSER);
|
---|
343 | if (!page) {
|
---|
344 | LogRelFunc(("failed to allocate page\n"));
|
---|
345 | SET_TYPE (VM_FAULT_OOM);
|
---|
346 | return NOPAGE_OOM;
|
---|
347 | }
|
---|
348 |
|
---|
349 | buf = kmap (page);
|
---|
350 | off = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
|
---|
351 | err = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
|
---|
352 | if (err) {
|
---|
353 | kunmap (page);
|
---|
354 | put_page (page);
|
---|
355 | SET_TYPE (VM_FAULT_SIGBUS);
|
---|
356 | return NOPAGE_SIGBUS;
|
---|
357 | }
|
---|
358 |
|
---|
359 | BUG_ON (nread > PAGE_SIZE);
|
---|
360 | if (!nread) {
|
---|
361 | #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
|
---|
362 | clear_user_page (page_address (page), vaddr);
|
---|
363 | #else
|
---|
364 | clear_user_page (page_address (page), vaddr, page);
|
---|
365 | #endif
|
---|
366 | }
|
---|
367 | else {
|
---|
368 | memset (buf + nread, 0, PAGE_SIZE - nread);
|
---|
369 | }
|
---|
370 |
|
---|
371 | flush_dcache_page (page);
|
---|
372 | kunmap (page);
|
---|
373 | SET_TYPE (VM_FAULT_MAJOR);
|
---|
374 | return page;
|
---|
375 | }
|
---|
376 |
|
---|
377 | static struct vm_operations_struct sf_vma_ops = {
|
---|
378 | .nopage = sf_reg_nopage
|
---|
379 | };
|
---|
380 |
|
---|
381 | static int
|
---|
382 | sf_reg_mmap (struct file *file, struct vm_area_struct *vma)
|
---|
383 | {
|
---|
384 | TRACE ();
|
---|
385 | if (vma->vm_flags & VM_SHARED) {
|
---|
386 | LogFunc(("shared mmapping not available\n"));
|
---|
387 | return -EINVAL;
|
---|
388 | }
|
---|
389 |
|
---|
390 | vma->vm_ops = &sf_vma_ops;
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | struct file_operations sf_reg_fops = {
|
---|
395 | .read = sf_reg_read,
|
---|
396 | .open = sf_reg_open,
|
---|
397 | .write = sf_reg_write,
|
---|
398 | .release = sf_reg_release,
|
---|
399 | .mmap = sf_reg_mmap,
|
---|
400 | #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
401 | # if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 23)
|
---|
402 | .splice_read = generic_file_splice_read,
|
---|
403 | # else
|
---|
404 | .sendfile = generic_file_sendfile,
|
---|
405 | # endif
|
---|
406 | .aio_read = generic_file_aio_read,
|
---|
407 | .aio_write = generic_file_aio_write,
|
---|
408 | .fsync = simple_sync_file,
|
---|
409 | .llseek = generic_file_llseek,
|
---|
410 | #endif
|
---|
411 | };
|
---|
412 |
|
---|
413 |
|
---|
414 | struct inode_operations sf_reg_iops = {
|
---|
415 | #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
|
---|
416 | .revalidate = sf_inode_revalidate
|
---|
417 | #else
|
---|
418 | .getattr = sf_getattr
|
---|
419 | #endif
|
---|
420 | };
|
---|
421 |
|
---|
422 |
|
---|
423 | #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
424 | static int
|
---|
425 | sf_readpage(struct file *file, struct page *page)
|
---|
426 | {
|
---|
427 | char *buf = kmap(page);
|
---|
428 | struct inode *inode = file->f_dentry->d_inode;
|
---|
429 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
430 | struct sf_reg_info *sf_r = file->private_data;
|
---|
431 | uint32_t nread = PAGE_SIZE;
|
---|
432 | loff_t off = page->index << PAGE_SHIFT;
|
---|
433 | int ret;
|
---|
434 |
|
---|
435 | TRACE ();
|
---|
436 |
|
---|
437 | ret = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
|
---|
438 | if (ret) {
|
---|
439 | kunmap (page);
|
---|
440 | return ret;
|
---|
441 | }
|
---|
442 | flush_dcache_page (page);
|
---|
443 | kunmap (page);
|
---|
444 | SetPageUptodate(page);
|
---|
445 | if (PageLocked(page))
|
---|
446 | unlock_page(page);
|
---|
447 | return 0;
|
---|
448 | }
|
---|
449 |
|
---|
450 | struct address_space_operations sf_reg_aops = {
|
---|
451 | .readpage = sf_readpage,
|
---|
452 | # if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
|
---|
453 | .write_begin = simple_write_begin,
|
---|
454 | .write_end = simple_write_end,
|
---|
455 | # else
|
---|
456 | .prepare_write = simple_prepare_write,
|
---|
457 | .commit_write = simple_commit_write,
|
---|
458 | # endif
|
---|
459 | };
|
---|
460 | #endif
|
---|