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