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