Whamcloud - gitweb
b=16437
[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  *  linux/fs/obdfilter/filter_io.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #define DEBUG_SUBSYSTEM S_FILTER
31
32 #ifndef AUTOCONF_INCLUDED
33 #include <linux/config.h>
34 #endif
35 #include <linux/module.h>
36 #include <linux/pagemap.h> // XXX kill me soon
37 #include <linux/version.h>
38
39 #include <obd_class.h>
40 #include <obd_ost.h>
41 #include <lustre_fsfilt.h>
42 #include "filter_internal.h"
43
44 int *obdfilter_created_scratchpad;
45
46 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
47                                  struct niobuf_local *lnb)
48 {
49         struct page *page;
50
51         LASSERT(lnb->page != NULL);
52
53         page = lnb->page;
54 #if 0
55         POISON_PAGE(page, 0xf1);
56         if (lnb->len != CFS_PAGE_SIZE) {
57                 memset(kmap(page) + lnb->len, 0, CFS_PAGE_SIZE - lnb->len);
58                 kunmap(page);
59         }
60 #endif
61         page->index = lnb->offset >> CFS_PAGE_SHIFT;
62
63         RETURN(0);
64 }
65
66 static void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
67                            int niocount, struct niobuf_local *res)
68 {
69         int i, j;
70
71         for (i = 0; i < objcount; i++, obj++) {
72                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++)
73                                 res->page = NULL;
74         }
75 }
76
77 /* Grab the dirty and seen grant announcements from the incoming obdo.
78  * We will later calculate the clients new grant and return it.
79  * Caller must hold osfs lock */
80 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
81 {
82         struct filter_export_data *fed;
83         struct obd_device *obd = exp->exp_obd;
84         ENTRY;
85
86         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
87
88         if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
89                                         (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
90                 oa->o_valid &= ~OBD_MD_FLGRANT;
91                 EXIT;
92                 return;
93         }
94
95         fed = &exp->exp_filter_data;
96
97         /* Add some margin, since there is a small race if other RPCs arrive
98          * out-or-order and have already consumed some grant.  We want to
99          * leave this here in case there is a large error in accounting. */
100         CDEBUG(D_CACHE,
101                "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
102                obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
103                oa->o_dropped, fed->fed_grant);
104
105         /* Update our accounting now so that statfs takes it into account.
106          * Note that fed_dirty is only approximate and can become incorrect
107          * if RPCs arrive out-of-order.  No important calculations depend
108          * on fed_dirty however, but we must check sanity to not assert. */
109         if ((long long)oa->o_dirty < 0)
110                 oa->o_dirty = 0;
111         else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
112                 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
113         obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
114         if (fed->fed_grant < oa->o_dropped) {
115                 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
116                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
117                        oa->o_dropped, fed->fed_grant);
118                 oa->o_dropped = 0;
119         }
120         if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
121                 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
122                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
123                        oa->o_dropped, obd->u.filter.fo_tot_granted);
124                 oa->o_dropped = 0;
125         }
126         obd->u.filter.fo_tot_granted -= oa->o_dropped;
127         fed->fed_grant -= oa->o_dropped;
128         fed->fed_dirty = oa->o_dirty;
129         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
130                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
131                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
132                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
133                 spin_unlock(&obd->obd_osfs_lock);
134                 LBUG();
135         }
136         EXIT;
137 }
138
139 /* Figure out how much space is available between what we've granted
140  * and what remains in the filesystem.  Compensate for ext3 indirect
141  * block overhead when computing how much free space is left ungranted.
142  *
143  * Caller must hold obd_osfs_lock. */
144 obd_size filter_grant_space_left(struct obd_export *exp)
145 {
146         struct obd_device *obd = exp->exp_obd;
147         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
148         obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
149         int rc, statfs_done = 0;
150
151         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
152
153         if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
154 restat:
155                 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
156                                    cfs_time_current_64() + HZ);
157                 if (rc) /* N.B. statfs can't really fail */
158                         RETURN(0);
159                 statfs_done = 1;
160         }
161
162         avail = obd->obd_osfs.os_bavail;
163         left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
164         if (left > GRANT_FOR_LLOG(obd)) {
165                 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
166         } else {
167                 left = 0 /* << blockbits */;
168         }
169
170         if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
171                 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
172                 goto restat;
173         }
174
175         if (left >= tot_granted) {
176                 left -= tot_granted;
177         } else {
178                 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
179                         CERROR("%s: cli %s/%p grant "LPU64" > available "
180                                LPU64" and pending "LPU64"\n", obd->obd_name,
181                                exp->exp_client_uuid.uuid, exp, tot_granted,
182                                left, obd->u.filter.fo_tot_pending);
183                 }
184                 left = 0;
185         }
186
187         CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
188                " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
189                exp->exp_client_uuid.uuid, exp,
190                obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
191                tot_granted, left, obd->u.filter.fo_tot_pending);
192
193         return left;
194 }
195
196 /* Calculate how much grant space to allocate to this client, based on how
197  * much space is currently free and how much of that is already granted.
198  *
199  * Caller must hold obd_osfs_lock. */
200 long filter_grant(struct obd_export *exp, obd_size current_grant,
201                   obd_size want, obd_size fs_space_left)
202 {
203         struct obd_device *obd = exp->exp_obd;
204         struct filter_export_data *fed = &exp->exp_filter_data;
205         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
206         __u64 grant = 0;
207
208         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
209
210         /* Grant some fraction of the client's requested grant space so that
211          * they are not always waiting for write credits (not all of it to
212          * avoid overgranting in face of multiple RPCs in flight).  This
213          * essentially will be able to control the OSC_MAX_RIF for a client.
214          *
215          * If we do have a large disparity between what the client thinks it
216          * has and what we think it has, don't grant very much and let the
217          * client consume its grant first.  Either it just has lots of RPCs
218          * in flight, or it was evicted and its grants will soon be used up. */
219         if (want > 0x7fffffff) {
220                 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
221                        obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
222         } else if (current_grant < want &&
223                    current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
224                 grant = min((want >> blockbits),
225                             (fs_space_left >> blockbits) / 8);
226                 grant <<= blockbits;
227
228                 if (grant) {
229                         /* Allow >FILTER_GRANT_CHUNK size when clients
230                          * reconnect due to a server reboot.
231                          */
232                         if ((grant > FILTER_GRANT_CHUNK) &&
233                             (!obd->obd_recovering))
234                                 grant = FILTER_GRANT_CHUNK;
235
236                         obd->u.filter.fo_tot_granted += grant;
237                         fed->fed_grant += grant;
238                         if (fed->fed_grant < 0) {
239                                 CERROR("%s: cli %s/%p grant %ld want "LPU64
240                                        "current"LPU64"\n",
241                                        obd->obd_name, exp->exp_client_uuid.uuid,
242                                        exp, fed->fed_grant, want,current_grant);
243                                 spin_unlock(&obd->obd_osfs_lock);
244                                 LBUG();
245                         }
246                 }
247         }
248
249         CDEBUG(D_CACHE,
250                "%s: cli %s/%p wants: "LPU64" current grant "LPU64 
251                " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
252                exp, want, current_grant, grant);
253         CDEBUG(D_CACHE,
254                "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
255                " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
256                exp, obd->u.filter.fo_tot_dirty,
257                obd->u.filter.fo_tot_granted, obd->obd_num_exports);
258
259         return grant;
260 }
261
262 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
263                               int objcount, struct obd_ioobj *obj,
264                               int niocount, struct niobuf_remote *nb,
265                               struct niobuf_local *res,
266                               struct obd_trans_info *oti,
267                               struct lustre_capa *capa)
268 {
269         struct obd_device *obd = exp->exp_obd;
270         struct lvfs_run_ctxt saved;
271         struct niobuf_remote *rnb;
272         struct niobuf_local *lnb;
273         struct dentry *dentry = NULL;
274         struct inode *inode;
275         void *iobuf = NULL;
276         int rc = 0, i, tot_bytes = 0;
277         unsigned long now = jiffies;
278         ENTRY;
279
280         /* We are currently not supporting multi-obj BRW_READ RPCS at all.
281          * When we do this function's dentry cleanup will need to be fixed.
282          * These values are verified in ost_brw_write() from the wire. */
283         LASSERTF(objcount == 1, "%d\n", objcount);
284         LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
285
286         rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
287                               CAPA_OPC_OSS_READ);
288         if (rc)
289                 RETURN(rc);
290
291         if (oa && oa->o_valid & OBD_MD_FLGRANT) {
292                 spin_lock(&obd->obd_osfs_lock);
293                 filter_grant_incoming(exp, oa);
294
295                 oa->o_grant = 0;
296                 spin_unlock(&obd->obd_osfs_lock);
297         }
298
299         iobuf = filter_iobuf_get(&obd->u.filter, oti);
300         if (IS_ERR(iobuf))
301                 RETURN(PTR_ERR(iobuf));
302
303         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
304         dentry = filter_oa2dentry(obd, oa);
305         if (IS_ERR(dentry)) {
306                 rc = PTR_ERR(dentry);
307                 dentry = NULL;
308                 GOTO(cleanup, rc);
309         }
310
311         inode = dentry->d_inode;
312
313         obdo_to_inode(inode, oa, OBD_MD_FLATIME);
314         fsfilt_check_slow(obd, now, "preprw_read setup");
315
316         for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
317              i++, rnb++, lnb++) {
318                 lnb->dentry = dentry;
319                 lnb->offset = rnb->offset;
320                 lnb->len    = rnb->len;
321                 lnb->flags  = rnb->flags;
322
323                 /*
324                  * ost_brw_write()->ost_nio_pages_get() already initialized
325                  * lnb->page to point to the page from the per-thread page
326                  * pool (bug 5137), initialize page.
327                  */
328                 LASSERT(lnb->page != NULL);
329
330                 if (i_size_read(inode) <= rnb->offset)
331                         /* If there's no more data, abort early.  lnb->rc == 0,
332                          * so it's easy to detect later. */
333                         break;
334                 else
335                         filter_alloc_dio_page(obd, inode, lnb);
336
337                 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
338                         lnb->rc = i_size_read(inode) - lnb->offset;
339                 else
340                         lnb->rc = lnb->len;
341
342                 tot_bytes += lnb->rc;
343
344                 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
345         }
346
347         fsfilt_check_slow(obd, now, "start_page_read");
348
349         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
350                               exp, NULL, NULL, NULL);
351         if (rc)
352                 GOTO(cleanup, rc);
353
354         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
355
356         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
357                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
358                                     LPROC_FILTER_READ_BYTES, tot_bytes);
359
360         EXIT;
361
362  cleanup:
363         if (rc != 0) {
364                 filter_free_dio_pages(objcount, obj, niocount, res);
365
366                 if (dentry != NULL)
367                         f_dput(dentry);
368         }
369
370         filter_iobuf_put(&obd->u.filter, iobuf, oti);
371
372         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
373         if (rc)
374                 CERROR("io error %d\n", rc);
375
376         return rc;
377 }
378
379 /* When clients have dirtied as much space as they've been granted they
380  * fall through to sync writes.  These sync writes haven't been expressed
381  * in grants and need to error with ENOSPC when there isn't room in the
382  * filesystem for them after grants are taken into account.  However,
383  * writeback of the dirty data that was already granted space can write
384  * right on through.
385  *
386  * Caller must hold obd_osfs_lock. */
387 static int filter_grant_check(struct obd_export *exp, struct obdo *oa, 
388                               int objcount, struct fsfilt_objinfo *fso, 
389                               int niocount, struct niobuf_remote *rnb,
390                               struct niobuf_local *lnb, obd_size *left,
391                               struct inode *inode)
392 {
393         struct filter_export_data *fed = &exp->exp_filter_data;
394         int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
395         unsigned long used = 0, ungranted = 0, using;
396         int i, rc = -ENOSPC, obj, n = 0;
397
398         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
399
400         for (obj = 0; obj < objcount; obj++) {
401                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
402                         int tmp, bytes;
403
404                         /* should match the code in osc_exit_cache */
405                         bytes = rnb[n].len;
406                         bytes += rnb[n].offset & (blocksize - 1);
407                         tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
408                         if (tmp)
409                                 bytes += blocksize - tmp;
410
411                         if ((rnb[n].flags & OBD_BRW_FROM_GRANT) &&
412                             (oa->o_valid & OBD_MD_FLGRANT)) {
413                                 if (fed->fed_grant < used + bytes) {
414                                         CDEBUG(D_CACHE,
415                                                "%s: cli %s/%p claims %ld+%d "
416                                                "GRANT, real grant %lu idx %d\n",
417                                                exp->exp_obd->obd_name,
418                                                exp->exp_client_uuid.uuid, exp,
419                                                used, bytes, fed->fed_grant, n);
420                                 } else {
421                                         used += bytes;
422                                         rnb[n].flags |= OBD_BRW_GRANTED;
423                                         lnb[n].lnb_grant_used = bytes;
424                                         CDEBUG(0, "idx %d used=%lu\n", n, used);
425                                         rc = 0;
426                                         continue;
427                                 }
428                         }
429                         if (*left > ungranted + bytes) {
430                                 /* if enough space, pretend it was granted */
431                                 ungranted += bytes;
432                                 rnb[n].flags |= OBD_BRW_GRANTED;
433                                 lnb[n].lnb_grant_used = bytes;
434                                 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
435                                 rc = 0;
436                                 continue;
437                         }
438
439                         /* We can't check for already-mapped blocks here, as
440                          * it requires dropping the osfs lock to do the bmap.
441                          * Instead, we return ENOSPC and in that case we need
442                          * to go through and verify if all of the blocks not
443                          * marked BRW_GRANTED are already mapped and we can
444                          * ignore this error. */
445                         lnb[n].rc = -ENOSPC;
446                         rnb[n].flags &= ~OBD_BRW_GRANTED;
447                         CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
448                                exp->exp_obd->obd_name,
449                                exp->exp_client_uuid.uuid, exp, n, bytes);
450                 }
451         }
452
453         /* Now substract what client have used already.  We don't subtract
454          * this from the tot_granted yet, so that other client's can't grab
455          * that space before we have actually allocated our blocks.  That
456          * happens in filter_grant_commit() after the writes are done. */
457         *left -= ungranted;
458         fed->fed_grant -= used;
459         fed->fed_pending += used + ungranted;
460         exp->exp_obd->u.filter.fo_tot_granted += ungranted;
461         exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
462
463         CDEBUG(D_CACHE,
464                "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
465                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
466                ungranted, fed->fed_grant, fed->fed_dirty);
467
468         /* Rough calc in case we don't refresh cached statfs data */
469         using = (used + ungranted + 1 ) >>
470                 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
471         if (exp->exp_obd->obd_osfs.os_bavail > using)
472                 exp->exp_obd->obd_osfs.os_bavail -= using;
473         else
474                 exp->exp_obd->obd_osfs.os_bavail = 0;
475
476         if (fed->fed_dirty < used) {
477                 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
478                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
479                        used, fed->fed_dirty);
480                 used = fed->fed_dirty;
481         }
482         exp->exp_obd->u.filter.fo_tot_dirty -= used;
483         fed->fed_dirty -= used;
484
485         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
486                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
487                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
488                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
489                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
490                 LBUG();
491         }
492         return rc;
493 }
494
495 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
496  * on mulitple inodes.  That isn't all, because there still exists the
497  * possibility of a truncate starting a new transaction while holding the ext3
498  * rwsem = write while some writes (which have started their transactions here)
499  * blocking on the ext3 rwsem = read => lock inversion.
500  *
501  * The handling gets very ugly when dealing with locked pages.  It may be easier
502  * to just get rid of the locked page code (which has problems of its own) and
503  * either discover we do not need it anymore (i.e. it was a symptom of another
504  * bug) or ensure we get the page locks in an appropriate order. */
505 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
506                                int objcount, struct obd_ioobj *obj,
507                                int niocount, struct niobuf_remote *nb,
508                                struct niobuf_local *res,
509                                struct obd_trans_info *oti,
510                                struct lustre_capa *capa)
511 {
512         struct lvfs_run_ctxt saved;
513         struct niobuf_remote *rnb;
514         struct niobuf_local *lnb = res;
515         struct fsfilt_objinfo fso;
516         struct filter_mod_data *fmd;
517         struct dentry *dentry = NULL;
518         void *iobuf;
519         obd_size left;
520         unsigned long now = jiffies;
521         int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
522         ENTRY;
523         LASSERT(objcount == 1);
524         LASSERT(obj->ioo_bufcnt > 0);
525
526         rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
527                               CAPA_OPC_OSS_WRITE);
528         if (rc)
529                 RETURN(rc);
530
531         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
532         iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
533         if (IS_ERR(iobuf))
534                 GOTO(cleanup, rc = PTR_ERR(iobuf));
535         cleanup_phase = 1;
536
537         dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
538                                    obj->ioo_id);
539         if (IS_ERR(dentry))
540                 GOTO(cleanup, rc = PTR_ERR(dentry));
541         cleanup_phase = 2;
542
543         if (dentry->d_inode == NULL) {
544                 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
545                        exp->exp_obd->obd_name, obj->ioo_id);
546                 GOTO(cleanup, rc = -ENOENT);
547         }
548
549         fso.fso_dentry = dentry;
550         fso.fso_bufcnt = obj->ioo_bufcnt;
551
552         fsfilt_check_slow(exp->exp_obd, now, "preprw_write setup");
553
554         /* Don't update inode timestamps if this write is older than a
555          * setattr which modifies the timestamps. b=10150 */
556         /* XXX when we start having persistent reservations this needs to
557          * be changed to filter_fmd_get() to create the fmd if it doesn't
558          * already exist so we can store the reservation handle there. */
559         fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
560
561         LASSERT(oa != NULL);
562         spin_lock(&exp->exp_obd->obd_osfs_lock);
563         filter_grant_incoming(exp, oa);
564         if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
565                 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
566                                  OBD_MD_FLATIME);
567         else
568                 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
569                               OBD_MD_FLMTIME | OBD_MD_FLCTIME);
570         cleanup_phase = 3;
571
572         left = filter_grant_space_left(exp);
573
574         rc = filter_grant_check(exp, oa, objcount, &fso, niocount, nb, res,
575                                 &left, dentry->d_inode);
576
577         /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
578          * for setting UID/GID and fid EA in first write time. */
579         if (oa->o_valid & OBD_MD_FLGRANT)
580                 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
581
582         spin_unlock(&exp->exp_obd->obd_osfs_lock);
583         filter_fmd_put(exp, fmd);
584
585         if (rc)
586                 GOTO(cleanup, rc);
587
588         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
589              i++, lnb++, rnb++) {
590                 /* We still set up for ungranted pages so that granted pages
591                  * can be written to disk as they were promised, and portals
592                  * needs to keep the pages all aligned properly. */
593                 lnb->dentry = dentry;
594                 lnb->offset = rnb->offset;
595                 lnb->len    = rnb->len;
596                 lnb->flags  = rnb->flags;
597
598                 /*
599                  * ost_brw_write()->ost_nio_pages_get() already initialized
600                  * lnb->page to point to the page from the per-thread page
601                  * pool (bug 5137), initialize page.
602                  */
603                 LASSERT(lnb->page != NULL);
604                 if (lnb->len != CFS_PAGE_SIZE) {
605                         memset(kmap(lnb->page) + lnb->len,
606                                0, CFS_PAGE_SIZE - lnb->len);
607                         kunmap(lnb->page);
608                 }
609                 lnb->page->index = lnb->offset >> CFS_PAGE_SHIFT;
610
611                 cleanup_phase = 4;
612
613                 /* If the filter writes a partial page, then has the file
614                  * extended, the client will read in the whole page.  the
615                  * filter has to be careful to zero the rest of the partial
616                  * page on disk.  we do it by hand for partial extending
617                  * writes, send_bio() is responsible for zeroing pages when
618                  * asked to read unmapped blocks -- brw_kiovec() does this. */
619                 if (lnb->len != CFS_PAGE_SIZE) {
620                         __s64 maxidx;
621
622                         maxidx = ((i_size_read(dentry->d_inode) +
623                                    CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
624                         if (maxidx >= lnb->page->index) {
625                                 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
626                                                LPU64" flg %x before EOF %llu\n",
627                                                lnb->len, lnb->offset,lnb->flags,
628                                                i_size_read(dentry->d_inode));
629                                 filter_iobuf_add_page(exp->exp_obd, iobuf,
630                                                       dentry->d_inode,
631                                                       lnb->page);
632                         } else {
633                                 long off;
634                                 char *p = kmap(lnb->page);
635
636                                 off = lnb->offset & ~CFS_PAGE_MASK;
637                                 if (off)
638                                         memset(p, 0, off);
639                                 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
640                                 if (off)
641                                         memset(p + off, 0, CFS_PAGE_SIZE - off);
642                                 kunmap(lnb->page);
643                         }
644                 }
645                 if (lnb->rc == 0)
646                         tot_bytes += lnb->len;
647         }
648
649         rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
650                               NULL, NULL, NULL);
651
652         fsfilt_check_slow(exp->exp_obd, now, "start_page_write");
653
654         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
655                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
656                                     LPROC_FILTER_WRITE_BYTES, tot_bytes);
657         EXIT;
658 cleanup:
659         switch(cleanup_phase) {
660         case 4:
661         case 3:
662                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
663         case 2:
664                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
665                 if (rc)
666                         f_dput(dentry);
667                 break;
668         case 1:
669                 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
670         case 0:
671                 spin_lock(&exp->exp_obd->obd_osfs_lock);
672                 if (oa)
673                         filter_grant_incoming(exp, oa);
674                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
675                 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
676                 break;
677         default:;
678         }
679         return rc;
680 }
681
682 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
683                   int objcount, struct obd_ioobj *obj, int niocount,
684                   struct niobuf_remote *nb, struct niobuf_local *res,
685                   struct obd_trans_info *oti, struct lustre_capa *capa)
686 {
687         if (cmd == OBD_BRW_WRITE)
688                 return filter_preprw_write(cmd, exp, oa, objcount, obj,
689                                            niocount, nb, res, oti, capa);
690         if (cmd == OBD_BRW_READ)
691                 return filter_preprw_read(cmd, exp, oa, objcount, obj,
692                                           niocount, nb, res, oti, capa);
693         LBUG();
694         return -EPROTO;
695 }
696
697 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
698                               struct page *page)
699 {
700         int drop = 0;
701
702         if (inode != NULL &&
703             (i_size_read(inode) > filter->fo_readcache_max_filesize))
704                 drop = 1;
705
706         /* drop from cache like truncate_list_pages() */
707         if (drop && !TryLockPage(page)) {
708                 if (page->mapping)
709                         ll_truncate_complete_page(page);
710                 unlock_page(page);
711         }
712         page_cache_release(page);
713 }
714
715 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
716                                 int objcount, struct obd_ioobj *obj,
717                                 int niocount, struct niobuf_local *res,
718                                 struct obd_trans_info *oti, int rc)
719 {
720         struct inode *inode = NULL;
721         struct ldlm_res_id res_id;
722         struct ldlm_resource *resource = NULL;
723         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
724         ENTRY;
725
726         osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
727         /* If oa != NULL then filter_preprw_read updated the inode atime
728          * and we should update the lvb so that other glimpses will also
729          * get the updated value. bug 5972 */
730         if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
731                 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
732
733                 if (resource != NULL) {
734                         ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
735                         ldlm_resource_putref(resource);
736                 }
737         }
738
739         if (res->dentry != NULL)
740                 inode = res->dentry->d_inode;
741
742         filter_free_dio_pages(objcount, obj, niocount, res);
743
744         if (res->dentry != NULL)
745                 f_dput(res->dentry);
746         RETURN(rc);
747 }
748
749 void flip_into_page_cache(struct inode *inode, struct page *new_page)
750 {
751         struct page *old_page;
752         int rc;
753
754         do {
755                 /* the dlm is protecting us from read/write concurrency, so we
756                  * expect this find_lock_page to return quickly.  even if we
757                  * race with another writer it won't be doing much work with
758                  * the page locked.  we do this 'cause t_c_p expects a
759                  * locked page, and it wants to grab the pagecache lock
760                  * as well. */
761                 old_page = find_lock_page(inode->i_mapping, new_page->index);
762                 if (old_page) {
763                         ll_truncate_complete_page(old_page);
764                         unlock_page(old_page);
765                         page_cache_release(old_page);
766                 }
767
768 #if 0 /* this should be a /proc tunable someday */
769                 /* racing o_directs (no locking ioctl) could race adding
770                  * their pages, so we repeat the page invalidation unless
771                  * we successfully added our new page */
772                 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
773                                               new_page->index,
774                                               page_hash(inode->i_mapping,
775                                                         new_page->index));
776                 if (rc == 0) {
777                         /* add_to_page_cache clears uptodate|dirty and locks
778                          * the page */
779                         SetPageUptodate(new_page);
780                         unlock_page(new_page);
781                 }
782 #else
783                 rc = 0;
784 #endif
785         } while (rc != 0);
786 }
787
788 void filter_grant_commit(struct obd_export *exp, int niocount,
789                          struct niobuf_local *res)
790 {
791         struct filter_obd *filter = &exp->exp_obd->u.filter;
792         struct niobuf_local *lnb = res;
793         unsigned long pending = 0;
794         int i;
795
796         spin_lock(&exp->exp_obd->obd_osfs_lock);
797         for (i = 0, lnb = res; i < niocount; i++, lnb++)
798                 pending += lnb->lnb_grant_used;
799
800         LASSERTF(exp->exp_filter_data.fed_pending >= pending,
801                  "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
802                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
803                  exp->exp_filter_data.fed_pending, pending);
804         exp->exp_filter_data.fed_pending -= pending;
805         LASSERTF(filter->fo_tot_granted >= pending,
806                  "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
807                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
808                  exp->exp_obd->u.filter.fo_tot_granted, pending);
809         filter->fo_tot_granted -= pending;
810         LASSERTF(filter->fo_tot_pending >= pending,
811                  "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
812                  exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
813                  filter->fo_tot_pending, pending);
814         filter->fo_tot_pending -= pending;
815
816         spin_unlock(&exp->exp_obd->obd_osfs_lock);
817 }
818
819 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
820                     int objcount, struct obd_ioobj *obj, int niocount,
821                     struct niobuf_local *res, struct obd_trans_info *oti,
822                     int rc)
823 {
824         if (cmd == OBD_BRW_WRITE)
825                 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
826                                              res, oti, rc);
827         if (cmd == OBD_BRW_READ)
828                 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
829                                             res, oti, rc);
830         LBUG();
831         return -EPROTO;
832 }
833
834 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
835                obd_count oa_bufs, struct brw_page *pga,
836                struct obd_trans_info *oti)
837 {
838         struct obd_ioobj ioo;
839         struct niobuf_local *lnb;
840         struct niobuf_remote *rnb;
841         obd_count i;
842         int ret = 0;
843         ENTRY;
844
845         OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
846         OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
847
848         if (lnb == NULL || rnb == NULL)
849                 GOTO(out, ret = -ENOMEM);
850
851         for (i = 0; i < oa_bufs; i++) {
852                 lnb[i].page = pga[i].pg;
853                 rnb[i].offset = pga[i].off;
854                 rnb[i].len = pga[i].count;
855         }
856
857         obdo_to_ioobj(oinfo->oi_oa, &ioo);
858         ioo.ioo_bufcnt = oa_bufs;
859
860         ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
861                             oa_bufs, rnb, lnb, oti, oinfo_capa(oinfo));
862         if (ret != 0)
863                 GOTO(out, ret);
864
865         ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
866                               oa_bufs, lnb, oti, ret);
867
868 out:
869         if (lnb)
870                 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
871         if (rnb)
872                 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
873         RETURN(ret);
874 }