Whamcloud - gitweb
b=16918
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdfilter/filter_io.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/pagemap.h> // XXX kill me soon
50 #include <linux/version.h>
51
52 #include <obd_class.h>
53 #include <obd_ost.h>
54 #include <lustre_fsfilt.h>
55 #include "filter_internal.h"
56
57 int *obdfilter_created_scratchpad;
58
59 /* Grab the dirty and seen grant announcements from the incoming obdo.
60  * We will later calculate the clients new grant and return it.
61  * Caller must hold osfs lock */
62 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
63 {
64         struct filter_export_data *fed;
65         struct obd_device *obd = exp->exp_obd;
66         ENTRY;
67
68         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
69
70         if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
71                                         (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
72                 oa->o_valid &= ~OBD_MD_FLGRANT;
73                 EXIT;
74                 return;
75         }
76
77         fed = &exp->exp_filter_data;
78
79         /* Add some margin, since there is a small race if other RPCs arrive
80          * out-or-order and have already consumed some grant.  We want to
81          * leave this here in case there is a large error in accounting. */
82         CDEBUG(D_CACHE,
83                "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
84                obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
85                oa->o_dropped, fed->fed_grant);
86
87         /* Update our accounting now so that statfs takes it into account.
88          * Note that fed_dirty is only approximate and can become incorrect
89          * if RPCs arrive out-of-order.  No important calculations depend
90          * on fed_dirty however, but we must check sanity to not assert. */
91         if ((long long)oa->o_dirty < 0)
92                 oa->o_dirty = 0;
93         else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
94                 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
95         obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
96         if (fed->fed_grant < oa->o_dropped) {
97                 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
98                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
99                        oa->o_dropped, fed->fed_grant);
100                 oa->o_dropped = 0;
101         }
102         if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
103                 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
104                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
105                        oa->o_dropped, obd->u.filter.fo_tot_granted);
106                 oa->o_dropped = 0;
107         }
108         obd->u.filter.fo_tot_granted -= oa->o_dropped;
109         fed->fed_grant -= oa->o_dropped;
110         fed->fed_dirty = oa->o_dirty;
111         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
112                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
113                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
114                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
115                 spin_unlock(&obd->obd_osfs_lock);
116                 LBUG();
117         }
118         EXIT;
119 }
120
121 /* Figure out how much space is available between what we've granted
122  * and what remains in the filesystem.  Compensate for ext3 indirect
123  * block overhead when computing how much free space is left ungranted.
124  *
125  * Caller must hold obd_osfs_lock. */
126 obd_size filter_grant_space_left(struct obd_export *exp)
127 {
128         struct obd_device *obd = exp->exp_obd;
129         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
130         obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
131         int rc, statfs_done = 0;
132
133         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
134
135         if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
136 restat:
137                 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
138                                    cfs_time_current_64() + HZ);
139                 if (rc) /* N.B. statfs can't really fail */
140                         RETURN(0);
141                 statfs_done = 1;
142         }
143
144         avail = obd->obd_osfs.os_bavail;
145         left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
146         if (left > GRANT_FOR_LLOG(obd)) {
147                 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
148         } else {
149                 left = 0 /* << blockbits */;
150         }
151
152         if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
153                 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
154                 goto restat;
155         }
156
157         if (left >= tot_granted) {
158                 left -= tot_granted;
159         } else {
160                 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
161                         CERROR("%s: cli %s/%p grant "LPU64" > available "
162                                LPU64" and pending "LPU64"\n", obd->obd_name,
163                                exp->exp_client_uuid.uuid, exp, tot_granted,
164                                left, obd->u.filter.fo_tot_pending);
165                 }
166                 left = 0;
167         }
168
169         CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
170                " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
171                exp->exp_client_uuid.uuid, exp,
172                obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
173                tot_granted, left, obd->u.filter.fo_tot_pending);
174
175         return left;
176 }
177
178 /* Calculate how much grant space to allocate to this client, based on how
179  * much space is currently free and how much of that is already granted.
180  *
181  * Caller must hold obd_osfs_lock. */
182 long filter_grant(struct obd_export *exp, obd_size current_grant,
183                   obd_size want, obd_size fs_space_left)
184 {
185         struct obd_device *obd = exp->exp_obd;
186         struct filter_export_data *fed = &exp->exp_filter_data;
187         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
188         __u64 grant = 0;
189
190         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
191
192         /* Grant some fraction of the client's requested grant space so that
193          * they are not always waiting for write credits (not all of it to
194          * avoid overgranting in face of multiple RPCs in flight).  This
195          * essentially will be able to control the OSC_MAX_RIF for a client.
196          *
197          * If we do have a large disparity between what the client thinks it
198          * has and what we think it has, don't grant very much and let the
199          * client consume its grant first.  Either it just has lots of RPCs
200          * in flight, or it was evicted and its grants will soon be used up. */
201         if (want > 0x7fffffff) {
202                 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
203                        obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
204         } else if (current_grant < want &&
205                    current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
206                 grant = min((want >> blockbits),
207                             (fs_space_left >> blockbits) / 8);
208                 grant <<= blockbits;
209
210                 if (grant) {
211                         /* Allow >FILTER_GRANT_CHUNK size when clients
212                          * reconnect due to a server reboot.
213                          */
214                         if ((grant > FILTER_GRANT_CHUNK) &&
215                             (!obd->obd_recovering))
216                                 grant = FILTER_GRANT_CHUNK;
217
218                         obd->u.filter.fo_tot_granted += grant;
219                         fed->fed_grant += grant;
220                         if (fed->fed_grant < 0) {
221                                 CERROR("%s: cli %s/%p grant %ld want "LPU64
222                                        "current"LPU64"\n",
223                                        obd->obd_name, exp->exp_client_uuid.uuid,
224                                        exp, fed->fed_grant, want,current_grant);
225                                 spin_unlock(&obd->obd_osfs_lock);
226                                 LBUG();
227                         }
228                 }
229         }
230
231         CDEBUG(D_CACHE,
232                "%s: cli %s/%p wants: "LPU64" current grant "LPU64
233                " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
234                exp, want, current_grant, grant);
235         CDEBUG(D_CACHE,
236                "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
237                " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
238                exp, obd->u.filter.fo_tot_dirty,
239                obd->u.filter.fo_tot_granted, obd->obd_num_exports);
240
241         return grant;
242 }
243
244 /*
245  * the routine is used to request pages from pagecache
246  *
247  * use GFP_NOFS not allowing to enter FS as the client can run on this node
248  * and we might end waiting on a page he sent in the request we're serving.
249  *
250  * use NORETRY so that the allocator doesn't go crazy: chance to more lucky
251  * thread have enough memory to complete his request. for our request client
252  * will do resend hopefully -bzzz
253  */
254 static struct page * filter_get_page(struct obd_device *obd,
255                                      struct inode *inode,
256                                      obd_off offset)
257 {
258         struct page *page;
259
260         page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
261                                    GFP_NOFS | __GFP_NORETRY);
262         if (unlikely(page == NULL))
263                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_NO_PAGE, 1);
264
265         return page;
266 }
267
268 /*
269  * the routine initializes array of local_niobuf from remote_niobuf
270  */
271 static int filter_map_remote_to_local(int objcount, struct obd_ioobj *obj,
272                                       struct niobuf_remote *nb,
273                                       int *nrpages, struct niobuf_local *res)
274 {
275         struct niobuf_remote *rnb;
276         struct niobuf_local *lnb;
277         int i, max;
278         ENTRY;
279
280         /* we don't support multiobject RPC yet
281          * ost_brw_read() and ost_brw_write() check this */
282         LASSERT(objcount == 1);
283
284         max = *nrpages;
285         *nrpages = 0;
286         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt; i++, rnb++) {
287                 obd_off offset = rnb->offset;
288                 unsigned int len = rnb->len;
289
290                 while (len > 0) {
291                         int poff = offset & (CFS_PAGE_SIZE - 1);
292                         int plen = CFS_PAGE_SIZE - poff;
293
294                         if (*nrpages >= max) {
295                                 CERROR("small array of local bufs: %d\n", max);
296                                 RETURN(-EINVAL);
297                         }
298
299                         if (plen > len)
300                                 plen = len;
301                         lnb->offset = offset;
302                         lnb->len = plen;
303                         lnb->flags = rnb->flags;
304                         lnb->page = NULL;
305                         lnb->rc = 0;
306                         lnb->lnb_grant_used = 0;
307
308                         LASSERTF(plen <= len, "plen %u, len %u\n", plen, len);
309                         offset += plen;
310                         len -= plen;
311                         lnb++;
312                         (*nrpages)++;
313                 }
314         }
315         RETURN(0);
316 }
317
318 /*
319  * the function is used to free all pages used for request
320  * just to mimic cacheless OSS which don't occupy much memory
321  */
322 void filter_invalidate_cache(struct obd_device *obd, struct obd_ioobj *obj,
323                              struct niobuf_remote *nb, struct inode *inode)
324 {
325         struct niobuf_remote *rnb;
326         int i;
327
328         LASSERT(inode != NULL);
329
330         for (i = 0, rnb = nb; i < obj->ioo_bufcnt; i++, rnb++) {
331                 obd_off start = rnb->offset >> CFS_PAGE_SHIFT;
332                 obd_off end = (rnb->offset + rnb->len) >> CFS_PAGE_SHIFT;
333                 invalidate_mapping_pages(inode->i_mapping, start, end);
334                 /* just to avoid warnings */
335                 start = 0;
336                 end = 0;
337         }
338 }
339
340 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
341                               int objcount, struct obd_ioobj *obj,
342                               struct niobuf_remote *nb,
343                               int *npages, struct niobuf_local *res,
344                               struct obd_trans_info *oti,
345                               struct lustre_capa *capa)
346 {
347         struct obd_device *obd = exp->exp_obd;
348         struct timeval start, end;
349         struct lvfs_run_ctxt saved;
350         struct niobuf_local *lnb;
351         struct dentry *dentry = NULL;
352         struct inode *inode = NULL;
353         void *iobuf = NULL;
354         int rc = 0, i, tot_bytes = 0;
355         unsigned long now = jiffies;
356         long timediff;
357         ENTRY;
358
359         /* We are currently not supporting multi-obj BRW_READ RPCS at all.
360          * When we do this function's dentry cleanup will need to be fixed.
361          * These values are verified in ost_brw_write() from the wire. */
362         LASSERTF(objcount == 1, "%d\n", objcount);
363         LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
364
365         rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
366                               CAPA_OPC_OSS_READ);
367         if (rc)
368                 RETURN(rc);
369
370         if (oa && oa->o_valid & OBD_MD_FLGRANT) {
371                 spin_lock(&obd->obd_osfs_lock);
372                 filter_grant_incoming(exp, oa);
373
374                 oa->o_grant = 0;
375                 spin_unlock(&obd->obd_osfs_lock);
376         }
377
378         iobuf = filter_iobuf_get(&obd->u.filter, oti);
379         if (IS_ERR(iobuf))
380                 RETURN(PTR_ERR(iobuf));
381
382         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
383         dentry = filter_oa2dentry(obd, oa);
384         if (IS_ERR(dentry)) {
385                 rc = PTR_ERR(dentry);
386                 dentry = NULL;
387                 GOTO(cleanup, rc);
388         }
389
390         inode = dentry->d_inode;
391
392         obdo_to_inode(inode, oa, OBD_MD_FLATIME);
393
394         rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
395         if (rc)
396                 GOTO(cleanup, rc);
397
398         fsfilt_check_slow(obd, now, "preprw_read setup");
399
400         /* find pages for all segments, fill array with them */
401         do_gettimeofday(&start);
402         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
403
404                 lnb->dentry = dentry;
405
406                 if (i_size_read(inode) <= lnb->offset)
407                         /* If there's no more data, abort early.  lnb->rc == 0,
408                          * so it's easy to detect later. */
409                         break;
410
411                 lnb->page = filter_get_page(obd, inode, lnb->offset);
412                 if (lnb->page == NULL)
413                         GOTO(cleanup, rc = -ENOMEM);
414
415                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
416
417                 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
418                         lnb->rc = i_size_read(inode) - lnb->offset;
419                 else
420                         lnb->rc = lnb->len;
421
422                 tot_bytes += lnb->rc;
423
424                 if (PageUptodate(lnb->page)) {
425                         lprocfs_counter_add(obd->obd_stats,
426                                             LPROC_FILTER_CACHE_HIT, 1);
427                         continue;
428                 }
429
430                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_MISS, 1);
431                 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
432         }
433         do_gettimeofday(&end);
434         timediff = cfs_timeval_sub(&end, &start, NULL);
435         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
436
437         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
438                 GOTO(cleanup, rc = -ENOMEM);
439
440         fsfilt_check_slow(obd, now, "start_page_read");
441
442         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
443                               exp, NULL, NULL, NULL);
444         if (rc)
445                 GOTO(cleanup, rc);
446
447         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
448
449         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
450                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
451                                     LPROC_FILTER_READ_BYTES, tot_bytes);
452
453         EXIT;
454
455  cleanup:
456         /* unlock pages to allow access from concurrent OST_READ */
457         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
458                 if (lnb->page) {
459                         LASSERT(PageLocked(lnb->page));
460                         unlock_page(lnb->page);
461
462                         if (rc) {
463                                 page_cache_release(lnb->page);
464                                 lnb->page = NULL;
465                         }
466                 }
467         }
468
469         if (rc != 0) {
470                 if (dentry != NULL)
471                         f_dput(dentry);
472         }
473
474         filter_iobuf_put(&obd->u.filter, iobuf, oti);
475
476         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
477         if (rc)
478                 CERROR("io error %d\n", rc);
479
480         return rc;
481 }
482
483 /* When clients have dirtied as much space as they've been granted they
484  * fall through to sync writes.  These sync writes haven't been expressed
485  * in grants and need to error with ENOSPC when there isn't room in the
486  * filesystem for them after grants are taken into account.  However,
487  * writeback of the dirty data that was already granted space can write
488  * right on through.
489  *
490  * Caller must hold obd_osfs_lock. */
491 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
492                               int objcount, struct fsfilt_objinfo *fso,
493                               int niocount, struct niobuf_local *lnb,
494                               obd_size *left, struct inode *inode)
495 {
496         struct filter_export_data *fed = &exp->exp_filter_data;
497         int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
498         unsigned long used = 0, ungranted = 0, using;
499         int i, rc = -ENOSPC, obj, n = 0;
500
501         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
502
503         for (obj = 0; obj < objcount; obj++) {
504                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
505                         int tmp, bytes;
506
507                         /* should match the code in osc_exit_cache */
508                         bytes = lnb[n].len;
509                         bytes += lnb[n].offset & (blocksize - 1);
510                         tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
511                         if (tmp)
512                                 bytes += blocksize - tmp;
513
514                         if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
515                             (oa->o_valid & OBD_MD_FLGRANT)) {
516                                 if (fed->fed_grant < used + bytes) {
517                                         CDEBUG(D_CACHE,
518                                                "%s: cli %s/%p claims %ld+%d "
519                                                "GRANT, real grant %lu idx %d\n",
520                                                exp->exp_obd->obd_name,
521                                                exp->exp_client_uuid.uuid, exp,
522                                                used, bytes, fed->fed_grant, n);
523                                 } else {
524                                         used += bytes;
525                                         lnb[n].flags |= OBD_BRW_GRANTED;
526                                         lnb[n].lnb_grant_used = bytes;
527                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
528                                         rc = 0;
529                                         continue;
530                                 }
531                         }
532                         if (*left > ungranted + bytes) {
533                                 /* if enough space, pretend it was granted */
534                                 ungranted += bytes;
535                                 lnb[n].flags |= OBD_BRW_GRANTED;
536                                 lnb[n].lnb_grant_used = bytes;
537                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
538                                 rc = 0;
539                                 continue;
540                         }
541
542                         /* We can't check for already-mapped blocks here, as
543                          * it requires dropping the osfs lock to do the bmap.
544                          * Instead, we return ENOSPC and in that case we need
545                          * to go through and verify if all of the blocks not
546                          * marked BRW_GRANTED are already mapped and we can
547                          * ignore this error. */
548                         lnb[n].rc = -ENOSPC;
549                         lnb[n].flags &= ~OBD_BRW_GRANTED;
550                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
551                                exp->exp_obd->obd_name,
552                                exp->exp_client_uuid.uuid, exp, n, bytes);
553                 }
554         }
555
556         /* Now substract what client have used already.  We don't subtract
557          * this from the tot_granted yet, so that other client's can't grab
558          * that space before we have actually allocated our blocks.  That
559          * happens in filter_grant_commit() after the writes are done. */
560         *left -= ungranted;
561         fed->fed_grant -= used;
562         fed->fed_pending += used + ungranted;
563         exp->exp_obd->u.filter.fo_tot_granted += ungranted;
564         exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
565
566         CDEBUG(D_CACHE,
567                "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
568                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
569                ungranted, fed->fed_grant, fed->fed_dirty);
570
571         /* Rough calc in case we don't refresh cached statfs data */
572         using = (used + ungranted + 1 ) >>
573                 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
574         if (exp->exp_obd->obd_osfs.os_bavail > using)
575                 exp->exp_obd->obd_osfs.os_bavail -= using;
576         else
577                 exp->exp_obd->obd_osfs.os_bavail = 0;
578
579         if (fed->fed_dirty < used) {
580                 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
581                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
582                        used, fed->fed_dirty);
583                 used = fed->fed_dirty;
584         }
585         exp->exp_obd->u.filter.fo_tot_dirty -= used;
586         fed->fed_dirty -= used;
587
588         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
589                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
590                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
591                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
592                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
593                 LBUG();
594         }
595         return rc;
596 }
597
598 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
599  * on mulitple inodes.  That isn't all, because there still exists the
600  * possibility of a truncate starting a new transaction while holding the ext3
601  * rwsem = write while some writes (which have started their transactions here)
602  * blocking on the ext3 rwsem = read => lock inversion.
603  *
604  * The handling gets very ugly when dealing with locked pages.  It may be easier
605  * to just get rid of the locked page code (which has problems of its own) and
606  * either discover we do not need it anymore (i.e. it was a symptom of another
607  * bug) or ensure we get the page locks in an appropriate order. */
608 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
609                                int objcount, struct obd_ioobj *obj,
610                                struct niobuf_remote *nb, int *npages,
611                                struct niobuf_local *res,
612                                struct obd_trans_info *oti,
613                                struct lustre_capa *capa)
614 {
615         struct obd_device *obd = exp->exp_obd;
616         struct timeval start, end;
617         struct lvfs_run_ctxt saved;
618         struct niobuf_local *lnb = res;
619         struct fsfilt_objinfo fso;
620         struct filter_mod_data *fmd;
621         struct dentry *dentry = NULL;
622         void *iobuf;
623         obd_size left;
624         unsigned long now = jiffies, timediff;
625         int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
626         ENTRY;
627         LASSERT(objcount == 1);
628         LASSERT(obj->ioo_bufcnt > 0);
629
630         rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
631                               CAPA_OPC_OSS_WRITE);
632         if (rc)
633                 RETURN(rc);
634
635         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
636         iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
637         if (IS_ERR(iobuf))
638                 GOTO(cleanup, rc = PTR_ERR(iobuf));
639         cleanup_phase = 1;
640
641         dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
642                                    obj->ioo_id);
643         if (IS_ERR(dentry))
644                 GOTO(cleanup, rc = PTR_ERR(dentry));
645         cleanup_phase = 2;
646
647         if (dentry->d_inode == NULL) {
648                 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
649                        exp->exp_obd->obd_name, obj->ioo_id);
650                 GOTO(cleanup, rc = -ENOENT);
651         }
652
653         rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
654         if (rc)
655                 GOTO(cleanup, rc);
656
657         fsfilt_check_slow(exp->exp_obd, now, "preprw_write setup");
658
659         /* Don't update inode timestamps if this write is older than a
660          * setattr which modifies the timestamps. b=10150 */
661         /* XXX when we start having persistent reservations this needs to
662          * be changed to filter_fmd_get() to create the fmd if it doesn't
663          * already exist so we can store the reservation handle there. */
664         fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
665
666         LASSERT(oa != NULL);
667         spin_lock(&exp->exp_obd->obd_osfs_lock);
668         filter_grant_incoming(exp, oa);
669         if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
670                 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
671                                  OBD_MD_FLATIME);
672         else
673                 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
674                               OBD_MD_FLMTIME | OBD_MD_FLCTIME);
675         cleanup_phase = 3;
676
677         left = filter_grant_space_left(exp);
678
679         fso.fso_dentry = dentry;
680         fso.fso_bufcnt = *npages;
681
682         rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
683                                 &left, dentry->d_inode);
684
685         /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
686          * for setting UID/GID and fid EA in first write time. */
687         if (oa->o_valid & OBD_MD_FLGRANT)
688                 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
689
690         spin_unlock(&exp->exp_obd->obd_osfs_lock);
691         filter_fmd_put(exp, fmd);
692
693         if (rc)
694                 GOTO(cleanup, rc);
695
696         do_gettimeofday(&start);
697         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
698
699                 /* We still set up for ungranted pages so that granted pages
700                  * can be written to disk as they were promised, and portals
701                  * needs to keep the pages all aligned properly. */
702                 lnb->dentry = dentry;
703
704                 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset);
705                 if (lnb->page == NULL)
706                         GOTO(cleanup, rc = -ENOMEM);
707                 cleanup_phase = 4;
708
709                 /* DLM locking protects us from write and truncate competing
710                  * for same region, but truncate can leave dirty page in the
711                  * cache. it's possible the writeout on a such a page is in
712                  * progress when we access it. it's also possible that during
713                  * this writeout we put new (partial) data, but then won't
714                  * be able to proceed in filter_commitrw_write(). thus let's
715                  * just wait for writeout completion, should be rare enough.
716                  * -bzzz */
717                 if (obd->u.filter.fo_writethrough_cache)
718                         wait_on_page_writeback(lnb->page);
719                 BUG_ON(PageWriteback(lnb->page));
720
721                 /* If the filter writes a partial page, then has the file
722                  * extended, the client will read in the whole page.  the
723                  * filter has to be careful to zero the rest of the partial
724                  * page on disk.  we do it by hand for partial extending
725                  * writes, send_bio() is responsible for zeroing pages when
726                  * asked to read unmapped blocks -- brw_kiovec() does this. */
727                 if (lnb->len != CFS_PAGE_SIZE) {
728                         __s64 maxidx;
729
730                         maxidx = ((i_size_read(dentry->d_inode) +
731                                    CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
732                         if (maxidx >= lnb->page->index) {
733                                 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
734                                                LPU64" flg %x before EOF %llu\n",
735                                                lnb->len, lnb->offset,lnb->flags,
736                                                i_size_read(dentry->d_inode));
737                                 filter_iobuf_add_page(exp->exp_obd, iobuf,
738                                                       dentry->d_inode,
739                                                       lnb->page);
740                         } else {
741                                 long off;
742                                 char *p = kmap(lnb->page);
743
744                                 off = lnb->offset & ~CFS_PAGE_MASK;
745                                 if (off)
746                                         memset(p, 0, off);
747                                 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
748                                 if (off)
749                                         memset(p + off, 0, CFS_PAGE_SIZE - off);
750                                 kunmap(lnb->page);
751                         }
752                 }
753                 if (lnb->rc == 0)
754                         tot_bytes += lnb->len;
755         }
756         do_gettimeofday(&end);
757         timediff = cfs_timeval_sub(&end, &start, NULL);
758         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
759
760         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
761                 GOTO(cleanup, rc = -ENOMEM);
762
763         /* don't unlock pages to prevent any access */
764         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
765                               NULL, NULL, NULL);
766
767         fsfilt_check_slow(exp->exp_obd, now, "start_page_write");
768
769         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
770                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
771                                     LPROC_FILTER_WRITE_BYTES, tot_bytes);
772         EXIT;
773 cleanup:
774         switch(cleanup_phase) {
775         case 4:
776                 if (rc) {
777                         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
778                                 if (lnb->page != NULL) {
779                                         unlock_page(lnb->page);
780                                         page_cache_release(lnb->page);
781                                         lnb->page = NULL;
782                                 }
783                         }
784                 }
785         case 3:
786                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
787         case 2:
788                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
789                 if (rc)
790                         f_dput(dentry);
791                 break;
792         case 1:
793                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
794         case 0:
795                 spin_lock(&exp->exp_obd->obd_osfs_lock);
796                 if (oa)
797                         filter_grant_incoming(exp, oa);
798                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
799                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
800                 break;
801         default:;
802         }
803         return rc;
804 }
805
806 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
807                   int objcount, struct obd_ioobj *obj,
808                   struct niobuf_remote *nb, int *npages,
809                   struct niobuf_local *res, struct obd_trans_info *oti,
810                   struct lustre_capa *capa)
811 {
812         if (cmd == OBD_BRW_WRITE)
813                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
814                                            nb, npages, res, oti, capa);
815         if (cmd == OBD_BRW_READ)
816                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
817                                           nb, npages, res, oti, capa);
818         LBUG();
819         return -EPROTO;
820 }
821
822 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
823                                 int objcount, struct obd_ioobj *obj,
824                                 struct niobuf_remote *rnb,
825                                 int npages, struct niobuf_local *res,
826                                 struct obd_trans_info *oti, int rc)
827 {
828         struct filter_obd *fo = &exp->exp_obd->u.filter;
829         struct inode *inode = NULL;
830         struct ldlm_res_id res_id;
831         struct ldlm_resource *resource = NULL;
832         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
833         struct niobuf_local *lnb;
834         int i;
835         ENTRY;
836
837         osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
838         /* If oa != NULL then filter_preprw_read updated the inode atime
839          * and we should update the lvb so that other glimpses will also
840          * get the updated value. bug 5972 */
841         if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
842                 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
843
844                 if (resource != NULL) {
845                         ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
846                         ldlm_resource_putref(resource);
847                 }
848         }
849
850         if (res->dentry != NULL)
851                 inode = res->dentry->d_inode;
852
853         for (i = 0, lnb = res; i < npages; i++, lnb++) {
854                 if (lnb->page != NULL) {
855                         page_cache_release(lnb->page);
856                         lnb->page = NULL;
857                 }
858         }
859
860         if (inode && (fo->fo_read_cache == 0 ||
861                         i_size_read(inode) > fo->fo_readcache_max_filesize))
862                 filter_invalidate_cache(exp->exp_obd, obj, rnb, inode);
863
864         if (res->dentry != NULL)
865                 f_dput(res->dentry);
866         RETURN(rc);
867 }
868
869 void filter_grant_commit(struct obd_export *exp, int niocount,
870                          struct niobuf_local *res)
871 {
872         struct filter_obd *filter = &exp->exp_obd->u.filter;
873         struct niobuf_local *lnb = res;
874         unsigned long pending = 0;
875         int i;
876
877         spin_lock(&exp->exp_obd->obd_osfs_lock);
878         for (i = 0, lnb = res; i < niocount; i++, lnb++)
879                 pending += lnb->lnb_grant_used;
880
881         LASSERTF(exp->exp_filter_data.fed_pending >= pending,
882                  "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
883                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
884                  exp->exp_filter_data.fed_pending, pending);
885         exp->exp_filter_data.fed_pending -= pending;
886         LASSERTF(filter->fo_tot_granted >= pending,
887                  "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
888                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
889                  exp->exp_obd->u.filter.fo_tot_granted, pending);
890         filter->fo_tot_granted -= pending;
891         LASSERTF(filter->fo_tot_pending >= pending,
892                  "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
893                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
894                  filter->fo_tot_pending, pending);
895         filter->fo_tot_pending -= pending;
896
897         spin_unlock(&exp->exp_obd->obd_osfs_lock);
898 }
899
900 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
901                     int objcount, struct obd_ioobj *obj,
902                     struct niobuf_remote *nb, int npages,
903                     struct niobuf_local *res, struct obd_trans_info *oti,
904                     int rc)
905 {
906         if (cmd == OBD_BRW_WRITE)
907                 return filter_commitrw_write(exp, oa, objcount, obj,
908                                              nb, npages, res, oti, rc);
909         if (cmd == OBD_BRW_READ)
910                 return filter_commitrw_read(exp, oa, objcount, obj,
911                                             nb, npages, res, oti, rc);
912         LBUG();
913         return -EPROTO;
914 }
915
916 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
917                obd_count oa_bufs, struct brw_page *pga,
918                struct obd_trans_info *oti)
919 {
920         struct obd_ioobj ioo;
921         struct niobuf_local *lnb;
922         struct niobuf_remote *rnb;
923         obd_count i;
924         int ret = 0, npages;
925         ENTRY;
926
927         OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
928         OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
929
930         if (lnb == NULL || rnb == NULL)
931                 GOTO(out, ret = -ENOMEM);
932
933         for (i = 0; i < oa_bufs; i++) {
934                 lnb[i].page = pga[i].pg;
935                 rnb[i].offset = pga[i].off;
936                 rnb[i].len = pga[i].count;
937         }
938
939         obdo_to_ioobj(oinfo->oi_oa, &ioo);
940         ioo.ioo_bufcnt = oa_bufs;
941
942         npages = oa_bufs;
943         ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
944                             rnb, &npages, lnb, oti, oinfo_capa(oinfo));
945         if (ret != 0)
946                 GOTO(out, ret);
947         LASSERTF(oa_bufs == npages, "%u != %u\n", oa_bufs, npages);
948
949         ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo, rnb,
950                               npages, lnb, oti, ret);
951
952 out:
953         if (lnb)
954                 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
955         if (rnb)
956                 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
957         RETURN(ret);
958 }