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