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