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