Whamcloud - gitweb
Land b_hd_capa onto HEAD (20050809_1942)
[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 {
269         struct obd_device *obd = exp->exp_obd;
270         struct lvfs_run_ctxt saved;
271         struct niobuf_remote *rnb;
272         struct niobuf_local *lnb;
273         struct dentry *dentry = NULL;
274         struct inode *inode;
275         void *iobuf = NULL;
276         int rc = 0, i, tot_bytes = 0;
277         unsigned long now = jiffies;
278         ENTRY;
279
280         /* We are currently not supporting multi-obj BRW_READ RPCS at all.
281          * When we do this function's dentry cleanup will need to be fixed */
282         LASSERTF(objcount == 1, "%d\n", objcount);
283         LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
284
285         if (oa && oa->o_valid & OBD_MD_FLGRANT) {
286                 spin_lock(&obd->obd_osfs_lock);
287                 filter_grant_incoming(exp, oa);
288
289                 oa->o_grant = 0;
290                 spin_unlock(&obd->obd_osfs_lock);
291         }
292
293         memset(res, 0, niocount * sizeof(*res));
294
295         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
296         rc = filter_alloc_iobuf(OBD_BRW_READ, obj->ioo_bufcnt, &iobuf);
297         if (rc)
298                 GOTO(cleanup, rc);
299
300         dentry = filter_id2dentry(obd, NULL, oa->o_gr, oa->o_id);
301         if (IS_ERR(dentry))
302                 GOTO(cleanup, rc = PTR_ERR(dentry));
303
304         inode = dentry->d_inode; 
305
306         fsfilt_check_slow(now, obd_timeout, "preprw_read setup");
307
308         for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
309              i++, rnb++, lnb++) {
310                 lnb->dentry = dentry;
311                 lnb->offset = rnb->offset;
312                 lnb->len    = rnb->len;
313                 lnb->flags  = rnb->flags;
314
315                 if ((inode && inode->i_size <= rnb->offset) || inode == NULL)
316                         /*
317                          * if there's no more data, abort early.  lnb->page == *
318                          * NULL and lnb->rc == 0, so it's easy to detect later.
319                          */
320                         break;
321                 
322                 rc = filter_alloc_dio_page(obd, inode, lnb);
323                 if (rc) {
324                         CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
325                              "page err %u@"LPU64" %u/%u %p: rc %d\n",
326                               lnb->len, lnb->offset, i, obj->ioo_bufcnt,
327                               dentry, rc);
328                         GOTO(cleanup, rc);
329                 }
330
331                 if (inode->i_size < lnb->offset + lnb->len - 1)
332                         lnb->rc = inode->i_size - lnb->offset;
333                 else
334                         lnb->rc = lnb->len;
335
336                 tot_bytes += lnb->rc;
337
338                 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
339         }
340
341         fsfilt_check_slow(now, obd_timeout, "start_page_read");
342
343         if (inode != NULL) {
344                 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
345                                       exp, NULL, NULL, NULL);
346                 if (rc)
347                         GOTO(cleanup, rc);
348         }
349
350         lprocfs_counter_add(obd->obd_stats,
351                             LPROC_FILTER_READ_BYTES, tot_bytes);
352         filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
353
354         EXIT;
355 cleanup:
356         if (rc) {
357                 filter_free_dio_pages(objcount, obj,
358                                       niocount, res);
359                 /*
360                  * in other cases (no errors) dentry is released in
361                  * filter_commitrw_read().
362                  */
363                 f_dput(dentry);
364         }
365
366         if (iobuf != NULL)
367                 filter_free_iobuf(iobuf);
368
369         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
370
371         if (rc)
372                 CERROR("io error %d\n", rc);
373         
374         return rc;
375 }
376
377 /* When clients have dirtied as much space as they've been granted they
378  * fall through to sync writes.  These sync writes haven't been expressed
379  * in grants and need to error with ENOSPC when there isn't room in the
380  * filesystem for them after grants are taken into account.  However,
381  * writeback of the dirty data that was already granted space can write
382  * right on through.
383  *
384  * Caller must hold obd_osfs_lock. */
385 static int filter_grant_check(struct obd_export *exp, int objcount,
386                               struct fsfilt_objinfo *fso, int niocount,
387                               struct niobuf_remote *rnb,
388                               struct niobuf_local *lnb, obd_size *left,
389                               struct inode *inode)
390 {
391         struct filter_export_data *fed = &exp->exp_filter_data;
392         int blocksize = exp->exp_obd->u.filter.fo_sb->s_blocksize;
393         unsigned long used = 0, ungranted = 0, using;
394         int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
395
396         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
397
398         for (obj = 0; obj < objcount; obj++) {
399                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
400                         int tmp, bytes;
401
402                         /* FIXME: this is calculated with PAGE_SIZE on client */
403                         bytes = rnb[n].len;
404                         bytes += rnb[n].offset & (blocksize - 1);
405                         tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
406                         if (tmp)
407                                 bytes += blocksize - tmp;
408
409                         if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
410                                 if (fed->fed_grant < used + bytes) {
411                                         CDEBUG(D_CACHE,
412                                                "%s: cli %s/%p claims %ld+%d "
413                                                "GRANT, real grant %lu idx %d\n",
414                                                exp->exp_obd->obd_name,
415                                                exp->exp_client_uuid.uuid, exp,
416                                                used, bytes, fed->fed_grant, n);
417                                         mask = D_ERROR;
418                                 } else {
419                                         used += bytes;
420                                         rnb[n].flags |= OBD_BRW_GRANTED;
421                                         lnb[n].lnb_grant_used = bytes;
422                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
423                                         rc = 0;
424                                         continue;
425                                 }
426                         }
427                         if (*left > ungranted) {
428                                 /* if enough space, pretend it was granted */
429                                 ungranted += bytes;
430                                 rnb[n].flags |= OBD_BRW_GRANTED;
431                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
432                                 rc = 0;
433                                 continue;
434                         }
435
436                         /* We can't check for already-mapped blocks here, as
437                          * it requires dropping the osfs lock to do the bmap.
438                          * Instead, we return ENOSPC and in that case we need
439                          * to go through and verify if all of the blocks not
440                          * marked BRW_GRANTED are already mapped and we can
441                          * ignore this error. */
442                         lnb[n].rc = -ENOSPC;
443                         rnb[n].flags &= OBD_BRW_GRANTED;
444                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
445                                exp->exp_obd->obd_name,
446                                exp->exp_client_uuid.uuid, exp, n, bytes);
447                 }
448         }
449
450         /* Now substract what client have used already.  We don't subtract
451          * this from the tot_granted yet, so that other client's can't grab
452          * that space before we have actually allocated our blocks.  That
453          * happens in filter_grant_commit() after the writes are done. */
454         *left -= ungranted;
455         fed->fed_grant -= used;
456         fed->fed_pending += used;
457         exp->exp_obd->u.filter.fo_tot_pending += used;
458
459         CDEBUG(mask,
460                "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
461                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
462                ungranted, fed->fed_grant, fed->fed_dirty);
463
464         /* Rough calc in case we don't refresh cached statfs data */
465         using = (used + ungranted + 1 ) >>
466                 exp->exp_obd->u.filter.fo_sb->s_blocksize_bits;
467         if (exp->exp_obd->obd_osfs.os_bavail > using)
468                 exp->exp_obd->obd_osfs.os_bavail -= using;
469         else
470                 exp->exp_obd->obd_osfs.os_bavail = 0;
471
472         if (fed->fed_dirty < used) {
473                 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
474                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
475                        used, fed->fed_dirty);
476                 used = fed->fed_dirty;
477         }
478         exp->exp_obd->u.filter.fo_tot_dirty -= used;
479         fed->fed_dirty -= used;
480
481         return rc;
482 }
483
484 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
485  * on mulitple inodes.  That isn't all, because there still exists the
486  * possibility of a truncate starting a new transaction while holding the ext3
487  * rwsem = write while some writes (which have started their transactions here)
488  * blocking on the ext3 rwsem = read => lock inversion.
489  *
490  * The handling gets very ugly when dealing with locked pages.  It may be easier
491  * to just get rid of the locked page code (which has problems of its own) and
492  * either discover we do not need it anymore (i.e. it was a symptom of another
493  * bug) or ensure we get the page locks in an appropriate order. */
494 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
495                                int objcount, struct obd_ioobj *obj,
496                                int niocount, struct niobuf_remote *nb,
497                                struct niobuf_local *res,
498                                struct obd_trans_info *oti)
499 {
500         int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
501         struct niobuf_local *lnb = res;
502         struct dentry *dentry = NULL;
503         unsigned long now = jiffies;
504         struct lvfs_run_ctxt saved;
505         struct niobuf_remote *rnb;
506         struct fsfilt_objinfo fso;
507         struct obd_device *obd;
508         obd_size left;
509         obd_uid uid;
510         obd_gid gid;
511         void *iobuf; 
512         
513         ENTRY;
514         LASSERT(objcount == 1);
515         LASSERT(obj->ioo_bufcnt > 0);
516
517         memset(res, 0, niocount * sizeof(*res));
518
519         rc = filter_alloc_iobuf(OBD_BRW_READ, obj->ioo_bufcnt, &iobuf);
520         if (rc)
521                 GOTO(cleanup, rc);
522         cleanup_phase = 1;
523
524         obd = exp->exp_obd;
525         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
526
527         uid = oa->o_valid & OBD_MD_FLUID ? oa->o_uid : 0;
528         gid = oa->o_valid & OBD_MD_FLGID ? oa->o_gid : 0;
529         
530         /* make sure that object is already allocated */
531         dentry = filter_crow_object(obd, oa);
532         if (IS_ERR(dentry))
533                 GOTO(cleanup, rc = PTR_ERR(dentry));
534
535         cleanup_phase = 2;
536
537         fso.fso_dentry = dentry;
538         fso.fso_bufcnt = obj->ioo_bufcnt;
539
540         fsfilt_check_slow(now, obd_timeout, "preprw_write setup");
541
542         spin_lock(&obd->obd_osfs_lock);
543         if (oa)
544                 filter_grant_incoming(exp, oa);
545         
546         cleanup_phase = 3;
547
548         left = filter_grant_space_left(exp);
549
550         rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
551                                 &left, dentry->d_inode);
552         if (oa && oa->o_valid & OBD_MD_FLGRANT)
553                 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
554
555         /* We're finishing using body->oa as an input variable, so reset
556          * o_valid here. */
557         oa->o_valid = 0;
558
559         spin_unlock(&obd->obd_osfs_lock);
560
561         if (rc) 
562                 GOTO(cleanup, rc);
563
564         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
565              i++, lnb++, rnb++) {
566                 /* We still set up for ungranted pages so that granted pages
567                  * can be written to disk as they were promised, and portals
568                  * needs to keep the pages all aligned properly. */
569                 lnb->dentry = dentry;
570                 lnb->offset = rnb->offset;
571                 lnb->len    = rnb->len;
572                 lnb->flags  = rnb->flags;
573
574                 rc = filter_alloc_dio_page(obd, dentry->d_inode,lnb);
575                 if (rc) {
576                         CERROR("page err %u@"LPU64" %u/%u %p: rc %d\n",
577                                lnb->len, lnb->offset,
578                                i, obj->ioo_bufcnt, dentry, rc);
579                         GOTO(cleanup, rc);
580                 }
581                 cleanup_phase = 4;
582
583                 /* If the filter writes a partial page, then has the file
584                  * extended, the client will read in the whole page.  the
585                  * filter has to be careful to zero the rest of the partial
586                  * page on disk.  we do it by hand for partial extending
587                  * writes, send_bio() is responsible for zeroing pages when
588                  * asked to read unmapped blocks -- brw_kiovec() does this. */
589                 if (lnb->len != PAGE_SIZE) {
590                         if (lnb->offset + lnb->len < dentry->d_inode->i_size) {
591                                 filter_iobuf_add_page(obd, iobuf, dentry->d_inode,
592                                                       lnb->page);
593                         } else {
594                                 memset(kmap(lnb->page) + lnb->len, 0,
595                                        PAGE_SIZE - lnb->len);
596                                 kunmap(lnb->page);
597                         }
598                 }
599                 if (lnb->rc == 0)
600                         tot_bytes += lnb->len;
601         }
602
603         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
604                               NULL, NULL, NULL);
605         
606         fsfilt_check_slow(now, obd_timeout, "start_page_write");
607
608         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
609                             tot_bytes);
610         EXIT;
611 cleanup:
612         switch(cleanup_phase) {
613         case 4:
614                 if (rc)
615                         filter_free_dio_pages(objcount, obj, niocount, res);
616         case 3:
617                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
618                 filter_free_iobuf(iobuf);
619         case 2:
620                 if (rc && dentry && !IS_ERR(dentry))
621                         f_dput(dentry);
622                 break;
623         case 1:
624                 spin_lock(&obd->obd_osfs_lock);
625                 if (oa)
626                         filter_grant_incoming(exp, oa);
627                 spin_unlock(&obd->obd_osfs_lock);
628                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
629                 filter_free_iobuf(iobuf);
630                 break;
631         default:;
632         
633         }
634         RETURN(rc);
635 }
636
637 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
638                   int objcount, struct obd_ioobj *obj, int niocount,
639                   struct niobuf_remote *nb, struct niobuf_local *res,
640                   struct obd_trans_info *oti, struct lustre_capa *capa)
641 {
642         int rc;
643
644         rc = filter_verify_capa(cmd, exp, capa);
645         if (rc)
646                 return rc;
647
648         if (cmd == OBD_BRW_WRITE)
649                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
650                                            niocount, nb, res, oti);
651
652         if (cmd == OBD_BRW_READ)
653                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
654                                           niocount, nb, res, oti);
655
656         LBUG();
657         return -EPROTO;
658 }
659
660 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
661                               struct page *page)
662 {
663         int drop = 0;
664
665         if (inode != NULL &&
666             (inode->i_size > filter->fo_readcache_max_filesize))
667                 drop = 1;
668
669         /* drop from cache like truncate_list_pages() */
670         if (drop && !TryLockPage(page)) {
671                 if (page->mapping)
672                         ll_truncate_complete_page(page);
673                 unlock_page(page);
674         }
675         page_cache_release(page);
676 }
677
678 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
679                                 int objcount, struct obd_ioobj *obj,
680                                 int niocount, struct niobuf_local *res,
681                                 struct obd_trans_info *oti, int rc)
682 {
683         struct inode *inode = NULL;
684         ENTRY;
685
686         if (res->dentry != NULL)
687                 inode = res->dentry->d_inode;
688
689         filter_free_dio_pages(objcount, obj, niocount, res);
690         
691         if (res->dentry != NULL)
692                 f_dput(res->dentry);
693         RETURN(rc);
694 }
695
696 void flip_into_page_cache(struct inode *inode, struct page *new_page)
697 {
698         struct page *old_page;
699         int rc;
700
701         do {
702                 /* the dlm is protecting us from read/write concurrency, so we
703                  * expect this find_lock_page to return quickly.  even if we
704                  * race with another writer it won't be doing much work with
705                  * the page locked.  we do this 'cause t_c_p expects a
706                  * locked page, and it wants to grab the pagecache lock
707                  * as well. */
708                 old_page = find_lock_page(inode->i_mapping, new_page->index);
709                 if (old_page) {
710                         ll_truncate_complete_page(old_page);
711                         unlock_page(old_page);
712                         page_cache_release(old_page);
713                 }
714
715 #if 0 /* this should be a /proc tunable someday */
716                 /* racing o_directs (no locking ioctl) could race adding
717                  * their pages, so we repeat the page invalidation unless
718                  * we successfully added our new page */
719                 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
720                                               new_page->index,
721                                               page_hash(inode->i_mapping,
722                                                         new_page->index));
723                 if (rc == 0) {
724                         /* add_to_page_cache clears uptodate|dirty and locks
725                          * the page */
726                         SetPageUptodate(new_page);
727                         unlock_page(new_page);
728                 }
729 #else
730                 rc = 0;
731 #endif
732         } while (rc != 0);
733 }
734
735 void filter_grant_commit(struct obd_export *exp, int niocount,
736                          struct niobuf_local *res)
737 {
738         struct filter_obd *filter = &exp->exp_obd->u.filter;
739         struct niobuf_local *lnb = res;
740         unsigned long pending = 0;
741         int i;
742
743         spin_lock(&exp->exp_obd->obd_osfs_lock);
744         for (i = 0, lnb = res; i < niocount; i++, lnb++)
745                 pending += lnb->lnb_grant_used;
746
747         LASSERTF(exp->exp_filter_data.fed_pending >= pending,
748                  "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
749                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
750                  exp->exp_filter_data.fed_pending, pending);
751         exp->exp_filter_data.fed_pending -= pending;
752         LASSERTF(filter->fo_tot_granted >= pending,
753                  "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
754                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
755                  exp->exp_obd->u.filter.fo_tot_granted, pending);
756         filter->fo_tot_granted -= pending;
757         LASSERTF(filter->fo_tot_pending >= pending,
758                  "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
759                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
760                  filter->fo_tot_pending, pending);
761         filter->fo_tot_pending -= pending;
762
763         spin_unlock(&exp->exp_obd->obd_osfs_lock);
764 }
765 int filter_do_cow(struct obd_export *exp, struct obd_ioobj *obj,
766                   int nioo, struct niobuf_remote *rnb)
767 {
768         struct dentry *dentry;
769         struct lvfs_run_ctxt saved;
770         struct write_extents *extents = NULL;
771         int j, rc = 0, numexts = 0, flags = 0;
772
773         ENTRY;
774
775         LASSERT(nioo == 1);
776
777         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
778         
779         dentry = filter_id2dentry(exp->exp_obd, NULL, obj->ioo_gr,
780                                   obj->ioo_id);
781         if (IS_ERR(dentry)) {
782                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
783                 RETURN (PTR_ERR(dentry));
784         }
785
786         if (dentry->d_inode == NULL) {
787                 CERROR("trying to write extents to non-existent file "LPU64"\n",
788                        obj->ioo_id);
789                 GOTO(cleanup, rc = -ENOENT);
790         }
791         
792         flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
793         if (!(flags & SM_DO_COW)) {
794                 GOTO(cleanup, rc);
795         }
796         OBD_ALLOC(extents, obj->ioo_bufcnt * sizeof(struct write_extents)); 
797         if (!extents) {
798                 CERROR("No Memory\n");
799                 GOTO(cleanup, rc = -ENOMEM);
800         }
801         for (j = 0; j < obj->ioo_bufcnt; j++) {
802                 if (rnb[j].len != 0) {
803                         extents[numexts].w_count = rnb[j].len;
804                         extents[numexts].w_pos = rnb[j].offset;
805                         numexts++;
806                 } 
807         } 
808         rc = fsfilt_do_write_cow(exp->exp_obd, dentry, extents, numexts);
809         if (rc) {
810                 CERROR("Do cow error id "LPU64" rc:%d \n",
811                         obj->ioo_id, rc);
812                 GOTO(cleanup, rc); 
813         }
814         
815 cleanup:
816         if (extents) {
817                 OBD_FREE(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
818         }
819         f_dput(dentry);
820         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
821         RETURN(rc);
822
823 }
824 int filter_write_extents(struct obd_export *exp, struct obd_ioobj *obj, int nobj,
825                          int niocount, struct niobuf_local *local, int rc)
826 {
827         struct lvfs_run_ctxt saved;
828         struct dentry *dentry;
829         struct niobuf_local *lnb;
830         __u64  offset = 0;
831         __u32  len = 0;
832         int    i, flags; 
833  
834         ENTRY;
835
836         LASSERT(nobj == 1);
837
838         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
839         dentry = filter_id2dentry(exp->exp_obd, NULL, obj->ioo_gr,
840                                   obj->ioo_id);
841         if (IS_ERR(dentry)) {
842                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
843                 RETURN (PTR_ERR(dentry));
844         }
845
846         if (dentry->d_inode == NULL) {
847                 CERROR("trying to write extents to non-existent file "LPU64"\n",
848                        obj->ioo_id);
849                 GOTO(cleanup, rc = -ENOENT);
850         }
851         
852         flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
853         if (!(flags & SM_DO_REC)) {
854                 GOTO(cleanup, rc);
855         }
856
857         for (i = 0, lnb = local; i < obj->ioo_bufcnt; i++, lnb++) {
858                 if (len == 0) {
859                         offset = lnb->offset;
860                         len = lnb->len;
861                 } else if (lnb->offset == (offset + len)) {
862                         len += lnb->len;
863                 } else {
864                         rc = fsfilt_write_extents(exp->exp_obd, dentry, 
865                                                   offset, len);
866                         if (rc) {
867                                 CERROR("write exts off "LPU64" num %u rc:%d\n",
868                                         offset, len, rc);
869                                 GOTO(cleanup, rc);
870                         }
871                         offset = lnb->offset;
872                         len = lnb->len; 
873                 } 
874         }
875         if (len > 0) {
876                 rc = fsfilt_write_extents(exp->exp_obd, dentry, 
877                                           offset, len);
878                 if (rc) {
879                         CERROR("write exts off "LPU64" num %u rc:%d\n",
880                                 offset, len, rc);
881                         GOTO(cleanup, rc);
882                 }
883         }
884 cleanup:
885         f_dput(dentry);
886         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
887         RETURN(rc);
888 }
889
890 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
891                     int objcount, struct obd_ioobj *obj, int niocount,
892                     struct niobuf_local *res, struct obd_trans_info *oti,int ret)
893 {
894         int rc = -EPROTO;
895         struct lustre_id *id = obdo_id(oa);
896         __u32 len = sizeof(*id);
897         struct inode * inode = res->dentry->d_inode;
898         struct super_block * sb = res->dentry->d_sb;
899         struct obd_device *obd = class_exp2obd(exp);
900         
901         if (cmd == OBD_BRW_WRITE) {
902                 rc = filter_commitrw_write(exp, oa, objcount, obj, niocount,
903                                            res, oti, ret);
904                 fsfilt_set_info(obd, sb, inode, 10, "file_write", len, (void*)id);
905         }
906         else if (cmd == OBD_BRW_READ) {
907                 rc = filter_commitrw_read(exp, oa, objcount, obj, niocount,
908                                           res, oti, ret);
909                 fsfilt_set_info(obd, sb, inode, 9, "file_read", len, (void*)id);
910         }
911         else
912                 LBUG();
913
914         return rc;
915 }
916
917 int filter_brw(int cmd, struct obd_export *exp, struct obdo *oa,
918                struct lov_stripe_md *lsm, obd_count oa_bufs,
919                struct brw_page *pga, struct obd_trans_info *oti)
920 {
921         struct obd_ioobj ioo;
922         struct niobuf_local *lnb;
923         struct niobuf_remote *rnb;
924         obd_count i;
925         int ret = 0;
926         ENTRY;
927
928         OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
929         OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
930
931         if (lnb == NULL || rnb == NULL)
932                 GOTO(out, ret = -ENOMEM);
933
934         for (i = 0; i < oa_bufs; i++) {
935                 rnb[i].offset = pga[i].disk_offset;
936                 rnb[i].len = pga[i].count;
937         }
938
939         obdo_to_ioobj(oa, &ioo);
940         ioo.ioo_bufcnt = oa_bufs;
941
942         ret = filter_preprw(cmd, exp, oa, 1, &ioo, oa_bufs, rnb, lnb, oti,NULL);
943         if (ret != 0)
944                 GOTO(out, ret);
945
946         for (i = 0; i < oa_bufs; i++) {
947                 void *virt;
948                 obd_off off;
949                 void *addr;
950
951                 if (lnb[i].page == NULL)
952                         break;
953
954                 off = pga[i].disk_offset & ~PAGE_MASK;
955                 virt = kmap(pga[i].pg);
956                 addr = kmap(lnb[i].page);
957
958                 /* 2 kmaps == vanishingly small deadlock opportunity */
959
960                 if (cmd & OBD_BRW_WRITE)
961                         memcpy(addr + off, virt + off, pga[i].count);
962                 else
963                         memcpy(virt + off, addr + off, pga[i].count);
964
965                 kunmap(lnb[i].page);
966                 kunmap(pga[i].pg);
967         }
968
969         ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti, ret);
970
971 out:
972         if (lnb)
973                 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
974         if (rnb)
975                 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
976         RETURN(ret);
977 }