Whamcloud - gitweb
ff068524df04d55ec8fb79e6c3f7530ba970e79a
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1999 Stelias Computing Inc, 
5  *                (author Peter J. Braam <braam@stelias.com>)
6  * Copryright (C) 1999 Seagate Technology Inc.
7  */
8
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/unistd.h>
18
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <linux/vmalloc.h>
26 #include <asm/segment.h>
27 #include <linux/mm.h>
28 #include <linux/pagemap.h>
29 #include <linux/smp_lock.h>
30
31 #include <../obd/linux/obd_support.h>
32 #include <../obd/linux/obd_sim.h>
33 #include <obdfs.h>
34
35 /* VFS super_block ops */
36
37 /* returns the page unlocked, but with a reference */
38 int obdfs_readpage(struct file *file, struct page *page)
39 {
40         struct obdfs_sb_info *sbi;
41         struct super_block *sb = file->f_dentry->d_inode->i_sb;
42         int rc;
43
44         ENTRY;
45
46         /* XXX flush stuff */
47         sbi = sb->u.generic_sbp;
48         PDEBUG(page, "READ");
49         rc =  sbi->osi_ops->o_brw(READ, sbi->osi_conn_info.conn_id, 
50                       file->f_dentry->d_inode->i_ino, page, 0);
51         if (rc == PAGE_SIZE ) {
52                 SetPageUptodate(page);
53                 UnlockPage(page);
54         } 
55         PDEBUG(page, "READ");
56         if ( rc == PAGE_SIZE ) 
57                 rc = 0;
58         return rc;
59
60 }
61
62 /*
63  * This does the "real" work of the write. The generic routine has
64  * allocated the page, locked it, done all the page alignment stuff
65  * calculations etc. Now we should just copy the data from user
66  * space and write it back to the real medium..
67  *
68  * If the writer ends up delaying the write, the writer needs to
69  * increment the page use counts until he is done with the page.
70  */
71 int obdfs_write_one_page(struct file *file, struct page *page, unsigned long offset, unsigned long bytes, const char * buf)
72 {
73         long status;
74         struct obdfs_sb_info *sbi = file->f_dentry->d_inode->i_sb->u.generic_sbp;
75
76         if ( !Page_Uptodate(page) ) {
77                 status =  sbi->osi_ops->o_brw(READ, 
78                                               sbi->osi_conn_info.conn_id, 
79                                               file->f_dentry->d_inode->i_ino, 
80                                               page, 1);
81                 if (status == PAGE_SIZE ) {
82                         SetPageUptodate(page);
83                 } else { 
84                         return status;
85                 }
86         }
87         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
88         status = -EFAULT;
89
90         if (bytes) {
91                 lock_kernel();
92                 status = obdfs_writepage(file, page);
93                 unlock_kernel();
94         }
95         if ( status != PAGE_SIZE ) 
96                 return status;
97         else
98                 return bytes;
99 }
100
101
102
103
104 /* returns the page unlocked, but with a reference */
105 int obdfs_writepage(struct file *file, struct page *page)
106 {
107         struct obdfs_sb_info *sbi = file->f_dentry->d_inode->i_sb->u.generic_sbp;
108         int rc;
109
110         ENTRY;
111         PDEBUG(page, "WRITEPAGE");
112         /* XXX flush stuff */
113
114         rc = sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
115                       file->f_dentry->d_inode->i_ino, page, 1);
116         SetPageUptodate(page);
117         PDEBUG(page,"WRITEPAGE");
118         return rc;
119 }
120
121
122 /* 
123    page is returned unlocked, with the up to date flag set, 
124    and held, i.e. caller must do a page_put
125 */
126 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
127 {
128         unsigned long new_page;
129         struct page ** hash;
130         struct page * page; 
131         struct obdfs_sb_info *sbi;
132         struct super_block *sb = inode->i_sb;
133
134         ENTRY;
135
136         sbi = sb->u.generic_sbp;
137         
138         page = find_lock_page(inode, offset); 
139         if (page && Page_Uptodate(page)) { 
140                 PDEBUG(page,"GETPAGE");
141                 if (!locked)
142                         UnlockPage(page);
143                 return page;
144         } 
145                 
146         if (page && !Page_Uptodate(page) ) {
147                 CDEBUG(D_INODE, "Page found but not up to date\n");
148         }
149
150         /* page_cache_alloc returns address of page */
151         new_page = page_cache_alloc();
152         if (!new_page)
153                 return NULL;
154         
155         /* corresponding struct page in the mmap */
156         hash = page_hash(inode, offset);
157         page = page_cache_entry(new_page);
158         PDEBUG(page, "GETPAGE");
159         if (!add_to_page_cache_unique(page, inode, offset, hash)) {
160                 CDEBUG(D_INODE, "Page not found. Reading it.\n");
161                 PDEBUG(page,"GETPAGE");
162                 sbi->osi_ops->o_brw(READ, sbi->osi_conn_info.conn_id, 
163                                     inode->i_ino, page, create);
164                 if ( !locked )
165                         UnlockPage(page);
166                 SetPageUptodate(page);
167                 PDEBUG(page,"GETPAGE");
168                 return page;
169         }
170         /*
171          * We arrive here in the unlikely event that someone 
172          * raced with us and added our page to the cache first.
173          */
174         CDEBUG(D_INODE, "Page not found. Someone raced us.\n");
175         PDEBUG(page,"GETPAGE");
176         return page;
177 }
178
179
180
181 struct file_operations obdfs_file_ops = {
182         NULL,                   /* lseek - default */
183         generic_file_read,      /* read */
184         obdfs_file_write,     /* write - bad */
185         obdfs_readdir,          /* readdir */
186         NULL,                   /* poll - default */
187         NULL,                   /* ioctl */
188         NULL,                   /* mmap */
189         NULL,                   /* no special open code */
190         NULL,                   /* flush */
191         NULL,                   /* no special release code */
192         NULL,                   /* fsync */
193         NULL,                   /* fasync */
194         NULL,                   /* check_media_change */
195         NULL                    /* revalidate */
196 };
197
198 struct inode_operations obdfs_inode_ops = {
199         &obdfs_file_ops,        /* default directory file-ops */
200         obdfs_create,   /* create */
201         obdfs_lookup,   /* lookup */
202         obdfs_link,     /* link */
203         obdfs_unlink,   /* unlink */
204         obdfs_symlink,  /* symlink */
205         obdfs_mkdir,    /* mkdir */
206         obdfs_rmdir,    /* rmdir */
207         obdfs_mknod,    /* mknod */
208         obdfs_rename,   /* rename */
209         NULL,           /* readlink */
210         NULL,           /* follow_link */
211         NULL,           /* get_block */
212         obdfs_readpage, /* readpage */
213         obdfs_writepage, /* writepage */
214         NULL,           /* flushpage */
215         NULL,           /* truncate */
216         NULL,           /* permission */
217         NULL,           /* smap */
218         NULL            /* revalidate */
219 };