Whamcloud - gitweb
a working file system!
[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 /* SYNCHRONOUS I/O for an inode */
38 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
39 {
40         obd_count        num_obdo = 1;
41         obd_count        bufs_per_obdo = 1;
42         struct obdo     *oa;
43         char            *buf = (char *)page_address(page);
44         obd_size         count = PAGE_SIZE;
45         obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
46         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
47         int              err;
48
49         ENTRY;
50         if (IOPS(inode, brw) == NULL) {
51                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
52                 EXIT;
53                 return -EIO;
54         }
55
56         oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
57         if ( IS_ERR(oa) ) {
58                 EXIT;
59                 return PTR_ERR(oa);
60         }
61         obdfs_from_inode(oa, inode);
62
63         err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
64                                &buf, &count, &offset, &flags);
65
66         if ( !err )
67                 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
68
69         obdo_free(oa);
70         
71         EXIT;
72         return err;
73 } /* obdfs_brw */
74
75 /* returns the page unlocked, but with a reference */
76 int obdfs_readpage(struct file *file, struct page *page)
77 {
78         struct dentry *dentry = file->f_dentry;
79         struct inode *inode = dentry->d_inode;
80         int rc;
81
82         ENTRY;
83         /* PDEBUG(page, "READ"); */
84         rc = obdfs_brw(READ, inode, page, 0);
85         if ( !rc ) {
86                 SetPageUptodate(page);
87                 obd_unlock_page(page);
88         } 
89         /* PDEBUG(page, "READ"); */
90         EXIT;
91         return rc;
92 } /* obdfs_readpage */
93
94 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
95
96 int obdfs_init_pgrqcache(void)
97 {
98         ENTRY;
99         if (obdfs_pgrq_cachep == NULL) {
100                 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
101                 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
102                                                       sizeof(struct obdfs_pgrq),
103                                                       0, SLAB_HWCACHE_ALIGN,
104                                                       NULL, NULL);
105                 if (obdfs_pgrq_cachep == NULL) {
106                         EXIT;
107                         return -ENOMEM;
108                 } else {
109                         CDEBUG(D_CACHE, "allocated cache at %p\n",
110                                obdfs_pgrq_cachep);
111                 }
112         } else {
113                 CDEBUG(D_CACHE, "using existing cache at %p\n",
114                        obdfs_pgrq_cachep);
115         }
116         EXIT;
117         return 0;
118 } /* obdfs_init_wreqcache */
119
120 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
121 {
122         --obdfs_cache_count;
123         CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
124                pgrq->rq_page, obdfs_cache_count);
125         list_del(&pgrq->rq_plist);
126         OBDClearCachePage(pgrq->rq_page);
127         kmem_cache_free(obdfs_pgrq_cachep, pgrq);
128 }
129
130 void obdfs_cleanup_pgrqcache(void)
131 {
132         ENTRY;
133         if (obdfs_pgrq_cachep != NULL) {
134                 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
135                        obdfs_pgrq_cachep, obdfs_cache_count);
136                 if (kmem_cache_destroy(obdfs_pgrq_cachep))
137                         printk(KERN_INFO __FUNCTION__
138                                ": unable to free all of cache\n");
139                 obdfs_pgrq_cachep = NULL;
140         } else
141                 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
142
143         EXIT;
144 } /* obdfs_cleanup_wreqcache */
145
146
147 /* called with the list lock held */
148 static struct page *obdfs_find_page_index(struct inode *inode,
149                                           unsigned long index)
150 {
151         struct list_head *page_list = obdfs_iplist(inode);
152         struct list_head *tmp;
153         struct page *page;
154
155         ENTRY;
156
157         CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
158                inode->i_ino, index);
159         OIDEBUG(inode);
160
161         if (list_empty(page_list)) {
162                 EXIT;
163                 return NULL;
164         }
165         tmp = page_list;
166         while ( (tmp = tmp->next) != page_list ) {
167                 struct obdfs_pgrq *pgrq;
168
169                 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
170                 page = pgrq->rq_page;
171                 if (index == page->index) {
172                         CDEBUG(D_INFO,
173                                "INDEX SEARCH found page %p, index %ld\n",
174                                page, index);
175                         EXIT;
176                         return page;
177                 }
178         } 
179
180         EXIT;
181         return NULL;
182 } /* obdfs_find_page_index */
183
184
185 /* call and free pages from Linux page cache: called with io lock on inodes */
186 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
187                     obd_count num_obdos, struct obdo **obdos,
188                     obd_count *oa_bufs, struct page **pages, char **bufs,
189                     obd_size *counts, obd_off *offsets, obd_flag *flags)
190 {
191         int err;
192
193         ENTRY;
194         if (IOPS(inodes[0], brw) == NULL) {
195                 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
196                 EXIT;
197                 return -EIO;
198         }
199
200         CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
201                num_io, num_obdos);
202         if (obd_debug_level & D_INFO) { /* DEBUGGING */
203                 int i;
204                 printk("OBDOS: ");
205                 for (i = 0; i < num_obdos; i++)
206                         printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
207
208                 printk("\nPAGES: ");
209                 for (i = 0; i < num_io; i++)
210                         printk("0x%p ", pages[i]);
211                 printk("\n");
212         }
213
214         err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
215                                   oa_bufs, bufs, counts, offsets, flags);
216
217         CDEBUG(D_INFO, "BRW done\n");
218         /* release the pages from the page cache */
219         while ( num_io > 0 ) {
220                 --num_io;
221                 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
222                        pages[num_io], pages[num_io]->index);
223                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
224                 put_page(pages[num_io]);
225                 /* PDEBUG(pages[num_io], "do_vec_wr"); */
226         }
227         CDEBUG(D_INFO, "put_page done\n");
228
229         while ( num_obdos > 0) {
230                 --num_obdos;
231                 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
232                 /* copy o_blocks to i_blocks */
233                 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
234                 obdo_free(obdos[num_obdos]);
235         }
236         CDEBUG(D_INFO, "obdo_free done\n");
237         EXIT;
238         return err;
239 }
240
241
242 /*
243  * Add a page to the write request cache list for later writing.
244  * ASYNCHRONOUS write method.
245  */
246 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
247 {
248         int err = 0;
249         ENTRY;
250
251         /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
252          * is written, so at worst we will write the page out twice.
253          *
254          * If the page has the PG_obdcache bit set, then the inode MUST be
255          * on the superblock dirty list so we don't need to check this.
256          * Dirty inodes are removed from the superblock list ONLY when they
257          * don't have any more cached pages.  It is possible to have an inode
258          * with no dirty pages on the superblock list, but not possible to
259          * have an inode with dirty pages NOT on the superblock dirty list.
260          */
261         if (!OBDAddCachePage(page)) {
262                 struct obdfs_pgrq *pgrq;
263                 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
264                 if (!pgrq) {
265                         OBDClearCachePage(page);
266                         EXIT;
267                         return -ENOMEM;
268                 }
269                 /* not really necessary since we set all pgrq fields here
270                 memset(pgrq, 0, sizeof(*pgrq)); 
271                 */
272                 
273                 pgrq->rq_page = page;
274                 pgrq->rq_jiffies = jiffies;
275                 get_page(pgrq->rq_page);
276
277                 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
278                 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
279                 obdfs_cache_count++;
280
281                 /* If inode isn't already on superblock inodes list, add it.
282                  *
283                  * We increment the reference count on the inode to keep it
284                  * from being freed from memory.  This _should_ be an iget()
285                  * with an iput() in both flush_reqs() and put_inode(), but
286                  * since put_inode() is called from iput() we can't call iput()
287                  * again there.  Instead we just increment/decrement i_count,
288                  * which is mostly what iget/iput do for an inode in memory.
289                  */
290                 if ( list_empty(obdfs_islist(inode)) ) {
291                         atomic_inc(&inode->i_count);
292                         CDEBUG(D_INFO,
293                                "adding inode %ld to superblock list %p\n",
294                                inode->i_ino, obdfs_slist(inode));
295                         list_add(obdfs_islist(inode), obdfs_slist(inode));
296                 }
297                 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
298         }
299
300         /* XXX For testing purposes, we can write out the page here.
301         err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
302          */
303
304         EXIT;
305         return err;
306 } /* obdfs_add_page_to_cache */
307
308
309 /* select between SYNC and ASYNC I/O methods */
310 int obdfs_do_writepage(struct page *page, int sync)
311 {
312         struct inode *inode = page->mapping->host;
313         int err;
314
315         ENTRY;
316         /* PDEBUG(page, "WRITEPAGE"); */
317         if ( sync )
318                 err = obdfs_brw(WRITE, inode, page, 1);
319         else {
320                 err = obdfs_add_page_to_cache(inode, page);
321                 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
322                        inode->i_ino, page, err, Page_Uptodate(page));
323         }
324                 
325         if ( !err )
326                 SetPageUptodate(page);
327         /* PDEBUG(page,"WRITEPAGE"); */
328         EXIT;
329         return err;
330 } /* obdfs_do_writepage */
331
332 /* returns the page unlocked, but with a reference */
333 int obdfs_writepage(struct page *page)
334 {
335         return obdfs_do_writepage(page, 0);
336 }
337
338
339 /*
340  * This does the "real" work of the write. The generic routine has
341  * allocated the page, locked it, done all the page alignment stuff
342  * calculations etc. Now we should just copy the data from user
343  * space and write it back to the real medium..
344  *
345  * If the writer ends up delaying the write, the writer needs to
346  * increment the page use counts until he is done with the page.
347  *
348  * Return value is the number of bytes written.
349  */
350 int obdfs_write_one_page(struct file *file, struct page *page,
351                          unsigned long offset, unsigned long bytes,
352                          const char * buf)
353 {
354         struct inode *inode = file->f_dentry->d_inode;
355         int err;
356
357         ENTRY;
358         /* We check for complete page writes here, as we then don't have to
359          * get the page before writing over everything anyways.
360          */
361         if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
362                 err = obdfs_brw(READ, inode, page, 0);
363                 if ( err )
364                         return err;
365                 SetPageUptodate(page);
366         }
367
368         if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
369                 return -EFAULT;
370
371         lock_kernel();
372         err = obdfs_writepage(page);
373         unlock_kernel();
374
375         return (err < 0 ? err : bytes);
376 } /* obdfs_write_one_page */
377
378 /* 
379  * return an up to date page:
380  *  - if locked is true then is returned locked
381  *  - if create is true the corresponding disk blocks are created 
382  *  - page is held, i.e. caller must release the page
383  *
384  * modeled on NFS code.
385  */
386 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
387                            int create, int locked)
388 {
389         struct page * page;
390         int index;
391         int err;
392
393         ENTRY;
394
395         offset = offset & PAGE_CACHE_MASK;
396         CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
397                inode->i_ino, offset, create, locked);
398         index = offset >> PAGE_CACHE_SHIFT;
399
400         page = grab_cache_page(&inode->i_data, index);
401
402         /* Yuck, no page */
403         if (! page) {
404             printk(KERN_WARNING " grab_cache_page says no dice ...\n");
405             EXIT;
406             return NULL;
407         }
408
409         /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
410         /* now check if the data in the page is up to date */
411         if ( Page_Uptodate(page)) { 
412                 if (!locked) {
413                         if (PageLocked(page))
414                                 obd_unlock_page(page);
415                 } else {
416                         printk("file %s, line %d: expecting locked page\n",
417                                __FILE__, __LINE__); 
418                 }
419                 EXIT;
420                 return page;
421         } 
422
423
424 #ifdef EXT2_OBD_DEBUG
425         if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
426                 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
427                        page, page->index);
428         }
429 #endif
430
431         err = obdfs_brw(READ, inode, page, create);
432
433         if ( err ) {
434                 SetPageError(page);
435                 obd_unlock_page(page);
436                 EXIT;
437                 return page;
438         }
439
440         if ( !locked )
441                 obd_unlock_page(page);
442         SetPageUptodate(page);
443         /* PDEBUG(page,"GETPAGE - after reading"); */
444         EXIT;
445         return page;
446 } /* obdfs_getpage */
447
448
449 void obdfs_truncate(struct inode *inode)
450 {
451         struct obdo *oa;
452         int err;
453         ENTRY;
454
455         obdfs_dequeue_pages(inode);
456
457         if (IOPS(inode, punch) == NULL) {
458                 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
459                 EXIT;
460                 return;
461         }
462         oa = obdo_alloc();
463         if ( !oa ) {
464                 /* XXX This would give an inconsistent FS, so deal with it as
465                  * best we can for now - an obdo on the stack is not pretty.
466                  */
467                 struct obdo obdo;
468
469                 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
470
471                 obdo.o_valid = OBD_MD_FLNOTOBD;
472                 obdfs_from_inode(&obdo, inode);
473
474                 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
475         } else {
476                 oa->o_valid = OBD_MD_FLNOTOBD;
477                 obdfs_from_inode(oa, inode);
478
479                 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
480                        (long)oa->o_id, oa->o_size);
481                 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
482
483                 obdo_free(oa);
484         }
485
486         if (err) {
487                 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
488                 EXIT;
489                 return;
490         }
491         EXIT;
492 } /* obdfs_truncate */