Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[fs/lustre-release.git] / lustre / obdfilter / filter_io.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/obdfilter/filter_io.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_FILTER
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/pagemap.h> // XXX kill me soon
32 #include <linux/version.h>
33
34 #include <linux/obd_class.h>
35 #include <linux/lustre_fsfilt.h>
36 #include "filter_internal.h"
37
38 static int filter_start_page_read(struct obd_device *obd, struct inode *inode,
39                                   struct niobuf_local *lnb)
40 {
41         struct page *page;
42         unsigned long index = lnb->offset >> PAGE_SHIFT;
43
44         page = fsfilt_getpage(obd, inode, index);
45         if (IS_ERR(page)) {
46                 CERROR("page index %lu, rc = %ld\n", index, PTR_ERR(page));
47
48                 lnb->page = NULL;
49                 lnb->rc = PTR_ERR(page);
50                 return lnb->rc;
51         }
52
53         lnb->page = page;
54
55         return 0;
56 }
57
58 static int filter_finish_page_read(struct niobuf_local *lnb)
59 {
60         if (lnb->page == NULL)
61                 return 0;
62
63         if (PageUptodate(lnb->page))
64                 return 0;
65
66         wait_on_page(lnb->page);
67         if (!PageUptodate(lnb->page)) {
68                 CERROR("page index %lu/offset "LPX64" not uptodate\n",
69                        lnb->page->index, lnb->offset);
70                 GOTO(err_page, lnb->rc = -EIO);
71         }
72         if (PageError(lnb->page)) {
73                 CERROR("page index %lu/offset "LPX64" has error\n",
74                        lnb->page->index, lnb->offset);
75                 GOTO(err_page, lnb->rc = -EIO);
76         }
77
78         return 0;
79
80 err_page:
81         page_cache_release(lnb->page);
82         lnb->page = NULL;
83         return lnb->rc;
84 }
85
86 /* Grab the dirty and seen grant announcements from the incoming obdo.
87  * We will later calculate the clients new grant and return it.
88  * Caller must hold osfs lock */
89 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
90 {
91         struct filter_export_data *fed;
92         struct obd_device *obd = exp->exp_obd;
93         static unsigned long last_msg;
94         static int last_count;
95         int mask = D_CACHE;
96         ENTRY;
97
98         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
99
100         if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
101                                         (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
102                 oa->o_valid &= ~OBD_MD_FLGRANT;
103                 EXIT;
104                 return;
105         }
106
107         fed = &exp->exp_filter_data;
108
109         /* Don't print this to the console the first time it happens, since
110          * it can happen legitimately on occasion, but only rarely. */
111         if (time_after(jiffies, last_msg + 60 * HZ)) {
112                 last_count = 0;
113                 last_msg = jiffies;
114         }
115         if ((last_count & (-last_count)) == last_count)
116                 mask = D_WARNING;
117         last_count++;
118
119         /* Add some margin, since there is a small race if other RPCs arrive
120          * out-or-order and have already consumed some grant.  We want to
121          * leave this here in case there is a large error in accounting. */
122         CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ? mask:D_CACHE,
123                "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
124                obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
125                oa->o_dropped, fed->fed_grant);
126
127         /* Update our accounting now so that statfs takes it into account.
128          * Note that fed_dirty is only approximate and can become incorrect
129          * if RPCs arrive out-of-order.  No important calculations depend
130          * on fed_dirty however. */
131         obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
132         if (fed->fed_grant < oa->o_dropped) {
133                 CERROR("%s: cli %s/%p reports %u dropped > fed_grant %lu\n",
134                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
135                        oa->o_dropped, fed->fed_grant);
136                 oa->o_dropped = 0;
137         }
138         if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
139                 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
140                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
141                        oa->o_dropped, obd->u.filter.fo_tot_granted);
142                 oa->o_dropped = 0;
143         }
144         obd->u.filter.fo_tot_granted -= oa->o_dropped;
145         fed->fed_grant -= oa->o_dropped;
146         fed->fed_dirty = oa->o_dirty;
147         EXIT;
148 }
149
150 #define GRANT_FOR_LLOG(obd) 16
151
152 /* Figure out how much space is available between what we've granted
153  * and what remains in the filesystem.  Compensate for ext3 indirect
154  * block overhead when computing how much free space is left ungranted.
155  *
156  * Caller must hold obd_osfs_lock. */
157 obd_size filter_grant_space_left(struct obd_export *exp)
158 {
159         struct obd_device *obd = exp->exp_obd;
160         int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
161         obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
162         int rc, statfs_done = 0;
163
164         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
165
166         if (time_before(obd->obd_osfs_age, jiffies - HZ)) {
167 restat:
168                 rc = fsfilt_statfs(obd, obd->u.filter.fo_sb, jiffies + 1);
169                 if (rc) /* N.B. statfs can't really fail */
170                         RETURN(0);
171                 statfs_done = 1;
172         }
173
174         avail = obd->obd_osfs.os_bavail;
175         left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
176         if (left > GRANT_FOR_LLOG(obd)) {
177                 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
178         } else {
179                 left = 0 /* << blockbits */;
180         }
181
182         if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
183                 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
184                 goto restat;
185         }
186
187         if (left >= tot_granted) {
188                 left -= tot_granted;
189         } else {
190                 static unsigned long next;
191                 if (left < tot_granted - obd->u.filter.fo_tot_pending &&
192                     time_after(jiffies, next)) {
193                         spin_unlock(&obd->obd_osfs_lock);
194                         CERROR("%s: cli %s/%p grant "LPU64" > available "
195                                LPU64" and pending "LPU64"\n", obd->obd_name,
196                                exp->exp_client_uuid.uuid, exp, tot_granted,
197                                left, obd->u.filter.fo_tot_pending);
198                         if (next == 0)
199                                 portals_debug_dumplog();
200                         next = jiffies + 20 * HZ;
201                         spin_lock(&obd->obd_osfs_lock);
202                 }
203                 left = 0;
204         }
205
206         CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
207                " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
208                exp->exp_client_uuid.uuid, exp,
209                obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
210                tot_granted, left, obd->u.filter.fo_tot_pending);
211
212         return left;
213 }
214
215 /* Calculate how much grant space to allocate to this client, based on how
216  * much space is currently free and how much of that is already granted.
217  *
218  * Caller must hold obd_osfs_lock. */
219 long filter_grant(struct obd_export *exp, obd_size current_grant,
220                   obd_size want, obd_size fs_space_left)
221 {
222         struct obd_device *obd = exp->exp_obd;
223         struct filter_export_data *fed = &exp->exp_filter_data;
224         int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
225         __u64 grant = 0;
226
227         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
228
229         /* Grant some fraction of the client's requested grant space so that
230          * they are not always waiting for write credits (not all of it to
231          * avoid overgranting in face of multiple RPCs in flight).  This
232          * essentially will be able to control the OSC_MAX_RIF for a client.
233          *
234          * If we do have a large disparity between what the client thinks it
235          * has and what we think it has, don't grant very much and let the
236          * client consume its grant first.  Either it just has lots of RPCs
237          * in flight, or it was evicted and its grants will soon be used up. */
238         if (current_grant < want &&
239             current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
240                 grant = min((want >> blockbits) / 2,
241                             (fs_space_left >> blockbits) / 8);
242                 grant <<= blockbits;
243
244                 if (grant) {
245                         if (grant > FILTER_GRANT_CHUNK)
246                                 grant = FILTER_GRANT_CHUNK;
247
248                         obd->u.filter.fo_tot_granted += grant;
249                         fed->fed_grant += grant;
250                 }
251         }
252
253         CDEBUG(D_CACHE,"%s: cli %s/%p wants: "LPU64" granting: "LPU64"\n",
254                obd->obd_name, exp->exp_client_uuid.uuid, exp, want, grant);
255         CDEBUG(D_CACHE,
256                "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
257                " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
258                exp, obd->u.filter.fo_tot_dirty,
259                obd->u.filter.fo_tot_granted, obd->obd_num_exports);
260
261         return grant;
262 }
263
264 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
265                               int objcount, struct obd_ioobj *obj,
266                               int niocount, struct niobuf_remote *nb,
267                               struct niobuf_local *res,
268                               struct obd_trans_info *oti)
269 {
270         struct obd_device *obd = exp->exp_obd;
271         struct lvfs_run_ctxt saved;
272         struct obd_ioobj *o;
273         struct niobuf_remote *rnb;
274         struct niobuf_local *lnb = NULL;
275         struct fsfilt_objinfo *fso;
276         struct dentry *dentry;
277         struct inode *inode;
278         int rc = 0, i, j, tot_bytes = 0, cleanup_phase = 0;
279         unsigned long now = jiffies;
280         ENTRY;
281
282         /* We are currently not supporting multi-obj BRW_READ RPCS at all.
283          * When we do this function's dentry cleanup will need to be fixed */
284         LASSERT(objcount == 1);
285         LASSERT(obj->ioo_bufcnt > 0);
286
287         if (oa && oa->o_valid & OBD_MD_FLGRANT) {
288                 spin_lock(&obd->obd_osfs_lock);
289                 filter_grant_incoming(exp, oa);
290
291 #if 0
292                 /* Reads do not increase grants */
293                 oa->o_grant = filter_grant(exp, oa->o_grant, oa->o_undirty,
294                                            filter_grant_space_left(exp));
295 #else
296                 oa->o_grant = 0;
297 #endif
298                 spin_unlock(&obd->obd_osfs_lock);
299         }
300
301         OBD_ALLOC(fso, objcount * sizeof(*fso));
302         if (fso == NULL)
303                 RETURN(-ENOMEM);
304
305         memset(res, 0, niocount * sizeof(*res));
306
307         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
308         for (i = 0, o = obj; i < objcount; i++, o++) {
309                 LASSERT(o->ioo_bufcnt);
310
311                 dentry = filter_oa2dentry(obd, oa);
312                 if (IS_ERR(dentry))
313                         GOTO(cleanup, rc = PTR_ERR(dentry));
314
315                 if (dentry->d_inode == NULL) {
316                         CERROR("trying to BRW to non-existent file "LPU64"\n",
317                                o->ioo_id);
318                         f_dput(dentry);
319                         GOTO(cleanup, rc = -ENOENT);
320                 }
321
322                 fso[i].fso_dentry = dentry;
323                 fso[i].fso_bufcnt = o->ioo_bufcnt;
324         }
325
326         if (time_after(jiffies, now + 15 * HZ))
327                 CERROR("slow preprw_read setup %lus\n", (jiffies - now) / HZ);
328         else
329                 CDEBUG(D_INFO, "preprw_read setup: %lu jiffies\n",
330                        (jiffies - now));
331
332         for (i = 0, o = obj, rnb = nb, lnb = res; i < objcount; i++, o++) {
333                 dentry = fso[i].fso_dentry;
334                 inode = dentry->d_inode;
335
336                 for (j = 0; j < o->ioo_bufcnt; j++, rnb++, lnb++) {
337                         lnb->dentry = dentry;
338                         lnb->offset = rnb->offset;
339                         lnb->len    = rnb->len;
340                         lnb->flags  = rnb->flags;
341
342                         if (inode->i_size <= rnb->offset) {
343                                 /* If there's no more data, abort early.
344                                  * lnb->page == NULL and lnb->rc == 0, so it's
345                                  * easy to detect later. */
346                                 break;
347                         } else {
348                                 rc = filter_start_page_read(obd, inode, lnb);
349                         }
350
351                         if (rc) {
352                                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
353                                        "page err %u@"LPU64" %u/%u %p: rc %d\n",
354                                        lnb->len, lnb->offset, j, o->ioo_bufcnt,
355                                        dentry, rc);
356                                 cleanup_phase = 1;
357                                 GOTO(cleanup, rc);
358                         }
359
360                         if (inode->i_size < lnb->offset + lnb->len - 1)
361                                 lnb->rc = inode->i_size - lnb->offset;
362                         else
363                                 lnb->rc = lnb->len;
364
365                         tot_bytes += lnb->rc;
366                         if (lnb->rc < lnb->len) {
367                                 /* short read, be sure to wait on it */
368                                 lnb++;
369                                 break;
370                         }
371                 }
372         }
373
374         if (time_after(jiffies, now + 15 * HZ))
375                 CERROR("slow start_page_read %lus\n", (jiffies - now) / HZ);
376         else
377                 CDEBUG(D_INFO, "start_page_read: %lu jiffies\n",
378                        (jiffies - now));
379
380         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
381         while (lnb-- > res) {
382                 rc = filter_finish_page_read(lnb);
383                 if (rc) {
384                         CERROR("error page %u@"LPU64" %u %p: rc %d\n", lnb->len,
385                                lnb->offset, (int)(lnb - res), lnb->dentry, rc);
386                         cleanup_phase = 1;
387                         GOTO(cleanup, rc);
388                 }
389         }
390
391         if (time_after(jiffies, now + 15 * HZ))
392                 CERROR("slow finish_page_read %lus\n", (jiffies - now) / HZ);
393         else
394                 CDEBUG(D_INFO, "finish_page_read: %lu jiffies\n",
395                        (jiffies - now));
396
397         filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
398
399         EXIT;
400
401  cleanup:
402         switch (cleanup_phase) {
403         case 1:
404                 for (lnb = res; lnb < (res + niocount); lnb++) {
405                         if (lnb->page)
406                                 page_cache_release(lnb->page);
407                 }
408                 if (res->dentry != NULL)
409                         f_dput(res->dentry);
410                 else
411                         CERROR("NULL dentry in cleanup -- tell CFS\n");
412         case 0:
413                 OBD_FREE(fso, objcount * sizeof(*fso));
414                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
415         }
416         return rc;
417 }
418
419 /* When clients have dirtied as much space as they've been granted they
420  * fall through to sync writes.  These sync writes haven't been expressed
421  * in grants and need to error with ENOSPC when there isn't room in the
422  * filesystem for them after grants are taken into account.  However,
423  * writeback of the dirty data that was already granted space can write
424  * right on through.
425  *
426  * Caller must hold obd_osfs_lock. */
427 static int filter_grant_check(struct obd_export *exp, int objcount,
428                               struct fsfilt_objinfo *fso, int niocount,
429                               struct niobuf_remote *rnb,
430                               struct niobuf_local *lnb, obd_size *left,
431                               struct inode *inode)
432 {
433         struct filter_export_data *fed = &exp->exp_filter_data;
434         int blocksize = exp->exp_obd->u.filter.fo_sb->s_blocksize;
435         unsigned long used = 0, ungranted = 0, using;
436         int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
437
438         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
439
440         for (obj = 0; obj < objcount; obj++) {
441                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
442                         int tmp, bytes;
443
444                         /* FIXME: this is calculated with PAGE_SIZE on client */
445                         bytes = rnb[n].len;
446                         bytes += rnb[n].offset & (blocksize - 1);
447                         tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
448                         if (tmp)
449                                 bytes += blocksize - tmp;
450
451                         if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
452                                 if (fed->fed_grant < used + bytes) {
453                                         CDEBUG(D_CACHE,
454                                                "%s: cli %s/%p claims %ld+%d "
455                                                "GRANT, real grant %lu idx %d\n",
456                                                exp->exp_obd->obd_name,
457                                                exp->exp_client_uuid.uuid, exp,
458                                                used, bytes, fed->fed_grant, n);
459                                         mask = D_ERROR;
460                                 } else {
461                                         used += bytes;
462                                         rnb[n].flags |= OBD_BRW_GRANTED;
463                                         lnb[n].lnb_grant_used = bytes;
464                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
465                                         rc = 0;
466                                         continue;
467                                 }
468                         }
469                         if (*left > ungranted) {
470                                 /* if enough space, pretend it was granted */
471                                 ungranted += bytes;
472                                 rnb[n].flags |= OBD_BRW_GRANTED;
473                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
474                                 rc = 0;
475                                 continue;
476                         }
477
478                         /* We can't check for already-mapped blocks here, as
479                          * it requires dropping the osfs lock to do the bmap.
480                          * Instead, we return ENOSPC and in that case we need
481                          * to go through and verify if all of the blocks not
482                          * marked BRW_GRANTED are already mapped and we can
483                          * ignore this error. */
484                         lnb[n].rc = -ENOSPC;
485                         rnb[n].flags &= OBD_BRW_GRANTED;
486                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
487                                exp->exp_obd->obd_name,
488                                exp->exp_client_uuid.uuid, exp, n, bytes);
489                 }
490         }
491
492         /* Now substract what client have used already.  We don't subtract
493          * this from the tot_granted yet, so that other client's can't grab
494          * that space before we have actually allocated our blocks.  That
495          * happens in filter_grant_commit() after the writes are done. */
496         *left -= ungranted;
497         fed->fed_grant -= used;
498         fed->fed_pending += used;
499         exp->exp_obd->u.filter.fo_tot_pending += used;
500
501         CDEBUG(mask,
502                "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
503                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
504                ungranted, fed->fed_grant, fed->fed_dirty);
505
506         /* Rough calc in case we don't refresh cached statfs data */
507         using = (used + ungranted + 1 ) >>
508                 exp->exp_obd->u.filter.fo_sb->s_blocksize_bits;
509         if (exp->exp_obd->obd_osfs.os_bavail > using)
510                 exp->exp_obd->obd_osfs.os_bavail -= using;
511         else
512                 exp->exp_obd->obd_osfs.os_bavail = 0;
513
514         if (fed->fed_dirty < used) {
515                 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
516                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
517                        used, fed->fed_dirty);
518                 used = fed->fed_dirty;
519         }
520         exp->exp_obd->u.filter.fo_tot_dirty -= used;
521         fed->fed_dirty -= used;
522
523         return rc;
524 }
525
526 static int filter_start_page_write(struct obd_device *obd, struct inode *inode,
527                                    struct niobuf_local *lnb)
528 {
529         struct page *page;
530
531         if (lnb->len != PAGE_SIZE)
532                 return filter_start_page_read(obd, inode, lnb);
533
534         page = alloc_pages(GFP_HIGHUSER, 0);
535         if (page == NULL) {
536                 CERROR("no memory for a temp page\n");
537                 RETURN(lnb->rc = -ENOMEM);
538         }
539 #if 0
540         POISON_PAGE(page, 0xf1);
541         if (lnb->len != PAGE_SIZE) {
542                 memset(kmap(page) + lnb->len, 0, PAGE_SIZE - lnb->len);
543                 kunmap(page);
544         }
545 #endif
546         page->index = lnb->offset >> PAGE_SHIFT;
547         lnb->page = page;
548
549         return 0;
550 }
551
552 static void filter_abort_page_write(struct niobuf_local *lnb)
553 {
554         LASSERT(lnb->page != NULL);
555
556         if (lnb->len != PAGE_SIZE)
557                 page_cache_release(lnb->page);
558         else
559                 __free_pages(lnb->page, 0);
560 }
561
562 /* a helper for both the 2.4 and 2.6 commitrw paths which are both built
563  * up by our shared filter_preprw_write() */
564 void filter_release_write_page(struct filter_obd *filter, struct inode *inode,
565                                struct niobuf_local *lnb, int rc)
566 {
567         if (lnb->len != PAGE_SIZE)
568                 return filter_release_read_page(filter, inode, lnb->page);
569
570         if (rc == 0)
571                 flip_into_page_cache(inode, lnb->page);
572         __free_page(lnb->page);
573 }
574
575 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
576  * on mulitple inodes.  That isn't all, because there still exists the
577  * possibility of a truncate starting a new transaction while holding the ext3
578  * rwsem = write while some writes (which have started their transactions here)
579  * blocking on the ext3 rwsem = read => lock inversion.
580  *
581  * The handling gets very ugly when dealing with locked pages.  It may be easier
582  * to just get rid of the locked page code (which has problems of its own) and
583  * either discover we do not need it anymore (i.e. it was a symptom of another
584  * bug) or ensure we get the page locks in an appropriate order. */
585 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
586                                int objcount, struct obd_ioobj *obj,
587                                int niocount, struct niobuf_remote *nb,
588                                struct niobuf_local *res,
589                                struct obd_trans_info *oti)
590 {
591         struct lvfs_run_ctxt saved;
592         struct niobuf_remote *rnb;
593         struct niobuf_local *lnb;
594         struct fsfilt_objinfo fso;
595         struct dentry *dentry;
596         obd_size left;
597         unsigned long now = jiffies;
598         int rc = 0, i, tot_bytes = 0, cleanup_phase = 1;
599         ENTRY;
600         LASSERT(objcount == 1);
601         LASSERT(obj->ioo_bufcnt > 0);
602
603         memset(res, 0, niocount * sizeof(*res));
604
605         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
606         dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
607                                    obj->ioo_id);
608         if (IS_ERR(dentry))
609                 GOTO(cleanup, rc = PTR_ERR(dentry));
610
611         if (dentry->d_inode == NULL) {
612                 CERROR("trying to BRW to non-existent file "LPU64"\n",
613                        obj->ioo_id);
614                 f_dput(dentry);
615                 GOTO(cleanup, rc = -ENOENT);
616         }
617
618         fso.fso_dentry = dentry;
619         fso.fso_bufcnt = obj->ioo_bufcnt;
620
621         if (time_after(jiffies, now + 15 * HZ))
622                 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
623         else
624                 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
625                        (jiffies - now));
626
627         spin_lock(&exp->exp_obd->obd_osfs_lock);
628         if (oa)
629                 filter_grant_incoming(exp, oa);
630         cleanup_phase = 0;
631
632         left = filter_grant_space_left(exp);
633
634         rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
635                                 &left, dentry->d_inode);
636         if (oa && oa->o_valid & OBD_MD_FLGRANT)
637                 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
638
639         spin_unlock(&exp->exp_obd->obd_osfs_lock);
640
641         if (rc) {
642                 f_dput(dentry);
643                 GOTO(cleanup, rc);
644         }
645
646         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
647              i++, lnb++, rnb++) {
648                 /* We still set up for ungranted pages so that granted pages
649                  * can be written to disk as they were promised, and portals
650                  * needs to keep the pages all aligned properly. */
651                 lnb->dentry = dentry;
652                 lnb->offset = rnb->offset;
653                 lnb->len    = rnb->len;
654                 lnb->flags  = rnb->flags;
655
656                 rc = filter_start_page_write(exp->exp_obd, dentry->d_inode,lnb);
657                 if (rc) {
658                         CERROR("page err %u@"LPU64" %u/%u %p: rc %d\n",
659                                lnb->len, lnb->offset,
660                                i, obj->ioo_bufcnt, dentry, rc);
661                         while (lnb-- > res)
662                                 filter_abort_page_write(lnb);
663                         f_dput(dentry);
664                         GOTO(cleanup, rc);
665                 }
666                 if (lnb->rc == 0)
667                         tot_bytes += lnb->len;
668         }
669
670         while (lnb-- > res) {
671                 if (lnb->len == PAGE_SIZE)
672                         continue;
673                 rc = filter_finish_page_read(lnb);
674                 if (rc) {
675                         CERROR("error page %u@"LPU64" %u %p: rc %d\n", lnb->len,
676                                lnb->offset, (int)(lnb - res), lnb->dentry, rc);
677                         GOTO(cleanup, rc);
678                 }
679         }
680
681         if (time_after(jiffies, now + 15 * HZ))
682                 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
683         else
684                 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
685                        (jiffies - now));
686
687         lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
688                             tot_bytes);
689         EXIT;
690 cleanup:
691         switch(cleanup_phase) {
692         case 1:
693                 spin_lock(&exp->exp_obd->obd_osfs_lock);
694                 if (oa)
695                         filter_grant_incoming(exp, oa);
696                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
697         default: ;
698         }
699         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
700         return rc;
701 }
702
703 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
704                   int objcount, struct obd_ioobj *obj, int niocount,
705                   struct niobuf_remote *nb, struct niobuf_local *res,
706                   struct obd_trans_info *oti)
707 {
708         if (cmd == OBD_BRW_WRITE)
709                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
710                                            niocount, nb, res, oti);
711
712         if (cmd == OBD_BRW_READ)
713                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
714                                           niocount, nb, res, oti);
715
716         LBUG();
717         return -EPROTO;
718 }
719
720 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
721                               struct page *page)
722 {
723         int drop = 0;
724
725         if (inode != NULL &&
726             (inode->i_size > filter->fo_readcache_max_filesize))
727                 drop = 1;
728
729         /* drop from cache like truncate_list_pages() */
730         if (drop && !TryLockPage(page)) {
731                 if (page->mapping)
732                         ll_truncate_complete_page(page);
733                 unlock_page(page);
734         }
735         page_cache_release(page);
736 }
737
738 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
739                                 int objcount, struct obd_ioobj *obj,
740                                 int niocount, struct niobuf_local *res,
741                                 struct obd_trans_info *oti, int rc)
742 {
743         struct obd_ioobj *o;
744         struct niobuf_local *lnb;
745         int i, j;
746         struct inode *inode = NULL;
747         ENTRY;
748
749         if (res->dentry != NULL)
750                 inode = res->dentry->d_inode;
751
752         for (i = 0, o = obj, lnb = res; i < objcount; i++, o++) {
753                 for (j = 0 ; j < o->ioo_bufcnt ; j++, lnb++) {
754                         if (lnb->page == NULL)
755                                 continue;
756                         filter_release_read_page(&exp->exp_obd->u.filter,
757                                                  inode, lnb->page);
758                 }
759         }
760
761         if (res->dentry != NULL)
762                 f_dput(res->dentry);
763         RETURN(rc);
764 }
765
766 void flip_into_page_cache(struct inode *inode, struct page *new_page)
767 {
768         struct page *old_page;
769         int rc;
770
771         do {
772                 /* the dlm is protecting us from read/write concurrency, so we
773                  * expect this find_lock_page to return quickly.  even if we
774                  * race with another writer it won't be doing much work with
775                  * the page locked.  we do this 'cause t_c_p expects a
776                  * locked page, and it wants to grab the pagecache lock
777                  * as well. */
778                 old_page = find_lock_page(inode->i_mapping, new_page->index);
779                 if (old_page) {
780                         ll_truncate_complete_page(old_page);
781                         unlock_page(old_page);
782                         page_cache_release(old_page);
783                 }
784
785 #if 0 /* this should be a /proc tunable someday */
786                 /* racing o_directs (no locking ioctl) could race adding
787                  * their pages, so we repeat the page invalidation unless
788                  * we successfully added our new page */
789                 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
790                                               new_page->index,
791                                               page_hash(inode->i_mapping,
792                                                         new_page->index));
793                 if (rc == 0) {
794                         /* add_to_page_cache clears uptodate|dirty and locks
795                          * the page */
796                         SetPageUptodate(new_page);
797                         unlock_page(new_page);
798                 }
799 #else
800                 rc = 0;
801 #endif
802         } while (rc != 0);
803 }
804
805 void filter_grant_commit(struct obd_export *exp, int niocount,
806                          struct niobuf_local *res)
807 {
808         struct filter_obd *filter = &exp->exp_obd->u.filter;
809         struct niobuf_local *lnb = res;
810         unsigned long pending = 0;
811         int i;
812
813         spin_lock(&exp->exp_obd->obd_osfs_lock);
814         for (i = 0, lnb = res; i < niocount; i++, lnb++)
815                 pending += lnb->lnb_grant_used;
816
817         LASSERTF(exp->exp_filter_data.fed_pending >= pending,
818                  "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
819                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
820                  exp->exp_filter_data.fed_pending, pending);
821         exp->exp_filter_data.fed_pending -= pending;
822         LASSERTF(filter->fo_tot_granted >= pending,
823                  "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
824                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
825                  exp->exp_obd->u.filter.fo_tot_granted, pending);
826         filter->fo_tot_granted -= pending;
827         LASSERTF(filter->fo_tot_pending >= pending,
828                  "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
829                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
830                  filter->fo_tot_pending, pending);
831         filter->fo_tot_pending -= pending;
832
833         spin_unlock(&exp->exp_obd->obd_osfs_lock);
834 }
835
836 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
837                     int objcount, struct obd_ioobj *obj, int niocount,
838                     struct niobuf_local *res, struct obd_trans_info *oti,int rc)
839 {
840         if (cmd == OBD_BRW_WRITE)
841                 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
842                                              res, oti, rc);
843         if (cmd == OBD_BRW_READ)
844                 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
845                                             res, oti, rc);
846         LBUG();
847         return -EPROTO;
848 }
849
850 int filter_brw(int cmd, struct obd_export *exp, struct obdo *oa,
851                struct lov_stripe_md *lsm, obd_count oa_bufs,
852                struct brw_page *pga, struct obd_trans_info *oti)
853 {
854         struct obd_ioobj ioo;
855         struct niobuf_local *lnb;
856         struct niobuf_remote *rnb;
857         obd_count i;
858         int ret = 0;
859         ENTRY;
860
861         OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
862         OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
863
864         if (lnb == NULL || rnb == NULL)
865                 GOTO(out, ret = -ENOMEM);
866
867         for (i = 0; i < oa_bufs; i++) {
868                 rnb[i].offset = pga[i].disk_offset;
869                 rnb[i].len = pga[i].count;
870         }
871
872         obdo_to_ioobj(oa, &ioo);
873         ioo.ioo_bufcnt = oa_bufs;
874
875         ret = filter_preprw(cmd, exp, oa, 1, &ioo, oa_bufs, rnb, lnb, oti);
876         if (ret != 0)
877                 GOTO(out, ret);
878
879         for (i = 0; i < oa_bufs; i++) {
880                 void *virt = kmap(pga[i].pg);
881                 obd_off off = pga[i].disk_offset & ~PAGE_MASK;
882                 void *addr = kmap(lnb[i].page);
883
884                 /* 2 kmaps == vanishingly small deadlock opportunity */
885
886                 if (cmd & OBD_BRW_WRITE)
887                         memcpy(addr + off, virt + off, pga[i].count);
888                 else
889                         memcpy(virt + off, addr + off, pga[i].count);
890
891                 kunmap(lnb[i].page);
892                 kunmap(pga[i].pg);
893         }
894
895         ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti, ret);
896
897 out:
898         if (lnb)
899                 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
900         if (rnb)
901                 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
902         RETURN(ret);
903 }