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