Import 2.1.81
[davej-history.git] / fs / fat / mmap.c
blob641c414a7ede029041e20a728186e8d3afcb0b6f
1 /*
2 * linux/fs/fat/mmap.c
4 * Written by Jacques Gelinas (jacques@solucorp.qc.ca)
5 * Inspired by fs/nfs/mmap.c (Jon Tombs 15 Aug 1993)
7 * mmap handling for fat-based filesystems
8 */
10 #define ASC_LINUX_VERSION(V, P, S) (((V) * 65536) + ((P) * 256) + (S))
11 #include <linux/version.h>
13 #include <linux/stat.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/shm.h>
18 #include <linux/errno.h>
19 #include <linux/mman.h>
20 #include <linux/string.h>
21 #include <linux/malloc.h>
22 #include <linux/msdos_fs.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
28 * Fill in the supplied page for mmap
30 static unsigned long fat_file_mmap_nopage(
31 struct vm_area_struct * area,
32 unsigned long address,
33 int error_code)
35 struct inode * inode = area->vm_dentry->d_inode;
36 unsigned long page;
37 unsigned int clear;
38 int pos;
39 long gap; /* distance from eof to pos */
41 page = __get_free_page(GFP_KERNEL);
42 if (!page)
43 return page;
44 address &= PAGE_MASK;
45 pos = address - area->vm_start + area->vm_offset;
47 clear = 0;
48 gap = inode->i_size - pos;
49 if (gap <= 0){
50 /* mmaping beyond end of file */
51 clear = PAGE_SIZE;
52 }else{
53 int cur_read;
54 int need_read;
55 struct file filp;
56 if (gap < PAGE_SIZE){
57 clear = PAGE_SIZE - gap;
59 filp.f_reada = 0;
60 filp.f_pos = pos;
61 need_read = PAGE_SIZE - clear;
63 mm_segment_t cur_fs = get_fs();
64 set_fs (KERNEL_DS);
65 cur_read = fat_file_read (&filp, (char*)page,
66 need_read, &filp.f_pos);
67 set_fs (cur_fs);
69 if (cur_read != need_read){
70 printk ("MSDOS: Error while reading an mmap file %d <> %d\n"
71 ,cur_read,need_read);
74 if (clear > 0){
75 memset ((char*)page+PAGE_SIZE-clear,0,clear);
77 return page;
80 struct vm_operations_struct fat_file_mmap = {
81 NULL, /* open */
82 NULL, /* close */
83 NULL, /* unmap */
84 NULL, /* protect */
85 NULL, /* sync */
86 NULL, /* advise */
87 fat_file_mmap_nopage, /* nopage */
88 NULL, /* wppage */
89 NULL, /* swapout */
90 NULL, /* swapin */
94 * This is used for a general mmap of an msdos file
95 * Returns 0 if ok, or a negative error code if not.
97 int fat_mmap(struct file * file, struct vm_area_struct * vma)
99 struct inode *inode = file->f_dentry->d_inode;
100 if (MSDOS_SB(inode->i_sb)->cvf_format &&
101 MSDOS_SB(inode->i_sb)->cvf_format->cvf_mmap)
102 return MSDOS_SB(inode->i_sb)->cvf_format->cvf_mmap(file,vma);
104 if (vma->vm_flags & VM_SHARED) /* only PAGE_COW or read-only supported now */
105 return -EINVAL;
106 if (vma->vm_offset & (inode->i_sb->s_blocksize - 1))
107 return -EINVAL;
108 if (!inode->i_sb || !S_ISREG(inode->i_mode))
109 return -EACCES;
110 if (!IS_RDONLY(inode)) {
111 inode->i_atime = CURRENT_TIME;
112 mark_inode_dirty(inode);
115 vma->vm_dentry = dget(file->f_dentry);
116 vma->vm_ops = &fat_file_mmap;
117 return 0;
121 int fat_readpage(struct dentry * dentry, struct page * page)
123 struct inode * inode = dentry->d_inode;
124 if (MSDOS_SB(inode->i_sb)->cvf_format &&
125 MSDOS_SB(inode->i_sb)->cvf_format->cvf_readpage)
126 return MSDOS_SB(inode->i_sb)->cvf_format->cvf_readpage(inode,page);
128 printk("fat_readpage called with no handler (shouldn't happen)\n");
129 return -1;