Whamcloud - gitweb
f58704ba6d2a5c883127e55807d84212d67ec57a
[fs/lustre-release.git] / lustre / obdfs / rw.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
5  * Copryright (C) 1999 Stelias Computing Inc, 
6  *                (author Peter J. Braam <braam@stelias.com>)
7  * Copryright (C) 1999 Seagate Technology Inc.
8  */
9
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <linux/unistd.h>
19
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
22
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <asm/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <asm/segment.h>
28 #include <linux/mm.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp_lock.h>
31
32 #include <linux/obd_support.h>
33 #include <linux/obd_ext2.h>
34 #include <linux/obdfs.h>
35
36 int console_loglevel;
37
38 /* SYNCHRONOUS I/O for an inode */
39 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
40 {
41         struct obdo *obdo;
42         obd_size count = PAGE_SIZE;
43         int err;
44
45         ENTRY;
46         obdo = obdo_alloc();
47         if ( ! obdo ) {
48                 EXIT;
49                 return -ENOMEM;
50         }
51
52         obdo->o_id = inode->i_ino;
53
54         err = IOPS(inode, brw)(rw, IID(inode), obdo, (char *)page_address(page),
55                                &count, (page->index) >> PAGE_SHIFT, create);
56
57         obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
58         obdo_free(obdo);
59         
60         EXIT;
61         return err;
62 } /* obdfs_brw */
63
64 /* returns the page unlocked, but with a reference */
65 int obdfs_readpage(struct dentry *dentry, struct page *page)
66 {
67         struct inode *inode = dentry->d_inode;
68         int rc;
69
70         ENTRY;
71         PDEBUG(page, "READ");
72         rc = obdfs_brw(READ, inode, page, 0);
73         if ( !rc ) {
74                 SetPageUptodate(page);
75                 UnlockPage(page);
76         } 
77         PDEBUG(page, "READ");
78         EXIT;
79         return rc;
80 } /* obdfs_readpage */
81
82 static kmem_cache_t *obdfs_pgrq_cachep;
83
84 int obdfs_init_pgrqcache(void)
85 {
86         ENTRY;
87         obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
88                                               sizeof(struct obdfs_pgrq),
89                                               0, SLAB_HWCACHE_ALIGN,
90                                               NULL, NULL);
91         if (obdfs_pgrq_cachep == NULL) {
92                 EXIT;
93                 return -ENOMEM;
94         }
95
96         EXIT;
97         return 0;
98 } /* obdfs_init_wreqcache */
99
100 void obdfs_cleanup_pgrqcache(void)
101 {
102         if (obdfs_pgrq_cachep != NULL)
103                 kmem_cache_destroy(obdfs_pgrq_cachep);
104         obdfs_pgrq_cachep = NULL;
105         EXIT;
106 } /* obdfs_cleanup_wreqcache */
107
108
109 /*
110  * Find a specific page in the page cache.  If it is found, we return
111  * the write request struct associated with it, if not found return NULL.
112  */
113 static struct obdfs_pgrq *
114 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
115 {
116         struct list_head *page_list = &OBD_LIST(inode);
117         struct list_head *tmp;
118         struct obdfs_pgrq *pgrq;
119
120         ENTRY;
121         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
122         if (list_empty(page_list)) {
123                 CDEBUG(D_INODE, "empty list\n");
124                 EXIT;
125                 return NULL;
126         }
127         tmp = page_list;
128         while ( (tmp = tmp->next) != page_list ) {
129                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
130                 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
131                 if (pgrq->rq_page == page) {
132                         CDEBUG(D_INODE, "found page %p in list\n", page);
133                         EXIT;
134                         return pgrq;
135                 }
136         } 
137
138         EXIT;
139         return NULL;
140 } /* obdfs_find_in_page_cache */
141
142
143 /*
144  * Remove a writeback request from a list
145  */
146 static inline int
147 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
148 {
149         struct inode *inode = pgrq->rq_inode;
150         struct page *page = pgrq->rq_page;
151         int rc;
152
153         ENTRY;
154         CDEBUG(D_INODE, "removing inode %ld page %p, pgrq: %p\n",
155                inode->i_ino, page, pgrq);
156         rc = obdfs_brw(WRITE, inode, page, 1);
157         /* XXX probably should handle error here somehow.  I think that
158          *     ext2 also does the same thing - discard write even if error?
159          */
160         put_page(page);
161         list_del(&pgrq->rq_list);
162         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
163
164         EXIT;
165         return rc;
166 } /* obdfs_remove_from_page_cache */
167
168 /*
169  * Add a page to the write request cache list for later writing
170  */
171 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
172 {
173         struct obdfs_pgrq *pgrq;
174
175         ENTRY;
176         pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
177         CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
178                inode->i_ino, page, pgrq);
179         if (!pgrq) {
180                 EXIT;
181                 return -ENOMEM;
182         }
183         memset(pgrq, 0, sizeof(*pgrq)); 
184
185         pgrq->rq_page = page;
186         pgrq->rq_inode = inode;
187
188         get_page(pgrq->rq_page);
189         list_add(&pgrq->rq_list, &OBD_LIST(inode));
190
191         /* For testing purposes, we write out the page here.
192          * In the future, a flush daemon will write out the page.
193         return 0;
194          */
195         pgrq = obdfs_find_in_page_cache(inode, page);
196         if (!pgrq) {
197                 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
198                 EXIT;
199                 return -EINVAL;
200         } 
201                 
202         return obdfs_remove_from_page_cache(pgrq);
203 } /* obdfs_add_to_page_cache */
204
205
206 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
207 {
208         int err;
209
210         ENTRY;
211         PDEBUG(page, "WRITEPAGE");
212         if ( sync )
213                 err = obdfs_brw(WRITE, inode, page, 1);
214         else
215                 err = obdfs_add_to_page_cache(inode, page);
216                 
217         if ( !err )
218                 SetPageUptodate(page);
219         PDEBUG(page,"WRITEPAGE");
220         return err;
221 } /* obdfs_do_writepage */
222
223 /* returns the page unlocked, but with a reference */
224 int obdfs_writepage(struct dentry *dentry, struct page *page)
225 {
226         return obdfs_do_writepage(dentry->d_inode, page, 0);
227 }
228
229 /*
230  * This does the "real" work of the write. The generic routine has
231  * allocated the page, locked it, done all the page alignment stuff
232  * calculations etc. Now we should just copy the data from user
233  * space and write it back to the real medium..
234  *
235  * If the writer ends up delaying the write, the writer needs to
236  * increment the page use counts until he is done with the page.
237  */
238 int obdfs_write_one_page(struct file *file, struct page *page,
239                          unsigned long offset, unsigned long bytes,
240                          const char * buf)
241 {
242         struct inode *inode = file->f_dentry->d_inode;
243         int err;
244
245         ENTRY;
246         if ( !Page_Uptodate(page) ) {
247                 err = obdfs_brw(READ, inode, page, 1);
248                 if ( !err )
249                         SetPageUptodate(page);
250                 else
251                         return err;
252         }
253         bytes -= copy_from_user((u8*)page_address(page) + offset, buf, bytes);
254         err = -EFAULT;
255
256         if (bytes) {
257                 lock_kernel();
258                 err = obdfs_writepage(file->f_dentry, page);
259                 unlock_kernel();
260         }
261
262         return err;
263 } /* obdfs_write_one_page */
264
265 /* 
266    return an up to date page:
267     - if locked is true then is returned locked
268     - if create is true the corresponding disk blocks are created 
269     - page is held, i.e. caller must release the page
270
271    modeled on NFS code.
272 */
273 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
274 {
275         struct page *page_cache;
276         struct page ** hash;
277         struct page * page;
278         int err;
279
280         ENTRY;
281
282         offset = offset & PAGE_CACHE_MASK;
283         CDEBUG(D_INODE, "\n");
284         
285         page = NULL;
286         page_cache = page_cache_alloc();
287         if ( ! page_cache ) {
288                 EXIT;
289                 return NULL;
290         }
291         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
292
293         hash = page_hash(&inode->i_data, offset);
294         page = grab_cache_page(&inode->i_data, offset);
295
296         /* Yuck, no page */
297         if (! page) {
298             printk("grab_cache_page says no dice ...\n");
299             EXIT;
300             return 0;
301         }
302
303         PDEBUG(page, "GETPAGE: got page - before reading\n");
304         /* now check if the data in the page is up to date */
305         if ( Page_Uptodate(page)) { 
306                 if (!locked)
307                         UnlockPage(page);
308                 EXIT;
309                 return page;
310         } 
311
312         err = obdfs_brw(READ, inode, page, create);
313
314         if ( err ) {
315                 SetPageError(page);
316                 UnlockPage(page);
317                 EXIT;
318                 return page;
319         }
320
321         if ( !locked )
322                 UnlockPage(page);
323         SetPageUptodate(page);
324         PDEBUG(page,"GETPAGE - after reading");
325         EXIT;
326         return page;
327 } /* obdfs_getpage */
328
329