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