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