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