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