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