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