Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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
476         EXIT;
477
478  cleanup:
479         /* unlock pages to allow access from concurrent OST_READ */
480         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
481                 if (lnb->page) {
482                         LASSERT(PageLocked(lnb->page));
483                         unlock_page(lnb->page);
484
485                         if (rc) {
486                                 page_cache_release(lnb->page);
487                                 lnb->page = NULL;
488                         }
489                 }
490         }
491
492         if (rc != 0) {
493                 if (dentry != NULL)
494                         f_dput(dentry);
495         }
496
497         filter_iobuf_put(&obd->u.filter, iobuf, oti);
498
499         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
500         if (rc)
501                 CERROR("io error %d\n", rc);
502
503         return rc;
504 }
505
506 /* When clients have dirtied as much space as they've been granted they
507  * fall through to sync writes.  These sync writes haven't been expressed
508  * in grants and need to error with ENOSPC when there isn't room in the
509  * filesystem for them after grants are taken into account.  However,
510  * writeback of the dirty data that was already granted space can write
511  * right on through.
512  *
513  * Caller must hold obd_osfs_lock. */
514 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
515                               int objcount, struct fsfilt_objinfo *fso,
516                               int niocount, struct niobuf_local *lnb,
517                               obd_size *left, struct inode *inode)
518 {
519         struct filter_export_data *fed = &exp->exp_filter_data;
520         int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
521         unsigned long used = 0, ungranted = 0, using;
522         int i, rc = -ENOSPC, obj, n = 0;
523         int resend = 0;
524
525         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
526             (oa->o_flags & OBD_FL_RECOV_RESEND)) {
527                 resend = 1;
528                 CDEBUG(D_CACHE, "Recoverable resend arrived, skipping "
529                        "accounting\n");
530         }
531
532         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
533
534         for (obj = 0; obj < objcount; obj++) {
535                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
536                         int tmp, bytes;
537
538                         /* should match the code in osc_exit_cache */
539                         bytes = lnb[n].len;
540                         bytes += lnb[n].offset & (blocksize - 1);
541                         tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
542                         if (tmp)
543                                 bytes += blocksize - tmp;
544
545                         if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
546                             (oa->o_valid & OBD_MD_FLGRANT)) {
547                                 if (resend) {
548                                         /* this is a recoverable resent */
549                                         lnb[n].flags |= OBD_BRW_GRANTED;
550                                         rc = 0;
551                                         continue;
552                                 } else if (fed->fed_grant < used + bytes) {
553                                         CDEBUG(D_CACHE,
554                                                "%s: cli %s/%p claims %ld+%d "
555                                                "GRANT, real grant %lu idx %d\n",
556                                                exp->exp_obd->obd_name,
557                                                exp->exp_client_uuid.uuid, exp,
558                                                used, bytes, fed->fed_grant, n);
559                                 } else {
560                                         used += bytes;
561                                         lnb[n].flags |= OBD_BRW_GRANTED;
562                                         lnb[n].lnb_grant_used = bytes;
563                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
564                                         rc = 0;
565                                         continue;
566                                 }
567                         }
568                         if (*left > ungranted + bytes) {
569                                 /* if enough space, pretend it was granted */
570                                 ungranted += bytes;
571                                 lnb[n].flags |= OBD_BRW_GRANTED;
572                                 lnb[n].lnb_grant_used = bytes;
573                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
574                                 rc = 0;
575                                 continue;
576                         }
577
578                         /* We can't check for already-mapped blocks here, as
579                          * it requires dropping the osfs lock to do the bmap.
580                          * Instead, we return ENOSPC and in that case we need
581                          * to go through and verify if all of the blocks not
582                          * marked BRW_GRANTED are already mapped and we can
583                          * ignore this error. */
584                         lnb[n].rc = -ENOSPC;
585                         lnb[n].flags &= ~OBD_BRW_GRANTED;
586                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
587                                exp->exp_obd->obd_name,
588                                exp->exp_client_uuid.uuid, exp, n, bytes);
589                 }
590         }
591
592         /* Now substract what client have used already.  We don't subtract
593          * this from the tot_granted yet, so that other client's can't grab
594          * that space before we have actually allocated our blocks.  That
595          * happens in filter_grant_commit() after the writes are done. */
596         *left -= ungranted;
597         fed->fed_grant -= used;
598         fed->fed_pending += used + ungranted;
599         exp->exp_obd->u.filter.fo_tot_granted += ungranted;
600         exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
601
602         CDEBUG(D_CACHE,
603                "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
604                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
605                ungranted, fed->fed_grant, fed->fed_dirty);
606
607         /* Rough calc in case we don't refresh cached statfs data */
608         using = (used + ungranted + 1 ) >>
609                 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
610         if (exp->exp_obd->obd_osfs.os_bavail > using)
611                 exp->exp_obd->obd_osfs.os_bavail -= using;
612         else
613                 exp->exp_obd->obd_osfs.os_bavail = 0;
614
615         if (fed->fed_dirty < used) {
616                 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
617                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
618                        used, fed->fed_dirty);
619                 used = fed->fed_dirty;
620         }
621         exp->exp_obd->u.filter.fo_tot_dirty -= used;
622         fed->fed_dirty -= used;
623
624         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
625                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
626                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
627                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
628                 cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
629                 LBUG();
630         }
631         return rc;
632 }
633
634 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
635  * on mulitple inodes.  That isn't all, because there still exists the
636  * possibility of a truncate starting a new transaction while holding the ext3
637  * rwsem = write while some writes (which have started their transactions here)
638  * blocking on the ext3 rwsem = read => lock inversion.
639  *
640  * The handling gets very ugly when dealing with locked pages.  It may be easier
641  * to just get rid of the locked page code (which has problems of its own) and
642  * either discover we do not need it anymore (i.e. it was a symptom of another
643  * bug) or ensure we get the page locks in an appropriate order. */
644 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
645                                int objcount, struct obd_ioobj *obj,
646                                struct niobuf_remote *nb, int *npages,
647                                struct niobuf_local *res,
648                                struct obd_trans_info *oti,
649                                struct lustre_capa *capa)
650 {
651         struct obd_device *obd = exp->exp_obd;
652         struct timeval start, end;
653         struct lvfs_run_ctxt saved;
654         struct niobuf_local *lnb = res;
655         struct fsfilt_objinfo fso;
656         struct filter_mod_data *fmd;
657         struct dentry *dentry = NULL;
658         void *iobuf;
659         obd_size left;
660         unsigned long now = jiffies, timediff;
661         int rc = 0, i, tot_bytes = 0, cleanup_phase = 0, localreq = 0;
662         ENTRY;
663         LASSERT(objcount == 1);
664         LASSERT(obj->ioo_bufcnt > 0);
665
666         rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
667                               CAPA_OPC_OSS_WRITE);
668         if (rc)
669                 RETURN(rc);
670
671         if (exp->exp_connection &&
672             exp->exp_connection->c_peer.nid == exp->exp_connection->c_self)
673                 localreq = 1;
674
675         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
676         iobuf = filter_iobuf_get(&obd->u.filter, oti);
677         if (IS_ERR(iobuf))
678                 GOTO(cleanup, rc = PTR_ERR(iobuf));
679         cleanup_phase = 1;
680
681         dentry = filter_fid2dentry(obd, NULL, obj->ioo_seq,
682                                    obj->ioo_id);
683         if (IS_ERR(dentry))
684                 GOTO(cleanup, rc = PTR_ERR(dentry));
685         cleanup_phase = 2;
686
687         if (dentry->d_inode == NULL) {
688                 if (exp->exp_obd->obd_recovering) {
689                         struct obdo *noa = oa;
690
691                         if (oa == NULL) {
692                                 OBDO_ALLOC(noa);
693                                 if (noa == NULL)
694                                         GOTO(recreate_out, rc = -ENOMEM);
695                                 noa->o_id = obj->ioo_id;
696                                 noa->o_valid = OBD_MD_FLID;
697                         }
698
699                         if (filter_create(exp, noa, NULL, oti) == 0) {
700                                 f_dput(dentry);
701                                 dentry = filter_fid2dentry(exp->exp_obd, NULL,
702                                                            obj->ioo_seq,
703                                                            obj->ioo_id);
704                         }
705                         if (oa == NULL)
706                                 OBDO_FREE(noa);
707                 }
708     recreate_out:
709                 if (IS_ERR(dentry) || dentry->d_inode == NULL) {
710                         CERROR("%s: BRW to missing obj "LPU64"/"LPU64":rc %d\n",
711                                exp->exp_obd->obd_name,
712                                obj->ioo_id, obj->ioo_seq,
713                                IS_ERR(dentry) ? (int)PTR_ERR(dentry) : -ENOENT);
714                         if (IS_ERR(dentry))
715                                 cleanup_phase = 1;
716                         GOTO(cleanup, rc = -ENOENT);
717                 }
718         }
719
720         if (oa->o_valid & (OBD_MD_FLUID | OBD_MD_FLGID) &&
721             dentry->d_inode->i_mode & (S_ISUID | S_ISGID)) {
722                 rc = filter_capa_fixoa(exp, oa, oa->o_seq, capa);
723                 if (rc)
724                         GOTO(cleanup, rc);
725         }
726
727         rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
728         if (rc)
729                 GOTO(cleanup, rc);
730
731         fsfilt_check_slow(obd, now, "preprw_write setup");
732
733         /* Filter truncate first locks i_mutex then partially truncated
734          * page, filter write code first locks pages then take
735          * i_mutex.  To avoid a deadlock in case of concurrent
736          * punch/write requests from one client, filter writes and
737          * filter truncates are serialized by i_alloc_sem, allowing
738          * multiple writes or single truncate. */
739         down_read(&dentry->d_inode->i_alloc_sem);
740         fsfilt_check_slow(obd, now, "i_alloc_sem");
741
742         /* Don't update inode timestamps if this write is older than a
743          * setattr which modifies the timestamps. b=10150 */
744         /* XXX when we start having persistent reservations this needs to
745          * be changed to filter_fmd_get() to create the fmd if it doesn't
746          * already exist so we can store the reservation handle there. */
747         fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_seq);
748
749         LASSERT(oa != NULL);
750         cfs_spin_lock(&obd->obd_osfs_lock);
751         filter_grant_incoming(exp, oa);
752         if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
753                 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
754                                  OBD_MD_FLATIME);
755         else
756                 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
757                               OBD_MD_FLMTIME | OBD_MD_FLCTIME);
758         cleanup_phase = 3;
759
760         left = filter_grant_space_left(exp);
761
762         fso.fso_dentry = dentry;
763         fso.fso_bufcnt = *npages;
764
765         rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
766                                 &left, dentry->d_inode);
767
768         /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
769          * for setting UID/GID and fid EA in first write time. */
770         /* If OBD_FL_SHRINK_GRANT is set, the client just returned us some grant
771          * so no sense in allocating it some more. We either return the grant
772          * back to the client if we have plenty of space or we don't return
773          * anything if we are short. This was decided in filter_grant_incoming*/
774         if ((oa->o_valid & OBD_MD_FLGRANT) &&
775             (!(oa->o_valid & OBD_MD_FLFLAGS) ||
776              !(oa->o_flags & OBD_FL_SHRINK_GRANT)))
777                 oa->o_grant = filter_grant(exp, oa->o_grant, oa->o_undirty,
778                                            left, 1);
779
780         cfs_spin_unlock(&obd->obd_osfs_lock);
781         filter_fmd_put(exp, fmd);
782
783         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK2, (obd_timeout + 1) / 4);
784
785         if (rc)
786                 GOTO(cleanup, rc);
787         cleanup_phase = 4;
788
789         cfs_gettimeofday(&start);
790         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
791
792                 /* We still set up for ungranted pages so that granted pages
793                  * can be written to disk as they were promised, and portals
794                  * needs to keep the pages all aligned properly. */
795                 lnb->dentry = dentry;
796
797                 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset,
798                                             localreq);
799                 if (lnb->page == NULL)
800                         GOTO(cleanup, rc = -ENOMEM);
801
802                 /* DLM locking protects us from write and truncate competing
803                  * for same region, but truncate can leave dirty page in the
804                  * cache. it's possible the writeout on a such a page is in
805                  * progress when we access it. it's also possible that during
806                  * this writeout we put new (partial) data, but then won't
807                  * be able to proceed in filter_commitrw_write(). thus let's
808                  * just wait for writeout completion, should be rare enough.
809                  * -bzzz */
810                 wait_on_page_writeback(lnb->page);
811                 BUG_ON(PageWriteback(lnb->page));
812
813                 /* If the filter writes a partial page, then has the file
814                  * extended, the client will read in the whole page.  the
815                  * filter has to be careful to zero the rest of the partial
816                  * page on disk.  we do it by hand for partial extending
817                  * writes, send_bio() is responsible for zeroing pages when
818                  * asked to read unmapped blocks -- brw_kiovec() does this. */
819                 if (lnb->len != CFS_PAGE_SIZE) {
820                         __s64 maxidx;
821
822                         maxidx = ((i_size_read(dentry->d_inode) +
823                                    CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
824                         if (maxidx >= lnb->page->index) {
825                                 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
826                                                LPU64" flg %x before EOF %llu\n",
827                                                lnb->len, lnb->offset,lnb->flags,
828                                                i_size_read(dentry->d_inode));
829                                 filter_iobuf_add_page(obd, iobuf,
830                                                       dentry->d_inode,
831                                                       lnb->page);
832                         } else {
833                                 long off;
834                                 char *p = kmap(lnb->page);
835
836                                 off = lnb->offset & ~CFS_PAGE_MASK;
837                                 if (off)
838                                         memset(p, 0, off);
839                                 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
840                                 if (off)
841                                         memset(p + off, 0, CFS_PAGE_SIZE - off);
842                                 kunmap(lnb->page);
843                         }
844                 }
845                 if (lnb->rc == 0)
846                         tot_bytes += lnb->len;
847         }
848         cfs_gettimeofday(&end);
849         timediff = cfs_timeval_sub(&end, &start, NULL);
850         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
851
852         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
853                 GOTO(cleanup, rc = -ENOMEM);
854
855         /* don't unlock pages to prevent any access */
856         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
857                               NULL, NULL, NULL);
858
859         fsfilt_check_slow(obd, now, "start_page_write");
860
861         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
862                             tot_bytes);
863
864         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
865                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
866                                     LPROC_FILTER_WRITE_BYTES, tot_bytes);
867         EXIT;
868 cleanup:
869         switch(cleanup_phase) {
870         case 4:
871                 if (rc) {
872                         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
873                                 if (lnb->page != NULL) {
874                                         unlock_page(lnb->page);
875                                         page_cache_release(lnb->page);
876                                         lnb->page = NULL;
877                                 }
878                         }
879                 }
880         case 3:
881                 if (rc)
882                         up_read(&dentry->d_inode->i_alloc_sem);
883
884                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
885         case 2:
886                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
887                 if (rc)
888                         f_dput(dentry);
889                 break;
890         case 1:
891                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
892         case 0:
893                 cfs_spin_lock(&obd->obd_osfs_lock);
894                 if (oa)
895                         filter_grant_incoming(exp, oa);
896                 cfs_spin_unlock(&obd->obd_osfs_lock);
897                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
898                 break;
899         default:;
900         }
901         return rc;
902 }
903
904 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
905                   int objcount, struct obd_ioobj *obj,
906                   struct niobuf_remote *nb, int *npages,
907                   struct niobuf_local *res, struct obd_trans_info *oti,
908                   struct lustre_capa *capa)
909 {
910         if (cmd == OBD_BRW_WRITE)
911                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
912                                            nb, npages, res, oti, capa);
913         if (cmd == OBD_BRW_READ)
914                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
915                                           nb, npages, res, oti, capa);
916         LBUG();
917         return -EPROTO;
918 }
919
920 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
921                                 int objcount, struct obd_ioobj *obj,
922                                 struct niobuf_remote *rnb,
923                                 int npages, struct niobuf_local *res,
924                                 struct obd_trans_info *oti, int rc)
925 {
926         struct filter_obd *fo = &exp->exp_obd->u.filter;
927         struct inode *inode = NULL;
928         struct ldlm_res_id res_id;
929         struct ldlm_resource *resource = NULL;
930         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
931         struct niobuf_local *lnb;
932         int i;
933         ENTRY;
934
935         osc_build_res_name(obj->ioo_id, obj->ioo_seq, &res_id);
936         /* If oa != NULL then filter_preprw_read updated the inode atime
937          * and we should update the lvb so that other glimpses will also
938          * get the updated value. bug 5972 */
939         if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
940                 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
941
942                 if (resource != NULL) {
943                         LDLM_RESOURCE_ADDREF(resource);
944                         ns->ns_lvbo->lvbo_update(resource, NULL, 1);
945                         LDLM_RESOURCE_DELREF(resource);
946                         ldlm_resource_putref(resource);
947                 }
948         }
949
950         if (res->dentry != NULL)
951                 inode = res->dentry->d_inode;
952
953         for (i = 0, lnb = res; i < npages; i++, lnb++) {
954                 if (lnb->page != NULL) {
955                         page_cache_release(lnb->page);
956                         lnb->page = NULL;
957                 }
958         }
959         if (inode && (fo->fo_read_cache == 0 ||
960                       i_size_read(inode) > fo->fo_readcache_max_filesize))
961                 filter_release_cache(exp->exp_obd, obj, rnb, inode);
962
963         if (res->dentry != NULL)
964                 f_dput(res->dentry);
965         RETURN(rc);
966 }
967
968 void filter_grant_commit(struct obd_export *exp, int niocount,
969                          struct niobuf_local *res)
970 {
971         struct filter_obd *filter = &exp->exp_obd->u.filter;
972         struct niobuf_local *lnb = res;
973         unsigned long pending = 0;
974         int i;
975
976         cfs_spin_lock(&exp->exp_obd->obd_osfs_lock);
977         for (i = 0, lnb = res; i < niocount; i++, lnb++)
978                 pending += lnb->lnb_grant_used;
979
980         LASSERTF(exp->exp_filter_data.fed_pending >= pending,
981                  "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
982                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
983                  exp->exp_filter_data.fed_pending, pending);
984         exp->exp_filter_data.fed_pending -= pending;
985         LASSERTF(filter->fo_tot_granted >= pending,
986                  "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
987                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
988                  exp->exp_obd->u.filter.fo_tot_granted, pending);
989         filter->fo_tot_granted -= pending;
990         LASSERTF(filter->fo_tot_pending >= pending,
991                  "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
992                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
993                  filter->fo_tot_pending, pending);
994         filter->fo_tot_pending -= pending;
995
996         cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
997 }
998
999 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1000                     int objcount, struct obd_ioobj *obj,
1001                     struct niobuf_remote *nb, int npages,
1002                     struct niobuf_local *res, struct obd_trans_info *oti,
1003                     int rc)
1004 {
1005         if (cmd == OBD_BRW_WRITE)
1006                 return filter_commitrw_write(exp, oa, objcount, obj,
1007                                              nb, npages, res, oti, rc);
1008         if (cmd == OBD_BRW_READ)
1009                 return filter_commitrw_read(exp, oa, objcount, obj,
1010                                             nb, npages, res, oti, rc);
1011         LBUG();
1012         return -EPROTO;
1013 }
1014