Whamcloud - gitweb
b79ee7dcbde5fc095ca5e4b026ca055f5292cafa
[fs/lustre-release.git] / lustre / llite / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O Page Cache
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/fs.h>
37 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/writeback.h>
41 #else
42 #include <linux/iobuf.h>
43 #endif
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <asm/segment.h>
47 #include <linux/mm.h>
48 #include <linux/pagemap.h>
49 #include <linux/smp_lock.h>
50
51 #define DEBUG_SUBSYSTEM S_LLITE
52
53 #include <linux/lustre_mds.h>
54 #include <linux/lustre_lite.h>
55 #include "llite_internal.h"
56 #include <linux/lustre_compat25.h>
57
58 /*
59  * Remove page from dirty list
60  */
61 static void __set_page_clean(struct page *page)
62 {
63         struct address_space *mapping = page->mapping;
64         struct inode *inode;
65
66         if (!mapping)
67                 return;
68
69         PGCACHE_WRLOCK(mapping);
70
71         list_del(&page->list);
72         list_add(&page->list, &mapping->clean_pages);
73
74         /* XXX doesn't inode_lock protect i_state ? */
75         inode = mapping->host;
76         if (list_empty(&mapping->dirty_pages)) {
77                 CDEBUG(D_INODE, "inode clean\n");
78                 inode->i_state &= ~I_DIRTY_PAGES;
79         }
80
81         PGCACHE_WRUNLOCK(mapping);
82         EXIT;
83 }
84
85 void set_page_clean(struct page *page)
86 {
87         if (PageDirty(page)) {
88                 ClearPageDirty(page);
89                 __set_page_clean(page);
90         }
91 }
92
93 /* SYNCHRONOUS I/O to object storage for an inode */
94 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
95                   struct page *page, int flags)
96 {
97         struct ll_inode_info *lli = ll_i2info(inode);
98         struct lov_stripe_md *lsm = lli->lli_smd;
99         struct brw_page pg;
100         int rc;
101         ENTRY;
102
103         pg.pg = page;
104         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
105
106         if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
107                 pg.count = inode->i_size % PAGE_SIZE;
108         else
109                 pg.count = PAGE_SIZE;
110
111         CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
112                cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
113                pg.off, pg.off);
114         if (pg.count == 0) {
115                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
116                        LPU64"\n",
117                        inode->i_ino, inode, inode->i_size, page->mapping->host,
118                        page->mapping->host->i_size, page->index, pg.off);
119         }
120
121         pg.flag = flags;
122
123         if (cmd == OBD_BRW_WRITE)
124                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
125                                     LPROC_LL_BRW_WRITE, pg.count);
126         else
127                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
128                                     LPROC_LL_BRW_READ, pg.count);
129         rc = obd_brw(cmd, ll_i2obdconn(inode), oa, lsm, 1, &pg, NULL);
130         if (rc != 0 && rc != -EIO)
131                 CERROR("error from obd_brw: rc = %d\n", rc);
132
133         RETURN(rc);
134 }
135
136 /*
137  * we were asked to read a single page but we're going to try and read a batch
138  * of pages all at once.  this vaguely simulates 2.5's readpages.
139  */
140 static int ll_readpage(struct file *file, struct page *first_page)
141 {
142         struct inode *inode = first_page->mapping->host;
143         struct ll_inode_info *lli = ll_i2info(inode);
144         struct page *page = first_page;
145         struct list_head *pos;
146         struct brw_page *pgs;
147         struct obdo *oa;
148         unsigned long end_index, extent_end = 0;
149         struct ptlrpc_request_set *set;
150         int npgs = 0, rc = 0, max_pages;
151         ENTRY;
152
153         LASSERT(PageLocked(page));
154         LASSERT(!PageUptodate(page));
155         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
156                inode->i_ino, inode->i_generation, inode,
157                (((obd_off)page->index) << PAGE_SHIFT));
158         LASSERT(atomic_read(&file->f_dentry->d_inode->i_count) > 0);
159
160         if (inode->i_size <= ((obd_off)page->index) << PAGE_SHIFT) {
161                 CERROR("reading beyond EOF\n");
162                 memset(kmap(page), 0, PAGE_SIZE);
163                 kunmap(page);
164                 SetPageUptodate(page);
165                 unlock_page(page);
166                 RETURN(rc);
167         }
168
169         /* try to read the file's preferred block size in a one-er */
170         end_index = first_page->index +
171                 (inode->i_blksize >> PAGE_CACHE_SHIFT);
172         if (end_index > (inode->i_size >> PAGE_CACHE_SHIFT))
173                 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
174
175         max_pages = ((end_index - first_page->index) << PAGE_CACHE_SHIFT) >>
176                 PAGE_SHIFT;
177         OBD_ALLOC_GFP(pgs, max_pages * sizeof(*pgs), GFP_USER);
178         if (pgs == NULL)
179                 RETURN(-ENOMEM);
180
181         /*
182          * find how far we're allowed to read under the extent ll_file_read
183          * is passing us..
184          */
185         spin_lock(&lli->lli_read_extent_lock);
186         list_for_each(pos, &lli->lli_read_extents) {
187                 struct ll_read_extent *rextent;
188                 rextent = list_entry(pos, struct ll_read_extent, re_lli_item);
189                 if (rextent->re_task != current)
190                         continue;
191
192                 if (rextent->re_extent.end + PAGE_SIZE < rextent->re_extent.end)
193                         /* extent wrapping */
194                         extent_end = ~0;
195                 else {
196                         extent_end = (rextent->re_extent.end + PAGE_SIZE)
197                                                         << PAGE_CACHE_SHIFT;
198                         /* 32bit indexes, 64bit extents.. */
199                         if (((u64)extent_end >> PAGE_CACHE_SHIFT) <
200                                         rextent->re_extent.end)
201                                 extent_end = ~0;
202                 }
203                 break;
204         }
205         spin_unlock(&lli->lli_read_extent_lock);
206
207         if (extent_end == 0) {
208                 static long next_print;
209                 if (time_after(jiffies, next_print)) {
210                         next_print = jiffies + 30 * HZ;
211                         CDEBUG(D_INODE, "mmap readpage - check locks\n");
212                 }
213                 end_index = page->index + 1;
214         } else if (extent_end < end_index)
215                 end_index = extent_end;
216
217         CDEBUG(D_INFO, "max_pages: %d, extent_end: %lu, end_index: %lu, "
218                "i_size: "LPU64"\n",
219                max_pages, extent_end, end_index, inode->i_size);
220
221         /* to balance the find_get_page ref the other pages get that is
222          * decrefed on teardown.. */
223         page_cache_get(page);
224         do {
225                 unsigned long index ;
226
227                 pgs[npgs].pg = page;
228                 pgs[npgs].off = ((obd_off)page->index) << PAGE_CACHE_SHIFT;
229                 pgs[npgs].flag = 0;
230                 pgs[npgs].count = PAGE_SIZE;
231                 /* XXX Workaround for BA OSTs returning short reads at EOF.
232                  * The linux OST will return the full page, zero-filled at the
233                  * end, which will just overwrite the data we set here.  Bug
234                  * 593 relates to fixing this properly.
235                  */
236                 if (inode->i_size < pgs[npgs].off + PAGE_SIZE) {
237                         int count = inode->i_size - pgs[npgs].off;
238                         void *addr = kmap(page);
239                         pgs[npgs].count = count;
240                         //POISON(addr, 0x7c, count);
241                         memset(addr + count, 0, PAGE_SIZE - count);
242                         kunmap(page);
243                 }
244
245                 npgs++;
246                 if (npgs == max_pages)
247                         break;
248
249                 /*
250                  * find pages ahead of us that we can read in.
251                  * grab_cache_page waits on pages that are locked so
252                  * we first try find_get_page, which doesn't.  this stops
253                  * the worst case behaviour of racing threads waiting on
254                  * each other, but doesn't remove it entirely.
255                  */
256                 for (index = page->index + 1, page = NULL;
257                      page == NULL && index < end_index; index++) {
258
259                         /* see if the page already exists and needs updating */
260                         page = find_get_page(inode->i_mapping, index);
261                         if (page) {
262                                 if (Page_Uptodate(page) || TryLockPage(page))
263                                         goto out_release;
264                                 if (!page->mapping || Page_Uptodate(page))
265                                         goto out_unlock;
266                         } else {
267                                 /* ok, we have to create it.. */
268                                 page = grab_cache_page(inode->i_mapping, index);
269                                 if (page == NULL)
270                                         continue;
271                                 if (Page_Uptodate(page))
272                                         goto out_unlock;
273                         }
274
275                         break;
276
277                 out_unlock:
278                         unlock_page(page);
279                 out_release:
280                         page_cache_release(page);
281                         page = NULL;
282                 }
283
284         } while (page);
285
286         if ((oa = obdo_alloc()) == NULL) {
287                 CERROR("ENOMEM allocing obdo\n");
288                 rc = -ENOMEM;
289         } else if ((set = ptlrpc_prep_set()) == NULL) {
290                 CERROR("ENOMEM allocing request set\n");
291                 obdo_free(oa);
292                 rc = -ENOMEM;
293         } else {
294                 struct ll_file_data *fd = file->private_data;
295
296                 oa->o_id = lli->lli_smd->lsm_object_id;
297                 memcpy(obdo_handle(oa), &fd->fd_ost_och.och_fh,
298                        sizeof(fd->fd_ost_och.och_fh));
299                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
300                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME);
301
302                 rc = obd_brw_async(OBD_BRW_READ, ll_i2obdconn(inode), oa,
303                                    ll_i2info(inode)->lli_smd, npgs, pgs,
304                                    set, NULL);
305                 if (rc == 0)
306                         rc = ptlrpc_set_wait(set);
307                 ptlrpc_set_destroy(set);
308                 if (rc == 0)
309                         obdo_refresh_inode(inode, oa, oa->o_valid);
310                 if (rc && rc != -EIO)
311                         CERROR("error from obd_brw_async: rc = %d\n", rc);
312                 obdo_free(oa);
313         }
314
315         while (npgs-- > 0) {
316                 page = pgs[npgs].pg;
317
318                 if (rc == 0)
319                         SetPageUptodate(page);
320                 unlock_page(page);
321                 page_cache_release(page);
322         }
323
324         OBD_FREE(pgs, max_pages * sizeof(*pgs));
325         RETURN(rc);
326 } /* ll_readpage */
327
328 /* this isn't where truncate starts.   roughly:
329  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
330  * we grab the lock back in setattr_raw to avoid races. */
331 void ll_truncate(struct inode *inode)
332 {
333         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
334         struct obdo oa;
335         int err;
336         ENTRY;
337         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
338                inode->i_generation, inode);
339
340         /* object not yet allocated */
341         if (!lsm) {
342                 CERROR("truncate on inode %lu with no objects\n", inode->i_ino);
343                 EXIT;
344                 return;
345         }
346
347         /* vmtruncate will just throw away our dirty pages, make sure
348          * we don't think they're still dirty, being careful to round
349          * i_size to the first whole page that was tossed */
350         err = ll_clear_dirty_pages(ll_i2obdconn(inode), lsm,
351                         (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT,
352                         ~0);
353
354         oa.o_id = lsm->lsm_object_id;
355         oa.o_valid = OBD_MD_FLID;
356         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
357                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
358
359         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
360                oa.o_id, inode->i_size);
361
362         /* truncate == punch from new size to absolute end of file */
363         err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
364                         OBD_OBJECT_EOF, NULL);
365         if (err)
366                 CERROR("obd_truncate fails (%d) ino %lu\n", err, inode->i_ino);
367         else
368                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
369                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
370                                           OBD_MD_FLCTIME);
371
372         EXIT;
373         return;
374 } /* ll_truncate */
375
376 //#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
377
378 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
379                             unsigned to)
380 {
381         struct inode *inode = page->mapping->host;
382         struct ll_inode_info *lli = ll_i2info(inode);
383         struct ll_file_data *fd = file->private_data;
384         struct lov_stripe_md *lsm = lli->lli_smd;
385         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
386         struct brw_page pg;
387         struct obdo oa;
388         int rc = 0;
389         ENTRY;
390
391         if (!PageLocked(page))
392                 LBUG();
393
394         if (PageUptodate(page))
395                 RETURN(0);
396
397         //POISON(addr + from, 0xca, to - from);
398
399         /* Check to see if we should return -EIO right away */
400         pg.pg = page;
401         pg.off = offset;
402         pg.count = PAGE_SIZE;
403         pg.flag = 0;
404         rc = obd_brw(OBD_BRW_CHECK, ll_i2obdconn(inode), NULL, lsm, 1,&pg,NULL);
405         if (rc)
406                 RETURN(rc);
407
408         /* We're completely overwriting an existing page, so _don't_ set it up
409          * to date until commit_write */
410         if (from == 0 && to == PAGE_SIZE)
411                 RETURN(0);
412
413         /* If are writing to a new page, no need to read old data.
414          * the extent locking and getattr procedures in ll_file_write have
415          * guaranteed that i_size is stable enough for our zeroing needs */
416         if (inode->i_size <= offset) {
417                 memset(kmap(page), 0, PAGE_SIZE);
418                 kunmap(page);
419                 GOTO(prepare_done, rc = 0);
420         }
421
422         oa.o_id = lsm->lsm_object_id;
423         oa.o_mode = inode->i_mode;
424         memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
425                sizeof(fd->fd_ost_och.och_fh));
426         oa.o_valid = OBD_MD_FLID |OBD_MD_FLMODE |OBD_MD_FLTYPE |OBD_MD_FLHANDLE;
427
428         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
429         if (rc == 0)
430                 obdo_refresh_inode(inode, &oa, oa.o_valid);
431
432         EXIT;
433  prepare_done:
434         if (rc == 0)
435                 SetPageUptodate(page);
436
437         return rc;
438 }
439
440 /*
441  * background file writeback.  This is called regularly from kupdated to write
442  * dirty data, from kswapd when memory is low, and from filemap_fdatasync when
443  * super blocks or inodes are synced..
444  *
445  * obd_brw errors down in _batch_writepage are ignored, so pages are always
446  * unlocked.  Also, there is nobody to return an error code to from here - the
447  * application may not even be running anymore.
448  *
449  * this should be async so that things like kswapd can have a chance to
450  * free some more pages that our allocating writeback may need, but it isn't
451  * yet.
452  */
453 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
454 static unsigned long ll_local_cache_dirty_pages;
455 static unsigned long ll_max_dirty_pages = 20 * 1024 * 1024 / PAGE_SIZE;
456
457 static spinlock_t ll_local_cache_page_count_lock = SPIN_LOCK_UNLOCKED;
458
459 int ll_rd_dirty_pages(char *page, char **start, off_t off, int count, int *eof,
460                       void *data)
461 {
462         unsigned long dirty_count;
463         spin_lock(&ll_local_cache_page_count_lock);
464         dirty_count = ll_local_cache_dirty_pages;
465         spin_unlock(&ll_local_cache_page_count_lock);
466         return snprintf(page, count, "%lu\n", dirty_count);
467 }
468
469 int ll_rd_max_dirty_pages(char *page, char **start, off_t off, int count,
470                           int *eof, void *data)
471 {
472         unsigned long max_dirty;
473         spin_lock(&ll_local_cache_page_count_lock);
474         max_dirty = ll_max_dirty_pages;
475         spin_unlock(&ll_local_cache_page_count_lock);
476         return snprintf(page, count, "%lu\n", max_dirty);
477 }
478
479 int ll_wr_max_dirty_pages(struct file *file, const char *buffer,
480                           unsigned long count, void *data)
481 {
482         unsigned long max_dirty;
483         signed long max_dirty_signed;
484         char kernbuf[20], *end;
485         
486         if (count > (sizeof(kernbuf) - 1))
487                 return -EINVAL;
488
489         if (copy_from_user(kernbuf, buffer, count))
490                 return -EFAULT;
491
492         kernbuf[count] = '\0';
493
494         max_dirty_signed = simple_strtol(kernbuf, &end, 0);
495         if (kernbuf == end)
496                 return -EINVAL;
497         max_dirty = (unsigned long)max_dirty_signed;
498
499 #if 0
500         if (max_dirty < ll_local_cache_dirty_pages)
501                 flush_to_new_max_dirty();
502 #endif
503
504         spin_lock(&ll_local_cache_page_count_lock);
505         CDEBUG(D_CACHE, "changing max_dirty from %lu to %lu\n",
506                ll_max_dirty_pages, max_dirty);
507         ll_max_dirty_pages = max_dirty;
508         spin_unlock(&ll_local_cache_page_count_lock);
509         return count;
510 }
511
512 static int ll_local_cache_full(void)
513 {
514         int full = 0;
515         spin_lock(&ll_local_cache_page_count_lock);
516         if (ll_max_dirty_pages &&
517             ll_local_cache_dirty_pages >= ll_max_dirty_pages) {
518                 full = 1;
519         }
520         spin_unlock(&ll_local_cache_page_count_lock);
521         /* XXX instrument? */
522         /* XXX trigger async writeback when full, or 75% of full? */
523         return full;
524 }
525
526 static void ll_local_cache_flushed_pages(unsigned long pgcount)
527 {
528         unsigned long dirty_count;
529         spin_lock(&ll_local_cache_page_count_lock);
530         dirty_count = ll_local_cache_dirty_pages;
531         ll_local_cache_dirty_pages -= pgcount;
532         CDEBUG(D_CACHE, "dirty pages: %lu->%lu)\n",
533                dirty_count, ll_local_cache_dirty_pages);
534         spin_unlock(&ll_local_cache_page_count_lock);
535         LASSERT(dirty_count >= pgcount);
536 }
537
538 static void ll_local_cache_dirtied_pages(unsigned long pgcount)
539 {
540         unsigned long dirty_count;
541         spin_lock(&ll_local_cache_page_count_lock);
542         dirty_count = ll_local_cache_dirty_pages;
543         ll_local_cache_dirty_pages += pgcount;
544         CDEBUG(D_CACHE, "dirty pages: %lu->%lu\n",
545                dirty_count, ll_local_cache_dirty_pages);
546         spin_unlock(&ll_local_cache_page_count_lock);
547         /* XXX track maximum cached, report to lprocfs */
548 }
549
550 int ll_clear_dirty_pages(struct lustre_handle *conn, struct lov_stripe_md *lsm,
551                          unsigned long start, unsigned long end)
552 {
553         unsigned long cleared;
554         int rc;
555
556         ENTRY;
557         rc = obd_clear_dirty_pages(conn, lsm, start, end, &cleared);
558         if (!rc)
559                 ll_local_cache_flushed_pages(cleared);
560         RETURN(rc);
561 }
562
563 int ll_mark_dirty_page(struct lustre_handle *conn, struct lov_stripe_md *lsm,
564                        unsigned long index)
565 {
566         int rc;
567
568         ENTRY;
569         if (ll_local_cache_full())
570                 RETURN(-EDQUOT);
571
572         rc = obd_mark_page_dirty(conn, lsm, index);
573         if (!rc)
574                 ll_local_cache_dirtied_pages(1);
575         RETURN(rc);
576 }
577
578 static int ll_writepage(struct page *page)
579 {
580         struct inode *inode = page->mapping->host;
581         struct obdo oa;
582         ENTRY;
583
584         CDEBUG(D_CACHE, "page %p [lau %d] inode %p\n", page,
585                PageLaunder(page), inode);
586         LASSERT(PageLocked(page));
587
588         oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
589         oa.o_valid = OBD_MD_FLID;
590         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
591                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
592
593         RETURN(ll_batch_writepage(inode, &oa, page));
594 }
595
596 /*
597  * we really don't want to start writeback here, we want to give callers some
598  * time to further dirty the pages before we write them out.
599  */
600 static int ll_commit_write(struct file *file, struct page *page,
601                            unsigned from, unsigned to)
602 {
603         struct inode *inode = page->mapping->host;
604         loff_t size;
605         int rc = 0;
606         ENTRY;
607
608         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
609         LASSERT(inode == file->f_dentry->d_inode);
610         LASSERT(PageLocked(page));
611
612         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
613                inode, page, from, to, page->index);
614         if (!PageDirty(page)) {
615                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
616                                      LPROC_LL_DIRTY_MISSES);
617                 rc = ll_mark_dirty_page(ll_i2obdconn(inode),
618                                         ll_i2info(inode)->lli_smd,
619                                         page->index);
620                 if (rc < 0 && rc != -EDQUOT)
621                         RETURN(rc); /* XXX lproc counter here? */
622         } else {
623                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
624                                      LPROC_LL_DIRTY_HITS);
625         }
626
627         size = (((obd_off)page->index) << PAGE_SHIFT) + to;
628         if (size > inode->i_size)
629                 inode->i_size = size;
630
631         SetPageUptodate(page);
632         set_page_dirty(page);
633
634         /* This means that we've hit either the local cache limit or the limit
635          * of the OST's grant. */
636         if (rc == -EDQUOT) {
637                 struct ll_file_data *fd = file->private_data;
638                 struct obdo oa;
639                 int rc;
640
641                 oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
642                 memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
643                        sizeof(fd->fd_ost_och.och_fh));
644                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
645                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
646                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
647
648                 rc = ll_batch_writepage(inode, &oa, page);
649                 lock_page(page); /* caller expects to unlock */
650                 RETURN(rc);
651         }
652
653         RETURN(0);
654 } /* ll_commit_write */
655 #else
656 static int ll_writepage(struct page *page,
657                         struct writeback_control *wbc)
658 {
659
660         return 0;
661 }
662 static int ll_commit_write(struct file *file, struct page *page,
663                            unsigned from, unsigned to)
664 {
665         return 0;
666 }
667 #endif
668
669 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
670 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
671                         unsigned long blocknr, int blocksize)
672 {
673         struct ll_inode_info *lli = ll_i2info(inode);
674         struct lov_stripe_md *lsm = lli->lli_smd;
675         struct brw_page *pga;
676         struct ptlrpc_request_set *set;
677         struct obdo oa;
678         int length, i, flags, rc = 0;
679         loff_t offset;
680         ENTRY;
681
682         if (!lsm || !lsm->lsm_object_id)
683                 RETURN(-EBADF);
684
685         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 */
686         if ((iobuf->offset & (PAGE_SIZE - 1)) ||
687             (iobuf->length & (PAGE_SIZE - 1)))
688                 RETURN(-EINVAL);
689
690         set = ptlrpc_prep_set();
691         if (set == NULL)
692                 RETURN(-ENOMEM);
693
694         OBD_ALLOC(pga, sizeof(*pga) * iobuf->nr_pages);
695         if (!pga) {
696                 ptlrpc_set_destroy(set);
697                 RETURN(-ENOMEM);
698         }
699
700         flags = (rw == WRITE ? OBD_BRW_CREATE : 0) /* | OBD_BRW_DIRECTIO */;
701         offset = ((obd_off)blocknr << inode->i_blkbits);
702         length = iobuf->length;
703
704         for (i = 0, length = iobuf->length; length > 0;
705              length -= pga[i].count, offset += pga[i].count, i++) { /*i last!*/
706                 pga[i].pg = iobuf->maplist[i];
707                 pga[i].off = offset;
708                 /* To the end of the page, or the length, whatever is less */
709                 pga[i].count = min_t(int, PAGE_SIZE - (offset & ~PAGE_MASK),
710                                      length);
711                 pga[i].flag = flags;
712                 if (rw == READ) {
713                         //POISON(kmap(iobuf->maplist[i]), 0xc5, PAGE_SIZE);
714                         //kunmap(iobuf->maplist[i]);
715                 }
716         }
717
718         oa.o_id = lsm->lsm_object_id;
719         oa.o_valid = OBD_MD_FLID;
720         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
721                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
722
723         if (rw == WRITE)
724                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
725                                     LPROC_LL_DIRECT_WRITE, iobuf->length);
726         else
727                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
728                                     LPROC_LL_DIRECT_READ, iobuf->length);
729         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
730                            ll_i2obdconn(inode), &oa, lsm, iobuf->nr_pages, pga,
731                            set, NULL);
732         if (rc) {
733                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
734                        "error from obd_brw_async: rc = %d\n", rc);
735         } else {
736                 rc = ptlrpc_set_wait(set);
737                 if (rc)
738                         CERROR("error from callback: rc = %d\n", rc);
739         }
740         ptlrpc_set_destroy(set);
741         if (rc == 0)
742                 rc = iobuf->length;
743
744         OBD_FREE(pga, sizeof(*pga) * iobuf->nr_pages);
745         RETURN(rc);
746 }
747 #endif
748
749 //#endif
750
751 struct address_space_operations ll_aops = {
752         readpage: ll_readpage,
753 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
754         direct_IO: ll_direct_IO,
755 #endif
756         writepage: ll_writepage,
757         sync_page: block_sync_page,
758         prepare_write: ll_prepare_write,
759         commit_write: ll_commit_write,
760         bmap: NULL
761 //#endif
762 };