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