2 * OBDFS Super operations
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.
11 #include <linux/config.h>
12 #include <linux/kernel.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>
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
24 #include <linux/stat.h>
25 #include <asm/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <asm/segment.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp_lock.h>
32 #include <linux/obd_support.h>
33 #include <linux/obd_ext2.h>
34 #include <linux/obdfs.h>
37 /* SYNCHRONOUS I/O for an inode */
38 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
40 obd_count num_obdo = 1;
41 obd_count bufs_per_obdo = 1;
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;
50 if (IOPS(inode, brw) == NULL) {
51 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
56 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
61 obdfs_from_inode(oa, inode);
63 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
64 &buf, &count, &offset, &flags);
67 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
75 /* returns the page unlocked, but with a reference */
76 int obdfs_readpage(struct file *file, struct page *page)
78 struct dentry *dentry = file->f_dentry;
79 struct inode *inode = dentry->d_inode;
83 /* PDEBUG(page, "READ"); */
84 rc = obdfs_brw(READ, inode, page, 0);
86 SetPageUptodate(page);
87 obd_unlock_page(page);
89 /* PDEBUG(page, "READ"); */
92 } /* obdfs_readpage */
94 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
96 int obdfs_init_pgrqcache(void)
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,
105 if (obdfs_pgrq_cachep == NULL) {
109 CDEBUG(D_CACHE, "allocated cache at %p\n",
113 CDEBUG(D_CACHE, "using existing cache at %p\n",
118 } /* obdfs_init_wreqcache */
120 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
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);
130 void obdfs_cleanup_pgrqcache(void)
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;
141 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
144 } /* obdfs_cleanup_wreqcache */
147 /* called with the list lock held */
148 static struct page *obdfs_find_page_index(struct inode *inode,
151 struct list_head *page_list = obdfs_iplist(inode);
152 struct list_head *tmp;
157 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
158 inode->i_ino, index);
161 if (list_empty(page_list)) {
166 while ( (tmp = tmp->next) != page_list ) {
167 struct obdfs_pgrq *pgrq;
169 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
170 page = pgrq->rq_page;
171 if (index == page->index) {
173 "INDEX SEARCH found page %p, index %ld\n",
182 } /* obdfs_find_page_index */
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)
194 if (IOPS(inodes[0], brw) == NULL) {
195 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
200 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
202 if (obd_debug_level & D_INFO) { /* DEBUGGING */
205 for (i = 0; i < num_obdos; i++)
206 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
209 for (i = 0; i < num_io; i++)
210 printk("0x%p ", pages[i]);
214 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
215 oa_bufs, bufs, counts, offsets, flags);
217 CDEBUG(D_INFO, "BRW done\n");
218 /* release the pages from the page cache */
219 while ( num_io > 0 ) {
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"); */
227 CDEBUG(D_INFO, "put_page done\n");
229 while ( num_obdos > 0) {
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]);
236 CDEBUG(D_INFO, "obdo_free done\n");
243 * Add a page to the write request cache list for later writing.
244 * ASYNCHRONOUS write method.
246 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
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.
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.
261 if (!OBDAddCachePage(page)) {
262 struct obdfs_pgrq *pgrq;
263 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
265 OBDClearCachePage(page);
269 /* not really necessary since we set all pgrq fields here
270 memset(pgrq, 0, sizeof(*pgrq));
273 pgrq->rq_page = page;
274 pgrq->rq_jiffies = jiffies;
275 get_page(pgrq->rq_page);
277 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
278 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
281 /* If inode isn't already on superblock inodes list, add it.
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.
290 if ( list_empty(obdfs_islist(inode)) ) {
291 atomic_inc(&inode->i_count);
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));
297 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
300 /* XXX For testing purposes, we can write out the page here.
301 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
306 } /* obdfs_add_page_to_cache */
309 /* select between SYNC and ASYNC I/O methods */
310 int obdfs_do_writepage(struct page *page, int sync)
312 struct inode *inode = page->mapping->host;
316 /* PDEBUG(page, "WRITEPAGE"); */
318 err = obdfs_brw(WRITE, inode, page, 1);
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));
326 SetPageUptodate(page);
327 /* PDEBUG(page,"WRITEPAGE"); */
330 } /* obdfs_do_writepage */
332 /* returns the page unlocked, but with a reference */
333 int obdfs_writepage(struct page *page)
335 return obdfs_do_writepage(page, 0);
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..
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.
348 * Return value is the number of bytes written.
350 int obdfs_write_one_page(struct file *file, struct page *page,
351 unsigned long offset, unsigned long bytes,
354 struct inode *inode = file->f_dentry->d_inode;
358 /* We check for complete page writes here, as we then don't have to
359 * get the page before writing over everything anyways.
361 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
362 err = obdfs_brw(READ, inode, page, 0);
365 SetPageUptodate(page);
368 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
372 err = obdfs_writepage(page);
375 return (err < 0 ? err : bytes);
376 } /* obdfs_write_one_page */
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
384 * modeled on NFS code.
386 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
387 int create, int locked)
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;
400 page = grab_cache_page(&inode->i_data, index);
404 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
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)) {
413 if (PageLocked(page))
414 obd_unlock_page(page);
416 printk("file %s, line %d: expecting locked page\n",
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",
431 err = obdfs_brw(READ, inode, page, create);
435 obd_unlock_page(page);
441 obd_unlock_page(page);
442 SetPageUptodate(page);
443 /* PDEBUG(page,"GETPAGE - after reading"); */
446 } /* obdfs_getpage */
449 void obdfs_truncate(struct inode *inode)
455 obdfs_dequeue_pages(inode);
457 if (IOPS(inode, punch) == NULL) {
458 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
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.
469 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
471 obdo.o_valid = OBD_MD_FLNOTOBD;
472 obdfs_from_inode(&obdo, inode);
474 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
476 oa->o_valid = OBD_MD_FLNOTOBD;
477 obdfs_from_inode(oa, inode);
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);
487 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
492 } /* obdfs_truncate */