2 * OBDFS Super operations
4 * This code is issued under the GNU General Public License.
5 * See the file COPYING in this distribution
7 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
8 * Copryright (C) 1999 Stelias Computing Inc,
9 * (author Peter J. Braam <braam@stelias.com>)
10 * Copryright (C) 1999 Seagate Technology Inc.
14 #include <linux/config.h>
15 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/errno.h>
20 #include <linux/locks.h>
21 #include <linux/unistd.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
27 #include <linux/stat.h>
28 #include <asm/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <asm/segment.h>
32 #include <linux/pagemap.h>
33 #include <linux/smp_lock.h>
35 #include <linux/obd_support.h>
36 #include <linux/obd_ext2.h>
37 #include <linux/obdfs.h>
40 /* SYNCHRONOUS I/O for an inode */
41 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
43 obd_count num_obdo = 1;
44 obd_count bufs_per_obdo = 1;
46 obd_size count = PAGE_SIZE;
47 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
48 obd_flag flags = create ? OBD_BRW_CREATE : 0;
52 if (IOPS(inode, brw) == NULL) {
53 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
58 oa = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
63 obdfs_from_inode(oa, inode);
65 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
66 &page, &count, &offset, &flags);
69 obdfs_to_inode(inode, oa); /* copy o_blocks to i_blocks */
77 /* returns the page unlocked, but with a reference */
78 int obdfs_readpage(struct file *file, struct page *page)
80 struct inode *inode = page->mapping->host;
85 if (Page_Uptodate(page)) {
90 rc = obdfs_brw(READ, inode, page, 0);
95 /* PDEBUG(page, "READ"); */
98 SetPageUptodate(page);
99 obd_unlock_page(page);
102 } /* obdfs_readpage */
104 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
106 struct inode *inode = page->mapping->host;
107 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
112 /* PDEBUG(page, "READ"); */
113 if (Page_Uptodate(page)) {
118 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
123 rc = obdfs_brw(READ, inode, page, 0);
125 SetPageUptodate(page);
126 /* obd_unlock_page(page); */
128 /* PDEBUG(page, "READ"); */
136 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
138 int obdfs_init_pgrqcache(void)
141 if (obdfs_pgrq_cachep == NULL) {
142 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
143 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
144 sizeof(struct obdfs_pgrq),
145 0, SLAB_HWCACHE_ALIGN,
147 if (obdfs_pgrq_cachep == NULL) {
151 CDEBUG(D_CACHE, "allocated cache at %p\n",
155 CDEBUG(D_CACHE, "using existing cache at %p\n",
160 } /* obdfs_init_wreqcache */
162 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
165 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
166 pgrq->rq_page, obdfs_cache_count);
167 list_del(&pgrq->rq_plist);
168 OBDClearCachePage(pgrq->rq_page);
169 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
172 void obdfs_cleanup_pgrqcache(void)
175 if (obdfs_pgrq_cachep != NULL) {
176 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
177 obdfs_pgrq_cachep, obdfs_cache_count);
178 if (kmem_cache_destroy(obdfs_pgrq_cachep))
179 printk(KERN_INFO __FUNCTION__
180 ": unable to free all of cache\n");
181 obdfs_pgrq_cachep = NULL;
183 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
186 } /* obdfs_cleanup_wreqcache */
189 /* called with the list lock held */
190 static struct page *obdfs_find_page_index(struct inode *inode,
193 struct list_head *page_list = obdfs_iplist(inode);
194 struct list_head *tmp;
199 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
200 inode->i_ino, index);
203 if (list_empty(page_list)) {
208 while ( (tmp = tmp->next) != page_list ) {
209 struct obdfs_pgrq *pgrq;
211 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
212 page = pgrq->rq_page;
213 if (index == page->index) {
215 "INDEX SEARCH found page %p, index %ld\n",
224 } /* obdfs_find_page_index */
227 /* call and free pages from Linux page cache: called with io lock on inodes */
228 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
229 obd_count num_obdos, struct obdo **obdos,
230 obd_count *oa_bufs, struct page **pages, char **bufs,
231 obd_size *counts, obd_off *offsets, obd_flag *flags)
236 if (IOPS(inodes[0], brw) == NULL) {
237 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
242 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
244 if (obd_debug_level & D_INFO) { /* DEBUGGING */
247 for (i = 0; i < num_obdos; i++)
248 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
251 for (i = 0; i < num_io; i++)
252 printk("0x%p ", pages[i]);
256 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
257 oa_bufs, pages, counts, offsets, flags);
259 CDEBUG(D_INFO, "BRW done\n");
260 /* release the pages from the page cache */
261 while ( num_io > 0 ) {
263 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
264 pages[num_io], pages[num_io]->index);
265 /* PDEBUG(pages[num_io], "do_vec_wr"); */
266 put_page(pages[num_io]);
267 /* PDEBUG(pages[num_io], "do_vec_wr"); */
269 CDEBUG(D_INFO, "put_page done\n");
271 while ( num_obdos > 0) {
273 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
274 /* copy o_blocks to i_blocks */
275 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
276 obdo_free(obdos[num_obdos]);
278 CDEBUG(D_INFO, "obdo_free done\n");
285 * Add a page to the write request cache list for later writing.
286 * ASYNCHRONOUS write method.
288 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
293 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
294 * is written, so at worst we will write the page out twice.
296 * If the page has the PG_obdcache bit set, then the inode MUST be
297 * on the superblock dirty list so we don't need to check this.
298 * Dirty inodes are removed from the superblock list ONLY when they
299 * don't have any more cached pages. It is possible to have an inode
300 * with no dirty pages on the superblock list, but not possible to
301 * have an inode with dirty pages NOT on the superblock dirty list.
303 if (!OBDAddCachePage(page)) {
304 struct obdfs_pgrq *pgrq;
305 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
307 OBDClearCachePage(page);
311 /* not really necessary since we set all pgrq fields here
312 memset(pgrq, 0, sizeof(*pgrq));
315 pgrq->rq_page = page;
316 pgrq->rq_jiffies = jiffies;
317 get_page(pgrq->rq_page);
319 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
320 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
323 /* If inode isn't already on superblock inodes list, add it.
325 * We increment the reference count on the inode to keep it
326 * from being freed from memory. This _should_ be an iget()
327 * with an iput() in both flush_reqs() and put_inode(), but
328 * since put_inode() is called from iput() we can't call iput()
329 * again there. Instead we just increment/decrement i_count,
330 * which is mostly what iget/iput do for an inode in memory.
332 if ( list_empty(obdfs_islist(inode)) ) {
333 atomic_inc(&inode->i_count);
335 "adding inode %ld to superblock list %p\n",
336 inode->i_ino, obdfs_slist(inode));
337 list_add(obdfs_islist(inode), obdfs_slist(inode));
339 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
342 /* XXX For testing purposes, we can write out the page here.
343 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
348 } /* obdfs_add_page_to_cache */
351 /* select between SYNC and ASYNC I/O methods */
352 int obdfs_do_writepage(struct page *page, int sync)
354 struct inode *inode = page->mapping->host;
358 /* PDEBUG(page, "WRITEPAGE"); */
360 err = obdfs_brw(WRITE, inode, page, 1);
362 err = obdfs_add_page_to_cache(inode, page);
363 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
364 inode->i_ino, page, err, Page_Uptodate(page));
368 SetPageUptodate(page);
369 /* PDEBUG(page,"WRITEPAGE"); */
372 } /* obdfs_do_writepage */
376 /* returns the page unlocked, but with a reference */
377 int obdfs_writepage(struct page *page)
379 return obdfs_do_writepage(page, 0);
382 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
385 struct inode *inode = page->mapping->host;
386 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
387 rc = obdfs_writepage(page);
389 if (pos > inode->i_size) {
391 mark_inode_dirty(inode);
398 * This does the "real" work of the write. The generic routine has
399 * allocated the page, locked it, done all the page alignment stuff
400 * calculations etc. Now we should just copy the data from user
401 * space and write it back to the real medium..
403 * If the writer ends up delaying the write, the writer needs to
404 * increment the page use counts until he is done with the page.
406 * Return value is the number of bytes written.
408 int obdfs_write_one_page(struct file *file, struct page *page,
409 unsigned long offset, unsigned long bytes,
412 struct inode *inode = file->f_dentry->d_inode;
416 /* We check for complete page writes here, as we then don't have to
417 * get the page before writing over everything anyways.
419 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
420 err = obdfs_brw(READ, inode, page, 0);
423 SetPageUptodate(page);
426 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
430 err = obdfs_writepage(page);
433 return (err < 0 ? err : bytes);
434 } /* obdfs_write_one_page */
437 * return an up to date page:
438 * - if locked is true then is returned locked
439 * - if create is true the corresponding disk blocks are created
440 * - page is held, i.e. caller must release the page
442 * modeled on NFS code.
444 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
445 int create, int locked)
453 offset = offset & PAGE_CACHE_MASK;
454 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
455 inode->i_ino, offset, create, locked);
456 index = offset >> PAGE_CACHE_SHIFT;
458 page = grab_cache_page(&inode->i_data, index);
462 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
467 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
468 /* now check if the data in the page is up to date */
469 if ( Page_Uptodate(page)) {
471 if (PageLocked(page))
472 obd_unlock_page(page);
474 printk("file %s, line %d: expecting locked page\n",
482 #ifdef EXT2_OBD_DEBUG
483 if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
484 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
489 err = obdfs_brw(READ, inode, page, create);
493 obd_unlock_page(page);
499 obd_unlock_page(page);
500 SetPageUptodate(page);
501 /* PDEBUG(page,"GETPAGE - after reading"); */
504 } /* obdfs_getpage */
507 void obdfs_truncate(struct inode *inode)
513 obdfs_dequeue_pages(inode);
515 if (IOPS(inode, punch) == NULL) {
516 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
522 /* XXX This would give an inconsistent FS, so deal with it as
523 * best we can for now - an obdo on the stack is not pretty.
527 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
529 obdo.o_valid = OBD_MD_FLNOTOBD;
530 obdfs_from_inode(&obdo, inode);
532 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
534 oa->o_valid = OBD_MD_FLNOTOBD;
535 obdfs_from_inode(oa, inode);
537 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
538 (long)oa->o_id, oa->o_size);
539 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
545 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
550 } /* obdfs_truncate */