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>
40 #include <lustre_fsfilt.h>
41 #include "filter_internal.h"
43 int *obdfilter_created_scratchpad;
45 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
46 struct niobuf_local *lnb)
50 LASSERT(lnb->page != NULL);
54 POISON_PAGE(page, 0xf1);
55 if (lnb->len != CFS_PAGE_SIZE) {
56 memset(kmap(page) + lnb->len, 0, CFS_PAGE_SIZE - lnb->len);
60 page->index = lnb->offset >> CFS_PAGE_SHIFT;
65 static void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
66 int niocount, struct niobuf_local *res)
70 for (i = 0; i < objcount; i++, obj++) {
71 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++)
76 /* Grab the dirty and seen grant announcements from the incoming obdo.
77 * We will later calculate the clients new grant and return it.
78 * Caller must hold osfs lock */
79 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
81 struct filter_export_data *fed;
82 struct obd_device *obd = exp->exp_obd;
85 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
87 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
88 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
89 oa->o_valid &= ~OBD_MD_FLGRANT;
94 fed = &exp->exp_filter_data;
96 /* Add some margin, since there is a small race if other RPCs arrive
97 * out-or-order and have already consumed some grant. We want to
98 * leave this here in case there is a large error in accounting. */
100 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
101 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
102 oa->o_dropped, fed->fed_grant);
104 /* Update our accounting now so that statfs takes it into account.
105 * Note that fed_dirty is only approximate and can become incorrect
106 * if RPCs arrive out-of-order. No important calculations depend
107 * on fed_dirty however, but we must check sanity to not assert. */
108 if ((long long)oa->o_dirty < 0)
110 else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
111 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
112 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
113 if (fed->fed_grant < oa->o_dropped) {
114 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
115 obd->obd_name, exp->exp_client_uuid.uuid, exp,
116 oa->o_dropped, fed->fed_grant);
119 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
120 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
121 obd->obd_name, exp->exp_client_uuid.uuid, exp,
122 oa->o_dropped, obd->u.filter.fo_tot_granted);
125 obd->u.filter.fo_tot_granted -= oa->o_dropped;
126 fed->fed_grant -= oa->o_dropped;
127 fed->fed_dirty = oa->o_dirty;
128 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
129 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
130 obd->obd_name, exp->exp_client_uuid.uuid, exp,
131 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
132 spin_unlock(&obd->obd_osfs_lock);
138 /* Figure out how much space is available between what we've granted
139 * and what remains in the filesystem. Compensate for ext3 indirect
140 * block overhead when computing how much free space is left ungranted.
142 * Caller must hold obd_osfs_lock. */
143 obd_size filter_grant_space_left(struct obd_export *exp)
145 struct obd_device *obd = exp->exp_obd;
146 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
147 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
148 int rc, statfs_done = 0;
150 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
152 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
154 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
155 cfs_time_current_64() + HZ);
156 if (rc) /* N.B. statfs can't really fail */
161 avail = obd->obd_osfs.os_bavail;
162 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
163 if (left > GRANT_FOR_LLOG(obd)) {
164 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
166 left = 0 /* << blockbits */;
169 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
170 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
174 if (left >= tot_granted) {
177 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
178 CERROR("%s: cli %s/%p grant "LPU64" > available "
179 LPU64" and pending "LPU64"\n", obd->obd_name,
180 exp->exp_client_uuid.uuid, exp, tot_granted,
181 left, obd->u.filter.fo_tot_pending);
186 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
187 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
188 exp->exp_client_uuid.uuid, exp,
189 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
190 tot_granted, left, obd->u.filter.fo_tot_pending);
195 /* Calculate how much grant space to allocate to this client, based on how
196 * much space is currently free and how much of that is already granted.
198 * Caller must hold obd_osfs_lock. */
199 long filter_grant(struct obd_export *exp, obd_size current_grant,
200 obd_size want, obd_size fs_space_left)
202 struct obd_device *obd = exp->exp_obd;
203 struct filter_export_data *fed = &exp->exp_filter_data;
204 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
207 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
209 /* Grant some fraction of the client's requested grant space so that
210 * they are not always waiting for write credits (not all of it to
211 * avoid overgranting in face of multiple RPCs in flight). This
212 * essentially will be able to control the OSC_MAX_RIF for a client.
214 * If we do have a large disparity between what the client thinks it
215 * has and what we think it has, don't grant very much and let the
216 * client consume its grant first. Either it just has lots of RPCs
217 * in flight, or it was evicted and its grants will soon be used up. */
218 if (want > 0x7fffffff) {
219 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
220 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
221 } else if (current_grant < want &&
222 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
223 grant = min((want >> blockbits),
224 (fs_space_left >> blockbits) / 8);
228 /* Allow >FILTER_GRANT_CHUNK size when clients
229 * reconnect due to a server reboot.
231 if ((grant > FILTER_GRANT_CHUNK) &&
232 (!obd->obd_recovering))
233 grant = FILTER_GRANT_CHUNK;
235 obd->u.filter.fo_tot_granted += grant;
236 fed->fed_grant += grant;
237 if (fed->fed_grant < 0) {
238 CERROR("%s: cli %s/%p grant %ld want "LPU64
240 obd->obd_name, exp->exp_client_uuid.uuid,
241 exp, fed->fed_grant, want,current_grant);
242 spin_unlock(&obd->obd_osfs_lock);
249 "%s: cli %s/%p wants: "LPU64" current grant "LPU64
250 " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
251 exp, want, current_grant, grant);
253 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
254 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
255 exp, obd->u.filter.fo_tot_dirty,
256 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
261 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
262 int objcount, struct obd_ioobj *obj,
263 int niocount, struct niobuf_remote *nb,
264 struct niobuf_local *res,
265 struct obd_trans_info *oti,
266 struct lustre_capa *capa)
268 struct obd_device *obd = exp->exp_obd;
269 struct lvfs_run_ctxt saved;
270 struct niobuf_remote *rnb;
271 struct niobuf_local *lnb;
272 struct dentry *dentry = NULL;
275 int rc = 0, i, tot_bytes = 0;
276 unsigned long now = jiffies;
279 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
280 * When we do this function's dentry cleanup will need to be fixed.
281 * These values are verified in ost_brw_write() from the wire. */
282 LASSERTF(objcount == 1, "%d\n", objcount);
283 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
285 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
290 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
291 spin_lock(&obd->obd_osfs_lock);
292 filter_grant_incoming(exp, oa);
295 spin_unlock(&obd->obd_osfs_lock);
298 iobuf = filter_iobuf_get(&obd->u.filter, oti);
300 RETURN(PTR_ERR(iobuf));
302 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
303 dentry = filter_oa2dentry(obd, oa);
304 if (IS_ERR(dentry)) {
305 rc = PTR_ERR(dentry);
310 inode = dentry->d_inode;
312 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
313 fsfilt_check_slow(obd, now, obd_timeout, "preprw_read setup");
315 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
317 lnb->dentry = dentry;
318 lnb->offset = rnb->offset;
320 lnb->flags = rnb->flags;
323 * ost_brw_write()->ost_nio_pages_get() already initialized
324 * lnb->page to point to the page from the per-thread page
325 * pool (bug 5137), initialize page.
327 LASSERT(lnb->page != NULL);
329 if (i_size_read(inode) <= rnb->offset)
330 /* If there's no more data, abort early. lnb->rc == 0,
331 * so it's easy to detect later. */
334 filter_alloc_dio_page(obd, inode, lnb);
336 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
337 lnb->rc = i_size_read(inode) - lnb->offset;
341 tot_bytes += lnb->rc;
343 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
346 fsfilt_check_slow(obd, now, obd_timeout, "start_page_read");
348 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
349 exp, NULL, NULL, NULL);
353 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
355 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
356 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
357 LPROC_FILTER_READ_BYTES, tot_bytes);
363 filter_free_dio_pages(objcount, obj, niocount, res);
369 filter_iobuf_put(&obd->u.filter, iobuf, oti);
371 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
373 CERROR("io error %d\n", rc);
378 /* When clients have dirtied as much space as they've been granted they
379 * fall through to sync writes. These sync writes haven't been expressed
380 * in grants and need to error with ENOSPC when there isn't room in the
381 * filesystem for them after grants are taken into account. However,
382 * writeback of the dirty data that was already granted space can write
385 * Caller must hold obd_osfs_lock. */
386 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
387 int objcount, struct fsfilt_objinfo *fso,
388 int niocount, struct niobuf_remote *rnb,
389 struct niobuf_local *lnb, obd_size *left,
392 struct filter_export_data *fed = &exp->exp_filter_data;
393 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
394 unsigned long used = 0, ungranted = 0, using;
395 int i, rc = -ENOSPC, obj, n = 0;
397 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
399 for (obj = 0; obj < objcount; obj++) {
400 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
403 /* should match the code in osc_exit_cache */
405 bytes += rnb[n].offset & (blocksize - 1);
406 tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
408 bytes += blocksize - tmp;
410 if ((rnb[n].flags & OBD_BRW_FROM_GRANT) &&
411 (oa->o_valid & OBD_MD_FLGRANT)) {
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);
421 rnb[n].flags |= OBD_BRW_GRANTED;
422 lnb[n].lnb_grant_used = bytes;
423 CDEBUG(0, "idx %d used=%lu\n", n, used);
428 if (*left > ungranted + bytes) {
429 /* if enough space, pretend it was granted */
431 rnb[n].flags |= OBD_BRW_GRANTED;
432 lnb[n].lnb_grant_used = bytes;
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 + ungranted;
459 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
460 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
463 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
464 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
465 ungranted, fed->fed_grant, fed->fed_dirty);
467 /* Rough calc in case we don't refresh cached statfs data */
468 using = (used + ungranted + 1 ) >>
469 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
470 if (exp->exp_obd->obd_osfs.os_bavail > using)
471 exp->exp_obd->obd_osfs.os_bavail -= using;
473 exp->exp_obd->obd_osfs.os_bavail = 0;
475 if (fed->fed_dirty < used) {
476 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
477 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
478 used, fed->fed_dirty);
479 used = fed->fed_dirty;
481 exp->exp_obd->u.filter.fo_tot_dirty -= used;
482 fed->fed_dirty -= used;
484 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
485 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
486 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
487 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
488 spin_unlock(&exp->exp_obd->obd_osfs_lock);
494 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
495 * on mulitple inodes. That isn't all, because there still exists the
496 * possibility of a truncate starting a new transaction while holding the ext3
497 * rwsem = write while some writes (which have started their transactions here)
498 * blocking on the ext3 rwsem = read => lock inversion.
500 * The handling gets very ugly when dealing with locked pages. It may be easier
501 * to just get rid of the locked page code (which has problems of its own) and
502 * either discover we do not need it anymore (i.e. it was a symptom of another
503 * bug) or ensure we get the page locks in an appropriate order. */
504 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
505 int objcount, struct obd_ioobj *obj,
506 int niocount, struct niobuf_remote *nb,
507 struct niobuf_local *res,
508 struct obd_trans_info *oti,
509 struct lustre_capa *capa)
511 struct lvfs_run_ctxt saved;
512 struct niobuf_remote *rnb;
513 struct niobuf_local *lnb = res;
514 struct fsfilt_objinfo fso;
515 struct filter_mod_data *fmd;
516 struct dentry *dentry = NULL;
519 unsigned long now = jiffies;
520 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
522 LASSERT(objcount == 1);
523 LASSERT(obj->ioo_bufcnt > 0);
525 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
530 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
531 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
533 GOTO(cleanup, rc = PTR_ERR(iobuf));
536 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
539 GOTO(cleanup, rc = PTR_ERR(dentry));
542 if (dentry->d_inode == NULL) {
543 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
544 exp->exp_obd->obd_name, obj->ioo_id);
545 GOTO(cleanup, rc = -ENOENT);
548 fso.fso_dentry = dentry;
549 fso.fso_bufcnt = obj->ioo_bufcnt;
551 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "preprw_write setup");
553 /* Don't update inode timestamps if this write is older than a
554 * setattr which modifies the timestamps. b=10150 */
555 /* XXX when we start having persistent reservations this needs to
556 * be changed to filter_fmd_get() to create the fmd if it doesn't
557 * already exist so we can store the reservation handle there. */
558 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
561 spin_lock(&exp->exp_obd->obd_osfs_lock);
562 filter_grant_incoming(exp, oa);
563 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
564 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
567 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
568 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
571 left = filter_grant_space_left(exp);
573 rc = filter_grant_check(exp, oa, objcount, &fso, niocount, nb, res,
574 &left, dentry->d_inode);
576 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
577 * for setting UID/GID and fid EA in first write time. */
578 if (oa->o_valid & OBD_MD_FLGRANT)
579 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
581 spin_unlock(&exp->exp_obd->obd_osfs_lock);
582 filter_fmd_put(exp, fmd);
587 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
589 /* We still set up for ungranted pages so that granted pages
590 * can be written to disk as they were promised, and portals
591 * needs to keep the pages all aligned properly. */
592 lnb->dentry = dentry;
593 lnb->offset = rnb->offset;
595 lnb->flags = rnb->flags;
598 * ost_brw_write()->ost_nio_pages_get() already initialized
599 * lnb->page to point to the page from the per-thread page
600 * pool (bug 5137), initialize page.
602 LASSERT(lnb->page != NULL);
603 if (lnb->len != CFS_PAGE_SIZE) {
604 memset(kmap(lnb->page) + lnb->len,
605 0, CFS_PAGE_SIZE - lnb->len);
608 lnb->page->index = lnb->offset >> CFS_PAGE_SHIFT;
612 /* If the filter writes a partial page, then has the file
613 * extended, the client will read in the whole page. the
614 * filter has to be careful to zero the rest of the partial
615 * page on disk. we do it by hand for partial extending
616 * writes, send_bio() is responsible for zeroing pages when
617 * asked to read unmapped blocks -- brw_kiovec() does this. */
618 if (lnb->len != CFS_PAGE_SIZE) {
621 maxidx = ((i_size_read(dentry->d_inode) +
622 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
623 if (maxidx >= lnb->page->index) {
624 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
625 LPU64" flg %x before EOF %llu\n",
626 lnb->len, lnb->offset,lnb->flags,
627 i_size_read(dentry->d_inode));
628 filter_iobuf_add_page(exp->exp_obd, iobuf,
633 char *p = kmap(lnb->page);
635 off = lnb->offset & ~CFS_PAGE_MASK;
638 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
640 memset(p + off, 0, CFS_PAGE_SIZE - off);
645 tot_bytes += lnb->len;
648 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
651 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "start_page_write");
653 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
654 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
655 LPROC_FILTER_WRITE_BYTES, tot_bytes);
658 switch(cleanup_phase) {
661 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
663 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
668 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
670 spin_lock(&exp->exp_obd->obd_osfs_lock);
672 filter_grant_incoming(exp, oa);
673 spin_unlock(&exp->exp_obd->obd_osfs_lock);
674 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
681 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
682 int objcount, struct obd_ioobj *obj, int niocount,
683 struct niobuf_remote *nb, struct niobuf_local *res,
684 struct obd_trans_info *oti, struct lustre_capa *capa)
686 if (cmd == OBD_BRW_WRITE)
687 return filter_preprw_write(cmd, exp, oa, objcount, obj,
688 niocount, nb, res, oti, capa);
689 if (cmd == OBD_BRW_READ)
690 return filter_preprw_read(cmd, exp, oa, objcount, obj,
691 niocount, nb, res, oti, capa);
696 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
702 (i_size_read(inode) > filter->fo_readcache_max_filesize))
705 /* drop from cache like truncate_list_pages() */
706 if (drop && !TryLockPage(page)) {
708 ll_truncate_complete_page(page);
711 page_cache_release(page);
714 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
715 int objcount, struct obd_ioobj *obj,
716 int niocount, struct niobuf_local *res,
717 struct obd_trans_info *oti, int rc)
719 struct inode *inode = NULL;
720 struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
722 struct ldlm_resource *resource = NULL;
723 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
726 /* If oa != NULL then filter_preprw_read updated the inode atime
727 * and we should update the lvb so that other glimpses will also
728 * get the updated value. bug 5972 */
729 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
730 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
732 if (resource != NULL) {
733 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
734 ldlm_resource_putref(resource);
738 if (res->dentry != NULL)
739 inode = res->dentry->d_inode;
741 filter_free_dio_pages(objcount, obj, niocount, res);
743 if (res->dentry != NULL)
748 void flip_into_page_cache(struct inode *inode, struct page *new_page)
750 struct page *old_page;
754 /* the dlm is protecting us from read/write concurrency, so we
755 * expect this find_lock_page to return quickly. even if we
756 * race with another writer it won't be doing much work with
757 * the page locked. we do this 'cause t_c_p expects a
758 * locked page, and it wants to grab the pagecache lock
760 old_page = find_lock_page(inode->i_mapping, new_page->index);
762 ll_truncate_complete_page(old_page);
763 unlock_page(old_page);
764 page_cache_release(old_page);
767 #if 0 /* this should be a /proc tunable someday */
768 /* racing o_directs (no locking ioctl) could race adding
769 * their pages, so we repeat the page invalidation unless
770 * we successfully added our new page */
771 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
773 page_hash(inode->i_mapping,
776 /* add_to_page_cache clears uptodate|dirty and locks
778 SetPageUptodate(new_page);
779 unlock_page(new_page);
787 void filter_grant_commit(struct obd_export *exp, int niocount,
788 struct niobuf_local *res)
790 struct filter_obd *filter = &exp->exp_obd->u.filter;
791 struct niobuf_local *lnb = res;
792 unsigned long pending = 0;
795 spin_lock(&exp->exp_obd->obd_osfs_lock);
796 for (i = 0, lnb = res; i < niocount; i++, lnb++)
797 pending += lnb->lnb_grant_used;
799 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
800 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
801 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
802 exp->exp_filter_data.fed_pending, pending);
803 exp->exp_filter_data.fed_pending -= pending;
804 LASSERTF(filter->fo_tot_granted >= pending,
805 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
806 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
807 exp->exp_obd->u.filter.fo_tot_granted, pending);
808 filter->fo_tot_granted -= pending;
809 LASSERTF(filter->fo_tot_pending >= pending,
810 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
811 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
812 filter->fo_tot_pending, pending);
813 filter->fo_tot_pending -= pending;
815 spin_unlock(&exp->exp_obd->obd_osfs_lock);
818 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
819 int objcount, struct obd_ioobj *obj, int niocount,
820 struct niobuf_local *res, struct obd_trans_info *oti,
823 if (cmd == OBD_BRW_WRITE)
824 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
826 if (cmd == OBD_BRW_READ)
827 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
833 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
834 obd_count oa_bufs, struct brw_page *pga,
835 struct obd_trans_info *oti)
837 struct obd_ioobj ioo;
838 struct niobuf_local *lnb;
839 struct niobuf_remote *rnb;
844 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
845 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
847 if (lnb == NULL || rnb == NULL)
848 GOTO(out, ret = -ENOMEM);
850 for (i = 0; i < oa_bufs; i++) {
851 lnb[i].page = pga[i].pg;
852 rnb[i].offset = pga[i].off;
853 rnb[i].len = pga[i].count;
856 obdo_to_ioobj(oinfo->oi_oa, &ioo);
857 ioo.ioo_bufcnt = oa_bufs;
859 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
860 oa_bufs, rnb, lnb, oti, oinfo_capa(oinfo));
864 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
865 oa_bufs, lnb, oti, ret);
869 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
871 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));