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