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