Whamcloud - gitweb
obdsyncd: removed from CVS
[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         if ( !err )
58                 obdo_to_inode(inode, obdo); /* copy o_blocks to i_blocks */
59
60         obdo_free(obdo);
61         
62         EXIT;
63         return err;
64 } /* obdfs_brw */
65
66 /* returns the page unlocked, but with a reference */
67 int obdfs_readpage(struct dentry *dentry, struct page *page)
68 {
69         struct inode *inode = dentry->d_inode;
70         int rc;
71
72         ENTRY;
73         PDEBUG(page, "READ");
74         rc = obdfs_brw(READ, inode, page, 0);
75         if ( !rc ) {
76                 SetPageUptodate(page);
77                 UnlockPage(page);
78         } 
79         PDEBUG(page, "READ");
80         EXIT;
81         return rc;
82 } /* obdfs_readpage */
83
84 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
85
86 int obdfs_init_pgrqcache(void)
87 {
88         ENTRY;
89         if (obdfs_pgrq_cachep == NULL) {
90                 CDEBUG(D_INODE, "allocating obdfs_pgrq_cache\n");
91                 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
92                                                       sizeof(struct obdfs_pgrq),
93                                                       0, SLAB_HWCACHE_ALIGN,
94                                                       NULL, NULL);
95                 if (obdfs_pgrq_cachep == NULL) {
96                         EXIT;
97                         return -ENOMEM;
98                 } else {
99                         CDEBUG(D_INODE, "allocated cache at %p\n",
100                                obdfs_pgrq_cachep);
101                 }
102         } else {
103                 CDEBUG(D_INODE, "using existing cache at %p\n",
104                        obdfs_pgrq_cachep);
105         }
106         EXIT;
107         return 0;
108 } /* obdfs_init_wreqcache */
109
110 void obdfs_cleanup_pgrqcache(void)
111 {
112         ENTRY;
113         if (obdfs_pgrq_cachep != NULL) {
114                 CDEBUG(D_INODE, "destroying obdfs_pgrqcache at %p\n",
115                        obdfs_pgrq_cachep);
116                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
117                         printk(KERN_INFO "obd_cleanup_pgrqcache: unable to free all of cache\n");
118         } else
119                 printk(KERN_INFO "obd_cleanup_pgrqcache: called with NULL cache pointer\n");
120
121         EXIT;
122 } /* obdfs_cleanup_wreqcache */
123
124
125 /*
126  * Find a specific page in the page cache.  If it is found, we return
127  * the write request struct associated with it, if not found return NULL.
128  */
129 static struct obdfs_pgrq *
130 obdfs_find_in_page_cache(struct inode *inode, struct page *page)
131 {
132         struct list_head *page_list = &OBDFS_LIST(inode);
133         struct list_head *tmp;
134         struct obdfs_pgrq *pgrq;
135
136         ENTRY;
137         CDEBUG(D_INODE, "looking for inode %ld page %p\n", inode->i_ino, page);
138         if (list_empty(page_list)) {
139                 CDEBUG(D_INODE, "empty list\n");
140                 EXIT;
141                 return NULL;
142         }
143         tmp = page_list;
144         while ( (tmp = tmp->next) != page_list ) {
145                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_list);
146                 CDEBUG(D_INODE, "checking page %p\n", pgrq->rq_page);
147                 if (pgrq->rq_page == page) {
148                         CDEBUG(D_INODE, "found page %p in list\n", page);
149                         EXIT;
150                         return pgrq;
151                 }
152         } 
153
154         EXIT;
155         return NULL;
156 } /* obdfs_find_in_page_cache */
157
158
159 /*
160  * Remove a writeback request from a list
161  */
162 static inline int
163 obdfs_remove_from_page_cache(struct obdfs_pgrq *pgrq)
164 {
165         struct inode *inode = pgrq->rq_inode;
166         struct page *page = pgrq->rq_page;
167         int err;
168
169         ENTRY;
170         CDEBUG(D_INODE, "removing inode %ld page %p, pgrq: %p\n",
171                inode->i_ino, page, pgrq);
172         OIDEBUG(inode);
173         PDEBUG(page, "REM_CACHE");
174         err = obdfs_brw(WRITE, inode, page, 1);
175         /* XXX probably should handle error here somehow.  I think that
176          *     ext2 also does the same thing - discard write even if error?
177          */
178         put_page(page);
179         list_del(&pgrq->rq_list);
180         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
181         OIDEBUG(inode);
182
183         EXIT;
184         return err;
185 } /* obdfs_remove_from_page_cache */
186
187 /*
188  * Add a page to the write request cache list for later writing
189  * ASYNCHRONOUS write method.
190  */
191 static int obdfs_add_to_page_cache(struct inode *inode, struct page *page)
192 {
193         struct obdfs_pgrq *pgrq;
194
195         ENTRY;
196         pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
197         CDEBUG(D_INODE, "adding inode %ld page %p, pgrq: %p\n",
198                inode->i_ino, page, pgrq);
199         if (!pgrq) {
200                 EXIT;
201                 return -ENOMEM;
202         }
203         memset(pgrq, 0, sizeof(*pgrq)); 
204
205         pgrq->rq_page = page;
206         pgrq->rq_inode = inode;
207
208         get_page(pgrq->rq_page);
209         list_add(&pgrq->rq_list, &OBDFS_LIST(inode));
210
211         /* For testing purposes, we write out the page here.
212          * In the future, a flush daemon will write out the page.
213         return 0;
214          */
215         pgrq = obdfs_find_in_page_cache(inode, page);
216         if (!pgrq) {
217                 CDEBUG(D_INODE, "XXXX Can't find page after adding it!!!\n");
218                 EXIT;
219                 return -EINVAL;
220         } 
221                 
222         return obdfs_remove_from_page_cache(pgrq);
223 } /* obdfs_add_to_page_cache */
224
225
226 /* select between SYNC and ASYNC I/O methods */
227 int obdfs_do_writepage(struct inode *inode, struct page *page, int sync)
228 {
229         int err;
230
231         ENTRY;
232         PDEBUG(page, "WRITEPAGE");
233         if ( sync )
234                 err = obdfs_brw(WRITE, inode, page, 1);
235         else
236                 err = obdfs_add_to_page_cache(inode, page);
237                 
238         if ( !err )
239                 SetPageUptodate(page);
240         PDEBUG(page,"WRITEPAGE");
241         return err;
242 } /* obdfs_do_writepage */
243
244 /* returns the page unlocked, but with a reference */
245 int obdfs_writepage(struct dentry *dentry, struct page *page)
246 {
247         return obdfs_do_writepage(dentry->d_inode, page, 0);
248 }
249
250 /*
251  * This does the "real" work of the write. The generic routine has
252  * allocated the page, locked it, done all the page alignment stuff
253  * calculations etc. Now we should just copy the data from user
254  * space and write it back to the real medium..
255  *
256  * If the writer ends up delaying the write, the writer needs to
257  * increment the page use counts until he is done with the page.
258  *
259  * Return value is the number of bytes written.
260  */
261 int obdfs_write_one_page(struct file *file, struct page *page,
262                           unsigned long offset, unsigned long bytes,
263                           const char * buf)
264 {
265         struct inode *inode = file->f_dentry->d_inode;
266         int err;
267
268         ENTRY;
269         if ( !Page_Uptodate(page) ) {
270                 err = obdfs_brw(READ, inode, page, 1);
271                 if ( !err )
272                         SetPageUptodate(page);
273                 else
274                         return err;
275         }
276
277         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
278                 return -EFAULT;
279
280         lock_kernel();
281         err = obdfs_writepage(file->f_dentry, page);
282         unlock_kernel();
283
284         return (err < 0 ? err : bytes);
285 } /* obdfs_write_one_page */
286
287 /* 
288    return an up to date page:
289     - if locked is true then is returned locked
290     - if create is true the corresponding disk blocks are created 
291     - page is held, i.e. caller must release the page
292
293    modeled on NFS code.
294 */
295 struct page *obdfs_getpage(struct inode *inode, unsigned long offset, int create, int locked)
296 {
297         struct page *page_cache;
298         struct page ** hash;
299         struct page * page;
300         int err;
301
302         ENTRY;
303
304         offset = offset & PAGE_CACHE_MASK;
305         CDEBUG(D_INODE, "\n");
306         
307         page = NULL;
308         page_cache = page_cache_alloc();
309         if ( ! page_cache ) {
310                 EXIT;
311                 return NULL;
312         }
313         CDEBUG(D_INODE, "page_cache %p\n", page_cache);
314
315         hash = page_hash(&inode->i_data, offset);
316         page = grab_cache_page(&inode->i_data, offset);
317
318         /* Yuck, no page */
319         if (! page) {
320             printk("grab_cache_page says no dice ...\n");
321             EXIT;
322             return 0;
323         }
324
325         PDEBUG(page, "GETPAGE: got page - before reading\n");
326         /* now check if the data in the page is up to date */
327         if ( Page_Uptodate(page)) { 
328                 if (!locked)
329                         UnlockPage(page);
330                 EXIT;
331                 return page;
332         } 
333
334         err = obdfs_brw(READ, inode, page, create);
335
336         if ( err ) {
337                 SetPageError(page);
338                 UnlockPage(page);
339                 EXIT;
340                 return page;
341         }
342
343         if ( !locked )
344                 UnlockPage(page);
345         SetPageUptodate(page);
346         PDEBUG(page,"GETPAGE - after reading");
347         EXIT;
348         return page;
349 } /* obdfs_getpage */
350
351