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