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