VirtualBox

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

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

more RT_ZERO()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 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 params.Info.Attr.fMode = inode->i_mode;
325 LogFunc(("sf_reg_open: calling vboxCallCreate, file %s, flags=%d, %#x\n",
326 sf_i->path->String.utf8 , file->f_flags, params.CreateFlags));
327 rc = vboxCallCreate (&client_handle, &sf_g->map, sf_i->path, &params);
328
329 if (RT_FAILURE (rc)) {
330 LogFunc(("vboxCallCreate failed flags=%d,%#x rc=%Rrc\n",
331 file->f_flags, params.CreateFlags, rc));
332 kfree (sf_r);
333 return -RTErrConvertToErrno(rc);
334 }
335
336 if (SHFL_HANDLE_NIL == params.Handle) {
337 switch (params.Result) {
338 case SHFL_PATH_NOT_FOUND:
339 case SHFL_FILE_NOT_FOUND:
340 rc_linux = -ENOENT;
341 break;
342 case SHFL_FILE_EXISTS:
343 rc_linux = -EEXIST;
344 break;
345 default:
346 break;
347 }
348 }
349
350 sf_i->force_restat = 1;
351 sf_r->handle = params.Handle;
352 sf_i->file = file;
353 file->private_data = sf_r;
354 return rc_linux;
355}
356
357static int
358sf_reg_release (struct inode *inode, struct file *file)
359{
360 int rc;
361 struct sf_reg_info *sf_r;
362 struct sf_glob_info *sf_g;
363 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
364
365 TRACE ();
366 sf_g = GET_GLOB_INFO (inode->i_sb);
367 sf_r = file->private_data;
368
369 BUG_ON (!sf_g);
370 BUG_ON (!sf_r);
371
372 rc = vboxCallClose (&client_handle, &sf_g->map, sf_r->handle);
373 if (RT_FAILURE (rc)) {
374 LogFunc(("vboxCallClose failed rc=%Rrc\n", rc));
375 }
376
377 kfree (sf_r);
378 sf_i->file = NULL;
379 file->private_data = NULL;
380 return 0;
381}
382
383#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
384static int
385sf_reg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
386#elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
387static struct page *
388sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int *type)
389# define SET_TYPE(t) *type = (t)
390#else /* LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0) */
391static struct page *
392sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int unused)
393# define SET_TYPE(t)
394#endif
395{
396 struct page *page;
397 char *buf;
398 loff_t off;
399 uint32_t nread = PAGE_SIZE;
400 int err;
401 struct file *file = vma->vm_file;
402 struct inode *inode = file->f_dentry->d_inode;
403 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
404 struct sf_reg_info *sf_r = file->private_data;
405
406 TRACE ();
407#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
408 if (vmf->pgoff > vma->vm_end)
409 return VM_FAULT_SIGBUS;
410#else
411 if (vaddr > vma->vm_end) {
412 SET_TYPE (VM_FAULT_SIGBUS);
413 return NOPAGE_SIGBUS;
414 }
415#endif
416
417 page = alloc_page (GFP_HIGHUSER);
418 if (!page) {
419 LogRelFunc(("failed to allocate page\n"));
420#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
421 return VM_FAULT_OOM;
422#else
423 SET_TYPE (VM_FAULT_OOM);
424 return NOPAGE_OOM;
425#endif
426 }
427
428 buf = kmap (page);
429#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
430 off = (vmf->pgoff << PAGE_SHIFT);
431#else
432 off = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
433#endif
434 err = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
435 if (err) {
436 kunmap (page);
437 put_page (page);
438#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
439 return VM_FAULT_SIGBUS;
440#else
441 SET_TYPE (VM_FAULT_SIGBUS);
442 return NOPAGE_SIGBUS;
443#endif
444 }
445
446 BUG_ON (nread > PAGE_SIZE);
447 if (!nread) {
448#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
449 clear_user_page (page_address (page), vmf->pgoff, page);
450#elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
451 clear_user_page (page_address (page), vaddr, page);
452#else
453 clear_user_page (page_address (page), vaddr);
454#endif
455 }
456 else {
457 memset (buf + nread, 0, PAGE_SIZE - nread);
458 }
459
460 flush_dcache_page (page);
461 kunmap (page);
462#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
463 vmf->page = page;
464 return 0;
465#else
466 SET_TYPE (VM_FAULT_MAJOR);
467 return page;
468#endif
469}
470
471static struct vm_operations_struct sf_vma_ops = {
472#if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
473 .fault = sf_reg_fault
474#else
475 .nopage = sf_reg_nopage
476#endif
477};
478
479static int
480sf_reg_mmap (struct file *file, struct vm_area_struct *vma)
481{
482 TRACE ();
483 if (vma->vm_flags & VM_SHARED) {
484 LogFunc(("shared mmapping not available\n"));
485 return -EINVAL;
486 }
487
488 vma->vm_ops = &sf_vma_ops;
489 return 0;
490}
491
492struct file_operations sf_reg_fops = {
493 .read = sf_reg_read,
494 .open = sf_reg_open,
495 .write = sf_reg_write,
496 .release = sf_reg_release,
497 .mmap = sf_reg_mmap,
498#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
499# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 23)
500 .splice_read = generic_file_splice_read,
501# else
502 .sendfile = generic_file_sendfile,
503# endif
504 .aio_read = generic_file_aio_read,
505 .aio_write = generic_file_aio_write,
506 .fsync = simple_sync_file,
507 .llseek = generic_file_llseek,
508#endif
509};
510
511
512struct inode_operations sf_reg_iops = {
513#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
514 .revalidate = sf_inode_revalidate
515#else
516 .getattr = sf_getattr,
517 .setattr = sf_setattr
518#endif
519};
520
521
522#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
523static int
524sf_readpage(struct file *file, struct page *page)
525{
526 struct inode *inode = file->f_dentry->d_inode;
527 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
528 struct sf_reg_info *sf_r = file->private_data;
529 uint32_t nread = PAGE_SIZE;
530 char *buf;
531 loff_t off = ((loff_t)page->index) << PAGE_SHIFT;
532 int ret;
533
534 TRACE ();
535
536 buf = kmap(page);
537 ret = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
538 if (ret) {
539 kunmap (page);
540 if (PageLocked(page))
541 unlock_page(page);
542 return ret;
543 }
544 BUG_ON (nread > PAGE_SIZE);
545 memset(&buf[nread], 0, PAGE_SIZE - nread);
546 flush_dcache_page (page);
547 kunmap (page);
548 SetPageUptodate(page);
549 unlock_page(page);
550 return 0;
551}
552
553static int
554sf_writepage(struct page *page, struct writeback_control *wbc)
555{
556 struct address_space *mapping = page->mapping;
557 struct inode *inode = mapping->host;
558 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
559 struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
560 struct file *file = sf_i->file;
561 struct sf_reg_info *sf_r = file->private_data;
562 char *buf;
563 uint32_t nwritten = PAGE_SIZE;
564 int end_index = inode->i_size >> PAGE_SHIFT;
565 loff_t off = ((loff_t) page->index) << PAGE_SHIFT;
566 int err;
567
568 TRACE ();
569
570 if (page->index >= end_index)
571 nwritten = inode->i_size & (PAGE_SIZE-1);
572
573 buf = kmap(page);
574
575 err = sf_reg_write_aux (__func__, sf_g, sf_r, buf, &nwritten, off);
576 if (err < 0) {
577 ClearPageUptodate(page);
578 goto out;
579 }
580
581 if (off > inode->i_size)
582 inode->i_size = off;
583
584 if (PageError(page))
585 ClearPageError(page);
586 err = 0;
587out:
588 kunmap(page);
589
590 unlock_page(page);
591 return err;
592}
593
594# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
595int
596sf_write_begin(struct file *file, struct address_space *mapping, loff_t pos,
597 unsigned len, unsigned flags, struct page **pagep, void **fsdata)
598{
599 TRACE ();
600
601 return simple_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
602}
603
604int
605sf_write_end(struct file *file, struct address_space *mapping, loff_t pos,
606 unsigned len, unsigned copied, struct page *page, void *fsdata)
607{
608 struct inode *inode = mapping->host;
609 struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
610 struct sf_reg_info *sf_r = file->private_data;
611 void *buf;
612 unsigned from = pos & (PAGE_SIZE - 1);
613 uint32_t nwritten = len;
614 int err;
615
616 TRACE ();
617
618 buf = kmap(page);
619 err = sf_reg_write_aux (__func__, sf_g, sf_r, buf+from, &nwritten, pos);
620 kunmap(page);
621
622 if (!PageUptodate(page) && err == PAGE_SIZE)
623 SetPageUptodate(page);
624
625 if (err >= 0) {
626 pos += nwritten;
627 if (pos > inode->i_size)
628 inode->i_size = pos;
629 }
630
631 unlock_page(page);
632 page_cache_release(page);
633
634 return nwritten;
635}
636
637# endif /* KERNEL_VERSION >= 2.6.24 */
638
639struct address_space_operations sf_reg_aops = {
640 .readpage = sf_readpage,
641 .writepage = sf_writepage,
642# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
643 .write_begin = sf_write_begin,
644 .write_end = sf_write_end,
645# else
646 .prepare_write = simple_prepare_write,
647 .commit_write = simple_commit_write,
648# endif
649};
650#endif
Note: See TracBrowser for help on using the repository browser.

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