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