1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdfilter/filter_io.c
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>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
30 #define DEBUG_SUBSYSTEM S_FILTER
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/pagemap.h> // XXX kill me soon
35 #include <linux/version.h>
37 #include <obd_class.h>
38 #include <lustre_fsfilt.h>
39 #include "filter_internal.h"
41 int *obdfilter_created_scratchpad;
43 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
44 struct niobuf_local *lnb)
48 LASSERT(lnb->page != NULL);
52 POISON_PAGE(page, 0xf1);
53 if (lnb->len != PAGE_SIZE) {
54 memset(kmap(page) + lnb->len, 0, PAGE_SIZE - lnb->len);
58 page->index = lnb->offset >> PAGE_SHIFT;
63 static void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
64 int niocount, struct niobuf_local *res)
68 for (i = 0; i < objcount; i++, obj++) {
69 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++)
74 /* Grab the dirty and seen grant announcements from the incoming obdo.
75 * We will later calculate the clients new grant and return it.
76 * Caller must hold osfs lock */
77 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
79 struct filter_export_data *fed;
80 struct obd_device *obd = exp->exp_obd;
81 static unsigned long last_msg;
82 static int last_count;
86 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
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;
95 fed = &exp->exp_filter_data;
97 /* Don't print this to the console the first time it happens, since
98 * it can happen legitimately on occasion, but only rarely. */
99 if (time_after(jiffies, last_msg + 60 * HZ)) {
103 if ((last_count & (-last_count)) == last_count)
104 mask = D_HA /* until bug 3273 is fixed D_WARNING */;
107 /* Add some margin, since there is a small race if other RPCs arrive
108 * out-or-order and have already consumed some grant. We want to
109 * leave this here in case there is a large error in accounting. */
110 CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ? mask:D_CACHE,
111 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
112 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
113 oa->o_dropped, fed->fed_grant);
115 /* Update our accounting now so that statfs takes it into account.
116 * Note that fed_dirty is only approximate and can become incorrect
117 * if RPCs arrive out-of-order. No important calculations depend
118 * on fed_dirty however, but we must check sanity to not assert. */
119 if ((long long)oa->o_dirty < 0)
121 else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
122 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
123 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
124 if (fed->fed_grant < oa->o_dropped) {
125 CDEBUG(D_HA,"%s: cli %s/%p reports %u dropped > fedgrant %lu\n",
126 obd->obd_name, exp->exp_client_uuid.uuid, exp,
127 oa->o_dropped, fed->fed_grant);
130 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
131 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
132 obd->obd_name, exp->exp_client_uuid.uuid, exp,
133 oa->o_dropped, obd->u.filter.fo_tot_granted);
136 obd->u.filter.fo_tot_granted -= oa->o_dropped;
137 fed->fed_grant -= oa->o_dropped;
138 fed->fed_dirty = oa->o_dirty;
139 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
140 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
141 obd->obd_name, exp->exp_client_uuid.uuid, exp,
142 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
143 spin_unlock(&obd->obd_osfs_lock);
149 /* Figure out how much space is available between what we've granted
150 * and what remains in the filesystem. Compensate for ext3 indirect
151 * block overhead when computing how much free space is left ungranted.
153 * Caller must hold obd_osfs_lock. */
154 obd_size filter_grant_space_left(struct obd_export *exp)
156 struct obd_device *obd = exp->exp_obd;
157 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
158 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
159 int rc, statfs_done = 0;
161 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
163 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
165 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
166 cfs_time_current_64() + HZ);
167 if (rc) /* N.B. statfs can't really fail */
172 avail = obd->obd_osfs.os_bavail;
173 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
174 if (left > GRANT_FOR_LLOG(obd)) {
175 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
177 left = 0 /* << blockbits */;
180 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
181 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
185 if (left >= tot_granted) {
188 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
189 CERROR("%s: cli %s/%p grant "LPU64" > available "
190 LPU64" and pending "LPU64"\n", obd->obd_name,
191 exp->exp_client_uuid.uuid, exp, tot_granted,
192 left, obd->u.filter.fo_tot_pending);
197 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
198 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
199 exp->exp_client_uuid.uuid, exp,
200 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
201 tot_granted, left, obd->u.filter.fo_tot_pending);
206 /* Calculate how much grant space to allocate to this client, based on how
207 * much space is currently free and how much of that is already granted.
209 * Caller must hold obd_osfs_lock. */
210 long filter_grant(struct obd_export *exp, obd_size current_grant,
211 obd_size want, obd_size fs_space_left)
213 struct obd_device *obd = exp->exp_obd;
214 struct filter_export_data *fed = &exp->exp_filter_data;
215 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
218 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
220 /* Grant some fraction of the client's requested grant space so that
221 * they are not always waiting for write credits (not all of it to
222 * avoid overgranting in face of multiple RPCs in flight). This
223 * essentially will be able to control the OSC_MAX_RIF for a client.
225 * If we do have a large disparity between what the client thinks it
226 * has and what we think it has, don't grant very much and let the
227 * client consume its grant first. Either it just has lots of RPCs
228 * in flight, or it was evicted and its grants will soon be used up. */
229 if (want > 0x7fffffff) {
230 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
231 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
232 } else if (current_grant < want &&
233 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
234 grant = min((want >> blockbits),
235 (fs_space_left >> blockbits) / 8);
239 /* Allow >FILTER_GRANT_CHUNK size when clients
240 * reconnect due to a server reboot.
242 if ((grant > FILTER_GRANT_CHUNK) &&
243 (!obd->obd_recovering))
244 grant = FILTER_GRANT_CHUNK;
246 obd->u.filter.fo_tot_granted += grant;
247 fed->fed_grant += grant;
248 if (fed->fed_grant < 0) {
249 CERROR("%s: cli %s/%p grant %ld want "LPU64
251 obd->obd_name, exp->exp_client_uuid.uuid,
252 exp, fed->fed_grant, want,current_grant);
253 spin_unlock(&obd->obd_osfs_lock);
259 CDEBUG(D_CACHE,"%s: cli %s/%p wants: "LPU64" granting: "LPU64"\n",
260 obd->obd_name, exp->exp_client_uuid.uuid, exp, want, grant);
262 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
263 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
264 exp, obd->u.filter.fo_tot_dirty,
265 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
270 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
271 int objcount, struct obd_ioobj *obj,
272 int niocount, struct niobuf_remote *nb,
273 struct niobuf_local *res,
274 struct obd_trans_info *oti)
276 struct obd_device *obd = exp->exp_obd;
277 struct lvfs_run_ctxt saved;
278 struct niobuf_remote *rnb;
279 struct niobuf_local *lnb;
280 struct dentry *dentry = NULL;
283 int rc = 0, i, tot_bytes = 0;
284 unsigned long now = jiffies;
287 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
288 * When we do this function's dentry cleanup will need to be fixed.
289 * These values are verified in ost_brw_write() from the wire. */
290 LASSERTF(objcount == 1, "%d\n", objcount);
291 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
293 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
294 spin_lock(&obd->obd_osfs_lock);
295 filter_grant_incoming(exp, oa);
298 spin_unlock(&obd->obd_osfs_lock);
301 iobuf = filter_iobuf_get(&obd->u.filter, oti);
303 RETURN(PTR_ERR(iobuf));
305 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
306 dentry = filter_oa2dentry(obd, oa);
307 if (IS_ERR(dentry)) {
308 rc = PTR_ERR(dentry);
313 inode = dentry->d_inode;
315 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
316 fsfilt_check_slow(obd, now, obd_timeout, "preprw_read setup");
318 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
320 lnb->dentry = dentry;
321 lnb->offset = rnb->offset;
323 lnb->flags = rnb->flags;
326 * ost_brw_write()->ost_nio_pages_get() already initialized
327 * lnb->page to point to the page from the per-thread page
328 * pool (bug 5137), initialize page.
330 LASSERT(lnb->page != NULL);
332 if (inode->i_size <= rnb->offset)
333 /* If there's no more data, abort early. lnb->rc == 0,
334 * so it's easy to detect later. */
337 filter_alloc_dio_page(obd, inode, lnb);
339 if (inode->i_size < lnb->offset + lnb->len - 1)
340 lnb->rc = inode->i_size - lnb->offset;
344 tot_bytes += lnb->rc;
346 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
349 fsfilt_check_slow(obd, now, obd_timeout, "start_page_read");
351 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
352 exp, NULL, NULL, NULL);
356 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
358 filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
364 filter_free_dio_pages(objcount, obj, niocount, res);
370 filter_iobuf_put(&obd->u.filter, iobuf, oti);
372 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
374 CERROR("io error %d\n", rc);
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
386 * Caller must hold obd_osfs_lock. */
387 static int filter_grant_check(struct obd_export *exp, int objcount,
388 struct fsfilt_objinfo *fso, int niocount,
389 struct niobuf_remote *rnb,
390 struct niobuf_local *lnb, obd_size *left,
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, mask = D_CACHE;
398 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
400 for (obj = 0; obj < objcount; obj++) {
401 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
404 /* should match the code in osc_exit_cache */
406 bytes += rnb[n].offset & (blocksize - 1);
407 tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
409 bytes += blocksize - tmp;
411 if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
412 if (fed->fed_grant < used + bytes) {
414 "%s: cli %s/%p claims %ld+%d "
415 "GRANT, real grant %lu idx %d\n",
416 exp->exp_obd->obd_name,
417 exp->exp_client_uuid.uuid, exp,
418 used, bytes, fed->fed_grant, n);
422 rnb[n].flags |= OBD_BRW_GRANTED;
423 lnb[n].lnb_grant_used = bytes;
424 CDEBUG(0, "idx %d used=%lu\n", n, used);
429 if (*left > ungranted) {
430 /* if enough space, pretend it was granted */
432 rnb[n].flags |= OBD_BRW_GRANTED;
433 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
438 /* We can't check for already-mapped blocks here, as
439 * it requires dropping the osfs lock to do the bmap.
440 * Instead, we return ENOSPC and in that case we need
441 * to go through and verify if all of the blocks not
442 * marked BRW_GRANTED are already mapped and we can
443 * ignore this error. */
445 rnb[n].flags &= ~OBD_BRW_GRANTED;
446 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
447 exp->exp_obd->obd_name,
448 exp->exp_client_uuid.uuid, exp, n, bytes);
452 /* Now substract what client have used already. We don't subtract
453 * this from the tot_granted yet, so that other client's can't grab
454 * that space before we have actually allocated our blocks. That
455 * happens in filter_grant_commit() after the writes are done. */
457 fed->fed_grant -= used;
458 fed->fed_pending += used;
459 exp->exp_obd->u.filter.fo_tot_pending += used;
462 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
463 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
464 ungranted, fed->fed_grant, fed->fed_dirty);
466 /* Rough calc in case we don't refresh cached statfs data */
467 using = (used + ungranted + 1 ) >>
468 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
469 if (exp->exp_obd->obd_osfs.os_bavail > using)
470 exp->exp_obd->obd_osfs.os_bavail -= using;
472 exp->exp_obd->obd_osfs.os_bavail = 0;
474 if (fed->fed_dirty < used) {
475 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
476 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
477 used, fed->fed_dirty);
478 used = fed->fed_dirty;
480 exp->exp_obd->u.filter.fo_tot_dirty -= used;
481 fed->fed_dirty -= used;
483 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
484 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
485 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
486 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
487 spin_unlock(&exp->exp_obd->obd_osfs_lock);
493 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
494 * on mulitple inodes. That isn't all, because there still exists the
495 * possibility of a truncate starting a new transaction while holding the ext3
496 * rwsem = write while some writes (which have started their transactions here)
497 * blocking on the ext3 rwsem = read => lock inversion.
499 * The handling gets very ugly when dealing with locked pages. It may be easier
500 * to just get rid of the locked page code (which has problems of its own) and
501 * either discover we do not need it anymore (i.e. it was a symptom of another
502 * bug) or ensure we get the page locks in an appropriate order. */
503 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
504 int objcount, struct obd_ioobj *obj,
505 int niocount, struct niobuf_remote *nb,
506 struct niobuf_local *res,
507 struct obd_trans_info *oti)
509 struct lvfs_run_ctxt saved;
510 struct niobuf_remote *rnb;
511 struct niobuf_local *lnb = res;
512 struct fsfilt_objinfo fso;
513 struct filter_mod_data *fmd;
514 struct dentry *dentry = NULL;
517 unsigned long now = jiffies;
518 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
520 LASSERT(objcount == 1);
521 LASSERT(obj->ioo_bufcnt > 0);
523 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
524 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
526 GOTO(cleanup, rc = PTR_ERR(iobuf));
529 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
532 GOTO(cleanup, rc = PTR_ERR(dentry));
535 if (dentry->d_inode == NULL) {
536 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
537 exp->exp_obd->obd_name, obj->ioo_id);
538 GOTO(cleanup, rc = -ENOENT);
541 fso.fso_dentry = dentry;
542 fso.fso_bufcnt = obj->ioo_bufcnt;
544 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "preprw_write setup");
546 /* Don't update inode timestamps if this write is older than a
547 * setattr which modifies the timestamps. b=10150 */
548 /* XXX when we start having persistent reservations this needs to
549 * be changed to filter_fmd_get() to create the fmd if it doesn't
550 * already exist so we can store the reservation handle there. */
551 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
553 spin_lock(&exp->exp_obd->obd_osfs_lock);
555 filter_grant_incoming(exp, oa);
556 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
557 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
560 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
561 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
565 left = filter_grant_space_left(exp);
567 rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
568 &left, dentry->d_inode);
570 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
571 * for setting UID/GID and fid EA in first write time. */
572 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
573 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
574 oa->o_valid |= OBD_MD_FLGRANT;
577 spin_unlock(&exp->exp_obd->obd_osfs_lock);
578 filter_fmd_put(exp, fmd);
583 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
585 /* We still set up for ungranted pages so that granted pages
586 * can be written to disk as they were promised, and portals
587 * needs to keep the pages all aligned properly. */
588 lnb->dentry = dentry;
589 lnb->offset = rnb->offset;
591 lnb->flags = rnb->flags;
594 * ost_brw_write()->ost_nio_pages_get() already initialized
595 * lnb->page to point to the page from the per-thread page
596 * pool (bug 5137), initialize page.
598 LASSERT(lnb->page != NULL);
599 if (lnb->len != PAGE_SIZE) {
600 memset(kmap(lnb->page) + lnb->len,
601 0, PAGE_SIZE - lnb->len);
604 lnb->page->index = lnb->offset >> PAGE_SHIFT;
608 /* If the filter writes a partial page, then has the file
609 * extended, the client will read in the whole page. the
610 * filter has to be careful to zero the rest of the partial
611 * page on disk. we do it by hand for partial extending
612 * writes, send_bio() is responsible for zeroing pages when
613 * asked to read unmapped blocks -- brw_kiovec() does this. */
614 if (lnb->len != PAGE_SIZE) {
617 maxidx = ((dentry->d_inode->i_size + PAGE_SIZE - 1) >>
619 if (maxidx >= lnb->page->index) {
620 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
621 LPU64" flg %x before EOF %llu\n",
622 lnb->len, lnb->offset,lnb->flags,
623 dentry->d_inode->i_size);
624 filter_iobuf_add_page(exp->exp_obd, iobuf,
629 char *p = kmap(lnb->page);
631 off = lnb->offset & ~PAGE_MASK;
634 off = (lnb->offset + lnb->len) & ~PAGE_MASK;
636 memset(p + off, 0, PAGE_SIZE - off);
641 tot_bytes += lnb->len;
644 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
647 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "start_page_write");
649 lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
653 switch(cleanup_phase) {
656 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
658 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
663 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
665 spin_lock(&exp->exp_obd->obd_osfs_lock);
667 filter_grant_incoming(exp, oa);
668 spin_unlock(&exp->exp_obd->obd_osfs_lock);
669 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
676 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
677 int objcount, struct obd_ioobj *obj, int niocount,
678 struct niobuf_remote *nb, struct niobuf_local *res,
679 struct obd_trans_info *oti)
681 if (cmd == OBD_BRW_WRITE)
682 return filter_preprw_write(cmd, exp, oa, objcount, obj,
683 niocount, nb, res, oti);
684 if (cmd == OBD_BRW_READ)
685 return filter_preprw_read(cmd, exp, oa, objcount, obj,
686 niocount, nb, res, oti);
691 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
697 (inode->i_size > filter->fo_readcache_max_filesize))
700 /* drop from cache like truncate_list_pages() */
701 if (drop && !TryLockPage(page)) {
703 ll_truncate_complete_page(page);
706 page_cache_release(page);
709 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
710 int objcount, struct obd_ioobj *obj,
711 int niocount, struct niobuf_local *res,
712 struct obd_trans_info *oti, int rc)
714 struct inode *inode = NULL;
715 struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
716 struct ldlm_resource *resource = NULL;
717 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
720 /* If oa != NULL then filter_preprw_read updated the inode atime
721 * and we should update the lvb so that other glimpses will also
722 * get the updated value. bug 5972 */
723 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
724 resource = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
726 if (resource != NULL) {
727 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
728 ldlm_resource_putref(resource);
732 if (res->dentry != NULL)
733 inode = res->dentry->d_inode;
735 filter_free_dio_pages(objcount, obj, niocount, res);
737 if (res->dentry != NULL)
742 void flip_into_page_cache(struct inode *inode, struct page *new_page)
744 struct page *old_page;
748 /* the dlm is protecting us from read/write concurrency, so we
749 * expect this find_lock_page to return quickly. even if we
750 * race with another writer it won't be doing much work with
751 * the page locked. we do this 'cause t_c_p expects a
752 * locked page, and it wants to grab the pagecache lock
754 old_page = find_lock_page(inode->i_mapping, new_page->index);
756 ll_truncate_complete_page(old_page);
757 unlock_page(old_page);
758 page_cache_release(old_page);
761 #if 0 /* this should be a /proc tunable someday */
762 /* racing o_directs (no locking ioctl) could race adding
763 * their pages, so we repeat the page invalidation unless
764 * we successfully added our new page */
765 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
767 page_hash(inode->i_mapping,
770 /* add_to_page_cache clears uptodate|dirty and locks
772 SetPageUptodate(new_page);
773 unlock_page(new_page);
781 void filter_grant_commit(struct obd_export *exp, int niocount,
782 struct niobuf_local *res)
784 struct filter_obd *filter = &exp->exp_obd->u.filter;
785 struct niobuf_local *lnb = res;
786 unsigned long pending = 0;
789 spin_lock(&exp->exp_obd->obd_osfs_lock);
790 for (i = 0, lnb = res; i < niocount; i++, lnb++)
791 pending += lnb->lnb_grant_used;
793 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
794 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
795 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
796 exp->exp_filter_data.fed_pending, pending);
797 exp->exp_filter_data.fed_pending -= pending;
798 LASSERTF(filter->fo_tot_granted >= pending,
799 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
800 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
801 exp->exp_obd->u.filter.fo_tot_granted, pending);
802 filter->fo_tot_granted -= pending;
803 LASSERTF(filter->fo_tot_pending >= pending,
804 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
805 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
806 filter->fo_tot_pending, pending);
807 filter->fo_tot_pending -= pending;
809 spin_unlock(&exp->exp_obd->obd_osfs_lock);
812 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
813 int objcount, struct obd_ioobj *obj, int niocount,
814 struct niobuf_local *res, struct obd_trans_info *oti,
817 if (cmd == OBD_BRW_WRITE)
818 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
820 if (cmd == OBD_BRW_READ)
821 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
827 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
828 obd_count oa_bufs, struct brw_page *pga,
829 struct obd_trans_info *oti)
831 struct obd_ioobj ioo;
832 struct niobuf_local *lnb;
833 struct niobuf_remote *rnb;
838 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
839 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
841 if (lnb == NULL || rnb == NULL)
842 GOTO(out, ret = -ENOMEM);
844 for (i = 0; i < oa_bufs; i++) {
845 lnb[i].page = pga[i].pg;
846 rnb[i].offset = pga[i].off;
847 rnb[i].len = pga[i].count;
850 obdo_to_ioobj(oinfo->oi_oa, &ioo);
851 ioo.ioo_bufcnt = oa_bufs;
853 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
854 oa_bufs, rnb, lnb, oti);
858 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
859 oa_bufs, lnb, oti, ret);
863 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
865 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));