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 #ifndef AUTOCONF_INCLUDED
33 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/pagemap.h> // XXX kill me soon
37 #include <linux/version.h>
39 #include <obd_class.h>
41 #include <lustre_fsfilt.h>
42 #include "filter_internal.h"
44 int *obdfilter_created_scratchpad;
46 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
47 struct niobuf_local *lnb)
51 LASSERT(lnb->page != NULL);
55 POISON_PAGE(page, 0xf1);
56 if (lnb->len != CFS_PAGE_SIZE) {
57 memset(kmap(page) + lnb->len, 0, CFS_PAGE_SIZE - lnb->len);
61 page->index = lnb->offset >> CFS_PAGE_SHIFT;
66 static void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
67 int niocount, struct niobuf_local *res)
71 for (i = 0; i < objcount; i++, obj++) {
72 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++)
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)
82 struct filter_export_data *fed;
83 struct obd_device *obd = exp->exp_obd;
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 /* 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. */
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);
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)
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);
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);
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);
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.
143 * Caller must hold obd_osfs_lock. */
144 obd_size filter_grant_space_left(struct obd_export *exp)
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;
151 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
153 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
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 */
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;
167 left = 0 /* << blockbits */;
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");
175 if (left >= tot_granted) {
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);
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);
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.
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)
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;
208 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
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.
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);
229 /* Allow >FILTER_GRANT_CHUNK size when clients
230 * reconnect due to a server reboot.
232 if ((grant > FILTER_GRANT_CHUNK) &&
233 (!obd->obd_recovering))
234 grant = FILTER_GRANT_CHUNK;
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
241 obd->obd_name, exp->exp_client_uuid.uuid,
242 exp, fed->fed_grant, want,current_grant);
243 spin_unlock(&obd->obd_osfs_lock);
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);
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);
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)
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;
276 int rc = 0, i, tot_bytes = 0;
277 unsigned long now = jiffies;
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);
286 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
291 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
292 spin_lock(&obd->obd_osfs_lock);
293 filter_grant_incoming(exp, oa);
296 spin_unlock(&obd->obd_osfs_lock);
299 iobuf = filter_iobuf_get(&obd->u.filter, oti);
301 RETURN(PTR_ERR(iobuf));
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);
311 inode = dentry->d_inode;
313 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
314 fsfilt_check_slow(obd, now, "preprw_read setup");
316 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
318 lnb->dentry = dentry;
319 lnb->offset = rnb->offset;
321 lnb->flags = rnb->flags;
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.
328 LASSERT(lnb->page != NULL);
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. */
335 filter_alloc_dio_page(obd, inode, lnb);
337 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
338 lnb->rc = i_size_read(inode) - lnb->offset;
342 tot_bytes += lnb->rc;
344 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
347 fsfilt_check_slow(obd, now, "start_page_read");
349 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
350 exp, NULL, NULL, NULL);
354 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
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);
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, 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,
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;
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 (oa->o_valid & OBD_MD_FLGRANT)) {
413 if (fed->fed_grant < used + bytes) {
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);
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 + bytes) {
430 /* if enough space, pretend it was granted */
432 rnb[n].flags |= OBD_BRW_GRANTED;
433 lnb[n].lnb_grant_used = bytes;
434 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
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. */
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);
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. */
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;
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);
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;
474 exp->exp_obd->obd_osfs.os_bavail = 0;
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;
482 exp->exp_obd->u.filter.fo_tot_dirty -= used;
483 fed->fed_dirty -= used;
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);
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.
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)
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;
520 unsigned long now = jiffies;
521 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
523 LASSERT(objcount == 1);
524 LASSERT(obj->ioo_bufcnt > 0);
526 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
531 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
532 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
534 GOTO(cleanup, rc = PTR_ERR(iobuf));
537 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
540 GOTO(cleanup, rc = PTR_ERR(dentry));
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);
549 fso.fso_dentry = dentry;
550 fso.fso_bufcnt = obj->ioo_bufcnt;
552 fsfilt_check_slow(exp->exp_obd, now, "preprw_write setup");
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);
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 |
568 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
569 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
572 left = filter_grant_space_left(exp);
574 rc = filter_grant_check(exp, oa, objcount, &fso, niocount, nb, res,
575 &left, dentry->d_inode);
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);
582 spin_unlock(&exp->exp_obd->obd_osfs_lock);
583 filter_fmd_put(exp, fmd);
588 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
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;
596 lnb->flags = rnb->flags;
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.
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);
609 lnb->page->index = lnb->offset >> CFS_PAGE_SHIFT;
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) {
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,
634 char *p = kmap(lnb->page);
636 off = lnb->offset & ~CFS_PAGE_MASK;
639 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
641 memset(p + off, 0, CFS_PAGE_SIZE - off);
646 tot_bytes += lnb->len;
649 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
652 fsfilt_check_slow(exp->exp_obd, now, "start_page_write");
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);
659 switch(cleanup_phase) {
662 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
664 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
669 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
671 spin_lock(&exp->exp_obd->obd_osfs_lock);
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);
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)
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);
697 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
703 (i_size_read(inode) > filter->fo_readcache_max_filesize))
706 /* drop from cache like truncate_list_pages() */
707 if (drop && !TryLockPage(page)) {
709 ll_truncate_complete_page(page);
712 page_cache_release(page);
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)
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;
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);
733 if (resource != NULL) {
734 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
735 ldlm_resource_putref(resource);
739 if (res->dentry != NULL)
740 inode = res->dentry->d_inode;
742 filter_free_dio_pages(objcount, obj, niocount, res);
744 if (res->dentry != NULL)
749 void flip_into_page_cache(struct inode *inode, struct page *new_page)
751 struct page *old_page;
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
761 old_page = find_lock_page(inode->i_mapping, new_page->index);
763 ll_truncate_complete_page(old_page);
764 unlock_page(old_page);
765 page_cache_release(old_page);
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,
774 page_hash(inode->i_mapping,
777 /* add_to_page_cache clears uptodate|dirty and locks
779 SetPageUptodate(new_page);
780 unlock_page(new_page);
788 void filter_grant_commit(struct obd_export *exp, int niocount,
789 struct niobuf_local *res)
791 struct filter_obd *filter = &exp->exp_obd->u.filter;
792 struct niobuf_local *lnb = res;
793 unsigned long pending = 0;
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;
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;
816 spin_unlock(&exp->exp_obd->obd_osfs_lock);
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,
824 if (cmd == OBD_BRW_WRITE)
825 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
827 if (cmd == OBD_BRW_READ)
828 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
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)
838 struct obd_ioobj ioo;
839 struct niobuf_local *lnb;
840 struct niobuf_remote *rnb;
845 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
846 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
848 if (lnb == NULL || rnb == NULL)
849 GOTO(out, ret = -ENOMEM);
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;
857 obdo_to_ioobj(oinfo->oi_oa, &ioo);
858 ioo.ioo_bufcnt = oa_bufs;
860 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
861 oa_bufs, rnb, lnb, oti, oinfo_capa(oinfo));
865 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
866 oa_bufs, lnb, oti, ret);
870 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
872 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));