VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/regops.c@ 24660

Last change on this file since 24660 was 24350, checked in by vboxsync, 15 years ago

APPEND mode for shared folders (SHFL_CF_ACCESS_APPEND).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
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
30static void *alloc_bounch_buffer (size_t *tmp_sizep, PRTCCPHYS physp, size_t xfer_size, const char *caller)
31{
32 size_t tmp_size;
33 void *tmp;
34
35 /* try for big first. */
36 tmp_size = RT_ALIGN_Z(xfer_size, PAGE_SIZE);
37 if (tmp_size > 16U*_1K)
38 tmp_size = 16U*_1K;
39 tmp = kmalloc (tmp_size, GFP_KERNEL);
40 if (!tmp) {
41
42 /* fall back on a page sized buffer. */
43 tmp = kmalloc (PAGE_SIZE, GFP_KERNEL);
44 if (!tmp) {
45 LogRel(("%s: could not allocate bounce buffer for xfer_size=%zu %s\n", caller, xfer_size));
46 return NULL;
47 }
48 tmp_size = PAGE_SIZE;
49 }
50
51 *tmp_sizep = tmp_size;
52 *physp = virt_to_phys(tmp);
53 return tmp;
54}
55
56static void free_bounch_buffer (void *tmp)
57{
58 kfree (tmp);
59}
60
61
62/* fops */
63static int
64sf_reg_read_aux (const char *caller, struct sf_glob_info *sf_g,
65 struct sf_reg_info *sf_r, void *buf, uint32_t *nread,
66 uint64_t pos)
67{
68 /** @todo bird: yes, kmap() and kmalloc() input only. Since the buffer is
69 * contiguous in physical memory (kmalloc or single page), we should
70 * use a physical address here to speed things up. */
71 int rc = vboxCallRead (&client_handle, &sf_g->map, sf_r->handle,
72 pos, nread, buf, false /* already locked? */);
73 if (RT_FAILURE (rc)) {
74 LogFunc(("vboxCallRead failed. caller=%s, rc=%Rrc\n",
75 caller, rc));
76 return -EPROTO;
77 }
78 return 0;
79}
80
81static int
82sf_reg_write_aux (const char *caller, struct sf_glob_info *sf_g,
83 struct sf_reg_info *sf_r, void *buf, uint32_t *nwritten,
84 uint64_t pos)
85{
86 /** @todo bird: yes, kmap() and kmalloc() input only. Since the buffer is
87 * contiguous in physical memory (kmalloc or single page), we should
88 * use a physical address here to speed things up. */
89 int rc = vboxCallWrite (&client_handle, &sf_g->map, sf_r->handle,
90 pos, nwritten, buf, false /* already locked? */);
91 if (RT_FAILURE (rc)) {
92 LogFunc(("vboxCallWrite failed. caller=%s, rc=%Rrc\n",
93 caller, rc));
94 return -EPROTO;
95 }
96 return 0;
97}
98
99static ssize_t
100sf_reg_read (struct file *file, char *buf, size_t size, loff_t *off)
101{
102 int err;
103 void *tmp;
104 RTCCPHYS tmp_phys;
105 size_t tmp_size;
106 size_t left = size;
107 ssize_t total_bytes_read = 0;
108 struct inode *inode = file->f_dentry->d_inode;
109 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
110 struct sf_reg_info *sf_r = file->private_data;
111 loff_t pos = *off;
112
113 TRACE ();
114 if (!S_ISREG (inode->i_mode)) {
115 LogFunc(("read from non regular file %d\n", inode->i_mode));
116 return -EINVAL;
117 }
118
119 /** XXX Check read permission accoring to inode->i_mode! */
120
121 if (!size) {
122 return 0;
123 }
124
125 tmp = alloc_bounch_buffer (&tmp_size, &tmp_phys, size, __PRETTY_FUNCTION__);
126 if (!tmp)
127 return -ENOMEM;
128
129 while (left) {
130 uint32_t to_read, nread;
131
132 to_read = tmp_size;
133 if (to_read > left) {
134 to_read = (uint32_t) left;
135 }
136 nread = to_read;
137
138 err = sf_reg_read_aux (__func__, sf_g, sf_r, tmp, &nread, pos);
139 if (err)
140 goto fail;
141
142 if (copy_to_user (buf, tmp, nread)) {
143 err = -EFAULT;
144 goto fail;
145 }
146
147 pos += nread;
148 left -= nread;
149 buf += nread;
150 total_bytes_read += nread;
151 if (nread != to_read) {
152 break;
153 }
154 }
155
156 *off += total_bytes_read;
157 free_bounch_buffer (tmp);
158 return total_bytes_read;
159
160 fail:
161 free_bounch_buffer (tmp);
162 return err;
163}
164
165static ssize_t
166sf_reg_write (struct file *file, const char *buf, size_t size, loff_t *off)
167{
168 int err;
169 void *tmp;
170 RTCCPHYS tmp_phys;
171 size_t tmp_size;
172 size_t left = size;
173 ssize_t total_bytes_written = 0;
174 struct inode *inode = file->f_dentry->d_inode;
175 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
176 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
177 struct sf_reg_info *sf_r = file->private_data;
178 loff_t pos;
179
180 TRACE ();
181 BUG_ON (!sf_i);
182 BUG_ON (!sf_g);
183 BUG_ON (!sf_r);
184
185 if (!S_ISREG (inode->i_mode)) {
186 LogFunc(("write to non regular file %d\n", inode->i_mode));
187 return -EINVAL;
188 }
189
190 pos = *off;
191 if (file->f_flags & O_APPEND)
192 {
193 pos = inode->i_size;
194 *off = pos;
195 }
196
197 /** XXX Check write permission accoring to inode->i_mode! */
198
199 if (!size)
200 return 0;
201
202 tmp = alloc_bounch_buffer (&tmp_size, &tmp_phys, size, __PRETTY_FUNCTION__);
203 if (!tmp)
204 return -ENOMEM;
205
206 while (left) {
207 uint32_t to_write, nwritten;
208
209 to_write = tmp_size;
210 if (to_write > left) {
211 to_write = (uint32_t) left;
212 }
213 nwritten = to_write;
214
215 if (copy_from_user (tmp, buf, to_write)) {
216 err = -EFAULT;
217 goto fail;
218 }
219
220#if 1
221 if (VbglR0CanUsePhysPageList()) {
222 err = VbglR0SfWritePhysCont (&client_handle, &sf_g->map, sf_r->handle,
223 pos, &nwritten, tmp_phys);
224 err = RT_FAILURE(err) ? -EPROTO : 0;
225 } else
226#endif
227 err = sf_reg_write_aux (__func__, sf_g, sf_r, tmp, &nwritten, pos);
228 if (err)
229 goto fail;
230
231 pos += nwritten;
232 left -= nwritten;
233 buf += nwritten;
234 total_bytes_written += nwritten;
235 if (nwritten != to_write)
236 break;
237 }
238
239 *off += total_bytes_written;
240 if (*off > inode->i_size)
241 inode->i_size = *off;
242
243 sf_i->force_restat = 1;
244 free_bounch_buffer (tmp);
245 return total_bytes_written;
246
247 fail:
248 free_bounch_buffer (tmp);
249 return err;
250}
251
252static int
253sf_reg_open (struct inode *inode, struct file *file)
254{
255 int rc, rc_linux = 0;
256 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
257 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
258 struct sf_reg_info *sf_r;
259 SHFLCREATEPARMS params;
260
261 TRACE ();
262 BUG_ON (!sf_g);
263 BUG_ON (!sf_i);
264
265 LogFunc(("open %s\n", sf_i->path->String.utf8));
266
267 sf_r = kmalloc (sizeof (*sf_r), GFP_KERNEL);
268 if (!sf_r) {
269 LogRelFunc(("could not allocate reg info\n"));
270 return -ENOMEM;
271 }
272
273 RT_ZERO(params);
274 params.Handle = SHFL_HANDLE_NIL;
275 /* We check the value of params.Handle afterwards to find out if
276 * the call succeeded or failed, as the API does not seem to cleanly
277 * distinguish error and informational messages.
278 *
279 * Furthermore, we must set params.Handle to SHFL_HANDLE_NIL to
280 * make the shared folders host service use our fMode parameter */
281
282 if (file->f_flags & O_CREAT) {
283 LogFunc(("O_CREAT set\n"));
284 params.CreateFlags |= SHFL_CF_ACT_CREATE_IF_NEW;
285 /* We ignore O_EXCL, as the Linux kernel seems to call create
286 beforehand itself, so O_EXCL should always fail. */
287 if (file->f_flags & O_TRUNC) {
288 LogFunc(("O_TRUNC set\n"));
289 params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
290 | SHFL_CF_ACCESS_WRITE);
291 }
292 else {
293 params.CreateFlags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
294 }
295 }
296 else {
297 params.CreateFlags |= SHFL_CF_ACT_FAIL_IF_NEW;
298 if (file->f_flags & O_TRUNC) {
299 LogFunc(("O_TRUNC set\n"));
300 params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
301 | SHFL_CF_ACCESS_WRITE);
302 }
303 }
304
305 if (!(params.CreateFlags & SHFL_CF_ACCESS_READWRITE)) {
306 switch (file->f_flags & O_ACCMODE) {
307 case O_RDONLY:
308 params.CreateFlags |= SHFL_CF_ACCESS_READ;
309 break;
310
311 case O_WRONLY:
312 params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
313 break;
314
315 case O_RDWR:
316 params.CreateFlags |= SHFL_CF_ACCESS_READWRITE;
317 break;
318
319 default:
320 BUG ();
321 }
322 }
323
324 if (file->f_flags & O_APPEND) {
325 LogFunc(("O_APPEND set\n"));
326 params.CreateFlags |= SHFL_CF_ACCESS_APPEND;
327 }
328
329 params.Info.Attr.fMode = inode->i_mode;
330 LogFunc(("sf_reg_open: calling vboxCallCreate, file %s, flags=%d, %#x\n",
331 sf_i->path->String.utf8 , file->f_flags, params.CreateFlags));
332 rc = vboxCallCreate (&client_handle, &sf_g->map, sf_i->path, &params);
333
334 if (RT_FAILURE (rc)) {
335 LogFunc(("vboxCallCreate failed flags=%d,%#x rc=%Rrc\n",
336 file->f_flags, params.CreateFlags, rc));
337 kfree (sf_r);
338 return -RTErrConvertToErrno(rc);
339 }
340
341 if (SHFL_HANDLE_NIL == params.Handle) {
342 switch (params.Result) {
343 case SHFL_PATH_NOT_FOUND:
344 case SHFL_FILE_NOT_FOUND:
345 rc_linux = -ENOENT;
346 break;
347 case SHFL_FILE_EXISTS:
348 rc_linux = -EEXIST;
349 break;
350 default:
351 break;
352 }
353 }
354
355 sf_i->force_restat = 1;
356 sf_r->handle = params.Handle;
357 sf_i->file = file;
358 file->private_data = sf_r;
359 return rc_linux;
360}
361
362static int
363sf_reg_release (struct inode *inode, struct file *file)
364{
365 int rc;
366 struct sf_reg_info *sf_r;
367 struct sf_glob_info *sf_g;
368 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
369
370 TRACE ();
371 sf_g = GET_GLOB_INFO (inode->i_sb);
372 sf_r = file->private_data;
373
374 BUG_ON (!sf_g);
375 BUG_ON (!sf_r);
376
377 rc = vboxCallClose (&client_handle, &sf_g->map, sf_r->handle);
378 if (RT_FAILURE (rc)) {
379 LogFunc(("vboxCallClose failed rc=%Rrc\n", rc));
380 }
381
382 kfree (sf_r);
383 sf_i->file = NULL;
384 file->private_data = NULL;
385 return 0;
386}
387
388#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
389static int
390sf_reg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
391#elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
392static struct page *
393sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int *type)
394# define SET_TYPE(t) *type = (t)
395#else /* LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0) */
396static struct page *
397sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int unused)
398# define SET_TYPE(t)
399#endif
400{
401 struct page *page;
402 char *buf;
403 loff_t off;
404 uint32_t nread = PAGE_SIZE;
405 int err;
406 struct file *file = vma->vm_file;
407 struct inode *inode = file->f_dentry->d_inode;
408 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
409 struct sf_reg_info *sf_r = file->private_data;
410
411 TRACE ();
412#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
413 if (vmf->pgoff > vma->vm_end)
414 return VM_FAULT_SIGBUS;
415#else
416 if (vaddr > vma->vm_end) {
417 SET_TYPE (VM_FAULT_SIGBUS);
418 return NOPAGE_SIGBUS;
419 }
420#endif
421
422 page = alloc_page (GFP_HIGHUSER);
423 if (!page) {
424 LogRelFunc(("failed to allocate page\n"));
425#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
426 return VM_FAULT_OOM;
427#else
428 SET_TYPE (VM_FAULT_OOM);
429 return NOPAGE_OOM;
430#endif
431 }
432
433 buf = kmap (page);
434#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
435 off = (vmf->pgoff << PAGE_SHIFT);
436#else
437 off = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
438#endif
439 err = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
440 if (err) {
441 kunmap (page);
442 put_page (page);
443#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
444 return VM_FAULT_SIGBUS;
445#else
446 SET_TYPE (VM_FAULT_SIGBUS);
447 return NOPAGE_SIGBUS;
448#endif
449 }
450
451 BUG_ON (nread > PAGE_SIZE);
452 if (!nread) {
453#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
454 clear_user_page (page_address (page), vmf->pgoff, page);
455#elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
456 clear_user_page (page_address (page), vaddr, page);
457#else
458 clear_user_page (page_address (page), vaddr);
459#endif
460 }
461 else {
462 memset (buf + nread, 0, PAGE_SIZE - nread);
463 }
464
465 flush_dcache_page (page);
466 kunmap (page);
467#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
468 vmf->page = page;
469 return 0;
470#else
471 SET_TYPE (VM_FAULT_MAJOR);
472 return page;
473#endif
474}
475
476static struct vm_operations_struct sf_vma_ops = {
477#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
478 .fault = sf_reg_fault
479#else
480 .nopage = sf_reg_nopage
481#endif
482};
483
484static int
485sf_reg_mmap (struct file *file, struct vm_area_struct *vma)
486{
487 TRACE ();
488 if (vma->vm_flags & VM_SHARED) {
489 LogFunc(("shared mmapping not available\n"));
490 return -EINVAL;
491 }
492
493 vma->vm_ops = &sf_vma_ops;
494 return 0;
495}
496
497struct file_operations sf_reg_fops = {
498 .read = sf_reg_read,
499 .open = sf_reg_open,
500 .write = sf_reg_write,
501 .release = sf_reg_release,
502 .mmap = sf_reg_mmap,
503#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
504# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 23)
505 .splice_read = generic_file_splice_read,
506# else
507 .sendfile = generic_file_sendfile,
508# endif
509 .aio_read = generic_file_aio_read,
510 .aio_write = generic_file_aio_write,
511 .fsync = simple_sync_file,
512 .llseek = generic_file_llseek,
513#endif
514};
515
516
517struct inode_operations sf_reg_iops = {
518#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
519 .revalidate = sf_inode_revalidate
520#else
521 .getattr = sf_getattr,
522 .setattr = sf_setattr
523#endif
524};
525
526
527#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
528static int
529sf_readpage(struct file *file, struct page *page)
530{
531 struct inode *inode = file->f_dentry->d_inode;
532 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
533 struct sf_reg_info *sf_r = file->private_data;
534 uint32_t nread = PAGE_SIZE;
535 char *buf;
536 loff_t off = ((loff_t)page->index) << PAGE_SHIFT;
537 int ret;
538
539 TRACE ();
540
541 buf = kmap(page);
542 ret = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
543 if (ret) {
544 kunmap (page);
545 if (PageLocked(page))
546 unlock_page(page);
547 return ret;
548 }
549 BUG_ON (nread > PAGE_SIZE);
550 memset(&buf[nread], 0, PAGE_SIZE - nread);
551 flush_dcache_page (page);
552 kunmap (page);
553 SetPageUptodate(page);
554 unlock_page(page);
555 return 0;
556}
557
558static int
559sf_writepage(struct page *page, struct writeback_control *wbc)
560{
561 struct address_space *mapping = page->mapping;
562 struct inode *inode = mapping->host;
563 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
564 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
565 struct file *file = sf_i->file;
566 struct sf_reg_info *sf_r = file->private_data;
567 char *buf;
568 uint32_t nwritten = PAGE_SIZE;
569 int end_index = inode->i_size >> PAGE_SHIFT;
570 loff_t off = ((loff_t) page->index) << PAGE_SHIFT;
571 int err;
572
573 TRACE ();
574
575 if (page->index >= end_index)
576 nwritten = inode->i_size & (PAGE_SIZE-1);
577
578 buf = kmap(page);
579
580 err = sf_reg_write_aux (__func__, sf_g, sf_r, buf, &nwritten, off);
581 if (err < 0) {
582 ClearPageUptodate(page);
583 goto out;
584 }
585
586 if (off > inode->i_size)
587 inode->i_size = off;
588
589 if (PageError(page))
590 ClearPageError(page);
591 err = 0;
592out:
593 kunmap(page);
594
595 unlock_page(page);
596 return err;
597}
598
599# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
600int
601sf_write_begin(struct file *file, struct address_space *mapping, loff_t pos,
602 unsigned len, unsigned flags, struct page **pagep, void **fsdata)
603{
604 TRACE ();
605
606 return simple_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
607}
608
609int
610sf_write_end(struct file *file, struct address_space *mapping, loff_t pos,
611 unsigned len, unsigned copied, struct page *page, void *fsdata)
612{
613 struct inode *inode = mapping->host;
614 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
615 struct sf_reg_info *sf_r = file->private_data;
616 void *buf;
617 unsigned from = pos & (PAGE_SIZE - 1);
618 uint32_t nwritten = len;
619 int err;
620
621 TRACE ();
622
623 buf = kmap(page);
624 err = sf_reg_write_aux (__func__, sf_g, sf_r, buf+from, &nwritten, pos);
625 kunmap(page);
626
627 if (!PageUptodate(page) && err == PAGE_SIZE)
628 SetPageUptodate(page);
629
630 if (err >= 0) {
631 pos += nwritten;
632 if (pos > inode->i_size)
633 inode->i_size = pos;
634 }
635
636 unlock_page(page);
637 page_cache_release(page);
638
639 return nwritten;
640}
641
642# endif /* KERNEL_VERSION >= 2.6.24 */
643
644struct address_space_operations sf_reg_aops = {
645 .readpage = sf_readpage,
646 .writepage = sf_writepage,
647# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
648 .write_begin = sf_write_begin,
649 .write_end = sf_write_end,
650# else
651 .prepare_write = simple_prepare_write,
652 .commit_write = simple_commit_write,
653# endif
654};
655#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette