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>
39 void obdfs_change_inode(struct inode *inode);
41 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated too */
42 static int obdfs_brw(int rw, struct inode *inode, struct page *page, int create)
44 obd_count num_obdo = 1;
45 obd_count bufs_per_obdo = 1;
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;
53 if (IOPS(inode, brw) == NULL) {
54 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
64 oa->o_valid = OBD_MD_FLNOTOBD;
65 obdfs_from_inode(oa, inode);
67 err = IOPS(inode, brw)(rw, IID(inode), num_obdo, &oa, &bufs_per_obdo,
68 &page, &count, &offset, &flags);
70 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 ( ((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT)
87 memset(kmap(page), 0, PAGE_CACHE_SIZE);
91 if (Page_Uptodate(page)) {
96 rc = obdfs_brw(READ, inode, page, 0);
101 /* PDEBUG(page, "READ"); */
104 SetPageUptodate(page);
105 obd_unlock_page(page);
108 } /* obdfs_readpage */
110 int obdfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
112 struct inode *inode = page->mapping->host;
113 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
118 /* PDEBUG(page, "READ"); */
119 if (Page_Uptodate(page)) {
124 if ( (from <= offset) && (to >= offset + PAGE_SIZE) ) {
129 rc = obdfs_brw(READ, inode, page, 0);
131 SetPageUptodate(page);
132 /* obd_unlock_page(page); */
134 /* PDEBUG(page, "READ"); */
142 static kmem_cache_t *obdfs_pgrq_cachep = NULL;
144 int obdfs_init_pgrqcache(void)
147 if (obdfs_pgrq_cachep == NULL) {
148 CDEBUG(D_CACHE, "allocating obdfs_pgrq_cache\n");
149 obdfs_pgrq_cachep = kmem_cache_create("obdfs_pgrq",
150 sizeof(struct obdfs_pgrq),
151 0, SLAB_HWCACHE_ALIGN,
153 if (obdfs_pgrq_cachep == NULL) {
157 CDEBUG(D_CACHE, "allocated cache at %p\n",
161 CDEBUG(D_CACHE, "using existing cache at %p\n",
166 } /* obdfs_init_wreqcache */
168 inline void obdfs_pgrq_del(struct obdfs_pgrq *pgrq)
171 CDEBUG(D_INFO, "deleting page %p from list [count %ld]\n",
172 pgrq->rq_page, obdfs_cache_count);
173 list_del(&pgrq->rq_plist);
174 OBDClearCachePage(pgrq->rq_page);
175 kmem_cache_free(obdfs_pgrq_cachep, pgrq);
178 void obdfs_cleanup_pgrqcache(void)
181 if (obdfs_pgrq_cachep != NULL) {
182 CDEBUG(D_CACHE, "destroying obdfs_pgrqcache at %p, count %ld\n",
183 obdfs_pgrq_cachep, obdfs_cache_count);
184 if (kmem_cache_destroy(obdfs_pgrq_cachep))
185 printk(KERN_INFO __FUNCTION__
186 ": unable to free all of cache\n");
187 obdfs_pgrq_cachep = NULL;
189 printk(KERN_INFO __FUNCTION__ ": called with NULL pointer\n");
192 } /* obdfs_cleanup_wreqcache */
195 /* called with the list lock held */
196 static struct page *obdfs_find_page_index(struct inode *inode,
199 struct list_head *page_list = obdfs_iplist(inode);
200 struct list_head *tmp;
205 CDEBUG(D_INFO, "looking for inode %ld pageindex %ld\n",
206 inode->i_ino, index);
209 if (list_empty(page_list)) {
214 while ( (tmp = tmp->next) != page_list ) {
215 struct obdfs_pgrq *pgrq;
217 pgrq = list_entry(tmp, struct obdfs_pgrq, rq_plist);
218 page = pgrq->rq_page;
219 if (index == page->index) {
221 "INDEX SEARCH found page %p, index %ld\n",
230 } /* obdfs_find_page_index */
233 /* call and free pages from Linux page cache: called with io lock on inodes */
234 int obdfs_do_vec_wr(struct inode **inodes, obd_count num_io,
235 obd_count num_obdos, struct obdo **obdos,
236 obd_count *oa_bufs, struct page **pages, char **bufs,
237 obd_size *counts, obd_off *offsets, obd_flag *flags)
242 if (IOPS(inodes[0], brw) == NULL) {
243 printk(KERN_ERR __FUNCTION__ ": no brw method!\n");
248 CDEBUG(D_INFO, "writing %d page(s), %d obdo(s) in vector\n",
250 if (obd_debug_level & D_INFO) { /* DEBUGGING */
253 for (i = 0; i < num_obdos; i++)
254 printk("%ld:0x%p ", (long)obdos[i]->o_id, obdos[i]);
257 for (i = 0; i < num_io; i++)
258 printk("0x%p ", pages[i]);
262 err = IOPS(inodes[0], brw)(WRITE, IID(inodes[0]), num_obdos, obdos,
263 oa_bufs, pages, counts, offsets, flags);
265 CDEBUG(D_INFO, "BRW done\n");
266 /* release the pages from the page cache */
267 while ( num_io > 0 ) {
269 CDEBUG(D_INFO, "calling put_page for %p, index %ld\n",
270 pages[num_io], pages[num_io]->index);
271 /* PDEBUG(pages[num_io], "do_vec_wr"); */
272 put_page(pages[num_io]);
273 /* PDEBUG(pages[num_io], "do_vec_wr"); */
275 CDEBUG(D_INFO, "put_page done\n");
277 while ( num_obdos > 0) {
279 CDEBUG(D_INFO, "free obdo %ld\n",(long)obdos[num_obdos]->o_id);
280 /* copy o_blocks to i_blocks */
281 obdfs_to_inode(inodes[num_obdos], obdos[num_obdos]);
282 obdo_free(obdos[num_obdos]);
284 CDEBUG(D_INFO, "obdo_free done\n");
291 * Add a page to the write request cache list for later writing.
292 * ASYNCHRONOUS write method.
294 static int obdfs_add_page_to_cache(struct inode *inode, struct page *page)
299 /* The PG_obdcache bit is cleared by obdfs_pgrq_del() BEFORE the page
300 * is written, so at worst we will write the page out twice.
302 * If the page has the PG_obdcache bit set, then the inode MUST be
303 * on the superblock dirty list so we don't need to check this.
304 * Dirty inodes are removed from the superblock list ONLY when they
305 * don't have any more cached pages. It is possible to have an inode
306 * with no dirty pages on the superblock list, but not possible to
307 * have an inode with dirty pages NOT on the superblock dirty list.
309 if (!OBDAddCachePage(page)) {
310 struct obdfs_pgrq *pgrq;
311 pgrq = kmem_cache_alloc(obdfs_pgrq_cachep, SLAB_KERNEL);
313 OBDClearCachePage(page);
317 /* not really necessary since we set all pgrq fields here
318 memset(pgrq, 0, sizeof(*pgrq));
321 pgrq->rq_page = page;
322 pgrq->rq_jiffies = jiffies;
323 get_page(pgrq->rq_page);
325 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
326 list_add(&pgrq->rq_plist, obdfs_iplist(inode));
329 /* If inode isn't already on superblock inodes list, add it.
331 * We increment the reference count on the inode to keep it
332 * from being freed from memory. This _should_ be an iget()
333 * with an iput() in both flush_reqs() and put_inode(), but
334 * since put_inode() is called from iput() we can't call iput()
335 * again there. Instead we just increment/decrement i_count,
336 * which is mostly what iget/iput do for an inode in memory.
338 if ( list_empty(obdfs_islist(inode)) ) {
339 atomic_inc(&inode->i_count);
341 "adding inode %ld to superblock list %p\n",
342 inode->i_ino, obdfs_slist(inode));
343 list_add(obdfs_islist(inode), obdfs_slist(inode));
345 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
348 /* XXX For testing purposes, we can write out the page here.
349 err = obdfs_flush_reqs(obdfs_slist(inode), ~0UL);
354 } /* obdfs_add_page_to_cache */
357 /* select between SYNC and ASYNC I/O methods */
358 int obdfs_do_writepage(struct page *page, int sync)
360 struct inode *inode = page->mapping->host;
364 /* PDEBUG(page, "WRITEPAGE"); */
366 err = obdfs_brw(WRITE, inode, page, 1);
368 err = obdfs_add_page_to_cache(inode, page);
369 CDEBUG(D_INFO, "DO_WR ino: %ld, page %p, err %d, uptodate %d\n",
370 inode->i_ino, page, err, Page_Uptodate(page));
374 SetPageUptodate(page);
375 /* PDEBUG(page,"WRITEPAGE"); */
378 } /* obdfs_do_writepage */
382 /* returns the page unlocked, but with a reference */
383 int obdfs_writepage(struct page *page)
385 return obdfs_do_writepage(page, 0);
388 int obdfs_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
391 struct inode *inode = page->mapping->host;
392 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
393 rc = obdfs_do_writepage(page, 0);
395 if (pos > inode->i_size) {
397 obdfs_change_inode(inode);
404 * This does the "real" work of the write. The generic routine has
405 * allocated the page, locked it, done all the page alignment stuff
406 * calculations etc. Now we should just copy the data from user
407 * space and write it back to the real medium..
409 * If the writer ends up delaying the write, the writer needs to
410 * increment the page use counts until he is done with the page.
412 * Return value is the number of bytes written.
414 int obdfs_write_one_page(struct file *file, struct page *page,
415 unsigned long offset, unsigned long bytes,
418 struct inode *inode = file->f_dentry->d_inode;
422 /* We check for complete page writes here, as we then don't have to
423 * get the page before writing over everything anyways.
425 if ( !Page_Uptodate(page) && (offset != 0 || bytes != PAGE_SIZE) ) {
426 err = obdfs_brw(READ, inode, page, 0);
429 SetPageUptodate(page);
432 if (copy_from_user((u8*)page_address(page) + offset, buf, bytes))
436 err = obdfs_writepage(page);
439 return (err < 0 ? err : bytes);
440 } /* obdfs_write_one_page */
443 * return an up to date page:
444 * - if locked is true then is returned locked
445 * - if create is true the corresponding disk blocks are created
446 * - page is held, i.e. caller must release the page
448 * modeled on NFS code.
450 struct page *obdfs_getpage(struct inode *inode, unsigned long offset,
451 int create, int locked)
459 offset = offset & PAGE_CACHE_MASK;
460 CDEBUG(D_INFO, "ino: %ld, offset %ld, create %d, locked %d\n",
461 inode->i_ino, offset, create, locked);
462 index = offset >> PAGE_CACHE_SHIFT;
464 page = grab_cache_page(&inode->i_data, index);
468 printk(KERN_WARNING " grab_cache_page says no dice ...\n");
473 /* PDEBUG(page, "GETPAGE: got page - before reading\n"); */
474 /* now check if the data in the page is up to date */
475 if ( Page_Uptodate(page)) {
477 if (PageLocked(page))
478 obd_unlock_page(page);
480 printk("file %s, line %d: expecting locked page\n",
488 #ifdef EXT2_OBD_DEBUG
489 if ((obd_debug_level & D_INFO) && obdfs_find_page_index(inode, index)) {
490 CDEBUG(D_INFO, "OVERWRITE: found dirty page %p, index %ld\n",
495 err = obdfs_brw(READ, inode, page, create);
499 obd_unlock_page(page);
505 obd_unlock_page(page);
506 SetPageUptodate(page);
507 /* PDEBUG(page,"GETPAGE - after reading"); */
510 } /* obdfs_getpage */
513 void obdfs_truncate(struct inode *inode)
519 obdfs_dequeue_pages(inode);
521 if (IOPS(inode, punch) == NULL) {
522 printk(KERN_ERR __FUNCTION__ ": no punch method!\n");
528 /* XXX This would give an inconsistent FS, so deal with it as
529 * best we can for now - an obdo on the stack is not pretty.
533 printk(__FUNCTION__ ": obdo_alloc failed - using stack!\n");
535 obdo.o_valid = OBD_MD_FLNOTOBD;
536 obdfs_from_inode(&obdo, inode);
538 err = IOPS(inode, punch)(IID(inode), &obdo, obdo.o_size, 0);
540 oa->o_valid = OBD_MD_FLNOTOBD;
541 obdfs_from_inode(oa, inode);
543 CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
544 (long)oa->o_id, oa->o_size);
545 err = IOPS(inode, punch)(IID(inode), oa, oa->o_size, 0);
551 printk(__FUNCTION__ ": obd_truncate fails (%d)\n", err);
556 } /* obdfs_truncate */