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 |
|
---|
30 | static 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 |
|
---|
56 | static void free_bounch_buffer (void *tmp)
|
---|
57 | {
|
---|
58 | kfree (tmp);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /* fops */
|
---|
63 | static int
|
---|
64 | sf_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 |
|
---|
81 | static int
|
---|
82 | sf_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 |
|
---|
99 | static ssize_t
|
---|
100 | sf_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 |
|
---|
165 | static ssize_t
|
---|
166 | sf_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 | pos += inode->i_size;
|
---|
193 |
|
---|
194 | /** XXX Check write permission accoring to inode->i_mode! */
|
---|
195 |
|
---|
196 | if (!size)
|
---|
197 | return 0;
|
---|
198 |
|
---|
199 | tmp = alloc_bounch_buffer (&tmp_size, &tmp_phys, size, __PRETTY_FUNCTION__);
|
---|
200 | if (!tmp)
|
---|
201 | return -ENOMEM;
|
---|
202 |
|
---|
203 | while (left) {
|
---|
204 | uint32_t to_write, nwritten;
|
---|
205 |
|
---|
206 | to_write = tmp_size;
|
---|
207 | if (to_write > left) {
|
---|
208 | to_write = (uint32_t) left;
|
---|
209 | }
|
---|
210 | nwritten = to_write;
|
---|
211 |
|
---|
212 | if (copy_from_user (tmp, buf, to_write)) {
|
---|
213 | err = -EFAULT;
|
---|
214 | goto fail;
|
---|
215 | }
|
---|
216 |
|
---|
217 | #if 1
|
---|
218 | if (VbglR0CanUsePhysPageList()) {
|
---|
219 | err = VbglR0SfWritePhysCont (&client_handle, &sf_g->map, sf_r->handle,
|
---|
220 | pos, &nwritten, tmp_phys);
|
---|
221 | err = RT_FAILURE(err) ? -EPROTO : 0;
|
---|
222 | } else
|
---|
223 | #endif
|
---|
224 | err = sf_reg_write_aux (__func__, sf_g, sf_r, tmp, &nwritten, pos);
|
---|
225 | if (err)
|
---|
226 | goto fail;
|
---|
227 |
|
---|
228 | pos += nwritten;
|
---|
229 | left -= nwritten;
|
---|
230 | buf += nwritten;
|
---|
231 | total_bytes_written += nwritten;
|
---|
232 | if (nwritten != to_write)
|
---|
233 | break;
|
---|
234 | }
|
---|
235 |
|
---|
236 | #if 1 /* XXX: which way is correct? */
|
---|
237 | *off += total_bytes_written;
|
---|
238 | #else
|
---|
239 | file->f_pos += total_bytes_written;
|
---|
240 | #endif
|
---|
241 | sf_i->force_restat = 1;
|
---|
242 | free_bounch_buffer (tmp);
|
---|
243 | return total_bytes_written;
|
---|
244 |
|
---|
245 | fail:
|
---|
246 | free_bounch_buffer (tmp);
|
---|
247 | return err;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static int
|
---|
251 | sf_reg_open (struct inode *inode, struct file *file)
|
---|
252 | {
|
---|
253 | int rc, rc_linux = 0;
|
---|
254 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
255 | struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
|
---|
256 | struct sf_reg_info *sf_r;
|
---|
257 | SHFLCREATEPARMS params;
|
---|
258 |
|
---|
259 | TRACE ();
|
---|
260 | BUG_ON (!sf_g);
|
---|
261 | BUG_ON (!sf_i);
|
---|
262 |
|
---|
263 | LogFunc(("open %s\n", sf_i->path->String.utf8));
|
---|
264 |
|
---|
265 | sf_r = kmalloc (sizeof (*sf_r), GFP_KERNEL);
|
---|
266 | if (!sf_r) {
|
---|
267 | LogRelFunc(("could not allocate reg info\n"));
|
---|
268 | return -ENOMEM;
|
---|
269 | }
|
---|
270 |
|
---|
271 | memset(¶ms, 0, sizeof(params));
|
---|
272 | params.Handle = SHFL_HANDLE_NIL;
|
---|
273 | /* We check the value of params.Handle afterwards to find out if
|
---|
274 | * the call succeeded or failed, as the API does not seem to cleanly
|
---|
275 | * distinguish error and informational messages.
|
---|
276 | *
|
---|
277 | * Furthermore, we must set params.Handle to SHFL_HANDLE_NIL to
|
---|
278 | * make the shared folders host service use our fMode parameter */
|
---|
279 |
|
---|
280 | if (file->f_flags & O_CREAT) {
|
---|
281 | LogFunc(("O_CREAT set\n"));
|
---|
282 | params.CreateFlags |= SHFL_CF_ACT_CREATE_IF_NEW;
|
---|
283 | /* We ignore O_EXCL, as the Linux kernel seems to call create
|
---|
284 | beforehand itself, so O_EXCL should always fail. */
|
---|
285 | if (file->f_flags & O_TRUNC) {
|
---|
286 | LogFunc(("O_TRUNC set\n"));
|
---|
287 | params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
|
---|
288 | | SHFL_CF_ACCESS_WRITE);
|
---|
289 | }
|
---|
290 | else {
|
---|
291 | params.CreateFlags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | else {
|
---|
295 | params.CreateFlags |= SHFL_CF_ACT_FAIL_IF_NEW;
|
---|
296 | if (file->f_flags & O_TRUNC) {
|
---|
297 | LogFunc(("O_TRUNC set\n"));
|
---|
298 | params.CreateFlags |= ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
|
---|
299 | | SHFL_CF_ACCESS_WRITE);
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (!(params.CreateFlags & SHFL_CF_ACCESS_READWRITE)) {
|
---|
304 | switch (file->f_flags & O_ACCMODE) {
|
---|
305 | case O_RDONLY:
|
---|
306 | params.CreateFlags |= SHFL_CF_ACCESS_READ;
|
---|
307 | break;
|
---|
308 |
|
---|
309 | case O_WRONLY:
|
---|
310 | params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
|
---|
311 | break;
|
---|
312 |
|
---|
313 | case O_RDWR:
|
---|
314 | params.CreateFlags |= SHFL_CF_ACCESS_READWRITE;
|
---|
315 | break;
|
---|
316 |
|
---|
317 | default:
|
---|
318 | BUG ();
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | params.Info.Attr.fMode = inode->i_mode;
|
---|
323 | LogFunc(("sf_reg_open: calling vboxCallCreate, file %s, flags=%d, %#x\n",
|
---|
324 | sf_i->path->String.utf8 , file->f_flags, params.CreateFlags));
|
---|
325 | rc = vboxCallCreate (&client_handle, &sf_g->map, sf_i->path, ¶ms);
|
---|
326 |
|
---|
327 | if (RT_FAILURE (rc)) {
|
---|
328 | LogFunc(("vboxCallCreate failed flags=%d,%#x rc=%Rrc\n",
|
---|
329 | file->f_flags, params.CreateFlags, rc));
|
---|
330 | kfree (sf_r);
|
---|
331 | return -RTErrConvertToErrno(rc);
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (SHFL_HANDLE_NIL == params.Handle) {
|
---|
335 | switch (params.Result) {
|
---|
336 | case SHFL_PATH_NOT_FOUND:
|
---|
337 | case SHFL_FILE_NOT_FOUND:
|
---|
338 | rc_linux = -ENOENT;
|
---|
339 | break;
|
---|
340 | case SHFL_FILE_EXISTS:
|
---|
341 | rc_linux = -EEXIST;
|
---|
342 | break;
|
---|
343 | default:
|
---|
344 | break;
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | sf_i->force_restat = 1;
|
---|
349 | sf_r->handle = params.Handle;
|
---|
350 | sf_i->file = file;
|
---|
351 | file->private_data = sf_r;
|
---|
352 | return rc_linux;
|
---|
353 | }
|
---|
354 |
|
---|
355 | static int
|
---|
356 | sf_reg_release (struct inode *inode, struct file *file)
|
---|
357 | {
|
---|
358 | int rc;
|
---|
359 | struct sf_reg_info *sf_r;
|
---|
360 | struct sf_glob_info *sf_g;
|
---|
361 | struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
|
---|
362 |
|
---|
363 | TRACE ();
|
---|
364 | sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
365 | sf_r = file->private_data;
|
---|
366 |
|
---|
367 | BUG_ON (!sf_g);
|
---|
368 | BUG_ON (!sf_r);
|
---|
369 |
|
---|
370 | rc = vboxCallClose (&client_handle, &sf_g->map, sf_r->handle);
|
---|
371 | if (RT_FAILURE (rc)) {
|
---|
372 | LogFunc(("vboxCallClose failed rc=%Rrc\n", rc));
|
---|
373 | }
|
---|
374 |
|
---|
375 | kfree (sf_r);
|
---|
376 | sf_i->file = NULL;
|
---|
377 | file->private_data = NULL;
|
---|
378 | return 0;
|
---|
379 | }
|
---|
380 |
|
---|
381 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
382 | static int
|
---|
383 | sf_reg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
---|
384 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
385 | static struct page *
|
---|
386 | sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int *type)
|
---|
387 | # define SET_TYPE(t) *type = (t)
|
---|
388 | #else /* LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0) */
|
---|
389 | static struct page *
|
---|
390 | sf_reg_nopage (struct vm_area_struct *vma, unsigned long vaddr, int unused)
|
---|
391 | # define SET_TYPE(t)
|
---|
392 | #endif
|
---|
393 | {
|
---|
394 | struct page *page;
|
---|
395 | char *buf;
|
---|
396 | loff_t off;
|
---|
397 | uint32_t nread = PAGE_SIZE;
|
---|
398 | int err;
|
---|
399 | struct file *file = vma->vm_file;
|
---|
400 | struct inode *inode = file->f_dentry->d_inode;
|
---|
401 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
402 | struct sf_reg_info *sf_r = file->private_data;
|
---|
403 |
|
---|
404 | TRACE ();
|
---|
405 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
406 | if (vmf->pgoff > vma->vm_end)
|
---|
407 | return VM_FAULT_SIGBUS;
|
---|
408 | #else
|
---|
409 | if (vaddr > vma->vm_end) {
|
---|
410 | SET_TYPE (VM_FAULT_SIGBUS);
|
---|
411 | return NOPAGE_SIGBUS;
|
---|
412 | }
|
---|
413 | #endif
|
---|
414 |
|
---|
415 | page = alloc_page (GFP_HIGHUSER);
|
---|
416 | if (!page) {
|
---|
417 | LogRelFunc(("failed to allocate page\n"));
|
---|
418 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
419 | return VM_FAULT_OOM;
|
---|
420 | #else
|
---|
421 | SET_TYPE (VM_FAULT_OOM);
|
---|
422 | return NOPAGE_OOM;
|
---|
423 | #endif
|
---|
424 | }
|
---|
425 |
|
---|
426 | buf = kmap (page);
|
---|
427 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
428 | off = (vmf->pgoff << PAGE_SHIFT);
|
---|
429 | #else
|
---|
430 | off = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
|
---|
431 | #endif
|
---|
432 | err = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
|
---|
433 | if (err) {
|
---|
434 | kunmap (page);
|
---|
435 | put_page (page);
|
---|
436 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
437 | return VM_FAULT_SIGBUS;
|
---|
438 | #else
|
---|
439 | SET_TYPE (VM_FAULT_SIGBUS);
|
---|
440 | return NOPAGE_SIGBUS;
|
---|
441 | #endif
|
---|
442 | }
|
---|
443 |
|
---|
444 | BUG_ON (nread > PAGE_SIZE);
|
---|
445 | if (!nread) {
|
---|
446 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
447 | clear_user_page (page_address (page), vmf->pgoff, page);
|
---|
448 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
449 | clear_user_page (page_address (page), vaddr, page);
|
---|
450 | #else
|
---|
451 | clear_user_page (page_address (page), vaddr);
|
---|
452 | #endif
|
---|
453 | }
|
---|
454 | else {
|
---|
455 | memset (buf + nread, 0, PAGE_SIZE - nread);
|
---|
456 | }
|
---|
457 |
|
---|
458 | flush_dcache_page (page);
|
---|
459 | kunmap (page);
|
---|
460 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
461 | vmf->page = page;
|
---|
462 | return 0;
|
---|
463 | #else
|
---|
464 | SET_TYPE (VM_FAULT_MAJOR);
|
---|
465 | return page;
|
---|
466 | #endif
|
---|
467 | }
|
---|
468 |
|
---|
469 | static struct vm_operations_struct sf_vma_ops = {
|
---|
470 | #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 25)
|
---|
471 | .fault = sf_reg_fault
|
---|
472 | #else
|
---|
473 | .nopage = sf_reg_nopage
|
---|
474 | #endif
|
---|
475 | };
|
---|
476 |
|
---|
477 | static int
|
---|
478 | sf_reg_mmap (struct file *file, struct vm_area_struct *vma)
|
---|
479 | {
|
---|
480 | TRACE ();
|
---|
481 | if (vma->vm_flags & VM_SHARED) {
|
---|
482 | LogFunc(("shared mmapping not available\n"));
|
---|
483 | return -EINVAL;
|
---|
484 | }
|
---|
485 |
|
---|
486 | vma->vm_ops = &sf_vma_ops;
|
---|
487 | return 0;
|
---|
488 | }
|
---|
489 |
|
---|
490 | struct file_operations sf_reg_fops = {
|
---|
491 | .read = sf_reg_read,
|
---|
492 | .open = sf_reg_open,
|
---|
493 | .write = sf_reg_write,
|
---|
494 | .release = sf_reg_release,
|
---|
495 | .mmap = sf_reg_mmap,
|
---|
496 | #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
497 | # if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 23)
|
---|
498 | .splice_read = generic_file_splice_read,
|
---|
499 | # else
|
---|
500 | .sendfile = generic_file_sendfile,
|
---|
501 | # endif
|
---|
502 | .aio_read = generic_file_aio_read,
|
---|
503 | .aio_write = generic_file_aio_write,
|
---|
504 | .fsync = simple_sync_file,
|
---|
505 | .llseek = generic_file_llseek,
|
---|
506 | #endif
|
---|
507 | };
|
---|
508 |
|
---|
509 |
|
---|
510 | struct inode_operations sf_reg_iops = {
|
---|
511 | #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
|
---|
512 | .revalidate = sf_inode_revalidate
|
---|
513 | #else
|
---|
514 | .getattr = sf_getattr,
|
---|
515 | .setattr = sf_setattr
|
---|
516 | #endif
|
---|
517 | };
|
---|
518 |
|
---|
519 |
|
---|
520 | #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
|
---|
521 | static int
|
---|
522 | sf_readpage(struct file *file, struct page *page)
|
---|
523 | {
|
---|
524 | struct inode *inode = file->f_dentry->d_inode;
|
---|
525 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
526 | struct sf_reg_info *sf_r = file->private_data;
|
---|
527 | uint32_t nread = PAGE_SIZE;
|
---|
528 | char *buf;
|
---|
529 | loff_t off = ((loff_t)page->index) << PAGE_SHIFT;
|
---|
530 | int ret;
|
---|
531 |
|
---|
532 | TRACE ();
|
---|
533 |
|
---|
534 | buf = kmap(page);
|
---|
535 | ret = sf_reg_read_aux (__func__, sf_g, sf_r, buf, &nread, off);
|
---|
536 | if (ret) {
|
---|
537 | kunmap (page);
|
---|
538 | if (PageLocked(page))
|
---|
539 | unlock_page(page);
|
---|
540 | return ret;
|
---|
541 | }
|
---|
542 | BUG_ON (nread > PAGE_SIZE);
|
---|
543 | memset(&buf[nread], 0, PAGE_SIZE - nread);
|
---|
544 | flush_dcache_page (page);
|
---|
545 | kunmap (page);
|
---|
546 | SetPageUptodate(page);
|
---|
547 | unlock_page(page);
|
---|
548 | return 0;
|
---|
549 | }
|
---|
550 |
|
---|
551 | static int
|
---|
552 | sf_writepage(struct page *page, struct writeback_control *wbc)
|
---|
553 | {
|
---|
554 | struct address_space *mapping = page->mapping;
|
---|
555 | struct inode *inode = mapping->host;
|
---|
556 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
557 | struct sf_inode_info *sf_i = GET_INODE_INFO (inode);
|
---|
558 | struct file *file = sf_i->file;
|
---|
559 | struct sf_reg_info *sf_r = file->private_data;
|
---|
560 | char *buf;
|
---|
561 | uint32_t nwritten = PAGE_SIZE;
|
---|
562 | int end_index = inode->i_size >> PAGE_SHIFT;
|
---|
563 | loff_t off = ((loff_t) page->index) << PAGE_SHIFT;
|
---|
564 | int err;
|
---|
565 |
|
---|
566 | TRACE ();
|
---|
567 |
|
---|
568 | if (page->index >= end_index)
|
---|
569 | nwritten = inode->i_size & (PAGE_SIZE-1);
|
---|
570 |
|
---|
571 | buf = kmap(page);
|
---|
572 |
|
---|
573 | err = sf_reg_write_aux (__func__, sf_g, sf_r, buf, &nwritten, off);
|
---|
574 | if (err < 0) {
|
---|
575 | ClearPageUptodate(page);
|
---|
576 | goto out;
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (off > inode->i_size)
|
---|
580 | inode->i_size = off;
|
---|
581 |
|
---|
582 | if (PageError(page))
|
---|
583 | ClearPageError(page);
|
---|
584 | err = 0;
|
---|
585 | out:
|
---|
586 | kunmap(page);
|
---|
587 |
|
---|
588 | unlock_page(page);
|
---|
589 | return err;
|
---|
590 | }
|
---|
591 |
|
---|
592 | # if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
|
---|
593 | int
|
---|
594 | sf_write_begin(struct file *file, struct address_space *mapping, loff_t pos,
|
---|
595 | unsigned len, unsigned flags, struct page **pagep, void **fsdata)
|
---|
596 | {
|
---|
597 | TRACE ();
|
---|
598 |
|
---|
599 | return simple_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
|
---|
600 | }
|
---|
601 |
|
---|
602 | int
|
---|
603 | sf_write_end(struct file *file, struct address_space *mapping, loff_t pos,
|
---|
604 | unsigned len, unsigned copied, struct page *page, void *fsdata)
|
---|
605 | {
|
---|
606 | struct inode *inode = mapping->host;
|
---|
607 | struct sf_glob_info *sf_g = GET_GLOB_INFO (inode->i_sb);
|
---|
608 | struct sf_reg_info *sf_r = file->private_data;
|
---|
609 | void *buf;
|
---|
610 | unsigned from = pos & (PAGE_SIZE - 1);
|
---|
611 | uint32_t nwritten = len;
|
---|
612 | int err;
|
---|
613 |
|
---|
614 | TRACE ();
|
---|
615 |
|
---|
616 | buf = kmap(page);
|
---|
617 | err = sf_reg_write_aux (__func__, sf_g, sf_r, buf+from, &nwritten, pos);
|
---|
618 | kunmap(page);
|
---|
619 |
|
---|
620 | if (!PageUptodate(page) && err == PAGE_SIZE)
|
---|
621 | SetPageUptodate(page);
|
---|
622 |
|
---|
623 | if (err >= 0) {
|
---|
624 | pos += nwritten;
|
---|
625 | if (pos > inode->i_size)
|
---|
626 | inode->i_size = pos;
|
---|
627 | }
|
---|
628 |
|
---|
629 | unlock_page(page);
|
---|
630 | page_cache_release(page);
|
---|
631 |
|
---|
632 | return nwritten;
|
---|
633 | }
|
---|
634 |
|
---|
635 | # endif /* KERNEL_VERSION >= 2.6.24 */
|
---|
636 |
|
---|
637 | struct address_space_operations sf_reg_aops = {
|
---|
638 | .readpage = sf_readpage,
|
---|
639 | .writepage = sf_writepage,
|
---|
640 | # if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 24)
|
---|
641 | .write_begin = sf_write_begin,
|
---|
642 | .write_end = sf_write_end,
|
---|
643 | # else
|
---|
644 | .prepare_write = simple_prepare_write,
|
---|
645 | .commit_write = simple_commit_write,
|
---|
646 | # endif
|
---|
647 | };
|
---|
648 | #endif
|
---|