Whamcloud - gitweb
- lost #define changes from 1_6
[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, obd_timeout, "preprw_read setup");
314
315         for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
316              i++, rnb++, lnb++) {
317                 lnb->dentry = dentry;
318                 lnb->offset = rnb->offset;
319                 lnb->len    = rnb->len;
320                 lnb->flags  = rnb->flags;
321
322                 /*
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, obd_timeout, "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         lprocfs_counter_add(exp->exp_ops_stats, LPROC_FILTER_READ_BYTES,
356                             tot_bytes);
357
358         EXIT;
359
360  cleanup:
361         if (rc != 0) {
362                 filter_free_dio_pages(objcount, obj, niocount, res);
363
364                 if (dentry != NULL)
365                         f_dput(dentry);
366         }
367
368         filter_iobuf_put(&obd->u.filter, iobuf, oti);
369
370         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
371         if (rc)
372                 CERROR("io error %d\n", rc);
373
374         return rc;
375 }
376
377 /* When clients have dirtied as much space as they've been granted they
378  * fall through to sync writes.  These sync writes haven't been expressed
379  * in grants and need to error with ENOSPC when there isn't room in the
380  * filesystem for them after grants are taken into account.  However,
381  * writeback of the dirty data that was already granted space can write
382  * right on through.
383  *
384  * Caller must hold obd_osfs_lock. */
385 static int filter_grant_check(struct obd_export *exp, struct obdo *oa, 
386                               int objcount, struct fsfilt_objinfo *fso, 
387                               int niocount, struct niobuf_remote *rnb,
388                               struct niobuf_local *lnb, obd_size *left,
389                               struct inode *inode)
390 {
391         struct filter_export_data *fed = &exp->exp_filter_data;
392         int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
393         unsigned long used = 0, ungranted = 0, using;
394         int i, rc = -ENOSPC, obj, n = 0;
395
396         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
397
398         for (obj = 0; obj < objcount; obj++) {
399                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
400                         int tmp, bytes;
401
402                         /* should match the code in osc_exit_cache */
403                         bytes = rnb[n].len;
404                         bytes += rnb[n].offset & (blocksize - 1);
405                         tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
406                         if (tmp)
407                                 bytes += blocksize - tmp;
408
409                         if ((rnb[n].flags & OBD_BRW_FROM_GRANT) &&
410                             (oa->o_valid & OBD_MD_FLGRANT)) {
411                                 if (fed->fed_grant < used + bytes) {
412                                         CDEBUG(D_CACHE,
413                                                "%s: cli %s/%p claims %ld+%d "
414                                                "GRANT, real grant %lu idx %d\n",
415                                                exp->exp_obd->obd_name,
416                                                exp->exp_client_uuid.uuid, exp,
417                                                used, bytes, fed->fed_grant, n);
418                                 } else {
419                                         used += bytes;
420                                         rnb[n].flags |= OBD_BRW_GRANTED;
421                                         lnb[n].lnb_grant_used = bytes;
422                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
423                                         rc = 0;
424                                         continue;
425                                 }
426                         }
427                         if (*left > ungranted + bytes) {
428                                 /* if enough space, pretend it was granted */
429                                 ungranted += bytes;
430                                 rnb[n].flags |= OBD_BRW_GRANTED;
431                                 lnb[n].lnb_grant_used = bytes;
432                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
433                                 rc = 0;
434                                 continue;
435                         }
436
437                         /* We can't check for already-mapped blocks here, as
438                          * it requires dropping the osfs lock to do the bmap.
439                          * Instead, we return ENOSPC and in that case we need
440                          * to go through and verify if all of the blocks not
441                          * marked BRW_GRANTED are already mapped and we can
442                          * ignore this error. */
443                         lnb[n].rc = -ENOSPC;
444                         rnb[n].flags &= ~OBD_BRW_GRANTED;
445                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
446                                exp->exp_obd->obd_name,
447                                exp->exp_client_uuid.uuid, exp, n, bytes);
448                 }
449         }
450
451         /* Now substract what client have used already.  We don't subtract
452          * this from the tot_granted yet, so that other client's can't grab
453          * that space before we have actually allocated our blocks.  That
454          * happens in filter_grant_commit() after the writes are done. */
455         *left -= ungranted;
456         fed->fed_grant -= used;
457         fed->fed_pending += used + ungranted;
458         exp->exp_obd->u.filter.fo_tot_granted += ungranted;
459         exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
460
461         CDEBUG(D_CACHE,
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                                struct lustre_capa *capa)
509 {
510         struct lvfs_run_ctxt saved;
511         struct niobuf_remote *rnb;
512         struct niobuf_local *lnb = res;
513         struct fsfilt_objinfo fso;
514         struct filter_mod_data *fmd;
515         struct dentry *dentry = NULL;
516         void *iobuf;
517         obd_size left;
518         unsigned long now = jiffies;
519         int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
520         ENTRY;
521         LASSERT(objcount == 1);
522         LASSERT(obj->ioo_bufcnt > 0);
523
524         rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
525                               CAPA_OPC_OSS_WRITE);
526         if (rc)
527                 RETURN(rc);
528
529         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
530         iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
531         if (IS_ERR(iobuf))
532                 GOTO(cleanup, rc = PTR_ERR(iobuf));
533         cleanup_phase = 1;
534
535         dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
536                                    obj->ioo_id);
537         if (IS_ERR(dentry))
538                 GOTO(cleanup, rc = PTR_ERR(dentry));
539         cleanup_phase = 2;
540
541         if (dentry->d_inode == NULL) {
542                 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
543                        exp->exp_obd->obd_name, obj->ioo_id);
544                 GOTO(cleanup, rc = -ENOENT);
545         }
546
547         fso.fso_dentry = dentry;
548         fso.fso_bufcnt = obj->ioo_bufcnt;
549
550         fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "preprw_write setup");
551
552         /* Don't update inode timestamps if this write is older than a
553          * setattr which modifies the timestamps. b=10150 */
554         /* XXX when we start having persistent reservations this needs to
555          * be changed to filter_fmd_get() to create the fmd if it doesn't
556          * already exist so we can store the reservation handle there. */
557         fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
558
559         LASSERT(oa != NULL);
560         spin_lock(&exp->exp_obd->obd_osfs_lock);
561         filter_grant_incoming(exp, oa);
562         if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
563                 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
564                                  OBD_MD_FLATIME);
565         else
566                 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
567                               OBD_MD_FLMTIME | OBD_MD_FLCTIME);
568         cleanup_phase = 3;
569
570         left = filter_grant_space_left(exp);
571
572         rc = filter_grant_check(exp, oa, objcount, &fso, niocount, nb, res,
573                                 &left, dentry->d_inode);
574
575         /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
576          * for setting UID/GID and fid EA in first write time. */
577         if (oa->o_valid & OBD_MD_FLGRANT)
578                 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
579
580         spin_unlock(&exp->exp_obd->obd_osfs_lock);
581         filter_fmd_put(exp, fmd);
582
583         if (rc)
584                 GOTO(cleanup, rc);
585
586         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
587              i++, lnb++, rnb++) {
588                 /* We still set up for ungranted pages so that granted pages
589                  * can be written to disk as they were promised, and portals
590                  * needs to keep the pages all aligned properly. */
591                 lnb->dentry = dentry;
592                 lnb->offset = rnb->offset;
593                 lnb->len    = rnb->len;
594                 lnb->flags  = rnb->flags;
595
596                 /*
597                  * ost_brw_write()->ost_nio_pages_get() already initialized
598                  * lnb->page to point to the page from the per-thread page
599                  * pool (bug 5137), initialize page.
600                  */
601                 LASSERT(lnb->page != NULL);
602                 if (lnb->len != CFS_PAGE_SIZE) {
603                         memset(kmap(lnb->page) + lnb->len,
604                                0, CFS_PAGE_SIZE - lnb->len);
605                         kunmap(lnb->page);
606                 }
607                 lnb->page->index = lnb->offset >> CFS_PAGE_SHIFT;
608
609                 cleanup_phase = 4;
610
611                 /* If the filter writes a partial page, then has the file
612                  * extended, the client will read in the whole page.  the
613                  * filter has to be careful to zero the rest of the partial
614                  * page on disk.  we do it by hand for partial extending
615                  * writes, send_bio() is responsible for zeroing pages when
616                  * asked to read unmapped blocks -- brw_kiovec() does this. */
617                 if (lnb->len != CFS_PAGE_SIZE) {
618                         __s64 maxidx;
619
620                         maxidx = ((i_size_read(dentry->d_inode) +
621                                    CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
622                         if (maxidx >= lnb->page->index) {
623                                 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
624                                                LPU64" flg %x before EOF %llu\n",
625                                                lnb->len, lnb->offset,lnb->flags,
626                                                i_size_read(dentry->d_inode));
627                                 filter_iobuf_add_page(exp->exp_obd, iobuf,
628                                                       dentry->d_inode,
629                                                       lnb->page);
630                         } else {
631                                 long off;
632                                 char *p = kmap(lnb->page);
633
634                                 off = lnb->offset & ~CFS_PAGE_MASK;
635                                 if (off)
636                                         memset(p, 0, off);
637                                 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
638                                 if (off)
639                                         memset(p + off, 0, CFS_PAGE_SIZE - off);
640                                 kunmap(lnb->page);
641                         }
642                 }
643                 if (lnb->rc == 0)
644                         tot_bytes += lnb->len;
645         }
646
647         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
648                               NULL, NULL, NULL);
649
650         fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "start_page_write");
651
652         lprocfs_counter_add(exp->exp_ops_stats, LPROC_FILTER_WRITE_BYTES,
653                             tot_bytes);
654         EXIT;
655 cleanup:
656         switch(cleanup_phase) {
657         case 4:
658         case 3:
659                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
660         case 2:
661                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
662                 if (rc)
663                         f_dput(dentry);
664                 break;
665         case 1:
666                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
667         case 0:
668                 spin_lock(&exp->exp_obd->obd_osfs_lock);
669                 if (oa)
670                         filter_grant_incoming(exp, oa);
671                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
672                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
673                 break;
674         default:;
675         }
676         return rc;
677 }
678
679 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
680                   int objcount, struct obd_ioobj *obj, int niocount,
681                   struct niobuf_remote *nb, struct niobuf_local *res,
682                   struct obd_trans_info *oti, struct lustre_capa *capa)
683 {
684         if (cmd == OBD_BRW_WRITE)
685                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
686                                            niocount, nb, res, oti, capa);
687         if (cmd == OBD_BRW_READ)
688                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
689                                           niocount, nb, res, oti, capa);
690         LBUG();
691         return -EPROTO;
692 }
693
694 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
695                               struct page *page)
696 {
697         int drop = 0;
698
699         if (inode != NULL &&
700             (i_size_read(inode) > filter->fo_readcache_max_filesize))
701                 drop = 1;
702
703         /* drop from cache like truncate_list_pages() */
704         if (drop && !TryLockPage(page)) {
705                 if (page->mapping)
706                         ll_truncate_complete_page(page);
707                 unlock_page(page);
708         }
709         page_cache_release(page);
710 }
711
712 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
713                                 int objcount, struct obd_ioobj *obj,
714                                 int niocount, struct niobuf_local *res,
715                                 struct obd_trans_info *oti, int rc)
716 {
717         struct inode *inode = NULL;
718         struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
719                                                 obj->ioo_gr, 0} };
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, oinfo_capa(oinfo));
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 }